From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id 89F2B1FF0C0 for ; Wed, 18 Nov 2026 10:49:18 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id D319421B6E; Wed, 18 Nov 2026 10:45:04 +0100 (CET) From: Lukas Wagner To: pve-devel@lists.proxmox.com Subject: [PATCH proxmox v2 35/63] notify: unify code paths for legacy-sendmail and regular notifications Date: Wed, 18 Nov 2026 10:43:19 +0100 Message-ID: <20261118094348.273646-36-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: 1794995039358 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.191 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: VJEHHCQAFA5MTKGOQWZXJKFS3HVEOCMN X-Message-ID-Hash: VJEHHCQAFA5MTKGOQWZXJKFS3HVEOCMN 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: In the future, we will introduce a per-product notification queue and notification history. Given these changed semantics, it is much easier if 'legacy-style', regular, and test notifications can be handled through the same code paths. This is achieved by introducing a 'recipient' field in the Notification struct. It defaults to 'matcher', which just uses the configured matchers to determine which targets shall receive the notification. The 'email' variant allows us to pass a subset of `SendmailConfig`; when sending there will be an implicitly created sendmail target that is the sole notified endpoint for this notification. Finally, there is also a 'target' mode, which allows us to deliver a notification straight to a single configured target, which will be useful to unify the code paths for test notifications. Signed-off-by: Lukas Wagner --- proxmox-notify/src/notification.rs | 40 +++++++++++++ proxmox-notify/src/send.rs | 93 ++++++++++++++++++++++-------- 2 files changed, 108 insertions(+), 25 deletions(-) diff --git a/proxmox-notify/src/notification.rs b/proxmox-notify/src/notification.rs index 06b6e67e7..b3b93b545 100644 --- a/proxmox-notify/src/notification.rs +++ b/proxmox-notify/src/notification.rs @@ -84,6 +84,36 @@ pub struct Metadata { pub(crate) additional_fields: HashMap, } +#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq)] +#[serde(rename_all = "kebab-case")] +/// Determine how the notification shall be sent. +pub enum Recipient { + /// Use matchers to determine which targets shall be notified. + /// + /// This is the default mode of operation, use this unless you have a good + /// reason not to. + #[default] + Matcher, + /// Deliver notification directly to a target, identified by its ID. + /// + /// This is useful for test notifications. + Target(String), + /// Deliver notification to an ephemeral sendmail target with given list of recipients. + /// + /// This is mainly for handling the legacy-sendmail mode in PVE and PBS. + Email(EmailRecipient), +} + +#[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq)] +#[serde(rename_all = "kebab-case")] +/// Parameters for [`Recipient::Email`]-based recipients. +pub struct EmailRecipient { + /// List of email addresses to send mails to. + pub mailto: Vec, + /// Email author + pub author: Option, +} + #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] #[serde(rename_all = "kebab-case")] /// Notification which can be sent @@ -94,6 +124,9 @@ pub struct Notification { pub(crate) metadata: Metadata, /// Unique ID pub(crate) id: Uuid, + #[serde(default)] + /// The recipient of the notification. + pub(crate) recipient: Recipient, } impl Notification { @@ -114,6 +147,7 @@ impl Notification { data: template_data, }, id: Uuid::generate(), + recipient: Recipient::Matcher, } } @@ -130,6 +164,7 @@ impl Notification { timestamp: proxmox_time::epoch_i64(), }, id: Uuid::generate(), + recipient: Recipient::Matcher, }) } @@ -157,6 +192,11 @@ impl Notification { pub fn fields_mut(&mut self) -> &mut HashMap { &mut self.metadata.additional_fields } + + /// Return the recipient of the notification. + pub fn recipient(&self) -> &Recipient { + &self.recipient + } } #[cfg(test)] diff --git a/proxmox-notify/src/send.rs b/proxmox-notify/src/send.rs index 210a42599..f05661367 100644 --- a/proxmox-notify/src/send.rs +++ b/proxmox-notify/src/send.rs @@ -5,14 +5,16 @@ use proxmox_uuid::Uuid; use serde_json::json; use tracing::{error, info}; -use crate::api_types::MatcherConfig; +use crate::api_types::{MatcherConfig, SendmailConfig}; use crate::config::{ConfigEntity, PrivateConfigEntity}; use crate::endpoints::Endpoint; use crate::endpoints::gotify::GotifyEndpoint; use crate::endpoints::sendmail::SendmailEndpoint; use crate::endpoints::smtp::SmtpEndpoint; use crate::endpoints::webhook::WebhookEndpoint; -use crate::{Config, Content, Context, Error, Metadata, Notification, Severity}; +use crate::{ + Config, Content, Context, EmailRecipient, Error, Metadata, Notification, Recipient, Severity, +}; /// Send a notification to a given target. /// @@ -143,30 +145,70 @@ impl Bus { /// /// Any errors will not be returned but only logged. pub fn send(&self, context: &Context, notification: &Notification) { - let targets = self.check_matches(notification); - - for target in targets { - if let Some(endpoint) = self.endpoints.get(target) { - let name = endpoint.name(); - - if endpoint.disabled() { - // Skip this target if it is disabled - info!("skipping disabled target '{name}'"); - continue; - } - - match endpoint.send(context, notification) { - Ok(_) => { - info!("notified via target `{name}`"); - } - Err(e) => { - // Only log on errors, do not propagate fail to the caller. - error!("could not notify via target `{name}`: {e}"); - } - } - } else { - error!("could not notify via target '{target}', it does not exist"); + match notification.recipient() { + Recipient::Email(params) => self.send_to_email(context, notification, params), + Recipient::Target(target) => { + self.send_to_target(context, notification, target); } + Recipient::Matcher => { + let targets = self.check_matches(notification); + + for target in targets { + self.send_to_target(context, notification, target); + } + } + } + } + + fn send_to_email( + &self, + context: &Context, + notification: &Notification, + email_recipient: &EmailRecipient, + ) { + let recipient_list = email_recipient.mailto.join(", "); + + let endpoint = SendmailEndpoint { + config: SendmailConfig { + name: "legacy-sendmail".into(), + mailto: email_recipient.mailto.clone(), + author: email_recipient.author.clone(), + ..Default::default() + }, + }; + + match endpoint.send(context, notification) { + Ok(_) => { + info!("notified via `{recipient_list}`"); + } + Err(e) => { + // Only log on errors, do not propagate fail to the caller. + error!("could not notify via `{recipient_list}`: {e}"); + } + } + } + + fn send_to_target(&self, context: &Context, notification: &Notification, target: &str) { + if let Some(endpoint) = self.endpoints.get(target) { + let name = endpoint.name(); + + if endpoint.disabled() { + // Skip this target if it is disabled + info!("skipping disabled target '{name}'"); + return; + } + + match endpoint.send(context, notification) { + Ok(_) => { + info!("notified via target `{name}`"); + } + Err(e) => { + // Only log on errors, do not propagate fail to the caller. + error!("could not notify via target `{name}`: {e}"); + } + } + } else { + error!("could not notify via target '{target}', it does not exist"); } } @@ -187,6 +229,7 @@ impl Bus { data: json!({ "target": target }), }, id: Uuid::generate(), + recipient: Recipient::Target(target.into()), }; if let Some(endpoint) = self.endpoints.get(target) { -- 2.47.3