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

Merge pull request #11531 from zhongwencool/authz-clean-cache-cli

fix: authz clean-cache clientid always return not_found
zhongwencool 2 лет назад
Родитель
Сommit
3b3024c08f

+ 1 - 1
apps/emqx_ctl/src/emqx_ctl.app.src

@@ -1,6 +1,6 @@
 {application, emqx_ctl, [
     {description, "Backend for emqx_ctl script"},
-    {vsn, "0.1.2"},
+    {vsn, "0.1.3"},
     {registered, []},
     {mod, {emqx_ctl_app, []}},
     {applications, [

+ 1 - 2
apps/emqx_ctl/src/emqx_ctl.erl

@@ -119,8 +119,7 @@ run_command(Cmd, Args) when is_atom(Cmd) ->
     case lookup_command(Cmd) of
         [{Mod, Fun}] ->
             try
-                _ = apply(Mod, Fun, [Args]),
-                ok
+                apply(Mod, Fun, [Args])
             catch
                 _:Reason:Stacktrace ->
                     ?LOG_ERROR(#{

+ 5 - 3
apps/emqx_management/src/emqx_mgmt_cli.erl

@@ -707,7 +707,7 @@ authz(["cache-clean", "all"]) ->
     with_log(fun emqx_mgmt:clean_authz_cache_all/0, Msg);
 authz(["cache-clean", ClientId]) ->
     Msg = io_lib:format("Drain ~ts authz cache", [ClientId]),
-    with_log(fun() -> emqx_mgmt:clean_authz_cache(ClientId) end, Msg);
+    with_log(fun() -> emqx_mgmt:clean_authz_cache(iolist_to_binary(ClientId)) end, Msg);
 authz(_) ->
     emqx_ctl:usage(
         [
@@ -921,12 +921,14 @@ for_node(Fun, Node) ->
     end.
 
 with_log(Fun, Msg) ->
-    case Fun() of
+    Res = Fun(),
+    case Res of
         ok ->
             emqx_ctl:print("~s OK~n", [Msg]);
         {error, Reason} ->
             emqx_ctl:print("~s FAILED~n~p~n", [Msg, Reason])
-    end.
+    end,
+    Res.
 
 cluster_info() ->
     RunningNodes = safe_call_mria(running_nodes, [], []),

+ 19 - 2
apps/emqx_management/test/emqx_mgmt_cli_SUITE.erl

@@ -25,6 +25,7 @@ all() ->
 
 init_per_suite(Config) ->
     emqx_mgmt_api_test_util:init_suite([emqx_conf, emqx_management]),
+    ok = emqx_mgmt_cli:load(),
     Config.
 
 end_per_suite(_) ->
@@ -183,9 +184,25 @@ t_listeners(_Config) ->
 
 t_authz(_Config) ->
     %% authz cache-clean all         # Clears authorization cache on all nodes
-    emqx_ctl:run_command(["authz", "cache-clean", "all"]),
-    %% authz cache-clean node <Node> # Clears authorization cache on given node
+    ?assertMatch(ok, emqx_ctl:run_command(["authz", "cache-clean", "all"])),
+    ClientId = "authz_clean_test",
+    ClientIdBin = list_to_binary(ClientId),
     %% authz cache-clean <ClientId>  # Clears authorization cache for given client
+    ?assertMatch({error, not_found}, emqx_ctl:run_command(["authz", "cache-clean", ClientId])),
+    {ok, C} = emqtt:start_link([{clean_start, true}, {clientid, ClientId}]),
+    {ok, _} = emqtt:connect(C),
+    {ok, _, _} = emqtt:subscribe(C, <<"topic/1">>, 1),
+    [Pid] = emqx_cm:lookup_channels(ClientIdBin),
+    ?assertMatch([_], gen_server:call(Pid, list_authz_cache)),
+
+    ?assertMatch(ok, emqx_ctl:run_command(["authz", "cache-clean", ClientId])),
+    ?assertMatch([], gen_server:call(Pid, list_authz_cache)),
+    %% authz cache-clean node <Node> # Clears authorization cache on given node
+    {ok, _, _} = emqtt:subscribe(C, <<"topic/2">>, 1),
+    ?assertMatch([_], gen_server:call(Pid, list_authz_cache)),
+    ?assertMatch(ok, emqx_ctl:run_command(["authz", "cache-clean", "node", atom_to_list(node())])),
+    ?assertMatch([], gen_server:call(Pid, list_authz_cache)),
+    ok = emqtt:disconnect(C),
     ok.
 
 t_olp(_Config) ->

+ 1 - 0
changes/ce/fix-11531.en.md

@@ -0,0 +1 @@
+Fixed issue where authorization cache cleaning cli was not working properly for specific client ID.