From 7b5bc27bec262dddaf1dea534fc2152ecdb1cd20 Mon Sep 17 00:00:00 2001 From: anthony Date: Wed, 25 Sep 2024 17:40:29 +0300 Subject: [PATCH 01/14] feat: supports reading config values from CLI --- crates/notary/server/src/domain/cli.rs | 9 +++ crates/notary/server/src/lib.rs | 2 + crates/notary/server/src/main.rs | 20 ++++--- crates/notary/server/src/settings.rs | 77 ++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 crates/notary/server/src/settings.rs diff --git a/crates/notary/server/src/domain/cli.rs b/crates/notary/server/src/domain/cli.rs index 83a03e98d8..a7f26730dc 100644 --- a/crates/notary/server/src/domain/cli.rs +++ b/crates/notary/server/src/domain/cli.rs @@ -7,4 +7,13 @@ 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 9353150dbd..140a1cac49 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 58d8ed3a80..db40561492 100644 --- a/crates/notary/server/src/main.rs +++ b/crates/notary/server/src/main.rs @@ -3,23 +3,27 @@ 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)?; + + // Load and merge configurations + let settings = Settings::new(&cli_fields) + .map_err(|err| eyre!("Failed to load settings: {}", err))?; // Set up tracing for logging - init_tracing(&config).map_err(|err| eyre!("Failed to set up tracing: {err}"))?; + init_tracing(&settings.config) + .map_err(|err| eyre!("Failed to set up tracing: {err}"))?; - debug!(?config, "Server config loaded"); + debug!(?settings, "Server settings loaded"); // 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 0000000000..3c6f90f943 --- /dev/null +++ b/crates/notary/server/src/settings.rs @@ -0,0 +1,77 @@ +use config::{Config, ConfigError, Environment, File}; +use std::path::Path; +use tracing::{info, warn}; +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 builder = Config::builder(); + + // Add default values + builder = builder + .set_default("server.name", "notary-server")? + .set_default("server.host", "0.0.0.0")? + .set_default("server.port", 7047)? + .set_default("server.html-info", "

Notary Server

