From 8d92d58b6cb035b3319d10ad10698bd787c048f9 Mon Sep 17 00:00:00 2001 From: JianBo He Date: Tue, 26 Mar 2019 10:25:51 +0800 Subject: [PATCH 1/2] Fix allow_anonymous behavoir error (#2355) EMQ should allow the anonymous connection if the allow_anonymous option is true, although it has one or more auth plugins started. --- src/emqx_access_control.erl | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/src/emqx_access_control.erl b/src/emqx_access_control.erl index 4fda19b08..501d5f0db 100644 --- a/src/emqx_access_control.erl +++ b/src/emqx_access_control.erl @@ -29,12 +29,14 @@ -spec(authenticate(emqx_types:credentials()) -> {ok, emqx_types:credentials()} | {error, term()}). authenticate(Credentials) -> - case emqx_hooks:run_fold('client.authenticate', [], Credentials#{auth_result => init_auth_result(Credentials)}) of - #{auth_result := success} = NewCredentials -> - {ok, NewCredentials}; - NewCredentials -> - {error, maps:get(auth_result, NewCredentials, unknown_error)} - end. + detect_anonymous_permission(Credentials, fun() -> + case emqx_hooks:run_fold('client.authenticate', [], init_auth_result(Credentials)) of + #{auth_result := success} = NewCredentials -> + {ok, NewCredentials}; + NewCredentials -> + {error, maps:get(auth_result, NewCredentials, unknown_error)} + end + end). %% @doc Check ACL -spec(check_acl(emqx_types:credentials(), emqx_types:pubsub(), emqx_types:topic()) -> allow | deny). @@ -67,7 +69,22 @@ reload_acl() -> emqx_mod_acl_internal:reload_acl(). init_auth_result(Credentials) -> - case emqx_zone:get_env(maps:get(zone, Credentials, undefined), allow_anonymous, false) of - true -> success; - false -> not_authorized + case anonymous_permission(Credentials) of + true -> Credentials#{auth_result => success}; + false -> Credentials#{auth_result => not_authorized} end. + +detect_anonymous_permission(#{username := undefined, + password := undefined} = Credentials, Fun) -> + case anonymous_permission(Credentials) of + true -> {ok, Credentials}; + false -> Fun() + end; + +detect_anonymous_permission(_Credentials, Fun) -> + Fun(). + +anonymous_permission(Credentials) -> + emqx_zone:get_env(maps:get(zone, Credentials, undefined), + allow_anonymous, false). + From f0fa9a92525fa095038e0a868874c61ac91acc9d Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Tue, 26 Mar 2019 10:35:10 +0800 Subject: [PATCH 2/2] Add a function converting message to binary-key map (#2360) --- src/emqx_message.erl | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/emqx_message.erl b/src/emqx_message.erl index e0f876719..f7cab7f6c 100644 --- a/src/emqx_message.erl +++ b/src/emqx_message.erl @@ -44,6 +44,8 @@ -export([ to_map/1 , to_list/1 + , to_bin_key_map/1 + , to_bin_key_list/1 ]). -export([format/1]). @@ -157,11 +159,24 @@ update_expiry(Msg) -> Msg. to_map(Msg) -> maps:from_list(to_list(Msg)). +%% @doc Message to map +-spec(to_bin_key_map(emqx_types:message()) -> #{binary() => any()}). +to_bin_key_map(Msg) -> + maps:from_list(to_bin_key_list(Msg)). + %% @doc Message to tuple list -spec(to_list(emqx_types:message()) -> map()). to_list(Msg) -> lists:zip(record_info(fields, message), tl(tuple_to_list(Msg))). +%% @doc Message to tuple list +-spec(to_bin_key_list(emqx_types:message()) -> map()). +to_bin_key_list(Msg) -> + lists:zipwith( + fun(Key, Val) -> + {bin(Key), Val} + end, record_info(fields, message), tl(tuple_to_list(Msg))). + %% MilliSeconds elapsed(Since) -> max(0, timer:now_diff(os:timestamp(), Since) div 1000). @@ -177,3 +192,6 @@ format(flags, Flags) -> format(headers, Headers) -> io_lib:format("~p", [Headers]). +bin(Bin) when is_binary(Bin) -> Bin; +bin(Atom) when is_atom(Atom) -> list_to_binary(atom_to_list(Atom)); +bin(Str) when is_list(Str) -> list_to_binary(Str). \ No newline at end of file