Merge pull request #7319 from EMQ-YangM/ignore_authz_authn_metrics_status

Ignore authz, authn metrics status
This commit is contained in:
Yang Miao 2022-03-16 14:41:37 +08:00 committed by GitHub
commit e6fcef16ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 96 additions and 98 deletions

View File

@ -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 {200, maps:put(id, AuthenticatorID, convert_certs(AuthenticatorConfig))};
{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;
{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), {StatusMap, MetricsMap, _} = make_result_map(ResList),
AggregateStatus = aggregate_status(maps:values(StatusMap)), AggregateStatus = aggregate_status(maps:values(StatusMap)),
AggregateMetrics = aggregate_metrics(maps:values(MetricsMap)), AggregateMetrics = aggregate_metrics(maps:values(MetricsMap)),
Fun = fun(_, V1) -> restructure_map(V1) end, Fun = fun (_, V1) -> restructure_map(V1) end,
{ok, #{node_status => StatusMap, MKMap = fun (Name) -> fun ({Key, Val}) -> #{ node => Key, Name => Val } end end,
node_metrics => maps:map(Fun, MetricsMap), HelpFun = fun (M, Name) -> lists:map(MKMap(Name), maps:to_list(M)) end,
node_error => ErrorMap, case AggregateStatus of
empty_metrics_and_status -> {ok, #{}};
_ -> {ok, #{node_status => HelpFun(StatusMap, status),
node_metrics => HelpFun(maps:map(Fun, MetricsMap), metrics),
status => AggregateStatus, status => AggregateStatus,
metrics => restructure_map(AggregateMetrics) 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),

View File

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

View File

@ -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] -> [Source] -> {200, read_certs(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
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), {StatusMap, MetricsMap, _} = make_result_map(ResList),
AggregateStatus = aggregate_status(maps:values(StatusMap)), AggregateStatus = aggregate_status(maps:values(StatusMap)),
AggregateMetrics = aggregate_metrics(maps:values(MetricsMap)), AggregateMetrics = aggregate_metrics(maps:values(MetricsMap)),
Fun = fun(_, V1) -> restructure_map(V1) end, Fun = fun (_, V1) -> restructure_map(V1) end,
{ok, #{node_status => StatusMap, MKMap = fun (Name) -> fun ({Key, Val}) -> #{ node => Key, Name => Val } end end,
node_metrics => maps:map(Fun, MetricsMap), HelpFun = fun (M, Name) -> lists:map(MKMap(Name), maps:to_list(M)) end,
node_error => ErrorMap, case AggregateStatus of
empty_metrics_and_status -> {ok, #{}};
_ -> {ok, #{node_status => HelpFun(StatusMap, status),
node_metrics => HelpFun(maps:map(Fun, MetricsMap), metrics),
status => AggregateStatus, status => AggregateStatus,
metrics => restructure_map(AggregateMetrics) 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) ->

View File

@ -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, %% Snd = fun ({_, Val}) -> Val end,
LookupVal = fun LookupV(List, RestJson) -> %% LookupVal = fun LookupV(List, RestJson) ->
case List of %% case List of
[Name] -> Snd(lists:keyfind(Name, 1, RestJson)); %% [Name] -> Snd(lists:keyfind(Name, 1, RestJson));
[Name | NS] -> LookupV(NS, Snd(lists:keyfind(Name, 1, RestJson))) %% [Name | NS] -> LookupV(NS, Snd(lists:keyfind(Name, 1, RestJson)))
end %% end
end, %% end,
EqualFun = fun (RList) -> %% EqualFun = fun (RList) ->
fun ({M, V}) -> %% fun ({M, V}) ->
?assertEqual(V, %% ?assertEqual(V,
LookupVal([<<"metrics">>, M], %% LookupVal([<<"metrics">>, M],
RList) %% RList)
) %% )
end %% end
end, %% end,
AssertFun = %% AssertFun =
fun (ResultJson) -> %% fun (ResultJson) ->
{ok, RList} = emqx_json:safe_decode(ResultJson), %% {ok, RList} = emqx_json:safe_decode(ResultJson),
MetricsList = [{<<"failed">>, 0}, %% MetricsList = [{<<"failed">>, 0},
{<<"matched">>, 0}, %% {<<"matched">>, 0},
{<<"rate">>, 0.0}, %% {<<"rate">>, 0.0},
{<<"rate_last5m">>, 0.0}, %% {<<"rate_last5m">>, 0.0},
{<<"rate_max">>, 0.0}, %% {<<"rate_max">>, 0.0},
{<<"success">>, 0}], %% {<<"success">>, 0}],
lists:map(EqualFun(RList), MetricsList) %% lists:map(EqualFun(RList), MetricsList)
end, %% 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,