fix(dashboard): handle add default user race condition
This can happen at least in tests, when nodes boot concurrently.
This commit is contained in:
parent
2a9c27d206
commit
b69f298058
|
@ -2,7 +2,7 @@
|
||||||
{application, emqx_dashboard, [
|
{application, emqx_dashboard, [
|
||||||
{description, "EMQX Web Dashboard"},
|
{description, "EMQX Web Dashboard"},
|
||||||
% strict semver, bump manually!
|
% strict semver, bump manually!
|
||||||
{vsn, "5.1.2"},
|
{vsn, "5.1.3"},
|
||||||
{modules, []},
|
{modules, []},
|
||||||
{registered, [emqx_dashboard_sup]},
|
{registered, [emqx_dashboard_sup]},
|
||||||
{applications, [
|
{applications, [
|
||||||
|
|
|
@ -63,6 +63,8 @@
|
||||||
|
|
||||||
-type emqx_admin() :: #?ADMIN{}.
|
-type emqx_admin() :: #?ADMIN{}.
|
||||||
|
|
||||||
|
-define(USERNAME_ALREADY_EXISTS_ERROR, <<"username_already_exists">>).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Mnesia bootstrap
|
%% Mnesia bootstrap
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
@ -214,7 +216,7 @@ add_user_(Username, Password, Role, Desc) ->
|
||||||
username => Username,
|
username => Username,
|
||||||
role => Role
|
role => Role
|
||||||
}),
|
}),
|
||||||
mnesia:abort(<<"username_already_exist">>)
|
mnesia:abort(?USERNAME_ALREADY_EXISTS_ERROR)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec remove_user(dashboard_username()) -> {ok, any()} | {error, any()}.
|
-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};
|
{ok, empty};
|
||||||
add_default_user(Username, Password) ->
|
add_default_user(Username, Password) ->
|
||||||
case lookup_user(Username) of
|
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.
|
end.
|
||||||
|
|
||||||
%% ensure the `role` is correct when it is directly read from the table
|
%% 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),
|
false = maps:is_key(password, NewUser),
|
||||||
|
|
||||||
%% add again
|
%% add again
|
||||||
{error, <<"username_already_exist">>} =
|
{error, <<"username_already_exists">>} =
|
||||||
emqx_dashboard_admin:add_user(AddUser, AddPassword, ?ROLE_SUPERUSER, AddDescription),
|
emqx_dashboard_admin:add_user(AddUser, AddPassword, ?ROLE_SUPERUSER, AddDescription),
|
||||||
|
|
||||||
%% add bad username
|
%% add bad username
|
||||||
|
|
Loading…
Reference in New Issue