feat(auth): add legacy ${access} placeholder

This commit is contained in:
Ilya Averyanov 2024-05-30 14:56:56 +03:00
parent 0c4da98b52
commit 97f9c81e19
2 changed files with 25 additions and 4 deletions

View File

@ -39,6 +39,10 @@
-compile(nowarn_export_all). -compile(nowarn_export_all).
-endif. -endif.
-define(PH_ACCESS, <<"${access}">>).
-define(LEGACY_SUBSCRIBE_ACTION, 1).
-define(LEGACY_PUBLISH_ACTION, 2).
-define(PLACEHOLDERS, [ -define(PLACEHOLDERS, [
?PH_USERNAME, ?PH_USERNAME,
?PH_CLIENTID, ?PH_CLIENTID,
@ -48,7 +52,8 @@
?PH_TOPIC, ?PH_TOPIC,
?PH_ACTION, ?PH_ACTION,
?PH_CERT_SUBJECT, ?PH_CERT_SUBJECT,
?PH_CERT_CN_NAME ?PH_CERT_CN_NAME,
?PH_ACCESS
]). ]).
-define(PLACEHOLDERS_FOR_RICH_ACTIONS, [ -define(PLACEHOLDERS_FOR_RICH_ACTIONS, [
@ -234,7 +239,14 @@ serialize_body(<<"application/x-www-form-urlencoded">>, Body) ->
client_vars(Client, Action, Topic) -> client_vars(Client, Action, Topic) ->
Vars = emqx_authz_utils:vars_for_rule_query(Client, Action), Vars = emqx_authz_utils:vars_for_rule_query(Client, Action),
Vars#{topic => Topic}. add_legacy_access_var(Vars#{topic => Topic}).
add_legacy_access_var(#{action := subscribe} = Vars) ->
Vars#{access => ?LEGACY_SUBSCRIBE_ACTION};
add_legacy_access_var(#{action := publish} = Vars) ->
Vars#{access => ?LEGACY_PUBLISH_ACTION};
add_legacy_access_var(Vars) ->
Vars.
to_list(A) when is_atom(A) -> to_list(A) when is_atom(A) ->
atom_to_list(A); atom_to_list(A);

View File

@ -202,6 +202,7 @@ t_query_params(_Config) ->
mountpoint := <<"MOUNTPOINT">>, mountpoint := <<"MOUNTPOINT">>,
topic := <<"t/1">>, topic := <<"t/1">>,
action := <<"publish">>, action := <<"publish">>,
access := <<"2">>,
qos := <<"1">>, qos := <<"1">>,
retain := <<"false">> retain := <<"false">>
} = cowboy_req:match_qs( } = cowboy_req:match_qs(
@ -213,6 +214,7 @@ t_query_params(_Config) ->
mountpoint, mountpoint,
topic, topic,
action, action,
access,
qos, qos,
retain retain
], ],
@ -230,6 +232,7 @@ t_query_params(_Config) ->
"mountpoint=${mountpoint}&" "mountpoint=${mountpoint}&"
"topic=${topic}&" "topic=${topic}&"
"action=${action}&" "action=${action}&"
"access=${access}&"
"qos=${qos}&" "qos=${qos}&"
"retain=${retain}" "retain=${retain}"
>> >>
@ -264,6 +267,7 @@ t_path(_Config) ->
"MOUNTPOINT/" "MOUNTPOINT/"
"t%2F1/" "t%2F1/"
"publish/" "publish/"
"2/"
"1/" "1/"
"false" "false"
>>, >>,
@ -281,6 +285,7 @@ t_path(_Config) ->
"${mountpoint}/" "${mountpoint}/"
"${topic}/" "${topic}/"
"${action}/" "${action}/"
"${access}/"
"${qos}/" "${qos}/"
"${retain}" "${retain}"
>> >>
@ -321,6 +326,7 @@ t_json_body(_Config) ->
<<"mountpoint">> := <<"MOUNTPOINT">>, <<"mountpoint">> := <<"MOUNTPOINT">>,
<<"topic">> := <<"t">>, <<"topic">> := <<"t">>,
<<"action">> := <<"publish">>, <<"action">> := <<"publish">>,
<<"access">> := <<"2">>,
<<"qos">> := <<"1">>, <<"qos">> := <<"1">>,
<<"retain">> := <<"false">> <<"retain">> := <<"false">>
}, },
@ -338,6 +344,7 @@ t_json_body(_Config) ->
<<"mountpoint">> => <<"${mountpoint}">>, <<"mountpoint">> => <<"${mountpoint}">>,
<<"topic">> => <<"${topic}">>, <<"topic">> => <<"${topic}">>,
<<"action">> => <<"${action}">>, <<"action">> => <<"${action}">>,
<<"access">> => <<"${access}">>,
<<"qos">> => <<"${qos}">>, <<"qos">> => <<"${qos}">>,
<<"retain">> => <<"${retain}">> <<"retain">> => <<"${retain}">>
} }
@ -405,7 +412,7 @@ t_placeholder_and_body(_Config) ->
cowboy_req:path(Req0) cowboy_req:path(Req0)
), ),
{ok, [{PostVars, true}], Req1} = cowboy_req:read_urlencoded_body(Req0), {ok, PostVars, Req1} = cowboy_req:read_urlencoded_body(Req0),
?assertMatch( ?assertMatch(
#{ #{
@ -416,10 +423,11 @@ t_placeholder_and_body(_Config) ->
<<"mountpoint">> := <<"MOUNTPOINT">>, <<"mountpoint">> := <<"MOUNTPOINT">>,
<<"topic">> := <<"t">>, <<"topic">> := <<"t">>,
<<"action">> := <<"publish">>, <<"action">> := <<"publish">>,
<<"access">> := <<"2">>,
<<"CN">> := ?PH_CERT_CN_NAME, <<"CN">> := ?PH_CERT_CN_NAME,
<<"CS">> := ?PH_CERT_SUBJECT <<"CS">> := ?PH_CERT_SUBJECT
}, },
emqx_utils_json:decode(PostVars, [return_maps]) maps:from_list(PostVars)
), ),
{ok, ?AUTHZ_HTTP_RESP(allow, Req1), State} {ok, ?AUTHZ_HTTP_RESP(allow, Req1), State}
end, end,
@ -433,6 +441,7 @@ t_placeholder_and_body(_Config) ->
<<"mountpoint">> => <<"${mountpoint}">>, <<"mountpoint">> => <<"${mountpoint}">>,
<<"topic">> => <<"${topic}">>, <<"topic">> => <<"${topic}">>,
<<"action">> => <<"${action}">>, <<"action">> => <<"${action}">>,
<<"access">> => <<"${access}">>,
<<"CN">> => ?PH_CERT_CN_NAME, <<"CN">> => ?PH_CERT_CN_NAME,
<<"CS">> => ?PH_CERT_SUBJECT <<"CS">> => ?PH_CERT_SUBJECT
}, },