From 4a6f089b23fccb5279a200546e81937199ad56f3 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sat, 13 Jul 2024 01:24:37 +0000 Subject: [PATCH] move some config checks into check unit Signed-off-by: Jason Volk --- src/core/config/check.rs | 61 ++++++++++++++++++++++++++-- src/core/config/mod.rs | 88 ++++++++-------------------------------- 2 files changed, 74 insertions(+), 75 deletions(-) diff --git a/src/core/config/check.rs b/src/core/config/check.rs index 67d19c72..20a61b70 100644 --- a/src/core/config/check.rs +++ b/src/core/config/check.rs @@ -1,6 +1,10 @@ -use crate::{error, error::Error, info, warn, Config, Err}; +use figment::Figment; -pub fn check(config: &Config) -> Result<(), Error> { +use super::DEPRECATED_KEYS; +use crate::{debug, error, info, warn, Config, Err, Result}; + +#[allow(clippy::cognitive_complexity)] +pub fn check(config: &Config) -> Result<()> { #[cfg(debug_assertions)] info!("Note: conduwuit was built without optimisations (i.e. debug build)"); @@ -15,8 +19,8 @@ pub fn check(config: &Config) -> Result<(), Error> { forwards-compatible way. Please update your build script to remove this feature." ); - config.warn_deprecated(); - config.warn_unknown_key(); + warn_deprecated(config); + warn_unknown_key(config); if config.sentry && config.sentry_endpoint.is_none() { return Err!(Config("sentry_endpoint", "Sentry cannot be enabled without an endpoint set")); @@ -183,3 +187,52 @@ For security and safety reasons, conduwuit will shut down. If you are extra sure Ok(()) } + +/// Iterates over all the keys in the config file and warns if there is a +/// deprecated key specified +fn warn_deprecated(config: &Config) { + debug!("Checking for deprecated config keys"); + let mut was_deprecated = false; + for key in config + .catchall + .keys() + .filter(|key| DEPRECATED_KEYS.iter().any(|s| s == key)) + { + warn!("Config parameter \"{}\" is deprecated, ignoring.", key); + was_deprecated = true; + } + + if was_deprecated { + warn!( + "Read conduwuit config documentation at https://conduwuit.puppyirl.gay/configuration.html and check your \ + configuration if any new configuration parameters should be adjusted" + ); + } +} + +/// iterates over all the catchall keys (unknown config options) and warns +/// if there are any. +fn warn_unknown_key(config: &Config) { + debug!("Checking for unknown config keys"); + for key in config + .catchall + .keys() + .filter(|key| "config".to_owned().ne(key.to_owned()) /* "config" is expected */) + { + warn!("Config parameter \"{}\" is unknown to conduwuit, ignoring.", key); + } +} + +/// Checks the presence of the `address` and `unix_socket_path` keys in the +/// raw_config, exiting the process if both keys were detected. +pub(super) fn is_dual_listening(raw_config: &Figment) -> Result<()> { + let contains_address = raw_config.contains("address"); + let contains_unix_socket = raw_config.contains("unix_socket_path"); + if contains_address && contains_unix_socket { + return Err!( + "TOML keys \"address\" and \"unix_socket_path\" were both defined. Please specify only one option." + ); + } + + Ok(()) +} diff --git a/src/core/config/mod.rs b/src/core/config/mod.rs index d12e7780..16c7b1a8 100644 --- a/src/core/config/mod.rs +++ b/src/core/config/mod.rs @@ -19,30 +19,15 @@ use ruma::{ api::client::discovery::discover_support::ContactRole, OwnedRoomId, OwnedServerName, OwnedUserId, RoomVersionId, }; use serde::{de::IgnoredAny, Deserialize}; -use tracing::{debug, error, warn}; use url::Url; pub use self::check::check; use self::proxy::ProxyConfig; -use crate::{error::Error, Err}; +use crate::{error::Error, Err, Result}; pub mod check; pub mod proxy; -#[derive(Deserialize, Clone, Debug)] -#[serde(transparent)] -struct ListeningPort { - #[serde(with = "either::serde_untagged")] - ports: Either>, -} - -#[derive(Deserialize, Clone, Debug)] -#[serde(transparent)] -struct ListeningAddr { - #[serde(with = "either::serde_untagged")] - addrs: Either>, -} - /// all the config options for conduwuit #[derive(Clone, Debug, Deserialize)] #[allow(clippy::struct_excessive_bools)] @@ -397,6 +382,20 @@ pub struct WellKnownConfig { pub support_mxid: Option, } +#[derive(Deserialize, Clone, Debug)] +#[serde(transparent)] +struct ListeningPort { + #[serde(with = "either::serde_untagged")] + ports: Either>, +} + +#[derive(Deserialize, Clone, Debug)] +#[serde(transparent)] +struct ListeningAddr { + #[serde(with = "either::serde_untagged")] + addrs: Either>, +} + const DEPRECATED_KEYS: &[&str] = &[ "cache_capacity", "max_concurrent_requests", @@ -410,7 +409,7 @@ const DEPRECATED_KEYS: &[&str] = &[ impl Config { /// Initialize config - pub fn new(path: Option) -> Result { + pub fn new(path: Option) -> Result { let raw_config = if let Some(config_file_env) = Env::var("CONDUIT_CONFIG") { Figment::new() .merge(Toml::file(config_file_env).nested()) @@ -438,64 +437,11 @@ impl Config { }; // don't start if we're listening on both UNIX sockets and TCP at same time - if Self::is_dual_listening(&raw_config) { - return Err!(Config("address", "dual listening on UNIX and TCP sockets not allowed.")); - }; + check::is_dual_listening(&raw_config)?; Ok(config) } - /// Iterates over all the keys in the config file and warns if there is a - /// deprecated key specified - pub(crate) fn warn_deprecated(&self) { - debug!("Checking for deprecated config keys"); - let mut was_deprecated = false; - for key in self - .catchall - .keys() - .filter(|key| DEPRECATED_KEYS.iter().any(|s| s == key)) - { - warn!("Config parameter \"{}\" is deprecated, ignoring.", key); - was_deprecated = true; - } - - if was_deprecated { - warn!( - "Read conduwuit config documentation at https://conduwuit.puppyirl.gay/configuration.html and check \ - your configuration if any new configuration parameters should be adjusted" - ); - } - } - - /// iterates over all the catchall keys (unknown config options) and warns - /// if there are any. - pub(crate) fn warn_unknown_key(&self) { - debug!("Checking for unknown config keys"); - for key in self - .catchall - .keys() - .filter(|key| "config".to_owned().ne(key.to_owned()) /* "config" is expected */) - { - warn!("Config parameter \"{}\" is unknown to conduwuit, ignoring.", key); - } - } - - /// Checks the presence of the `address` and `unix_socket_path` keys in the - /// raw_config, exiting the process if both keys were detected. - fn is_dual_listening(raw_config: &Figment) -> bool { - let check_address = raw_config.find_value("address"); - let check_unix_socket = raw_config.find_value("unix_socket_path"); - - // are the check_address and check_unix_socket keys both Ok (specified) at the - // same time? - if check_address.is_ok() && check_unix_socket.is_ok() { - error!("TOML keys \"address\" and \"unix_socket_path\" were both defined. Please specify only one option."); - return true; - } - - false - } - #[must_use] pub fn get_bind_addrs(&self) -> Vec { let mut addrs = Vec::new();