-
Notifications
You must be signed in to change notification settings - Fork 11
/
manager.go
357 lines (313 loc) · 11.3 KB
/
manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package certificate
import (
"context"
"errors"
"math/rand"
"sync"
"time"
"github.com/flomesh-io/fsm/pkg/announcements"
"github.com/flomesh-io/fsm/pkg/constants"
"github.com/flomesh-io/fsm/pkg/errcode"
"github.com/flomesh-io/fsm/pkg/k8s/events"
"github.com/flomesh-io/fsm/pkg/logger"
"github.com/flomesh-io/fsm/pkg/messaging"
)
var (
log = logger.New("certificate")
)
// NewManager creates a new CertificateManager with the passed MRCClient and options
func NewManager(ctx context.Context, mrcClient MRCClient, getServiceCertValidityPeriod func() time.Duration, getIngressCertValidityDuration func() time.Duration, msgBroker *messaging.Broker, checkInterval time.Duration) (*Manager, error) {
m := &Manager{
serviceCertValidityDuration: getServiceCertValidityPeriod,
ingressCertValidityDuration: getIngressCertValidityDuration,
msgBroker: msgBroker,
}
err := m.start(ctx, mrcClient)
if err != nil {
return nil, err
}
m.startRotationTicker(ctx, checkInterval)
return m, nil
}
func (m *Manager) startRotationTicker(ctx context.Context, checkInterval time.Duration) {
ticker := time.NewTicker(checkInterval)
go func() {
m.checkAndRotate()
for {
select {
case <-ctx.Done():
ticker.Stop()
return
case <-ticker.C:
m.checkAndRotate()
}
}
}()
}
func (m *Manager) start(ctx context.Context, mrcClient MRCClient) error {
// start a watch and we wait until the manager is initialized so that
// the caller gets a manager that's ready to be used
var once sync.Once
var wg sync.WaitGroup
mrcEvents, err := mrcClient.Watch(ctx)
if err != nil {
return err
}
wg.Add(1)
go func(wg *sync.WaitGroup, once *sync.Once) {
for {
select {
case <-ctx.Done():
if err := ctx.Err(); err != nil {
log.Error().Err(err).Msg("context canceled with error. stopping MRC watch...")
return
}
log.Info().Msg("context canceled. stopping MRC watch...")
return
case event, open := <-mrcEvents:
if !open {
// channel was closed; return
log.Info().Msg("stopping MRC watch...")
return
}
err = m.handleMRCEvent(mrcClient, event)
if err != nil {
log.Error().Err(err).Msgf("error encountered processing MRCEvent")
continue
}
}
if m.signingIssuer != nil && m.validatingIssuer != nil {
once.Do(func() {
wg.Done()
})
}
}
}(&wg, &once)
done := make(chan struct{})
// Wait for WaitGroup to finish and notify select when it does
go func() {
wg.Wait()
close(done)
}()
select {
case <-time.After(10 * time.Second):
// We timed out
return errors.New("manager initialization timed out. Make sure your MeshRootCertificate(s) are valid")
case <-done:
}
return nil
}
func (m *Manager) handleMRCEvent(mrcClient MRCClient, event MRCEvent) error {
switch event.Type {
case MRCEventAdded:
mrc := event.MRC
if mrc.Status.State == constants.MRCStateError {
log.Debug().Msgf("skipping MRC with error state %s", mrc.GetName())
return nil
}
client, ca, err := mrcClient.GetCertIssuerForMRC(mrc)
if err != nil {
return err
}
c := &issuer{Issuer: client, ID: mrc.Name, CertificateAuthority: ca, TrustDomain: mrc.Spec.TrustDomain}
switch {
case mrc.Status.State == constants.MRCStateActive:
m.mu.Lock()
m.signingIssuer = c
m.validatingIssuer = c
m.mu.Unlock()
case mrc.Status.State == constants.MRCStateIssuingRollback || mrc.Status.State == constants.MRCStateIssuingRollout:
m.mu.Lock()
m.signingIssuer = c
m.mu.Unlock()
case mrc.Status.State == constants.MRCStateValidatingRollback || mrc.Status.State == constants.MRCStateValidatingRollout:
m.mu.Lock()
m.validatingIssuer = c
m.mu.Unlock()
default:
m.mu.Lock()
m.signingIssuer = c
m.validatingIssuer = c
m.mu.Unlock()
}
case MRCEventUpdated:
// TODO
}
return nil
}
// GetTrustDomain returns the trust domain from the configured signingkey issuer.
// Note that the CRD uses a default, so this value will always be set.
func (m *Manager) GetTrustDomain() string {
m.mu.Lock()
defer m.mu.Unlock()
return m.signingIssuer.TrustDomain
}
// ShouldRotate determines whether a certificate should be rotated.
func (m *Manager) ShouldRotate(c *Certificate) bool {
// The certificate is going to expire at a timestamp T
// We want to renew earlier. How much earlier is defined in renewBeforeCertExpires.
// We add a few seconds noise to the early renew period so that certificates that may have been
// created at the same time are not renewed at the exact same time.
intNoise := rand.Intn(noiseSeconds) // #nosec G404
secondsNoise := time.Duration(intNoise) * time.Second
renewBefore := RenewBeforeCertExpires + secondsNoise
// Round is called to truncate monotonic clock to the nearest second. This is done to avoid environments where the
// CPU clock may stop, resulting in a time measurement that differs significantly from the x509 timestamp.
// See https://github.com/flomesh-io/fsm/issues/5000#issuecomment-1218539412 for more details.
expiration := c.GetExpiration().Round(0)
if time.Until(expiration) <= renewBefore {
log.Info().Msgf("Cert %s should be rotated; expires in %+v; renewBefore is %+v",
c.GetCommonName(),
time.Until(expiration),
renewBefore)
return true
}
m.mu.Lock()
validatingIssuer := m.validatingIssuer
signingIssuer := m.signingIssuer
m.mu.Unlock()
// During root certificate rotation the Issuers will change. If the Manager's Issuers are
// different than the validating Issuer and signing Issuer IDs in the certificate, the
// certificate must be reissued with the correct Issuers for the current rotation stage and
// state. If there is no root certificate rotation in progress, the cert and Manager Issuers
// will match.
if c.signingIssuerID != signingIssuer.ID || c.validatingIssuerID != validatingIssuer.ID {
log.Info().Msgf("Cert %s should be rotated; in progress root certificate rotation",
c.GetCommonName())
return true
}
return false
}
func (m *Manager) checkAndRotate() {
// NOTE: checkAndRotate can reintroduce a certificate that has been released, thereby creating an unbounded cache.
// A certificate can also have been rotated already, leaving the list of issued certs stale, and we re-rotate.
// the latter is not a bug, but a source of inefficiency.
certs := map[string]*Certificate{}
m.cache.Range(func(keyIface interface{}, certInterface interface{}) bool {
key := keyIface.(string)
certs[key] = certInterface.(*Certificate)
return true // continue the iteration
})
for key, cert := range certs {
opts := []IssueOption{}
if key == cert.CommonName.String() {
opts = append(opts, FullCNProvided())
opts = append(opts, SubjectAlternativeNames(uniqueSubjectAlternativeNames(cert.SANames, key)...))
}
_, err := m.IssueCertificate(key, cert.certType, opts...)
if err != nil {
log.Error().Err(err).Str(errcode.Kind, errcode.GetErrCodeWithMetric(errcode.ErrRotatingCert)).
Msgf("Error rotating cert SerialNumber=%s", cert.GetSerialNumber())
}
}
}
func (m *Manager) getValidityDurationForCertType(ct CertType) time.Duration {
switch ct {
case Internal:
return constants.FSMCertificateValidityPeriod
case IngressGateway:
return m.ingressCertValidityDuration()
case Service:
return m.serviceCertValidityDuration()
default:
log.Debug().Msgf("Unknown certificate type %s provided when getting validity duration", ct)
return constants.FSMCertificateValidityPeriod
}
}
// getFromCache returns the certificate with the specified cn from cache if it exists.
// Note: getFromCache might return an expired or invalid certificate.
func (m *Manager) getFromCache(key string) *Certificate {
certInterface, exists := m.cache.Load(key)
if !exists {
return nil
}
cert := certInterface.(*Certificate)
log.Trace().Msgf("Certificate found in cache SerialNumber=%s", cert.GetSerialNumber())
return cert
}
// GetCertificate returns the certificate with the specified cn from cache if it exists.
func (m *Manager) GetCertificate(prefix string) *Certificate {
// a singleflight group is used here to ensure that only one issueCertificate is in
// flight at a time for a given certificate prefix. Helps avoid a race condition if
// issueCertificate is called multiple times in a row for the same certificate prefix.
cert, _, _ := m.group.Do(prefix, func() (interface{}, error) {
return m.getFromCache(prefix), nil
})
return cert.(*Certificate)
}
// IssueCertificate returns a newly issued certificate from the given client
// or an existing valid certificate from the local cache.
func (m *Manager) IssueCertificate(prefix string, ct CertType, opts ...IssueOption) (*Certificate, error) {
// a singleflight group is used here to ensure that only one issueCertificate is in
// flight at a time for a given certificate prefix. Helps avoid a race condition if
// issueCertificate is called multiple times in a row for the same certificate prefix.
cert, err, _ := m.group.Do(prefix, func() (interface{}, error) {
return m.issueCertificate(prefix, ct, opts...)
})
if err != nil {
return nil, err
}
return cert.(*Certificate), nil
}
func (m *Manager) issueCertificate(prefix string, ct CertType, opts ...IssueOption) (*Certificate, error) {
var rotate bool
cert := m.getFromCache(prefix) // Don't call this while holding the lock
if cert != nil {
// check if cert needs to be rotated
rotate = m.ShouldRotate(cert)
if !rotate {
return cert, nil
}
}
options := &issueOptions{}
for _, o := range opts {
o(options)
}
if cert != nil && rotate {
options.saNames = uniqueSubjectAlternativeNames(cert.SANames)
}
m.mu.Lock()
validatingIssuer := m.validatingIssuer
signingIssuer := m.signingIssuer
m.mu.Unlock()
start := time.Now()
validityDuration := m.getValidityDurationForCertType(ct)
newCert, err := signingIssuer.IssueCertificate(options.formatCN(prefix, signingIssuer.TrustDomain), options.subjectAlternativeNames(), options.validityPeriod(validityDuration))
if err != nil {
return nil, err
}
// if we have different signing and validating issuers,
// create the cert's trust context
if validatingIssuer.ID != signingIssuer.ID {
newCert = newCert.newMergedWithRoot(validatingIssuer.CertificateAuthority)
}
newCert.signingIssuerID = signingIssuer.ID
newCert.validatingIssuerID = validatingIssuer.ID
newCert.certType = ct
m.cache.Store(prefix, newCert)
log.Trace().Msgf("It took %s to issue certificate with SerialNumber=%s", time.Since(start), newCert.GetSerialNumber())
if rotate {
// Certificate was rotated
m.msgBroker.GetCertPubSub().Pub(events.PubSubMessage{
Kind: announcements.CertificateRotated,
NewObj: newCert,
OldObj: cert,
}, announcements.CertificateRotated.String())
log.Debug().Msgf("Rotated certificate (old SerialNumber=%s) with new SerialNumber=%s", cert.SerialNumber, newCert.SerialNumber)
}
return newCert, nil
}
// ReleaseCertificate is called when a cert will no longer be needed and should be removed from the system.
func (m *Manager) ReleaseCertificate(key string) {
log.Trace().Msgf("Releasing certificate %s", key)
m.cache.Delete(key)
}
// ListIssuedCertificates implements CertificateDebugger interface and returns the list of issued certificates.
func (m *Manager) ListIssuedCertificates() []*Certificate {
var certs []*Certificate
m.cache.Range(func(cnInterface interface{}, certInterface interface{}) bool {
certs = append(certs, certInterface.(*Certificate))
return true // continue the iteration
})
return certs
}