fix: return 'not found' for subscriptions of unknown client
This commit is contained in:
parent
f3ced5d5eb
commit
0d2ce85776
|
@ -286,12 +286,12 @@ maybe_format(undefined, A) ->
|
||||||
maybe_format({M, F}, A) ->
|
maybe_format({M, F}, A) ->
|
||||||
M:F(A).
|
M:F(A).
|
||||||
|
|
||||||
kickout_client(ClientID) ->
|
kickout_client(ClientId) ->
|
||||||
case lookup_client({clientid, ClientID}, undefined) of
|
case lookup_client({clientid, ClientId}, undefined) of
|
||||||
[] ->
|
[] ->
|
||||||
{error, not_found};
|
{error, not_found};
|
||||||
_ ->
|
_ ->
|
||||||
Results = [kickout_client(Node, ClientID) || Node <- mria_mnesia:running_nodes()],
|
Results = [kickout_client(Node, ClientId) || Node <- mria_mnesia:running_nodes()],
|
||||||
check_results(Results)
|
check_results(Results)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
@ -302,17 +302,22 @@ list_authz_cache(ClientId) ->
|
||||||
call_client(ClientId, list_authz_cache).
|
call_client(ClientId, list_authz_cache).
|
||||||
|
|
||||||
list_client_subscriptions(ClientId) ->
|
list_client_subscriptions(ClientId) ->
|
||||||
Results = [client_subscriptions(Node, ClientId) || Node <- mria_mnesia:running_nodes()],
|
case lookup_client({clientid, ClientId}, undefined) of
|
||||||
Filter =
|
[] ->
|
||||||
fun
|
{error, not_found};
|
||||||
({error, _}) ->
|
_ ->
|
||||||
false;
|
Results = [client_subscriptions(Node, ClientId) || Node <- mria_mnesia:running_nodes()],
|
||||||
({_Node, List}) ->
|
Filter =
|
||||||
erlang:is_list(List) andalso 0 < erlang:length(List)
|
fun
|
||||||
end,
|
({error, _}) ->
|
||||||
case lists:filter(Filter, Results) of
|
false;
|
||||||
[] -> [];
|
({_Node, List}) ->
|
||||||
[Result | _] -> Result
|
erlang:is_list(List) andalso 0 < erlang:length(List)
|
||||||
|
end,
|
||||||
|
case lists:filter(Filter, Results) of
|
||||||
|
[] -> [];
|
||||||
|
[Result | _] -> Result
|
||||||
|
end
|
||||||
end.
|
end.
|
||||||
|
|
||||||
client_subscriptions(Node, ClientId) ->
|
client_subscriptions(Node, ClientId) ->
|
||||||
|
|
|
@ -274,11 +274,10 @@ schema("/clients/:clientid/subscriptions") ->
|
||||||
responses => #{
|
responses => #{
|
||||||
200 => hoconsc:mk(
|
200 => hoconsc:mk(
|
||||||
hoconsc:array(hoconsc:ref(emqx_mgmt_api_subscriptions, subscription)), #{}
|
hoconsc:array(hoconsc:ref(emqx_mgmt_api_subscriptions, subscription)), #{}
|
||||||
|
),
|
||||||
|
404 => emqx_dashboard_swagger:error_codes(
|
||||||
|
['CLIENTID_NOT_FOUND'], <<"Client ID not found">>
|
||||||
)
|
)
|
||||||
%% returns [] if client not existed in cluster
|
|
||||||
%404 => emqx_dashboard_swagger:error_codes(
|
|
||||||
% ['CLIENTID_NOT_FOUND'], <<"Client ID not found">>
|
|
||||||
%)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -599,6 +598,8 @@ unsubscribe_batch(post, #{bindings := #{clientid := ClientID}, body := TopicInfo
|
||||||
|
|
||||||
subscriptions(get, #{bindings := #{clientid := ClientID}}) ->
|
subscriptions(get, #{bindings := #{clientid := ClientID}}) ->
|
||||||
case emqx_mgmt:list_client_subscriptions(ClientID) of
|
case emqx_mgmt:list_client_subscriptions(ClientID) of
|
||||||
|
{error, not_found} ->
|
||||||
|
{404, ?CLIENTID_NOT_FOUND};
|
||||||
[] ->
|
[] ->
|
||||||
{200, []};
|
{200, []};
|
||||||
{Node, Subs} ->
|
{Node, Subs} ->
|
||||||
|
|
|
@ -145,6 +145,27 @@ t_kickout_client(Config) ->
|
||||||
end,
|
end,
|
||||||
?assertEqual({error, not_found}, emqx_mgmt:kickout_client(<<"notfound">>)).
|
?assertEqual({error, not_found}, emqx_mgmt:kickout_client(<<"notfound">>)).
|
||||||
|
|
||||||
|
t_list_authz_cache(init, Config) ->
|
||||||
|
setup_clients(Config);
|
||||||
|
t_list_authz_cache('end', Config) ->
|
||||||
|
disconnect_clients(Config).
|
||||||
|
|
||||||
|
t_list_authz_cache(_) ->
|
||||||
|
?assertNotMatch({error, _}, emqx_mgmt:list_authz_cache(<<"client1">>)),
|
||||||
|
?assertMatch({error, not_found}, emqx_mgmt:list_authz_cache(<<"notfound">>)).
|
||||||
|
|
||||||
|
t_list_client_subscriptions(init, Config) ->
|
||||||
|
setup_clients(Config);
|
||||||
|
t_list_client_subscriptions('end', Config) ->
|
||||||
|
disconnect_clients(Config).
|
||||||
|
|
||||||
|
t_list_client_subscriptions(Config) ->
|
||||||
|
[Client | _] = ?config(clients, Config),
|
||||||
|
?assertEqual([], emqx_mgmt:list_client_subscriptions(<<"client1">>)),
|
||||||
|
emqtt:subscribe(Client, <<"t/#">>),
|
||||||
|
?assertMatch({_, [{<<"t/#">>, _Opts}]}, emqx_mgmt:list_client_subscriptions(<<"client1">>)),
|
||||||
|
?assertEqual({error, not_found}, emqx_mgmt:list_client_subscriptions(<<"notfound">>)).
|
||||||
|
|
||||||
%%% helpers
|
%%% helpers
|
||||||
ident(Arg) ->
|
ident(Arg) ->
|
||||||
Arg.
|
Arg.
|
||||||
|
|
|
@ -279,7 +279,7 @@ t_client_id_not_found(_Config) ->
|
||||||
%% Client kickout
|
%% Client kickout
|
||||||
?assertMatch({error, {Http, _, Body}}, ReqFun(delete, PathFun([]))),
|
?assertMatch({error, {Http, _, Body}}, ReqFun(delete, PathFun([]))),
|
||||||
%% Client Subscription list
|
%% Client Subscription list
|
||||||
?assertMatch({ok, {{"HTTP/1.1", 200, "OK"}, _, "[]"}}, ReqFun(get, PathFun(["subscriptions"]))),
|
?assertMatch({error, {Http, _, Body}}, ReqFun(get, PathFun(["subscriptions"]))),
|
||||||
%% AuthZ Cache lookup
|
%% AuthZ Cache lookup
|
||||||
?assertMatch({error, {Http, _, Body}}, ReqFun(get, PathFun(["authorization", "cache"]))),
|
?assertMatch({error, {Http, _, Body}}, ReqFun(get, PathFun(["authorization", "cache"]))),
|
||||||
%% AuthZ Cache clean
|
%% AuthZ Cache clean
|
||||||
|
|
Loading…
Reference in New Issue