From 14b59779961e14372ddfa1d619710017a8114704 Mon Sep 17 00:00:00 2001 From: firest Date: Thu, 15 Sep 2022 15:02:16 +0800 Subject: [PATCH] feat(authz): Support `cert_common_name` and `cert_subject` in Authz --- apps/emqx_authz/src/emqx_authz_http.erl | 4 +- apps/emqx_authz/src/emqx_authz_mongodb.erl | 4 +- .../emqx_authz/test/emqx_authz_http_SUITE.erl | 15 +++++-- .../test/emqx_authz_mongodb_SUITE.erl | 42 ++++++++++++++++++- 4 files changed, 58 insertions(+), 7 deletions(-) diff --git a/apps/emqx_authz/src/emqx_authz_http.erl b/apps/emqx_authz/src/emqx_authz_http.erl index 69f21932a..246d9c294 100644 --- a/apps/emqx_authz/src/emqx_authz_http.erl +++ b/apps/emqx_authz/src/emqx_authz_http.erl @@ -45,7 +45,9 @@ ?PH_PROTONAME, ?PH_MOUNTPOINT, ?PH_TOPIC, - ?PH_ACTION + ?PH_ACTION, + ?PH_CERT_SUBJECT, + ?PH_CERT_CN_NAME ]). description() -> diff --git a/apps/emqx_authz/src/emqx_authz_mongodb.erl b/apps/emqx_authz/src/emqx_authz_mongodb.erl index ac450e4cc..931d83c07 100644 --- a/apps/emqx_authz/src/emqx_authz_mongodb.erl +++ b/apps/emqx_authz/src/emqx_authz_mongodb.erl @@ -40,7 +40,9 @@ -define(PLACEHOLDERS, [ ?PH_USERNAME, ?PH_CLIENTID, - ?PH_PEERHOST + ?PH_PEERHOST, + ?PH_CERT_CN_NAME, + ?PH_CERT_SUBJECT ]). description() -> diff --git a/apps/emqx_authz/test/emqx_authz_http_SUITE.erl b/apps/emqx_authz/test/emqx_authz_http_SUITE.erl index 4b5ad7cbf..672fd6ddd 100644 --- a/apps/emqx_authz/test/emqx_authz_http_SUITE.erl +++ b/apps/emqx_authz/test/emqx_authz_http_SUITE.erl @@ -22,6 +22,7 @@ -include("emqx_authz.hrl"). -include_lib("eunit/include/eunit.hrl"). -include_lib("common_test/include/ct.hrl"). +-include_lib("emqx/include/emqx_placeholder.hrl"). -define(HTTP_PORT, 33333). -define(HTTP_PATH, "/authz/[...]"). @@ -303,7 +304,7 @@ t_json_body(_Config) -> emqx_access_control:authorize(ClientInfo, publish, <<"t">>) ). -t_form_body(_Config) -> +t_placeholder_and_body(_Config) -> ok = setup_handler_and_config( fun(Req0, State) -> ?assertEqual( @@ -321,7 +322,9 @@ t_form_body(_Config) -> <<"proto_name">> := <<"MQTT">>, <<"mountpoint">> := <<"MOUNTPOINT">>, <<"topic">> := <<"t">>, - <<"action">> := <<"publish">> + <<"action">> := <<"publish">>, + <<"CN">> := ?PH_CERT_CN_NAME, + <<"CS">> := ?PH_CERT_SUBJECT }, jiffy:decode(PostVars, [return_maps]) ), @@ -336,7 +339,9 @@ t_form_body(_Config) -> <<"proto_name">> => <<"${proto_name}">>, <<"mountpoint">> => <<"${mountpoint}">>, <<"topic">> => <<"${topic}">>, - <<"action">> => <<"${action}">> + <<"action">> => <<"${action}">>, + <<"CN">> => ?PH_CERT_CN_NAME, + <<"CS">> => ?PH_CERT_SUBJECT }, <<"headers">> => #{<<"content-type">> => <<"application/x-www-form-urlencoded">>} } @@ -349,7 +354,9 @@ t_form_body(_Config) -> protocol => <<"MQTT">>, mountpoint => <<"MOUNTPOINT">>, zone => default, - listener => {tcp, default} + listener => {tcp, default}, + cn => ?PH_CERT_CN_NAME, + dn => ?PH_CERT_SUBJECT }, ?assertEqual( diff --git a/apps/emqx_authz/test/emqx_authz_mongodb_SUITE.erl b/apps/emqx_authz/test/emqx_authz_mongodb_SUITE.erl index 5e5a6ca1e..f080f7e72 100644 --- a/apps/emqx_authz/test/emqx_authz_mongodb_SUITE.erl +++ b/apps/emqx_authz/test/emqx_authz_mongodb_SUITE.erl @@ -18,8 +18,8 @@ -compile(nowarn_export_all). -compile(export_all). --include("emqx_connector.hrl"). -include("emqx_authz.hrl"). +-include_lib("emqx_connector/include/emqx_connector.hrl"). -include_lib("eunit/include/eunit.hrl"). -include_lib("common_test/include/ct.hrl"). -include_lib("emqx/include/emqx_placeholder.hrl"). @@ -188,6 +188,46 @@ t_lookups(_Config) -> #{<<"filter">> => #{<<"peerhost">> => <<"${peerhost}">>}} ), + ok = emqx_authz_test_lib:test_samples( + ClientInfo, + [ + {allow, subscribe, <<"a">>}, + {deny, subscribe, <<"b">>} + ] + ), + + ByCN = #{ + <<"CN">> => <<"cn">>, + <<"topics">> => [<<"a">>], + <<"action">> => <<"all">>, + <<"permission">> => <<"allow">> + }, + + ok = setup_samples([ByCN]), + ok = setup_config( + #{<<"filter">> => #{<<"CN">> => ?PH_CERT_CN_NAME}} + ), + + ok = emqx_authz_test_lib:test_samples( + ClientInfo, + [ + {allow, subscribe, <<"a">>}, + {deny, subscribe, <<"b">>} + ] + ), + + ByDN = #{ + <<"DN">> => <<"dn">>, + <<"topics">> => [<<"a">>], + <<"action">> => <<"all">>, + <<"permission">> => <<"allow">> + }, + + ok = setup_samples([ByDN]), + ok = setup_config( + #{<<"filter">> => #{<<"DN">> => ?PH_CERT_SUBJECT}} + ), + ok = emqx_authz_test_lib:test_samples( ClientInfo, [