@@ -3,7 +3,6 @@ package certificate
3
3
import (
4
4
"time"
5
5
6
- "github.com/pkg/errors"
7
6
"github.com/rs/zerolog/log"
8
7
9
8
"github.com/openservicemesh/osm/pkg/announcements"
@@ -39,19 +38,13 @@ func NewManager(mrcClient MRCClient, serviceCertValidityDuration time.Duration,
39
38
40
39
// Start takes an interval to check if the certificate
41
40
// 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 {}) {
49
42
ticker := time .NewTicker (checkInterval )
50
43
go func () {
51
44
m .checkAndRotate ()
52
45
for {
53
46
select {
54
- case <- certRotation :
47
+ case <- stop :
55
48
ticker .Stop ()
56
49
return
57
50
case <- ticker .C :
@@ -73,15 +66,21 @@ func (m *Manager) checkAndRotate() {
73
66
RenewBeforeCertExpires )
74
67
75
68
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 )
78
70
if err != nil {
79
71
// TODO(#3962): metric might not be scraped before process restart resulting from this error
80
72
log .Error ().Err (err ).Str (errcode .Kind , errcode .GetErrCodeWithMetric (errcode .ErrRotatingCert )).
81
73
Msgf ("Error rotating cert SerialNumber=%s" , cert .GetSerialNumber ())
82
74
continue
83
75
}
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 )
85
84
}
86
85
}
87
86
}
@@ -143,36 +142,6 @@ func (m *Manager) ReleaseCertificate(cn CommonName) {
143
142
m .cache .Delete (cn )
144
143
}
145
144
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
-
176
145
// ListIssuedCertificates implements CertificateDebugger interface and returns the list of issued certificates.
177
146
func (m * Manager ) ListIssuedCertificates () []* Certificate {
178
147
var certs []* Certificate
0 commit comments