diff --git a/apps/emqx/src/emqx_config.erl b/apps/emqx/src/emqx_config.erl index ecb843ed5..b7a4f23b5 100644 --- a/apps/emqx/src/emqx_config.erl +++ b/apps/emqx/src/emqx_config.erl @@ -270,28 +270,37 @@ init_load(SchemaMod, Conf) when is_list(Conf) orelse is_binary(Conf) -> end; init_load(SchemaMod, RawConf) when is_map(RawConf) -> ok = save_schema_mod_and_names(SchemaMod), - %% check and save configs - {_AppEnvs, CheckedConf} = check_config(SchemaMod, RawConf), + %% check configs agains the schema, with environment variables applied on top + {_AppEnvs, CheckedConf} = + check_config(SchemaMod, RawConf, #{apply_override_envs => true}), %% fill default values for raw config - Opts = #{only_fill_defaults => true, - logger => fun(_, _) -> ok end, %% everything should have been logged already - nullable => true %% TODO: evil, remove, nullable should be declared in schema - }, - RawConfWithDefaults = hocon_schema:check_plain(SchemaMod, RawConf, Opts), + RawConfWithEnvs = merge_envs(SchemaMod, RawConf), RootNames = get_root_names(), ok = save_to_config_map(maps:with(get_atom_root_names(), CheckedConf), - maps:with(RootNames, RawConfWithDefaults)). + maps:with(RootNames, RawConfWithEnvs)). include_dirs() -> [filename:join(emqx:data_dir(), "configs")]. +merge_envs(SchemaMod, RawConf) -> + Opts = #{logger => fun(_, _) -> ok end, %% everything should have been logged already when check_config + nullable => true, %% TODO: evil, remove, nullable should be declared in schema + format => map, + apply_override_envs => true + }, + hocon_schema:merge_env_overrides(SchemaMod, RawConf, all, Opts). + -spec check_config(module(), raw_config()) -> {AppEnvs, CheckedConf} when AppEnvs :: app_envs(), CheckedConf :: config(). check_config(SchemaMod, RawConf) -> - Opts = #{return_plain => true, - nullable => true, %% TODO: evil, remove, nullable should be declared in schema - format => map - }, + check_config(SchemaMod, RawConf, #{}). + +check_config(SchemaMod, RawConf, Opts0) -> + Opts1 = #{return_plain => true, + nullable => true, %% TODO: evil, remove, nullable should be declared in schema + format => map + }, + Opts = maps:merge(Opts0, Opts1), {AppEnvs, CheckedConf} = hocon_schema:map_translate(SchemaMod, RawConf, Opts), {AppEnvs, emqx_map_lib:unsafe_atom_key_map(CheckedConf)}. diff --git a/apps/emqx_dashboard/src/emqx_dashboard_swagger.erl b/apps/emqx_dashboard/src/emqx_dashboard_swagger.erl index a699da8e6..a722872a3 100644 --- a/apps/emqx_dashboard/src/emqx_dashboard_swagger.erl +++ b/apps/emqx_dashboard/src/emqx_dashboard_swagger.erl @@ -182,12 +182,12 @@ check_parameter([{Name, Type} | Spec], Bindings, QueryStr, Module, BindingsAcc, Schema = ?INIT_SCHEMA#{roots => [{Name, Type}]}, case hocon_schema:field_schema(Type, in) of path -> - Option = #{atom_key => true, override_env => false}, + Option = #{atom_key => true}, NewBindings = hocon_schema:check_plain(Schema, Bindings, Option), NewBindingsAcc = maps:merge(BindingsAcc, NewBindings), check_parameter(Spec, Bindings, QueryStr, Module, NewBindingsAcc, QueryStrAcc); query -> - Option = #{override_env => false}, + Option = #{}, NewQueryStr = hocon_schema:check_plain(Schema, QueryStr, Option), NewQueryStrAcc = maps:merge(QueryStrAcc, NewQueryStr), check_parameter(Spec, Bindings, QueryStr, Module,BindingsAcc, NewQueryStrAcc) @@ -201,7 +201,7 @@ check_request_body(#{body := Body}, Schema, Module, CheckFun, true) -> _ -> Type0 end, NewSchema = ?INIT_SCHEMA#{roots => [{root, Type}]}, - Option = #{override_env => false, nullable => true}, + Option = #{nullable => true}, #{<<"root">> := NewBody} = CheckFun(NewSchema, #{<<"root">> => Body}, Option), NewBody; %% TODO not support nest object check yet, please use ref! @@ -214,7 +214,7 @@ check_request_body(#{body := Body}, Schema, Module, CheckFun, true) -> check_request_body(#{body := Body}, Spec, _Module, CheckFun, false) -> lists:foldl(fun({Name, Type}, Acc) -> Schema = ?INIT_SCHEMA#{roots => [{Name, Type}]}, - maps:merge(Acc, CheckFun(Schema, Body, #{override_env => false})) + maps:merge(Acc, CheckFun(Schema, Body, #{})) end, #{}, Spec). %% tags, description, summary, security, deprecated