Просмотр исходного кода

improve: module_acl_internal load/unload api (#3706)

turtleDeng 5 лет назад
Родитель
Сommit
edcbdcce87

+ 2 - 2
priv/emqx.schema

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

+ 4 - 4
src/emqx_mod_acl_internal.erl

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

+ 3 - 4
test/emqx_acl_cache_SUITE.erl

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

+ 2 - 2
test/mqtt_protocol_v5_SUITE.erl

@@ -181,8 +181,8 @@ t_batch_subscribe(_) ->
     application:set_env(emqx, enable_acl_cache, false),
     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"),
-    application:set_env(emqx, acl_file, TempAcl),
-    emqx_mod_acl_internal:reload([]),
+    timer:sleep(10),
+    emqx_mod_acl_internal:reload([{acl_file, TempAcl}]),
     {ok, _, [?RC_NOT_AUTHORIZED,
              ?RC_NOT_AUTHORIZED,
              ?RC_NOT_AUTHORIZED]} = emqtt:subscribe(Client, [{<<"t1">>, qos1},