From 88ac8631a0109f0d13e3da9bc5e53c130bd22686 Mon Sep 17 00:00:00 2001 From: JianBo He Date: Fri, 5 Jan 2024 16:41:13 +0800 Subject: [PATCH] fix: avoid emqx starting crash if bootstrap_file is not existed --- apps/emqx_management/src/emqx_mgmt_app.erl | 10 +++------- .../test/emqx_mgmt_api_api_keys_SUITE.erl | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/apps/emqx_management/src/emqx_mgmt_app.erl b/apps/emqx_management/src/emqx_mgmt_app.erl index e8bd5d76e..115d3b8dc 100644 --- a/apps/emqx_management/src/emqx_mgmt_app.erl +++ b/apps/emqx_management/src/emqx_mgmt_app.erl @@ -28,13 +28,9 @@ -include("emqx_mgmt.hrl"). start(_Type, _Args) -> - 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: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/test/emqx_mgmt_api_api_keys_SUITE.erl b/apps/emqx_management/test/emqx_mgmt_api_api_keys_SUITE.erl index d437e07c9..7ad0dea74 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 @@ -52,7 +52,12 @@ groups() -> [ {parallel, [parallel], [t_create, t_update, t_delete, t_authorize, t_create_unexpired_app]}, {parallel, [parallel], ?EE_CASES}, - {sequence, [], [t_bootstrap_file, t_bootstrap_file_with_role, t_create_failed]} + {sequence, [], [ + t_application_start_with_non_exist_file, + t_bootstrap_file, + t_bootstrap_file_with_role, + t_create_failed + ]} ]. init_per_suite(Config) -> @@ -62,6 +67,18 @@ init_per_suite(Config) -> end_per_suite(_) -> emqx_mgmt_api_test_util:end_suite([emqx_conf, emqx_management]). +t_application_start_with_non_exist_file(_) -> + application:stop(emqx_management), + + Original = emqx_config:get_raw([api_key, bootstrap_file]), + update_file(<<"non-exist-file">>), + + %% assert: start the application succeed with a non-exist bootstrap_file + ok = application:start(emqx_management), + + update_file(Original), + ok. + t_bootstrap_file(_) -> TestPath = <<"/api/v5/status">>, Bin = <<"test-1:secret-1\ntest-2:secret-2">>,