From 14dca91d92f4aeceef5e01c527b65b547151dea4 Mon Sep 17 00:00:00 2001 From: Malte Meiboom Date: Mon, 18 May 2026 11:11:22 +0200 Subject: [PATCH] Move logging config to main configuration file. - Move the content of what was stored in log4rs.yml into the main config file, so that there is just one file to parse to get the complete configuration. - Create the global switch `--home` to pass/overload the value for SEQUOIA_HOME in the configuration file. --- config/config.toml | 23 +++++++++++++++++++++-- src/cli/cli_args.rs | 7 +++++++ src/config.rs | 38 ++++++++++++++++++++++++++------------ src/main.rs | 15 ++++++--------- src/types/defaults.rs | 21 +++++++++++++++++++++ 5 files changed, 81 insertions(+), 23 deletions(-) diff --git a/config/config.toml b/config/config.toml index c2d21ae..9f7f7ec 100644 --- a/config/config.toml +++ b/config/config.toml @@ -5,6 +5,25 @@ connection = "localhost:3000" sequoia_home = "/tmp/sq_home" -logfile_config = "./config/log4rs.yml" +#keyservers = [ "hkps://keys.openpgp.org/", "hkps://mail-api.proton.me", "hkps://keys.mailvelope.com", "hkps://keyserver.ubuntu.com", "hkps://sks.pod01.fleetstreetops.com" ] +keyservers = [ "hkp://example.com" ] + +[logging.appenders.console] +kind = "console" +[logging.appenders.console.encoder] +pattern = "{d(%Y-%m-%d %H:%M:%S)(local)} {h({l})} [{t}] {m}{n}" + +[logging.appenders.file] +kind = "file" +path = "/tmp/husk.log" + +[logging.appenders.file.encoder] +pattern = "{d(%Y-%m-%d %H:%M:%S)(local)} {h({l})} [{t}] {m}{n}" + +[logging.root] +level = "debug" +appenders = ["file"] + +[logging.loggers."husk"] +level = "debug" -keyservers = [ "hkps://keys.openpgp.org/", "hkps://mail-api.proton.me", "hkps://keys.mailvelope.com", "hkps://keyserver.ubuntu.com", "hkps://sks.pod01.fleetstreetops.com" ] diff --git a/src/cli/cli_args.rs b/src/cli/cli_args.rs index c8075b8..f437203 100644 --- a/src/cli/cli_args.rs +++ b/src/cli/cli_args.rs @@ -15,6 +15,13 @@ pub struct CliArgs { )] pub config: Option, + #[clap( + long, + global = true, + help = "Certification store (SEQUOIA_HOME).", + )] + pub home: Option, + #[clap(subcommand)] pub subcommand: HuskSubcommands, } diff --git a/src/config.rs b/src/config.rs index 5e95778..a758404 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,27 +3,36 @@ // use anyhow; + use std::fs; use std::sync::{Arc, Mutex}; use serde_derive::Deserialize; +use toml; +use log4rs; use crate::types::errors::HuskError; +use crate::types::defaults::CONSOLE_LOGGING; #[derive(Deserialize, Debug, Clone)] pub struct HuskConfig { pub connection: String, pub sequoia_home: String, - logfile_config: Option, pub keyservers: Option>, + logging: Option, } impl HuskConfig { - pub fn load(path: &str) -> anyhow::Result { + pub fn load(path: &str, home: Option) -> anyhow::Result { match fs::read_to_string(path) { Ok(data) => { - match toml::from_str(data.as_str()) { - Ok(configuration) => Ok(configuration), + match toml::from_str::(data.as_str()) { + Ok(mut configuration) => { + if let Some(home) = home { + configuration.sequoia_home = home; + } + Ok(configuration) + }, Err(e) => { Err(HuskError::ConfigFileParseError(e.to_string()).into()) } @@ -35,14 +44,19 @@ impl HuskConfig { } } - pub fn logfile_config(&self) -> String { - match &self.logfile_config { - Some(c) => c.clone(), - None => { - eprintln!("Using logging configuration from 'log4rs.yml'"); - "log4rs.yml".to_string() - }, - } + pub fn init_logging(&self) -> anyhow::Result<()> { + let config = match toml::to_string(&self.logging) { + Ok(c) => c, + Err(e) => { + eprintln!("use default logging, because {}", e); + eprintln!("{:?}", self.logging); + CONSOLE_LOGGING.to_string() + } + }; + let raw_config = toml::from_str::(config.as_str())?; + log4rs::init_raw_config(raw_config)?; + + Ok(()) } pub fn get_keyservers(&self) -> Vec { diff --git a/src/main.rs b/src/main.rs index 2f8666d..5c6c265 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ //! Husk milter +use anyhow; use std::process; use std::sync::{Arc, Mutex}; @@ -18,7 +19,7 @@ use daemon::Daemon; pub mod common; #[tokio::main] -async fn main() { +async fn main() -> anyhow::Result<()> { let args = CliArgs::parse(); let config_file = match &args.config { @@ -26,7 +27,8 @@ async fn main() { None => defaults::CONFIG_FILE_LOCATION }; - let husk_config = match HuskConfig::load(config_file) { + // XXX: Improve the way cli parameters are passed into the configuration. + let husk_config = match HuskConfig::load(config_file, args.home.clone()) { Ok(config) => config, Err(e) => { eprintln!("{:?}", e); @@ -34,14 +36,9 @@ async fn main() { } }; - log4rs::init_file(husk_config.logfile_config(), Default::default()).unwrap(); - - log::info!("starting..."); + husk_config.init_logging()?; let config_container: HuskConfigContainer = Arc::new(Mutex::new(husk_config)); - if let Err(e) = commands::dispatch(args, config_container).await { - eprintln!("exit with {:?}", e); - // XXX: actually return an error code - } + commands::dispatch(args, config_container).await } diff --git a/src/types/defaults.rs b/src/types/defaults.rs index b645273..ab955ea 100644 --- a/src/types/defaults.rs +++ b/src/types/defaults.rs @@ -1,3 +1,24 @@ //! Defaults pub const CONFIG_FILE_LOCATION: &'static str = "/etc/husk/config.toml"; + +/// Default logging configuration. +pub const CONSOLE_LOGGING: &'static str = " +[appenders.console] +kind = \"console\" + +[appenders.console.encoder] +pattern = \"{d(%Y-%m-%d %H:%M:%S)(local)} {h({l})} [{t}] {m}{n}\" + +[[appenders.console.filters]] +kind = \"threshold\" +level = \"debug\" + +[root] +level = \"info\" +appenders = [\"console\"] + +[loggers.\"husk\"] +level = \"debug\" +"; +