Ver código fonte

Merge pull request #7319 from EMQ-YangM/ignore_authz_authn_metrics_status

Ignore authz, authn metrics status
Yang Miao 3 anos atrás
pai
commit
e6fcef16ba

+ 24 - 22
apps/emqx_authn/src/emqx_authn_api.erl

@@ -752,17 +752,11 @@ list_authenticators(ConfKeyPath) ->
                         || AuthenticatorConfig <- AuthenticatorsConfig],
                         || AuthenticatorConfig <- AuthenticatorsConfig],
     {200, NAuthenticators}.
     {200, NAuthenticators}.
 
 
-list_authenticator(ChainName, ConfKeyPath, AuthenticatorID) ->
+list_authenticator(_, ConfKeyPath, AuthenticatorID) ->
     AuthenticatorsConfig = get_raw_config_with_defaults(ConfKeyPath),
     AuthenticatorsConfig = get_raw_config_with_defaults(ConfKeyPath),
     case find_config(AuthenticatorID, AuthenticatorsConfig) of
     case find_config(AuthenticatorID, AuthenticatorsConfig) of
         {ok, AuthenticatorConfig} ->
         {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)};
-                {error, ErrorMsg} -> {500, ErrorMsg}
-            end;
+                    {200, maps:put(id, AuthenticatorID, convert_certs(AuthenticatorConfig))};
         {error, Reason} ->
         {error, Reason} ->
             serialize_error(Reason)
             serialize_error(Reason)
     end.
     end.
@@ -796,22 +790,26 @@ lookup_from_all_nodes(ChainName, AuthenticatorID) ->
     Nodes = mria_mnesia:running_nodes(),
     Nodes = mria_mnesia:running_nodes(),
     case is_ok(emqx_authn_proto_v1:lookup_from_all_nodes(Nodes, ChainName, AuthenticatorID)) of
     case is_ok(emqx_authn_proto_v1:lookup_from_all_nodes(Nodes, ChainName, AuthenticatorID)) of
         {ok, ResList} ->
         {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, ErrL} ->
             {error, error_msg('INTERNAL_ERROR', ErrL)}
             {error, error_msg('INTERNAL_ERROR', ErrL)}
     end.
     end.
 
 
-aggregate_status([]) -> error_some_strange_happen;
+aggregate_status([]) -> empty_metrics_and_status;
 aggregate_status(AllStatus) ->
 aggregate_status(AllStatus) ->
     Head = fun ([A | _]) -> A end,
     Head = fun ([A | _]) -> A end,
     HeadVal = Head(AllStatus),
     HeadVal = Head(AllStatus),
@@ -821,7 +819,7 @@ aggregate_status(AllStatus) ->
         false -> inconsistent
         false -> inconsistent
     end.
     end.
 
 
-aggregate_metrics([]) -> error_some_strange_happen;
+aggregate_metrics([]) -> empty_metrics_and_status;
 aggregate_metrics([HeadMetrics | AllMetrics]) ->
 aggregate_metrics([HeadMetrics | AllMetrics]) ->
     CombinerFun =
     CombinerFun =
         fun ComFun(Val1, Val2) ->
         fun ComFun(Val1, Val2) ->
@@ -880,7 +878,11 @@ is_ok(ResL) ->
     end.
     end.
 
 
 update_authenticator(ConfKeyPath, ChainName, AuthenticatorID, Config) ->
 update_authenticator(ConfKeyPath, ChainName, AuthenticatorID, Config) ->
-    case update_config(ConfKeyPath, {update_authenticator, ChainName, AuthenticatorID, Config}) of
+    case update_config(ConfKeyPath,
+                       {update_authenticator,
+                       ChainName,
+                       AuthenticatorID,
+                       Config}) of
         {ok, #{post_config_update := #{emqx_authentication := #{id := ID}},
         {ok, #{post_config_update := #{emqx_authentication := #{id := ID}},
                raw_config := AuthenticatorsConfig}} ->
                raw_config := AuthenticatorsConfig}} ->
             {ok, AuthenticatorConfig} = find_config(ID, AuthenticatorsConfig),
             {ok, AuthenticatorConfig} = find_config(ID, AuthenticatorsConfig),

+ 25 - 25
apps/emqx_authn/test/emqx_authn_api_SUITE.erl

@@ -164,33 +164,33 @@ test_authenticator(PathPrefix) ->
                      post,
                      post,
                      uri(PathPrefix ++ [?CONF_NS]),
                      uri(PathPrefix ++ [?CONF_NS]),
                      ValidConfig0),
                      ValidConfig0),
-    {ok, 200, Res} = request(
+    {ok, 200, _} = request(
                      get,
                      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 LookupV(List, RestJson) ->
-            case List of
-                [Name] -> Snd(lists:keyfind(Name, 1, RestJson));
-                [Name | NS] -> LookupV(NS, Snd(lists:keyfind(Name, 1, RestJson)))
-            end
-        end,
-    LookFun = fun (List) -> 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([<<"metrics">>,
-                                            M]
-                                          )
-                               ) end,
-    lists:map(EqualFun, MetricsList),
-    ?assertEqual(<<"connected">>,
-                 LookFun([<<"status">>
-                         ])),
+    %% {ok, RList} = emqx_json:safe_decode(Res),
+    %% Snd = fun ({_, Val}) -> Val end,
+    %% LookupVal = fun LookupV(List, RestJson) ->
+    %%         case List of
+    %%             [Name] -> Snd(lists:keyfind(Name, 1, RestJson));
+    %%             [Name | NS] -> LookupV(NS, Snd(lists:keyfind(Name, 1, RestJson)))
+    %%         end
+    %%     end,
+    %% LookFun = fun (List) -> 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([<<"metrics">>,
+    %%                                         M]
+    %%                                       )
+    %%                            ) end,
+    %% lists:map(EqualFun, MetricsList),
+    %% ?assertEqual(<<"connected">>,
+    %%              LookFun([<<"status">>
+    %%                      ])),
     {ok, 404, _} = request(
     {ok, 404, _} = request(
                      get,
                      get,
                      uri(PathPrefix ++ [?CONF_NS, "password_based:redis"])),
                      uri(PathPrefix ++ [?CONF_NS, "password_based:redis"])),

+ 19 - 23
apps/emqx_authz/src/emqx_authz_api_sources.erl

@@ -227,16 +227,7 @@ source(get, #{bindings := #{type := Type}}) ->
                     {400, #{code => <<"BAD_REQUEST">>,
                     {400, #{code => <<"BAD_REQUEST">>,
                             message => bin(Reason)}}
                             message => bin(Reason)}}
             end;
             end;
