fix(api_key): do not crash boot when the bootstrap file is not exists
This commit is contained in:
parent
09ec31908b
commit
c9c4d1a196
|
@ -29,13 +29,9 @@
|
|||
|
||||
start(_Type, _Args) ->
|
||||
ok = mria:wait_for_tables(emqx_mgmt_auth:create_tables()),
|
||||
case emqx_mgmt_auth:init_bootstrap_file() of
|
||||
ok ->
|
||||
emqx_conf:add_handler([api_key], emqx_mgmt_auth),
|
||||
emqx_mgmt_sup:start_link();
|
||||
{error, Reason} ->
|
||||
{error, Reason}
|
||||
end.
|
||||
emqx_mgmt_auth:try_init_bootstrap_file(),
|
||||
emqx_conf:add_handler([api_key], emqx_mgmt_auth),
|
||||
emqx_mgmt_sup:start_link().
|
||||
|
||||
stop(_State) ->
|
||||
emqx_conf:remove_handler([api_key]),
|
||||
|
|
|
@ -32,7 +32,7 @@
|
|||
update/5,
|
||||
delete/1,
|
||||
list/0,
|
||||
init_bootstrap_file/0,
|
||||
try_init_bootstrap_file/0,
|
||||
format/1
|
||||
]).
|
||||
|
||||
|
@ -52,6 +52,7 @@
|
|||
-ifdef(TEST).
|
||||
-export([create/7]).
|
||||
-export([trans/2, force_create_app/1]).
|
||||
-export([init_bootstrap_file/1]).
|
||||
-endif.
|
||||
|
||||
-define(APP, emqx_app).
|
||||
|
@ -114,11 +115,12 @@ post_config_update([api_key], _Req, NewConf, _OldConf, _AppEnvs) ->
|
|||
end,
|
||||
ok.
|
||||
|
||||
-spec init_bootstrap_file() -> ok | {error, _}.
|
||||
init_bootstrap_file() ->
|
||||
-spec try_init_bootstrap_file() -> ok | {error, _}.
|
||||
try_init_bootstrap_file() ->
|
||||
File = bootstrap_file(),
|
||||
?SLOG(debug, #{msg => "init_bootstrap_api_keys_from_file", file => File}),
|
||||
init_bootstrap_file(File).
|
||||
_ = init_bootstrap_file(File),
|
||||
ok.
|
||||
|
||||
create(Name, Enable, ExpiredAt, Desc, Role) ->
|
||||
ApiKey = generate_unique_api_key(Name),
|
||||
|
@ -357,10 +359,6 @@ init_bootstrap_file(File) ->
|
|||
init_bootstrap_file(File, Dev, MP);
|
||||
{error, Reason0} ->
|
||||
Reason = emqx_utils:explain_posix(Reason0),
|
||||
FmtReason = emqx_utils:format(
|
||||
"load API bootstrap file failed, file:~ts, reason:~ts",
|
||||
[File, Reason]
|
||||
),
|
||||
|
||||
?SLOG(
|
||||
error,
|
||||
|
@ -371,7 +369,7 @@ init_bootstrap_file(File) ->
|
|||
}
|
||||
),
|
||||
|
||||
{error, FmtReason}
|
||||
{error, Reason}
|
||||
end.
|
||||
|
||||
init_bootstrap_file(File, Dev, MP) ->
|
||||
|
|
|
@ -100,7 +100,7 @@ t_bootstrap_file(_) ->
|
|||
BadBin = <<"test-1:secret-11\ntest-2 secret-12">>,
|
||||
ok = file:write_file(File, BadBin),
|
||||
update_file(File),
|
||||
?assertMatch({error, #{reason := "invalid_format"}}, emqx_mgmt_auth:init_bootstrap_file()),
|
||||
?assertMatch({error, #{reason := "invalid_format"}}, emqx_mgmt_auth:init_bootstrap_file(File)),
|
||||
?assertEqual(ok, auth_authorize(TestPath, <<"test-1">>, <<"secret-11">>)),
|
||||
?assertMatch({error, _}, auth_authorize(TestPath, <<"test-2">>, <<"secret-12">>)),
|
||||
update_file(<<>>),
|
||||
|
@ -123,7 +123,7 @@ t_bootstrap_file_override(_) ->
|
|||
ok = file:write_file(File, Bin),
|
||||
update_file(File),
|
||||
|
||||
?assertEqual(ok, emqx_mgmt_auth:init_bootstrap_file()),
|
||||
?assertEqual(ok, emqx_mgmt_auth:init_bootstrap_file(File)),
|
||||
|
||||
MatchFun = fun(ApiKey) -> mnesia:match_object(#?APP{api_key = ApiKey, _ = '_'}) end,
|
||||
?assertMatch(
|
||||
|
@ -156,7 +156,7 @@ t_bootstrap_file_dup_override(_) ->
|
|||
File = "./bootstrap_api_keys.txt",
|
||||
ok = file:write_file(File, Bin),
|
||||
update_file(File),
|
||||
?assertEqual(ok, emqx_mgmt_auth:init_bootstrap_file()),
|
||||
?assertEqual(ok, emqx_mgmt_auth:init_bootstrap_file(File)),
|
||||
|
||||
SameAppWithDiffName = #?APP{
|
||||
name = <<"name-1">>,
|
||||
|
@ -190,7 +190,7 @@ t_bootstrap_file_dup_override(_) ->
|
|||
|
||||
%% Similar to loading bootstrap file at node startup
|
||||
%% the duplicated apikey in mnesia will be cleaned up
|
||||
?assertEqual(ok, emqx_mgmt_auth:init_bootstrap_file()),
|
||||
?assertEqual(ok, emqx_mgmt_auth:init_bootstrap_file(File)),
|
||||
?assertMatch(
|
||||
{ok, [
|
||||
#?APP{
|
||||
|
|
Loading…
Reference in New Issue