Skip to content

Commit

Permalink
fix(config): Fixed config and added unit test to verify correct funct…
Browse files Browse the repository at this point in the history
…ionality
  • Loading branch information
schoenenberg committed Aug 21, 2023
1 parent 0a474c0 commit 76765e6
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 28 deletions.
63 changes: 52 additions & 11 deletions clearing-house-app/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions clearing-house-app/logging-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ edition = "2021"
[dependencies]
biscuit = "0.6.0"
core-lib = { path = "../core-lib" }
chrono = { version = "0.4", features = ["serde"] }
chrono = { version = "0.4.26", features = ["serde", "clock", "std"], default-features = false }
mongodb ="2"
percent-encoding = "2.1.0"
rocket = { version = "0.5.0-rc.1", features = ["json"] }
serde = { version = "1", features = ["derive"] }
serde = { version = "= 1.0.171", features = ["derive"] }
serde_json = "1"
anyhow = "1"
hex = "0.4.3"
Expand All @@ -35,3 +35,9 @@ error-chain = "0.12.4"
num-bigint = "0.4.3"
ring = "0.16.20"
openssh-keys = "0.6.2"

[dev-dependencies]
# Controlling execution of unit test cases, which could interfere with each other
serial_test = "2.0.0"
# Tempfile creation for testing
tempfile = "3.8.0"
95 changes: 86 additions & 9 deletions clearing-house-app/logging-service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub(crate) struct CHConfig {
pub(crate) log_level: Option<LogLevel>,
}

#[derive(Debug, serde::Deserialize)]
#[derive(Debug, PartialEq, serde::Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub(crate) enum LogLevel {
Trace,
Expand Down Expand Up @@ -42,15 +42,32 @@ impl ToString for LogLevel {
}
}

/// Read configuration from `config.toml` and environment variables
pub(crate) fn read_config() -> CHConfig {
let conf = config::Config::builder()
.add_source(config::File::with_name("config.toml"))
.add_source(config::Environment::with_prefix("CH_APP_"))
.build()
.expect("Failure to read configuration! Exiting...");
/// Read configuration from `config.toml` and environment variables. `config_file_override` can be
/// used to override the default config file, mainly for testing purposes.
pub(crate) fn read_config(config_file_override: Option<&std::path::Path>) -> CHConfig {
// Create config builder
let mut conf_builder = config::Config::builder();

// Override config file override path
conf_builder = if let Some(config_file) = config_file_override {
conf_builder
.add_source(config::File::from(config_file))
} else {
conf_builder
.add_source(config::File::with_name("config.toml"))
};

// Add environment variables and finish
conf_builder = conf_builder
.add_source(
config::Environment::with_prefix("CH_APP")
.prefix_separator("_")
);

conf.try_deserialize::<CHConfig>()
conf_builder
.build()
.expect("Failure to read configuration! Exiting...")
.try_deserialize::<CHConfig>()
.expect("Failure to parse configuration! Exiting...")
}

Expand All @@ -66,4 +83,64 @@ pub(crate) fn configure_logging(log_level: Option<LogLevel>) {
tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.init();
}

#[cfg(test)]
mod test {
use serial_test::serial;
#[test]
#[serial]
fn test_read_config_from_env() {
std::env::set_var("CH_APP_PROCESS_DATABASE_URL", "mongodb://localhost:27117");
std::env::set_var("CH_APP_KEYRING_DATABASE_URL", "mongodb://localhost:27118");
std::env::set_var("CH_APP_DOCUMENT_DATABASE_URL", "mongodb://localhost:27119");
std::env::set_var("CH_APP_CLEAR_DB", "true");
std::env::set_var("CH_APP_LOG_LEVEL", "INFO");

let conf = super::read_config(None);
assert_eq!(conf.process_database_url, "mongodb://localhost:27117");
assert_eq!(conf.keyring_database_url, "mongodb://localhost:27118");
assert_eq!(conf.document_database_url, "mongodb://localhost:27119");
assert_eq!(conf.clear_db, true);
assert_eq!(conf.log_level, Some(super::LogLevel::Info));

// Cleanup
std::env::remove_var("CH_APP_PROCESS_DATABASE_URL");
std::env::remove_var("CH_APP_KEYRING_DATABASE_URL");
std::env::remove_var("CH_APP_DOCUMENT_DATABASE_URL");
std::env::remove_var("CH_APP_CLEAR_DB");
std::env::remove_var("CH_APP_LOG_LEVEL");
}

#[test]
#[serial]
fn test_read_config_from_toml() {
// Create tempfile
let file = tempfile::Builder::new()
.suffix(".toml")
.tempfile()
.unwrap();

// Write config to file
let toml =
r#"process_database_url = "mongodb://localhost:27019"
keyring_database_url = "mongodb://localhost:27020"
document_database_url = "mongodb://localhost:27017"
clear_db = true
log_level = "ERROR"
"#;

// Write to file
std::fs::write(file.path(), toml).expect("Failure to write config file!");

// Read config
let conf = super::read_config(Some(file.path()));

// Test
assert_eq!(conf.process_database_url, "mongodb://localhost:27019");
assert_eq!(conf.keyring_database_url, "mongodb://localhost:27020");
assert_eq!(conf.document_database_url, "mongodb://localhost:27017");
assert_eq!(conf.clear_db, true);
assert_eq!(conf.log_level, Some(super::LogLevel::Error));
}
}
2 changes: 1 addition & 1 deletion clearing-house-app/logging-service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn add_signing_key() -> AdHoc {
#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
// Read configuration
let conf = config::read_config();
let conf = config::read_config(None);
config::configure_logging(conf.log_level);

let process_store =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,16 @@ impl DocumentService {
};

debug!("start encryption");
let mut enc_doc;
match doc.encrypt(keys) {
let mut enc_doc = match doc.encrypt(keys) {
Ok(ct) => {
debug!("got ct");
enc_doc = ct
Ok(ct)
}
Err(e) => {
error!("Error while encrypting: {:?}", e);
return Err(anyhow!("Error while encrypting!")); // InternalError
Err(anyhow!("Error while encrypting!")) // InternalError
}
};
}?;

// chain the document to previous documents
debug!("add the chain hash...");
Expand Down

0 comments on commit 76765e6

Please sign in to comment.