Wrap TLS options password into a function in more places

A follow-up to #13958 #13999.

Pair: @dcorbacho.
This commit is contained in:
Michael Klishin 2025-06-04 12:24:45 +04:00
parent f7a238a8f4
commit e9fc656241
No known key found for this signature in database
GPG Key ID: 16AB14D00D613900
6 changed files with 48 additions and 27 deletions

View File

@ -39,18 +39,7 @@
-spec wrap_password_opt(tls_opts()) -> tls_opts().
wrap_password_opt(Opts0) ->
case proplists:get_value(password, Opts0) of
undefined ->
Opts0;
Fun when is_function(Fun) ->
Opts0;
Password ->
%% A password can be a value or a function returning that value.
%% See the key_pem_password/0 type in https://github.com/erlang/otp/pull/5843/files.
NewOpts = proplists:delete(password, Opts0),
Fun = fun() -> Password end,
[{password, Fun} | NewOpts]
end.
rabbit_ssl_options:wrap_password_opt(Opts0).
-spec cipher_suites(cipher_suites_mode()) -> ssl:ciphers().
cipher_suites(Mode) ->

View File

@ -33,7 +33,7 @@ wrap_tls_opts_with_binary_password(_Config) ->
{password, Bin}
],
Opts = rabbit_ssl:wrap_password_opt(Opts0),
Opts = rabbit_ssl_options:wrap_password_opt(Opts0),
M = maps:from_list(Opts),
?assertEqual(Path, maps:get(keyfile, M)),
@ -53,7 +53,7 @@ wrap_tls_opts_with_function_password(_Config) ->
{password, Fun}
],
Opts = rabbit_ssl:wrap_password_opt(Opts0),
Opts = rabbit_ssl_options:wrap_password_opt(Opts0),
M = maps:from_list(Opts),
?assertEqual(Path, maps:get(keyfile, M)),

View File

@ -7,15 +7,34 @@
-module(rabbit_ssl_options).
-export([fix/1]).
-export([fix_client/1]).
-export([
fix/1,
fix_client/1,
wrap_password_opt/1
]).
-define(BAD_SSL_PROTOCOL_VERSIONS, [
%% POODLE
sslv3
]).
-type tls_opts() :: [ssl:tls_server_option()] | [ssl:tls_client_option()].
-spec wrap_password_opt(tls_opts()) -> tls_opts().
wrap_password_opt(Opts0) ->
case proplists:get_value(password, Opts0) of
undefined ->
Opts0;
Fun when is_function(Fun) ->
Opts0;
Password ->
%% A password can be a value or a function returning that value.
%% See the key_pem_password/0 type in https://github.com/erlang/otp/pull/5843/files.
NewOpts = proplists:delete(password, Opts0),
Fun = fun() -> Password end,
[{password, Fun} | NewOpts]
end.
-spec fix(rabbit_types:infos()) -> rabbit_types:infos().
fix(Config) ->

View File

@ -128,16 +128,17 @@ get_legacy_listener() ->
get_tls_listener() ->
{ok, Listener0} = application:get_env(rabbitmq_management, ssl_config),
{ok, Listener1} = ensure_port(tls, Listener0),
Listener2 = rabbit_ssl:wrap_password_opt(Listener1),
Port = proplists:get_value(port, Listener1),
case proplists:get_value(cowboy_opts, Listener0) of
undefined ->
[
{port, Port},
{ssl, true},
{ssl_opts, Listener0}
{ssl_opts, Listener2}
];
CowboyOpts ->
WithoutCowboyOpts = lists:keydelete(cowboy_opts, 1, Listener0),
WithoutCowboyOpts = lists:keydelete(cowboy_opts, 1, Listener2),
[
{port, Port},
{ssl, true},

View File

@ -34,7 +34,16 @@ init(_) ->
-spec start_configured_listener() -> ok.
start_configured_listener() ->
TCPListenerConf = get_env(tcp_config, []),
TLSListenerConf = get_env(ssl_config, []),
TLSListenerConf0 = get_env(ssl_config, []),
TLSListenerConf =
case proplists:get_value(ssl_opts, TLSListenerConf0, undef) of
undef ->
TLSListenerConf0;
Opts0 ->
Opts = rabbit_ssl:wrap_password_opt(Opts0),
Tmp = proplists:delete(ssl_opts, TLSListenerConf0),
[{ssl_opts, Opts} | Tmp]
end,
case {TCPListenerConf, TLSListenerConf} of
%% nothing is configured
@ -64,10 +73,11 @@ start_configured_tcp_listener(Conf) ->
start_configured_tls_listener(Conf) ->
case Conf of
[] -> ok;
SSLCon ->
SSLListener0 = [{ssl, true} | SSLCon],
SSLListener1 = maybe_disable_sendfile(SSLListener0),
start_listener(SSLListener1)
TLSConf ->
TLSListener0 = [{ssl, true} | TLSConf],
TLSListener1 = maybe_disable_sendfile(TLSListener0),
TLSListener2 = rabbit_ssl:wrap_password_opt(TLSListener1),
start_listener(TLSListener2)
end.
maybe_disable_sendfile(Listener) ->

View File

@ -27,7 +27,8 @@ ensure_listener(Listener) ->
undefined ->
{error, {no_port_given, Listener}};
_ ->
{Transport, TransportOpts, ProtoOpts} = preprocess_config(Listener),
{Transport, TransportOpts0, ProtoOpts} = preprocess_config(Listener),
TransportOpts = rabbit_ssl_options:wrap_password_opt(TransportOpts0),
ProtoOptsMap = maps:from_list(ProtoOpts),
StreamHandlers = stream_handlers_config(ProtoOpts),
rabbit_log:debug("Starting HTTP[S] listener with transport ~ts", [Transport]),
@ -86,9 +87,10 @@ auto_ssl(Options) ->
fix_ssl([{ssl_opts, SSLOpts} | Options]).
fix_ssl(Options) ->
SSLOpts = proplists:get_value(ssl_opts, Options),
TLSOpts0 = proplists:get_value(ssl_opts, Options),
TLSOpts = rabbit_ssl_options:wrap_password_opt(TLSOpts0),
{ranch_ssl,
transport_config(Options ++ rabbit_networking:fix_ssl_options(SSLOpts)),
transport_config(Options ++ rabbit_networking:fix_ssl_options(TLSOpts)),
protocol_config(Options)}.
transport_config(Options0) ->