do not forward on PBS systems if co-installed with PVE

With the newly built-in targets/matchers, we should not add
a target/matcher manually any more. In fact, this broke mail
forwarding on PBS because 'default-matcher' already existed as a
built-in and could thus not be created.
We now simply do an early return.

Also initialize notify-context before instantiating the config,
since that already requires the context to be set.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
This commit is contained in:
Lukas Wagner 2023-11-21 16:53:03 +01:00 committed by Thomas Lamprecht
parent 0fd9c187e7
commit 0451898c05

View File

@ -25,8 +25,6 @@ use anyhow::Error;
use proxmox_notify::context::pbs::PBS_CONTEXT; use proxmox_notify::context::pbs::PBS_CONTEXT;
use proxmox_notify::context::pve::PVE_CONTEXT; use proxmox_notify::context::pve::PVE_CONTEXT;
use proxmox_notify::endpoints::sendmail::SendmailConfig;
use proxmox_notify::matcher::MatcherConfig;
use proxmox_notify::Config; use proxmox_notify::Config;
use proxmox_sys::fs; use proxmox_sys::fs;
@ -74,48 +72,40 @@ fn forward_common(mail: &[u8], config: &Config) -> Result<(), Error> {
/// Forward a mail to PVE's notification system /// Forward a mail to PVE's notification system
fn forward_for_pve(mail: &[u8]) -> Result<(), Error> { fn forward_for_pve(mail: &[u8]) -> Result<(), Error> {
proxmox_notify::context::set_context(&PVE_CONTEXT);
let config = attempt_file_read(PVE_PUB_NOTIFICATION_CFG_FILENAME).unwrap_or_default(); let config = attempt_file_read(PVE_PUB_NOTIFICATION_CFG_FILENAME).unwrap_or_default();
let priv_config = attempt_file_read(PVE_PRIV_NOTIFICATION_CFG_FILENAME).unwrap_or_default(); let priv_config = attempt_file_read(PVE_PRIV_NOTIFICATION_CFG_FILENAME).unwrap_or_default();
let config = Config::new(&config, &priv_config)?; let config = Config::new(&config, &priv_config)?;
proxmox_notify::context::set_context(&PVE_CONTEXT);
forward_common(mail, &config) forward_common(mail, &config)
} }
/// Forward a mail to PBS's notification system /// Forward a mail to PBS's notification system
fn forward_for_pbs(mail: &[u8], has_pve: bool) -> Result<(), Error> { fn forward_for_pbs(mail: &[u8], has_pve: bool) -> Result<(), Error> {
proxmox_notify::context::set_context(&PBS_CONTEXT);
let config = if Path::new(PBS_PUB_NOTIFICATION_CFG_FILENAME).exists() { let config = if Path::new(PBS_PUB_NOTIFICATION_CFG_FILENAME).exists() {
let config = attempt_file_read(PBS_PUB_NOTIFICATION_CFG_FILENAME).unwrap_or_default(); let config = attempt_file_read(PBS_PUB_NOTIFICATION_CFG_FILENAME).unwrap_or_default();
let priv_config = attempt_file_read(PBS_PRIV_NOTIFICATION_CFG_FILENAME).unwrap_or_default(); let priv_config = attempt_file_read(PBS_PRIV_NOTIFICATION_CFG_FILENAME).unwrap_or_default();
Config::new(&config, &priv_config)? Config::new(&config, &priv_config)?
} else { } else {
// TODO: This can be removed once PBS has full notification integration // Instantiate empty config.
let mut config = Config::new("", "")?; // Note: This will contain the default built-in targets/matchers.
if !has_pve { let config = Config::new("", "")?;
proxmox_notify::api::sendmail::add_endpoint( if has_pve {
&mut config, // Skip forwarding if we are co-installed with PVE AND
&SendmailConfig { // we do not have our own notifications.cfg file yet
name: "default-target".to_string(), // --> We assume that PVE has a sane matcher configured that
mailto_user: Some(vec!["root@pam".to_string()]), // forwards the mail properly
..Default::default() // TODO: This can be removed once PBS has full notification integration
},
)?;
proxmox_notify::api::matcher::add_matcher( return Ok(());
&mut config,
&MatcherConfig {
name: "default-matcher".to_string(),
target: Some(vec!["default-target".to_string()]),
..Default::default()
},
)?;
} }
config config
}; };
proxmox_notify::context::set_context(&PBS_CONTEXT);
forward_common(mail, &config)?; forward_common(mail, &config)?;
Ok(()) Ok(())