Merge pull request #8302 from zhongwencool/dashboard-default-username-missing
fix: can't start dashboard if default_username is missing
This commit is contained in:
commit
84856252c9
|
@ -31,6 +31,7 @@ File format:
|
||||||
- Fixed issue in Lua hook that didn't prevent a topic from being
|
- Fixed issue in Lua hook that didn't prevent a topic from being
|
||||||
subscribed to. [#8288]
|
subscribed to. [#8288]
|
||||||
- Ensuring that exhook dispatches the client events are sequential. [#8311]
|
- Ensuring that exhook dispatches the client events are sequential. [#8311]
|
||||||
|
- Ensure start dashboard ok event if default_username is missing.
|
||||||
|
|
||||||
## v4.3.15
|
## v4.3.15
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
|
|
||||||
-include("emqx_dashboard.hrl").
|
-include("emqx_dashboard.hrl").
|
||||||
-include_lib("emqx/include/logger.hrl").
|
-include_lib("emqx/include/logger.hrl").
|
||||||
|
-define(DEFAULT_PASSWORD, <<"public">>).
|
||||||
|
|
||||||
-boot_mnesia({mnesia, [boot]}).
|
-boot_mnesia({mnesia, [boot]}).
|
||||||
-copy_mnesia({mnesia, [copy]}).
|
-copy_mnesia({mnesia, [copy]}).
|
||||||
|
@ -206,11 +207,15 @@ is_valid_pwd(<<Salt:4/binary, Hash/binary>>, Password) ->
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
init([]) ->
|
init([]) ->
|
||||||
%% Add default admin user
|
case binenv(default_user_username) of
|
||||||
{ok, _} = mnesia:subscribe({table, mqtt_admin, simple}),
|
<<>> -> ok;
|
||||||
PasswordHash = ensure_default_user_in_db(binenv(default_user_username)),
|
UserName ->
|
||||||
ok = ensure_default_user_passwd_hashed_in_pt(PasswordHash),
|
%% Add default admin user
|
||||||
ok = maybe_warn_default_pwd(),
|
{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}.
|
{ok, state}.
|
||||||
|
|
||||||
handle_call(_Req, _From, State) ->
|
handle_call(_Req, _From, State) ->
|
||||||
|
@ -220,7 +225,7 @@ handle_cast(_Msg, State) ->
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
|
||||||
handle_info({mnesia_table_event, {write, Admin, _}}, 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,
|
#mqtt_admin{username = Username, password = HashedPassword} = Admin,
|
||||||
case binenv(default_user_username) of
|
case binenv(default_user_username) of
|
||||||
Username ->
|
Username ->
|
||||||
|
@ -258,6 +263,7 @@ salt() ->
|
||||||
binenv(Key) ->
|
binenv(Key) ->
|
||||||
iolist_to_binary(application:get_env(emqx_dashboard, Key, <<>>)).
|
iolist_to_binary(application:get_env(emqx_dashboard, Key, <<>>)).
|
||||||
|
|
||||||
|
ensure_default_user_in_db(<<>>) -> <<>>;
|
||||||
ensure_default_user_in_db(Username) ->
|
ensure_default_user_in_db(Username) ->
|
||||||
F =
|
F =
|
||||||
fun() ->
|
fun() ->
|
||||||
|
@ -279,12 +285,9 @@ ensure_default_user_in_db(Username) ->
|
||||||
initial_default_user_passwd_hashed() ->
|
initial_default_user_passwd_hashed() ->
|
||||||
case get_default_user_passwd_hashed_from_pt() of
|
case get_default_user_passwd_hashed_from_pt() of
|
||||||
Empty when ?EMPTY_KEY(Empty) ->
|
Empty when ?EMPTY_KEY(Empty) ->
|
||||||
%% in case it's not set yet
|
|
||||||
case binenv(default_user_passwd) of
|
case binenv(default_user_passwd) of
|
||||||
Empty when ?EMPTY_KEY(Empty) ->
|
Empty when ?EMPTY_KEY(Empty) -> hash(?DEFAULT_PASSWORD);
|
||||||
error({missing_configuration, default_user_passwd});
|
Pwd -> hash(Pwd)
|
||||||
Pwd ->
|
|
||||||
hash(Pwd)
|
|
||||||
end;
|
end;
|
||||||
PwdHash ->
|
PwdHash ->
|
||||||
PwdHash
|
PwdHash
|
||||||
|
@ -300,7 +303,7 @@ get_default_user_passwd_hashed_from_pt() ->
|
||||||
persistent_term:get({?MODULE, default_user_passwd_hashed}, <<>>).
|
persistent_term:get({?MODULE, default_user_passwd_hashed}, <<>>).
|
||||||
|
|
||||||
maybe_warn_default_pwd() ->
|
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 ->
|
true ->
|
||||||
?LOG(warning,
|
?LOG(warning,
|
||||||
"[Dashboard] Using default password for dashboard 'admin' user. "
|
"[Dashboard] Using default password for dashboard 'admin' user. "
|
||||||
|
|
Loading…
Reference in New Issue