Skip to content

Commit 1d1f6bf

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 e235e87 commit 1d1f6bf

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
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
@@ -1509,7 +1509,8 @@ i(SockStat, S) when SockStat =:= recv_oct;
15091509
SockStat =:= send_pend ->
15101510
socket_info(fun (Sock) -> rabbit_net:getstat(Sock, [SockStat]) end,
15111511
fun ([{_, I}]) -> I end, S);
1512-
i(ssl, #v1{sock = Sock}) -> rabbit_net:is_ssl(Sock);
1512+
i(ssl, #v1{sock = Sock, proxy_socket = ProxySock}) ->
1513+
rabbit_net:proxy_ssl_info(Sock, ProxySock) /= nossl;
15131514
i(ssl_protocol, S) -> ssl_info(fun ({P, _}) -> P end, S);
15141515
i(ssl_key_exchange, S) -> ssl_info(fun ({_, {K, _, _}}) -> K end, S);
15151516
i(ssl_cipher, S) -> ssl_info(fun ({_, {_, C, _}}) -> C end, S);
@@ -1579,8 +1580,8 @@ socket_info(Get, Select, #v1{sock = Sock}) ->
15791580
{error, _} -> 0
15801581
end.
15811582

1582-
ssl_info(F, #v1{sock = Sock}) ->
1583-
case rabbit_net:ssl_info(Sock) of
1583+
ssl_info(F, #v1{sock = Sock, proxy_socket = ProxySock}) ->
1584+
case rabbit_net:proxy_ssl_info(Sock, ProxySock) of
15841585
nossl -> '';
15851586
{error, _} -> '';
15861587
{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} ->

0 commit comments

Comments
 (0)