From d9a04ff9ad5ba85571a8933b52eaf253179260bd Mon Sep 17 00:00:00 2001 From: Gabriela Moldovan Date: Fri, 11 Aug 2023 11:33:45 +0100 Subject: [PATCH 1/2] arti-client: Make from_directories() derive the keystore_dir from state_dir. Closes #988 --- crates/arti-client/src/config.rs | 40 +++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/crates/arti-client/src/config.rs b/crates/arti-client/src/config.rs index 0c01953e5..ed784339e 100644 --- a/crates/arti-client/src/config.rs +++ b/crates/arti-client/src/config.rs @@ -685,17 +685,42 @@ impl TorClientConfig { impl TorClientConfigBuilder { /// Returns a `TorClientConfigBuilder` using the specified state and cache directories. /// - /// All other configuration options are set to their defaults. + /// All other configuration options are set to their defaults, except `storage.keystore.path`, + /// which is derived from the specified state directory. pub fn from_directories(state_dir: P, cache_dir: Q) -> Self where P: AsRef, Q: AsRef, { let mut builder = Self::default(); + builder .storage() .cache_dir(CfgPath::new_literal(cache_dir.as_ref())) .state_dir(CfgPath::new_literal(state_dir.as_ref())); + + // Derive the keystore path from `state_dir` + // + // TODO HSS: perhaps the default `keystore_dir` should more generally be derived from + // `state_dir`. + // + // Note this will involve writing a custom deserializer for StorageConfig (if + // `storage.keystore.path` is missing from the config, it will need to be initialized using + // the value we deserialized for `storage.state_dir` rather than a static default value). + #[cfg(feature = "experimental-api")] + { + use tor_keymgr::config::arti::ArtiNativeKeystoreConfigBuilder; + + let mut sub_builder = ArtiNativeKeystoreConfigBuilder::default(); + let keystore_dir = format!("{}/keystore", state_dir.as_ref().display()); + sub_builder.path(CfgPath::new_literal(keystore_dir)); + // This shouldn't fail, but if it does, we use the ArtiNativeKeystoreConfig + // defaults. + let keystore = sub_builder.build().unwrap_or_default(); + + builder.storage().keystore(keystore); + }; + builder } } @@ -974,4 +999,17 @@ mod test { chk(&from_toml(test_case), *expected); } } + + #[test] + #[cfg(feature = "experimental-api")] + fn from_directories_keystore_dir() { + let builder = + TorClientConfigBuilder::from_directories("/home/bob/state", "/home/bob/cache"); + let client_cfg = builder.build().unwrap(); + + assert_eq!( + client_cfg.storage.keystore.expand_keystore_dir().unwrap(), + PathBuf::from("/home/bob/state/keystore") + ); + } } From 5c5a7bfb14e930b4a9093abf6830aa6a85cbdef1 Mon Sep 17 00:00:00 2001 From: Gabriela Moldovan Date: Mon, 14 Aug 2023 16:21:55 +0100 Subject: [PATCH 2/2] arti-client: Use Path::join instead of format!. --- crates/arti-client/src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/arti-client/src/config.rs b/crates/arti-client/src/config.rs index ed784339e..ce6fd33c7 100644 --- a/crates/arti-client/src/config.rs +++ b/crates/arti-client/src/config.rs @@ -712,7 +712,7 @@ impl TorClientConfigBuilder { use tor_keymgr::config::arti::ArtiNativeKeystoreConfigBuilder; let mut sub_builder = ArtiNativeKeystoreConfigBuilder::default(); - let keystore_dir = format!("{}/keystore", state_dir.as_ref().display()); + let keystore_dir = state_dir.as_ref().join("keystore"); sub_builder.path(CfgPath::new_literal(keystore_dir)); // This shouldn't fail, but if it does, we use the ArtiNativeKeystoreConfig // defaults.