Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit cd6f772

Browse files
committed
collapse rotateCertificate method
Signed-off-by: Sean Teeling <[email protected]>
1 parent 3fc068b commit cd6f772

File tree

1 file changed

+11
-42
lines changed

1 file changed

+11
-42
lines changed

pkg/certificate/manager.go

+11-42
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package certificate
33
import (
44
"time"
55

6-
"github.com/pkg/errors"
76
"github.com/rs/zerolog/log"
87

98
"github.com/openservicemesh/osm/pkg/announcements"
@@ -39,19 +38,13 @@ func NewManager(mrcClient MRCClient, serviceCertValidityDuration time.Duration,
3938

4039
// Start takes an interval to check if the certificate
4140
// needs to be rotated
42-
func (m *Manager) Start(checkInterval time.Duration, certRotation <-chan struct{}) {
43-
// iterate over the list of certificates
44-
// when a cert needs to be rotated - call RotateCertificate()
45-
if certRotation == nil {
46-
log.Error().Msgf("Cannot start certificate rotation, certRotation is nil")
47-
return
48-
}
41+
func (m *Manager) Start(checkInterval time.Duration, stop <-chan struct{}) {
4942
ticker := time.NewTicker(checkInterval)
5043
go func() {
5144
m.checkAndRotate()
5245
for {
5346
select {
54-
case <-certRotation:
47+
case <-stop:
5548
ticker.Stop()
5649
return
5750
case <-ticker.C:
@@ -73,15 +66,21 @@ func (m *Manager) checkAndRotate() {
7366
RenewBeforeCertExpires)
7467

7568
if shouldRotate {
76-
// Remove the certificate from the cache of the certificate manager
77-
newCert, err := m.rotateCertificate(cert.GetCommonName())
69+
newCert, err := m.IssueCertificate(cert.GetCommonName(), m.serviceCertValidityDuration)
7870
if err != nil {
7971
// TODO(#3962): metric might not be scraped before process restart resulting from this error
8072
log.Error().Err(err).Str(errcode.Kind, errcode.GetErrCodeWithMetric(errcode.ErrRotatingCert)).
8173
Msgf("Error rotating cert SerialNumber=%s", cert.GetSerialNumber())
8274
continue
8375
}
84-
log.Trace().Msgf("Rotated cert SerialNumber=%s", newCert.GetSerialNumber())
76+
77+
m.msgBroker.GetCertPubSub().Pub(events.PubSubMessage{
78+
Kind: announcements.CertificateRotated,
79+
NewObj: newCert,
80+
OldObj: cert,
81+
}, announcements.CertificateRotated.String())
82+
83+
log.Debug().Msgf("Rotated certificate (old SerialNumber=%s) with new SerialNumber=%s", cert.SerialNumber, newCert.SerialNumber)
8584
}
8685
}
8786
}
@@ -143,36 +142,6 @@ func (m *Manager) ReleaseCertificate(cn CommonName) {
143142
m.cache.Delete(cn)
144143
}
145144

146-
// RotateCertificate implements Manager and rotates an existing
147-
func (m *Manager) rotateCertificate(cn CommonName) (*Certificate, error) {
148-
start := time.Now()
149-
150-
oldObj, ok := m.cache.Load(cn)
151-
if !ok {
152-
return nil, errors.Errorf("Old certificate does not exist for CN=%s", cn)
153-
}
154-
155-
oldCert, ok := oldObj.(*Certificate)
156-
if !ok {
157-
return nil, errors.Errorf("unexpected type %T for old certificate does not exist for CN=%s", oldCert, cn)
158-
}
159-
160-
newCert, err := m.IssueCertificate(cn, m.serviceCertValidityDuration)
161-
if err != nil {
162-
return nil, err
163-
}
164-
165-
m.msgBroker.GetCertPubSub().Pub(events.PubSubMessage{
166-
Kind: announcements.CertificateRotated,
167-
NewObj: newCert,
168-
OldObj: oldCert,
169-
}, announcements.CertificateRotated.String())
170-
171-
log.Debug().Msgf("Rotated certificate (old SerialNumber=%s) with new SerialNumber=%s took %+v", oldCert.SerialNumber, newCert.SerialNumber, time.Since(start))
172-
173-
return newCert, nil
174-
}
175-
176145
// ListIssuedCertificates implements CertificateDebugger interface and returns the list of issued certificates.
177146
func (m *Manager) ListIssuedCertificates() []*Certificate {
178147
var certs []*Certificate

0 commit comments

Comments
 (0)