From c6e52b32fb71b94d9ba3e921e032d0282750debe Mon Sep 17 00:00:00 2001 From: zhouzb Date: Thu, 9 Sep 2021 09:32:18 +0800 Subject: [PATCH] chore(authn): improve code of moving authenticator --- apps/emqx/src/emqx_authentication.erl | 30 +++++++++++++++----------- apps/emqx_authn/src/emqx_authn_api.erl | 22 +++++++++++++++---- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/apps/emqx/src/emqx_authentication.erl b/apps/emqx/src/emqx_authentication.erl index b9973033c..c5028d348 100644 --- a/apps/emqx/src/emqx_authentication.erl +++ b/apps/emqx/src/emqx_authentication.erl @@ -78,6 +78,14 @@ -define(VER_1, <<"1">>). -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 state() :: #{atom() => term()}. -type extra() :: #{is_superuser := boolean(), @@ -159,6 +167,8 @@ authentication(_) -> undefined. %% Callbacks of config handler %%------------------------------------------------------------------------------ +-spec pre_config_update(update_request(), emqx_config:raw_config()) + -> {ok, map() | list()} | {error, term()}. pre_config_update(UpdateReq, OldConfig) -> case do_pre_config_update(UpdateReq, to_list(OldConfig)) of {error, Reason} -> {error, Reason}; @@ -185,22 +195,22 @@ do_pre_config_update({move_authenticator, _ChainName, AuthenticatorID, Position} {error, Reason} -> {error, Reason}; {ok, Part1, [Found | Part2]} -> case Position of - <<"top">> -> + top -> {ok, [Found | Part1] ++ Part2}; - <<"bottom">> -> + bottom -> {ok, Part1 ++ Part2 ++ [Found]}; - <<"before:", Before/binary>> -> + {before, Before} -> case split_by_id(Before, Part1 ++ Part2) of {error, Reason} -> {error, Reason}; {ok, NPart1, [NFound | NPart2]} -> {ok, NPart1 ++ [Found, NFound | NPart2]} - end; - _ -> - {error, {invalid_parameter, position}} + 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) -> 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); do_post_config_update({move_authenticator, ChainName, AuthenticatorID, Position}, _NewConfig, _OldConfig, _AppEnvs) -> - NPosition = case Position of - <<"top">> -> top; - <<"bottom">> -> bottom; - <<"before:", Before/binary>> -> - {before, Before} - end, - move_authenticator(ChainName, AuthenticatorID, NPosition). + move_authenticator(ChainName, AuthenticatorID, Position). check_config(Config) -> #{authentication := CheckedConfig} = hocon_schema:check_plain(emqx_authentication, diff --git a/apps/emqx_authn/src/emqx_authn_api.erl b/apps/emqx_authn/src/emqx_authn_api.erl index 02f3060db..f50cceccb 100644 --- a/apps/emqx_authn/src/emqx_authn_api.erl +++ b/apps/emqx_authn/src/emqx_authn_api.erl @@ -1877,10 +1877,15 @@ delete_authenticator(ConfKeyPath, ChainName0, AuthenticatorID) -> move_authenitcator(ConfKeyPath, ChainName0, AuthenticatorID, Position) -> ChainName = to_atom(ChainName0), - case update_config(ConfKeyPath, {move_authenticator, ChainName, AuthenticatorID, Position}) of - {ok, _} -> - {204}; - {error, {_, _, Reason}} -> + case parse_position(Position) of + {ok, NPosition} -> + case update_config(ConfKeyPath, {move_authenticator, ChainName, AuthenticatorID, NPosition}) of + {ok, _} -> + {204}; + {error, {_, _, Reason}} -> + serialize_error(Reason) + end; + {error, Reason} -> serialize_error(Reason) end. @@ -2016,6 +2021,15 @@ serialize_error(Reason) -> {400, #{code => <<"BAD_REQUEST">>, 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) -> [M]; to_list(L) when is_list(L) ->