From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 30C981FF0C0 for ; Wed, 18 Nov 2026 10:46:22 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id A93EB2186F; Wed, 18 Nov 2026 10:44:52 +0100 (CET) From: Lukas Wagner To: pve-devel@lists.proxmox.com Subject: [PATCH proxmox v2 14/63] notify: move Config to config module Date: Wed, 18 Nov 2026 10:42:58 +0100 Message-ID: <20261118094348.273646-15-l.wagner@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261118094348.273646-1-l.wagner@proxmox.com> References: <20261118094348.273646-1-l.wagner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1794995036601 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.196 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: WLLC2HGIOCZPLAJKGMSISIIFVHMH24W4 X-Message-ID-Hash: WLLC2HGIOCZPLAJKGMSISIIFVHMH24W4 X-MailFrom: l.wagner@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Another step in cleaning up lib.rs. Took the opportunity to make `digest` a private field and add a new getter `digest`. Signed-off-by: Lukas Wagner --- proxmox-notify/src/api/gotify.rs | 2 +- proxmox-notify/src/api/matcher.rs | 12 ++-- proxmox-notify/src/api/sendmail.rs | 2 +- proxmox-notify/src/api/smtp.rs | 2 +- proxmox-notify/src/api/webhook.rs | 2 +- proxmox-notify/src/config.rs | 103 +++++++++++++++++++++++++++- proxmox-notify/src/lib.rs | 106 +---------------------------- 7 files changed, 113 insertions(+), 116 deletions(-) diff --git a/proxmox-notify/src/api/gotify.rs b/proxmox-notify/src/api/gotify.rs index fc56d250b..e0f7cf16b 100644 --- a/proxmox-notify/src/api/gotify.rs +++ b/proxmox-notify/src/api/gotify.rs @@ -234,7 +234,7 @@ mod tests { let mut config = empty_config(); add_default_gotify_endpoint(&mut config)?; - let digest = config.digest.clone(); + let digest = config.digest().clone(); update_endpoint( &mut config, diff --git a/proxmox-notify/src/api/matcher.rs b/proxmox-notify/src/api/matcher.rs index 1994cb65c..7df30c083 100644 --- a/proxmox-notify/src/api/matcher.rs +++ b/proxmox-notify/src/api/matcher.rs @@ -270,7 +270,7 @@ matcher: matcher2 let mut config = config_with_two_matchers(); - let digest = config.digest.clone(); + let digest = config.digest().clone(); update_matcher( &mut config, @@ -327,7 +327,7 @@ matcher: matcher2 fn test_matcher_update() -> Result<(), HttpError> { let mut config = config_with_two_matchers(); - let digest = config.digest.clone(); + let digest = config.digest().clone(); update_matcher( &mut config, @@ -382,7 +382,7 @@ matcher: matcher2 #[cfg(feature = "legacy-matchers")] fn test_update_matcher_mutually_exclusive_with_expression() -> Result<(), HttpError> { let mut config = config_with_two_matchers(); - let digest = config.digest.clone(); + let digest = config.digest().clone(); let proto = MatcherConfigUpdater { expression: Some(valid_expression_string()), @@ -416,7 +416,7 @@ matcher: matcher2 #[test] fn test_update_invalid_expression() -> Result<(), HttpError> { let mut config = config_with_two_matchers(); - let digest = config.digest.clone(); + let digest = config.digest().clone(); assert!( update_matcher( @@ -447,7 +447,7 @@ matcher: matcher2 #[test] fn test_update_minifies_pretty_printed_expression() -> Result<(), HttpError> { let mut config = config_with_two_matchers(); - let digest = config.digest.clone(); + let digest = config.digest().clone(); let expr = nested_expression(); let pretty = serde_json::to_string_pretty(&expr).unwrap(); @@ -476,7 +476,7 @@ matcher: matcher2 #[test] fn test_update_normalizes_expression_with_extra_whitespace() -> Result<(), HttpError> { let mut config = config_with_two_matchers(); - let digest = config.digest.clone(); + let digest = config.digest().clone(); // Same expression as `nested_expression`, but handwritten, with all sorts of // odd whitespace characters. diff --git a/proxmox-notify/src/api/sendmail.rs b/proxmox-notify/src/api/sendmail.rs index aecc69b38..cff63b806 100644 --- a/proxmox-notify/src/api/sendmail.rs +++ b/proxmox-notify/src/api/sendmail.rs @@ -230,7 +230,7 @@ pub mod tests { let mut config = empty_config(); add_sendmail_endpoint_for_test(&mut config, "sendmail-endpoint")?; - let digest = config.digest.clone(); + let digest = config.digest().clone(); update_endpoint( &mut config, diff --git a/proxmox-notify/src/api/smtp.rs b/proxmox-notify/src/api/smtp.rs index f36a51e8c..ceae77df3 100644 --- a/proxmox-notify/src/api/smtp.rs +++ b/proxmox-notify/src/api/smtp.rs @@ -289,7 +289,7 @@ pub mod tests { let mut config = empty_config(); add_smtp_endpoint_for_test(&mut config, "smtp-endpoint")?; - let digest = config.digest.clone(); + let digest = config.digest().clone(); update_endpoint( &mut config, diff --git a/proxmox-notify/src/api/webhook.rs b/proxmox-notify/src/api/webhook.rs index 8755eb291..7266faca6 100644 --- a/proxmox-notify/src/api/webhook.rs +++ b/proxmox-notify/src/api/webhook.rs @@ -351,7 +351,7 @@ mod tests { let mut config = empty_config(); add_default_webhook_endpoint(&mut config)?; - let digest = config.digest.clone(); + let digest = config.digest().clone(); update_endpoint( &mut config, diff --git a/proxmox-notify/src/config.rs b/proxmox-notify/src/config.rs index bf1469160..59da68c5e 100644 --- a/proxmox-notify/src/config.rs +++ b/proxmox-notify/src/config.rs @@ -3,14 +3,16 @@ use std::sync::OnceLock; use proxmox_config_digest::ConfigDigest; use proxmox_schema::{ApiType, ObjectSchema}; use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin}; +use serde_json::Value; +use tracing::error; -use crate::Error; use crate::api_types::{ GOTIFY_TYPENAME, GotifyConfig, GotifyPrivateConfig, MATCHER_TYPENAME, MatcherConfig, SENDMAIL_TYPENAME, SMTP_TYPENAME, SendmailConfig, SmtpConfig, SmtpPrivateConfig, WEBHOOK_TYPENAME, WebhookConfig, WebhookPrivateConfig, }; use crate::schema::BACKEND_NAME_SCHEMA; +use crate::{Context, Error}; /// Section config schema for the public config file. pub fn config_parser() -> &'static SectionConfig { @@ -126,3 +128,102 @@ pub fn write_private(config: &SectionConfigData) -> Result { .write("priv/notifications.cfg", config) .map_err(Error::config_serialization) } + +/// Notification configuration +#[derive(Debug, Clone)] +pub struct Config { + pub(crate) config: SectionConfigData, + pub(crate) private_config: SectionConfigData, + digest: ConfigDigest, +} + +impl Config { + /// Parse raw config + pub fn new( + context: &Context, + raw_config: &str, + raw_private_config: &str, + ) -> Result { + let (mut config, digest) = config(raw_config)?; + let (private_config, _) = private_config(raw_private_config)?; + + let default_config = context.default_config(); + + let builtin_config = config_parser() + .parse("", default_config) + .map_err(|err| Error::ConfigDeserialization(err.into()))?; + + for (key, (builtin_typename, builtin_value)) in &builtin_config.sections { + if let Some((typename, value)) = config.sections.get_mut(key) { + if builtin_typename == typename && value == builtin_value { + // Entry is built-in and the config entry section in notifications.cfg + // is exactly the same. + if let Some(obj) = value.as_object_mut() { + obj.insert("origin".to_string(), Value::String("builtin".into())); + } else { + error!("section config entry is not an object. This should not happen"); + } + } else { + // Entry is built-in, but it has been modified by the user. + if let Some(obj) = value.as_object_mut() { + obj.insert( + "origin".to_string(), + Value::String("modified-builtin".into()), + ); + } else { + error!("section config entry is not an object. This should not happen"); + } + } + } else { + let mut val = builtin_value.clone(); + + if let Some(obj) = val.as_object_mut() { + obj.insert("origin".to_string(), Value::String("builtin".into())); + } else { + error!("section config entry is not an object. This should not happen"); + } + config + .set_data(key, builtin_typename, val) + .map_err(|err| Error::ConfigDeserialization(err.into()))?; + } + } + + for (_, (_, value)) in config.sections.iter_mut() { + if let Some(obj) = value.as_object_mut() { + if obj.get("origin").is_none() { + obj.insert("origin".to_string(), Value::String("user-created".into())); + } + } + } + + Ok(Self { + config, + digest, + private_config, + }) + } + + /// Serialize config + pub fn write(&self) -> Result<(String, String), Error> { + let mut c = self.config.clone(); + for (_, (_, value)) in c.sections.iter_mut() { + // Remove 'origin' parameter, we do not want it in our + // config fields + // TODO: Check if there is a better way for this, maybe a + // separate type for API responses? + if let Some(obj) = value.as_object_mut() { + obj.remove("origin"); + } else { + error!("section config entry is not an object. This should not happen"); + } + } + + Ok((write(&c)?, write_private(&self.private_config)?)) + } + + /// Returns the SHA256 digest of the configuration. + /// The digest is only computed once when the configuration deserialized. + pub fn digest(&self) -> &ConfigDigest { + &self.digest + } +} diff --git a/proxmox-notify/src/lib.rs b/proxmox-notify/src/lib.rs index 8f0b962e7..4407ae80d 100644 --- a/proxmox-notify/src/lib.rs +++ b/proxmox-notify/src/lib.rs @@ -5,11 +5,8 @@ use std::{collections::HashMap, fmt::Display, str::FromStr}; use serde::{Deserialize, Serialize}; use serde_json::Value; -use tracing::error; -use proxmox_config_digest::ConfigDigest; use proxmox_schema::api; -use proxmox_section_config::SectionConfigData; use proxmox_uuid::Uuid; #[cfg(feature = "api-impl")] @@ -30,6 +27,7 @@ pub mod api_types; mod error; pub use error::Error; +pub use config::Config; pub use context::Context; pub use context::ContextImpl; @@ -187,105 +185,3 @@ impl Notification { self.metadata.timestamp } } - -/// Notification configuration -#[derive(Debug, Clone)] -pub struct Config { - config: SectionConfigData, - private_config: SectionConfigData, - digest: ConfigDigest, -} - -impl Config { - /// Parse raw config - pub fn new( - context: &Context, - raw_config: &str, - raw_private_config: &str, - ) -> Result { - let (mut config, digest) = config::config(raw_config)?; - let (private_config, _) = config::private_config(raw_private_config)?; - - let default_config = context.default_config(); - - let builtin_config = config::config_parser() - .parse("", default_config) - .map_err(|err| Error::ConfigDeserialization(err.into()))?; - - for (key, (builtin_typename, builtin_value)) in &builtin_config.sections { - if let Some((typename, value)) = config.sections.get_mut(key) { - if builtin_typename == typename && value == builtin_value { - // Entry is built-in and the config entry section in notifications.cfg - // is exactly the same. - if let Some(obj) = value.as_object_mut() { - obj.insert("origin".to_string(), Value::String("builtin".into())); - } else { - error!("section config entry is not an object. This should not happen"); - } - } else { - // Entry is built-in, but it has been modified by the user. - if let Some(obj) = value.as_object_mut() { - obj.insert( - "origin".to_string(), - Value::String("modified-builtin".into()), - ); - } else { - error!("section config entry is not an object. This should not happen"); - } - } - } else { - let mut val = builtin_value.clone(); - - if let Some(obj) = val.as_object_mut() { - obj.insert("origin".to_string(), Value::String("builtin".into())); - } else { - error!("section config entry is not an object. This should not happen"); - } - config - .set_data(key, builtin_typename, val) - .map_err(|err| Error::ConfigDeserialization(err.into()))?; - } - } - - for (_, (_, value)) in config.sections.iter_mut() { - if let Some(obj) = value.as_object_mut() { - if obj.get("origin").is_none() { - obj.insert("origin".to_string(), Value::String("user-created".into())); - } - } - } - - Ok(Self { - config, - digest, - private_config, - }) - } - - /// Serialize config - pub fn write(&self) -> Result<(String, String), Error> { - let mut c = self.config.clone(); - for (_, (_, value)) in c.sections.iter_mut() { - // Remove 'origin' parameter, we do not want it in our - // config fields - // TODO: Check if there is a better way for this, maybe a - // separate type for API responses? - if let Some(obj) = value.as_object_mut() { - obj.remove("origin"); - } else { - error!("section config entry is not an object. This should not happen"); - } - } - - Ok(( - config::write(&c)?, - config::write_private(&self.private_config)?, - )) - } - - /// Returns the SHA256 digest of the configuration. - /// The digest is only computed once when the configuration deserialized. - pub fn digest(&self) -> &ConfigDigest { - &self.digest - } -} -- 2.47.3