From eb5570dd07f81c94014103b2beb6cdf1df801c05 Mon Sep 17 00:00:00 2001 From: EMQ-YangM Date: Mon, 14 Mar 2022 19:12:06 +0800 Subject: [PATCH] fix: disable authn_api, authz_api_sources swagger spec --- apps/emqx_authn/src/emqx_authn_api.erl | 48 ++++++++++++------- .../emqx_authz/src/emqx_authz_api_sources.erl | 38 +++++++++------ 2 files changed, 54 insertions(+), 32 deletions(-) diff --git a/apps/emqx_authn/src/emqx_authn_api.erl b/apps/emqx_authn/src/emqx_authn_api.erl index 2266abfb2..b06eb02e4 100644 --- a/apps/emqx_authn/src/emqx_authn_api.erl +++ b/apps/emqx_authn/src/emqx_authn_api.erl @@ -84,7 +84,7 @@ -elvis([{elvis_style, god_modules, disable}]). api_spec() -> - emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true}). + emqx_dashboard_swagger:spec(?MODULE, #{check_schema => false}). paths() -> [ "/authentication" , "/authentication/:id" @@ -758,9 +758,8 @@ list_authenticator(ChainName, ConfKeyPath, AuthenticatorID) -> {ok, AuthenticatorConfig} -> case lookup_from_all_nodes(ChainName, AuthenticatorID) of {ok, StatusAndMetrics} -> - Fun = fun ({Key, Val}, Map) -> maps:put(Key, Val, Map) end, - AppendList = [{id, AuthenticatorID} | maps:to_list(StatusAndMetrics)], - {200, lists:foldl(Fun, convert_certs(AuthenticatorConfig), AppendList)}; + {200, maps:merge(convert_certs(AuthenticatorConfig), + maps:put(id, AuthenticatorID, StatusAndMetrics))}; {error, ErrorMsg} -> {500, ErrorMsg} end; {error, Reason} -> @@ -796,22 +795,26 @@ lookup_from_all_nodes(ChainName, AuthenticatorID) -> Nodes = mria_mnesia:running_nodes(), case is_ok(emqx_authn_proto_v1:lookup_from_all_nodes(Nodes, ChainName, AuthenticatorID)) of {ok, ResList} -> - {StatusMap, MetricsMap, ErrorMap} = make_result_map(ResList), - AggregateStatus = aggregate_status(maps:values(StatusMap)), - AggregateMetrics = aggregate_metrics(maps:values(MetricsMap)), - Fun = fun(_, V1) -> restructure_map(V1) end, - {ok, #{node_status => StatusMap, - node_metrics => maps:map(Fun, MetricsMap), - node_error => ErrorMap, - status => AggregateStatus, - metrics => restructure_map(AggregateMetrics) - } - }; + {StatusMap, MetricsMap, _} = make_result_map(ResList), + AggregateStatus = aggregate_status(maps:values(StatusMap)), + AggregateMetrics = aggregate_metrics(maps:values(MetricsMap)), + Fun = fun (_, V1) -> restructure_map(V1) end, + MKMap = fun (Name) -> fun ({Key, Val}) -> #{ node => Key, Name => Val } end end, + HelpFun = fun (M, Name) -> lists:map(MKMap(Name), maps:to_list(M)) end, + case AggregateStatus of + empty_metrics_and_status -> {ok, #{}}; + _ -> {ok, #{node_status => HelpFun(StatusMap, status), + node_metrics => HelpFun(maps:map(Fun, MetricsMap), metrics), + status => AggregateStatus, + metrics => restructure_map(AggregateMetrics) + } + } + end; {error, ErrL} -> {error, error_msg('INTERNAL_ERROR', ErrL)} end. -aggregate_status([]) -> error_some_strange_happen; +aggregate_status([]) -> empty_metrics_and_status; aggregate_status(AllStatus) -> Head = fun ([A | _]) -> A end, HeadVal = Head(AllStatus), @@ -821,7 +824,7 @@ aggregate_status(AllStatus) -> false -> inconsistent end. -aggregate_metrics([]) -> error_some_strange_happen; +aggregate_metrics([]) -> empty_metrics_and_status; aggregate_metrics([HeadMetrics | AllMetrics]) -> CombinerFun = fun ComFun(Val1, Val2) -> @@ -879,8 +882,17 @@ is_ok(ResL) -> ErrL -> {error, ErrL} end. +filter_out_request_body(Conf) -> + ExtraConfs = [<<"status">>, <<"node_status">>, + <<"node_metrics">>, <<"metrics">>, <<"node">>], + maps:without(ExtraConfs, Conf). + update_authenticator(ConfKeyPath, ChainName, AuthenticatorID, Config) -> - case update_config(ConfKeyPath, {update_authenticator, ChainName, AuthenticatorID, Config}) of + case update_config(ConfKeyPath, + {update_authenticator, + ChainName, + AuthenticatorID, + filter_out_request_body(Config)}) of {ok, #{post_config_update := #{emqx_authentication := #{id := ID}}, raw_config := AuthenticatorsConfig}} -> {ok, AuthenticatorConfig} = find_config(ID, AuthenticatorsConfig), diff --git a/apps/emqx_authz/src/emqx_authz_api_sources.erl b/apps/emqx_authz/src/emqx_authz_api_sources.erl index 58eed9982..dce3d4d04 100644 --- a/apps/emqx_authz/src/emqx_authz_api_sources.erl +++ b/apps/emqx_authz/src/emqx_authz_api_sources.erl @@ -69,7 +69,7 @@ ]). api_spec() -> - emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true}). + emqx_dashboard_swagger:spec(?MODULE, #{check_schema => false}). paths() -> [ "/authorization/sources" @@ -254,10 +254,16 @@ source(put, #{bindings := #{type := <<"file">>}, body := #{<<"type">> := <<"file message => bin(Reason)}} end; source(put, #{bindings := #{type := Type}, body := Body}) when is_map(Body) -> - update_config({?CMD_REPLACE, Type}, maybe_write_certs(Body#{<<"type">> => Type})); + update_config({?CMD_REPLACE, Type}, + maybe_write_certs(filter_out_request_body(Body#{<<"type">> => Type}))); source(delete, #{bindings := #{type := Type}}) -> update_config({?CMD_DELETE, Type}, #{}). +filter_out_request_body(Conf) -> + ExtraConfs = [<<"status">>, <<"node_status">>, + <<"node_metrics">>, <<"metrics">>, <<"node">>], + maps:without(ExtraConfs, Conf). + move_source(Method, #{bindings := #{type := Type} = Bindings } = Req) when is_atom(Type) -> move_source(Method, Req#{bindings => Bindings#{type => atom_to_binary(Type, utf8)}}); @@ -302,22 +308,26 @@ lookup_from_all_nodes(ResourceId) -> Nodes = mria_mnesia:running_nodes(), case is_ok(emqx_authz_proto_v1:lookup_from_all_nodes(Nodes, ResourceId)) of {ok, ResList} -> - {StatusMap, MetricsMap, ErrorMap} = make_result_map(ResList), - AggregateStatus = aggregate_status(maps:values(StatusMap)), - AggregateMetrics = aggregate_metrics(maps:values(MetricsMap)), - Fun = fun(_, V1) -> restructure_map(V1) end, - {ok, #{node_status => StatusMap, - node_metrics => maps:map(Fun, MetricsMap), - node_error => ErrorMap, - status => AggregateStatus, - metrics => restructure_map(AggregateMetrics) + {StatusMap, MetricsMap, _} = make_result_map(ResList), + AggregateStatus = aggregate_status(maps:values(StatusMap)), + AggregateMetrics = aggregate_metrics(maps:values(MetricsMap)), + Fun = fun (_, V1) -> restructure_map(V1) end, + MKMap = fun (Name) -> fun ({Key, Val}) -> #{ node => Key, Name => Val } end end, + HelpFun = fun (M, Name) -> lists:map(MKMap(Name), maps:to_list(M)) end, + case AggregateStatus of + empty_metrics_and_status -> {ok, #{}}; + _ -> {ok, #{node_status => HelpFun(StatusMap, status), + node_metrics => HelpFun(maps:map(Fun, MetricsMap), metrics), + status => AggregateStatus, + metrics => restructure_map(AggregateMetrics) + } } - }; + end; {error, ErrL} -> {error, error_msg('INTERNAL_ERROR', ErrL)} end. -aggregate_status([]) -> error_some_strange_happen; +aggregate_status([]) -> empty_metrics_and_status; aggregate_status(AllStatus) -> Head = fun ([A | _]) -> A end, HeadVal = Head(AllStatus), @@ -327,7 +337,7 @@ aggregate_status(AllStatus) -> false -> inconsistent end. -aggregate_metrics([]) -> error_some_strange_happen; +aggregate_metrics([]) -> empty_metrics_and_status; aggregate_metrics([HeadMetrics | AllMetrics]) -> CombinerFun = fun ComFun(Val1, Val2) ->