-
Notifications
You must be signed in to change notification settings - Fork 137
/
rotator.go
523 lines (435 loc) · 14.1 KB
/
rotator.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
package mtls
import (
"bytes"
"context"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"math/big"
"time"
"github.com/pkg/errors"
infrav1 "github.com/weaveworks/tf-controller/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
)
// Heavily based on and inspired by https://github.com/open-policy-agent/cert-controller
// process should be:
// ca rotates on controller startup --if flag is specified
// creates the ca and controller cert
// then finds all runner certs and rotates them
// ca refreshes on a long interval once every 7 days
// server cert and runner certs refresh on a configurable interval
// runner certs rotate when the runner starts or after configurable interval 30 minutes
//TODO:
// when ca is rotated rotate any labelled secrets
// before creating a grpc client, do a cert check and if within the lookahead time rotate
const (
certName = "tls.crt"
keyName = "tls.key"
caCertName = "ca.crt"
caKeyName = "ca.key"
)
var crLog = logf.Log.WithName("cert-rotation")
var _ manager.Runnable = &CertRotator{}
// KeyPairArtifacts stores cert artifacts.
type KeyPairArtifacts struct {
Cert *x509.Certificate
Key *rsa.PrivateKey
CertPEM []byte
KeyPEM []byte
}
// CertRotator contains cert artifacts and a channel to close when the certs are ready.
type CertRotator struct {
client client.Client
SecretKey types.NamespacedName
CAName string
CAOrganization string
DNSName string
mgr manager.Manager
Ready chan struct{}
CAValidityDuration time.Duration
CertValidityDuration time.Duration
RotationCheckFrequency time.Duration
LookaheadInterval time.Duration
}
// AddRotator adds the CertRotator to the manager
func AddRotator(ctx context.Context, mgr manager.Manager, cr *CertRotator) error {
cr.client = mgr.GetClient()
cr.mgr = mgr
if err := mgr.Add(cr); err != nil {
return err
}
return nil
}
// Start starts the CertRotator runnable to rotate certs and ensure the certs are ready.
func (cr *CertRotator) Start(ctx context.Context) error {
// OK the leader do cert rotation.
// if we're not the leader, we're blocked here and don't do any cert rotation
<-cr.mgr.Elected()
crLog.Info("starting cert rotator controller")
defer crLog.Info("stopping cert rotator controller")
// explicitly rotate on the first round so that the certificate
// can be bootstrapped, otherwise manager exits before a cert can be written
if err := cr.refreshCertIfNeeded(); err != nil {
crLog.Error(err, "could not refresh cert on startup")
return err
}
close(cr.Ready)
ticker := time.NewTicker(cr.RotationCheckFrequency)
tickerLoop:
for {
select {
case <-ticker.C:
if err := cr.refreshCertIfNeeded(); err != nil {
crLog.Error(err, "error rotating certs")
}
case <-ctx.Done():
break tickerLoop
}
}
ticker.Stop()
return nil
}
func (cr *CertRotator) IsCertReady(ctx context.Context) bool {
secret := &corev1.Secret{}
if err := cr.client.Get(ctx, cr.SecretKey, secret); err != nil {
return false
}
if secret.Data == nil || !cr.validCACert(secret.Data[caCertName], secret.Data[caKeyName]) {
return false
}
if !cr.validServerCert(secret.Data[caCertName], secret.Data[certName], secret.Data[keyName]) {
return false
}
return true
}
// refreshCertIfNeeded returns whether there's any error when refreshing the certs if needed.
func (cr *CertRotator) refreshCertIfNeeded() error {
refreshFn := func() (bool, error) {
ctx := context.Background()
secret := &corev1.Secret{}
if err := cr.client.Get(ctx, cr.SecretKey, secret); err != nil {
if !apierrors.IsNotFound(err) {
return false, errors.Wrap(err, "acquiring secret to update certificates")
}
secret.ObjectMeta = metav1.ObjectMeta{
Name: cr.SecretKey.Name,
Namespace: cr.SecretKey.Namespace,
}
if err := cr.client.Create(ctx, secret); err != nil {
return false, errors.Wrap(err, "creating secret to update certificates")
}
}
if secret.Data == nil {
crLog.Info("creating CA and server certs")
if err := cr.refreshCerts(ctx, true, secret); err != nil {
crLog.Error(err, "could not refresh CA and server certs")
return false, nil
}
crLog.Info("CA and server certs created")
return true, nil
}
if !cr.validCACert(secret.Data[caCertName], secret.Data[caKeyName]) {
crLog.Info("refreshing CA and server certs")
if err := cr.refreshCerts(ctx, true, secret); err != nil {
crLog.Error(err, "could not refresh CA and server certs")
return false, nil
}
crLog.Info("CA and server certs refreshed")
return true, nil
}
if !cr.validServerCert(secret.Data[caCertName], secret.Data[certName], secret.Data[keyName]) {
crLog.Info("refreshing server certs")
if err := cr.refreshCerts(ctx, false, secret); err != nil {
crLog.Error(err, "could not refresh server certs")
return false, nil
}
crLog.Info("server certs refreshed")
return true, nil
}
crLog.Info("no cert refresh needed")
return true, nil
}
if err := wait.ExponentialBackoff(wait.Backoff{
Duration: 10 * time.Millisecond,
Factor: 2,
Jitter: 1,
Steps: 10,
}, refreshFn); err != nil {
return err
}
return nil
}
// refreshCerts refreshes the certs. If refreshCA is true the CA cert will be refreshed and the server certs will be rotated. We also kill any runner pods so that they will be recreated and pick new certs.
func (cr *CertRotator) refreshCerts(ctx context.Context, refreshCA bool, secret *corev1.Secret) error {
var caArtifacts *KeyPairArtifacts
var err error
now := time.Now()
begin := now.Add(-1 * time.Hour)
end := now.Add(cr.CertValidityDuration)
if refreshCA {
var err error
caArtifacts, err = cr.createCACert(begin, now.Add(cr.CAValidityDuration))
if err != nil {
return err
}
secrets := &corev1.SecretList{}
if err := cr.client.List(ctx, secrets, client.HasLabels([]string{infrav1.RunnerLabel})); err != nil {
return fmt.Errorf("listing secrets to refresh certificates: %w", err)
}
for _, secret := range secrets.Items {
certArtifacts, err := parseArtifacts("tls.crt", "tls.key", &secret)
if err != nil {
return err
}
hostname := certArtifacts.Cert.DNSNames[0]
cert, key, err := cr.createCertPEM(caArtifacts, hostname, begin, end)
if err != nil {
return err
}
if err := cr.writeSecret(cert, key, caArtifacts, &secret); err != nil {
return err
}
}
runnerPods := &corev1.PodList{}
if err := cr.client.List(ctx, runnerPods, client.HasLabels([]string{infrav1.RunnerLabel})); err != nil {
return fmt.Errorf("failed to list runner pod: %w", err)
}
for _, pod := range runnerPods.Items {
if err := cr.client.Delete(ctx, &pod); err != nil {
return fmt.Errorf("failed to restart runner pod: %w", err)
}
}
} else {
caArtifacts, err = parseArtifacts(caCertName, caKeyName, secret)
if err != nil {
return err
}
}
cert, key, err := cr.createCertPEM(caArtifacts, cr.DNSName, begin, end)
if err != nil {
return err
}
if err := cr.writeSecret(cert, key, caArtifacts, secret); err != nil {
return err
}
return nil
}
func (cr *CertRotator) RefreshRunnerCertIfNeeded(hostname string, secret *corev1.Secret) error {
var caArtifacts *KeyPairArtifacts
now := time.Now()
begin := now.Add(-1 * time.Hour)
end := now.Add(cr.CertValidityDuration)
caSecret := &corev1.Secret{}
if err := cr.client.Get(context.Background(), cr.SecretKey, caSecret); err != nil {
return err
}
caArtifacts, err := parseArtifacts(caCertName, caKeyName, caSecret)
if err != nil {
return err
}
cert, key, err := cr.createCertPEM(caArtifacts, hostname, begin, end)
if err != nil {
return err
}
if err := cr.writeSecret(cert, key, caArtifacts, secret); err != nil {
return err
}
return nil
}
func (cr *CertRotator) writeSecret(cert, key []byte, caArtifacts *KeyPairArtifacts, secret *corev1.Secret) error {
if secret.Data == nil {
secret.Data = make(map[string][]byte)
}
secret.Data[caCertName] = caArtifacts.CertPEM
secret.Data[caKeyName] = caArtifacts.KeyPEM
secret.Data[certName] = cert
secret.Data[keyName] = key
return cr.client.Update(context.Background(), secret)
}
func buildArtifactsFromSecret(secret *corev1.Secret) (*KeyPairArtifacts, *KeyPairArtifacts, error) {
caArtifacts, err := parseArtifacts(caCertName, caKeyName, secret)
if err != nil {
return nil, nil, err
}
certArtifacts, err := parseArtifacts(certName, keyName, secret)
if err != nil {
return nil, nil, err
}
return caArtifacts, certArtifacts, nil
}
func parseArtifacts(certName, keyName string, secret *corev1.Secret) (*KeyPairArtifacts, error) {
certPem, ok := secret.Data[certName]
if !ok {
return nil, errors.New(fmt.Sprintf("Cert secret is not well-formed, missing %s", caCertName))
}
keyPem, ok := secret.Data[keyName]
if !ok {
return nil, errors.New(fmt.Sprintf("Cert secret is not well-formed, missing %s", caKeyName))
}
certDer, _ := pem.Decode(certPem)
if certDer == nil {
return nil, errors.New("bad cert")
}
cert, err := x509.ParseCertificate(certDer.Bytes)
if err != nil {
return nil, errors.Wrap(err, "while parsing CA cert")
}
keyDer, _ := pem.Decode(keyPem)
if keyDer == nil {
return nil, errors.New("bad cert")
}
key, err := x509.ParsePKCS1PrivateKey(keyDer.Bytes)
if err != nil {
return nil, errors.Wrap(err, "while parsing key")
}
return &KeyPairArtifacts{
Cert: cert,
CertPEM: certPem,
KeyPEM: keyPem,
Key: key,
}, nil
}
// CreateCACert creates the self-signed CA cert and private key that will
// be used to sign the server certificate
func (cr *CertRotator) createCACert(begin, end time.Time) (*KeyPairArtifacts, error) {
templ := &x509.Certificate{
SerialNumber: big.NewInt(0),
Subject: pkix.Name{
CommonName: cr.CAName,
Organization: []string{cr.CAOrganization},
},
DNSNames: []string{
cr.DNSName,
},
NotBefore: begin,
NotAfter: end,
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment | x509.KeyUsageCertSign,
BasicConstraintsValid: true,
IsCA: true,
}
key, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
return nil, errors.Wrap(err, "generating key")
}
der, err := x509.CreateCertificate(rand.Reader, templ, templ, key.Public(), key)
if err != nil {
return nil, errors.Wrap(err, "creating certificate")
}
certPEM, keyPEM, err := pemEncode(der, key)
if err != nil {
return nil, errors.Wrap(err, "encoding PEM")
}
cert, err := x509.ParseCertificate(der)
if err != nil {
return nil, errors.Wrap(err, "parsing certificate")
}
return &KeyPairArtifacts{Cert: cert, Key: key, CertPEM: certPEM, KeyPEM: keyPEM}, nil
}
// CreateCertPEM takes the results of CreateCACert and uses it to create the
// PEM-encoded public certificate and private key, respectively
func (cr *CertRotator) createCertPEM(ca *KeyPairArtifacts, hostname string, begin, end time.Time) ([]byte, []byte, error) {
templ := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{
CommonName: cr.CAName,
},
DNSNames: []string{
hostname,
},
NotBefore: begin,
NotAfter: end,
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
BasicConstraintsValid: true,
}
key, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
return nil, nil, errors.Wrap(err, "generating key")
}
der, err := x509.CreateCertificate(rand.Reader, templ, ca.Cert, key.Public(), ca.Key)
if err != nil {
return nil, nil, errors.Wrap(err, "creating certificate")
}
certPEM, keyPEM, err := pemEncode(der, key)
if err != nil {
return nil, nil, errors.Wrap(err, "encoding PEM")
}
return certPEM, keyPEM, nil
}
// pemEncode takes a certificate and encodes it as PEM
func pemEncode(certificateDER []byte, key *rsa.PrivateKey) ([]byte, []byte, error) {
certBuf := &bytes.Buffer{}
if err := pem.Encode(certBuf, &pem.Block{Type: "CERTIFICATE", Bytes: certificateDER}); err != nil {
return nil, nil, errors.Wrap(err, "encoding cert")
}
keyBuf := &bytes.Buffer{}
if err := pem.Encode(keyBuf, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)}); err != nil {
return nil, nil, errors.Wrap(err, "encoding key")
}
return certBuf.Bytes(), keyBuf.Bytes(), nil
}
// ValidCert verifies if the cert is valid for the given hostname and time
func ValidCert(caCert, cert, key []byte, dnsName string, at time.Time) (bool, error) {
if len(caCert) == 0 || len(cert) == 0 || len(key) == 0 {
return false, errors.New("empty cert")
}
pool := x509.NewCertPool()
caDer, _ := pem.Decode(caCert)
if caDer == nil {
return false, errors.New("bad CA cert")
}
cac, err := x509.ParseCertificate(caDer.Bytes)
if err != nil {
return false, errors.Wrap(err, "parsing CA cert")
}
pool.AddCert(cac)
_, err = tls.X509KeyPair(cert, key)
if err != nil {
return false, errors.Wrap(err, "building key pair")
}
b, _ := pem.Decode(cert)
if b == nil {
return false, errors.New("bad private key")
}
crt, err := x509.ParseCertificate(b.Bytes)
if err != nil {
return false, errors.Wrap(err, "parsing cert")
}
_, err = crt.Verify(x509.VerifyOptions{
DNSName: dnsName,
Roots: pool,
CurrentTime: at,
})
if err != nil {
return false, errors.Wrap(err, "verifying cert")
}
return true, nil
}
func (cr *CertRotator) validCACert(cert, key []byte) bool {
valid, err := ValidCert(cert, cert, key, cr.CAName, cr.lookaheadTime())
if err != nil {
return false
}
return valid
}
func (cr *CertRotator) validServerCert(caCert, cert, key []byte) bool {
valid, err := ValidCert(caCert, cert, key, cr.DNSName, cr.lookaheadTime())
if err != nil {
return false
}
return valid
}
func (cr *CertRotator) lookaheadTime() time.Time {
return time.Now().Add(cr.LookaheadInterval)
}