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 2CD721FF0E0 for ; Thu, 09 Jul 2026 14:00:36 +0200 (CEST) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 421E12178F; Thu, 09 Jul 2026 13:58:20 +0200 (CEST) From: Lukas Wagner To: pbs-devel@lists.proxmox.com, pve-devel@lists.proxmox.com Subject: [PATCH proxmox 14/29] notify: migrate PBS's and PVE's default matcher to expression syntax Date: Thu, 9 Jul 2026 13:57:01 +0200 Message-ID: <20260709115716.299836-15-l.wagner@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260709115716.299836-1-l.wagner@proxmox.com> References: <20260709115716.299836-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: 1783598239245 X-SPAM-LEVEL: Spam detection results: 0 DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) 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: TDO425ZMDELEVK3ZAK42ZJZ5MJFRTDN6 X-Message-ID-Hash: TDO425ZMDELEVK3ZAK42ZJZ5MJFRTDN6 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: Instead of providing the default matcher expression as a JSON blob, we programmatically construct the appropricate SectionConfigData entity. This required a change of the Context trait, now the default_config method returns &'static SectionConfigData instead of &'static str. The actual SectionConfigData entity is only constructed once and then stored in a OnceLock. A nice side-effect of this change is that the config-parsing code path is more efficient, since we don't have to deserialize the default config over and over again. Signed-off-by: Lukas Wagner --- proxmox-notify/src/context/mod.rs | 4 +- proxmox-notify/src/context/pbs.rs | 87 ++++++++++++++++++++------ proxmox-notify/src/context/pve.rs | 67 +++++++++++++++----- proxmox-notify/src/context/test.rs | 9 ++- proxmox-notify/src/lib.rs | 6 +- proxmox-notify/src/matcher/calendar.rs | 9 ++- proxmox-notify/src/matcher/field.rs | 11 +++- proxmox-notify/src/matcher/severity.rs | 10 ++- 8 files changed, 157 insertions(+), 46 deletions(-) diff --git a/proxmox-notify/src/context/mod.rs b/proxmox-notify/src/context/mod.rs index 87a2a716..70c3a567 100644 --- a/proxmox-notify/src/context/mod.rs +++ b/proxmox-notify/src/context/mod.rs @@ -1,6 +1,8 @@ use std::fmt::Debug; use std::sync::Mutex; +use proxmox_section_config::SectionConfigData; + use crate::Error; use crate::renderer::TemplateSource; @@ -24,7 +26,7 @@ pub trait Context: Send + Sync + Debug { /// Proxy configuration for the current node fn http_proxy_config(&self) -> Option; /// Return default config for built-in targets/matchers. - fn default_config(&self) -> &'static str; + fn default_config(&self) -> &'static SectionConfigData; /// Return the path of `filename` from `source` and a certain (optional) `namespace` fn lookup_template( &self, diff --git a/proxmox-notify/src/context/pbs.rs b/proxmox-notify/src/context/pbs.rs index a9121548..b2561ad1 100644 --- a/proxmox-notify/src/context/pbs.rs +++ b/proxmox-notify/src/context/pbs.rs @@ -1,14 +1,19 @@ use std::path::Path; +use std::sync::OnceLock; use serde::Deserialize; use tracing::error; use proxmox_schema::{ObjectSchema, Schema, StringSchema}; -use proxmox_section_config::{SectionConfig, SectionConfigPlugin}; +use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin}; -use crate::Error; use crate::context::{Context, common}; +use crate::endpoints::sendmail::{SENDMAIL_TYPENAME, SendmailConfig}; +use crate::matcher::field::FieldMatcher; +use crate::matcher::severity::SeverityMatcher; +use crate::matcher::{MATCHER_TYPENAME, MatcherConfig}; use crate::renderer::TemplateSource; +use crate::{Error, Severity}; const PBS_USER_CFG_FILENAME: &str = "/etc/proxmox-backup/user.cfg"; const PBS_NODE_CFG_FILENAME: &str = "/etc/proxmox-backup/node.cfg"; @@ -60,21 +65,6 @@ fn lookup_mail_address(content: &str, username: &str) -> Option { } } -const DEFAULT_CONFIG: &str = "\ -sendmail: mail-to-root - comment Send mails to root@pam's email address - mailto-user root@pam - - -matcher: default-matcher - mode all - invert-match true - match-field exact:type=prune - match-severity info - target mail-to-root - comment Route everything but successful prune job notifications to mail-to-root -"; - #[derive(Debug)] pub struct PBSContext; @@ -102,8 +92,67 @@ impl Context for PBSContext { content.and_then(|content| common::lookup_datacenter_config_key(&content, "http-proxy")) } - fn default_config(&self) -> &'static str { - DEFAULT_CONFIG + fn default_config(&self) -> &'static SectionConfigData { + static DEFAULT_CONFIG: OnceLock = OnceLock::new(); + DEFAULT_CONFIG.get_or_init(|| { + // FIXME: Long-term we want to move the trait implementation to the product, + // maybe add some nice builder to construct the default config. + // Moving this as-is to the product would expose a lot of internals + // to the product. + + let mut config = SectionConfigData::default(); + config + .set_data( + "mail-to-root", + SENDMAIL_TYPENAME, + SendmailConfig { + name: "mail-to-root".into(), + mailto_user: vec!["root@pam".into()], + comment: Some("Send mails to root@pam's email address".into()), + ..Default::default() + }, + ) + .expect("failed to set 'mail-to-root' in default config"); + + use proxmox_match_expression::{not, any_of, all_of}; + + let expression = any_of![ + not!( + FieldMatcher::Exact { + field: "type".into(), + values: vec!["prune".into()], + }.into() + ), + all_of![ + FieldMatcher::Exact { + field: "type".into(), + values: vec!["prune".into()], + }.into(), + SeverityMatcher { + severities: vec![Severity::Error, Severity::Warning], + }.into(), + ], + ]; + + let expression = serde_json::to_string(&expression) + .expect("failed serialize expression for 'default-matcher'"); + + config + .set_data( + "default-matcher", + MATCHER_TYPENAME, + MatcherConfig { + name: "default-matcher".into(), + expression: Some(expression), + target: vec!["mail-to-root".into()], + comment: Some("Route everything but successful prune job notifications to mail-to-root".into()), + ..Default::default() + }, + ) + .expect("failed to set 'default-matcher' in default config"); + + config + }) } fn lookup_template( diff --git a/proxmox-notify/src/context/pve.rs b/proxmox-notify/src/context/pve.rs index 3d9ff92e..00fab322 100644 --- a/proxmox-notify/src/context/pve.rs +++ b/proxmox-notify/src/context/pve.rs @@ -1,7 +1,15 @@ +use std::path::Path; +use std::sync::OnceLock; + +use proxmox_match_expression::Expression; +use proxmox_section_config::SectionConfigData; + use crate::Error; use crate::context::{Context, common}; +use crate::endpoints::sendmail::{SENDMAIL_TYPENAME, SendmailConfig}; +use crate::matcher::expression::NotificationMatcher; +use crate::matcher::{MATCHER_TYPENAME, MatcherConfig}; use crate::renderer::TemplateSource; -use std::path::Path; fn lookup_mail_address(content: &str, user: &str) -> Option { common::normalize_for_return(content.lines().find_map(|line| { @@ -14,18 +22,6 @@ fn lookup_mail_address(content: &str, user: &str) -> Option { })) } -const DEFAULT_CONFIG: &str = "\ -sendmail: mail-to-root - comment Send mails to root@pam's email address - mailto-user root@pam - - -matcher: default-matcher - mode all - target mail-to-root - comment Route all notifications to mail-to-root -"; - #[derive(Debug)] pub struct PVEContext; @@ -51,8 +47,49 @@ impl Context for PVEContext { content.and_then(|content| common::lookup_datacenter_config_key(&content, "http_proxy")) } - fn default_config(&self) -> &'static str { - DEFAULT_CONFIG + fn default_config(&self) -> &'static SectionConfigData { + static DEFAULT_CONFIG: OnceLock = OnceLock::new(); + DEFAULT_CONFIG.get_or_init(|| { + // FIXME: Long-term we want to move the trait implementation to the product, + // maybe add some nice builder to construct the default config. + // Moving this as-is to the product would expose a lot of internals + // to the product. + + let mut config = SectionConfigData::default(); + config + .set_data( + "mail-to-root", + SENDMAIL_TYPENAME, + SendmailConfig { + name: "mail-to-root".into(), + mailto_user: vec!["root@pam".into()], + comment: Some("Send mails to root@pam's email address".into()), + ..Default::default() + }, + ) + .expect("failed to set 'mail-to-root' in default config"); + + let expr: Expression = Expression::Constant(true); + + let expr_str = serde_json::to_string(&expr) + .expect("failed serialize expression for 'default-matcher'"); + + config + .set_data( + "default-matcher", + MATCHER_TYPENAME, + MatcherConfig { + name: "default-matcher".into(), + expression: Some(expr_str), + target: vec!["mail-to-root".into()], + comment: Some("Route all notifications to mail-to-root".into()), + ..Default::default() + }, + ) + .expect("failed to set 'default-matcher' in default config"); + + config + }) } fn lookup_template( diff --git a/proxmox-notify/src/context/test.rs b/proxmox-notify/src/context/test.rs index 22da38d3..ad8ef696 100644 --- a/proxmox-notify/src/context/test.rs +++ b/proxmox-notify/src/context/test.rs @@ -1,3 +1,7 @@ +use std::sync::OnceLock; + +use proxmox_section_config::SectionConfigData; + use crate::Error; use crate::context::Context; use crate::renderer::TemplateSource; @@ -28,8 +32,9 @@ impl Context for TestContext { None } - fn default_config(&self) -> &'static str { - "" + fn default_config(&self) -> &'static SectionConfigData { + static DEFAULT_CONFIG: OnceLock = OnceLock::new(); + DEFAULT_CONFIG.get_or_init(|| SectionConfigData::default()) } fn lookup_template( diff --git a/proxmox-notify/src/lib.rs b/proxmox-notify/src/lib.rs index 12d21edc..1d1c03cc 100644 --- a/proxmox-notify/src/lib.rs +++ b/proxmox-notify/src/lib.rs @@ -285,11 +285,7 @@ impl Config { 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()))?; + let builtin_config = context().default_config(); for (key, (builtin_typename, builtin_value)) in &builtin_config.sections { if let Some((typename, value)) = config.sections.get_mut(key) { diff --git a/proxmox-notify/src/matcher/calendar.rs b/proxmox-notify/src/matcher/calendar.rs index c6263eb5..43805840 100644 --- a/proxmox-notify/src/matcher/calendar.rs +++ b/proxmox-notify/src/matcher/calendar.rs @@ -3,9 +3,10 @@ use std::str::FromStr; use serde::{Deserialize, Serialize}; +use proxmox_match_expression::Expression; use proxmox_time::DailyDuration; -use crate::{Error, Notification}; +use crate::{Error, Notification, matcher::expression::NotificationMatcher}; use super::MatchDirective; @@ -53,6 +54,12 @@ pub struct CalendarMatcher { schedule: DailyDurationWrapper, } +impl From for Expression { + fn from(value: CalendarMatcher) -> Self { + Expression::Match(NotificationMatcher::Calendar(value)) + } +} + impl MatchDirective for CalendarMatcher { fn matches(&self, notification: &Notification) -> Result { self.schedule diff --git a/proxmox-notify/src/matcher/field.rs b/proxmox-notify/src/matcher/field.rs index fd25939c..a21b00d3 100644 --- a/proxmox-notify/src/matcher/field.rs +++ b/proxmox-notify/src/matcher/field.rs @@ -2,13 +2,14 @@ use std::{fmt, str::FromStr}; use const_format::concatcp; use regex::Regex; +use serde::{Deserialize, Serialize}; +use proxmox_match_expression::Expression; use proxmox_schema::{ ApiStringFormat, Schema, StringSchema, api_types::SAFE_ID_REGEX_STR, const_regex, }; -use serde::{Deserialize, Serialize}; -use crate::{Error, Notification}; +use crate::{Error, Notification, matcher::expression::NotificationMatcher}; use super::MatchDirective; @@ -45,6 +46,12 @@ pub enum FieldMatcher { }, } +impl From for Expression { + fn from(value: FieldMatcher) -> Self { + Expression::Match(NotificationMatcher::Field(value)) + } +} + impl MatchDirective for FieldMatcher { fn matches(&self, notification: &Notification) -> Result { Ok(match self { diff --git a/proxmox-notify/src/matcher/severity.rs b/proxmox-notify/src/matcher/severity.rs index 64472fab..a2538bcc 100644 --- a/proxmox-notify/src/matcher/severity.rs +++ b/proxmox-notify/src/matcher/severity.rs @@ -3,7 +3,9 @@ use std::str::FromStr; use serde::{Deserialize, Serialize}; -use crate::{Error, Notification, Severity}; +use proxmox_match_expression::Expression; + +use crate::{Error, Notification, Severity, matcher::expression::NotificationMatcher}; use super::MatchDirective; @@ -13,6 +15,12 @@ pub struct SeverityMatcher { pub(crate) severities: Vec, } +impl From for Expression { + fn from(value: SeverityMatcher) -> Self { + Expression::Match(NotificationMatcher::Severity(value)) + } +} + impl MatchDirective for SeverityMatcher { /// Check if this directive matches a given notification fn matches(&self, notification: &Notification) -> Result { -- 2.47.3