fix: psk_authentication is updated failed
This commit is contained in:
parent
055a1e8d75
commit
0ef00d5919
|
@ -17,3 +17,7 @@
|
||||||
-define(TAB, emqx_psk).
|
-define(TAB, emqx_psk).
|
||||||
|
|
||||||
-define(PSK_SHARD, emqx_psk_shard).
|
-define(PSK_SHARD, emqx_psk_shard).
|
||||||
|
|
||||||
|
-define(PSK_KEY, psk_authentication).
|
||||||
|
|
||||||
|
-define(DEFAULT_DELIMITER, <<":">>).
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
{application, emqx_psk, [
|
{application, emqx_psk, [
|
||||||
{description, "EMQX PSK"},
|
{description, "EMQX PSK"},
|
||||||
% strict semver, bump manually!
|
% strict semver, bump manually!
|
||||||
{vsn, "5.0.3"},
|
{vsn, "5.0.4"},
|
||||||
{modules, []},
|
{modules, []},
|
||||||
{registered, [emqx_psk_sup]},
|
{registered, [emqx_psk_sup]},
|
||||||
{applications, [kernel, stdlib]},
|
{applications, [kernel, stdlib]},
|
||||||
|
|
|
@ -27,7 +27,8 @@
|
||||||
load/0,
|
load/0,
|
||||||
unload/0,
|
unload/0,
|
||||||
on_psk_lookup/2,
|
on_psk_lookup/2,
|
||||||
import/1
|
import/1,
|
||||||
|
post_config_update/5
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-export([
|
-export([
|
||||||
|
@ -68,13 +69,11 @@
|
||||||
|
|
||||||
-include("emqx_psk.hrl").
|
-include("emqx_psk.hrl").
|
||||||
|
|
||||||
-define(DEFAULT_DELIMITER, <<":">>).
|
|
||||||
|
|
||||||
-define(CR, 13).
|
-define(CR, 13).
|
||||||
-define(LF, 10).
|
-define(LF, 10).
|
||||||
|
|
||||||
-ifdef(TEST).
|
-ifdef(TEST).
|
||||||
-export([call/1, trim_crlf/1]).
|
-export([call/1, trim_crlf/1, import_psks/3]).
|
||||||
-endif.
|
-endif.
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
|
@ -135,10 +134,6 @@ stop() ->
|
||||||
import_config(#{<<"psk_authentication">> := PskConf}) ->
|
import_config(#{<<"psk_authentication">> := PskConf}) ->
|
||||||
case emqx_conf:update([psk_authentication], PskConf, #{override_to => cluster}) of
|
case emqx_conf:update([psk_authentication], PskConf, #{override_to => cluster}) of
|
||||||
{ok, _} ->
|
{ok, _} ->
|
||||||
case get_config(enable) of
|
|
||||||
true -> load();
|
|
||||||
false -> ok
|
|
||||||
end,
|
|
||||||
{ok, #{root_key => psk_authentication, changed => []}};
|
{ok, #{root_key => psk_authentication, changed => []}};
|
||||||
Error ->
|
Error ->
|
||||||
{error, #{root_key => psk_authentication, reason => Error}}
|
{error, #{root_key => psk_authentication, reason => Error}}
|
||||||
|
@ -146,6 +141,16 @@ import_config(#{<<"psk_authentication">> := PskConf}) ->
|
||||||
import_config(_RawConf) ->
|
import_config(_RawConf) ->
|
||||||
{ok, #{root_key => psk_authentication, changed => []}}.
|
{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
|
%% gen_server callbacks
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
|
@ -169,6 +174,15 @@ handle_call(Req, _From, State) ->
|
||||||
?SLOG(info, #{msg => "unexpected_call_discarded", req => Req}),
|
?SLOG(info, #{msg => "unexpected_call_discarded", req => Req}),
|
||||||
{reply, {error, unexpected}, State}.
|
{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) ->
|
handle_cast(Req, State) ->
|
||||||
?SLOG(info, #{msg => "unexpected_cast_discarded", req => Req}),
|
?SLOG(info, #{msg => "unexpected_cast_discarded", req => Req}),
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
@ -198,6 +212,11 @@ get_config(chunk_size) ->
|
||||||
emqx_conf:get([psk_authentication, chunk_size]).
|
emqx_conf:get([psk_authentication, chunk_size]).
|
||||||
|
|
||||||
import_psks(SrcFile) ->
|
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
|
case file:open(SrcFile, [read, raw, binary, read_ahead]) of
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
?SLOG(error, #{
|
?SLOG(error, #{
|
||||||
|
@ -207,7 +226,7 @@ import_psks(SrcFile) ->
|
||||||
}),
|
}),
|
||||||
{error, Reason};
|
{error, Reason};
|
||||||
{ok, Io} ->
|
{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 ->
|
||||||
ok;
|
ok;
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
|
|
||||||
start(_Type, _Args) ->
|
start(_Type, _Args) ->
|
||||||
ok = mria:wait_for_tables([?TAB]),
|
ok = mria:wait_for_tables([?TAB]),
|
||||||
|
emqx_conf:add_handler([?PSK_KEY], emqx_psk),
|
||||||
{ok, Sup} = emqx_psk_sup:start_link(),
|
{ok, Sup} = emqx_psk_sup:start_link(),
|
||||||
{ok, Sup}.
|
{ok, Sup}.
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
|
|
||||||
-include_lib("typerefl/include/types.hrl").
|
-include_lib("typerefl/include/types.hrl").
|
||||||
-include_lib("hocon/include/hoconsc.hrl").
|
-include_lib("hocon/include/hoconsc.hrl").
|
||||||
|
-include("emqx_psk.hrl").
|
||||||
|
|
||||||
-export([
|
-export([
|
||||||
namespace/0,
|
namespace/0,
|
||||||
|
@ -52,7 +53,7 @@ fields() ->
|
||||||
})},
|
})},
|
||||||
{separator,
|
{separator,
|
||||||
?HOCON(binary(), #{
|
?HOCON(binary(), #{
|
||||||
default => <<":">>,
|
default => ?DEFAULT_DELIMITER,
|
||||||
desc => ?DESC(separator)
|
desc => ?DESC(separator)
|
||||||
})},
|
})},
|
||||||
{chunk_size,
|
{chunk_size,
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
|
|
||||||
-include_lib("common_test/include/ct.hrl").
|
-include_lib("common_test/include/ct.hrl").
|
||||||
-include_lib("eunit/include/eunit.hrl").
|
-include_lib("eunit/include/eunit.hrl").
|
||||||
|
-include("emqx_psk.hrl").
|
||||||
|
|
||||||
-define(CR, 13).
|
-define(CR, 13).
|
||||||
-define(LF, 10).
|
-define(LF, 10).
|
||||||
|
@ -124,7 +125,15 @@ t_load_unload(_) ->
|
||||||
|
|
||||||
t_import(_) ->
|
t_import(_) ->
|
||||||
Init = emqx_conf:get([psk_authentication, init_file], undefined),
|
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)),
|
?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_")),
|
?assertMatch({error, _}, emqx_psk:import("~/_none_")),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue