Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: authz-http return body to reject pub/sub #8377

Merged
merged 2 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions apps/emqx_authz/src/emqx_authz.appup.src
@@ -1,7 +1,11 @@
%% -*- mode: erlang -*-
%% Unless you know what you are doing, DO NOT edit manually!!
{VSN,
[{"0.1.0",[{load_module,emqx_authz_utils,brutal_purge,soft_purge,[]}]},
{"0.1.1",[{load_module,emqx_authz_utils,brutal_purge,soft_purge,[]}]}],
[{"0.1.0",[{load_module,emqx_authz_utils,brutal_purge,soft_purge,[]}]},
{"0.1.1",[{load_module,emqx_authz_utils,brutal_purge,soft_purge,[]}]}]}.
[{<<"0\\.1\\.[0-1]">>,[
{load_module,emqx_authz_utils,brutal_purge,soft_purge,[]},
{load_module,emqx_authz_http,brutal_purge,soft_purge,[]}]}
],
[{<<"0\\.1\\.[0-1]">>,[
{load_module,emqx_authz_utils,brutal_purge,soft_purge,[]},
{load_module,emqx_authz_http,brutal_purge,soft_purge,[]}]}
]}.
26 changes: 24 additions & 2 deletions apps/emqx_authz/src/emqx_authz_http.erl
Expand Up @@ -84,8 +84,21 @@ authorize(
{matched, allow};
{ok, 204, _Headers} ->
{matched, allow};
{ok, 200, _Headers, _Body} ->
{matched, allow};
{ok, 200, Headers, Body} ->
ContentType = content_type(Headers),
case emqx_authz_utils:parse_http_resp_body(ContentType, Body) of
error ->
?SLOG(error, #{
msg => authz_http_response_incorrect,
content_type => proplists:get_value(
<<"content-type">>, Headers
),
body => Body
}),
nomatch;
Result ->
{matched, Result}
end;
{ok, _Status, _Headers} ->
nomatch;
{ok, _Status, _Headers, _Body} ->
Expand Down Expand Up @@ -205,6 +218,15 @@ serialize_body(<<"application/json">>, Body) ->
serialize_body(<<"application/x-www-form-urlencoded">>, Body) ->
query_string(Body).

content_type(Headers) when is_list(Headers) ->
content_type(maps:from_list(Headers));
content_type(#{<<"content-type">> := Type}) ->
Type;
content_type(#{<<"Content-Type">> := Type}) ->
Type;
content_type(Headers) when is_map(Headers) ->
<<"application/json">>.

client_vars(Client, PubSub, Topic) ->
Client#{
action => PubSub,
Expand Down
21 changes: 21 additions & 0 deletions apps/emqx_authz/src/emqx_authz_utils.erl
Expand Up @@ -34,6 +34,8 @@
render_sql_params/2
]).

-export([parse_http_resp_body/2]).

-define(DEFAULT_RESOURCE_OPTS, #{
auto_retry_interval => 6000,
start_after_created => false
Expand Down Expand Up @@ -130,6 +132,25 @@ render_sql_params(ParamList, Values) ->
#{return => rawlist, var_trans => fun handle_sql_var/2}
).

-spec parse_http_resp_body(binary(), binary()) -> allow | deny | ignore | error.
parse_http_resp_body(<<"application/x-www-form-urlencoded", _/binary>>, Body) ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks backward compatibility?
We should also check if the body is empty, then fallback to the old behavior?
5.0.0 and 4.x

try
result(maps:from_list(cow_qs:parse_qs(Body)))
catch
_:_ -> error
end;
parse_http_resp_body(<<"application/json", _/binary>>, Body) ->
try
result(emqx_json:decode(Body, [return_maps]))
catch
_:_ -> error
end.

result(#{<<"result">> := <<"allow">>}) -> allow;
result(#{<<"result">> := <<"deny">>}) -> deny;
result(#{<<"result">> := <<"ignore">>}) -> ignore;
result(_) -> error.

%%--------------------------------------------------------------------
%% Internal functions
%%--------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions apps/emqx_authz/test/emqx_authz_http_SUITE.erl
Expand Up @@ -85,8 +85,8 @@ t_response_handling(_Config) ->
fun(Req0, State) ->
Req = cowboy_req:reply(
200,
#{<<"content-type">> => <<"text/plain">>},
"Response body",
#{<<"content-type">> => <<"application/json">>},
"{\"result\": \"allow\"}",
Req0
),
{ok, Req, State}
Expand Down