chore(authn): improve code of moving authenticator

This commit is contained in:
zhouzb 2021-09-09 09:32:18 +08:00
parent 7d312a630b
commit c6e52b32fb
2 changed files with 35 additions and 17 deletions

View File

@ -78,6 +78,14 @@
-define(VER_1, <<"1">>). -define(VER_1, <<"1">>).
-define(VER_2, <<"2">>). -define(VER_2, <<"2">>).
-type chain_name() :: atom().
-type authenticator_id() :: binary().
-type position() :: top | bottom | {before, authenticator_id()}.
-type update_request() :: {create_authenticator, chain_name(), map()}
| {delete_authenticator, chain_name(), authenticator_id()}
| {update_authenticator, chain_name(), authenticator_id(), map()}
| {move_authenitcator, chain_name(), authenticator_id(), position()}.
-type config() :: #{atom() => term()}. -type config() :: #{atom() => term()}.
-type state() :: #{atom() => term()}. -type state() :: #{atom() => term()}.
-type extra() :: #{is_superuser := boolean(), -type extra() :: #{is_superuser := boolean(),
@ -159,6 +167,8 @@ authentication(_) -> undefined.
%% Callbacks of config handler %% Callbacks of config handler
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
-spec pre_config_update(update_request(), emqx_config:raw_config())
-> {ok, map() | list()} | {error, term()}.
pre_config_update(UpdateReq, OldConfig) -> pre_config_update(UpdateReq, OldConfig) ->
case do_pre_config_update(UpdateReq, to_list(OldConfig)) of case do_pre_config_update(UpdateReq, to_list(OldConfig)) of
{error, Reason} -> {error, Reason}; {error, Reason} -> {error, Reason};
@ -185,22 +195,22 @@ do_pre_config_update({move_authenticator, _ChainName, AuthenticatorID, Position}
{error, Reason} -> {error, Reason}; {error, Reason} -> {error, Reason};
{ok, Part1, [Found | Part2]} -> {ok, Part1, [Found | Part2]} ->
case Position of case Position of
<<"top">> -> top ->
{ok, [Found | Part1] ++ Part2}; {ok, [Found | Part1] ++ Part2};
<<"bottom">> -> bottom ->
{ok, Part1 ++ Part2 ++ [Found]}; {ok, Part1 ++ Part2 ++ [Found]};
<<"before:", Before/binary>> -> {before, Before} ->
case split_by_id(Before, Part1 ++ Part2) of case split_by_id(Before, Part1 ++ Part2) of
{error, Reason} -> {error, Reason} ->
{error, Reason}; {error, Reason};
{ok, NPart1, [NFound | NPart2]} -> {ok, NPart1, [NFound | NPart2]} ->
{ok, NPart1 ++ [Found, NFound | NPart2]} {ok, NPart1 ++ [Found, NFound | NPart2]}
end; end
_ ->
{error, {invalid_parameter, position}}
end end
end. end.
-spec post_config_update(update_request, map() | list(), emqx_config:raw_config(), emqx_config:app_envs())
-> ok | {ok, map()} | {error, term()}.
post_config_update(UpdateReq, NewConfig, OldConfig, AppEnvs) -> post_config_update(UpdateReq, NewConfig, OldConfig, AppEnvs) ->
do_post_config_update(UpdateReq, check_config(to_list(NewConfig)), OldConfig, AppEnvs). do_post_config_update(UpdateReq, check_config(to_list(NewConfig)), OldConfig, AppEnvs).
@ -220,13 +230,7 @@ do_post_config_update({update_authenticator, ChainName, AuthenticatorID, _Config
update_authenticator(ChainName, AuthenticatorID, NConfig); update_authenticator(ChainName, AuthenticatorID, NConfig);
do_post_config_update({move_authenticator, ChainName, AuthenticatorID, Position}, _NewConfig, _OldConfig, _AppEnvs) -> do_post_config_update({move_authenticator, ChainName, AuthenticatorID, Position}, _NewConfig, _OldConfig, _AppEnvs) ->
NPosition = case Position of move_authenticator(ChainName, AuthenticatorID, Position).
<<"top">> -> top;
<<"bottom">> -> bottom;
<<"before:", Before/binary>> ->
{before, Before}
end,
move_authenticator(ChainName, AuthenticatorID, NPosition).
check_config(Config) -> check_config(Config) ->
#{authentication := CheckedConfig} = hocon_schema:check_plain(emqx_authentication, #{authentication := CheckedConfig} = hocon_schema:check_plain(emqx_authentication,

View File

@ -1877,10 +1877,15 @@ delete_authenticator(ConfKeyPath, ChainName0, AuthenticatorID) ->
move_authenitcator(ConfKeyPath, ChainName0, AuthenticatorID, Position) -> move_authenitcator(ConfKeyPath, ChainName0, AuthenticatorID, Position) ->
ChainName = to_atom(ChainName0), ChainName = to_atom(ChainName0),
case update_config(ConfKeyPath, {move_authenticator, ChainName, AuthenticatorID, Position}) of case parse_position(Position) of
{ok, _} -> {ok, NPosition} ->
{204}; case update_config(ConfKeyPath, {move_authenticator, ChainName, AuthenticatorID, NPosition}) of
{error, {_, _, Reason}} -> {ok, _} ->
{204};
{error, {_, _, Reason}} ->
serialize_error(Reason)
end;
{error, Reason} ->
serialize_error(Reason) serialize_error(Reason)
end. end.
@ -2016,6 +2021,15 @@ serialize_error(Reason) ->
{400, #{code => <<"BAD_REQUEST">>, {400, #{code => <<"BAD_REQUEST">>,
message => list_to_binary(io_lib:format("~p", [Reason]))}}. message => list_to_binary(io_lib:format("~p", [Reason]))}}.
parse_position(<<"top">>) ->
{ok, top};
parse_position(<<"bottom">>) ->
{ok, bottom};
parse_position(<<"before:", Before/binary>>) ->
{ok, {before, Before}};
parse_position(_) ->
{error, {invalid_parameter, position}}.
to_list(M) when is_map(M) -> to_list(M) when is_map(M) ->
[M]; [M];
to_list(L) when is_list(L) -> to_list(L) when is_list(L) ->