public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Lukas Wagner <l.wagner@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox-backup v3 03/10] notifications: add type for ACME notification template data
Date: Fri, 28 Mar 2025 11:22:35 +0100	[thread overview]
Message-ID: <20250328102242.75539-4-l.wagner@proxmox.com> (raw)
In-Reply-To: <20250328102242.75539-1-l.wagner@proxmox.com>

This commit adds a separate type for the data passed to this type of
notification template. Also we make sure that we do not expose any
non-primitive types to the template renderer, any data
needed in the template is mapped into the new dedicated
template data type.
This ensures that any changes in types defined in other places
do not leak into the template rendering process by accident.

This commit also tries to unify the style and naming of template
variables.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
Reviewed-by: Maximiliano Sandoval <m.sandoval@proxmox.com>
---
 src/server/notifications/mod.rs           | 24 ++++++++++++-----------
 src/server/notifications/template_data.rs | 11 +++++++++++
 templates/default/acme-err-body.txt.hbs   |  2 +-
 3 files changed, 25 insertions(+), 12 deletions(-)

diff --git a/src/server/notifications/mod.rs b/src/server/notifications/mod.rs
index 3d467b85..a2730d71 100644
--- a/src/server/notifications/mod.rs
+++ b/src/server/notifications/mod.rs
@@ -23,7 +23,7 @@ const SPOOL_DIR: &str = concatcp!(pbs_buildcfg::PROXMOX_BACKUP_STATE_DIR, "/noti
 
 mod template_data;
 
-use template_data::{GcErrTemplateData, GcOkTemplateData};
+use template_data::{AcmeErrTemplateData, CommonData, GcErrTemplateData, GcOkTemplateData};
 
 /// Initialize the notification system by setting context in proxmox_notify
 pub fn init() -> Result<(), Error> {
@@ -489,24 +489,26 @@ pub fn send_updates_available(updates: &[&APTUpdateInfo]) -> Result<(), Error> {
 /// send email on certificate renewal failure.
 pub fn send_certificate_renewal_mail(result: &Result<(), Error>) -> Result<(), Error> {
     let error: String = match result {
-        Err(e) => e.to_string(),
+        Err(e) => format!("{e:#}"),
         _ => return Ok(()),
     };
 
-    let (fqdn, port) = get_server_url();
-
-    let data = json!({
-        "fqdn": fqdn,
-        "port": port,
-        "error": error,
-    });
-
     let metadata = HashMap::from([
         ("hostname".into(), proxmox_sys::nodename().into()),
         ("type".into(), "acme".into()),
     ]);
 
-    let notification = Notification::from_template(Severity::Info, "acme-err", data, metadata);
+    let template_data = AcmeErrTemplateData {
+        common: CommonData::new(),
+        error,
+    };
+
+    let notification = Notification::from_template(
+        Severity::Info,
+        "acme-err",
+        serde_json::to_value(template_data)?,
+        metadata,
+    );
 
     send_notification(notification)?;
     Ok(())
diff --git a/src/server/notifications/template_data.rs b/src/server/notifications/template_data.rs
index 264fab44..5455cc23 100644
--- a/src/server/notifications/template_data.rs
+++ b/src/server/notifications/template_data.rs
@@ -133,3 +133,14 @@ impl GcErrTemplateData {
         }
     }
 }
+
+/// Template data for the acme-err template.
+#[derive(Serialize)]
+#[serde(rename_all = "kebab-case")]
+pub struct AcmeErrTemplateData {
+    /// Common properties.
+    #[serde(flatten)]
+    pub common: CommonData,
+    /// The error that occured when trying to request the certificate.
+    pub error: String,
+}
diff --git a/templates/default/acme-err-body.txt.hbs b/templates/default/acme-err-body.txt.hbs
index 3cbfea4a..b9f52a25 100644
--- a/templates/default/acme-err-body.txt.hbs
+++ b/templates/default/acme-err-body.txt.hbs
@@ -4,4 +4,4 @@ Error: {{error}}
 
 Please visit the web interface for further details:
 
-<https://{{fqdn}}:{{port}}/#pbsCertificateConfiguration>
+<{{base-url}}/#pbsCertificateConfiguration>
-- 
2.39.5



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


  parent reply	other threads:[~2025-03-28 10:22 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-28 10:22 [pbs-devel] [PATCH proxmox-backup v3 00/10] notifications: cleanup in preparation of overridable templates Lukas Wagner
2025-03-28 10:22 ` [pbs-devel] [PATCH proxmox-backup v3 01/10] notifications: move make notifications module a dir-style module Lukas Wagner
2025-03-28 10:22 ` [pbs-devel] [PATCH proxmox-backup v3 02/10] notifications: add type for GC notification template data Lukas Wagner
2025-03-28 10:22 ` Lukas Wagner [this message]
2025-03-28 10:22 ` [pbs-devel] [PATCH proxmox-backup v3 04/10] notifications: add type for APT " Lukas Wagner
2025-03-28 10:22 ` [pbs-devel] [PATCH proxmox-backup v3 05/10] notifications: add type for prune " Lukas Wagner
2025-03-28 10:22 ` [pbs-devel] [PATCH proxmox-backup v3 06/10] notifications: add type for sync " Lukas Wagner
2025-03-28 10:22 ` [pbs-devel] [PATCH proxmox-backup v3 07/10] notifications: add type for tape backup " Lukas Wagner
2025-03-28 10:22 ` [pbs-devel] [PATCH proxmox-backup v3 08/10] notifications: add type for tape load " Lukas Wagner
2025-03-28 10:22 ` [pbs-devel] [PATCH proxmox-backup v3 09/10] notifications: add type for verify " Lukas Wagner
2025-03-28 10:22 ` [pbs-devel] [PATCH proxmox-backup v3 10/10] notifications: remove HTML template for test notification Lukas Wagner
2025-04-02 12:45 ` [pbs-devel] applied-series: [PATCH proxmox-backup v3 00/10] notifications: cleanup in preparation of overridable templates Thomas Lamprecht
2025-04-02 13:27   ` Lukas Wagner
2025-04-02 14:33     ` Thomas Lamprecht
2025-04-03 10:50       ` Lukas Wagner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250328102242.75539-4-l.wagner@proxmox.com \
    --to=l.wagner@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal