Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(banned): add a new API used to clear all banned data #11436

Merged
merged 2 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion apps/emqx/src/emqx_banned.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
delete/1,
info/1,
format/1,
parse/1
parse/1,
clear/0
]).

%% gen_server callbacks
Expand Down Expand Up @@ -226,6 +227,10 @@ delete(Who) ->
info(InfoKey) ->
mnesia:table_info(?BANNED_TAB, InfoKey).

clear() ->
_ = mria:clear_table(?BANNED_TAB),
ok.

%%--------------------------------------------------------------------
%% gen_server callbacks
%%--------------------------------------------------------------------
Expand Down
12 changes: 11 additions & 1 deletion apps/emqx_management/src/emqx_mgmt_api_banned.erl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ schema("/banned") ->
?DESC(create_banned_api_response400)
)
}
},
delete => #{
description => ?DESC(clear_banned_api),
tags => ?TAGS,
parameters => [],
'requestBody' => [],
responses => #{204 => <<"No Content">>}
}
};
schema("/banned/:as/:who") ->
Expand Down Expand Up @@ -168,7 +175,10 @@ banned(post, #{body := Body}) ->
OldBannedFormat = emqx_utils_json:encode(format(Old)),
{400, 'ALREADY_EXISTS', OldBannedFormat}
end
end.
end;
banned(delete, _) ->
emqx_banned:clear(),
{204}.

delete_banned(delete, #{bindings := Params}) ->
case emqx_banned:look_up(Params) of
Expand Down
28 changes: 28 additions & 0 deletions apps/emqx_management/test/emqx_mgmt_api_banned_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,30 @@ t_delete(_Config) ->
),
ok.

t_clear(_Config) ->
Now = erlang:system_time(second),
At = emqx_banned:to_rfc3339(Now),
Until = emqx_banned:to_rfc3339(Now + 3),
Who = <<"TestClient-"/utf8>>,
By = <<"banned suite 中"/utf8>>,
Reason = <<"test测试"/utf8>>,
As = <<"clientid">>,
Banned = #{
as => clientid,
who => Who,
by => By,
reason => Reason,
at => At,
until => Until
},
{ok, _} = create_banned(Banned),
?assertMatch({ok, _}, clear_banned()),
?assertMatch(
{error, {"HTTP/1.1", 404, "Not Found"}},
delete_banned(binary_to_list(As), binary_to_list(Who))
),
ok.

list_banned() ->
Path = emqx_mgmt_api_test_util:api_path(["banned"]),
case emqx_mgmt_api_test_util:request_api(get, Path) of
Expand All @@ -176,5 +200,9 @@ delete_banned(As, Who) ->
DeletePath = emqx_mgmt_api_test_util:api_path(["banned", As, Who]),
emqx_mgmt_api_test_util:request_api(delete, DeletePath).

clear_banned() ->
ClearPath = emqx_mgmt_api_test_util:api_path(["banned"]),
emqx_mgmt_api_test_util:request_api(delete, ClearPath).

to_rfc3339(Sec) ->
list_to_binary(calendar:system_time_to_rfc3339(Sec)).
1 change: 1 addition & 0 deletions changes/ce/feat-11436.en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a new API endpoint `DELETE /banned` to clear all `banned` data.
5 changes: 5 additions & 0 deletions rel/i18n/emqx_mgmt_api_banned.hocon
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ who.desc:
who.label:
"""Ban Object"""

clear_banned_api.desc:
"""Clear all banned data."""
clear_banned_api.label:
"""Clear"""

}