diff --git a/crates/notary/server/Cargo.toml b/crates/notary/server/Cargo.toml index 856177014..e29b9aa5f 100644 --- a/crates/notary/server/Cargo.toml +++ b/crates/notary/server/Cargo.toml @@ -15,6 +15,7 @@ axum-core = { version = "0.4" } axum-macros = { version = "0.4" } base64 = { version = "0.21" } chrono = { version = "0.4" } +config = { version = "0.14", features = ["yaml"] } csv = { version = "1.3" } eyre = { version = "0.6" } futures = { workspace = true } diff --git a/crates/notary/server/config/config.yaml b/crates/notary/server/config/config.yaml index 18336dc56..354834aa8 100644 --- a/crates/notary/server/config/config.yaml +++ b/crates/notary/server/config/config.yaml @@ -17,16 +17,16 @@ notarization: tls: enabled: true - private-key-pem-path: "./fixture/tls/notary.key" - certificate-pem-path: "./fixture/tls/notary.crt" + private-key-pem-path: "../fixture/tls/notary.key" + certificate-pem-path: "../fixture/tls/notary.crt" notary-key: - private-key-pem-path: "./fixture/notary/notary.key" - public-key-pem-path: "./fixture/notary/notary.pub" + private-key-pem-path: "../fixture/notary/notary.key" + public-key-pem-path: "../fixture/notary/notary.pub" logging: - level: DEBUG + level: "DEBUG" authorization: enabled: false - whitelist-csv-path: "./fixture/auth/whitelist.csv" + whitelist-csv-path: "../fixture/auth/whitelist.csv" \ No newline at end of file diff --git a/crates/notary/server/src/domain/cli.rs b/crates/notary/server/src/domain/cli.rs index 83a03e98d..d4d98a1c6 100644 --- a/crates/notary/server/src/domain/cli.rs +++ b/crates/notary/server/src/domain/cli.rs @@ -2,9 +2,18 @@ use structopt::StructOpt; /// Fields loaded from the command line when launching this server. #[derive(Clone, Debug, StructOpt)] -#[structopt(name = "Notary Server")] +#[structopt(name = "notary-server")] pub struct CliFields { /// Configuration file location #[structopt(long, default_value = "./config/config.yaml")] pub config_file: String, + + #[structopt(long)] + pub port: Option, + + #[structopt(long)] + pub tls_enabled: Option, + + #[structopt(long)] + pub log_level: Option, } diff --git a/crates/notary/server/src/lib.rs b/crates/notary/server/src/lib.rs index 9353150db..140a1cac4 100644 --- a/crates/notary/server/src/lib.rs +++ b/crates/notary/server/src/lib.rs @@ -5,6 +5,7 @@ mod middleware; mod server; mod server_tracing; mod service; +mod settings; mod signing; mod util; @@ -19,4 +20,5 @@ pub use domain::{ pub use error::NotaryServerError; pub use server::{read_pem_file, run_server}; pub use server_tracing::init_tracing; +pub use settings::Settings; pub use util::parse_config_file; diff --git a/crates/notary/server/src/main.rs b/crates/notary/server/src/main.rs index 58d8ed3a8..a694b885c 100644 --- a/crates/notary/server/src/main.rs +++ b/crates/notary/server/src/main.rs @@ -1,25 +1,24 @@ use eyre::{eyre, Result}; use structopt::StructOpt; -use tracing::debug; - use notary_server::{ - init_tracing, parse_config_file, run_server, CliFields, NotaryServerError, - NotaryServerProperties, + init_tracing, run_server, CliFields, NotaryServerError, + Settings }; #[tokio::main] async fn main() -> Result<(), NotaryServerError> { - // Load command line arguments which contains the config file location + // Load command line arguments let cli_fields: CliFields = CliFields::from_args(); - let config: NotaryServerProperties = parse_config_file(&cli_fields.config_file)?; - // Set up tracing for logging - init_tracing(&config).map_err(|err| eyre!("Failed to set up tracing: {err}"))?; + let settings = Settings::new(&cli_fields) + .map_err(|err| eyre!("Failed to load settings: {}", err))?; - debug!(?config, "Server config loaded"); + // Set up tracing for logging + init_tracing(&settings.config) + .map_err(|err| eyre!("Failed to set up tracing: {err}"))?; // Run the server - run_server(&config).await?; + run_server(&settings.config).await?; Ok(()) -} +} \ No newline at end of file diff --git a/crates/notary/server/src/settings.rs b/crates/notary/server/src/settings.rs new file mode 100644 index 000000000..3d8b8f676 --- /dev/null +++ b/crates/notary/server/src/settings.rs @@ -0,0 +1,41 @@ +use config::{Config, ConfigError, Environment, File}; +use std::env; +use crate::{ CliFields, NotaryServerProperties }; +use serde::Deserialize; + +#[derive(Debug, Deserialize)] +pub struct Settings { + #[serde(flatten)] + pub config: NotaryServerProperties, +} + +impl Settings { + pub fn new(cli_fields: &CliFields) -> Result { + let mut base_path = env::current_dir().expect("Failed to determine the current directory"); + base_path.pop(); + let configuration_path = base_path.join("config").join("config.yaml"); + + let mut builder = Config::builder() + // Load base configuration + .add_source(File::from(configuration_path)) + // Add in settings from environment variables (with a prefix of NOTARY_SERVER and '__' as separator) + .add_source(Environment::with_prefix("NOTARY_SERVER").separator("__")); + + // Apply CLI argument overrides + if let Some(port) = cli_fields.port { + builder = builder.set_override("server.port", port)?; + } + if let Some(tls_enabled) = cli_fields.tls_enabled { + builder = builder.set_override("tls.enabled", tls_enabled)?; + } + if let Some(log_level) = &cli_fields.log_level { + builder = builder.set_override("logging.level", log_level.clone())?; + } + + let config = builder.build()?; + + let settings: Settings = config.try_deserialize()?; + + Ok(settings) + } +} \ No newline at end of file