Merge pull request #13570 from lafirest/fix/api_key_bootstrap

fix(api_key): do not crash boot when the bootstrap file is not exists
This commit is contained in:
lafirest 2024-08-05 16:33:43 +08:00 committed by GitHub
commit 4644072fd8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 14 additions and 20 deletions

View File

@ -29,13 +29,9 @@
start(_Type, _Args) -> start(_Type, _Args) ->
ok = mria:wait_for_tables(emqx_mgmt_auth:create_tables()), ok = mria:wait_for_tables(emqx_mgmt_auth:create_tables()),
case emqx_mgmt_auth:init_bootstrap_file() of emqx_mgmt_auth:try_init_bootstrap_file(),
ok -> emqx_conf:add_handler([api_key], emqx_mgmt_auth),
emqx_conf:add_handler([api_key], emqx_mgmt_auth), emqx_mgmt_sup:start_link().
emqx_mgmt_sup:start_link();
{error, Reason} ->
{error, Reason}
end.
stop(_State) -> stop(_State) ->
emqx_conf:remove_handler([api_key]), emqx_conf:remove_handler([api_key]),

View File

@ -32,7 +32,7 @@
update/5, update/5,
delete/1, delete/1,
list/0, list/0,
init_bootstrap_file/0, try_init_bootstrap_file/0,
format/1 format/1
]). ]).
@ -52,6 +52,7 @@
-ifdef(TEST). -ifdef(TEST).
-export([create/7]). -export([create/7]).
-export([trans/2, force_create_app/1]). -export([trans/2, force_create_app/1]).
-export([init_bootstrap_file/1]).
-endif. -endif.
-define(APP, emqx_app). -define(APP, emqx_app).
@ -114,11 +115,12 @@ post_config_update([api_key], _Req, NewConf, _OldConf, _AppEnvs) ->
end, end,
ok. ok.
-spec init_bootstrap_file() -> ok | {error, _}. -spec try_init_bootstrap_file() -> ok | {error, _}.
init_bootstrap_file() -> try_init_bootstrap_file() ->
File = bootstrap_file(), File = bootstrap_file(),
?SLOG(debug, #{msg => "init_bootstrap_api_keys_from_file", file => 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) -> create(Name, Enable, ExpiredAt, Desc, Role) ->
ApiKey = generate_unique_api_key(Name), ApiKey = generate_unique_api_key(Name),
@ -357,10 +359,6 @@ init_bootstrap_file(File) ->
init_bootstrap_file(File, Dev, MP); init_bootstrap_file(File, Dev, MP);
{error, Reason0} -> {error, Reason0} ->
Reason = emqx_utils:explain_posix(Reason0), Reason = emqx_utils:explain_posix(Reason0),
FmtReason = emqx_utils:format(
"load API bootstrap file failed, file:~ts, reason:~ts",
[File, Reason]
),
?SLOG( ?SLOG(
error, error,
@ -371,7 +369,7 @@ init_bootstrap_file(File) ->
} }
), ),
{error, FmtReason} {error, Reason}
end. end.
init_bootstrap_file(File, Dev, MP) -> init_bootstrap_file(File, Dev, MP) ->

View File

@ -100,7 +100,7 @@ t_bootstrap_file(_) ->
BadBin = <<"test-1:secret-11\ntest-2 secret-12">>, BadBin = <<"test-1:secret-11\ntest-2 secret-12">>,
ok = file:write_file(File, BadBin), ok = file:write_file(File, BadBin),
update_file(File), 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">>)), ?assertEqual(ok, auth_authorize(TestPath, <<"test-1">>, <<"secret-11">>)),
?assertMatch({error, _}, auth_authorize(TestPath, <<"test-2">>, <<"secret-12">>)), ?assertMatch({error, _}, auth_authorize(TestPath, <<"test-2">>, <<"secret-12">>)),
update_file(<<>>), update_file(<<>>),
@ -123,7 +123,7 @@ t_bootstrap_file_override(_) ->
ok = file:write_file(File, Bin), ok = file:write_file(File, Bin),
update_file(File), 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, MatchFun = fun(ApiKey) -> mnesia:match_object(#?APP{api_key = ApiKey, _ = '_'}) end,
?assertMatch( ?assertMatch(
@ -156,7 +156,7 @@ t_bootstrap_file_dup_override(_) ->
File = "./bootstrap_api_keys.txt", File = "./bootstrap_api_keys.txt",
ok = file:write_file(File, Bin), ok = file:write_file(File, Bin),
update_file(File), update_file(File),
?assertEqual(ok, emqx_mgmt_auth:init_bootstrap_file()), ?assertEqual(ok, emqx_mgmt_auth:init_bootstrap_file(File)),
SameAppWithDiffName = #?APP{ SameAppWithDiffName = #?APP{
name = <<"name-1">>, name = <<"name-1">>,
@ -190,7 +190,7 @@ t_bootstrap_file_dup_override(_) ->
%% Similar to loading bootstrap file at node startup %% Similar to loading bootstrap file at node startup
%% the duplicated apikey in mnesia will be cleaned up %% 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( ?assertMatch(
{ok, [ {ok, [
#?APP{ #?APP{