From ef59309ed0b7a8d183b521f28ce4dfc2ac85a943 Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Thu, 19 Aug 2021 15:29:34 +0800 Subject: [PATCH] fix(config): check config failed when updating --- apps/emqx/src/emqx_config_handler.erl | 26 ++++++++++++------- apps/emqx/src/emqx_map_lib.erl | 2 +- .../src/emqx_mgmt_api_configs.erl | 2 +- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/apps/emqx/src/emqx_config_handler.erl b/apps/emqx/src/emqx_config_handler.erl index a7c28fff0..7c66656ce 100644 --- a/apps/emqx/src/emqx_config_handler.erl +++ b/apps/emqx/src/emqx_config_handler.erl @@ -89,12 +89,10 @@ handle_call({add_child, ConfKeyPath, HandlerName}, _From, handle_call({change_config, SchemaModule, ConfKeyPath, UpdateArgs}, _From, #{handlers := Handlers} = State) -> - OldConf = emqx_config:get_root(ConfKeyPath), - OldRawConf = emqx_config:get_root_raw(ConfKeyPath), Reply = try - case process_update_request(ConfKeyPath, OldRawConf, Handlers, UpdateArgs) of + case process_update_request(ConfKeyPath, Handlers, UpdateArgs) of {ok, NewRawConf, OverrideConf} -> - check_and_save_configs(SchemaModule, ConfKeyPath, Handlers, NewRawConf, OldConf, + check_and_save_configs(SchemaModule, ConfKeyPath, Handlers, NewRawConf, OverrideConf, UpdateArgs); {error, Result} -> {error, Result} @@ -121,12 +119,14 @@ terminate(_Reason, _State) -> code_change(_OldVsn, State, _Extra) -> {ok, State}. -process_update_request(ConfKeyPath, OldRawConf, _Handlers, {remove, _Opts}) -> +process_update_request(ConfKeyPath, _Handlers, {remove, _Opts}) -> + OldRawConf = emqx_config:get_root_raw(ConfKeyPath), BinKeyPath = bin_path(ConfKeyPath), NewRawConf = emqx_map_lib:deep_remove(BinKeyPath, OldRawConf), OverrideConf = emqx_map_lib:deep_remove(BinKeyPath, emqx_config:read_override_conf()), {ok, NewRawConf, OverrideConf}; -process_update_request(ConfKeyPath, OldRawConf, Handlers, {{update, UpdateReq}, _Opts}) -> +process_update_request(ConfKeyPath, Handlers, {{update, UpdateReq}, _Opts}) -> + OldRawConf = emqx_config:get_root_raw(ConfKeyPath), case do_update_config(ConfKeyPath, Handlers, OldRawConf, UpdateReq) of {ok, NewRawConf} -> OverrideConf = update_override_config(NewRawConf), @@ -146,12 +146,15 @@ do_update_config([ConfKey | ConfKeyPath], Handlers, OldRawConf, UpdateReq) -> Error end. -check_and_save_configs(SchemaModule, ConfKeyPath, Handlers, NewRawConf, OldConf, OverrideConf, +check_and_save_configs(SchemaModule, ConfKeyPath, Handlers, NewRawConf, OverrideConf, UpdateArgs) -> - {AppEnvs, CheckedConf} = emqx_config:check_config(SchemaModule, NewRawConf), - case do_post_config_update(ConfKeyPath, Handlers, OldConf, CheckedConf, UpdateArgs, #{}) of + OldConf = emqx_config:get_root(ConfKeyPath), + FullRawConf = with_full_raw_confs(NewRawConf), + {AppEnvs, CheckedConf} = emqx_config:check_config(SchemaModule, FullRawConf), + NewConf = maps:with(maps:keys(OldConf), CheckedConf), + case do_post_config_update(ConfKeyPath, Handlers, OldConf, NewConf, UpdateArgs, #{}) of {ok, Result0} -> - case save_configs(ConfKeyPath, AppEnvs, CheckedConf, NewRawConf, OverrideConf, + case save_configs(ConfKeyPath, AppEnvs, NewConf, NewRawConf, OverrideConf, UpdateArgs) of {ok, Result1} -> {ok, Result1#{post_config_update => Result0}}; @@ -231,6 +234,9 @@ return_rawconf(ConfKeyPath, #{rawconf_with_defaults := true}) -> return_rawconf(ConfKeyPath, _) -> emqx_config:get_raw(ConfKeyPath). +with_full_raw_confs(PartialConf) -> + maps:merge(emqx_config:get_raw([]), PartialConf). + bin_path(ConfKeyPath) -> [bin(Key) || Key <- ConfKeyPath]. bin(A) when is_atom(A) -> atom_to_binary(A, utf8); diff --git a/apps/emqx/src/emqx_map_lib.erl b/apps/emqx/src/emqx_map_lib.erl index e5baeb850..468553193 100644 --- a/apps/emqx/src/emqx_map_lib.erl +++ b/apps/emqx/src/emqx_map_lib.erl @@ -118,7 +118,7 @@ unsafe_atom_key_map(Map) -> safe_atom_key_map(Map) -> covert_keys_to_atom(Map, fun(K) -> binary_to_existing_atom(K, utf8) end). --spec jsonable_map(map()) -> map(). +-spec jsonable_map(map() | list()) -> map() | list(). jsonable_map(Map) -> deep_convert(Map, fun(K, V) -> {jsonable_value(K), jsonable_value(V)} diff --git a/apps/emqx_management/src/emqx_mgmt_api_configs.erl b/apps/emqx_management/src/emqx_mgmt_api_configs.erl index 1a89835ff..46b679c3d 100644 --- a/apps/emqx_management/src/emqx_mgmt_api_configs.erl +++ b/apps/emqx_management/src/emqx_mgmt_api_configs.erl @@ -120,7 +120,7 @@ config(put, Req) -> Path = conf_path(Req), {ok, #{raw_config := RawConf}} = emqx:update_config(Path, http_body(Req), #{rawconf_with_defaults => true}), - {200, emqx_map_lib:deep_get(Path, emqx_map_lib:jsonable_map(RawConf))}. + {200, emqx_map_lib:jsonable_map(RawConf)}. config_reset(post, Req) -> %% reset the config specified by the query string param 'conf_path'