Merge branch 'keystore-dir' into 'main'

arti-client: Make from_directories() derive the keystore_dir from state_dir.

Closes #988

See merge request tpo/core/arti!1498
This commit is contained in:
Ian Jackson 2023-08-16 14:11:01 +00:00
commit 8c73223626
1 changed files with 39 additions and 1 deletions

View File

@ -685,17 +685,42 @@ impl TorClientConfig {
impl TorClientConfigBuilder { impl TorClientConfigBuilder {
/// Returns a `TorClientConfigBuilder` using the specified state and cache directories. /// 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<P, Q>(state_dir: P, cache_dir: Q) -> Self pub fn from_directories<P, Q>(state_dir: P, cache_dir: Q) -> Self
where where
P: AsRef<Path>, P: AsRef<Path>,
Q: AsRef<Path>, Q: AsRef<Path>,
{ {
let mut builder = Self::default(); let mut builder = Self::default();
builder builder
.storage() .storage()
.cache_dir(CfgPath::new_literal(cache_dir.as_ref())) .cache_dir(CfgPath::new_literal(cache_dir.as_ref()))
.state_dir(CfgPath::new_literal(state_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 = 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.
let keystore = sub_builder.build().unwrap_or_default();
builder.storage().keystore(keystore);
};
builder builder
} }
} }
@ -974,4 +999,17 @@ mod test {
chk(&from_toml(test_case), *expected); 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")
);
}
} }