Merge pull request #13317 from thalesmg/authz-ignore-metric-m-20240621
feat(authz): add `ignore` metric for each source type
This commit is contained in:
commit
20c47243ab
|
@ -1,7 +1,7 @@
|
||||||
%% -*- mode: erlang -*-
|
%% -*- mode: erlang -*-
|
||||||
{application, emqx_auth, [
|
{application, emqx_auth, [
|
||||||
{description, "EMQX Authentication and authorization"},
|
{description, "EMQX Authentication and authorization"},
|
||||||
{vsn, "0.3.2"},
|
{vsn, "0.3.3"},
|
||||||
{modules, []},
|
{modules, []},
|
||||||
{registered, [emqx_auth_sup]},
|
{registered, [emqx_auth_sup]},
|
||||||
{applications, [
|
{applications, [
|
||||||
|
|
|
@ -408,7 +408,7 @@ init_metrics(Source) ->
|
||||||
emqx_metrics_worker:create_metrics(
|
emqx_metrics_worker:create_metrics(
|
||||||
authz_metrics,
|
authz_metrics,
|
||||||
TypeName,
|
TypeName,
|
||||||
[total, allow, deny, nomatch],
|
[total, allow, deny, nomatch, ignore],
|
||||||
[total]
|
[total]
|
||||||
)
|
)
|
||||||
end.
|
end.
|
||||||
|
@ -518,6 +518,7 @@ do_authorize(
|
||||||
}),
|
}),
|
||||||
do_authorize(Client, PubSub, Topic, Tail);
|
do_authorize(Client, PubSub, Topic, Tail);
|
||||||
ignore ->
|
ignore ->
|
||||||
|
emqx_metrics_worker:inc(authz_metrics, Type, ignore),
|
||||||
?TRACE("AUTHZ", "authorization_module_ignore", #{
|
?TRACE("AUTHZ", "authorization_module_ignore", #{
|
||||||
module => Module,
|
module => Module,
|
||||||
username => Username,
|
username => Username,
|
||||||
|
|
|
@ -529,6 +529,68 @@ t_bad_response_content_type(_Config) ->
|
||||||
end
|
end
|
||||||
).
|
).
|
||||||
|
|
||||||
|
%% Checks that we bump the correct metrics when we receive an error response
|
||||||
|
t_bad_response(_Config) ->
|
||||||
|
ok = setup_handler_and_config(
|
||||||
|
fun(Req0, State) ->
|
||||||
|
?assertEqual(
|
||||||
|
<<"/authz/users/">>,
|
||||||
|
cowboy_req:path(Req0)
|
||||||
|
),
|
||||||
|
|
||||||
|
{ok, _PostVars, Req1} = cowboy_req:read_urlencoded_body(Req0),
|
||||||
|
|
||||||
|
Req = cowboy_req:reply(
|
||||||
|
400,
|
||||||
|
#{<<"content-type">> => <<"application/json">>},
|
||||||
|
"{\"error\":true}",
|
||||||
|
Req1
|
||||||
|
),
|
||||||
|
{ok, Req, State}
|
||||||
|
end,
|
||||||
|
#{
|
||||||
|
<<"method">> => <<"post">>,
|
||||||
|
<<"body">> => #{
|
||||||
|
<<"username">> => <<"${username}">>
|
||||||
|
},
|
||||||
|
<<"headers">> => #{}
|
||||||
|
}
|
||||||
|
),
|
||||||
|
|
||||||
|
ClientInfo = #{
|
||||||
|
clientid => <<"client id">>,
|
||||||
|
username => <<"user name">>,
|
||||||
|
peerhost => {127, 0, 0, 1},
|
||||||
|
protocol => <<"MQTT">>,
|
||||||
|
mountpoint => <<"MOUNTPOINT">>,
|
||||||
|
zone => default,
|
||||||
|
listener => {tcp, default},
|
||||||
|
cn => ?PH_CERT_CN_NAME,
|
||||||
|
dn => ?PH_CERT_SUBJECT
|
||||||
|
},
|
||||||
|
|
||||||
|
?assertEqual(
|
||||||
|
deny,
|
||||||
|
emqx_access_control:authorize(ClientInfo, ?AUTHZ_PUBLISH, <<"t">>)
|
||||||
|
),
|
||||||
|
?assertMatch(
|
||||||
|
#{
|
||||||
|
counters := #{
|
||||||
|
total := 1,
|
||||||
|
ignore := 1,
|
||||||
|
nomatch := 0,
|
||||||
|
allow := 0,
|
||||||
|
deny := 0
|
||||||
|
},
|
||||||
|
'authorization.superuser' := 0,
|
||||||
|
'authorization.matched.allow' := 0,
|
||||||
|
'authorization.matched.deny' := 0,
|
||||||
|
'authorization.nomatch' := 1
|
||||||
|
},
|
||||||
|
get_metrics()
|
||||||
|
),
|
||||||
|
ok.
|
||||||
|
|
||||||
t_no_value_for_placeholder(_Config) ->
|
t_no_value_for_placeholder(_Config) ->
|
||||||
ok = setup_handler_and_config(
|
ok = setup_handler_and_config(
|
||||||
fun(Req0, State) ->
|
fun(Req0, State) ->
|
||||||
|
@ -729,3 +791,18 @@ start_apps(Apps) ->
|
||||||
|
|
||||||
stop_apps(Apps) ->
|
stop_apps(Apps) ->
|
||||||
lists:foreach(fun application:stop/1, Apps).
|
lists:foreach(fun application:stop/1, Apps).
|
||||||
|
|
||||||
|
get_metrics() ->
|
||||||
|
Metrics = emqx_metrics_worker:get_metrics(authz_metrics, http),
|
||||||
|
lists:foldl(
|
||||||
|
fun(Name, Acc) ->
|
||||||
|
Acc#{Name => emqx_metrics:val(Name)}
|
||||||
|
end,
|
||||||
|
Metrics,
|
||||||
|
[
|
||||||
|
'authorization.superuser',
|
||||||
|
'authorization.matched.allow',
|
||||||
|
'authorization.matched.deny',
|
||||||
|
'authorization.nomatch'
|
||||||
|
]
|
||||||
|
).
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Added a new per-authorization source metric type: `ignore`. The meaning of this counter is that it's increased whenever the authorization source attempts to authorize a request, but either it's not applicable, or an error was encountered and the result is undecidable.
|
Loading…
Reference in New Issue