Skip to content

Commit b94ca39

Browse files
Merge pull request #3305 from rabbitmq/mergify/bp/v3.9.x/pr-3299
Add support to override `default_{user,pass,vhost}` and the Erlang cookie from the environment (backport #3299)
2 parents b21148f + cd3f6e4 commit b94ca39

File tree

4 files changed

+157
-4
lines changed

4 files changed

+157
-4
lines changed

deps/rabbit/apps/rabbitmq_prelaunch/src/rabbit_prelaunch_dist.erl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
-module(rabbit_prelaunch_dist).
22

3+
-include_lib("eunit/include/eunit.hrl").
34
-include_lib("kernel/include/logger.hrl").
45

56
-include_lib("rabbit_common/include/logging.hrl").
@@ -33,7 +34,9 @@ setup(#{nodename := Node, nodename_type := NameType} = Context) ->
3334
end,
3435
ok.
3536

36-
do_setup(#{nodename := Node, nodename_type := NameType}) ->
37+
do_setup(#{nodename := Node,
38+
nodename_type := NameType,
39+
var_origins := Origins} = Config) ->
3740
?LOG_DEBUG(
3841
"Starting Erlang distribution",
3942
#{domain => ?RMQLOG_DOMAIN_PRELAUNCH}),
@@ -49,6 +52,19 @@ do_setup(#{nodename := Node, nodename_type := NameType}) ->
4952
{ok, _} = net_kernel:start([Node, NameType]),
5053
ok
5154
end,
55+
56+
%% Override the Erlang cookie if one was set in the environment.
57+
case maps:get(erlang_cookie, Origins, default) of
58+
environment ->
59+
?LOG_WARNING(
60+
"Overriding Erlang cookie using the value set in the environment",
61+
#{domain => ?RMQLOG_DOMAIN_PRELAUNCH}),
62+
Cookie = maps:get(erlang_cookie, Config),
63+
?assert(is_atom(Cookie)),
64+
true = erlang:set_cookie(node(), Cookie);
65+
_ ->
66+
ok
67+
end,
5268
ok.
5369

5470
%% Check whether a node with the same name is already running

deps/rabbit/src/rabbit.erl

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
-module(rabbit).
99

10+
-include_lib("eunit/include/eunit.hrl").
1011
-include_lib("kernel/include/logger.hrl").
1112
-include_lib("rabbit_common/include/logging.hrl").
1213

@@ -1047,10 +1048,10 @@ maybe_insert_default_data() ->
10471048
end.
10481049

10491050
insert_default_data() ->
1050-
{ok, DefaultUser} = application:get_env(default_user),
1051-
{ok, DefaultPass} = application:get_env(default_pass),
1051+
DefaultUser = get_default_data_param(default_user),
1052+
DefaultPass = get_default_data_param(default_pass),
10521053
{ok, DefaultTags} = application:get_env(default_user_tags),
1053-
{ok, DefaultVHost} = application:get_env(default_vhost),
1054+
DefaultVHost = get_default_data_param(default_vhost),
10541055
{ok, [DefaultConfigurePerm, DefaultWritePerm, DefaultReadPerm]} =
10551056
application:get_env(default_permissions),
10561057

@@ -1077,6 +1078,18 @@ insert_default_data() ->
10771078
?INTERNAL_USER),
10781079
ok.
10791080

1081+
get_default_data_param(Param) ->
1082+
#{var_origins := Origins} = Context = rabbit_prelaunch:get_context(),
1083+
case maps:get(Param, Origins, default) of
1084+
environment ->
1085+
Value = maps:get(Param, Context),
1086+
?assert(is_binary(Value)),
1087+
Value;
1088+
default ->
1089+
{ok, Value} = application:get_env(Param),
1090+
Value
1091+
end.
1092+
10801093
%%---------------------------------------------------------------------------
10811094
%% logging
10821095

deps/rabbit_common/src/rabbit_env.erl

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,13 @@
5151
"RABBITMQ_CONFIG_FILE",
5252
"RABBITMQ_CONFIG_FILES",
5353
"RABBITMQ_DBG",
54+
"RABBITMQ_DEFAULT_PASS",
55+
"RABBITMQ_DEFAULT_USER",
56+
"RABBITMQ_DEFAULT_VHOST",
5457
"RABBITMQ_DIST_PORT",
5558
"RABBITMQ_ENABLED_PLUGINS",
5659
"RABBITMQ_ENABLED_PLUGINS_FILE",
60+
"RABBITMQ_ERLANG_COOKIE",
5761
"RABBITMQ_FEATURE_FLAGS",
5862
"RABBITMQ_FEATURE_FLAGS_FILE",
5963
"RABBITMQ_HOME",
@@ -152,6 +156,10 @@ get_context_after_reloading_env(Context) ->
152156
fun plugins_expand_dir/1,
153157
fun enabled_plugins_file/1,
154158
fun enabled_plugins/1,
159+
fun default_vhost/1,
160+
fun default_user/1,
161+
fun default_pass/1,
162+
fun erlang_cookie/1,
155163
fun maybe_stop_dist_for_remote_query/1,
156164
fun amqp_ipaddr/1,
157165
fun amqp_tcp_port/1,
@@ -1439,6 +1447,62 @@ motd_file_from_node(#{from_remote_node := Remote} = Context) ->
14391447
update_context(Context, motd_file, File, remote_node)
14401448
end.
14411449

1450+
%% -------------------------------------------------------------------
1451+
%%
1452+
%% RABBITMQ_DEFAULT_VHOST
1453+
%% Override the default virtual host.
1454+
%% Default: unset (i.e. <<"/">>)
1455+
%%
1456+
%% RABBITMQ_DEFAULT_USER
1457+
%% Override the default username.
1458+
%% Default: unset (i.e. <<"guest">>).
1459+
%%
1460+
%% RABBITMQ_MOTD_FILE
1461+
%% Override the default user's password.
1462+
%% Default: unset (i.e. <<"guest">>).
1463+
1464+
default_vhost(Context) ->
1465+
case get_prefixed_env_var("RABBITMQ_DEFAULT_VHOST") of
1466+
false ->
1467+
update_context(Context, default_vhost, undefined, default);
1468+
Value ->
1469+
VHost = list_to_binary(Value),
1470+
update_context(Context, default_vhost, VHost, environment)
1471+
end.
1472+
1473+
default_user(Context) ->
1474+
case get_prefixed_env_var("RABBITMQ_DEFAULT_USER") of
1475+
false ->
1476+
update_context(Context, default_user, undefined, default);
1477+
Value ->
1478+
Username = list_to_binary(Value),
1479+
update_context(Context, default_user, Username, environment)
1480+
end.
1481+
1482+
default_pass(Context) ->
1483+
case get_prefixed_env_var("RABBITMQ_DEFAULT_PASS") of
1484+
false ->
1485+
update_context(Context, default_pass, undefined, default);
1486+
Value ->
1487+
Password = list_to_binary(Value),
1488+
update_context(Context, default_pass, Password, environment)
1489+
end.
1490+
1491+
%% -------------------------------------------------------------------
1492+
%%
1493+
%% RABBITMQ_ERLANG_COOKIE
1494+
%% Override the on-disk Erlang cookie.
1495+
%% Default: unset (i.e. defaults to the content of ~/.erlang.cookie)
1496+
1497+
erlang_cookie(Context) ->
1498+
case get_prefixed_env_var("RABBITMQ_ERLANG_COOKIE") of
1499+
false ->
1500+
update_context(Context, erlang_cookie, undefined, default);
1501+
Value ->
1502+
Cookie = list_to_atom(Value),
1503+
update_context(Context, erlang_cookie, Cookie, environment)
1504+
end.
1505+
14421506
%% -------------------------------------------------------------------
14431507
%% Loading of rabbitmq-env.conf.
14441508
%% -------------------------------------------------------------------

deps/rabbit_common/test/rabbit_env_SUITE.erl

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@
2828
check_RABBITMQ_ADVANCED_CONFIG_FILE/1,
2929
check_RABBITMQ_CONFIG_FILE/1,
3030
check_RABBITMQ_CONFIG_FILES/1,
31+
check_RABBITMQ_DEFAULT_PASS/1,
32+
check_RABBITMQ_DEFAULT_USER/1,
33+
check_RABBITMQ_DEFAULT_VHOST/1,
3134
check_RABBITMQ_DIST_PORT/1,
3235
check_RABBITMQ_ENABLED_PLUGINS/1,
3336
check_RABBITMQ_ENABLED_PLUGINS_FILE/1,
37+
check_RABBITMQ_ERLANG_COOKIE/1,
3438
check_RABBITMQ_FEATURE_FLAGS_FILE/1,
3539
check_RABBITMQ_KEEP_PID_FILE_ON_EXIT/1,
3640
check_RABBITMQ_LOG/1,
@@ -69,9 +73,13 @@ all() ->
6973
check_RABBITMQ_ADVANCED_CONFIG_FILE,
7074
check_RABBITMQ_CONFIG_FILE,
7175
check_RABBITMQ_CONFIG_FILES,
76+
check_RABBITMQ_DEFAULT_PASS,
77+
check_RABBITMQ_DEFAULT_USER,
78+
check_RABBITMQ_DEFAULT_VHOST,
7279
check_RABBITMQ_DIST_PORT,
7380
check_RABBITMQ_ENABLED_PLUGINS,
7481
check_RABBITMQ_ENABLED_PLUGINS_FILE,
82+
check_RABBITMQ_ERLANG_COOKIE,
7583
check_RABBITMQ_FEATURE_FLAGS_FILE,
7684
check_RABBITMQ_KEEP_PID_FILE_ON_EXIT,
7785
check_RABBITMQ_LOG,
@@ -167,8 +175,12 @@ check_default_values(_) ->
167175
amqp_ipaddr => default,
168176
amqp_tcp_port => default,
169177
conf_env_file => default,
178+
default_user => default,
179+
default_pass => default,
180+
default_vhost => default,
170181
enabled_plugins => default,
171182
enabled_plugins_file => default,
183+
erlang_cookie => default,
172184
erlang_dist_tcp_port => default,
173185
feature_flags_file => default,
174186
forced_feature_flags_on_init => RFFOrigin,
@@ -207,8 +219,12 @@ check_default_values(_) ->
207219
data_dir => "/var/lib/rabbitmq",
208220
dbg_mods => [],
209221
dbg_output => stdout,
222+
default_user => undefined,
223+
default_pass => undefined,
224+
default_vhost => undefined,
210225
enabled_plugins => undefined,
211226
enabled_plugins_file => "/etc/rabbitmq/enabled_plugins",
227+
erlang_cookie => undefined,
212228
erlang_dist_tcp_port => 25672,
213229
feature_flags_file =>
214230
"/var/lib/rabbitmq/mnesia/" ++ NodeS ++ "-feature_flags",
@@ -256,8 +272,12 @@ check_default_values(_) ->
256272
data_dir => "%APPDATA%/RabbitMQ",
257273
dbg_mods => [],
258274
dbg_output => stdout,
275+
default_user => undefined,
276+
default_pass => undefined,
277+
default_vhost => undefined,
259278
enabled_plugins => undefined,
260279
enabled_plugins_file => "%APPDATA%/RabbitMQ/enabled_plugins",
280+
erlang_cookie => undefined,
261281
erlang_dist_tcp_port => 25672,
262282
feature_flags_file =>
263283
"%APPDATA%/RabbitMQ/db/" ++ NodeS ++ "-feature_flags",
@@ -380,8 +400,12 @@ check_values_from_reachable_remote_node(Config) ->
380400
amqp_ipaddr => default,
381401
amqp_tcp_port => default,
382402
conf_env_file => default,
403+
default_user => default,
404+
default_pass => default,
405+
default_vhost => default,
383406
enabled_plugins => default,
384407
enabled_plugins_file => remote_node,
408+
erlang_cookie => default,
385409
erlang_dist_tcp_port => default,
386410
feature_flags_file => remote_node,
387411
forced_feature_flags_on_init => RFFOrigin,
@@ -420,8 +444,12 @@ check_values_from_reachable_remote_node(Config) ->
420444
data_dir => "/var/lib/rabbitmq",
421445
dbg_mods => [],
422446
dbg_output => stdout,
447+
default_user => undefined,
448+
default_pass => undefined,
449+
default_vhost => undefined,
423450
enabled_plugins => undefined,
424451
enabled_plugins_file => EnabledPluginsFile,
452+
erlang_cookie => undefined,
425453
erlang_dist_tcp_port => 25672,
426454
feature_flags_file => FeatureFlagsFile,
427455
forced_feature_flags_on_init => RFFValue,
@@ -497,8 +525,12 @@ check_values_from_offline_remote_node(_) ->
497525
amqp_ipaddr => default,
498526
amqp_tcp_port => default,
499527
conf_env_file => default,
528+
default_user => default,
529+
default_pass => default,
530+
default_vhost => default,
500531
enabled_plugins => default,
501532
enabled_plugins_file => default,
533+
erlang_cookie => default,
502534
erlang_dist_tcp_port => default,
503535
feature_flags_file => default,
504536
forced_feature_flags_on_init => RFFOrigin,
@@ -537,8 +569,12 @@ check_values_from_offline_remote_node(_) ->
537569
data_dir => "/var/lib/rabbitmq",
538570
dbg_mods => [],
539571
dbg_output => stdout,
572+
default_user => undefined,
573+
default_pass => undefined,
574+
default_vhost => undefined,
540575
enabled_plugins => undefined,
541576
enabled_plugins_file => undefined,
577+
erlang_cookie => undefined,
542578
erlang_dist_tcp_port => 25672,
543579
feature_flags_file => undefined,
544580
forced_feature_flags_on_init => RFFValue,
@@ -735,6 +771,24 @@ check_RABBITMQ_CONFIG_FILES(_) ->
735771
Value1, Value1,
736772
Value2, Value2).
737773

774+
check_RABBITMQ_DEFAULT_PASS(_) ->
775+
Value1 = random_string(),
776+
check_variable("RABBITMQ_DEFAULT_PASS",
777+
default_pass,
778+
Value1, list_to_binary(Value1)).
779+
780+
check_RABBITMQ_DEFAULT_USER(_) ->
781+
Value1 = random_string(),
782+
check_variable("RABBITMQ_DEFAULT_USER",
783+
default_user,
784+
Value1, list_to_binary(Value1)).
785+
786+
check_RABBITMQ_DEFAULT_VHOST(_) ->
787+
Value1 = random_string(),
788+
check_variable("RABBITMQ_DEFAULT_VHOST",
789+
default_vhost,
790+
Value1, list_to_binary(Value1)).
791+
738792
check_RABBITMQ_DIST_PORT(_) ->
739793
Value1 = random_int(),
740794
Value2 = random_int(),
@@ -767,6 +821,12 @@ check_RABBITMQ_ENABLED_PLUGINS_FILE(_) ->
767821
Value1, Value1,
768822
Value2, Value2).
769823

824+
check_RABBITMQ_ERLANG_COOKIE(_) ->
825+
Value1 = random_atom(),
826+
check_variable("RABBITMQ_ERLANG_COOKIE",
827+
erlang_cookie,
828+
atom_to_list(Value1), Value1).
829+
770830
check_RABBITMQ_FEATURE_FLAGS_FILE(_) ->
771831
Value1 = random_string(),
772832
check_variable("RABBITMQ_FEATURE_FLAGS_FILE",

0 commit comments

Comments
 (0)