-
Notifications
You must be signed in to change notification settings - Fork 69
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
anthonykimani
wants to merge
7
commits into
tlsnotary:dev
Choose a base branch
from
anthonykimani:dev
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7b5bc27
feat: supports reading config values from CLI
anthonykimani 9d0ac1f
chore: adds config lib to cargo.toml, uses server default config valu…
anthonykimani ea7c31b
chore: tries to load YAML file
anthonykimani f2bf609
chore: tries to load YAML file
anthonykimani ec9ede0
Merge branch 'dev' of github.com:tlsnotary/tlsn into dev
anthonykimani 15c5bd6
fix: loads config.yaml properly, refactors code
anthonykimani 9d04e31
fix: removes .idea folder and moves config lib to notary-server cargo…
anthonykimani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use config::{Config, ConfigError, Environment, File}; | ||
anthonykimani marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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<Self, ConfigError> { | ||
let mut builder = Config::builder(); | ||
|
||
// Add default values | ||
builder = builder | ||
anthonykimani marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.set_default("server.name", "notary-server")? | ||
.set_default("server.host", "0.0.0.0")? | ||
.set_default("server.port", 7047)? | ||
.set_default("server.html-info", "<h1>Notary Server</h1>")? | ||
.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::<String>::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); | ||
anthonykimani marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// Add environment variables | ||
builder = builder.add_source(Environment::with_prefix("NOTARY_SERVER").separator("__")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: so env var will override config file; and CLI will override both of them; is that right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @anthonykimani can u help to address this question? |
||
|
||
// 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")?; | ||
anthonykimani marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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(()) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lets keep one debug log instead