Browse Source

fix: psk_authentication is updated failed

zhongwencool 2 năm trước cách đây
mục cha
commit
0ef00d5919

+ 4 - 0
apps/emqx_psk/include/emqx_psk.hrl

@@ -17,3 +17,7 @@
 -define(TAB, emqx_psk).
 
 -define(PSK_SHARD, emqx_psk_shard).
+
+-define(PSK_KEY, psk_authentication).
+
+-define(DEFAULT_DELIMITER, <<":">>).

+ 1 - 1
apps/emqx_psk/src/emqx_psk.app.src

@@ -2,7 +2,7 @@
 {application, emqx_psk, [
     {description, "EMQX PSK"},
     % strict semver, bump manually!
-    {vsn, "5.0.3"},
+    {vsn, "5.0.4"},
     {modules, []},
     {registered, [emqx_psk_sup]},
     {applications, [kernel, stdlib]},

+ 28 - 9
apps/emqx_psk/src/emqx_psk.erl

@@ -27,7 +27,8 @@
     load/0,
     unload/0,
     on_psk_lookup/2,
-    import/1
+    import/1,
+    post_config_update/5
 ]).
 
 -export([
@@ -68,13 +69,11 @@
 
 -include("emqx_psk.hrl").
 
--define(DEFAULT_DELIMITER, <<":">>).
-
 -define(CR, 13).
 -define(LF, 10).
 
 -ifdef(TEST).
--export([call/1, trim_crlf/1]).
+-export([call/1, trim_crlf/1, import_psks/3]).
 -endif.
 
 %%------------------------------------------------------------------------------
@@ -135,10 +134,6 @@ stop() ->
 import_config(#{<<"psk_authentication">> := PskConf}) ->
     case emqx_conf:update([psk_authentication], PskConf, #{override_to => cluster}) of
         {ok, _} ->
-            case get_config(enable) of
-                true -> load();
-                false -> ok
-            end,
             {ok, #{root_key => psk_authentication, changed => []}};
         Error ->
             {error, #{root_key => psk_authentication, reason => Error}}
@@ -146,6 +141,16 @@ import_config(#{<<"psk_authentication">> := PskConf}) ->
 import_config(_RawConf) ->
     {ok, #{root_key => psk_authentication, changed => []}}.
 
+post_config_update([?PSK_KEY], _Req, #{enable := Enable} = NewConf, _OldConf, _AppEnvs) ->
+    case Enable of
+        true ->
+            load(),
+            _ = gen_server:cast(?MODULE, {import_from_conf, NewConf});
+        false ->
+            unload()
+    end,
+    ok.
+
 %%------------------------------------------------------------------------------
 %% gen_server callbacks
 %%------------------------------------------------------------------------------
@@ -169,6 +174,15 @@ handle_call(Req, _From, State) ->
     ?SLOG(info, #{msg => "unexpected_call_discarded", req => Req}),
     {reply, {error, unexpected}, State}.
 
+handle_cast({import_from_conf, Conf}, State) ->
+    Separator = maps:get(separator, Conf, ?DEFAULT_DELIMITER),
+    ChunkSize = maps:get(chunk_size, Conf),
+    _ =
+        case maps:get(init_file, Conf, undefined) of
+            undefined -> ok;
+            InitFile -> import_psks(InitFile, Separator, ChunkSize)
+        end,
+    {noreply, State};
 handle_cast(Req, State) ->
     ?SLOG(info, #{msg => "unexpected_cast_discarded", req => Req}),
     {noreply, State}.
@@ -198,6 +212,11 @@ get_config(chunk_size) ->
     emqx_conf:get([psk_authentication, chunk_size]).
 
 import_psks(SrcFile) ->
+    Separator = get_config(separator),
+    ChunkSize = get_config(chunk_size),
+    import_psks(SrcFile, Separator, ChunkSize).
+
+import_psks(SrcFile, Separator, ChunkSize) ->
     case file:open(SrcFile, [read, raw, binary, read_ahead]) of
         {error, Reason} ->
             ?SLOG(error, #{
@@ -207,7 +226,7 @@ import_psks(SrcFile) ->
             }),
             {error, Reason};
         {ok, Io} ->
-            try import_psks(Io, get_config(separator), get_config(chunk_size), 0) of
+            try import_psks(Io, Separator, ChunkSize, 0) of
                 ok ->
                     ok;
                 {error, Reason} ->

+ 1 - 0
apps/emqx_psk/src/emqx_psk_app.erl

@@ -27,6 +27,7 @@
 
 start(_Type, _Args) ->
     ok = mria:wait_for_tables([?TAB]),
+    emqx_conf:add_handler([?PSK_KEY], emqx_psk),
     {ok, Sup} = emqx_psk_sup:start_link(),
     {ok, Sup}.
 

+ 2 - 1
apps/emqx_psk/src/emqx_psk_schema.erl

@@ -20,6 +20,7 @@
 
 -include_lib("typerefl/include/types.hrl").
 -include_lib("hocon/include/hoconsc.hrl").
+-include("emqx_psk.hrl").
 
 -export([
     namespace/0,
@@ -52,7 +53,7 @@ fields() ->
             })},
         {separator,
             ?HOCON(binary(), #{
-                default => <<":">>,
+                default => ?DEFAULT_DELIMITER,
                 desc => ?DESC(separator)
             })},
         {chunk_size,

+ 9 - 0
apps/emqx_psk/test/emqx_psk_SUITE.erl

@@ -20,6 +20,7 @@
 
 -include_lib("common_test/include/ct.hrl").
 -include_lib("eunit/include/eunit.hrl").
+-include("emqx_psk.hrl").
 
 -define(CR, 13).
 -define(LF, 10).
@@ -124,7 +125,15 @@ t_load_unload(_) ->
 
 t_import(_) ->
     Init = emqx_conf:get([psk_authentication, init_file], undefined),
+    Separator = emqx_conf:get([psk_authentication, separator], ?DEFAULT_DELIMITER),
+    ChunkSize = emqx_conf:get([psk_authentication, chunk_size], 50),
     ?assertEqual(ok, emqx_psk:import(Init)),
+    Keys0 = lists:sort(mnesia:dirty_all_keys(emqx_psk)),
+    ?assert(length(Keys0) > 0),
+    {atomic, ok} = mnesia:clear_table(emqx_psk),
+    ok = emqx_psk:import_psks(Init, Separator, ChunkSize),
+    Keys1 = lists:sort(mnesia:dirty_all_keys(emqx_psk)),
+    ?assertEqual(Keys0, Keys1),
     ?assertMatch({error, _}, emqx_psk:import("~/_none_")),
     ok.