From 6b8111c0665bc3df393b85cd3cc8af10e540f007 Mon Sep 17 00:00:00 2001 From: JianBo He Date: Tue, 16 Apr 2024 19:07:17 +0800 Subject: [PATCH 1/2] fix(license): license file is not taking effect after importing backup files --- apps/emqx_license/src/emqx_license.app.src | 2 +- apps/emqx_license/src/emqx_license.erl | 33 +++++++++++++------ apps/emqx_license/test/emqx_license_SUITE.erl | 30 +++++++++++++++++ 3 files changed, 54 insertions(+), 11 deletions(-) diff --git a/apps/emqx_license/src/emqx_license.app.src b/apps/emqx_license/src/emqx_license.app.src index 18545cbed..e24a152c7 100644 --- a/apps/emqx_license/src/emqx_license.app.src +++ b/apps/emqx_license/src/emqx_license.app.src @@ -1,6 +1,6 @@ {application, emqx_license, [ {description, "EMQX License"}, - {vsn, "5.0.16"}, + {vsn, "5.0.17"}, {modules, []}, {registered, [emqx_license_sup]}, {applications, [kernel, stdlib, emqx_ctl]}, diff --git a/apps/emqx_license/src/emqx_license.erl b/apps/emqx_license/src/emqx_license.erl index c95ad0e7f..73c1cdf4e 100644 --- a/apps/emqx_license/src/emqx_license.erl +++ b/apps/emqx_license/src/emqx_license.erl @@ -10,6 +10,7 @@ -include_lib("typerefl/include/types.hrl"). -behaviour(emqx_config_handler). +-behaviour(emqx_config_backup). -export([ pre_config_update/3, @@ -26,6 +27,8 @@ update_setting/1 ]). +-export([import_config/1]). + -define(CONF_KEY_PATH, [license]). %% Give the license app the highest priority. @@ -58,21 +61,20 @@ unload() -> -spec update_key(binary() | string()) -> {ok, emqx_config:update_result()} | {error, emqx_config:update_error()}. update_key(Value) when is_binary(Value); is_list(Value) -> - Result = emqx_conf:update( - ?CONF_KEY_PATH, - {key, Value}, - #{rawconf_with_defaults => true, override_to => cluster} - ), + Result = exec_config_update({key, Value}), handle_config_update_result(Result). update_setting(Setting) when is_map(Setting) -> - Result = emqx_conf:update( - ?CONF_KEY_PATH, - {setting, Setting}, - #{rawconf_with_defaults => true, override_to => cluster} - ), + Result = exec_config_update({setting, Setting}), handle_config_update_result(Result). +exec_config_update(Param) -> + emqx_conf:update( + ?CONF_KEY_PATH, + Param, + #{rawconf_with_defaults => true, override_to => cluster} + ). + %%------------------------------------------------------------------------------ %% emqx_hooks %%------------------------------------------------------------------------------ @@ -106,6 +108,17 @@ check(_ConnInfo, AckProps) -> {stop, {error, ?RC_QUOTA_EXCEEDED}} end. +import_config(#{<<"license">> := Config}) -> + OldConf = emqx:get_config(?CONF_KEY_PATH), + case exec_config_update(Config) of + {ok, #{config := NewConf}} -> + Changed = maps:get(changed, emqx_utils_maps:diff_maps(NewConf, OldConf)), + Changed1 = lists:map(fun(Key) -> [license, Key] end, maps:keys(Changed)), + {ok, #{root_key => license, changed => Changed1}}; + Error -> + {error, #{root_key => license, reason => Error}} + end. + %%------------------------------------------------------------------------------ %% emqx_config_handler callbacks %%------------------------------------------------------------------------------ diff --git a/apps/emqx_license/test/emqx_license_SUITE.erl b/apps/emqx_license/test/emqx_license_SUITE.erl index 1aa370359..7c041aad1 100644 --- a/apps/emqx_license/test/emqx_license_SUITE.erl +++ b/apps/emqx_license/test/emqx_license_SUITE.erl @@ -149,6 +149,36 @@ t_check_not_loaded(_Config) -> emqx_license:check(#{}, #{}) ). +t_import_config(_Config) -> + %% Import to default license + ?assertMatch( + {ok, #{root_key := license, changed := _}}, + emqx_license:import_config(#{<<"license">> => #{<<"key">> => <<"default">>}}) + ), + ?assertEqual(default, emqx:get_config([license, key])), + ?assertMatch({ok, #{max_connections := 10}}, emqx_license_checker:limits()), + + %% Import to a new license + EncodedLicense = emqx_license_test_lib:make_license(#{max_connections => "100"}), + ?assertMatch( + {ok, #{root_key := license, changed := _}}, + emqx_license:import_config( + #{ + <<"license">> => + #{ + <<"key">> => EncodedLicense, + <<"connection_low_watermark">> => <<"20%">>, + <<"connection_high_watermark">> => <<"50%">> + } + } + ) + ), + ?assertMatch({ok, #{max_connections := 100}}, emqx_license_checker:limits()), + ?assertMatch( + #{connection_low_watermark := 0.2, connection_high_watermark := 0.5}, + emqx:get_config([license]) + ). + %%------------------------------------------------------------------------------ %% Helpers %%------------------------------------------------------------------------------ From 2f0b72e0aab820b1f075375385ae3b2fd77bc6ac Mon Sep 17 00:00:00 2001 From: JianBo He Date: Tue, 16 Apr 2024 19:11:28 +0800 Subject: [PATCH 2/2] chore: update changes --- changes/ee/fix-12888.en.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changes/ee/fix-12888.en.md diff --git a/changes/ee/fix-12888.en.md b/changes/ee/fix-12888.en.md new file mode 100644 index 000000000..98b42065c --- /dev/null +++ b/changes/ee/fix-12888.en.md @@ -0,0 +1 @@ +Fix License related configuration loss after importing backup data.