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 {}}.
@@ -643,16 +649,16 @@ check_extended_auth(_) ->
643
649
check_credentials (Username , Password , SslLoginName , PeerIp ) ->
644
650
case creds (Username , Password , SslLoginName ) of
645
651
nocreds ->
646
- auth_attempt_failed (PeerIp , <<>>),
647
652
? LOG_ERROR (" MQTT login failed: no credentials provided" ),
653
+ auth_attempt_failed (PeerIp , <<>>),
648
654
{error , ? RC_BAD_USER_NAME_OR_PASSWORD };
649
655
{invalid_creds , {undefined , Pass }} when is_binary (Pass ) ->
650
- auth_attempt_failed (PeerIp , <<>>),
651
656
? LOG_ERROR (" MQTT login failed: no username is provided" ),
657
+ auth_attempt_failed (PeerIp , <<>>),
652
658
{error , ? RC_BAD_USER_NAME_OR_PASSWORD };
653
659
{invalid_creds , {User , _Pass }} when is_binary (User ) ->
654
- auth_attempt_failed (PeerIp , User ),
655
660
? LOG_ERROR (" MQTT login failed for user '~s ': no password provided" , [User ]),
661
+ auth_attempt_failed (PeerIp , User ),
656
662
{error , ? RC_BAD_USER_NAME_OR_PASSWORD };
657
663
{UserBin , PassBin } ->
658
664
{ok , {UserBin , PassBin }}
@@ -1038,8 +1044,8 @@ check_vhost_exists(VHost, Username, PeerIp) ->
1038
1044
true ->
1039
1045
ok ;
1040
1046
false ->
1041
- auth_attempt_failed (PeerIp , Username ),
1042
1047
? LOG_ERROR (" MQTT connection failed: virtual host '~s ' does not exist" , [VHost ]),
1048
+ auth_attempt_failed (PeerIp , Username ),
1043
1049
{error , ? RC_BAD_USER_NAME_OR_PASSWORD }
1044
1050
end .
1045
1051
@@ -1078,10 +1084,10 @@ check_user_login(VHost, Username, Password, ClientId, PeerIp, ConnName) ->
1078
1084
notify_auth_result (user_authentication_success , Username1 , ConnName ),
1079
1085
{ok , User };
1080
1086
{refused , Username , Msg , Args } ->
1081
- auth_attempt_failed (PeerIp , Username ),
1082
1087
? LOG_ERROR (" MQTT connection failed: access refused for user '~s ':" ++ Msg ,
1083
1088
[Username | Args ]),
1084
1089
notify_auth_result (user_authentication_failure , Username , ConnName ),
1090
+ auth_attempt_failed (PeerIp , Username ),
1085
1091
{error , ? RC_BAD_USER_NAME_OR_PASSWORD }
1086
1092
end .
1087
1093
@@ -1110,9 +1116,9 @@ check_vhost_access(VHost, User = #user{username = Username}, ClientId, PeerIp) -
1110
1116
ok ->
1111
1117
{ok , AuthzCtx }
1112
1118
catch exit :# amqp_error {name = not_allowed } ->
1113
- auth_attempt_failed (PeerIp , Username ),
1114
1119
? LOG_ERROR (" MQTT connection failed: access refused for user '~s ' to vhost '~s '" ,
1115
1120
[Username , VHost ]),
1121
+ auth_attempt_failed (PeerIp , Username ),
1116
1122
{error , ? RC_NOT_AUTHORIZED }
1117
1123
end .
1118
1124
@@ -1121,9 +1127,9 @@ check_user_loopback(Username, PeerIp) ->
1121
1127
ok ->
1122
1128
ok ;
1123
1129
not_allowed ->
1130
+ ? LOG_WARNING (" MQTT login failed: user '~s ' can only connect via localhost" ,
1131
+ [Username ]),
1124
1132
auth_attempt_failed (PeerIp , Username ),
1125
- ? LOG_WARNING (
1126
- " MQTT login failed: user '~s ' can only connect via localhost" , [Username ]),
1127
1133
{error , ? RC_NOT_AUTHORIZED }
1128
1134
end .
1129
1135
@@ -1142,8 +1148,8 @@ ensure_credential_expiry_timer(User = #user{username = Username}, PeerIp) ->
1142
1148
_TimerRef = erlang :send_after (Time , self (), credential_expired ),
1143
1149
ok ;
1144
1150
false ->
1145
- auth_attempt_failed (PeerIp , Username ),
1146
1151
? LOG_WARNING (" Credential expired ~b ms ago" , [abs (Time )]),
1152
+ auth_attempt_failed (PeerIp , Username ),
1147
1153
{error , ? RC_NOT_AUTHORIZED }
1148
1154
end
1149
1155
end .
@@ -1262,7 +1268,8 @@ creds(User, Pass, SSLLoginName) ->
1262
1268
1263
1269
-spec auth_attempt_failed (inet :ip_address (), binary ()) -> ok .
1264
1270
auth_attempt_failed (PeerIp , Username ) ->
1265
- rabbit_core_metrics :auth_attempt_failed (PeerIp , Username , mqtt ).
1271
+ rabbit_core_metrics :auth_attempt_failed (PeerIp , Username , mqtt ),
1272
+ timer :sleep (? SILENT_CLOSE_DELAY ).
1266
1273
1267
1274
maybe_downgrade_qos (? QOS_0 ) -> ? QOS_0 ;
1268
1275
maybe_downgrade_qos (? QOS_1 ) -> ? QOS_1 ;
0 commit comments