Skip to content

Commit

Permalink
Quiet OCSP warnings if the cert has a short lifetime (#320)
Browse files Browse the repository at this point in the history
* Quiet OCSP warnings if the cert has a short lifetime

* Quier differently

* Fix condition

Co-authored-by: Matt Holt <mholt@users.noreply.github.com>

* Oops, had the Lifetime math wrong

---------

Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
  • Loading branch information
francislavoie and mholt authored Nov 9, 2024
1 parent 4293198 commit 3fcd710
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
8 changes: 8 additions & 0 deletions certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@ func (cert Certificate) Expired() bool {
return time.Now().After(expiresAt(cert.Leaf))
}

// Lifetime returns the duration of the certificate's validity.
func (cert Certificate) Lifetime() time.Duration {
if cert.Leaf == nil || cert.Leaf.NotAfter.IsZero() {
return 0
}
return expiresAt(cert.Leaf).Sub(cert.Leaf.NotBefore)
}

// currentlyInRenewalWindow returns true if the current time is within
// (or after) the renewal window, according to the given start/end
// dates and the ratio of the renewal window. If true is returned,
Expand Down
11 changes: 8 additions & 3 deletions ocsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,16 @@ func stapleOCSP(ctx context.Context, ocspConfig OCSPConfig, storage Storage, cer
// then we need to request it from the OCSP responder
if ocspResp == nil || len(ocspBytes) == 0 {
ocspBytes, ocspResp, ocspErr = getOCSPForCert(ocspConfig, pemBundle)
// An error here is not a problem because a certificate
// may simply not contain a link to an OCSP server.
if ocspErr != nil {
// An error here is not a problem because a certificate may simply
// not contain a link to an OCSP server. But we should log it anyway.
// For short-lived certificates, this is fine and we can ignore
// logging because OCSP doesn't make much sense for them anyway.
if cert.Lifetime() < 7*24*time.Hour {
return nil
}
// There's nothing else we can do to get OCSP for this certificate,
// so we can return here with the error.
// so we can return here with the error to warn about it.
return fmt.Errorf("no OCSP stapling for %v: %w", cert.Names, ocspErr)
}
gotNewOCSP = true
Expand Down

0 comments on commit 3fcd710

Please sign in to comment.