Skip to content

Commit b054f6e

Browse files
Merge pull request #3175 from processone/proxy_protocol_tls_info
Extract TLS informations that are delivered in PROXY protocol frame (cherry picked from commit 29bb9c5)
1 parent e6bc22c commit b054f6e

File tree

5 files changed

+48
-17
lines changed

5 files changed

+48
-17
lines changed

deps/amqp_client/src/amqp_direct_connection.erl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,14 @@ socket_adapter_info(Sock, Protocol) ->
195195

196196
maybe_ssl_info(Sock) ->
197197
RealSocket = rabbit_net:unwrap_socket(Sock),
198-
case rabbit_net:is_ssl(RealSocket) of
199-
true -> [{ssl, true}] ++ ssl_info(RealSocket) ++ ssl_cert_info(RealSocket);
200-
false -> [{ssl, false}]
198+
case rabbit_net:proxy_ssl_info(RealSocket, rabbit_net:maybe_get_proxy_socket(Sock)) of
199+
nossl -> [{ssl, false}];
200+
Info -> [{ssl, true}] ++ ssl_info(Info) ++ ssl_cert_info(RealSocket)
201201
end.
202202

203-
ssl_info(Sock) ->
203+
ssl_info(Info) ->
204204
{Protocol, KeyExchange, Cipher, Hash} =
205-
case rabbit_net:ssl_info(Sock) of
205+
case Info of
206206
{ok, Infos} ->
207207
{_, P} = lists:keyfind(protocol, 1, Infos),
208208
#{cipher := C,

