Skip to content

Commit 3fcd710

Browse files
francislavoiemholt
andauthored
Quiet OCSP warnings if the cert has a short lifetime (#320)
* Quiet OCSP warnings if the cert has a short lifetime * Quier differently * Fix condition Co-authored-by: Matt Holt <[email protected]> * Oops, had the Lifetime math wrong --------- Co-authored-by: Matt Holt <[email protected]>
1 parent 4293198 commit 3fcd710

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

certificates.go

+8
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,14 @@ func (cert Certificate) Expired() bool {
188188
return time.Now().After(expiresAt(cert.Leaf))
189189
}
190190

191+
// Lifetime returns the duration of the certificate's validity.
192+
func (cert Certificate) Lifetime() time.Duration {
193+
if cert.Leaf == nil || cert.Leaf.NotAfter.IsZero() {
194+
return 0
195+
}
196+
return expiresAt(cert.Leaf).Sub(cert.Leaf.NotBefore)
197+
}
198+
191199
// currentlyInRenewalWindow returns true if the current time is within
192200
// (or after) the renewal window, according to the given start/end
193201
// dates and the ratio of the renewal window. If true is returned,

ocsp.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,16 @@ func stapleOCSP(ctx context.Context, ocspConfig OCSPConfig, storage Storage, cer
9393
// then we need to request it from the OCSP responder
9494
if ocspResp == nil || len(ocspBytes) == 0 {
9595
ocspBytes, ocspResp, ocspErr = getOCSPForCert(ocspConfig, pemBundle)
96+
// An error here is not a problem because a certificate
97+
// may simply not contain a link to an OCSP server.
9698
if ocspErr != nil {
97-
// An error here is not a problem because a certificate may simply
98-
// not contain a link to an OCSP server. But we should log it anyway.
99+
// For short-lived certificates, this is fine and we can ignore
100+
// logging because OCSP doesn't make much sense for them anyway.
101+
if cert.Lifetime() < 7*24*time.Hour {
102+
return nil
103+
}
99104
// There's nothing else we can do to get OCSP for this certificate,
100-
// so we can return here with the error.
105+
// so we can return here with the error to warn about it.
101106
return fmt.Errorf("no OCSP stapling for %v: %w", cert.Names, ocspErr)
102107
}
103108
gotNewOCSP = true

0 commit comments

Comments
 (0)