Skip to content

Commit a93a7f6

Browse files
committed
refactor: derive certificate expiry buffer from lifetime, add comments
1 parent 318948c commit a93a7f6

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

  • crates/stackable-webhook/src/tls

crates/stackable-webhook/src/tls/mod.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,25 @@ use crate::{
3939
mod cert_resolver;
4040

4141
pub const WEBHOOK_CA_LIFETIME: Duration = Duration::from_hours_unchecked(24);
42-
pub const WEBHOOK_CERTIFICATE_LIFETIME: Duration = Duration::from_hours_unchecked(24);
4342

44-
/// How often to check whether the certificate needs rotation (5 minutes).
43+
/// The wall-clock lifetime of generated webhook certificates. If this is ever
44+
/// reduced, ensure it stays well above [`CERTIFICATE_ROTATION_CHECK_INTERVAL`]
45+
/// (currently 5 minutes), otherwise the certificate could expire between checks.
46+
const WEBHOOK_CERTIFICATE_LIFETIME_HOURS: u64 = 24;
47+
pub const WEBHOOK_CERTIFICATE_LIFETIME: Duration =
48+
Duration::from_hours_unchecked(WEBHOOK_CERTIFICATE_LIFETIME_HOURS);
49+
50+
/// How often to check whether the certificate needs rotation. This is
51+
/// intentionally independent of the certificate lifetime — it controls how
52+
/// quickly we detect wall-clock drift (from hibernation, VM migration, etc.),
53+
/// not how long the certificate lives.
4554
const CERTIFICATE_ROTATION_CHECK_INTERVAL: Duration = Duration::from_minutes_unchecked(5);
4655

47-
/// Rotate the certificate when it is within this buffer of expiry according
48-
/// to wall-clock time (4 hours before the 24h certificate expires).
49-
const CERTIFICATE_EXPIRY_BUFFER: Duration = Duration::from_hours_unchecked(4);
56+
/// Rotate the certificate when less than 1/6 of its lifetime remains
57+
/// (4 hours for the current 24h lifetime). Derived from
58+
/// [`WEBHOOK_CERTIFICATE_LIFETIME`] so it scales if the lifetime changes.
59+
const CERTIFICATE_EXPIRY_BUFFER: Duration =
60+
Duration::from_minutes_unchecked(WEBHOOK_CERTIFICATE_LIFETIME_HOURS * 60 / 6);
5061

5162
pub type Result<T, E = TlsServerError> = std::result::Result<T, E>;
5263

0 commit comments

Comments
 (0)