|
@@ -46,18 +46,26 @@ all_rules() ->
|
|
|
-spec(init([File :: string()]) -> {ok, #{}}).
|
|
-spec(init([File :: string()]) -> {ok, #{}}).
|
|
|
init([File]) ->
|
|
init([File]) ->
|
|
|
_ = emqx_tables:new(?ACL_RULE_TAB, [set, public, {read_concurrency, true}]),
|
|
_ = emqx_tables:new(?ACL_RULE_TAB, [set, public, {read_concurrency, true}]),
|
|
|
- true = load_rules_from_file(File),
|
|
|
|
|
|
|
+ ok = load_rules_from_file(File),
|
|
|
{ok, #{acl_file => File}}.
|
|
{ok, #{acl_file => File}}.
|
|
|
|
|
|
|
|
load_rules_from_file(AclFile) ->
|
|
load_rules_from_file(AclFile) ->
|
|
|
- {ok, Terms} = file:consult(AclFile),
|
|
|
|
|
- Rules = [emqx_access_rule:compile(Term) || Term <- Terms],
|
|
|
|
|
- lists:foreach(fun(PubSub) ->
|
|
|
|
|
- ets:insert(?ACL_RULE_TAB, {PubSub,
|
|
|
|
|
- lists:filter(fun(Rule) -> filter(PubSub, Rule) end, Rules)})
|
|
|
|
|
- end, [publish, subscribe]),
|
|
|
|
|
- ets:insert(?ACL_RULE_TAB, {all_rules, Terms}).
|
|
|
|
|
|
|
+ case file:consult(AclFile) of
|
|
|
|
|
+ {ok, Terms} ->
|
|
|
|
|
+ Rules = [emqx_access_rule:compile(Term) || Term <- Terms],
|
|
|
|
|
+ lists:foreach(fun(PubSub) ->
|
|
|
|
|
+ ets:insert(?ACL_RULE_TAB, {PubSub,
|
|
|
|
|
+ lists:filter(fun(Rule) -> filter(PubSub, Rule) end, Rules)})
|
|
|
|
|
+ end, [publish, subscribe]),
|
|
|
|
|
+ ets:insert(?ACL_RULE_TAB, {all_rules, Terms}),
|
|
|
|
|
+ ok;
|
|
|
|
|
+ {error, Reason} ->
|
|
|
|
|
+ emqx_logger:error("[ACL_INTERNAL] Failed to read ~s: ~p", [AclFile, Reason]),
|
|
|
|
|
+ {error, Reason}
|
|
|
|
|
+ end.
|
|
|
|
|
|
|
|
|
|
+filter(_PubSub, {error, _}) ->
|
|
|
|
|
+ false;
|
|
|
filter(_PubSub, {allow, all}) ->
|
|
filter(_PubSub, {allow, all}) ->
|
|
|
true;
|
|
true;
|
|
|
filter(_PubSub, {deny, all}) ->
|
|
filter(_PubSub, {deny, all}) ->
|
|
@@ -100,9 +108,11 @@ match(Credentials, Topic, [Rule|Rules]) ->
|
|
|
-spec(reload_acl(state()) -> ok | {error, term()}).
|
|
-spec(reload_acl(state()) -> ok | {error, term()}).
|
|
|
reload_acl(#{acl_file := AclFile}) ->
|
|
reload_acl(#{acl_file := AclFile}) ->
|
|
|
case catch load_rules_from_file(AclFile) of
|
|
case catch load_rules_from_file(AclFile) of
|
|
|
- true ->
|
|
|
|
|
|
|
+ ok ->
|
|
|
emqx_logger:info("Reload acl_file ~s successfully", [AclFile]),
|
|
emqx_logger:info("Reload acl_file ~s successfully", [AclFile]),
|
|
|
ok;
|
|
ok;
|
|
|
|
|
+ {error, Error} ->
|
|
|
|
|
+ {error, Error};
|
|
|
{'EXIT', Error} ->
|
|
{'EXIT', Error} ->
|
|
|
{error, Error}
|
|
{error, Error}
|
|
|
end.
|
|
end.
|