diff --git a/keyserver/addons/rust-node-addon/src/identity_client/config.rs b/keyserver/addons/rust-node-addon/src/identity_client/config.rs new file mode 100644 --- /dev/null +++ b/keyserver/addons/rust-node-addon/src/identity_client/config.rs @@ -0,0 +1,77 @@ +use super::IdentityServiceConfig; +use serde::Deserialize; +use serde_json; +use std::env; +use std::fs; +use std::path::{Path, PathBuf}; +use tracing::warn; + +pub(super) fn get_identity_service_config( +) -> Result> { + const IDENTITY_SERVICE_CONFIG_NAME: ConfigName = ConfigName { + folder: ConfigFolder::Secrets, + name: "identity_service_config", + }; + let config = get_config(&IDENTITY_SERVICE_CONFIG_NAME)?; + Ok(config) +} + +#[derive(Debug)] +enum ConfigFolder { + Secrets, + #[allow(dead_code)] + Facts, +} + +impl ConfigFolder { + fn as_str(&self) -> &'static str { + match self { + ConfigFolder::Secrets => "secrets", + ConfigFolder::Facts => "facts", + } + } +} + +struct ConfigName { + folder: ConfigFolder, + name: &'static str, +} + +impl ConfigName { + fn get_key_for_config_name(&self) -> String { + format!("COMM_JSONCONFIG_{}_{}", self.folder.as_str(), self.name) + } + + fn get_path_for_config_name(&self) -> PathBuf { + Path::new(self.folder.as_str()).join(format!("{}.json", self.name)) + } +} + +fn get_config Deserialize<'de>>( + config_name: &ConfigName, +) -> Result> { + let key = config_name.get_key_for_config_name(); + + match env::var(&key) { + Ok(env_value) => match serde_json::from_str::(&env_value) { + Ok(config) => return Ok(config), + Err(e) => { + warn!("Failed to deserialize env value '{}': {}", env_value, e); + } + }, + Err(e) => { + warn!("Failed to read config from env var '{}': {}", key, e); + } + } + + let path = config_name.get_path_for_config_name(); + let file_content = fs::read_to_string(&path).map_err(|e| { + warn!("Failed to read config file '{}': {}", path.display(), e); + e + })?; + + serde_json::from_str::(&file_content).map_err(|e| { + warn!("Failed to deserialize file content: {}", e); + e.into() + }) +} diff --git a/keyserver/addons/rust-node-addon/src/identity_client/mod.rs b/keyserver/addons/rust-node-addon/src/identity_client/mod.rs --- a/keyserver/addons/rust-node-addon/src/identity_client/mod.rs +++ b/keyserver/addons/rust-node-addon/src/identity_client/mod.rs @@ -1,4 +1,5 @@ pub mod add_reserved_usernames; +pub mod config; pub mod get_inbound_keys_for_user; pub mod login; pub mod prekey; @@ -12,6 +13,8 @@ PreKey, RegistrationFinishRequest, RegistrationStartRequest, RemoveReservedUsernameRequest, }; +use config::get_identity_service_config; +use generated::CODE_VERSION; use grpc_clients::identity::authenticated::ChainedInterceptedAuthClient; use grpc_clients::identity::protos::authenticated::{ InboundKeyInfo, UploadOneTimeKeysRequest, @@ -35,8 +38,7 @@ include!(concat!(env!("OUT_DIR"), "/version.rs")); } -pub use generated::CODE_VERSION; -pub const DEVICE_TYPE: &str = "keyserver"; +const DEVICE_TYPE: &str = "keyserver"; const ENV_NODE_ENV: &str = "NODE_ENV"; const ENV_DEVELOPMENT: &str = "development"; @@ -51,12 +53,8 @@ tracing::subscriber::set_global_default(subscriber) .expect("Unable to configure tracing"); - let config_json_string = - var("COMM_JSONCONFIG_secrets_identity_service_config"); - match config_json_string { - Ok(json) => serde_json::from_str(&json).unwrap(), - Err(_) => IdentityServiceConfig::default(), - } + get_identity_service_config() + .unwrap_or_else(|_| IdentityServiceConfig::default()) }; }