Skip to content

Commit 034ef2c

Browse files
Merge pull request #13999 from rabbitmq/mk-pass-in-tls-certificate-password-as-a-function
TLS listener startup: wrap private key password option into a function
2 parents 08a74da + 67ee867 commit 034ef2c

File tree

5 files changed

+88
-3
lines changed

5 files changed

+88
-3
lines changed

deps/rabbit/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ define ct_master.erl
257257
halt(0)
258258
endef
259259

260-
PARALLEL_CT_SET_1_A = unit_cluster_formation_locking_mocks unit_cluster_formation_sort_nodes unit_collections unit_config_value_encryption unit_connection_tracking
260+
PARALLEL_CT_SET_1_A = unit_rabbit_ssl unit_cluster_formation_locking_mocks unit_cluster_formation_sort_nodes unit_collections unit_config_value_encryption unit_connection_tracking
261261
PARALLEL_CT_SET_1_B = amqp_address amqp_auth amqp_credit_api_v2 amqp_filtex amqp_dotnet amqp_jms signal_handling single_active_consumer unit_access_control_authn_authz_context_propagation unit_access_control_credential_validation unit_amqp091_content_framing unit_amqp091_server_properties unit_app_management
262262
PARALLEL_CT_SET_1_C = amqp_proxy_protocol amqpl_consumer_ack amqpl_direct_reply_to backing_queue bindings rabbit_db_maintenance rabbit_db_msup rabbit_db_policy rabbit_db_queue rabbit_db_topic_exchange rabbit_direct_reply_to_prop cluster_limit cluster_minority term_to_binary_compat_prop topic_permission transactions unicode unit_access_control
263263
PARALLEL_CT_SET_1_D = amqqueue_backward_compatibility channel_interceptor channel_operation_timeout classic_queue classic_queue_prop config_schema peer_discovery_dns peer_discovery_tmp_hidden_node per_node_limit per_user_connection_channel_limit

deps/rabbit/src/rabbit_definitions_import_https.erl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ load(Proplist) ->
4949
URL = pget(url, Proplist),
5050
rabbit_log:info("Applying definitions from a remote URL"),
5151
rabbit_log:debug("HTTPS URL: ~ts", [URL]),
52-
TLSOptions = tls_options_or_default(Proplist),
52+
TLSOptions0 = tls_options_or_default(Proplist),
53+
TLSOptions = rabbit_ssl:wrap_password_opt(TLSOptions0),
5354
HTTPOptions = http_options(TLSOptions),
5455
load_from_url(URL, HTTPOptions).
5556

deps/rabbit/src/rabbit_networking.erl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ start_ssl_listener(Listener, SslOpts, NumAcceptors) ->
297297
-spec start_ssl_listener(
298298
listener_config(), rabbit_types:infos(), integer(), integer()) -> 'ok' | {'error', term()}.
299299

300-
start_ssl_listener(Listener, SslOpts, NumAcceptors, ConcurrentConnsSupsCount) ->
300+
start_ssl_listener(Listener, SslOpts0, NumAcceptors, ConcurrentConnsSupsCount) ->
301+
SslOpts = rabbit_ssl:wrap_password_opt(SslOpts0),
301302
start_listener(Listener, NumAcceptors, ConcurrentConnsSupsCount, 'amqp/ssl',
302303
"TLS (SSL) listener", tcp_opts() ++ SslOpts).
303304

deps/rabbit/src/rabbit_ssl.erl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
cipher_suites_openssl/2, cipher_suites_openssl/1,
1616
cipher_suites/1]).
1717
-export([info/2, cert_info/2]).
18+
-export([wrap_password_opt/1]).
1819

1920
%%--------------------------------------------------------------------------
2021

@@ -34,6 +35,22 @@
3435
-type certificate() :: rabbit_cert_info:certificate().
3536

3637
-type cipher_suites_mode() :: default | all | anonymous.
38+
-type tls_opts() :: [ssl:tls_server_option()] | [ssl:tls_client_option()].
39+
40+
-spec wrap_password_opt(tls_opts()) -> tls_opts().
41+
wrap_password_opt(Opts0) ->
42+
case proplists:get_value(password, Opts0) of
43+
undefined ->
44+
Opts0;
45+
Fun when is_function(Fun) ->
46+
Opts0;
47+
Password ->
48+
%% A password can be a value or a function returning that value.
49+
%% See the key_pem_password/0 type in https://github.com/erlang/otp/pull/5843/files.
50+
NewOpts = proplists:delete(password, Opts0),
51+
Fun = fun() -> Password end,
52+
[{password, Fun} | NewOpts]
53+
end.
3754

3855
-spec cipher_suites(cipher_suites_mode()) -> ssl:ciphers().
3956
cipher_suites(Mode) ->
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
%% This Source Code Form is subject to the terms of the Mozilla Public
2+
%% License, v. 2.0. If a copy of the MPL was not distributed with this
3+
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
%%
5+
%% Copyright (c) 2007-2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
6+
%%
7+
8+
-module(unit_rabbit_ssl_SUITE).
9+
10+
-include_lib("eunit/include/eunit.hrl").
11+
12+
-compile(export_all).
13+
14+
all() ->
15+
[
16+
{group, parallel_tests}
17+
].
18+
19+
groups() ->
20+
[
21+
{parallel_tests, [], [
22+
wrap_tls_opts_with_binary_password,
23+
wrap_tls_opts_with_function_password
24+
]}
25+
].
26+
27+
28+
wrap_tls_opts_with_binary_password(_Config) ->
29+
Path = "/tmp/path/to/private_key.pem",
30+
Bin = <<"s3krE7">>,
31+
Opts0 = [
32+
{keyfile, Path},
33+
{password, Bin}
34+
],
35+
36+
Opts = rabbit_ssl:wrap_password_opt(Opts0),
37+
M = maps:from_list(Opts),
38+
39+
?assertEqual(Path, maps:get(keyfile, M)),
40+
?assert(is_function(maps:get(password, M))),
41+
42+
F = maps:get(password, M),
43+
?assertEqual(Bin, F()),
44+
45+
passed.
46+
47+
wrap_tls_opts_with_function_password(_Config) ->
48+
Path = "/tmp/path/to/private_key.pem",
49+
Bin = <<"s3krE7">>,
50+
Fun = fun() -> Bin end,
51+
Opts0 = [
52+
{keyfile, Path},
53+
{password, Fun}
54+
],
55+
56+
Opts = rabbit_ssl:wrap_password_opt(Opts0),
57+
M = maps:from_list(Opts),
58+
59+
?assertEqual(Path, maps:get(keyfile, M)),
60+
?assert(is_function(maps:get(password, M))),
61+
?assertEqual(Fun, maps:get(password, M)),
62+
63+
F = maps:get(password, M),
64+
?assertEqual(Bin, F()),
65+
66+
passed.

0 commit comments

Comments
 (0)