Merge pull request #13365 from thalesmg/20240628-r57-fix-dashboard-add-default-user-concurrently
fix(dashboard): handle add default user race condition
This commit is contained in:
commit
d1e9b097d1
|
@ -2,7 +2,7 @@
|
|||
{application, emqx_dashboard, [
|
||||
{description, "EMQX Web Dashboard"},
|
||||
% strict semver, bump manually!
|
||||
{vsn, "5.1.2"},
|
||||
{vsn, "5.1.3"},
|
||||
{modules, []},
|
||||
{registered, [emqx_dashboard_sup]},
|
||||
{applications, [
|
||||
|
|
|
@ -63,6 +63,8 @@
|
|||
|
||||
-type emqx_admin() :: #?ADMIN{}.
|
||||
|
||||
-define(USERNAME_ALREADY_EXISTS_ERROR, <<"username_already_exists">>).
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% Mnesia bootstrap
|
||||
%%--------------------------------------------------------------------
|
||||
|
@ -214,7 +216,7 @@ add_user_(Username, Password, Role, Desc) ->
|
|||
username => Username,
|
||||
role => Role
|
||||
}),
|
||||
mnesia:abort(<<"username_already_exist">>)
|
||||
mnesia:abort(?USERNAME_ALREADY_EXISTS_ERROR)
|
||||
end.
|
||||
|
||||
-spec remove_user(dashboard_username()) -> {ok, any()} | {error, any()}.
|
||||
|
@ -411,8 +413,16 @@ add_default_user(Username, Password) when ?EMPTY_KEY(Username) orelse ?EMPTY_KEY
|
|||
{ok, empty};
|
||||
add_default_user(Username, Password) ->
|
||||
case lookup_user(Username) of
|
||||
[] -> do_add_user(Username, Password, ?ROLE_SUPERUSER, <<"administrator">>);
|
||||
_ -> {ok, default_user_exists}
|
||||
[] ->
|
||||
case do_add_user(Username, Password, ?ROLE_SUPERUSER, <<"administrator">>) of
|
||||
{error, ?USERNAME_ALREADY_EXISTS_ERROR} ->
|
||||
%% race condition: multiple nodes booting at the same time?
|
||||
{ok, default_user_exists};
|
||||
Res ->
|
||||
Res
|
||||
end;
|
||||
_ ->
|
||||
{ok, default_user_exists}
|
||||
end.
|
||||
|
||||
%% ensure the `role` is correct when it is directly read from the table
|
||||
|
|
|
@ -69,7 +69,7 @@ t_add_user(_) ->
|
|||
false = maps:is_key(password, NewUser),
|
||||
|
||||
%% add again
|
||||
{error, <<"username_already_exist">>} =
|
||||
{error, <<"username_already_exists">>} =
|
||||
emqx_dashboard_admin:add_user(AddUser, AddPassword, ?ROLE_SUPERUSER, AddDescription),
|
||||
|
||||
%% add bad username
|
||||
|
|
Loading…
Reference in New Issue