improve: module_acl_internal load/unload api (#3706)

This commit is contained in:
turtleDeng 2020-09-04 15:24:45 +08:00 committed by tigercl
parent 90b33b044d
commit edcbdcce87
4 changed files with 11 additions and 12 deletions

View File

@ -1969,7 +1969,7 @@ end}.
{datatype, string} {datatype, string}
]}. ]}.
{translation, "emqx.modules", fun(Conf) -> {translation, "emqx.modules", fun(Conf, _, Conf1) ->
Subscriptions = fun() -> Subscriptions = fun() ->
List = cuttlefish_variable:filter_by_prefix("module.subscription", Conf), List = cuttlefish_variable:filter_by_prefix("module.subscription", Conf),
TopicList = [{N, Topic}|| {[_,"subscription",N,"topic"], Topic} <- List], TopicList = [{N, Topic}|| {[_,"subscription",N,"topic"], Topic} <- List],
@ -1998,7 +1998,7 @@ end}.
[{emqx_mod_rewrite, Rewrites()}], [{emqx_mod_rewrite, Rewrites()}],
[{emqx_mod_topic_metrics, []}], [{emqx_mod_topic_metrics, []}],
[{emqx_mod_delayed, []}], [{emqx_mod_delayed, []}],
[{emqx_mod_acl_internal, []}] [{emqx_mod_acl_internal, [{acl_file, cuttlefish:conf_get("acl_file", Conf1)}]}]
]) ])
end}. end}.

View File

@ -40,19 +40,19 @@
%% API %% API
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
load(_Env) -> load(Env) ->
Rules = rules_from_file(emqx:get_env(acl_file)), Rules = rules_from_file(proplists:get_value(acl_file, Env)),
emqx_hooks:add('client.check_acl', {?MODULE, check_acl, [Rules]}, -1). emqx_hooks:add('client.check_acl', {?MODULE, check_acl, [Rules]}, -1).
unload(_Env) -> unload(_Env) ->
emqx_hooks:del('client.check_acl', {?MODULE, check_acl}). emqx_hooks:del('client.check_acl', {?MODULE, check_acl}).
reload(_Env) -> reload(Env) ->
emqx_acl_cache:is_enabled() andalso ( emqx_acl_cache:is_enabled() andalso (
lists:foreach( lists:foreach(
fun(Pid) -> erlang:send(Pid, clean_acl_cache) end, fun(Pid) -> erlang:send(Pid, clean_acl_cache) end,
emqx_cm:all_channels())), emqx_cm:all_channels())),
unload([]), load([]). unload(Env), load(Env).
description() -> description() ->
"EMQ X Internal ACL Module". "EMQ X Internal ACL Module".

View File

@ -83,9 +83,8 @@ t_reload_aclfile_and_cleanall(Config) ->
Path = filename:join([testdir(proplists:get_value(data_dir, Config)), "acl2.conf"]), Path = filename:join([testdir(proplists:get_value(data_dir, Config)), "acl2.conf"]),
ok = file:write_file(Path, <<"{deny, all}.">>), ok = file:write_file(Path, <<"{deny, all}.">>),
OldPath = emqx:get_env(acl_file), OldPath = emqx:get_env(acl_file),
application:set_env(emqx, acl_file, Path), % application:set_env(emqx, acl_file, Path),
emqx_mod_acl_internal:reload([{acl_file, Path}]),
emqx_mod_acl_internal:reload([]),
?assert(length(gen_server:call(ClientPid, list_acl_cache)) == 0), ?assert(length(gen_server:call(ClientPid, list_acl_cache)) == 0),
{ok, PktId2} = emqtt:publish(Client, <<"t1">>, <<"{\"x\":1}">>, qos1), {ok, PktId2} = emqtt:publish(Client, <<"t1">>, <<"{\"x\":1}">>, qos1),
@ -99,7 +98,7 @@ t_reload_aclfile_and_cleanall(Config) ->
end, end,
application:set_env(emqx, acl_file, OldPath), application:set_env(emqx, acl_file, OldPath),
file:delete(Path), file:delete(Path),
emqx_mod_acl_internal:reload([]), emqx_mod_acl_internal:reload([{acl_file, OldPath}]),
emqtt:stop(Client). emqtt:stop(Client).
%% @private %% @private

View File

@ -181,8 +181,8 @@ t_batch_subscribe(_) ->
application:set_env(emqx, enable_acl_cache, false), application:set_env(emqx, enable_acl_cache, false),
TempAcl = emqx_ct_helpers:deps_path(emqx, "test/emqx_access_SUITE_data/acl_temp.conf"), TempAcl = emqx_ct_helpers:deps_path(emqx, "test/emqx_access_SUITE_data/acl_temp.conf"),
file:write_file(TempAcl, "{deny, {client, \"batch_test\"}, subscribe, [\"t1\", \"t2\", \"t3\"]}.\n"), file:write_file(TempAcl, "{deny, {client, \"batch_test\"}, subscribe, [\"t1\", \"t2\", \"t3\"]}.\n"),
application:set_env(emqx, acl_file, TempAcl), timer:sleep(10),
emqx_mod_acl_internal:reload([]), emqx_mod_acl_internal:reload([{acl_file, TempAcl}]),
{ok, _, [?RC_NOT_AUTHORIZED, {ok, _, [?RC_NOT_AUTHORIZED,
?RC_NOT_AUTHORIZED, ?RC_NOT_AUTHORIZED,
?RC_NOT_AUTHORIZED]} = emqtt:subscribe(Client, [{<<"t1">>, qos1}, ?RC_NOT_AUTHORIZED]} = emqtt:subscribe(Client, [{<<"t1">>, qos1},