From 4a87d77be33963246cbf9b36e726a0cc0fcb2cb6 Mon Sep 17 00:00:00 2001 From: zhongwencool Date: Thu, 23 Jun 2022 10:27:31 +0800 Subject: [PATCH 1/3] fix: can't start dashboard if default_username is missing --- CHANGES-4.3.md | 1 + .../src/emqx_dashboard_admin.erl | 27 ++++++++++--------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/CHANGES-4.3.md b/CHANGES-4.3.md index e73e45124..7799eedda 100644 --- a/CHANGES-4.3.md +++ b/CHANGES-4.3.md @@ -31,6 +31,7 @@ File format: - Fixed issue in Lua hook that didn't prevent a topic from being subscribed to. [#8288] - Ensuring that exhook dispatches the client events are sequential. [#8311] +- Ensure start dashboard ok event if default_username is missing. ## v4.3.15 diff --git a/lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl b/lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl index a4504fe29..a3a773114 100644 --- a/lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl +++ b/lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl @@ -206,11 +206,15 @@ is_valid_pwd(<>, Password) -> %%-------------------------------------------------------------------- init([]) -> - %% Add default admin user - {ok, _} = mnesia:subscribe({table, mqtt_admin, simple}), - PasswordHash = ensure_default_user_in_db(binenv(default_user_username)), - ok = ensure_default_user_passwd_hashed_in_pt(PasswordHash), - ok = maybe_warn_default_pwd(), + case binenv(default_user_username) of + <<>> -> ok; + UserName -> + %% Add default admin user + {ok, _} = mnesia:subscribe({table, mqtt_admin, simple}), + PasswordHash = ensure_default_user_in_db(UserName), + ok = ensure_default_user_passwd_hashed_in_pt(PasswordHash), + ok = maybe_warn_default_pwd() + end, {ok, state}. handle_call(_Req, _From, State) -> @@ -220,7 +224,7 @@ handle_cast(_Msg, State) -> {noreply, State}. handle_info({mnesia_table_event, {write, Admin, _}}, State) -> - %% the password is chagned from another node, sync it to persistent_term + %% the password is changed from another node, sync it to persistent_term #mqtt_admin{username = Username, password = HashedPassword} = Admin, case binenv(default_user_username) of Username -> @@ -258,6 +262,7 @@ salt() -> binenv(Key) -> iolist_to_binary(application:get_env(emqx_dashboard, Key, <<>>)). +ensure_default_user_in_db(<<>>) -> <<>>; ensure_default_user_in_db(Username) -> F = fun() -> @@ -279,13 +284,8 @@ ensure_default_user_in_db(Username) -> initial_default_user_passwd_hashed() -> case get_default_user_passwd_hashed_from_pt() of Empty when ?EMPTY_KEY(Empty) -> - %% in case it's not set yet - case binenv(default_user_passwd) of - Empty when ?EMPTY_KEY(Empty) -> - error({missing_configuration, default_user_passwd}); - Pwd -> - hash(Pwd) - end; + Pwd = binenv(default_user_passwd), + hash(Pwd); PwdHash -> PwdHash end. @@ -293,6 +293,7 @@ initial_default_user_passwd_hashed() -> %% use this persistent_term for a copy of the value in mnesia database %% so that after the node leaves a cluster, db gets purged, %% we can still find the changed password back from PT +ensure_default_user_passwd_hashed_in_pt(<<>>) -> ok; ensure_default_user_passwd_hashed_in_pt(Hashed) -> ok = persistent_term:put({?MODULE, default_user_passwd_hashed}, Hashed). From fd22639bcb2f1ec66eb6a2326ecadfeef933ed17 Mon Sep 17 00:00:00 2001 From: zhongwencool Date: Thu, 23 Jun 2022 12:09:26 +0800 Subject: [PATCH 2/3] chore: remove unnessary function case --- lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl | 1 - 1 file changed, 1 deletion(-) diff --git a/lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl b/lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl index a3a773114..b7c729955 100644 --- a/lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl +++ b/lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl @@ -293,7 +293,6 @@ initial_default_user_passwd_hashed() -> %% use this persistent_term for a copy of the value in mnesia database %% so that after the node leaves a cluster, db gets purged, %% we can still find the changed password back from PT -ensure_default_user_passwd_hashed_in_pt(<<>>) -> ok; ensure_default_user_passwd_hashed_in_pt(Hashed) -> ok = persistent_term:put({?MODULE, default_user_passwd_hashed}, Hashed). From ab2e477ffd93cf6469aa09b5ff2b8cad7be360db Mon Sep 17 00:00:00 2001 From: zhongwencool Date: Mon, 27 Jun 2022 14:33:37 +0800 Subject: [PATCH 3/3] fix: default_password should not be empty --- lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl b/lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl index b7c729955..a76ed9cff 100644 --- a/lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl +++ b/lib-ce/emqx_dashboard/src/emqx_dashboard_admin.erl @@ -22,6 +22,7 @@ -include("emqx_dashboard.hrl"). -include_lib("emqx/include/logger.hrl"). +-define(DEFAULT_PASSWORD, <<"public">>). -boot_mnesia({mnesia, [boot]}). -copy_mnesia({mnesia, [copy]}). @@ -284,8 +285,10 @@ ensure_default_user_in_db(Username) -> initial_default_user_passwd_hashed() -> case get_default_user_passwd_hashed_from_pt() of Empty when ?EMPTY_KEY(Empty) -> - Pwd = binenv(default_user_passwd), - hash(Pwd); + case binenv(default_user_passwd) of + Empty when ?EMPTY_KEY(Empty) -> hash(?DEFAULT_PASSWORD); + Pwd -> hash(Pwd) + end; PwdHash -> PwdHash end. @@ -300,7 +303,7 @@ get_default_user_passwd_hashed_from_pt() -> persistent_term:get({?MODULE, default_user_passwd_hashed}, <<>>). maybe_warn_default_pwd() -> - case is_valid_pwd(initial_default_user_passwd_hashed(), <<"public">>) of + case is_valid_pwd(initial_default_user_passwd_hashed(), ?DEFAULT_PASSWORD) of true -> ?LOG(warning, "[Dashboard] Using default password for dashboard 'admin' user. "