From c9c4d1a196bba42dadb79165c015a8b2b205a9b1 Mon Sep 17 00:00:00 2001 From: firest Date: Mon, 5 Aug 2024 15:56:05 +0800 Subject: [PATCH] fix(api_key): do not crash boot when the bootstrap file is not exists --- apps/emqx_management/src/emqx_mgmt_app.erl | 10 +++------- apps/emqx_management/src/emqx_mgmt_auth.erl | 16 +++++++--------- .../test/emqx_mgmt_api_api_keys_SUITE.erl | 8 ++++---- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/apps/emqx_management/src/emqx_mgmt_app.erl b/apps/emqx_management/src/emqx_mgmt_app.erl index f8f0b6f5b..e8febb7ec 100644 --- a/apps/emqx_management/src/emqx_mgmt_app.erl +++ b/apps/emqx_management/src/emqx_mgmt_app.erl @@ -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]), diff --git a/apps/emqx_management/src/emqx_mgmt_auth.erl b/apps/emqx_management/src/emqx_mgmt_auth.erl index d822eb788..e64caad57 100644 --- a/apps/emqx_management/src/emqx_mgmt_auth.erl +++ b/apps/emqx_management/src/emqx_mgmt_auth.erl @@ -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) -> diff --git a/apps/emqx_management/test/emqx_mgmt_api_api_keys_SUITE.erl b/apps/emqx_management/test/emqx_mgmt_api_api_keys_SUITE.erl index 177216b42..f2856fe63 100644 --- a/apps/emqx_management/test/emqx_mgmt_api_api_keys_SUITE.erl +++ b/apps/emqx_management/test/emqx_mgmt_api_api_keys_SUITE.erl @@ -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{