Skip to content

Commit ada87aa

Browse files
Merge pull request #14406 from rabbitmq/mergify/bp/v4.1.x/pr-14402
Close stream connection if secret update fails (backport #14402)
2 parents be6dd7f + b1e978f commit ada87aa

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

deps/rabbitmq_stream/src/rabbit_stream_reader.erl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,12 @@ open(info, {OK, S, Data},
694694
{next_state, close_sent,
695695
StatemData#statem_data{connection = Connection1,
696696
connection_state = State1}};
697+
failure ->
698+
_ = demonitor_all_streams(Connection),
699+
rabbit_log_connection:info("Force closing stream connection ~tp because of "
700+
"transition to invalid state",
701+
[self()]),
702+
{stop, {shutdown, <<"Invalid state">>}};
697703
_ ->
698704
State2 =
699705
case Blocked of
@@ -1583,6 +1589,7 @@ handle_frame_post_auth(Transport,
15831589
stream),
15841590
auth_fail(NewUsername, Msg, Args, C1, S1),
15851591
rabbit_log_connection:warning(Msg, Args),
1592+
silent_close_delay(),
15861593
{C1#stream_connection{connection_step = failure},
15871594
{sasl_authenticate,
15881595
?RESPONSE_AUTHENTICATION_FAILURE, <<>>}};
@@ -1628,6 +1635,7 @@ handle_frame_post_auth(Transport,
16281635
stream),
16291636
rabbit_log_connection:warning("Not allowed to change username '~ts'. Only password",
16301637
[Username]),
1638+
silent_close_delay(),
16311639
{C1#stream_connection{connection_step =
16321640
failure},
16331641
{sasl_authenticate,
@@ -1649,6 +1657,7 @@ handle_frame_post_auth(Transport,
16491657
{OtherMechanism, _} ->
16501658
rabbit_log_connection:warning("User '~ts' cannot change initial auth mechanism '~ts' for '~ts'",
16511659
[Username, NewMechanism, OtherMechanism]),
1660+
silent_close_delay(),
16521661
CmdBody =
16531662
{sasl_authenticate, ?RESPONSE_SASL_CANNOT_CHANGE_MECHANISM, <<>>},
16541663
Frame = rabbit_stream_core:frame({response, CorrelationId, CmdBody}),

deps/rabbitmq_stream/src/rabbit_stream_reader.hrl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
-type publisher_reference() :: binary().
2121
-type subscription_id() :: byte().
2222
-type internal_id() :: integer().
23+
-type connection_step() :: tcp_connected | peer_properties_exchanged |
24+
authenticating | authenticated | tuning |
25+
tuned | opened | failure |
26+
closing | close_sent | closing_done.
2327

2428
-record(publisher,
2529
{publisher_id :: publisher_id(),
@@ -75,8 +79,7 @@
7579
credits :: atomics:atomics_ref(),
7680
user :: undefined | #user{},
7781
virtual_host :: undefined | binary(),
78-
connection_step ::
79-
atom(), % tcp_connected, peer_properties_exchanged, authenticating, authenticated, tuning, tuned, opened, failure, closing, closing_done
82+
connection_step :: connection_step(),
8083
frame_max :: integer(),
8184
heartbeat :: undefined | integer(),
8285
heartbeater :: any(),

deps/rabbitmq_stream/test/rabbit_stream_SUITE.erl

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ groups() ->
4848
test_update_secret,
4949
cannot_update_username_after_authenticated,
5050
cannot_use_another_authmechanism_when_updating_secret,
51+
update_secret_should_close_connection_if_wrong_secret,
5152
unauthenticated_client_rejected_tcp_connected,
5253
timeout_tcp_connected,
5354
unauthenticated_client_rejected_peer_properties_exchanged,
@@ -292,22 +293,30 @@ test_update_secret(Config) ->
292293

293294
cannot_update_username_after_authenticated(Config) ->
294295
{S, C0} = connect_and_authenticate(gen_tcp, Config),
295-
C1 = expect_unsuccessful_authentication(
296-
try_authenticate(gen_tcp, S, C0, <<"PLAIN">>, <<"other">>, <<"other">>),
297-
?RESPONSE_SASL_CANNOT_CHANGE_USERNAME),
298-
_C2 = test_close(gen_tcp, S, C1),
296+
_C1 = expect_unsuccessful_authentication(
297+
try_authenticate(gen_tcp, S, C0, <<"PLAIN">>, <<"other">>, <<"other">>),
298+
?RESPONSE_SASL_CANNOT_CHANGE_USERNAME),
299299
closed = wait_for_socket_close(gen_tcp, S, 10),
300300
ok.
301301

302302
cannot_use_another_authmechanism_when_updating_secret(Config) ->
303303
{S, C0} = connect_and_authenticate(gen_tcp, Config),
304-
C1 = expect_unsuccessful_authentication(
305-
try_authenticate(gen_tcp, S, C0, <<"EXTERNAL">>, <<"guest">>, <<"new_password">>),
306-
?RESPONSE_SASL_CANNOT_CHANGE_MECHANISM),
307-
_C2 = test_close(gen_tcp, S, C1),
304+
_C1 = expect_unsuccessful_authentication(
305+
try_authenticate(gen_tcp, S, C0, <<"EXTERNAL">>, <<"guest">>, <<"new_password">>),
306+
?RESPONSE_SASL_CANNOT_CHANGE_MECHANISM),
308307
closed = wait_for_socket_close(gen_tcp, S, 10),
309308
ok.
310309

310+
update_secret_should_close_connection_if_wrong_secret(Config) ->
311+
Transport = gen_tcp,
312+
{S, C0} = connect_and_authenticate(Transport, Config),
313+
Pwd = rand:bytes(20),
314+
_C1 = expect_unsuccessful_authentication(
315+
try_authenticate(Transport, S, C0, <<"PLAIN">>, <<"guest">>, Pwd),
316+
?RESPONSE_AUTHENTICATION_FAILURE),
317+
closed = wait_for_socket_close(Transport, S, 10),
318+
ok.
319+
311320
test_stream_tls(Config) ->
312321
Stream = atom_to_binary(?FUNCTION_NAME, utf8),
313322
test_server(ssl, Stream, Config),

0 commit comments

Comments
 (0)