")? + .set_default("notarization.max-sent-data", 4096)? + .set_default("notarization.max-recv-data", 16384)? + .set_default("tls.enabled", true)? + .set_default("tls.private-key-pem-path", "../fixture/tls/notary.key")? + .set_default("tls.certificate-pem-path", "../fixture/tls/notary.crt")? + .set_default("notary-key.private-key-pem-path", "../fixture/notary/notary.key")? + .set_default("notary-key.public-key-pem-path", "../fixture/notary/notary.pub")? + .set_default("logging.level", "DEBUG")? + .set_default("logging.filter", Option::::None)? + .set_default("authorization.enabled", false)? + .set_default("authorization.whitelist-csv-path", "../fixture/auth/whitelist.csv")?; + + // Add the config file if it exists + let config_path = Path::new(&cli_fields.config_file); + if config_path.exists() { + info!("Loading configuration from: {}", cli_fields.config_file); + builder = builder.add_source(File::from(config_path)); + } else { + warn!("Config file not found: {}. Using defaults and overrides.", cli_fields.config_file); + } + + // Add environment variables + builder = builder.add_source(Environment::with_prefix("NOTARY_SERVER").separator("__")); + + // Add CLI 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()?; + + // Validate file existence + Self::validate_file_exists(&settings.config.tls.private_key_pem_path, "TLS private key")?; + Self::validate_file_exists(&settings.config.tls.certificate_pem_path, "TLS certificate")?; + Self::validate_file_exists(&settings.config.notary_key.private_key_pem_path, "Notary private key")?; + Self::validate_file_exists(&settings.config.notary_key.public_key_pem_path, "Notary public key")?; + + Ok(settings) + } + + fn validate_file_exists(path: &str, file_type: &str) -> Result<(), ConfigError> { + if !Path::new(path).exists() { + Err(ConfigError::NotFound(format!("{} file not found: {}", file_type, path))) + } else { + Ok(()) + } + } +} \ No newline at end of file From 9d0ac1f3c62d32e1e1a26ec0935a7b5e1c115482 Mon Sep 17 00:00:00 2001 From: anthony Date: Thu, 26 Sep 2024 13:00:34 +0300 Subject: [PATCH 02/14] chore: adds config lib to cargo.toml, uses server default config values instead, removes validations in settings.rs --- crates/notary/server/src/domain/cli.rs | 2 +- crates/notary/server/src/main.rs | 24 ++++++++++---- crates/notary/server/src/settings.rs | 43 +++++--------------------- 3 files changed, 27 insertions(+), 42 deletions(-) diff --git a/crates/notary/server/src/domain/cli.rs b/crates/notary/server/src/domain/cli.rs index a7f26730dc..41dbd061b2 100644 --- a/crates/notary/server/src/domain/cli.rs +++ b/crates/notary/server/src/domain/cli.rs @@ -5,7 +5,7 @@ use structopt::StructOpt; #[structopt(name = "Notary Server")] pub struct CliFields { /// Configuration file location - #[structopt(long, default_value = "./config/config.yaml")] + #[structopt(long, default_value = "../server/config/config.yaml")] pub config_file: String, #[structopt(long)] diff --git a/crates/notary/server/src/main.rs b/crates/notary/server/src/main.rs index db40561492..16d9e0e829 100644 --- a/crates/notary/server/src/main.rs +++ b/crates/notary/server/src/main.rs @@ -1,10 +1,9 @@ use eyre::{eyre, Result}; use structopt::StructOpt; -use tracing::debug; - +use tracing::{info, debug, error}; use notary_server::{ init_tracing, run_server, CliFields, NotaryServerError, - Settings, + Settings }; #[tokio::main] @@ -12,15 +11,28 @@ async fn main() -> Result<(), NotaryServerError> { // Load command line arguments let cli_fields: CliFields = CliFields::from_args(); - // Load and merge configurations - let settings = Settings::new(&cli_fields) - .map_err(|err| eyre!("Failed to load settings: {}", err))?; + let settings = match Settings::new(&cli_fields) { + Ok(s) => s, + Err(e) => { + error!("Failed to load settings: {:?}. Check that all required fields are provided in the configuration.", e); + return Err(eyre!( + "Failed to load settings: {:?}. Ensure required fields (like `server.port`, `tls.enabled`, etc.) are present.", + e + ).into()); + } + }; + + // Print the entire configuration for debugging + println!("Loaded settings: {:#?}", settings); // Set up tracing for logging init_tracing(&settings.config) .map_err(|err| eyre!("Failed to set up tracing: {err}"))?; debug!(?settings, "Server settings loaded"); + info!("Server port: {}", settings.config.server.port); + info!("TLS enabled: {}", settings.config.tls.enabled); + info!("Log level: {}", settings.config.logging.level); // Run the server run_server(&settings.config).await?; diff --git a/crates/notary/server/src/settings.rs b/crates/notary/server/src/settings.rs index 3c6f90f943..903f5f7a60 100644 --- a/crates/notary/server/src/settings.rs +++ b/crates/notary/server/src/settings.rs @@ -1,6 +1,6 @@ use config::{Config, ConfigError, Environment, File}; use std::path::Path; -use tracing::{info, warn}; +use tracing::{info, warn, debug}; use crate::{CliFields, NotaryServerProperties}; use serde::Deserialize; @@ -14,31 +14,14 @@ impl Settings { pub fn new(cli_fields: &CliFields) -> Result { let mut builder = Config::builder(); - // Add default values - builder = builder - .set_default("server.name", "notary-server")? - .set_default("server.host", "0.0.0.0")? - .set_default("server.port", 7047)? - .set_default("server.html-info", "

Notary Server

")? - .set_default("notarization.max-sent-data", 4096)? - .set_default("notarization.max-recv-data", 16384)? - .set_default("tls.enabled", true)? - .set_default("tls.private-key-pem-path", "../fixture/tls/notary.key")? - .set_default("tls.certificate-pem-path", "../fixture/tls/notary.crt")? - .set_default("notary-key.private-key-pem-path", "../fixture/notary/notary.key")? - .set_default("notary-key.public-key-pem-path", "../fixture/notary/notary.pub")? - .set_default("logging.level", "DEBUG")? - .set_default("logging.filter", Option::::None)? - .set_default("authorization.enabled", false)? - .set_default("authorization.whitelist-csv-path", "../fixture/auth/whitelist.csv")?; - // Add the config file if it exists let config_path = Path::new(&cli_fields.config_file); + if config_path.exists() { - info!("Loading configuration from: {}", cli_fields.config_file); + info!("Loading configuration from: {}", config_path.display()); builder = builder.add_source(File::from(config_path)); } else { - warn!("Config file not found: {}. Using defaults and overrides.", cli_fields.config_file); + warn!("Config file not found: {}. Using defaults and overrides.", cli_fields.config_file.clone()); } // Add environment variables @@ -56,22 +39,12 @@ impl Settings { } let config = builder.build()?; - let settings: Settings = config.try_deserialize()?; - // Validate file existence - Self::validate_file_exists(&settings.config.tls.private_key_pem_path, "TLS private key")?; - Self::validate_file_exists(&settings.config.tls.certificate_pem_path, "TLS certificate")?; - Self::validate_file_exists(&settings.config.notary_key.private_key_pem_path, "Notary private key")?; - Self::validate_file_exists(&settings.config.notary_key.public_key_pem_path, "Notary public key")?; + // Log the entire configuration for debugging + debug!("Loaded configuration: {:#?}", config); - Ok(settings) - } + let settings: Settings = config.try_deserialize()?; - fn validate_file_exists(path: &str, file_type: &str) -> Result<(), ConfigError> { - if !Path::new(path).exists() { - Err(ConfigError::NotFound(format!("{} file not found: {}", file_type, path))) - } else { - Ok(()) - } + Ok(settings) } } \ No newline at end of file From ea7c31b135b504ac7d1c2e733d9483126c373c1b Mon Sep 17 00:00:00 2001 From: anthony Date: Thu, 26 Sep 2024 14:34:46 +0300 Subject: [PATCH 03/14] chore: tries to load YAML file --- crates/notary/server/src/domain/cli.rs | 2 +- crates/notary/server/src/settings.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/notary/server/src/domain/cli.rs b/crates/notary/server/src/domain/cli.rs index 41dbd061b2..0cc0f229ad 100644 --- a/crates/notary/server/src/domain/cli.rs +++ b/crates/notary/server/src/domain/cli.rs @@ -5,7 +5,7 @@ use structopt::StructOpt; #[structopt(name = "Notary Server")] pub struct CliFields { /// Configuration file location - #[structopt(long, default_value = "../server/config/config.yaml")] + #[structopt(long, default_value = "./server/config/config.yaml")] pub config_file: String, #[structopt(long)] diff --git a/crates/notary/server/src/settings.rs b/crates/notary/server/src/settings.rs index 903f5f7a60..9d773ae0d4 100644 --- a/crates/notary/server/src/settings.rs +++ b/crates/notary/server/src/settings.rs @@ -18,10 +18,10 @@ impl Settings { let config_path = Path::new(&cli_fields.config_file); if config_path.exists() { - info!("Loading configuration from: {}", config_path.display()); + info!("Loading configuration from: {}", cli_fields.config_file); builder = builder.add_source(File::from(config_path)); } else { - warn!("Config file not found: {}. Using defaults and overrides.", cli_fields.config_file.clone()); + warn!("Config file not found: {}. Using defaults and overrides.", cli_fields.config_file); } // Add environment variables From f2bf609b9b32d308fd9385c1923ecf7c55558ccc Mon Sep 17 00:00:00 2001 From: anthony Date: Thu, 26 Sep 2024 14:43:04 +0300 Subject: [PATCH 04/14] chore: tries to load YAML file --- .idea/.gitignore | 5 +++++ .idea/modules.xml | 8 ++++++++ .idea/tlsn.iml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ Cargo.toml | 1 + crates/notary/server/Cargo.toml | 1 + crates/notary/server/config/config.yaml | 20 ++++++++++---------- 7 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/modules.xml create mode 100644 .idea/tlsn.iml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000..ea7ed093e5 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000..af8f6e1850 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/tlsn.iml b/.idea/tlsn.iml new file mode 100644 index 0000000000..ca6a26db7a --- /dev/null +++ b/.idea/tlsn.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000..c8397c94c0 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index cadea13b3e..fb7475e15b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -87,6 +87,7 @@ bincode = { version = "1.3" } blake3 = { version = "1.5" } bytes = { version = "1.4" } chrono = { version = "0.4" } +config = { version = "0.14", features = ["yaml"] } cipher = { version = "0.4" } criterion = { version = "0.5" } ctr = { version = "0.9" } diff --git a/crates/notary/server/Cargo.toml b/crates/notary/server/Cargo.toml index 856177014e..8e5961d14c 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 = { workspace = true } 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 18336dc567..e3ff66dc24 100644 --- a/crates/notary/server/config/config.yaml +++ b/crates/notary/server/config/config.yaml @@ -2,7 +2,7 @@ server: name: "notary-server" host: "0.0.0.0" port: 7047 - html-info: | + html_info: |

Notary Server {version}!

  • git commit hash: {git_commit_hash}
  • @@ -12,21 +12,21 @@ server: health check - info
    notarization: - max-sent-data: 4096 - max-recv-data: 16384 + max_sent_data: 4096 + max_recv_data: 16384 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" +notary_key: + 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 From 15c5bd62079396c8763b3121fb9d4caa317d8bc9 Mon Sep 17 00:00:00 2001 From: anthony Date: Fri, 27 Sep 2024 07:35:58 +0300 Subject: [PATCH 05/14] fix: loads config.yaml properly, refactors code --- crates/notary/server/src/domain/cli.rs | 4 ++-- crates/notary/server/src/main.rs | 21 ++--------------- crates/notary/server/src/settings.rs | 31 +++++++++----------------- 3 files changed, 15 insertions(+), 41 deletions(-) diff --git a/crates/notary/server/src/domain/cli.rs b/crates/notary/server/src/domain/cli.rs index 0cc0f229ad..d4d98a1c69 100644 --- a/crates/notary/server/src/domain/cli.rs +++ b/crates/notary/server/src/domain/cli.rs @@ -2,10 +2,10 @@ 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 = "./server/config/config.yaml")] + #[structopt(long, default_value = "./config/config.yaml")] pub config_file: String, #[structopt(long)] diff --git a/crates/notary/server/src/main.rs b/crates/notary/server/src/main.rs index 16d9e0e829..a694b885c4 100644 --- a/crates/notary/server/src/main.rs +++ b/crates/notary/server/src/main.rs @@ -1,6 +1,5 @@ use eyre::{eyre, Result}; use structopt::StructOpt; -use tracing::{info, debug, error}; use notary_server::{ init_tracing, run_server, CliFields, NotaryServerError, Settings @@ -11,29 +10,13 @@ async fn main() -> Result<(), NotaryServerError> { // Load command line arguments let cli_fields: CliFields = CliFields::from_args(); - let settings = match Settings::new(&cli_fields) { - Ok(s) => s, - Err(e) => { - error!("Failed to load settings: {:?}. Check that all required fields are provided in the configuration.", e); - return Err(eyre!( - "Failed to load settings: {:?}. Ensure required fields (like `server.port`, `tls.enabled`, etc.) are present.", - e - ).into()); - } - }; - - // Print the entire configuration for debugging - println!("Loaded settings: {:#?}", settings); + let settings = Settings::new(&cli_fields) + .map_err(|err| eyre!("Failed to load settings: {}", err))?; // Set up tracing for logging init_tracing(&settings.config) .map_err(|err| eyre!("Failed to set up tracing: {err}"))?; - debug!(?settings, "Server settings loaded"); - info!("Server port: {}", settings.config.server.port); - info!("TLS enabled: {}", settings.config.tls.enabled); - info!("Log level: {}", settings.config.logging.level); - // Run the server run_server(&settings.config).await?; diff --git a/crates/notary/server/src/settings.rs b/crates/notary/server/src/settings.rs index 9d773ae0d4..3d8b8f676b 100644 --- a/crates/notary/server/src/settings.rs +++ b/crates/notary/server/src/settings.rs @@ -1,7 +1,6 @@ use config::{Config, ConfigError, Environment, File}; -use std::path::Path; -use tracing::{info, warn, debug}; -use crate::{CliFields, NotaryServerProperties}; +use std::env; +use crate::{ CliFields, NotaryServerProperties }; use serde::Deserialize; #[derive(Debug, Deserialize)] @@ -12,22 +11,17 @@ pub struct Settings { impl Settings { pub fn new(cli_fields: &CliFields) -> Result { - let mut builder = Config::builder(); + 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"); - // Add the config file if it exists - let config_path = Path::new(&cli_fields.config_file); + 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("__")); - if config_path.exists() { - info!("Loading configuration from: {}", cli_fields.config_file); - builder = builder.add_source(File::from(config_path)); - } else { - warn!("Config file not found: {}. Using defaults and overrides.", cli_fields.config_file); - } - - // Add environment variables - builder = builder.add_source(Environment::with_prefix("NOTARY_SERVER").separator("__")); - - // Add CLI overrides + // Apply CLI argument overrides if let Some(port) = cli_fields.port { builder = builder.set_override("server.port", port)?; } @@ -40,9 +34,6 @@ impl Settings { let config = builder.build()?; - // Log the entire configuration for debugging - debug!("Loaded configuration: {:#?}", config); - let settings: Settings = config.try_deserialize()?; Ok(settings) From 9d04e316c97a44c5e555f1bcaca02138cbd51b0e Mon Sep 17 00:00:00 2001 From: anthony Date: Fri, 27 Sep 2024 07:53:47 +0300 Subject: [PATCH 06/14] fix: removes .idea folder and moves config lib to notary-server cargo.toml --- .idea/.gitignore | 5 ----- .idea/modules.xml | 8 -------- .idea/tlsn.iml | 8 -------- .idea/vcs.xml | 6 ------ Cargo.toml | 1 - crates/notary/server/Cargo.toml | 2 +- crates/notary/server/config/config.yaml | 18 +++++++++--------- 7 files changed, 10 insertions(+), 38 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/modules.xml delete mode 100644 .idea/tlsn.iml delete mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index ea7ed093e5..0000000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index af8f6e1850..0000000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/tlsn.iml b/.idea/tlsn.iml deleted file mode 100644 index ca6a26db7a..0000000000 --- a/.idea/tlsn.iml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index c8397c94c0..0000000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index fb7475e15b..cadea13b3e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -87,7 +87,6 @@ bincode = { version = "1.3" } blake3 = { version = "1.5" } bytes = { version = "1.4" } chrono = { version = "0.4" } -config = { version = "0.14", features = ["yaml"] } cipher = { version = "0.4" } criterion = { version = "0.5" } ctr = { version = "0.9" } diff --git a/crates/notary/server/Cargo.toml b/crates/notary/server/Cargo.toml index 8e5961d14c..e29b9aa5f1 100644 --- a/crates/notary/server/Cargo.toml +++ b/crates/notary/server/Cargo.toml @@ -15,7 +15,7 @@ axum-core = { version = "0.4" } axum-macros = { version = "0.4" } base64 = { version = "0.21" } chrono = { version = "0.4" } -config = { workspace = true } +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 e3ff66dc24..354834aa82 100644 --- a/crates/notary/server/config/config.yaml +++ b/crates/notary/server/config/config.yaml @@ -2,7 +2,7 @@ server: name: "notary-server" host: "0.0.0.0" port: 7047 - html_info: | + html-info: |

    Notary Server {version}!

    • git commit hash: {git_commit_hash}
    • @@ -12,21 +12,21 @@ server: health check - info
      notarization: - max_sent_data: 4096 - max_recv_data: 16384 + max-sent-data: 4096 + max-recv-data: 16384 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" +notary-key: + private-key-pem-path: "../fixture/notary/notary.key" + public-key-pem-path: "../fixture/notary/notary.pub" logging: level: "DEBUG" authorization: enabled: false - whitelist_csv_path: "../fixture/auth/whitelist.csv" \ No newline at end of file + whitelist-csv-path: "../fixture/auth/whitelist.csv" \ No newline at end of file From 4f962ae51e04f01348c77421809c779560c01bee Mon Sep 17 00:00:00 2001 From: anthony Date: Tue, 1 Oct 2024 15:41:37 +0300 Subject: [PATCH 07/14] feat: uses serde-aux to deserialize env vars port and tls-enabled from string and restores &cli_fields.config_file path and debug log --- crates/notary/server/Cargo.toml | 1 + crates/notary/server/config/config.yaml | 12 ++++++------ crates/notary/server/src/config.rs | 3 +++ crates/notary/server/src/main.rs | 3 +++ crates/notary/server/src/settings.rs | 10 ++++------ 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/crates/notary/server/Cargo.toml b/crates/notary/server/Cargo.toml index e29b9aa5f1..4e63d7ea57 100644 --- a/crates/notary/server/Cargo.toml +++ b/crates/notary/server/Cargo.toml @@ -35,6 +35,7 @@ rustls-pemfile = { workspace = true } serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } serde_yaml = { version = "0.9" } +serde-aux = "4.5.0" sha1 = { version = "0.10" } structopt = { version = "0.3" } thiserror = { workspace = true } diff --git a/crates/notary/server/config/config.yaml b/crates/notary/server/config/config.yaml index 354834aa82..72724cc355 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" \ No newline at end of file + whitelist-csv-path: "./fixture/auth/whitelist.csv" \ No newline at end of file diff --git a/crates/notary/server/src/config.rs b/crates/notary/server/src/config.rs index 8937207018..e72bdcc8b3 100644 --- a/crates/notary/server/src/config.rs +++ b/crates/notary/server/src/config.rs @@ -1,4 +1,5 @@ use serde::Deserialize; +use serde_aux::field_attributes::{ deserialize_number_from_string, deserialize_bool_from_anything }; #[derive(Clone, Debug, Deserialize, Default)] #[serde(rename_all = "kebab-case")] @@ -41,6 +42,7 @@ pub struct ServerProperties { /// Used for testing purpose pub name: String, pub host: String, + #[serde(deserialize_with = "deserialize_number_from_string")] pub port: u16, /// Static html response returned from API root endpoint "/". Default html response contains /// placeholder strings that will be replaced with actual values in server.rs, e.g. {version}, {public_key} @@ -51,6 +53,7 @@ pub struct ServerProperties { #[serde(rename_all = "kebab-case")] pub struct TLSProperties { /// Flag to turn on/off TLS between prover and notary (should always be turned on unless TLS is handled by external setup e.g. reverse proxy, cloud) + #[serde(deserialize_with = "deserialize_bool_from_anything")] pub enabled: bool, pub private_key_pem_path: String, pub certificate_pem_path: String, diff --git a/crates/notary/server/src/main.rs b/crates/notary/server/src/main.rs index a694b885c4..cd7a08534c 100644 --- a/crates/notary/server/src/main.rs +++ b/crates/notary/server/src/main.rs @@ -1,5 +1,6 @@ use eyre::{eyre, Result}; use structopt::StructOpt; +use tracing::debug; use notary_server::{ init_tracing, run_server, CliFields, NotaryServerError, Settings @@ -17,6 +18,8 @@ async fn main() -> Result<(), NotaryServerError> { init_tracing(&settings.config) .map_err(|err| eyre!("Failed to set up tracing: {err}"))?; + debug!(?settings.config, "Server config loaded"); + // Run the server run_server(&settings.config).await?; diff --git a/crates/notary/server/src/settings.rs b/crates/notary/server/src/settings.rs index 3d8b8f676b..9872d32192 100644 --- a/crates/notary/server/src/settings.rs +++ b/crates/notary/server/src/settings.rs @@ -1,5 +1,5 @@ use config::{Config, ConfigError, Environment, File}; -use std::env; +use std::path::Path; use crate::{ CliFields, NotaryServerProperties }; use serde::Deserialize; @@ -11,15 +11,13 @@ pub struct Settings { 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 config_path = Path::new(&cli_fields.config_file); let mut builder = Config::builder() // Load base configuration - .add_source(File::from(configuration_path)) + .add_source(File::from(config_path)) // Add in settings from environment variables (with a prefix of NOTARY_SERVER and '__' as separator) - .add_source(Environment::with_prefix("NOTARY_SERVER").separator("__")); + .add_source(Environment::with_prefix("NOTARY_SERVER").prefix_separator("_").separator("__")); // Apply CLI argument overrides if let Some(port) = cli_fields.port { From ac1a24a1c9e4f765adc1bac21ff18d7aff5a9d0d Mon Sep 17 00:00:00 2001 From: anthony Date: Fri, 4 Oct 2024 07:09:32 +0300 Subject: [PATCH 08/14] fix: parses int and bool using try-parsing instead of serde-aux and removes unnecessary whitespaces --- crates/notary/server/Cargo.toml | 1 - crates/notary/server/src/config.rs | 3 --- crates/notary/server/src/domain/cli.rs | 2 +- crates/notary/server/src/settings.rs | 2 +- 4 files changed, 2 insertions(+), 6 deletions(-) diff --git a/crates/notary/server/Cargo.toml b/crates/notary/server/Cargo.toml index 4e63d7ea57..e29b9aa5f1 100644 --- a/crates/notary/server/Cargo.toml +++ b/crates/notary/server/Cargo.toml @@ -35,7 +35,6 @@ rustls-pemfile = { workspace = true } serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } serde_yaml = { version = "0.9" } -serde-aux = "4.5.0" sha1 = { version = "0.10" } structopt = { version = "0.3" } thiserror = { workspace = true } diff --git a/crates/notary/server/src/config.rs b/crates/notary/server/src/config.rs index df4827da5f..750f308eb1 100644 --- a/crates/notary/server/src/config.rs +++ b/crates/notary/server/src/config.rs @@ -1,5 +1,4 @@ use serde::Deserialize; -use serde_aux::field_attributes::{ deserialize_number_from_string, deserialize_bool_from_anything }; #[derive(Clone, Debug, Deserialize, Default)] #[serde(rename_all = "kebab-case")] @@ -42,7 +41,6 @@ pub struct ServerProperties { /// Used for testing purpose pub name: String, pub host: String, - #[serde(deserialize_with = "deserialize_number_from_string")] pub port: u16, /// Static html response returned from API root endpoint "/". Default html /// response contains placeholder strings that will be replaced with @@ -54,7 +52,6 @@ pub struct ServerProperties { #[serde(rename_all = "kebab-case")] pub struct TLSProperties { /// Flag to turn on/off TLS between prover and notary (should always be turned on unless TLS is handled by external setup e.g. reverse proxy, cloud) - #[serde(deserialize_with = "deserialize_bool_from_anything")] pub enabled: bool, pub private_key_pem_path: String, pub certificate_pem_path: String, diff --git a/crates/notary/server/src/domain/cli.rs b/crates/notary/server/src/domain/cli.rs index d4d98a1c69..a7f26730dc 100644 --- a/crates/notary/server/src/domain/cli.rs +++ b/crates/notary/server/src/domain/cli.rs @@ -2,7 +2,7 @@ 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")] diff --git a/crates/notary/server/src/settings.rs b/crates/notary/server/src/settings.rs index 9872d32192..1df240a19e 100644 --- a/crates/notary/server/src/settings.rs +++ b/crates/notary/server/src/settings.rs @@ -17,7 +17,7 @@ impl Settings { // Load base configuration .add_source(File::from(config_path)) // Add in settings from environment variables (with a prefix of NOTARY_SERVER and '__' as separator) - .add_source(Environment::with_prefix("NOTARY_SERVER").prefix_separator("_").separator("__")); + .add_source(Environment::with_prefix("NOTARY_SERVER").try_parsing(true).prefix_separator("__").separator("_")); // Apply CLI argument overrides if let Some(port) = cli_fields.port { From 04c7976d9b8ccf68c2a4434b390a6a099f533483 Mon Sep 17 00:00:00 2001 From: anthony Date: Sat, 5 Oct 2024 02:51:10 +0300 Subject: [PATCH 09/14] chore: converts config to snake_case for consistency --- crates/notary/server/config/config.yaml | 18 +++++++++--------- crates/notary/server/src/config.rs | 7 ------- crates/notary/server/src/main.rs | 2 +- crates/notary/server/src/settings.rs | 4 ++-- 4 files changed, 12 insertions(+), 19 deletions(-) diff --git a/crates/notary/server/config/config.yaml b/crates/notary/server/config/config.yaml index 72724cc355..5916b9bdf1 100644 --- a/crates/notary/server/config/config.yaml +++ b/crates/notary/server/config/config.yaml @@ -2,7 +2,7 @@ server: name: "notary-server" host: "0.0.0.0" port: 7047 - html-info: | + html_info: |

      Notary Server {version}!

      • git commit hash: {git_commit_hash}
      • @@ -12,21 +12,21 @@ server: health check - info
        notarization: - max-sent-data: 4096 - max-recv-data: 16384 + max_sent_data: 4096 + max_recv_data: 16384 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" +notary_key: + private_key_pem_path: "./fixture/notary/notary.key" + public_key_pem_path: "./fixture/notary/notary.pub" logging: level: DEBUG authorization: enabled: false - whitelist-csv-path: "./fixture/auth/whitelist.csv" \ No newline at end of file + whitelist_csv_path: "./fixture/auth/whitelist.csv" diff --git a/crates/notary/server/src/config.rs b/crates/notary/server/src/config.rs index 750f308eb1..b4ccbbb955 100644 --- a/crates/notary/server/src/config.rs +++ b/crates/notary/server/src/config.rs @@ -1,7 +1,6 @@ use serde::Deserialize; #[derive(Clone, Debug, Deserialize, Default)] -#[serde(rename_all = "kebab-case")] pub struct NotaryServerProperties { /// Name and address of the notary server pub server: ServerProperties, @@ -18,7 +17,6 @@ pub struct NotaryServerProperties { } #[derive(Clone, Debug, Deserialize, Default)] -#[serde(rename_all = "kebab-case")] pub struct AuthorizationProperties { /// Switch to turn on or off auth middleware pub enabled: bool, @@ -27,7 +25,6 @@ pub struct AuthorizationProperties { } #[derive(Clone, Debug, Deserialize, Default)] -#[serde(rename_all = "kebab-case")] pub struct NotarizationProperties { /// Global limit for maximum number of bytes that can be sent pub max_sent_data: usize, @@ -36,7 +33,6 @@ pub struct NotarizationProperties { } #[derive(Clone, Debug, Deserialize, Default)] -#[serde(rename_all = "kebab-case")] pub struct ServerProperties { /// Used for testing purpose pub name: String, @@ -49,7 +45,6 @@ pub struct ServerProperties { } #[derive(Clone, Debug, Deserialize, Default)] -#[serde(rename_all = "kebab-case")] pub struct TLSProperties { /// Flag to turn on/off TLS between prover and notary (should always be turned on unless TLS is handled by external setup e.g. reverse proxy, cloud) pub enabled: bool, @@ -58,14 +53,12 @@ pub struct TLSProperties { } #[derive(Clone, Debug, Deserialize, Default)] -#[serde(rename_all = "kebab-case")] pub struct NotarySigningKeyProperties { pub private_key_pem_path: String, pub public_key_pem_path: String, } #[derive(Clone, Debug, Deserialize, Default)] -#[serde(rename_all = "kebab-case")] pub struct LoggingProperties { /// Log verbosity level of the default filtering logic, which is /// notary_server=,tlsn_verifier=,tls_mpc= Must be either of diff --git a/crates/notary/server/src/main.rs b/crates/notary/server/src/main.rs index cd7a08534c..401f9126ed 100644 --- a/crates/notary/server/src/main.rs +++ b/crates/notary/server/src/main.rs @@ -24,4 +24,4 @@ async fn main() -> Result<(), NotaryServerError> { 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 index 1df240a19e..12eea198c1 100644 --- a/crates/notary/server/src/settings.rs +++ b/crates/notary/server/src/settings.rs @@ -17,7 +17,7 @@ impl Settings { // Load base configuration .add_source(File::from(config_path)) // Add in settings from environment variables (with a prefix of NOTARY_SERVER and '__' as separator) - .add_source(Environment::with_prefix("NOTARY_SERVER").try_parsing(true).prefix_separator("__").separator("_")); + .add_source(Environment::with_prefix("NOTARY_SERVER").try_parsing(true).prefix_separator("__").separator("__")); // Apply CLI argument overrides if let Some(port) = cli_fields.port { @@ -36,4 +36,4 @@ impl Settings { Ok(settings) } -} \ No newline at end of file +} From a75e60f08c282a2608eb7a7bc193accdef7e141b Mon Sep 17 00:00:00 2001 From: anthony Date: Tue, 8 Oct 2024 13:14:59 +0300 Subject: [PATCH 10/14] doc: adds configuration documentation, code comments and fixes linting errors --- crates/notary/server/Cargo.toml | 2 +- crates/notary/server/README.md | 29 +++++++++++++++++++ crates/notary/server/src/domain/cli.rs | 3 ++ .../server/src/service/axum_websocket.rs | 2 +- crates/notary/server/src/settings.rs | 2 +- 5 files changed, 35 insertions(+), 3 deletions(-) diff --git a/crates/notary/server/Cargo.toml b/crates/notary/server/Cargo.toml index e29b9aa5f1..0d51dcfca4 100644 --- a/crates/notary/server/Cargo.toml +++ b/crates/notary/server/Cargo.toml @@ -41,7 +41,7 @@ thiserror = { workspace = true } tokio = { workspace = true, features = ["full"] } tokio-rustls = { workspace = true } tokio-util = { workspace = true, features = ["compat"] } -tower = { version = "0.4", features = ["make"] } +tower = { version = "0.4", features = ["make", "util"] } tower-http = { version = "0.5", features = ["cors"] } tower-service = { version = "0.3" } tracing = { workspace = true } diff --git a/crates/notary/server/README.md b/crates/notary/server/README.md index fa5b50bc3c..c1db9b2267 100644 --- a/crates/notary/server/README.md +++ b/crates/notary/server/README.md @@ -66,6 +66,35 @@ docker run --init -p 127.0.0.1:7047:7047 -v :/root/.notary-ser ```bash docker run --init -p 127.0.0.1:7047:7047 -v :/root/.notary-server/fixture/notary notary-server:local ``` + +## Configuration + +- The Notary Server can be configured using three methods: a configuration file, command-line interface (CLI) arguments, and environment variables. These methods provide flexibility in how you set up and run the server. + +1. Configuration File - By default, the server looks for a config.yaml file in the `notary/server/config/` directory. This file contains all the configurable settings for the server. + - Example: + ```yaml + server: + name: "notary-server" + host: "0.0.0.0" + port: 7047 + + notarization: + max_sent_data: 4096 + max_recv_data: 16384 + ``` + +2. Command-Line Interface (CLI) Arguments - You can override configuration file settings using CLI arguments when starting the server. CLI arguments take precedence over the config file. + - Example: + ```shell + cargo run -- --port 8080 --tls-enabled false --log-level INFO --max-sent-data 2048 + ``` + +3. Environment Variables can also be used to configure the server. They take precedence over both the config file and CLI arguments. The environment variables use the prefix `NOTARY_SERVER__` followed by the configuration path in uppercase, with double underscores as separators. + - Example: + ```shell + NOTARY_SERVER__SERVER__PORT=8080 NOTARY_SERVER__NOTARIZATION__MAX_SENT_DATA=2048 NOTARY_SERVER__TLS__ENABLED=false cargo run + ``` --- ## API All APIs are TLS-protected, hence please use `https://` or `wss://`. diff --git a/crates/notary/server/src/domain/cli.rs b/crates/notary/server/src/domain/cli.rs index a7f26730dc..e5928f62eb 100644 --- a/crates/notary/server/src/domain/cli.rs +++ b/crates/notary/server/src/domain/cli.rs @@ -8,12 +8,15 @@ pub struct CliFields { #[structopt(long, default_value = "./config/config.yaml")] pub config_file: String, + /// Port of notary server #[structopt(long)] pub port: Option, + /// Flag to turn on/off TLS when connecting to prover #[structopt(long)] pub tls_enabled: Option, + /// Level of logging #[structopt(long)] pub log_level: Option, } diff --git a/crates/notary/server/src/service/axum_websocket.rs b/crates/notary/server/src/service/axum_websocket.rs index 9a21cb8b85..863b0bdf97 100644 --- a/crates/notary/server/src/service/axum_websocket.rs +++ b/crates/notary/server/src/service/axum_websocket.rs @@ -898,7 +898,7 @@ mod tests { use super::*; use axum::{body::Body, routing::get, Router}; use http::{Request, Version}; - use tower::ServiceExt; + use tower::util::ServiceExt; #[tokio::test] async fn rejects_http_1_0_requests() { diff --git a/crates/notary/server/src/settings.rs b/crates/notary/server/src/settings.rs index 12eea198c1..7ef7eb0689 100644 --- a/crates/notary/server/src/settings.rs +++ b/crates/notary/server/src/settings.rs @@ -16,7 +16,7 @@ impl Settings { let mut builder = Config::builder() // Load base configuration .add_source(File::from(config_path)) - // Add in settings from environment variables (with a prefix of NOTARY_SERVER and '__' as separator) + // Add in settings from environment variables (with a prefix of NOTARY_SERVER and '__' as separator). .add_source(Environment::with_prefix("NOTARY_SERVER").try_parsing(true).prefix_separator("__").separator("__")); // Apply CLI argument overrides From 9c68a0c078070af7372681235c2d2783709e0490 Mon Sep 17 00:00:00 2001 From: anthony Date: Wed, 9 Oct 2024 12:25:17 +0300 Subject: [PATCH 11/14] fix: fixes ci linting formatting --- crates/notary/server/README.md | 17 +++-- .../server/fixture/auth/whitelist_copied.csv | 4 ++ crates/notary/server/src/config.rs | 4 +- crates/notary/server/src/main.rs | 12 ++-- .../server/src/service/axum_websocket.rs | 1 + crates/notary/server/src/settings.rs | 71 ++++++++++++++++++- 6 files changed, 88 insertions(+), 21 deletions(-) create mode 100644 crates/notary/server/fixture/auth/whitelist_copied.csv diff --git a/crates/notary/server/README.md b/crates/notary/server/README.md index c1db9b2267..df429ab91d 100644 --- a/crates/notary/server/README.md +++ b/crates/notary/server/README.md @@ -67,12 +67,11 @@ docker run --init -p 127.0.0.1:7047:7047 -v :/root/.notary-ser docker run --init -p 127.0.0.1:7047:7047 -v :/root/.notary-server/fixture/notary notary-server:local ``` -## Configuration +### Configuration -- The Notary Server can be configured using three methods: a configuration file, command-line interface (CLI) arguments, and environment variables. These methods provide flexibility in how you set up and run the server. +The notary server can be configured using three methods: a configuration file, command-line interface (CLI) arguments, and environment variables. These methods provide flexibility in how you set up and run the server. -1. Configuration File - By default, the server looks for a config.yaml file in the `notary/server/config/` directory. This file contains all the configurable settings for the server. - - Example: +1. Configuration File - By default, the server looks for a config.yaml file in the `notary/server/config/` directory. This file contains all the configurable settings for the server, e.g. ```yaml server: name: "notary-server" @@ -82,16 +81,16 @@ docker run --init -p 127.0.0.1:7047:7047 -v :/root/.notary-ser notarization: max_sent_data: 4096 max_recv_data: 16384 + + ... ``` -2. Command-Line Interface (CLI) Arguments - You can override configuration file settings using CLI arguments when starting the server. CLI arguments take precedence over the config file. - - Example: +2. Command-Line Interface (CLI) Arguments - You can override configuration file settings using CLI arguments when starting the server. They take precedence over both the config file and Environment Variables arguments, e.g. ```shell - cargo run -- --port 8080 --tls-enabled false --log-level INFO --max-sent-data 2048 + cargo run -- --port 8080 --tls-enabled false --log-level INFO ``` -3. Environment Variables can also be used to configure the server. They take precedence over both the config file and CLI arguments. The environment variables use the prefix `NOTARY_SERVER__` followed by the configuration path in uppercase, with double underscores as separators. - - Example: +3. Environment Variables can also be used to configure the server and take precedence over the config file. The environment variables use the prefix `NOTARY_SERVER__` followed by the configuration path in uppercase, with double underscores used for nested configuration such that `tls.enabled` in the config file, which will be `TLS__ENABLED` on CLI, e.g. ```shell NOTARY_SERVER__SERVER__PORT=8080 NOTARY_SERVER__NOTARIZATION__MAX_SENT_DATA=2048 NOTARY_SERVER__TLS__ENABLED=false cargo run ``` diff --git a/crates/notary/server/fixture/auth/whitelist_copied.csv b/crates/notary/server/fixture/auth/whitelist_copied.csv new file mode 100644 index 0000000000..330b497cd4 --- /dev/null +++ b/crates/notary/server/fixture/auth/whitelist_copied.csv @@ -0,0 +1,4 @@ +"Name","ApiKey","CreatedAt" +"Jonas Nielsen","test_api_key_0","2023-09-18T07:38:53Z" +"Eren Jaeger","test_api_key_1","2023-10-18T07:38:53Z" +unit-test-name,unit-test-api-key,unit-test-created-at diff --git a/crates/notary/server/src/config.rs b/crates/notary/server/src/config.rs index b4ccbbb955..c2f3963182 100644 --- a/crates/notary/server/src/config.rs +++ b/crates/notary/server/src/config.rs @@ -46,7 +46,9 @@ pub struct ServerProperties { #[derive(Clone, Debug, Deserialize, Default)] pub struct TLSProperties { - /// Flag to turn on/off TLS between prover and notary (should always be turned on unless TLS is handled by external setup e.g. reverse proxy, cloud) + /// Flag to turn on/off TLS between prover and notary (should always be + /// turned on unless TLS is handled by external setup e.g. reverse proxy, + /// cloud) pub enabled: bool, pub private_key_pem_path: String, pub certificate_pem_path: String, diff --git a/crates/notary/server/src/main.rs b/crates/notary/server/src/main.rs index 401f9126ed..53756e7c5a 100644 --- a/crates/notary/server/src/main.rs +++ b/crates/notary/server/src/main.rs @@ -1,22 +1,18 @@ use eyre::{eyre, Result}; +use notary_server::{init_tracing, run_server, CliFields, NotaryServerError, Settings}; use structopt::StructOpt; use tracing::debug; -use notary_server::{ - init_tracing, run_server, CliFields, NotaryServerError, - Settings -}; #[tokio::main] async fn main() -> Result<(), NotaryServerError> { // Load command line arguments let cli_fields: CliFields = CliFields::from_args(); - let settings = Settings::new(&cli_fields) - .map_err(|err| eyre!("Failed to load settings: {}", err))?; + let settings = + Settings::new(&cli_fields).map_err(|err| eyre!("Failed to load settings: {}", err))?; // Set up tracing for logging - init_tracing(&settings.config) - .map_err(|err| eyre!("Failed to set up tracing: {err}"))?; + init_tracing(&settings.config).map_err(|err| eyre!("Failed to set up tracing: {err}"))?; debug!(?settings.config, "Server config loaded"); diff --git a/crates/notary/server/src/service/axum_websocket.rs b/crates/notary/server/src/service/axum_websocket.rs index 3365aa0fc0..991391b6f9 100644 --- a/crates/notary/server/src/service/axum_websocket.rs +++ b/crates/notary/server/src/service/axum_websocket.rs @@ -898,6 +898,7 @@ mod tests { use super::*; use axum::{body::Body, routing::get, Router}; use http::{Request, Version}; + // NOTARY_MODIFICATION: use tower_util instead of tower to make clippy happy use tower_util::ServiceExt; #[tokio::test] diff --git a/crates/notary/server/src/settings.rs b/crates/notary/server/src/settings.rs index 7ef7eb0689..50f0f707fc 100644 --- a/crates/notary/server/src/settings.rs +++ b/crates/notary/server/src/settings.rs @@ -1,7 +1,7 @@ -use config::{Config, ConfigError, Environment, File}; -use std::path::Path; use crate::{ CliFields, NotaryServerProperties }; +use config::{Config, ConfigError, Environment, File}; use serde::Deserialize; +use std::path::Path; #[derive(Debug, Deserialize)] pub struct Settings { @@ -17,7 +17,12 @@ impl Settings { // Load base configuration .add_source(File::from(config_path)) // Add in settings from environment variables (with a prefix of NOTARY_SERVER and '__' as separator). - .add_source(Environment::with_prefix("NOTARY_SERVER").try_parsing(true).prefix_separator("__").separator("__")); + .add_source( + Environment::with_prefix("NOTARY_SERVER") + .try_parsing(true) + .prefix_separator("__") + .separator("__") + ); // Apply CLI argument overrides if let Some(port) = cli_fields.port { @@ -37,3 +42,63 @@ impl Settings { Ok(settings) } } + +#[cfg(test)] +mod test { + use super::*; + use std::env; + use eyre::{eyre}; + use tracing::Level; + use crate::config::NotaryServerProperties; + + #[test] + fn test_settings_from_config_file(){ + let cli_fields: CliFields = CliFields { + config_file: "./config/config.yaml".to_string(), + port: None, + tls_enabled: None, + log_level: None + }; + let settings: NotaryServerProperties = + Settings::new(&cli_fields).map_err(|err| eyre!("Failed to load settings: {}", err)).unwrap().config; + + assert_eq!(settings.server.port,7047); + assert_eq!(settings.tls.enabled, true); + } + + #[test] + fn test_settings_with_cli_override(){ + let cli_fields = CliFields { + config_file: "./config/config.yaml".to_string(), + port: Some(8080), + tls_enabled: Some(false), + log_level: Some(Level::INFO.to_string()) + }; + let settings: NotaryServerProperties = + Settings::new(&cli_fields).map_err(|err| eyre!("Failed to load settings: {}", err)).unwrap().config; + + assert_eq!(settings.server.port,8080); + assert_eq!(settings.tls.enabled, false); + } + + #[test] + fn test_settings_with_env_vars(){ + env::set_var("NOTARY_SERVER__SERVER__PORT", "3000"); + env::set_var("NOTARY_SERVER__NOTARIZATION__MAX_SENT_DATA", "3072"); + + let cli_fields = CliFields { + config_file: "./config/config.yaml".to_string(), + port: None, + tls_enabled: None, + log_level: None + }; + let settings: NotaryServerProperties = + Settings::new(&cli_fields).map_err(|err| eyre!("Failed to load settings: {}", err)).unwrap().config; + + assert_eq!(settings.server.port, 3000); + assert_eq!(settings.notarization.max_sent_data, 3072); + + env::remove_var("NOTARY_SERVER__SERVER__PORT"); + env::remove_var("NOTARY_SERVER__NOTARIZATION__MAX_SENT_DATA"); + } +} From 1f23f0c4a10fd6a2e2d64f1bc68f137e1eadccdb Mon Sep 17 00:00:00 2001 From: anthony Date: Wed, 9 Oct 2024 12:25:37 +0300 Subject: [PATCH 12/14] fix: fixes ci linting formatting --- crates/notary/server/src/settings.rs | 60 ---------------------------- 1 file changed, 60 deletions(-) diff --git a/crates/notary/server/src/settings.rs b/crates/notary/server/src/settings.rs index 50f0f707fc..2b2f93e6db 100644 --- a/crates/notary/server/src/settings.rs +++ b/crates/notary/server/src/settings.rs @@ -42,63 +42,3 @@ impl Settings { Ok(settings) } } - -#[cfg(test)] -mod test { - use super::*; - use std::env; - use eyre::{eyre}; - use tracing::Level; - use crate::config::NotaryServerProperties; - - #[test] - fn test_settings_from_config_file(){ - let cli_fields: CliFields = CliFields { - config_file: "./config/config.yaml".to_string(), - port: None, - tls_enabled: None, - log_level: None - }; - let settings: NotaryServerProperties = - Settings::new(&cli_fields).map_err(|err| eyre!("Failed to load settings: {}", err)).unwrap().config; - - assert_eq!(settings.server.port,7047); - assert_eq!(settings.tls.enabled, true); - } - - #[test] - fn test_settings_with_cli_override(){ - let cli_fields = CliFields { - config_file: "./config/config.yaml".to_string(), - port: Some(8080), - tls_enabled: Some(false), - log_level: Some(Level::INFO.to_string()) - }; - let settings: NotaryServerProperties = - Settings::new(&cli_fields).map_err(|err| eyre!("Failed to load settings: {}", err)).unwrap().config; - - assert_eq!(settings.server.port,8080); - assert_eq!(settings.tls.enabled, false); - } - - #[test] - fn test_settings_with_env_vars(){ - env::set_var("NOTARY_SERVER__SERVER__PORT", "3000"); - env::set_var("NOTARY_SERVER__NOTARIZATION__MAX_SENT_DATA", "3072"); - - let cli_fields = CliFields { - config_file: "./config/config.yaml".to_string(), - port: None, - tls_enabled: None, - log_level: None - }; - let settings: NotaryServerProperties = - Settings::new(&cli_fields).map_err(|err| eyre!("Failed to load settings: {}", err)).unwrap().config; - - assert_eq!(settings.server.port, 3000); - assert_eq!(settings.notarization.max_sent_data, 3072); - - env::remove_var("NOTARY_SERVER__SERVER__PORT"); - env::remove_var("NOTARY_SERVER__NOTARIZATION__MAX_SENT_DATA"); - } -} From 44cd32497a208f33897672216edb70f7e5890255 Mon Sep 17 00:00:00 2001 From: anthony Date: Thu, 10 Oct 2024 12:31:50 +0300 Subject: [PATCH 13/14] fix: adjusts formatting for settings.rs and minor adjustments to documentation --- crates/notary/server/README.md | 6 +++--- crates/notary/server/fixture/auth/whitelist_copied.csv | 4 ---- crates/notary/server/src/settings.rs | 7 ++++--- 3 files changed, 7 insertions(+), 10 deletions(-) delete mode 100644 crates/notary/server/fixture/auth/whitelist_copied.csv diff --git a/crates/notary/server/README.md b/crates/notary/server/README.md index df429ab91d..d890cc953c 100644 --- a/crates/notary/server/README.md +++ b/crates/notary/server/README.md @@ -84,13 +84,13 @@ The notary server can be configured using three methods: a configuration file, c ... ``` - -2. Command-Line Interface (CLI) Arguments - You can override configuration file settings using CLI arguments when starting the server. They take precedence over both the config file and Environment Variables arguments, e.g. + +2. Command-Line Interface (CLI) Arguments - You can override *some* configuration file settings using CLI arguments when starting the server. This also takes precedence over the environment variable method below. E.g. ```shell cargo run -- --port 8080 --tls-enabled false --log-level INFO ``` -3. Environment Variables can also be used to configure the server and take precedence over the config file. The environment variables use the prefix `NOTARY_SERVER__` followed by the configuration path in uppercase, with double underscores used for nested configuration such that `tls.enabled` in the config file, which will be `TLS__ENABLED` on CLI, e.g. +3. Environment Variables - This can be used to configure all the server settings, where it will override the config file. It uses the prefix `NOTARY_SERVER__` followed by the configuration key(s) in uppercase. Double underscores are used in nested configuration keys, e.g. `tls.enabled` in the config file will be `NOTARY_SERVER__TLS__ENABLED`. E.g. ```shell NOTARY_SERVER__SERVER__PORT=8080 NOTARY_SERVER__NOTARIZATION__MAX_SENT_DATA=2048 NOTARY_SERVER__TLS__ENABLED=false cargo run ``` diff --git a/crates/notary/server/fixture/auth/whitelist_copied.csv b/crates/notary/server/fixture/auth/whitelist_copied.csv deleted file mode 100644 index 330b497cd4..0000000000 --- a/crates/notary/server/fixture/auth/whitelist_copied.csv +++ /dev/null @@ -1,4 +0,0 @@ -"Name","ApiKey","CreatedAt" -"Jonas Nielsen","test_api_key_0","2023-09-18T07:38:53Z" -"Eren Jaeger","test_api_key_1","2023-10-18T07:38:53Z" -unit-test-name,unit-test-api-key,unit-test-created-at diff --git a/crates/notary/server/src/settings.rs b/crates/notary/server/src/settings.rs index 2b2f93e6db..b32a2172ed 100644 --- a/crates/notary/server/src/settings.rs +++ b/crates/notary/server/src/settings.rs @@ -1,4 +1,4 @@ -use crate::{ CliFields, NotaryServerProperties }; +use crate::{CliFields, NotaryServerProperties}; use config::{Config, ConfigError, Environment, File}; use serde::Deserialize; use std::path::Path; @@ -16,12 +16,13 @@ impl Settings { let mut builder = Config::builder() // Load base configuration .add_source(File::from(config_path)) - // Add in settings from environment variables (with a prefix of NOTARY_SERVER and '__' as separator). + // Add in settings from environment variables (with a prefix of NOTARY_SERVER and '__' + // as separator). .add_source( Environment::with_prefix("NOTARY_SERVER") .try_parsing(true) .prefix_separator("__") - .separator("__") + .separator("__"), ); // Apply CLI argument overrides From 20f33c4c0c8fe098c2cfdd2d749eb2f32df720a3 Mon Sep 17 00:00:00 2001 From: anthony Date: Thu, 10 Oct 2024 13:01:24 +0300 Subject: [PATCH 14/14] fix: uses cargo nightly to format correctly --- crates/notary/server/src/settings.rs | 90 ++++++++++++++-------------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/crates/notary/server/src/settings.rs b/crates/notary/server/src/settings.rs index b32a2172ed..b951dcee63 100644 --- a/crates/notary/server/src/settings.rs +++ b/crates/notary/server/src/settings.rs @@ -1,45 +1,45 @@ -use crate::{CliFields, NotaryServerProperties}; -use config::{Config, ConfigError, Environment, File}; -use serde::Deserialize; -use std::path::Path; - -#[derive(Debug, Deserialize)] -pub struct Settings { - #[serde(flatten)] - pub config: NotaryServerProperties, -} - -impl Settings { - pub fn new(cli_fields: &CliFields) -> Result { - let config_path = Path::new(&cli_fields.config_file); - - let mut builder = Config::builder() - // Load base configuration - .add_source(File::from(config_path)) - // Add in settings from environment variables (with a prefix of NOTARY_SERVER and '__' - // as separator). - .add_source( - Environment::with_prefix("NOTARY_SERVER") - .try_parsing(true) - .prefix_separator("__") - .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) - } -} +use crate::{CliFields, NotaryServerProperties}; +use config::{Config, ConfigError, Environment, File}; +use serde::Deserialize; +use std::path::Path; + +#[derive(Debug, Deserialize)] +pub struct Settings { + #[serde(flatten)] + pub config: NotaryServerProperties, +} + +impl Settings { + pub fn new(cli_fields: &CliFields) -> Result { + let config_path = Path::new(&cli_fields.config_file); + + let mut builder = Config::builder() + // Load base configuration + .add_source(File::from(config_path)) + // Add in settings from environment variables (with a prefix of NOTARY_SERVER and '__' + // as separator). + .add_source( + Environment::with_prefix("NOTARY_SERVER") + .try_parsing(true) + .prefix_separator("__") + .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) + } +}