deps/rabbit/src/rabbit_reader.erl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,7 +1506,8 @@ i(SockStat, S) when SockStat =:= recv_oct;
15061506
SockStat =:= send_pend ->
15071507
socket_info(fun (Sock) -> rabbit_net:getstat(Sock, [SockStat]) end,
15081508
fun ([{_, I}]) -> I end, S);
1509-
i(ssl, #v1{sock = Sock}) -> rabbit_net:is_ssl(Sock);
1509+
i(ssl, #v1{sock = Sock, proxy_socket = ProxySock}) ->
1510+
rabbit_net:proxy_ssl_info(Sock, ProxySock) /= nossl;
15101511
i(ssl_protocol, S) -> ssl_info(fun ({P, _}) -> P end, S);
15111512
i(ssl_key_exchange, S) -> ssl_info(fun ({_, {K, _, _}}) -> K end, S);
15121513
i(ssl_cipher, S) -> ssl_info(fun ({_, {_, C, _}}) -> C end, S);
@@ -1576,8 +1577,8 @@ socket_info(Get, Select, #v1{sock = Sock}) ->
15761577
{error, _} -> 0
15771578
end.
15781579

1579-
ssl_info(F, #v1{sock = Sock}) ->
1580-
case rabbit_net:ssl_info(Sock) of
1580+
ssl_info(F, #v1{sock = Sock, proxy_socket = ProxySock}) ->
1581+
case rabbit_net:proxy_ssl_info(Sock, ProxySock) of
15811582
nossl -> '';
15821583
{error, _} -> '';
15831584
{ok, Items} ->

deps/rabbit_common/src/rabbit_net.erl

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
setopts/2, send/2, close/1, fast_close/1, sockname/1, peername/1,
1616
peercert/1, connection_string/2, socket_ends/2, is_loopback/1,
1717
tcp_host/1, unwrap_socket/1, maybe_get_proxy_socket/1,
18-
hostname/0, getifaddrs/0]).
18+
hostname/0, getifaddrs/0, proxy_ssl_info/2]).
1919

2020
%%---------------------------------------------------------------------------
2121

@@ -34,6 +34,7 @@
3434
% -type host_or_ip() :: binary() | inet:ip_address().
3535
-spec is_ssl(socket()) -> boolean().
3636
-spec ssl_info(socket()) -> 'nossl' | ok_val_or_error([{atom(), any()}]).
37+
-spec proxy_ssl_info(socket(), ranch_proxy:proxy_socket()) -> 'nossl' | ok_val_or_error([{atom(), any()}]).
3738
-spec controlling_process(socket(), pid()) -> ok_or_any_error().
3839
-spec getstat(socket(), [stat_option()]) ->
3940
ok_val_or_error([{stat_option(), integer()}]).
@@ -98,6 +99,33 @@ ssl_info(Sock) when ?IS_SSL(Sock) ->
9899
ssl_info(_Sock) ->
99100
nossl.
100101

102+
proxy_ssl_info(Sock, {rabbit_proxy_socket, _, ProxyInfo}) ->
103+
case ProxyInfo of
104+
#{ssl := #{version := Version, cipher := Cipher}} ->
105+
Proto = case Version of
106+
<<"SSL3">> -> 'ssl3';
107+
<<"TLSv1">> -> 'tlsv1';
108+
<<"TLSv1.1">> -> 'tlsv1.1';
109+
<<"TLSv1.2">> -> 'tlsv1.2';
110+
<<"TLSv1.3">> -> 'tlsv1.3';
111+
_ -> nossl
112+
end,
113+
CipherSuite = case ssl:str_to_suite(binary_to_list(Cipher)) of
114+
#{} = CS -> CS;
115+
_ -> ssl_info(Sock)
116+
end,
117+
case {Proto, CipherSuite} of
118+
{nossl, _} -> ssl_info(Sock);
119+
{_, nossl} -> ssl_info(Sock);
120+
_ -> {ok, [{protocol, Proto}, {selected_cipher_suite, CipherSuite}]}
121+
end;
122+
_ ->
123+
ssl_info(Sock)
124+
end;
125+
proxy_ssl_info(Sock, _) ->
126+
ssl_info(Sock).
127+
128+
101129
controlling_process(Sock, Pid) when ?IS_SSL(Sock) ->
102130
ssl:controlling_process(Sock, Pid);
103131
controlling_process(Sock, Pid) when is_port(Sock) ->

deps/rabbitmq_amqp1_0/src/rabbit_amqp1_0_reader.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -793,8 +793,8 @@ socket_info(Get, Select, #v1{sock = Sock}) ->
793793
{error, _} -> ''
794794
end.
795795

796-
ssl_info(F, #v1{sock = Sock}) ->
797-
case rabbit_net:ssl_info(Sock) of
796+
ssl_info(F, #v1{sock = Sock, proxy_socket = ProxySock}) ->
797+
case rabbit_net:proxy_ssl_info(Sock, ProxySock) of
798798
nossl -> '';
799799
{error, _} -> '';
800800
{ok, Items} ->

deps/rabbitmq_stream/src/rabbit_stream_reader.erl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@
8282
resource_alarm :: boolean(),
8383
send_file_oct ::
8484
atomics:atomics_ref(), % number of bytes sent with send_file (for metrics)
85-
transport :: tcp | ssl}).
85+
transport :: tcp | ssl,
86+
proxy_socket :: undefined | ranch_proxy:proxy_socket()}).
8687
-record(configuration,
8788
{initial_credits :: integer(),
8889
credits_required_for_unblocking :: integer(),
@@ -224,7 +225,8 @@ init([KeepaliveSup,
224225
frame_max = FrameMax,
225226
resource_alarm = false,
226227
send_file_oct = SendFileOct,
227-
transport = ConnTransport},
228+
transport = ConnTransport,
229+
proxy_socket = rabbit_net:maybe_get_proxy_socket(Sock)},
228230
State =
229231
#stream_connection_state{consumers = #{},
230232
blocked = false,
@@ -2776,8 +2778,8 @@ i(host, #stream_connection{host = Host}, _) ->
27762778
Host;
27772779
i(peer_host, #stream_connection{peer_host = PeerHost}, _) ->
27782780
PeerHost;
2779-
i(ssl, #stream_connection{socket = Socket}, _) ->
2780-
rabbit_net:is_ssl(Socket);
2781+
i(ssl, #stream_connection{socket = Socket, proxy_socket = ProxySock}, _) ->
2782+
rabbit_net:proxy_ssl_info(Socket, ProxySock) /= nossl;
27812783
i(peer_cert_subject, S, _) ->
27822784
cert_info(fun rabbit_ssl:peer_cert_subject/1, S);
27832785
i(peer_cert_issuer, S, _) ->
@@ -2843,8 +2845,8 @@ cert_info(F, #stream_connection{socket = Sock}) ->
28432845
list_to_binary(F(Cert))
28442846
end.
28452847

2846-
ssl_info(F, #stream_connection{socket = Sock}) ->
2847-
case rabbit_net:ssl_info(Sock) of
2848+
ssl_info(F, #stream_connection{socket = Sock, proxy_socket = ProxySock}) ->
2849+
case rabbit_net:proxy_ssl_info(Sock, ProxySock) of
28482850
nossl ->
28492851
'';
28502852
{error, _} ->

0 commit comments

Comments
 (0)