-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathunit_config_value_encryption_SUITE.erl
More file actions
275 lines (248 loc) · 10.7 KB
/
unit_config_value_encryption_SUITE.erl
File metadata and controls
275 lines (248 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
%% This Source Code Form is subject to the terms of the Mozilla Public
%% License, v. 2.0. If a copy of the MPL was not distributed with this
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
%%
%% Copyright (c) 2007-2026 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
%%
-module(unit_config_value_encryption_SUITE).
-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl").
-compile(export_all).
%% This cipher is listed as supported, but doesn't actually work.
%% OTP bug: https://bugs.erlang.org/browse/ERL-1478
-define(SKIPPED_CIPHERS, [aes_ige256]).
-define(SKIPPED_HASHES, [shake128, shake256]).
all() ->
[
{group, sequential_tests}
].
groups() ->
[
{sequential_tests, [], [
decrypt_start_app,
decrypt_start_app_file,
decrypt_start_app_undefined,
decrypt_start_app_wrong_passphrase,
decrypt_config,
decrypt_config_map,
rabbitmqctl_encode
]}
].
init_per_suite(Config) ->
application:set_env(rabbit, feature_flags_file, "", [{persistent, true}]),
ThisNode = node(),
ok = rabbit_feature_flags:override_nodes([ThisNode]),
ok = rabbit_feature_flags:override_running_nodes([ThisNode]),
{ok, _Pid} = rabbit_ff_controller:start(),
Config.
end_per_suite(_Config) ->
ok.
init_per_testcase(_Testcase, Config) ->
Config.
end_per_testcase(_TC, _Config) ->
ok.
%% -------------------------------------------------------------------
%% Test Cases
%% -------------------------------------------------------------------
decrypt_config(_Config) ->
%% Take all available block ciphers.
Hashes = rabbit_pbe:supported_hashes() -- ?SKIPPED_HASHES,
Ciphers = rabbit_pbe:supported_ciphers() -- ?SKIPPED_CIPHERS,
Iterations = [1, 10, 100, 1000],
%% Loop through all hashes, ciphers and iterations.
_ = [begin
PassPhrase = crypto:strong_rand_bytes(16),
do_decrypt_config({C, H, I, PassPhrase})
end || H <- Hashes, C <- Ciphers, I <- Iterations],
ok.
do_decrypt_config(Algo = {C, H, I, P}) ->
case application:load(rabbit) of
ok -> ok;
{error, {already_loaded, rabbit}} -> ok
end,
RabbitConfig = application:get_all_env(rabbit),
%% Encrypt a few values in configuration.
%% Common cases.
_ = [encrypt_value(Key, Algo) || Key <- [
tcp_listeners,
num_tcp_acceptors,
ssl_options,
vm_memory_high_watermark,
default_pass,
default_permissions,
cluster_nodes,
auth_mechanisms,
msg_store_credit_disc_bound]],
%% Special case: encrypt a value in a list.
{ok, [LoopbackUser]} = application:get_env(rabbit, loopback_users),
{encrypted, EncLoopbackUser} = rabbit_pbe:encrypt_term(C, H, I, P, LoopbackUser),
application:set_env(rabbit, loopback_users, [{encrypted, EncLoopbackUser}]),
%% Special case: encrypt a value in a key/value list.
{ok, TCPOpts} = application:get_env(rabbit, tcp_listen_options),
{_, Backlog} = lists:keyfind(backlog, 1, TCPOpts),
{_, Linger} = lists:keyfind(linger, 1, TCPOpts),
{encrypted, EncBacklog} = rabbit_pbe:encrypt_term(C, H, I, P, Backlog),
{encrypted, EncLinger} = rabbit_pbe:encrypt_term(C, H, I, P, Linger),
TCPOpts1 = lists:keyreplace(backlog, 1, TCPOpts, {backlog, {encrypted, EncBacklog}}),
TCPOpts2 = lists:keyreplace(linger, 1, TCPOpts1, {linger, {encrypted, EncLinger}}),
application:set_env(rabbit, tcp_listen_options, TCPOpts2),
%% Decrypt configuration.
rabbit_prelaunch_conf:decrypt_config([rabbit], Algo),
%% Check that configuration was decrypted properly.
RabbitConfig = application:get_all_env(rabbit),
ok = application:unload(rabbit),
ok.
decrypt_config_map(_Config) ->
Hashes = rabbit_pbe:supported_hashes() -- ?SKIPPED_HASHES,
Ciphers = rabbit_pbe:supported_ciphers() -- ?SKIPPED_CIPHERS,
Iterations = [1, 100, 1000],
_ = [begin
PassPhrase = crypto:strong_rand_bytes(16),
do_decrypt_config_map({C, H, I, PassPhrase})
end || H <- Hashes, C <- Ciphers, I <- Iterations],
ok.
%% Verifies that encrypted values nested inside maps are decrypted,
%% as required by e.g. rabbitmq_management.oauth_resource_servers.
do_decrypt_config_map({C, H, I, P} = Algo) ->
case application:load(rabbit) of
ok -> ok;
{error, {already_loaded, rabbit}} -> ok
end,
Secret = <<"test_oauth_secret">>,
{encrypted, EncSecret} = rabbit_pbe:encrypt_term(C, H, I, P, Secret),
application:set_env(rabbit, test_map_decrypt,
#{<<"server">> => [{oauth_client_secret, {encrypted, EncSecret}}]}),
rabbit_prelaunch_conf:decrypt_config([rabbit], Algo),
{ok, Decrypted} = application:get_env(rabbit, test_map_decrypt),
Secret = proplists:get_value(oauth_client_secret, maps:get(<<"server">>, Decrypted)),
application:unset_env(rabbit, test_map_decrypt),
ok.
encrypt_value(Key, {C, H, I, P}) ->
{ok, Value} = application:get_env(rabbit, Key),
{encrypted, EncValue} = rabbit_pbe:encrypt_term(C, H, I, P, Value),
application:set_env(rabbit, Key, {encrypted, EncValue}).
decrypt_start_app(Config) ->
do_decrypt_start_app(Config, "hello").
decrypt_start_app_file(Config) ->
do_decrypt_start_app(Config, {file, ?config(data_dir, Config) ++ "/rabbit_shovel_test.passphrase"}).
do_decrypt_start_app(Config, Passphrase) ->
%% Configure rabbit for decrypting configuration.
application:set_env(rabbit, config_entry_decoder, [
{cipher, aes_256_cbc},
{hash, sha512},
{iterations, 1000},
{passphrase, Passphrase}
], [{persistent, true}]),
%% Add the path to our test application.
code:add_path(?config(data_dir, Config) ++ "/lib/rabbit_shovel_test/ebin"),
%% Attempt to start our test application.
%%
%% We expect a failure *after* the decrypting has been done.
try
rabbit:start_apps([rabbit_shovel_test], #{rabbit => temporary})
catch _:Err:Stacktrace ->
ct:pal("start_apps failed with ~tp~n~tp", [Err, Stacktrace]),
ok
end,
%% Check if the values have been decrypted.
{ok, Shovels} = application:get_env(rabbit_shovel_test, shovels),
{_, FirstShovel} = lists:keyfind(my_first_shovel, 1, Shovels),
{_, Sources} = lists:keyfind(sources, 1, FirstShovel),
{_, Brokers} = lists:keyfind(brokers, 1, Sources),
["amqp://fred:secret@host1.domain/my_vhost",
"amqp://john:secret@host2.domain/my_vhost"] = Brokers,
ok.
decrypt_start_app_undefined(Config) ->
%% Configure rabbit for decrypting configuration.
application:set_env(rabbit, config_entry_decoder, [
{cipher, aes_cbc256},
{hash, sha512},
{iterations, 1000}
%% No passphrase option!
], [{persistent, true}]),
%% Add the path to our test application.
code:add_path(?config(data_dir, Config) ++ "/lib/rabbit_shovel_test/ebin"),
%% Attempt to start our test application.
%%
%% We expect a failure during decryption because the passphrase is missing.
try
rabbit:start_apps([rabbit_shovel_test], #{rabbit => temporary})
catch
throw:{bad_config_entry_decoder, missing_passphrase} -> ok;
_:Exception -> exit({unexpected_exception, Exception})
end.
decrypt_start_app_wrong_passphrase(Config) ->
%% Configure rabbit for decrypting configuration.
application:set_env(rabbit, config_entry_decoder, [
{cipher, aes_cbc256},
{hash, sha512},
{iterations, 1000},
{passphrase, "wrong passphrase"}
], [{persistent, true}]),
%% Add the path to our test application.
code:add_path(?config(data_dir, Config) ++ "/lib/rabbit_shovel_test/ebin"),
%% Attempt to start our test application.
%%
%% We expect a failure during decryption because the passphrase is wrong.
try
rabbit:start_apps([rabbit_shovel_test], #{rabbit => temporary})
catch
throw:{config_decryption_error, _, _} -> ok;
_:Exception -> exit({unexpected_exception, Exception})
end.
rabbitmqctl_encode(_Config) ->
% list ciphers and hashes
{ok, _} = rabbit_control_pbe:list_ciphers(),
{ok, _} = rabbit_control_pbe:list_hashes(),
% incorrect ciphers, hashes and iteration number
{error, _} = rabbit_control_pbe:encode(funny_cipher, undefined, undefined, undefined),
{error, _} = rabbit_control_pbe:encode(undefined, funny_hash, undefined, undefined),
{error, _} = rabbit_control_pbe:encode(undefined, undefined, -1, undefined),
{error, _} = rabbit_control_pbe:encode(undefined, undefined, 0, undefined),
% incorrect number of arguments
{error, _} = rabbit_control_pbe:encode(
rabbit_pbe:default_cipher(), rabbit_pbe:default_hash(), rabbit_pbe:default_iterations(),
[]
),
{error, _} = rabbit_control_pbe:encode(
rabbit_pbe:default_cipher(), rabbit_pbe:default_hash(), rabbit_pbe:default_iterations(),
[undefined]
),
{error, _} = rabbit_control_pbe:encode(
rabbit_pbe:default_cipher(), rabbit_pbe:default_hash(), rabbit_pbe:default_iterations(),
[undefined, undefined, undefined]
),
% encrypt/decrypt
% string
rabbitmqctl_encode_encrypt_decrypt("foobar"),
% binary
rabbitmqctl_encode_encrypt_decrypt("<<\"foobar\">>"),
% tuple
rabbitmqctl_encode_encrypt_decrypt("{password,<<\"secret\">>}"),
ok.
rabbitmqctl_encode_encrypt_decrypt(Secret) ->
PassPhrase = "passphrase",
{ok, Output} = rabbit_control_pbe:encode(
rabbit_pbe:default_cipher(), rabbit_pbe:default_hash(), rabbit_pbe:default_iterations(),
[Secret, PassPhrase]
),
{encrypted, Encrypted} = rabbit_control_pbe:evaluate_input_as_term(lists:flatten(Output)),
{ok, Result} = rabbit_control_pbe:decode(
rabbit_pbe:default_cipher(), rabbit_pbe:default_hash(), rabbit_pbe:default_iterations(),
[lists:flatten(io_lib:format("~tp", [Encrypted])), PassPhrase]
),
Secret = lists:flatten(Result),
% decrypt with {encrypted, ...} form as input
{ok, Result} = rabbit_control_pbe:decode(
rabbit_pbe:default_cipher(), rabbit_pbe:default_hash(), rabbit_pbe:default_iterations(),
[lists:flatten(io_lib:format("~tp", [{encrypted, Encrypted}])), PassPhrase]
),
% wrong passphrase
{error, _} = rabbit_control_pbe:decode(
rabbit_pbe:default_cipher(), rabbit_pbe:default_hash(), rabbit_pbe:default_iterations(),
[lists:flatten(io_lib:format("~tp", [Encrypted])), PassPhrase ++ " "]
),
{error, _} = rabbit_control_pbe:decode(
rabbit_pbe:default_cipher(), rabbit_pbe:default_hash(), rabbit_pbe:default_iterations(),
[lists:flatten(io_lib:format("~tp", [{encrypted, Encrypted}])), PassPhrase ++ " "]
).