Merge pull request #12888 from HJianBo/data-backup-for-license

fix(license): license file is not taking effect after importing backup files
This commit is contained in:
JianBo He 2024-04-17 16:31:16 +08:00 committed by GitHub
commit c61c3157c6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 55 additions and 11 deletions

View File

@ -1,6 +1,6 @@
{application, emqx_license, [ {application, emqx_license, [
{description, "EMQX License"}, {description, "EMQX License"},
{vsn, "5.0.16"}, {vsn, "5.0.17"},
{modules, []}, {modules, []},
{registered, [emqx_license_sup]}, {registered, [emqx_license_sup]},
{applications, [kernel, stdlib, emqx_ctl]}, {applications, [kernel, stdlib, emqx_ctl]},

View File

@ -10,6 +10,7 @@
-include_lib("typerefl/include/types.hrl"). -include_lib("typerefl/include/types.hrl").
-behaviour(emqx_config_handler). -behaviour(emqx_config_handler).
-behaviour(emqx_config_backup).
-export([ -export([
pre_config_update/3, pre_config_update/3,
@ -26,6 +27,8 @@
update_setting/1 update_setting/1
]). ]).
-export([import_config/1]).
-define(CONF_KEY_PATH, [license]). -define(CONF_KEY_PATH, [license]).
%% Give the license app the highest priority. %% Give the license app the highest priority.
@ -58,21 +61,20 @@ unload() ->
-spec update_key(binary() | string()) -> -spec update_key(binary() | string()) ->
{ok, emqx_config:update_result()} | {error, emqx_config:update_error()}. {ok, emqx_config:update_result()} | {error, emqx_config:update_error()}.
update_key(Value) when is_binary(Value); is_list(Value) -> update_key(Value) when is_binary(Value); is_list(Value) ->
Result = emqx_conf:update( Result = exec_config_update({key, Value}),
?CONF_KEY_PATH,
{key, Value},
#{rawconf_with_defaults => true, override_to => cluster}
),
handle_config_update_result(Result). handle_config_update_result(Result).
update_setting(Setting) when is_map(Setting) -> update_setting(Setting) when is_map(Setting) ->
Result = emqx_conf:update( Result = exec_config_update({setting, Setting}),
?CONF_KEY_PATH,
{setting, Setting},
#{rawconf_with_defaults => true, override_to => cluster}
),
handle_config_update_result(Result). 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 %% emqx_hooks
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
@ -106,6 +108,17 @@ check(_ConnInfo, AckProps) ->
{stop, {error, ?RC_QUOTA_EXCEEDED}} {stop, {error, ?RC_QUOTA_EXCEEDED}}
end. 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 %% emqx_config_handler callbacks
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------

View File

@ -149,6 +149,36 @@ t_check_not_loaded(_Config) ->
emqx_license:check(#{}, #{}) 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 %% Helpers
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------

View File

@ -0,0 +1 @@
Fix License related configuration loss after importing backup data.