|
|
@@ -26,7 +26,7 @@
|
|
|
, compile/1
|
|
|
, lookup/0
|
|
|
, update/1
|
|
|
- , check_authz/5
|
|
|
+ , authorize/5
|
|
|
, match/4
|
|
|
]).
|
|
|
|
|
|
@@ -41,7 +41,7 @@ init() ->
|
|
|
#{<<"authz">> := #{<<"rules">> := Rules}} = hocon_schema:check_plain(emqx_authz_schema, RawConf),
|
|
|
ok = application:set_env(?APP, rules, Rules),
|
|
|
NRules = [compile(Rule) || Rule <- Rules],
|
|
|
- ok = emqx_hooks:add('client.check_authz', {?MODULE, check_authz, [NRules]}, -1).
|
|
|
+ ok = emqx_hooks:add('client.authorize', {?MODULE, authorize, [NRules]}, -1).
|
|
|
|
|
|
lookup() ->
|
|
|
application:get_env(?APP, rules, []).
|
|
|
@@ -50,8 +50,8 @@ update(Rules) ->
|
|
|
ok = application:set_env(?APP, rules, Rules),
|
|
|
NRules = [compile(Rule) || Rule <- Rules],
|
|
|
Action = find_action_in_hooks(),
|
|
|
- ok = emqx_hooks:del('client.check_authz', Action),
|
|
|
- ok = emqx_hooks:add('client.check_authz', {?MODULE, check_authz, [NRules]}, -1),
|
|
|
+ ok = emqx_hooks:del('client.authorize', Action),
|
|
|
+ ok = emqx_hooks:add('client.authorize', {?MODULE, authorize, [NRules]}, -1),
|
|
|
ok = emqx_acl_cache:empty_acl_cache().
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
@@ -59,8 +59,8 @@ update(Rules) ->
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
|
find_action_in_hooks() ->
|
|
|
- Callbacks = emqx_hooks:lookup('client.check_authz'),
|
|
|
- [Action] = [Action || {callback,{?MODULE, check_authz, _} = Action, _, _} <- Callbacks ],
|
|
|
+ Callbacks = emqx_hooks:lookup('client.authorize'),
|
|
|
+ [Action] = [Action || {callback,{?MODULE, authorize, _} = Action, _, _} <- Callbacks ],
|
|
|
Action.
|
|
|
|
|
|
create_resource(#{<<"type">> := DB,
|
|
|
@@ -149,12 +149,12 @@ b2l(B) when is_binary(B) -> binary_to_list(B).
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
|
%% @doc Check ACL
|
|
|
--spec(check_authz(emqx_types:clientinfo(), emqx_types:all(), emqx_topic:topic(), emqx_permission_rule:acl_result(), rules())
|
|
|
+-spec(authorize(emqx_types:clientinfo(), emqx_types:all(), emqx_topic:topic(), emqx_permission_rule:acl_result(), rules())
|
|
|
-> {stop, allow} | {ok, deny}).
|
|
|
-check_authz(#{username := Username,
|
|
|
+authorize(#{username := Username,
|
|
|
peerhost := IpAddress
|
|
|
} = Client, PubSub, Topic, _DefaultResult, Rules) ->
|
|
|
- case do_check_authz(Client, PubSub, Topic, Rules) of
|
|
|
+ case do_authorize(Client, PubSub, Topic, Rules) of
|
|
|
{matched, allow} ->
|
|
|
?LOG(info, "Client succeeded authorization: Username: ~p, IP: ~p, Topic: ~p, Permission: allow", [Username, IpAddress, Topic]),
|
|
|
emqx_metrics:inc(?ACL_METRICS(allow)),
|
|
|
@@ -168,25 +168,25 @@ check_authz(#{username := Username,
|
|
|
{stop, deny}
|
|
|
end.
|
|
|
|
|
|
-do_check_authz(Client, PubSub, Topic,
|
|
|
+do_authorize(Client, PubSub, Topic,
|
|
|
[Connector = #{<<"principal">> := Principal,
|
|
|
<<"type">> := DB} | Tail] ) ->
|
|
|
case match_principal(Client, Principal) of
|
|
|
true ->
|
|
|
Mod = list_to_existing_atom(io_lib:format("~s_~s",[emqx_authz, DB])),
|
|
|
- case Mod:check_authz(Client, PubSub, Topic, Connector) of
|
|
|
- nomatch -> do_check_authz(Client, PubSub, Topic, Tail);
|
|
|
+ case Mod:authorize(Client, PubSub, Topic, Connector) of
|
|
|
+ nomatch -> do_authorize(Client, PubSub, Topic, Tail);
|
|
|
Matched -> Matched
|
|
|
end;
|
|
|
- false -> do_check_authz(Client, PubSub, Topic, Tail)
|
|
|
+ false -> do_authorize(Client, PubSub, Topic, Tail)
|
|
|
end;
|
|
|
-do_check_authz(Client, PubSub, Topic,
|
|
|
+do_authorize(Client, PubSub, Topic,
|
|
|
[#{<<"permission">> := Permission} = Rule | Tail]) ->
|
|
|
case match(Client, PubSub, Topic, Rule) of
|
|
|
true -> {matched, Permission};
|
|
|
- false -> do_check_authz(Client, PubSub, Topic, Tail)
|
|
|
+ false -> do_authorize(Client, PubSub, Topic, Tail)
|
|
|
end;
|
|
|
-do_check_authz(_Client, _PubSub, _Topic, []) -> nomatch.
|
|
|
+do_authorize(_Client, _PubSub, _Topic, []) -> nomatch.
|
|
|
|
|
|
match(Client, PubSub, Topic,
|
|
|
#{<<"principal">> := Principal,
|