Просмотр исходного кода

fix: disable authn_api, authz_api_sources swagger spec

EMQ-YangM 3 лет назад
Родитель
Сommit
eb5570dd07
2 измененных файлов с 54 добавлено и 32 удалено
  1. 30 18
      apps/emqx_authn/src/emqx_authn_api.erl
  2. 24 14
      apps/emqx_authz/src/emqx_authz_api_sources.erl

+ 30 - 18
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),

+ 24 - 14
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) ->