Skip to content

Commit c5b9b44

Browse files
committed
feat(cert): implement early renewal logic for short-lived certificates
1 parent e47fc25 commit c5b9b44

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

internal/cert/auto_cert.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,28 @@ func autoCert(certModel *model.Cert) {
6666
certAge := int(time.Since(certInfo.NotBefore).Hours() / 24)
6767
// Calculate days until expiration
6868
daysUntilExpiration := int(time.Until(certInfo.NotAfter).Hours() / 24)
69+
// Calculate total certificate validity period
70+
totalValidityDays := int(certInfo.NotAfter.Sub(certInfo.NotBefore).Hours() / 24)
6971

70-
// Skip renewal only if:
71-
// 1. Certificate age is less than renewal interval AND
72-
// 2. Certificate has more than 6 days remaining before expiration
73-
if certAge < settings.CertSettings.GetCertRenewalInterval() && daysUntilExpiration > 6 {
74-
// Certificate is too young and not expiring soon, ignore
75-
return
72+
renewalInterval := settings.CertSettings.GetCertRenewalInterval()
73+
74+
// For certificates with short validity periods (less than renewal interval),
75+
// use early renewal logic to prevent expiration
76+
if totalValidityDays < renewalInterval {
77+
// Renew when 2/3 of the certificate's lifetime remains
78+
// This provides a safety buffer for short-lived certificates
79+
earlyRenewalThreshold := 2 * totalValidityDays / 3
80+
if daysUntilExpiration > earlyRenewalThreshold {
81+
return
82+
}
83+
// If we reach here, proceed with renewal for short-lived certificate
84+
} else {
85+
// For normal certificates with validity >= renewal interval:
86+
// Skip renewal if certificate age is less than the configured renewal interval
87+
// This ensures we don't renew certificates too frequently
88+
if certAge < renewalInterval {
89+
return
90+
}
7691
}
7792

7893
// after 1 mo, reissue certificate

0 commit comments

Comments
 (0)