Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: supports reading config values from CLI #605

Open
wants to merge 11 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/notary/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion crates/notary/server/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ logging:

authorization:
enabled: false
whitelist-csv-path: "./fixture/auth/whitelist.csv"
whitelist-csv-path: "./fixture/auth/whitelist.csv"
anthonykimani marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 1 addition & 3 deletions crates/notary/server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ 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)
/// 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,
Expand Down
9 changes: 9 additions & 0 deletions crates/notary/server/src/domain/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u16>,

#[structopt(long)]
pub tls_enabled: Option<bool>,

#[structopt(long)]
pub log_level: Option<String>,
}
2 changes: 2 additions & 0 deletions crates/notary/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod middleware;
mod server;
mod server_tracing;
mod service;
mod settings;
mod signing;
mod util;

Expand All @@ -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;
20 changes: 11 additions & 9 deletions crates/notary/server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
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)?;

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");
anthonykimani marked this conversation as resolved.
Show resolved Hide resolved
debug!(?settings.config, "Server config loaded");

// Run the server
run_server(&config).await?;
run_server(&settings.config).await?;

Ok(())
}
}
anthonykimani marked this conversation as resolved.
Show resolved Hide resolved
39 changes: 39 additions & 0 deletions crates/notary/server/src/settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use config::{Config, ConfigError, Environment, File};
anthonykimani marked this conversation as resolved.
Show resolved Hide resolved
use std::path::Path;
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<Self, ConfigError> {
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("_"));
anthonykimani marked this conversation as resolved.
Show resolved Hide resolved

// 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)
}
}
anthonykimani marked this conversation as resolved.
Show resolved Hide resolved