From ffd710c58de2dabcb76be97e1484be2891c2e6fc Mon Sep 17 00:00:00 2001 From: Sebastian Bernauer Date: Tue, 17 Nov 2026 13:27:01 +0100 Subject: [PATCH 1/4] chore(webhook): Replace humantime with jiff Fixes https://rustsec.org/advisories/RUSTSEC-2026-0542 --- Cargo.lock | 8 +------- Cargo.toml | 1 - crates/stackable-webhook/Cargo.toml | 2 +- crates/stackable-webhook/src/tls/cert_resolver.rs | 7 +++++-- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9571b3637..4aab6cc84 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1545,12 +1545,6 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" -[[package]] -name = "humantime" -version = "2.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15cdd26707701c53297e2fa6afb323d55fbc1d0810c3aec078ae3ef0424c3c15" - [[package]] name = "hybrid-array" version = "0.4.15" @@ -3959,9 +3953,9 @@ dependencies = [ "axum", "clap", "futures-util", - "humantime", "hyper", "hyper-util", + "jiff", "k8s-openapi", "kube", "opentelemetry", diff --git a/Cargo.toml b/Cargo.toml index cdd0d3cb6..666009f58 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,7 +36,6 @@ futures = "0.3.34" futures-util = "0.3.34" hickory-resolver = "0.26.3" http = "1.5.0" -humantime = "2.4.0" indexmap = "2.14.2" indoc = "2.0.8" jiff = "0.2.38" diff --git a/crates/stackable-webhook/Cargo.toml b/crates/stackable-webhook/Cargo.toml index d3a07e411..dca6ebb07 100644 --- a/crates/stackable-webhook/Cargo.toml +++ b/crates/stackable-webhook/Cargo.toml @@ -15,9 +15,9 @@ arc-swap.workspace = true async-trait.workspace = true axum.workspace = true futures-util.workspace = true -humantime.workspace = true hyper-util.workspace = true hyper.workspace = true +jiff.workspace = true k8s-openapi.workspace = true kube.workspace = true opentelemetry.workspace = true diff --git a/crates/stackable-webhook/src/tls/cert_resolver.rs b/crates/stackable-webhook/src/tls/cert_resolver.rs index 4f4108e4b..250d96726 100644 --- a/crates/stackable-webhook/src/tls/cert_resolver.rs +++ b/crates/stackable-webhook/src/tls/cert_resolver.rs @@ -1,6 +1,7 @@ use std::{sync::Arc, time::SystemTime}; use arc_swap::ArcSwap; +use jiff::Timestamp; use snafu::{OptionExt, ResultExt, Snafu}; use stackable_certs::{CertificatePairError, ca::CertificateAuthority, keys::ecdsa}; use tokio::sync::mpsc; @@ -112,8 +113,10 @@ impl CertificateResolver { tracing::debug!( x509.subject_alternative_names = ?self.subject_alternative_dns_names, - x509.not_after = %humantime::format_rfc3339(not_after), - deadline = %humantime::format_rfc3339(deadline), + x509.not_after = %Timestamp::try_from(not_after) + .map_or_else(|err| err.to_string(), |ts| ts.to_string()), + deadline = %Timestamp::try_from(deadline) + .map_or_else(|err| err.to_string(), |ts| ts.to_string()), "checking if certificate needs rotation" ); From 55835993b0a51d940d7a984622981d261f64d952 Mon Sep 17 00:00:00 2001 From: Sebastian Bernauer Date: Tue, 17 Nov 2026 13:30:47 +0100 Subject: [PATCH 2/4] cargo update -p libz-sys --- Cargo.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4aab6cc84..049f23865 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2228,9 +2228,9 @@ checksum = "2e126dda6f34391ab7b444f9922055facc83c07a910da3eb16f1e4d9c45dc777" [[package]] name = "libz-sys" -version = "1.1.31" +version = "1.1.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff20308ecabd635a2dc1dd5834a86c462cca20efba1d5d3ddab1f8aea3455cb2" +checksum = "fee3f59f4321dab0c78d828942e7aa09d0bd8ad6e78a501977e6bf3d02938045" dependencies = [ "cc", "libc", @@ -4138,7 +4138,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" dependencies = [ "fastrand", - "getrandom 0.4.3", + "getrandom 0.3.4", "once_cell", "rustix", "windows-sys 0.61.2", From 8d4d9140c5f4bb98f55994acff6ebd280f37bfac Mon Sep 17 00:00:00 2001 From: Sebastian Bernauer Date: Wed, 18 Nov 2026 09:20:43 +0100 Subject: [PATCH 3/4] fix: constant naming --- crates/stackable-webhook/src/tls/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/stackable-webhook/src/tls/mod.rs b/crates/stackable-webhook/src/tls/mod.rs index 4b9e4aa24..ad339694a 100644 --- a/crates/stackable-webhook/src/tls/mod.rs +++ b/crates/stackable-webhook/src/tls/mod.rs @@ -53,10 +53,10 @@ pub const WEBHOOK_CERTIFICATE_LIFETIME: Duration = /// Rotate the certificate when less than 1/6 of its lifetime remains (4 hours for the current 24h /// lifetime). Derived from [`CERTIFICATE_LIFETIME_HOURS`] so it scales if the lifetime changes. -const CERTIFICATE_EXPIRY_BUFFER_HOURS: u64 = CERTIFICATE_LIFETIME_HOURS * 60 / 6; +const CERTIFICATE_EXPIRY_BUFFER_MINUTES: u64 = CERTIFICATE_LIFETIME_HOURS * 60 / 6; const CERTIFICATE_EXPIRY_BUFFER: Duration = - Duration::from_minutes_unchecked(CERTIFICATE_EXPIRY_BUFFER_HOURS); + Duration::from_minutes_unchecked(CERTIFICATE_EXPIRY_BUFFER_MINUTES); /// How often to check whether the certificate needs rotation. This is intentionally independent of /// the certificate lifetime - it controls how quickly we detect wall-clock drift (from hibernation, From 3e1f8717b8c4660466f53d2a04b4c0802eecb640 Mon Sep 17 00:00:00 2001 From: Sebastian Bernauer Date: Wed, 18 Nov 2026 09:53:00 +0100 Subject: [PATCH 4/4] refactor: cert expiration code, so that deadline conversion fails early --- Cargo.lock | 1 - crates/stackable-webhook/Cargo.toml | 1 - .../src/tls/cert_resolver.rs | 136 ++++++++++++++---- crates/stackable-webhook/src/tls/mod.rs | 2 +- 4 files changed, 110 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 049f23865..85fa090c8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3955,7 +3955,6 @@ dependencies = [ "futures-util", "hyper", "hyper-util", - "jiff", "k8s-openapi", "kube", "opentelemetry", diff --git a/crates/stackable-webhook/Cargo.toml b/crates/stackable-webhook/Cargo.toml index dca6ebb07..afcc6370e 100644 --- a/crates/stackable-webhook/Cargo.toml +++ b/crates/stackable-webhook/Cargo.toml @@ -17,7 +17,6 @@ axum.workspace = true futures-util.workspace = true hyper-util.workspace = true hyper.workspace = true -jiff.workspace = true k8s-openapi.workspace = true kube.workspace = true opentelemetry.workspace = true diff --git a/crates/stackable-webhook/src/tls/cert_resolver.rs b/crates/stackable-webhook/src/tls/cert_resolver.rs index 250d96726..d4f58ad8d 100644 --- a/crates/stackable-webhook/src/tls/cert_resolver.rs +++ b/crates/stackable-webhook/src/tls/cert_resolver.rs @@ -1,16 +1,15 @@ use std::{sync::Arc, time::SystemTime}; use arc_swap::ArcSwap; -use jiff::Timestamp; use snafu::{OptionExt, ResultExt, Snafu}; use stackable_certs::{CertificatePairError, ca::CertificateAuthority, keys::ecdsa}; use tokio::sync::mpsc; use tokio_rustls::rustls::{ crypto::CryptoProvider, server::ResolvesServerCert, sign::CertifiedKey, }; -use x509_cert::Certificate; +use x509_cert::{Certificate, der::DateTime, time::Time}; -use super::{WEBHOOK_CA_LIFETIME, WEBHOOK_CERTIFICATE_LIFETIME}; +use super::{CERTIFICATE_EXPIRY_BUFFER, WEBHOOK_CA_LIFETIME, WEBHOOK_CERTIFICATE_LIFETIME}; type Result = std::result::Result; @@ -46,6 +45,19 @@ pub enum CertificateResolverError { #[snafu(display("no default rustls CryptoProvider installed"))] NoDefaultCryptoProviderInstalled, + + #[snafu(display("failed to compute certificate rotation deadline"))] + ComputeRotationDeadline { source: x509_cert::der::Error }, +} + +/// The wall-clock expiry times of a certificate. +#[derive(Debug)] +struct CertificateExpiry { + /// The `notAfter` time of the certificate. + not_after: Time, + + /// When to rotate the certificate: `notAfter` minus [`CERTIFICATE_EXPIRY_BUFFER`]. + rotation_deadline: Time, } /// This struct serves as [`ResolvesServerCert`] to always hand out the current certificate for TLS @@ -59,9 +71,9 @@ pub struct CertificateResolver { /// (and performant) bridge between async write and sync read. current_certified_key: ArcSwap, - /// The wall-clock expiry time (`not_after`) of the current certificate. + /// The wall-clock expiry times of the current certificate. /// Used to detect clock drift between monotonic and wall-clock time. - current_not_after: ArcSwap, + current_expiry: ArcSwap, subject_alternative_dns_names: Arc>, @@ -74,7 +86,7 @@ impl CertificateResolver { certificate_tx: mpsc::Sender, ) -> Result { let subject_alternative_dns_names = Arc::new(subject_alternative_dns_names); - let (certified_key, not_after) = Self::generate_new_certificate_inner( + let (certified_key, expiry) = Self::generate_new_certificate_inner( subject_alternative_dns_names.clone(), &certificate_tx, ) @@ -83,53 +95,48 @@ impl CertificateResolver { Ok(Self { subject_alternative_dns_names, current_certified_key: ArcSwap::new(certified_key), - current_not_after: ArcSwap::new(Arc::new(not_after)), + current_expiry: ArcSwap::new(Arc::new(expiry)), certificate_tx, }) } pub async fn rotate_certificate(&self) -> Result<()> { - let (certified_key, not_after) = self.generate_new_certificate().await?; + let (certified_key, expiry) = self.generate_new_certificate().await?; // TODO: Sign the new cert somehow with the old cert. See https://github.com/stackabletech/decisions/issues/56 self.current_certified_key.store(certified_key); - self.current_not_after.store(Arc::new(not_after)); + self.current_expiry.store(Arc::new(expiry)); Ok(()) } /// Returns `true` if the current certificate is expired or will expire - /// within the given `buffer` duration according to wall-clock time. + /// within [`CERTIFICATE_EXPIRY_BUFFER`] according to wall-clock time. /// /// This catches cases where the monotonic timer (used by `tokio::time`) /// has drifted from wall-clock time, e.g. due to system hibernation. - pub fn needs_rotation(&self, buffer: std::time::Duration) -> bool { - let not_after = **self.current_not_after.load(); - // If subtraction underflows (buffer > time since epoch), fall back to - // UNIX_EPOCH so that the comparison always triggers rotation. - let deadline = not_after - .checked_sub(buffer) - .unwrap_or(SystemTime::UNIX_EPOCH); + pub fn needs_rotation(&self) -> bool { + let expiry = self.current_expiry.load(); tracing::debug!( x509.subject_alternative_names = ?self.subject_alternative_dns_names, - x509.not_after = %Timestamp::try_from(not_after) - .map_or_else(|err| err.to_string(), |ts| ts.to_string()), - deadline = %Timestamp::try_from(deadline) - .map_or_else(|err| err.to_string(), |ts| ts.to_string()), + // Will print in RFC 3339 format + x509.not_after = %expiry.not_after, + deadline = %expiry.rotation_deadline, + expiry_buffer = %CERTIFICATE_EXPIRY_BUFFER, "checking if certificate needs rotation" ); - SystemTime::now() >= deadline + SystemTime::now() >= expiry.rotation_deadline.to_system_time() } - async fn generate_new_certificate(&self) -> Result<(Arc, SystemTime)> { + async fn generate_new_certificate(&self) -> Result<(Arc, CertificateExpiry)> { let subject_alternative_dns_names = self.subject_alternative_dns_names.clone(); Self::generate_new_certificate_inner(subject_alternative_dns_names, &self.certificate_tx) .await } - /// Creates a new certificate and returns the certified key as well as `notAfter` timestamp. + /// Creates a new certificate and returns the certified key as well as its expiry times. /// /// The certificate is send to the passed `cert_tx`. /// @@ -139,7 +146,7 @@ impl CertificateResolver { async fn generate_new_certificate_inner( subject_alternative_dns_names: Arc>, certificate_tx: &mpsc::Sender, - ) -> Result<(Arc, SystemTime)> { + ) -> Result<(Arc, CertificateExpiry)> { // The certificate generations can take a while, so we use `spawn_blocking` let (cert, certified_key) = tokio::task::spawn_blocking(move || { let tls_provider = @@ -177,17 +184,32 @@ impl CertificateResolver { .await .context(TokioSpawnBlockingSnafu)??; - let not_after = cert.tbs_certificate().validity().not_after.to_system_time(); + let not_after = cert.tbs_certificate().validity().not_after; + let expiry = CertificateExpiry { + not_after, + rotation_deadline: rotation_deadline(not_after, *CERTIFICATE_EXPIRY_BUFFER)?, + }; certificate_tx .send(cert) .await .map_err(|_err| CertificateResolverError::SendCertificateToChannel)?; - Ok((certified_key, not_after)) + Ok((certified_key, expiry)) } } +/// Returns the time at which a certificate expiring at `not_after` should be rotated. +fn rotation_deadline(not_after: Time, expiry_buffer: std::time::Duration) -> Result