42
42
-define (QUEUE_TTL_KEY , <<" x-expires" >>).
43
43
-define (DEFAULT_EXCHANGE_NAME , <<>>).
44
44
45
+ -ifdef (TEST ).
46
+ -define (SILENT_CLOSE_DELAY , 10 ).
47
+ -else .
48
+ -define (SILENT_CLOSE_DELAY , 3_000 ).
49
+ -endif .
50
+
45
51
-type send_fun () :: fun ((iodata ()) -> ok ).
46
52
-type session_expiry_interval () :: non_neg_integer () | infinity .
47
53
-type subscriptions () :: #{topic_filter () => # mqtt_subscription_opts {}}.
@@ -621,16 +627,16 @@ check_extended_auth(_) ->
621
627
check_credentials (Username , Password , SslLoginName , PeerIp ) ->
622
628
case creds (Username , Password , SslLoginName ) of
623
629
nocreds ->
624
- auth_attempt_failed (PeerIp , <<>>),
625
630
? LOG_ERROR (" MQTT login failed: no credentials provided" ),
631
+ auth_attempt_failed (PeerIp , <<>>),
626
632
{error , ? RC_BAD_USER_NAME_OR_PASSWORD };
627
633
{invalid_creds , {undefined , Pass }} when is_binary (Pass ) ->
628
- auth_attempt_failed (PeerIp , <<>>),
629
634
? LOG_ERROR (" MQTT login failed: no username is provided" ),
635
+ auth_attempt_failed (PeerIp , <<>>),
630
636
{error , ? RC_BAD_USER_NAME_OR_PASSWORD };
631
637
{invalid_creds , {User , _Pass }} when is_binary (User ) ->
632
- auth_attempt_failed (PeerIp , User ),
633
638
? LOG_ERROR (" MQTT login failed for user '~s ': no password provided" , [User ]),
639
+ auth_attempt_failed (PeerIp , User ),
634
640
{error , ? RC_BAD_USER_NAME_OR_PASSWORD };
635
641
{UserBin , PassBin } ->
636
642
{ok , {UserBin , PassBin }}
@@ -998,8 +1004,8 @@ check_vhost_exists(VHost, Username, PeerIp) ->
998
1004
true ->
999
1005
ok ;
1000
1006
false ->
1001
- auth_attempt_failed (PeerIp , Username ),
1002
1007
? LOG_ERROR (" MQTT connection failed: virtual host '~s ' does not exist" , [VHost ]),
1008
+ auth_attempt_failed (PeerIp , Username ),
1003
1009
{error , ? RC_BAD_USER_NAME_OR_PASSWORD }
1004
1010
end .
1005
1011
@@ -1038,10 +1044,10 @@ check_user_login(VHost, Username, Password, ClientId, PeerIp, ConnName) ->
1038
1044
notify_auth_result (user_authentication_success , Username1 , ConnName ),
1039
1045
{ok , User };
1040
1046
{refused , Username , Msg , Args } ->
1041
- auth_attempt_failed (PeerIp , Username ),
1042
1047
? LOG_ERROR (" MQTT connection failed: access refused for user '~s ':" ++ Msg ,
1043
1048
[Username | Args ]),
1044
1049
notify_auth_result (user_authentication_failure , Username , ConnName ),
1050
+ auth_attempt_failed (PeerIp , Username ),
1045
1051
{error , ? RC_BAD_USER_NAME_OR_PASSWORD }
1046
1052
end .
1047
1053
@@ -1070,9 +1076,9 @@ check_vhost_access(VHost, User = #user{username = Username}, ClientId, PeerIp) -
1070
1076
ok ->
1071
1077
{ok , AuthzCtx }
1072
1078
catch exit :# amqp_error {name = not_allowed } ->
1073
- auth_attempt_failed (PeerIp , Username ),
1074
1079
? LOG_ERROR (" MQTT connection failed: access refused for user '~s ' to vhost '~s '" ,
1075
1080
[Username , VHost ]),
1081
+ auth_attempt_failed (PeerIp , Username ),
1076
1082
{error , ? RC_NOT_AUTHORIZED }
1077
1083
end .
1078
1084
@@ -1081,9 +1087,9 @@ check_user_loopback(Username, PeerIp) ->
1081
1087
ok ->
1082
1088
ok ;
1083
1089
not_allowed ->
1090
+ ? LOG_WARNING (" MQTT login failed: user '~s ' can only connect via localhost" ,
1091
+ [Username ]),
1084
1092
auth_attempt_failed (PeerIp , Username ),
1085
- ? LOG_WARNING (
1086
- " MQTT login failed: user '~s ' can only connect via localhost" , [Username ]),
1087
1093
{error , ? RC_NOT_AUTHORIZED }
1088
1094
end .
1089
1095
@@ -1102,8 +1108,8 @@ ensure_credential_expiry_timer(User = #user{username = Username}, PeerIp) ->
1102
1108
_TimerRef = erlang :send_after (Time , self (), credential_expired ),
1103
1109
ok ;
1104
1110
false ->
1105
- auth_attempt_failed (PeerIp , Username ),
1106
1111
? LOG_WARNING (" Credential expired ~b ms ago" , [abs (Time )]),
1112
+ auth_attempt_failed (PeerIp , Username ),
1107
1113
{error , ? RC_NOT_AUTHORIZED }
1108
1114
end
1109
1115
end .
@@ -1222,7 +1228,8 @@ creds(User, Pass, SSLLoginName) ->
1222
1228
1223
1229
-spec auth_attempt_failed (inet :ip_address (), binary ()) -> ok .
1224
1230
auth_attempt_failed (PeerIp , Username ) ->
1225
- rabbit_core_metrics :auth_attempt_failed (PeerIp , Username , mqtt ).
1231
+ rabbit_core_metrics :auth_attempt_failed (PeerIp , Username , mqtt ),
1232
+ timer :sleep (? SILENT_CLOSE_DELAY ).
1226
1233
1227
1234
maybe_downgrade_qos (? QOS_0 ) -> ? QOS_0 ;
1228
1235
maybe_downgrade_qos (? QOS_1 ) -> ? QOS_1 ;
0 commit comments