forked from proxmox-mirrors/proxmox
notify: factor out notification content into its own type
This will be useful later for system mail forwarding, where the content of the mail should be forwarded unchanged. This moves notification properties into this new type and calls them 'data'. They will exclusively used for template rendering. `Notification` will receive a separate field for metadata, which will be useful for notification filtering. This decouples template rendering and filtering, which enables us to be very precise about which metadata fields we allow to be used in filters. Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
This commit is contained in:
parent
b2089c37c5
commit
df4858e989
@ -53,10 +53,10 @@ fn main() -> Result<(), Error> {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let output = render_template(TemplateRenderer::Html, TEMPLATE, Some(&properties))?;
|
let output = render_template(TemplateRenderer::Html, TEMPLATE, &properties)?;
|
||||||
println!("{output}");
|
println!("{output}");
|
||||||
|
|
||||||
let output = render_template(TemplateRenderer::Plaintext, TEMPLATE, Some(&properties))?;
|
let output = render_template(TemplateRenderer::Plaintext, TEMPLATE, &properties)?;
|
||||||
println!("{output}");
|
println!("{output}");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -11,7 +11,7 @@ use proxmox_schema::{api, Updater};
|
|||||||
use crate::context::context;
|
use crate::context::context;
|
||||||
use crate::renderer::TemplateRenderer;
|
use crate::renderer::TemplateRenderer;
|
||||||
use crate::schema::ENTITY_NAME_SCHEMA;
|
use crate::schema::ENTITY_NAME_SCHEMA;
|
||||||
use crate::{renderer, Endpoint, Error, Notification, Severity};
|
use crate::{renderer, Content, Endpoint, Error, Notification, Severity};
|
||||||
|
|
||||||
fn severity_to_priority(level: Severity) -> u32 {
|
fn severity_to_priority(level: Severity) -> u32 {
|
||||||
match level {
|
match level {
|
||||||
@ -85,15 +85,21 @@ pub enum DeleteableGotifyProperty {
|
|||||||
|
|
||||||
impl Endpoint for GotifyEndpoint {
|
impl Endpoint for GotifyEndpoint {
|
||||||
fn send(&self, notification: &Notification) -> Result<(), Error> {
|
fn send(&self, notification: &Notification) -> Result<(), Error> {
|
||||||
let properties = notification.properties.as_ref();
|
|
||||||
|
|
||||||
let title = renderer::render_template(
|
let (title, message) = match ¬ification.content {
|
||||||
TemplateRenderer::Plaintext,
|
Content::Template {
|
||||||
¬ification.title,
|
title_template,
|
||||||
properties,
|
body_template,
|
||||||
)?;
|
data
|
||||||
let message =
|
} => {
|
||||||
renderer::render_template(TemplateRenderer::Plaintext, ¬ification.body, properties)?;
|
let rendered_title =
|
||||||
|
renderer::render_template(TemplateRenderer::Plaintext, title_template, data)?;
|
||||||
|
let rendered_message =
|
||||||
|
renderer::render_template(TemplateRenderer::Plaintext, body_template, data)?;
|
||||||
|
|
||||||
|
(rendered_title, rendered_message)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// We don't have a TemplateRenderer::Markdown yet, so simply put everything
|
// We don't have a TemplateRenderer::Markdown yet, so simply put everything
|
||||||
// in code tags. Otherwise tables etc. are not formatted properly
|
// in code tags. Otherwise tables etc. are not formatted properly
|
||||||
|
@ -8,7 +8,7 @@ use proxmox_schema::{api, Updater};
|
|||||||
use crate::context::context;
|
use crate::context::context;
|
||||||
use crate::renderer::TemplateRenderer;
|
use crate::renderer::TemplateRenderer;
|
||||||
use crate::schema::{EMAIL_SCHEMA, ENTITY_NAME_SCHEMA, USER_SCHEMA};
|
use crate::schema::{EMAIL_SCHEMA, ENTITY_NAME_SCHEMA, USER_SCHEMA};
|
||||||
use crate::{renderer, Endpoint, Error, Notification};
|
use crate::{renderer, Content, Endpoint, Error, Notification};
|
||||||
|
|
||||||
pub(crate) const SENDMAIL_TYPENAME: &str = "sendmail";
|
pub(crate) const SENDMAIL_TYPENAME: &str = "sendmail";
|
||||||
|
|
||||||
@ -102,41 +102,43 @@ impl Endpoint for SendmailEndpoint {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let properties = notification.properties.as_ref();
|
let recipients_str: Vec<&str> = recipients.iter().map(String::as_str).collect();
|
||||||
|
|
||||||
let subject = renderer::render_template(
|
|
||||||
TemplateRenderer::Plaintext,
|
|
||||||
¬ification.title,
|
|
||||||
properties,
|
|
||||||
)?;
|
|
||||||
let html_part =
|
|
||||||
renderer::render_template(TemplateRenderer::Html, ¬ification.body, properties)?;
|
|
||||||
let text_part =
|
|
||||||
renderer::render_template(TemplateRenderer::Plaintext, ¬ification.body, properties)?;
|
|
||||||
|
|
||||||
let author = self
|
|
||||||
.config
|
|
||||||
.author
|
|
||||||
.clone()
|
|
||||||
.unwrap_or_else(|| context().default_sendmail_author());
|
|
||||||
|
|
||||||
let mailfrom = self
|
let mailfrom = self
|
||||||
.config
|
.config
|
||||||
.from_address
|
.from_address
|
||||||
.clone()
|
.clone()
|
||||||
.unwrap_or_else(|| context().default_sendmail_from());
|
.unwrap_or_else(|| context().default_sendmail_from());
|
||||||
|
|
||||||
let recipients_str: Vec<&str> = recipients.iter().map(String::as_str).collect();
|
match ¬ification.content {
|
||||||
|
Content::Template {
|
||||||
|
title_template,
|
||||||
|
body_template,
|
||||||
|
data,
|
||||||
|
} => {
|
||||||
|
let subject =
|
||||||
|
renderer::render_template(TemplateRenderer::Plaintext, title_template, data)?;
|
||||||
|
let html_part =
|
||||||
|
renderer::render_template(TemplateRenderer::Html, body_template, data)?;
|
||||||
|
let text_part =
|
||||||
|
renderer::render_template(TemplateRenderer::Plaintext, body_template, data)?;
|
||||||
|
|
||||||
proxmox_sys::email::sendmail(
|
let author = self
|
||||||
&recipients_str,
|
.config
|
||||||
&subject,
|
.author
|
||||||
Some(&text_part),
|
.clone()
|
||||||
Some(&html_part),
|
.unwrap_or_else(|| context().default_sendmail_author());
|
||||||
Some(&mailfrom),
|
|
||||||
Some(&author),
|
proxmox_sys::email::sendmail(
|
||||||
)
|
&recipients_str,
|
||||||
.map_err(|err| Error::NotifyFailed(self.config.name.clone(), err.into()))
|
&subject,
|
||||||
|
Some(&text_part),
|
||||||
|
Some(&html_part),
|
||||||
|
Some(&mailfrom),
|
||||||
|
Some(&author),
|
||||||
|
)
|
||||||
|
.map_err(|err| Error::NotifyFailed(self.config.name.clone(), err.into()))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn name(&self) -> &str {
|
fn name(&self) -> &str {
|
||||||
|
@ -160,7 +160,7 @@ impl<'a> FilterMatcher<'a> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::config;
|
use crate::{config, Content};
|
||||||
|
|
||||||
fn parse_filters(config: &str) -> Result<Vec<FilterConfig>, Error> {
|
fn parse_filters(config: &str) -> Result<Vec<FilterConfig>, Error> {
|
||||||
let (config, _) = config::config(config)?;
|
let (config, _) = config::config(config)?;
|
||||||
@ -169,10 +169,12 @@ mod tests {
|
|||||||
|
|
||||||
fn empty_notification_with_severity(severity: Severity) -> Notification {
|
fn empty_notification_with_severity(severity: Severity) -> Notification {
|
||||||
Notification {
|
Notification {
|
||||||
title: String::new(),
|
content: Content::Template {
|
||||||
body: String::new(),
|
title_template: String::new(),
|
||||||
|
body_template: String::new(),
|
||||||
|
data: Default::default(),
|
||||||
|
},
|
||||||
severity,
|
severity,
|
||||||
properties: Default::default(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,17 +116,44 @@ pub trait Endpoint {
|
|||||||
fn filter(&self) -> Option<&str>;
|
fn filter(&self) -> Option<&str>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub enum Content {
|
||||||
|
/// Title and body will be rendered as a template
|
||||||
|
Template {
|
||||||
|
/// Template for the notification title.
|
||||||
|
title_template: String,
|
||||||
|
/// Template for the notification body.
|
||||||
|
body_template: String,
|
||||||
|
/// Data that can be used for template rendering.
|
||||||
|
data: Value,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
/// Notification which can be sent
|
/// Notification which can be sent
|
||||||
pub struct Notification {
|
pub struct Notification {
|
||||||
/// Notification severity
|
/// Notification severity
|
||||||
pub severity: Severity,
|
severity: Severity,
|
||||||
/// The title of the notification
|
/// Notification content
|
||||||
pub title: String,
|
content: Content,
|
||||||
/// Notification text
|
}
|
||||||
pub body: String,
|
|
||||||
/// Additional metadata for the notification
|
impl Notification {
|
||||||
pub properties: Option<Value>,
|
pub fn new_templated<S: AsRef<str>>(
|
||||||
|
severity: Severity,
|
||||||
|
title: S,
|
||||||
|
body: S,
|
||||||
|
properties: Value,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
severity,
|
||||||
|
content: Content::Template {
|
||||||
|
title_template: title.as_ref().to_string(),
|
||||||
|
body_template: body.as_ref().to_string(),
|
||||||
|
data: properties,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Notification configuration
|
/// Notification configuration
|
||||||
@ -384,9 +411,11 @@ impl Bus {
|
|||||||
pub fn test_target(&self, target: &str) -> Result<(), Error> {
|
pub fn test_target(&self, target: &str) -> Result<(), Error> {
|
||||||
let notification = Notification {
|
let notification = Notification {
|
||||||
severity: Severity::Info,
|
severity: Severity::Info,
|
||||||
title: "Test notification".into(),
|
content: Content::Template {
|
||||||
body: "This is a test of the notification target '{{ target }}'".into(),
|
title_template: "Test notification".into(),
|
||||||
properties: Some(json!({ "target": target })),
|
body_template: "This is a test of the notification target '{{ target }}'".into(),
|
||||||
|
data: json!({ "target": target }),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut errors: Vec<Box<dyn StdError + Send + Sync>> = Vec::new();
|
let mut errors: Vec<Box<dyn StdError + Send + Sync>> = Vec::new();
|
||||||
@ -473,12 +502,7 @@ mod tests {
|
|||||||
// Send directly to endpoint
|
// Send directly to endpoint
|
||||||
bus.send(
|
bus.send(
|
||||||
"endpoint",
|
"endpoint",
|
||||||
&Notification {
|
&Notification::new_templated(Severity::Info, "Title", "Body", Default::default()),
|
||||||
title: "Title".into(),
|
|
||||||
body: "Body".into(),
|
|
||||||
severity: Severity::Info,
|
|
||||||
properties: Default::default(),
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
let messages = mock.messages();
|
let messages = mock.messages();
|
||||||
assert_eq!(messages.len(), 1);
|
assert_eq!(messages.len(), 1);
|
||||||
@ -511,15 +535,9 @@ mod tests {
|
|||||||
bus.add_endpoint(Box::new(endpoint2.clone()));
|
bus.add_endpoint(Box::new(endpoint2.clone()));
|
||||||
|
|
||||||
let send_to_group = |channel| {
|
let send_to_group = |channel| {
|
||||||
bus.send(
|
let notification =
|
||||||
channel,
|
Notification::new_templated(Severity::Info, "Title", "Body", Default::default());
|
||||||
&Notification {
|
bus.send(channel, ¬ification)
|
||||||
title: "Title".into(),
|
|
||||||
body: "Body".into(),
|
|
||||||
severity: Severity::Info,
|
|
||||||
properties: Default::default(),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
send_to_group("group1");
|
send_to_group("group1");
|
||||||
@ -579,15 +597,10 @@ mod tests {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let send_with_severity = |severity| {
|
let send_with_severity = |severity| {
|
||||||
bus.send(
|
let notification =
|
||||||
"channel1",
|
Notification::new_templated(severity, "Title", "Body", Default::default());
|
||||||
&Notification {
|
|
||||||
title: "Title".into(),
|
bus.send("channel1", ¬ification);
|
||||||
body: "Body".into(),
|
|
||||||
severity,
|
|
||||||
properties: Default::default(),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
send_with_severity(Severity::Info);
|
send_with_severity(Severity::Info);
|
||||||
|
@ -228,11 +228,9 @@ impl BlockRenderFunctions {
|
|||||||
|
|
||||||
fn render_template_impl(
|
fn render_template_impl(
|
||||||
template: &str,
|
template: &str,
|
||||||
properties: Option<&Value>,
|
data: &Value,
|
||||||
renderer: TemplateRenderer,
|
renderer: TemplateRenderer,
|
||||||
) -> Result<String, Error> {
|
) -> Result<String, Error> {
|
||||||
let properties = properties.unwrap_or(&Value::Null);
|
|
||||||
|
|
||||||
let mut handlebars = Handlebars::new();
|
let mut handlebars = Handlebars::new();
|
||||||
handlebars.register_escape_fn(renderer.escape_fn());
|
handlebars.register_escape_fn(renderer.escape_fn());
|
||||||
|
|
||||||
@ -242,7 +240,7 @@ fn render_template_impl(
|
|||||||
ValueRenderFunction::register_helpers(&mut handlebars);
|
ValueRenderFunction::register_helpers(&mut handlebars);
|
||||||
|
|
||||||
let rendered_template = handlebars
|
let rendered_template = handlebars
|
||||||
.render_template(template, properties)
|
.render_template(template, data)
|
||||||
.map_err(|err| Error::RenderError(err.into()))?;
|
.map_err(|err| Error::RenderError(err.into()))?;
|
||||||
|
|
||||||
Ok(rendered_template)
|
Ok(rendered_template)
|
||||||
@ -255,11 +253,11 @@ fn render_template_impl(
|
|||||||
pub fn render_template(
|
pub fn render_template(
|
||||||
renderer: TemplateRenderer,
|
renderer: TemplateRenderer,
|
||||||
template: &str,
|
template: &str,
|
||||||
properties: Option<&Value>,
|
data: &Value,
|
||||||
) -> Result<String, Error> {
|
) -> Result<String, Error> {
|
||||||
let mut rendered_template = String::from(renderer.prefix());
|
let mut rendered_template = String::from(renderer.prefix());
|
||||||
|
|
||||||
rendered_template.push_str(&render_template_impl(template, properties, renderer)?);
|
rendered_template.push_str(&render_template_impl(template, data, renderer)?);
|
||||||
rendered_template.push_str(renderer.postfix());
|
rendered_template.push_str(renderer.postfix());
|
||||||
|
|
||||||
Ok(rendered_template)
|
Ok(rendered_template)
|
||||||
@ -314,7 +312,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_render_template() -> Result<(), Error> {
|
fn test_render_template() -> Result<(), Error> {
|
||||||
let properties = json!({
|
let data = json!({
|
||||||
"dur": 12345,
|
"dur": 12345,
|
||||||
"size": 1024 * 15,
|
"size": 1024 * 15,
|
||||||
|
|
||||||
@ -370,8 +368,7 @@ val1 val2
|
|||||||
val3 val4
|
val3 val4
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
let rendered_plaintext =
|
let rendered_plaintext = render_template(TemplateRenderer::Plaintext, template, &data)?;
|
||||||
render_template(TemplateRenderer::Plaintext, template, Some(&properties))?;
|
|
||||||
|
|
||||||
// Let's not bother about testing the HTML output, too fragile.
|
// Let's not bother about testing the HTML output, too fragile.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user