diff --git a/apps/emqx_authn/src/emqx_authn_api.erl b/apps/emqx_authn/src/emqx_authn_api.erl index 028dd9ac4..b28ded461 100644 --- a/apps/emqx_authn/src/emqx_authn_api.erl +++ b/apps/emqx_authn/src/emqx_authn_api.erl @@ -552,7 +552,7 @@ authenticators(get, _Params) -> list_authenticators([authentication]). authenticator(get, #{bindings := #{id := AuthenticatorID}}) -> - list_authenticator([authentication], AuthenticatorID); + list_authenticator(?GLOBAL, [authentication], AuthenticatorID); authenticator(put, #{bindings := #{id := AuthenticatorID}, body := Config}) -> update_authenticator([authentication], ?GLOBAL, AuthenticatorID, Config); @@ -576,8 +576,8 @@ listener_authenticators(get, #{bindings := #{listener_id := ListenerID}}) -> listener_authenticator(get, #{bindings := #{listener_id := ListenerID, id := AuthenticatorID}}) -> with_listener(ListenerID, - fun(Type, Name, _) -> - list_authenticator([listeners, Type, Name, authentication], + fun(Type, Name, ChainName) -> + list_authenticator(ChainName, [listeners, Type, Name, authentication], AuthenticatorID) end); listener_authenticator(put, @@ -752,11 +752,11 @@ list_authenticators(ConfKeyPath) -> || AuthenticatorConfig <- AuthenticatorsConfig], {200, NAuthenticators}. -list_authenticator(ConfKeyPath, AuthenticatorID) -> +list_authenticator(ChainName, ConfKeyPath, AuthenticatorID) -> AuthenticatorsConfig = get_raw_config_with_defaults(ConfKeyPath), case find_config(AuthenticatorID, AuthenticatorsConfig) of {ok, AuthenticatorConfig} -> - Status_And_Metrics = lookup_from_all_nodes(?GLOBAL, AuthenticatorID), + Status_And_Metrics = lookup_from_all_nodes(ChainName, AuthenticatorID), Fun = fun ({Key, Val}, Map) -> maps:put(Key, Val, Map) end, AppendList = [{id, AuthenticatorID}, {status_and_metrics, Status_And_Metrics}], {200, lists:foldl(Fun, convert_certs(AuthenticatorConfig), AppendList)}; @@ -862,7 +862,9 @@ restructure_map(#{counters := #{failed := Failed, matched := Match, success := S rate => Rate, rate_last5m => Rate5m, rate_max => RateMax - }. + }; +restructure_map(Error) -> + Error. error_msg(Code, Msg) when is_binary(Msg) -> #{code => Code, message => Msg}; diff --git a/apps/emqx_authn/test/emqx_authn_api_SUITE.erl b/apps/emqx_authn/test/emqx_authn_api_SUITE.erl index b0164d93d..dba516346 100644 --- a/apps/emqx_authn/test/emqx_authn_api_SUITE.erl +++ b/apps/emqx_authn/test/emqx_authn_api_SUITE.erl @@ -164,10 +164,38 @@ test_authenticator(PathPrefix) -> post, uri(PathPrefix ++ [?CONF_NS]), ValidConfig0), - {ok, 200, _} = request( + {ok, 200, Res} = request( get, - uri(PathPrefix ++ [?CONF_NS, "password_based:http"])), - + uri(PathPrefix ++ [?CONF_NS, "password-based:http"])), + {ok, RList} = emqx_json:safe_decode(Res), + Snd = fun ({_, Val}) -> Val end, + LookupVal = + fun(FixVal) -> + fun(List, RestJson) -> + case List of + [Name] -> Snd(lists:keyfind(Name, 1, RestJson)); + [Name | NS] -> (FixVal(FixVal))(NS, Snd(lists:keyfind(Name, 1, RestJson))) + end + end + end, + LookFun = fun (List) -> (LookupVal(LookupVal)) (List, RList) end, + MetricsList = [{<<"failed">>, 0}, + {<<"matched">>, 0}, + {<<"rate">>, 0.0}, + {<<"rate_last5m">>, 0.0}, + {<<"rate_max">>, 0.0}, + {<<"success">>, 0}], + EqualFun = fun ({M, V}) -> + ?assertEqual(V, LookFun([<<"status_and_metrics">>, + <<"metrics">>, + M] + ) + ) end, + lists:map(EqualFun, MetricsList), + ?assertEqual(<<"connected">>, + LookFun([<<"status_and_metrics">>, + <<"status">> + ])), {ok, 404, _} = request( get, uri(PathPrefix ++ [?CONF_NS, "password_based:redis"])),