-        [Source] ->
-            case emqx_authz:lookup(Type) of
-                #{annotations := #{id := ResourceId }} ->
-                    case lookup_from_all_nodes(ResourceId) of
-                        {ok, StatusAndMetrics} ->
-                            {200, maps:merge(read_certs(Source), StatusAndMetrics)};
-                        {error, ErrorMsg} -> {500, ErrorMsg}
-                    end;
-                _ -> {200, read_certs(Source)}
-            end
+        [Source] -> {200, read_certs(Source)}
     end;
     end;
 source(put, #{bindings := #{type := <<"file">>}, body := #{<<"type">> := <<"file">>,
 source(put, #{bindings := #{type := <<"file">>}, body := #{<<"type">> := <<"file">>,
                                                            <<"rules">> := Rules,
                                                            <<"rules">> := Rules,
@@ -254,7 +245,8 @@ source(put, #{bindings := #{type := <<"file">>}, body := #{<<"type">> := <<"file
                     message => bin(Reason)}}
                     message => bin(Reason)}}
     end;
     end;
 source(put, #{bindings := #{type := Type}, body := Body}) when is_map(Body) ->
 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(Body#{<<"type">> => Type}));
 source(delete, #{bindings := #{type := Type}}) ->
 source(delete, #{bindings := #{type := Type}}) ->
     update_config({?CMD_DELETE, Type}, #{}).
     update_config({?CMD_DELETE, Type}, #{}).
 
 
@@ -302,22 +294,26 @@ lookup_from_all_nodes(ResourceId) ->
     Nodes = mria_mnesia:running_nodes(),
     Nodes = mria_mnesia:running_nodes(),
     case is_ok(emqx_authz_proto_v1:lookup_from_all_nodes(Nodes, ResourceId)) of
     case is_ok(emqx_authz_proto_v1:lookup_from_all_nodes(Nodes, ResourceId)) of
         {ok, ResList} ->
         {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, ErrL} ->
             {error, error_msg('INTERNAL_ERROR', ErrL)}
             {error, error_msg('INTERNAL_ERROR', ErrL)}
     end.
     end.
 
 
-aggregate_status([]) -> error_some_strange_happen;
+aggregate_status([]) -> empty_metrics_and_status;
 aggregate_status(AllStatus) ->
 aggregate_status(AllStatus) ->
     Head = fun ([A | _]) -> A end,
     Head = fun ([A | _]) -> A end,
     HeadVal = Head(AllStatus),
     HeadVal = Head(AllStatus),
@@ -327,7 +323,7 @@ aggregate_status(AllStatus) ->
         false -> inconsistent
         false -> inconsistent
     end.
     end.
 
 
-aggregate_metrics([]) -> error_some_strange_happen;
+aggregate_metrics([]) -> empty_metrics_and_status;
 aggregate_metrics([HeadMetrics | AllMetrics]) ->
 aggregate_metrics([HeadMetrics | AllMetrics]) ->
     CombinerFun =
     CombinerFun =
         fun ComFun(Val1, Val2) ->
         fun ComFun(Val1, Val2) ->

+ 28 - 28
apps/emqx_authz/test/emqx_authz_api_sources_SUITE.erl

@@ -176,32 +176,32 @@ t_api(_) ->
                            [?SOURCE2, ?SOURCE3, ?SOURCE4, ?SOURCE5, ?SOURCE6]),
                            [?SOURCE2, ?SOURCE3, ?SOURCE4, ?SOURCE5, ?SOURCE6]),
     {ok, 204, _} = request(post, uri(["authorization", "sources"]), ?SOURCE1),
     {ok, 204, _} = request(post, uri(["authorization", "sources"]), ?SOURCE1),
 
 
-    Snd = fun ({_, Val}) -> Val end,
-    LookupVal = fun LookupV(List, RestJson) ->
-                        case List of
-                            [Name] -> Snd(lists:keyfind(Name, 1, RestJson));
-                            [Name | NS] -> LookupV(NS, Snd(lists:keyfind(Name, 1, RestJson)))
-                        end
-                end,
-    EqualFun = fun (RList) ->
-                   fun ({M, V}) ->
-                       ?assertEqual(V,
-                                    LookupVal([<<"metrics">>, M],
-                                             RList)
-                                   )
-                   end
-               end,
-    AssertFun =
-        fun (ResultJson) ->
-            {ok, RList} = emqx_json:safe_decode(ResultJson),
-            MetricsList = [{<<"failed">>, 0},
-                           {<<"matched">>, 0},
-                           {<<"rate">>, 0.0},
-                           {<<"rate_last5m">>, 0.0},
-                           {<<"rate_max">>, 0.0},
-                           {<<"success">>, 0}],
-            lists:map(EqualFun(RList), MetricsList)
-        end,
+    %% Snd = fun ({_, Val}) -> Val end,
+    %% LookupVal = fun LookupV(List, RestJson) ->
+    %%                     case List of
+    %%                         [Name] -> Snd(lists:keyfind(Name, 1, RestJson));
+    %%                         [Name | NS] -> LookupV(NS, Snd(lists:keyfind(Name, 1, RestJson)))
+    %%                     end
+    %%             end,
+    %% EqualFun = fun (RList) ->
+    %%                fun ({M, V}) ->
+    %%                    ?assertEqual(V,
+    %%                                 LookupVal([<<"metrics">>, M],
+    %%                                          RList)
+    %%                                )
+    %%                end
+    %%            end,
+    %% AssertFun =
+    %%     fun (ResultJson) ->
+    %%         {ok, RList} = emqx_json:safe_decode(ResultJson),
+    %%         MetricsList = [{<<"failed">>, 0},
+    %%                        {<<"matched">>, 0},
+    %%                        {<<"rate">>, 0.0},
+    %%                        {<<"rate_last5m">>, 0.0},
+    %%                        {<<"rate_max">>, 0.0},
+    %%                        {<<"success">>, 0}],
+    %%         lists:map(EqualFun(RList), MetricsList)
+    %%     end,
 
 
     {ok, 200, Result2} = request(get, uri(["authorization", "sources"]), []),
     {ok, 200, Result2} = request(get, uri(["authorization", "sources"]), []),
     Sources = get_sources(Result2),
     Sources = get_sources(Result2),
@@ -238,7 +238,7 @@ t_api(_) ->
                                          <<"verify">> => <<"verify_none">>
                                          <<"verify">> => <<"verify_none">>
                                         }}),
                                         }}),
     {ok, 200, Result4} = request(get, uri(["authorization", "sources", "mongodb"]), []),
     {ok, 200, Result4} = request(get, uri(["authorization", "sources", "mongodb"]), []),
-    AssertFun(Result4),
+    %% AssertFun(Result4),
     ?assertMatch(#{<<"type">> := <<"mongodb">>,
     ?assertMatch(#{<<"type">> := <<"mongodb">>,
                    <<"ssl">> := #{<<"enable">> := <<"true">>,
                    <<"ssl">> := #{<<"enable">> := <<"true">>,
                                   <<"cacertfile">> := ?MATCH_CERT,
                                   <<"cacertfile">> := ?MATCH_CERT,
@@ -261,7 +261,7 @@ t_api(_) ->
                                          <<"verify">> => <<"verify_none">>
                                          <<"verify">> => <<"verify_none">>
                                         }}),
                                         }}),
     {ok, 200, Result5} = request(get, uri(["authorization", "sources", "mongodb"]), []),
     {ok, 200, Result5} = request(get, uri(["authorization", "sources", "mongodb"]), []),
-    AssertFun(Result5),
+    %% AssertFun(Result5),
     ?assertMatch(#{<<"type">> := <<"mongodb">>,
     ?assertMatch(#{<<"type">> := <<"mongodb">>,
                    <<"ssl">> := #{<<"enable">> := <<"true">>,
                    <<"ssl">> := #{<<"enable">> := <<"true">>,
                                   <<"cacertfile">> := ?MATCH_CERT,
                                   <<"cacertfile">> := ?MATCH_CERT,