From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id AB3447073A for ; Tue, 14 Jun 2022 10:10:39 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 9CF111BB4C for ; Tue, 14 Jun 2022 10:10:09 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id E46B31BB42 for ; Tue, 14 Jun 2022 10:10:08 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id B56B343B1D for ; Tue, 14 Jun 2022 10:10:02 +0200 (CEST) From: Stefan Sterz To: pbs-devel@lists.proxmox.com Date: Tue, 14 Jun 2022 10:09:45 +0200 Message-Id: <20220614080945.106769-1-s.sterz@proxmox.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.058 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [certificates.rs] Subject: [pbs-devel] [PATCH proxmox-backup v3] fix #3867: server/api: send emails on certificate renewal failure X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 14 Jun 2022 08:10:39 -0000 the superuser's email will be used to notify them that certificate renewal has failed. Signed-off-by: Stefan Sterz --- changes v1 -> v2: - remove renewal notifications from `node.cfg` changes v2 -> v3: - rebase - remove notify parameter from `send_certificate_renewal_mail()` completely src/api2/node/certificates.rs | 20 ++++++++++---- src/server/email_notifications.rs | 44 +++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/src/api2/node/certificates.rs b/src/api2/node/certificates.rs index e303973a..1b9a8850 100644 --- a/src/api2/node/certificates.rs +++ b/src/api2/node/certificates.rs @@ -20,6 +20,7 @@ use pbs_tools::cert; use crate::acme::AcmeClient; use crate::api2::types::AcmeDomain; use crate::config::node::NodeConfig; +use crate::server::send_certificate_renewal_mail; use proxmox_rest_server::WorkerTask; pub const ROUTER: Router = Router::new() @@ -544,11 +545,20 @@ fn spawn_certificate_worker( let auth_id = rpcenv.get_auth_id().unwrap(); WorkerTask::spawn(name, None, auth_id, true, move |worker| async move { - if let Some(cert) = order_certificate(worker, &node_config).await? { - crate::config::set_proxy_certificate(&cert.certificate, &cert.private_key_pem)?; - crate::server::reload_proxy_certificate().await?; - } - Ok(()) + let work = || async { + if let Some(cert) = order_certificate(worker, &node_config).await? { + crate::config::set_proxy_certificate(&cert.certificate, &cert.private_key_pem)?; + crate::server::reload_proxy_certificate().await?; + } + + Ok(()) + }; + + let res = work().await; + + send_certificate_renewal_mail(&res)?; + + res }) } diff --git a/src/server/email_notifications.rs b/src/server/email_notifications.rs index 4534cd38..38d4fb47 100644 --- a/src/server/email_notifications.rs +++ b/src/server/email_notifications.rs @@ -183,6 +183,18 @@ Please visit the web interface for further details: "###; +const ACME_CERTIFICATE_ERR_RENEWAL: &str = r###" + +Proxmox Backup Server was not able to renew a TLS certificate. + +Error: {{error}} + +Please visit the web interface for further details: + + + +"###; + lazy_static::lazy_static! { static ref HANDLEBARS: Handlebars<'static> = { @@ -209,6 +221,8 @@ lazy_static::lazy_static! { hb.register_template_string("package_update_template", PACKAGE_UPDATES_TEMPLATE)?; + hb.register_template_string("certificate_renewal_err_template", ACME_CERTIFICATE_ERR_RENEWAL)?; + Ok(()) }); @@ -507,6 +521,34 @@ pub fn send_updates_available(updates: &[&APTUpdateInfo]) -> Result<(), Error> { Ok(()) } +/// send email on certificate renewal failure. +/// `notify` currently only accepts `Notify::Error`. +pub fn send_certificate_renewal_mail(result: &Result<(), Error>) -> Result<(), Error> { + let error: String = match result { + Err(e) => e.to_string().into(), + _ => return Ok(()), + }; + + if let Some(email) = lookup_user_email(Userid::root_userid()) { + let (fqdn, port) = get_server_url(); + + let text = HANDLEBARS.render( + "certificate_renewal_err_template", + &json!({ + "fqdn": fqdn, + "port": port, + "error": error, + }), + )?; + + let subject = "Could not renew certificate"; + + send_job_status_mail(&email, subject, &text)?; + } + + Ok(()) +} + /// Lookup users email address pub fn lookup_user_email(userid: &Userid) -> Option { if let Ok(user_config) = pbs_config::user::cached_config() { @@ -618,4 +660,6 @@ fn test_template_register() { assert!(HANDLEBARS.has_template("tape_backup_err_template")); assert!(HANDLEBARS.has_template("package_update_template")); + + assert!(HANDLEBARS.has_template("certificate_renewal_err_template")); } -- 2.30.2