Skip to content

Commit 02449bd

Browse files
committed
Close stream connection if secret update fails
1 parent 4d60fc4 commit 02449bd

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
@@ -697,6 +697,12 @@ open(info, {OK, S, Data},
697697
{next_state, close_sent,
698698
StatemData#statem_data{connection = Connection1,
699699
connection_state = State1}};
700+
failure ->
701+
_ = demonitor_all_streams(Connection),
702+
?LOG_INFO("Forcing stream connection ~tp closing because of "
703+
"transition to invalid state",
704+
[self()]),
705+
{stop, {shutdown, <<"Invalid state">>}};
700706
_ ->
701707
State2 =
702708
case Blocked of
@@ -1586,6 +1592,7 @@ handle_frame_post_auth(Transport,
15861592
stream),
15871593
auth_fail(NewUsername, Msg, Args, C1, S1),
15881594
?LOG_WARNING(Msg, Args),
1595+
silent_close_delay(),
15891596
{C1#stream_connection{connection_step = failure},
15901597
{sasl_authenticate,
15911598
?RESPONSE_AUTHENTICATION_FAILURE, <<>>}};
@@ -1631,6 +1638,7 @@ handle_frame_post_auth(Transport,
16311638
stream),
16321639
?LOG_WARNING("Not allowed to change username '~ts'. Only password",
16331640
[Username]),
1641+
silent_close_delay(),
16341642
{C1#stream_connection{connection_step =
16351643
failure},
16361644
{sasl_authenticate,
@@ -1652,6 +1660,7 @@ handle_frame_post_auth(Transport,
16521660
{OtherMechanism, _} ->
16531661
?LOG_WARNING("User '~ts' cannot change initial auth mechanism '~ts' for '~ts'",
16541662
[Username, NewMechanism, OtherMechanism]),
1663+
silent_close_delay(),
16551664
CmdBody =
16561665
{sasl_authenticate, ?RESPONSE_SASL_CANNOT_CHANGE_MECHANISM, <<>>},
16571666
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)