Skip to content

Commit 9983d12

Browse files
MB-61292: [config-remap] Add support for encrypted...
... config.dat and chronicle files Change-Id: I03bc7ac3f84c5d387adec106879b45bbb4474733 Reviewed-on: https://review.couchbase.org/c/ns_server/+/217978 Tested-by: Timofey Barmin <[email protected]> Well-Formed: Build Bot <[email protected]> Reviewed-by: Navdeep S Boparai <[email protected]>
1 parent 021bb97 commit 9983d12

File tree

3 files changed

+50
-45
lines changed

3 files changed

+50
-45
lines changed

apps/config_remap/src/config_remap.erl

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,29 @@ read_term_from_file(Path) ->
7171
{ok, Data} = file:read_file(Path),
7272
erlang:binary_to_term(Data).
7373

74-
read_ns_config_from_file(Path) ->
75-
[Config | _] = read_term_from_file(Path),
76-
Config.
74+
get_dek_snapshot(DekKind) ->
75+
%% In order to make path_config work
76+
application:load(ns_server),
77+
case cb_deks_raw_utils:bootstrap_get_deks(DekKind, #{}) of
78+
{ok, Snapshot} -> Snapshot;
79+
{error, Reason} ->
80+
erlang:exit("Failed to read ~p encryption keys. Got error ~p",
81+
[DekKind, Reason])
82+
end.
83+
84+
read_ns_config_from_file(Path, DekSnapshot) ->
85+
case cb_crypto:read_file(Path, DekSnapshot) of
86+
{ResType, Data} when ResType == decrypted; ResType == raw ->
87+
[Config | _] = erlang:binary_to_term(Data),
88+
Config;
89+
{error, Reason} ->
90+
erlang:exit("Failed to read ~s. Got error ~p", [Path, Reason])
91+
end.
92+
93+
write_ns_config(Path, NewCfg, DekSnapshot) ->
94+
ok = filelib:ensure_dir(Path),
95+
Data = term_to_binary([NewCfg]),
96+
ok = cb_crypto:atomic_write_file(Path, Data, DekSnapshot).
7797

7898
modify_ns_config_tuples(Config, Args) ->
7999
rewrite_term(Config, "ns_config", Args).
@@ -83,8 +103,8 @@ get_ns_config_path(#{?INITARGS_DATA_DIR := Path}) ->
83103

84104
rewrite_ns_config(#{output_path := OutputPath} = Args) ->
85105
?log_info("Rewriting ns_config"),
86-
87-
OriginalCfg = read_ns_config_from_file(get_ns_config_path(Args)),
106+
Deks = get_dek_snapshot(configDek),
107+
OriginalCfg = read_ns_config_from_file(get_ns_config_path(Args), Deks),
88108
NewCfg = functools:chain(OriginalCfg,
89109
[modify_ns_config_tuples(_, Args),
90110
maybe_rewrite_cookie(_, Args),
@@ -93,8 +113,7 @@ rewrite_ns_config(#{output_path := OutputPath} = Args) ->
93113
maybe_disable_auto_failover(_, Args)]),
94114

95115
NsConfigPath = filename:join(OutputPath, ?NS_CONFIG_NAME),
96-
ok = filelib:ensure_dir(NsConfigPath),
97-
ok = file:write_file(NsConfigPath, term_to_binary([NewCfg])).
116+
write_ns_config(NsConfigPath, NewCfg, Deks).
98117

99118
maybe_disable_auto_failover(Cfg, Args) ->
100119
case maps:find(disable_auto_failover, Args) of
@@ -163,33 +182,33 @@ maybe_rewrite_cookie(Cfg, Args) ->
163182
_ -> Cfg
164183
end.
165184

166-
rewrite_cookie(Cfg, #{go_secrets_pid := SecretsPid,
167-
node_map := NodeMap}) ->
185+
rewrite_cookie(Cfg, #{node_map := NodeMap}) ->
168186
?log_info("Rewriting ns_config cookie"),
169187
lists:map(
170-
fun({otp, [VClock, {cookie, {encrypted, OldCookie}}]}) ->
171-
{ok, OldUnencryptedCookie} =
172-
cb_gosecrets_runner:decrypt(SecretsPid, OldCookie),
173-
NewCookie =
174-
term_to_binary(generate_cookie(OldUnencryptedCookie,
175-
NodeMap)),
176-
177-
{ok, EncryptedCookie} =
178-
cb_gosecrets_runner:encrypt(SecretsPid,NewCookie),
179-
188+
fun({otp, [VClock, {cookie, OldCookie}]}) ->
189+
NewCookie = generate_cookie(OldCookie, NodeMap),
180190
?log_debug("Replacing encrypted cookie ~p with ~p",
181-
[OldCookie, EncryptedCookie]),
182-
{otp, [VClock, {cookie, {encrypted, EncryptedCookie}}]};
191+
[ns_cookie_manager:sanitize_cookie(OldCookie),
192+
ns_cookie_manager:sanitize_cookie(NewCookie)]),
193+
{otp, [VClock, {cookie, NewCookie}]};
183194
(V) -> V
184195
end, Cfg).
185196

186197
rewrite_chronicle(#{?INITARGS_DATA_DIR := InputDir,
187198
output_path := OutputDir} = Args) ->
188199
?log_info("Rewriting chronicle"),
200+
Deks = get_dek_snapshot(chronicleDek),
189201

190202
%% Required to re-use chronicle snapshot storage write fun
191203
ChronicleEnvDataDir = filename:join([OutputDir, ?CONFIG_DIR]),
192204
?log_debug("Rewriting chronicle files to ~p", [ChronicleEnvDataDir]),
205+
206+
chronicle_local:set_chronicle_deks_snapshot(Deks),
207+
application:set_env(chronicle, encrypt_function,
208+
{chronicle_local, encrypt_data}),
209+
application:set_env(chronicle, decrypt_function,
210+
{chronicle_local, decrypt_data}),
211+
193212
application:set_env(chronicle, data_dir, ChronicleEnvDataDir),
194213
application:set_env(chronicle, setup_logger_filter, false),
195214
ok = chronicle_env:setup(),
@@ -511,22 +530,6 @@ maybe_tweak_log_verbosity(#{log_level := Level}) ->
511530
ok = ale:set_loglevel(?NS_SERVER_LOGGER, Level),
512531
ok = ale:set_sink_loglevel(?NS_SERVER_LOGGER, stderr, Level).
513532

514-
start_gosecrets(#{?INITARGS_DATA_DIR := InputPath}) ->
515-
CfgPath = filename:join(InputPath, "config/gosecrets.cfg"),
516-
517-
?log_debug("Spawning gosecrets with cfg path ~p~n", [CfgPath]),
518-
519-
%% We are assuming here that the gosecrets.cfg exists, which requires that
520-
%% the installation is EE.
521-
{ok, Pid} = cb_gosecrets_runner:start_link(CfgPath),
522-
?log_debug("Gosecrets loop started with pid = ~p", [Pid]),
523-
Pid.
524-
525-
init_gosecrets(Args) ->
526-
?log_info("Initializing gosecrets"),
527-
Pid = start_gosecrets(Args),
528-
Args#{go_secrets_pid => Pid}.
529-
530533
usage(Args) ->
531534
?log_error("Invalid args specified ~p", [Args]),
532535
erlang:halt(1).
@@ -586,10 +589,6 @@ load_initargs(#{initargs_path := Path} = Args) ->
586589
LogDir = misc:expect_prop_value(?INITARGS_LOG_DIR, NsServerProps),
587590
DataDir = misc:expect_prop_value(?INITARGS_DATA_DIR, NsServerProps),
588591

589-
%% Required for gosecrets/cb_gosecrets_runner which uses path_config
590-
application:set_env(ns_server, ?INITARGS_DATA_DIR, DataDir),
591-
application:set_env(ns_server, ?INITARGS_BIN_DIR, BinDir),
592-
593592
Args#{?INITARGS_BIN_DIR => BinDir,
594593
?INITARGS_LOG_DIR => LogDir,
595594
?INITARGS_DATA_DIR => DataDir}.
@@ -619,10 +618,9 @@ setup(Args) ->
619618
setup_file_logging(ArgsMap1),
620619

621620
ArgsMap2 = maybe_derive_output_path(ArgsMap1),
622-
ArgsMap3 = init_gosecrets(ArgsMap2),
623621

624-
?log_debug("Final args map ~p", [ArgsMap3]),
625-
ArgsMap3.
622+
?log_debug("Final args map ~p", [ArgsMap2]),
623+
ArgsMap2.
626624

627625
main(CmdLineArgs) ->
628626
Args = setup(CmdLineArgs),

apps/ns_server/src/cb_crypto.erl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,9 @@ is_file_encrypted(Path) ->
193193
file:close(File)
194194
end.
195195

196-
-spec read_file(string(), cb_deks:dek_kind() | fun(() -> fetch_deks_res())) ->
196+
-spec read_file(string(),
197+
cb_deks:dek_kind() | #dek_snapshot{} |
198+
fun(() -> fetch_deks_res())) ->
197199
{decrypted, binary()} | {raw, binary()} | {error, term()}.
198200
read_file(Path, GetDekSnapshotFun) when is_function(GetDekSnapshotFun, 0) ->
199201
maybe
@@ -209,6 +211,8 @@ read_file(Path, GetDekSnapshotFun) when is_function(GetDekSnapshotFun, 0) ->
209211
Error
210212
end
211213
end;
214+
read_file(Path, #dek_snapshot{} = DekSnapshot) ->
215+
read_file(Path, fun () -> {ok, DekSnapshot} end);
212216
read_file(Path, DekKind) ->
213217
GetSnapshotFun = fun () -> fetch_deks_snapshot(DekKind) end,
214218
read_file(Path, GetSnapshotFun).

apps/ns_server/src/chronicle_local.erl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
-export([log/4, report_stats/1, encrypt_data/1, decrypt_data/1,
3636
external_decrypt/1]).
3737

38+
%% used by config_remap
39+
-export([set_chronicle_deks_snapshot/1]).
40+
3841
%% exported for log formatting
3942
-export([format_msg/2, format_time/1]).
4043

0 commit comments

Comments
 (0)