Skip to content

Commit 45f0452

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

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

pkg/certificate/manager.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package certificate
22

33
import (
4+
"math/rand"
45
"time"
56

67
"github.com/pkg/errors"
@@ -58,7 +59,16 @@ func (m *manager) IssueCertificate(cn CommonName, validityPeriod time.Duration)
5859
start := time.Now()
5960

6061
if cert := m.getFromCache(cn); cert != nil {
61-
return cert, nil
62+
intNoise := rand.Intn(noiseSeconds) // #nosec G404
63+
secondsNoise := time.Duration(intNoise) * time.Second
64+
renewBefore := RenewBeforeCertExpires + secondsNoise
65+
// Round is called to truncate monotonic clock to the nearest second. This is done to avoid environments where the
66+
// CPU clock may stop, resulting in a time measurement that differs significantly from the x509 timestamp.
67+
// See https://github.com/openservicemesh/osm/issues/5000#issuecomment-1218539412 for more details.
68+
expiration := cert.GetExpiration().Round(0)
69+
if time.Until(expiration) > renewBefore {
70+
return cert, nil
71+
}
6272
}
6373

6474
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)