Skip to content

Commit 5f7e229

Browse files
author
Daniil Fedotov
committed
Refuse connections to dead vhosts.
If a vhost supervision tree is not active, which can be a result of an error in message store, refuse connections to this vhost on the node. This is a follow-up to [#140841611] [#1158] [#145106713]
1 parent 7a82b43 commit 5f7e229

File tree

2 files changed

+47
-16
lines changed

2 files changed

+47
-16
lines changed

src/rabbit_direct.erl

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,21 @@ connect(Creds, VHost, Protocol, Pid, Infos) ->
9090
true ->
9191
{error, not_allowed};
9292
false ->
93-
case AuthFun() of
94-
{ok, User = #user{username = Username}} ->
95-
notify_auth_result(Username,
96-
user_authentication_success, []),
97-
connect1(User, VHost, Protocol, Pid, Infos);
98-
{refused, Username, Msg, Args} ->
99-
notify_auth_result(Username,
100-
user_authentication_failure,
101-
[{error, rabbit_misc:format(Msg, Args)}]),
102-
{error, {auth_failure, "Refused"}}
93+
case is_vhost_alive(VHost, Creds, Pid) of
94+
false ->
95+
{error, {internal_error, vhost_is_down}};
96+
true ->
97+
case AuthFun() of
98+
{ok, User = #user{username = Username}} ->
99+
notify_auth_result(Username,
100+
user_authentication_success, []),
101+
connect1(User, VHost, Protocol, Pid, Infos);
102+
{refused, Username, Msg, Args} ->
103+
notify_auth_result(Username,
104+
user_authentication_failure,
105+
[{error, rabbit_misc:format(Msg, Args)}]),
106+
{error, {auth_failure, "Refused"}}
107+
end
103108
end
104109
end;
105110
false -> {error, broker_not_found_on_node}
@@ -140,6 +145,21 @@ maybe_call_connection_info_module(Protocol, Creds, VHost, Pid, Infos) ->
140145
[]
141146
end.
142147

148+
is_vhost_alive(VHost, {Username, _Password}, Pid) ->
149+
PrintedUsername = case Username of
150+
none -> "";
151+
_ -> Username
152+
end,
153+
case rabbit_vhost_sup_sup:is_vhost_alive(VHost) of
154+
true -> true;
155+
false ->
156+
rabbit_log_connection:error(
157+
"Error on Direct connection ~p~n"
158+
"access to vhost '~s' refused for user '~s': "
159+
"vhost '~s' is down",
160+
[Pid, VHost, PrintedUsername, VHost]),
161+
false
162+
end.
143163

144164
is_over_connection_limit(VHost, {Username, _Password}, Pid) ->
145165
PrintedUsername = case Username of

src/rabbit_reader.erl

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ handle_other(handshake_timeout, State) ->
567567
throw({handshake_timeout, State#v1.callback});
568568
handle_other(heartbeat_timeout, State = #v1{connection_state = closed}) ->
569569
State;
570-
handle_other(heartbeat_timeout,
570+
handle_other(heartbeat_timeout,
571571
State = #v1{connection = #connection{timeout_sec = T}}) ->
572572
maybe_emit_stats(State),
573573
throw({heartbeat_timeout, T});
@@ -623,7 +623,7 @@ send_blocked(#v1{connection = #connection{protocol = Protocol,
623623
sock = Sock}, Reason) ->
624624
case rabbit_misc:table_lookup(Capabilities, <<"connection.blocked">>) of
625625
{bool, true} ->
626-
626+
627627
ok = send_on_channel0(Sock, #'connection.blocked'{reason = Reason},
628628
Protocol);
629629
_ ->
@@ -1164,6 +1164,7 @@ handle_method0(#'connection.open'{virtual_host = VHost},
11641164

11651165
ok = is_over_connection_limit(VHost, User),
11661166
ok = rabbit_access_control:check_vhost_access(User, VHost, Sock),
1167+
ok = is_vhost_alive(VHost, User),
11671168
NewConnection = Connection#connection{vhost = VHost},
11681169
ok = send_on_channel0(Sock, #'connection.open_ok'{}, Protocol),
11691170

@@ -1209,6 +1210,16 @@ handle_method0(_Method, #v1{connection_state = S}) ->
12091210
rabbit_misc:protocol_error(
12101211
channel_error, "unexpected method in connection state ~w", [S]).
12111212

1213+
is_vhost_alive(VHostPath, User) ->
1214+
case rabbit_vhost_sup_sup:is_vhost_alive(VHostPath) of
1215+
true -> ok;
1216+
false ->
1217+
rabbit_misc:protocol_error(internal_error,
1218+
"access to vhost '~s' refused for user '~s': "
1219+
"vhost '~s' is down",
1220+
[VHostPath, User#user.username, VHostPath])
1221+
end.
1222+
12121223
is_over_connection_limit(VHostPath, User) ->
12131224
try rabbit_vhost_limit:is_over_connection_limit(VHostPath) of
12141225
false -> ok;
@@ -1567,7 +1578,7 @@ maybe_block(State = #v1{connection_state = CS, throttle = Throttle}) ->
15671578
State1 = State#v1{connection_state = blocked,
15681579
throttle = update_last_blocked_at(Throttle)},
15691580
case CS of
1570-
running ->
1581+
running ->
15711582
ok = rabbit_heartbeat:pause_monitor(State#v1.heartbeater);
15721583
_ -> ok
15731584
end,
@@ -1589,7 +1600,7 @@ maybe_send_unblocked(State = #v1{throttle = Throttle}) ->
15891600
case should_send_unblocked(Throttle) of
15901601
true ->
15911602
ok = send_unblocked(State),
1592-
State#v1{throttle =
1603+
State#v1{throttle =
15931604
Throttle#throttle{connection_blocked_message_sent = false}};
15941605
false -> State
15951606
end.
@@ -1598,7 +1609,7 @@ maybe_send_blocked_or_unblocked(State = #v1{throttle = Throttle}) ->
15981609
case should_send_blocked(Throttle) of
15991610
true ->
16001611
ok = send_blocked(State, blocked_by_message(Throttle)),
1601-
State#v1{throttle =
1612+
State#v1{throttle =
16021613
Throttle#throttle{connection_blocked_message_sent = true}};
16031614
false -> maybe_send_unblocked(State)
16041615
end.
@@ -1624,7 +1635,7 @@ control_throttle(State = #v1{connection_state = CS,
16241635
running -> maybe_block(State1);
16251636
%% unblock or re-enable blocking
16261637
blocked -> maybe_block(maybe_unblock(State1));
1627-
_ -> State1
1638+
_ -> State1
16281639
end.
16291640

16301641
augment_connection_log_name(#connection{client_properties = ClientProperties,

0 commit comments

Comments
 (0)