fix(license): license file is not taking effect after importing backup files

This commit is contained in:
JianBo He 2024-04-16 19:07:17 +08:00
parent 34be2ea9a0
commit 6b8111c066
3 changed files with 54 additions and 11 deletions

View File

@ -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]},

View File

@ -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
%%------------------------------------------------------------------------------

View File

@ -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
%%------------------------------------------------------------------------------