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:
Thales Macedo Garitezi 2024-06-27 17:43:06 -03:00 committed by GitHub
commit 20c47243ab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 81 additions and 2 deletions

View File

@ -1,7 +1,7 @@
%% -*- mode: erlang -*-
{application, emqx_auth, [
{description, "EMQX Authentication and authorization"},
{vsn, "0.3.2"},
{vsn, "0.3.3"},
{modules, []},
{registered, [emqx_auth_sup]},
{applications, [

View File

@ -408,7 +408,7 @@ init_metrics(Source) ->
emqx_metrics_worker:create_metrics(
authz_metrics,
TypeName,
[total, allow, deny, nomatch],
[total, allow, deny, nomatch, ignore],
[total]
)
end.
@ -518,6 +518,7 @@ do_authorize(
}),
do_authorize(Client, PubSub, Topic, Tail);
ignore ->
emqx_metrics_worker:inc(authz_metrics, Type, ignore),
?TRACE("AUTHZ", "authorization_module_ignore", #{
module => Module,
username => Username,

View File

@ -529,6 +529,68 @@ t_bad_response_content_type(_Config) ->
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) ->
ok = setup_handler_and_config(
fun(Req0, State) ->
@ -729,3 +791,18 @@ start_apps(Apps) ->
stop_apps(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'
]
).

View File

@ -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.