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],
{200, NAuthenticators}.
list_authenticator(ChainName, ConfKeyPath, AuthenticatorID) ->
list_authenticator(_, ConfKeyPath, AuthenticatorID) ->
AuthenticatorsConfig = get_raw_config_with_defaults(ConfKeyPath),
case find_config(AuthenticatorID, AuthenticatorsConfig) of
{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} ->
serialize_error(Reason)
end.
@ -796,22 +790,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 +819,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) ->
@ -880,7 +878,11 @@ is_ok(ResL) ->
end.
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}},
raw_config := AuthenticatorsConfig}} ->
{ok, AuthenticatorConfig} = find_config(ID, AuthenticatorsConfig),

View File

@ -164,33 +164,33 @@ test_authenticator(PathPrefix) ->
post,
uri(PathPrefix ++ [?CONF_NS]),
ValidConfig0),
{ok, 200, Res} = request(
{ok, 200, _} = request(
get,
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(
get,
uri(PathPrefix ++ [?CONF_NS, "password_based:redis"])),

View File

@ -227,16 +227,7 @@ source(get, #{bindings := #{type := Type}}) ->
{400, #{code => <<"BAD_REQUEST">>,
message => bin(Reason)}}
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;
source(put, #{bindings := #{type := <<"file">>}, body := #{<<"type">> := <<"file">>,
<<"rules">> := Rules,
@ -254,7 +245,8 @@ 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(Body#{<<"type">> => Type}));
source(delete, #{bindings := #{type := Type}}) ->
update_config({?CMD_DELETE, Type}, #{}).
@ -302,22 +294,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 +323,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) ->

View File

@ -176,32 +176,32 @@ t_api(_) ->
[?SOURCE2, ?SOURCE3, ?SOURCE4, ?SOURCE5, ?SOURCE6]),
{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"]), []),
Sources = get_sources(Result2),
@ -238,7 +238,7 @@ t_api(_) ->
<<"verify">> => <<"verify_none">>
}}),
{ok, 200, Result4} = request(get, uri(["authorization", "sources", "mongodb"]), []),
AssertFun(Result4),
%% AssertFun(Result4),
?assertMatch(#{<<"type">> := <<"mongodb">>,
<<"ssl">> := #{<<"enable">> := <<"true">>,
<<"cacertfile">> := ?MATCH_CERT,
@ -261,7 +261,7 @@ t_api(_) ->
<<"verify">> => <<"verify_none">>
}}),
{ok, 200, Result5} = request(get, uri(["authorization", "sources", "mongodb"]), []),
AssertFun(Result5),
%% AssertFun(Result5),
?assertMatch(#{<<"type">> := <<"mongodb">>,
<<"ssl">> := #{<<"enable">> := <<"true">>,
<<"cacertfile">> := ?MATCH_CERT,