Quellcode durchsuchen

Merge pull request #9599 from lafirest/fix/blacklist_default_exp

fix(banned): ensure the default expiration time of `banned` is large enough
lafirest vor 3 Jahren
Ursprung
Commit
2ff6ef01f1

+ 5 - 1
apps/emqx/src/emqx_banned.erl

@@ -59,6 +59,10 @@
 
 -define(BANNED_TAB, ?MODULE).
 
+%% The default expiration time should be infinite
+%% but for compatibility, a large number (1 years) is used here to represent the 'infinite'
+-define(EXPIRATION_TIME, 31536000).
+
 -ifdef(TEST).
 -compile(export_all).
 -compile(nowarn_export_all).
@@ -126,7 +130,7 @@ parse(Params) ->
             By = maps:get(<<"by">>, Params, <<"mgmt_api">>),
             Reason = maps:get(<<"reason">>, Params, <<"">>),
             At = maps:get(<<"at">>, Params, erlang:system_time(second)),
-            Until = maps:get(<<"until">>, Params, At + 5 * 60),
+            Until = maps:get(<<"until">>, Params, At + ?EXPIRATION_TIME),
             case Until > erlang:system_time(second) of
                 true ->
                     #banned{

+ 24 - 0
apps/emqx_management/test/emqx_mgmt_api_banned_SUITE.erl

@@ -20,6 +20,8 @@
 
 -include_lib("eunit/include/eunit.hrl").
 
+-define(EXPIRATION_TIME, 31536000).
+
 all() ->
     emqx_common_test_helpers:all(?MODULE).
 
@@ -82,6 +84,28 @@ t_create(_Config) ->
     {ok, #{<<"data">> := List}} = list_banned(),
     Bans = lists:sort(lists:map(fun(#{<<"who">> := W, <<"as">> := A}) -> {A, W} end, List)),
     ?assertEqual([{<<"clientid">>, ClientId}, {<<"peerhost">>, PeerHost}], Bans),
+
+    ClientId2 = <<"TestClient2"/utf8>>,
+    ClientIdBanned2 = #{
+        as => As,
+        who => ClientId2,
+        by => By,
+        reason => Reason,
+        at => At
+    },
+    {ok, ClientIdBannedRes2} = create_banned(ClientIdBanned2),
+    Until2 = emqx_banned:to_rfc3339(Now + ?EXPIRATION_TIME),
+    ?assertEqual(
+        #{
+            <<"as">> => As,
+            <<"at">> => At,
+            <<"by">> => By,
+            <<"reason">> => Reason,
+            <<"until">> => Until2,
+            <<"who">> => ClientId2
+        },
+        ClientIdBannedRes2
+    ),
     ok.
 
 t_create_failed(_Config) ->

+ 2 - 0
changes/v5.0.13-en.md

@@ -16,6 +16,8 @@
 
 - Added the option to customize the clientid prefix of egress MQTT bridges. [#9609](https://github.com/emqx/emqx/pull/9609)
 
+- Ensure the default expiration time of `banned` is large enough [#9599](https://github.com/emqx/emqx/pull/9599/).
+
 ## Bug fixes
 
 - Trigger `message.dropped` hook when QoS2 message is resend by client with a same packet id, or 'awaiting_rel' queue is full [#9487](https://github.com/emqx/emqx/pull/9487).

+ 2 - 0
changes/v5.0.13-zh.md

@@ -16,6 +16,8 @@
 
 - 增加了自定义出口MQTT桥的clientid前缀的选项。[#9609](https://github.com/emqx/emqx/pull/9609)
 
+- 确保黑名单的默认超期时间足够长 [#9599](https://github.com/emqx/emqx/pull/9599/)。
+
 ## 修复
 
 - 当 QoS2 消息被重发(使用相同 Packet ID),或当 'awaiting_rel' 队列已满时,触发消息丢弃钩子(`message.dropped`)及计数器 [#9487](https://github.com/emqx/emqx/pull/9487)。