Skip to content

Commit a54a55c

Browse files
steelingshalier
authored andcommitted
[backport] cherry-pick a016262 to release-v1.1
cert rotation now ignores monotonic clock readings when checking expiration (openservicemesh#5012) Signed-off-by: Sean Teeling <[email protected]> Signed-off-by: Shalier Xia <[email protected]>
1 parent cc859d5 commit a54a55c

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

pkg/certificate/certificate.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,11 @@ func (c *Certificate) ShouldRotate() bool {
5656

5757
intNoise := rand.Intn(noiseSeconds) // #nosec G404
5858
secondsNoise := time.Duration(intNoise) * time.Second
59-
return time.Until(c.GetExpiration()) <= (RenewBeforeCertExpires + secondsNoise)
59+
renewBefore := RenewBeforeCertExpires + secondsNoise
60+
// Round is called to truncate monotonic clock to the nearest second. This is done to avoid environments where the
61+
// CPU clock may stop, resulting in a time measurement that differs significantly from the x509 timestamp.
62+
// See https://github.com/openservicemesh/osm/issues/5000#issuecomment-1218539412 for more details.
63+
expiration := c.GetExpiration().Round(0)
64+
65+
return time.Until(expiration) <= renewBefore
6066
}

pkg/certificate/manager.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ func (m *manager) IssueCertificate(cn CommonName, validityPeriod time.Duration)
5858
start := time.Now()
5959

6060
if cert := m.getFromCache(cn); cert != nil {
61-
return cert, nil
61+
// check if cert needs to be rotated
62+
rotate := cert.ShouldRotate()
63+
if !rotate {
64+
return cert, nil
65+
}
6266
}
6367

6468
cert, err := m.client.IssueCertificate(cn, validityPeriod)

pkg/debugger/certificate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ func (ds DebugConfig) getCertHandler() http.Handler {
2828

2929
_, _ = fmt.Fprintf(w, "---[ %d ]---\n", idx)
3030
_, _ = fmt.Fprintf(w, "\t Common Name: %q\n", cert.GetCommonName())
31-
_, _ = fmt.Fprintf(w, "\t Valid Until: %+v (%+v remaining)\n", cert.GetExpiration(), time.Until(cert.GetExpiration()))
31+
_, _ = fmt.Fprintf(w, "\t Valid Until: %+v (%+v remaining)\n", cert.GetExpiration(), time.Until(cert.GetExpiration().Round(0)))
3232
_, _ = fmt.Fprintf(w, "\t Issuing CA (SHA256): %x\n", sha256.Sum256(ca))
3333
_, _ = fmt.Fprintf(w, "\t Cert Chain (SHA256): %x\n", sha256.Sum256(chain))
3434

3535
// Show only some x509 fields to keep the output clean
3636
_, _ = fmt.Fprintf(w, "\t x509.SignatureAlgorithm: %+v\n", x509.SignatureAlgorithm)
3737
_, _ = fmt.Fprintf(w, "\t x509.PublicKeyAlgorithm: %+v\n", x509.PublicKeyAlgorithm)
3838
_, _ = fmt.Fprintf(w, "\t x509.Version: %+v\n", x509.Version)
39-
_, _ = fmt.Fprintf(w, "\t x509.SerialNumber: %x\n", x509.SerialNumber)
39+
_, _ = fmt.Fprintf(w, "\t x509.SerialNumber: %s\n", x509.SerialNumber)
4040
_, _ = fmt.Fprintf(w, "\t x509.Issuer: %+v\n", x509.Issuer)
4141
_, _ = fmt.Fprintf(w, "\t x509.Subject: %+v\n", x509.Subject)
4242
_, _ = fmt.Fprintf(w, "\t x509.NotBefore (begin): %+v (%+v ago)\n", x509.NotBefore, time.Since(x509.NotBefore))

0 commit comments

Comments
 (0)