@@ -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
7898modify_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
84104rewrite_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
99118maybe_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
186197rewrite_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-
530533usage (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
627625main (CmdLineArgs ) ->
628626 Args = setup (CmdLineArgs ),
0 commit comments