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 9A0EE1FF0C0 for ; Wed, 21 Oct 2026 11:53:16 +0200 (CEST) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 438B4216F9; Wed, 21 Oct 2026 11:52:57 +0200 (CEST) From: David Riley To: pve-devel@lists.proxmox.com Subject: [PATCH pve-common v3 2/4] certificate: add helper for lifetime-based ACME renewal lead time Date: Wed, 21 Oct 2026 11:52:27 +0200 Message-ID: <20261021095229.50842-3-d.riley@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261021095229.50842-1-d.riley@proxmox.com> References: <20261021095229.50842-1-d.riley@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1792576368983 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.667 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: YRZDIND5HMKZRQW6TWI57VPWNJSKU74L X-Message-ID-Hash: YRZDIND5HMKZRQW6TWI57VPWNJSKU74L X-MailFrom: d.riley@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 CC: David Riley X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Add helper to calculate renewal lead time dynamically based on the certificate's actual validity period. The scaling mirrors the logic already used in Proxmox Backup Server [0]: certificates living for less than 10 days are renewed once 1/2 of their lifetime has elapsed, while longer-lived certificates are renewed once 2/3 has elapsed [1]. A 3-day minimum floor is present to ensure the daily update service has multiple chances to retry transient failures. [0] https://lore.proxmox.com/pbs-devel/20260423134607.105229-2-m.federanko@proxmox.com/ [1] https://letsencrypt.org/docs/integration-guide/#when-to-renew Signed-off-by: David Riley Reviewed-by: Michael Köppl Tested-by: Michael Köppl --- src/PVE/Certificate.pm | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/PVE/Certificate.pm b/src/PVE/Certificate.pm index 8406949..d2d0847 100644 --- a/src/PVE/Certificate.pm +++ b/src/PVE/Certificate.pm @@ -5,6 +5,7 @@ use warnings; use Date::Parse; use Encode qw(decode encode); +use List::Util qw(max); use MIME::Base64 qw(decode_base64 encode_base64); use Net::SSLeay; @@ -366,6 +367,28 @@ sub check_expiry { return ($not_after < $timestamp) ? 1 : 0; } +# Long-lived certs are renewed once 2/3 of their lifetime has elapsed; short-lived ones (under +# ten days) already at 1/2, following Let's Encrypt's integration guide. A 3-day floor still +# applies so the daily-update service has a couple of chances to retry transient failures. +sub get_cert_renew_lead_time { + my ($cert_path) = @_; + + my ($not_before, $not_after) = get_validity_as_epoch($cert_path); + my $lifetime = $not_after - $not_before; + + if ($lifetime <= 0) { + warn "Unexpected validity period ($lifetime s) for '$cert_path', falling back to a 30 day" + . " renewal lead time\n"; + + return 30 * 24 * 60 * 60; + } + + my $scale = $lifetime < (10 * 24 * 60 * 60) ? 2 : 3; + my $lead = int($lifetime / $scale); + + return max($lead, 3 * 24 * 60 * 60); # 3 day minimum +} + # Create a CSR and certificate key for a given order # returns path to CSR file or path to CSR and key files sub generate_csr { -- 2.47.3