/
certificates.go
529 lines (466 loc) · 16 KB
/
certificates.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
524
525
526
527
528
529
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package secret
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"crypto/x509/pkix"
"encoding/hex"
"math/big"
"path"
"strings"
"time"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/cert"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
"sigs.k8s.io/cluster-api/util/certs"
)
const (
rootOwnerValue = "root:root"
// DefaultCertificatesDir is the default directory where Kubernetes stores its PKI information.
DefaultCertificatesDir = "/etc/kubernetes/pki"
)
var (
// ErrMissingCertificate is an error indicating a certificate is entirely missing.
ErrMissingCertificate = errors.New("missing certificate")
// ErrMissingCrt is an error indicating the crt file is missing from the certificate.
ErrMissingCrt = errors.New("missing crt data")
// ErrMissingKey is an error indicating the key file is missing from the certificate.
ErrMissingKey = errors.New("missing key data")
)
// Certificates are the certificates necessary to bootstrap a cluster.
type Certificates []*Certificate
// NewCertificatesForInitialControlPlane returns a list of certificates configured for a control plane node.
func NewCertificatesForInitialControlPlane(config *bootstrapv1.ClusterConfiguration) Certificates {
certificatesDir := DefaultCertificatesDir
if config != nil && config.CertificatesDir != "" {
certificatesDir = config.CertificatesDir
}
certificates := Certificates{
&Certificate{
Purpose: ClusterCA,
CertFile: path.Join(certificatesDir, "ca.crt"),
KeyFile: path.Join(certificatesDir, "ca.key"),
},
&Certificate{
Purpose: ServiceAccount,
CertFile: path.Join(certificatesDir, "sa.pub"),
KeyFile: path.Join(certificatesDir, "sa.key"),
},
&Certificate{
Purpose: FrontProxyCA,
CertFile: path.Join(certificatesDir, "front-proxy-ca.crt"),
KeyFile: path.Join(certificatesDir, "front-proxy-ca.key"),
},
}
etcdCert := &Certificate{
Purpose: EtcdCA,
CertFile: path.Join(certificatesDir, "etcd", "ca.crt"),
KeyFile: path.Join(certificatesDir, "etcd", "ca.key"),
}
// TODO make sure all the fields are actually defined and return an error if not
if config != nil && config.Etcd.External != nil {
etcdCert = &Certificate{
Purpose: EtcdCA,
CertFile: config.Etcd.External.CAFile,
External: true,
}
apiserverEtcdClientCert := &Certificate{
Purpose: APIServerEtcdClient,
CertFile: config.Etcd.External.CertFile,
KeyFile: config.Etcd.External.KeyFile,
External: true,
}
certificates = append(certificates, apiserverEtcdClientCert)
}
certificates = append(certificates, etcdCert)
return certificates
}
// NewControlPlaneJoinCerts gets any certs that exist and writes them to disk.
func NewControlPlaneJoinCerts(config *bootstrapv1.ClusterConfiguration) Certificates {
certificatesDir := DefaultCertificatesDir
if config != nil && config.CertificatesDir != "" {
certificatesDir = config.CertificatesDir
}
certificates := Certificates{
&Certificate{
Purpose: ClusterCA,
CertFile: path.Join(certificatesDir, "ca.crt"),
KeyFile: path.Join(certificatesDir, "ca.key"),
},
&Certificate{
Purpose: ServiceAccount,
CertFile: path.Join(certificatesDir, "sa.pub"),
KeyFile: path.Join(certificatesDir, "sa.key"),
},
&Certificate{
Purpose: FrontProxyCA,
CertFile: path.Join(certificatesDir, "front-proxy-ca.crt"),
KeyFile: path.Join(certificatesDir, "front-proxy-ca.key"),
},
}
etcdCert := &Certificate{
Purpose: EtcdCA,
CertFile: path.Join(certificatesDir, "etcd", "ca.crt"),
KeyFile: path.Join(certificatesDir, "etcd", "ca.key"),
}
// TODO make sure all the fields are actually defined and return an error if not
if config != nil && config.Etcd.External != nil {
etcdCert = &Certificate{
Purpose: EtcdCA,
CertFile: config.Etcd.External.CAFile,
External: true,
}
apiserverEtcdClientCert := &Certificate{
Purpose: APIServerEtcdClient,
CertFile: config.Etcd.External.CertFile,
KeyFile: config.Etcd.External.KeyFile,
External: true,
}
certificates = append(certificates, apiserverEtcdClientCert)
}
certificates = append(certificates, etcdCert)
return certificates
}
// NewCertificatesForWorker return an initialized but empty set of CA certificates needed to bootstrap a cluster.
func NewCertificatesForWorker(caCertPath string) Certificates {
if caCertPath == "" {
caCertPath = path.Join(DefaultCertificatesDir, "ca.crt")
}
return Certificates{
&Certificate{
Purpose: ClusterCA,
CertFile: caCertPath,
},
}
}
// GetByPurpose returns a certificate by the given name.
// This could be removed if we use a map instead of a slice to hold certificates, however other code becomes more complex.
func (c Certificates) GetByPurpose(purpose Purpose) *Certificate {
for _, certificate := range c {
if certificate.Purpose == purpose {
return certificate
}
}
return nil
}
// Lookup looks up each certificate from secrets and populates the certificate with the secret data.
func (c Certificates) Lookup(ctx context.Context, ctrlclient client.Client, clusterName client.ObjectKey) error {
return c.LookupCached(ctx, nil, ctrlclient, clusterName)
}
// LookupCached looks up each certificate from secrets and populates the certificate with the secret data.
// First we try to lookup the certificate secret via the secretCachingClient. If we get a NotFound error
// we fall back to the regular uncached client.
func (c Certificates) LookupCached(ctx context.Context, secretCachingClient, ctrlclient client.Client, clusterName client.ObjectKey) error {
// Look up each certificate as a secret and populate the certificate/key
for _, certificate := range c {
key := client.ObjectKey{
Name: Name(clusterName.Name, certificate.Purpose),
Namespace: clusterName.Namespace,
}
s, err := getCertificateSecret(ctx, secretCachingClient, ctrlclient, key)
if err != nil {
if apierrors.IsNotFound(err) {
if certificate.External {
return errors.Wrap(err, "external certificate not found")
}
continue
}
return err
}
// If a user has a badly formatted secret it will prevent the cluster from working.
kp, err := secretToKeyPair(s)
if err != nil {
return errors.Wrapf(err, "failed to read keypair from certificate %s", klog.KObj(s))
}
certificate.KeyPair = kp
certificate.Secret = s
}
return nil
}
func getCertificateSecret(ctx context.Context, secretCachingClient, ctrlclient client.Client, key client.ObjectKey) (*corev1.Secret, error) {
secret := &corev1.Secret{}
if secretCachingClient != nil {
// Try to get the certificate via the cached client.
err := secretCachingClient.Get(ctx, key, secret)
if err != nil && !apierrors.IsNotFound(err) {
// Return error if we got an error which is not a NotFound error.
return nil, errors.Wrapf(err, "failed to get certificate %s", klog.KObj(secret))
}
if err == nil {
return secret, nil
}
}
// Try to get the certificate via the uncached client.
if err := ctrlclient.Get(ctx, key, secret); err != nil {
return nil, errors.Wrapf(err, "failed to get certificate %s", klog.KObj(secret))
}
return secret, nil
}
// EnsureAllExist ensure that there is some data present for every certificate.
func (c Certificates) EnsureAllExist() error {
for _, certificate := range c {
if certificate.KeyPair == nil {
return ErrMissingCertificate
}
if len(certificate.KeyPair.Cert) == 0 {
return errors.Wrapf(ErrMissingCrt, "for certificate: %s", certificate.Purpose)
}
if !certificate.External {
if len(certificate.KeyPair.Key) == 0 {
return errors.Wrapf(ErrMissingKey, "for certificate: %s", certificate.Purpose)
}
}
}
return nil
}
// Generate will generate any certificates that do not have KeyPair data.
func (c Certificates) Generate() error {
for _, certificate := range c {
if certificate.KeyPair == nil {
err := certificate.Generate()
if err != nil {
return err
}
}
}
return nil
}
// SaveGenerated will save any certificates that have been generated as Kubernetes secrets.
func (c Certificates) SaveGenerated(ctx context.Context, ctrlclient client.Client, clusterName client.ObjectKey, owner metav1.OwnerReference) error {
for _, certificate := range c {
if !certificate.Generated {
continue
}
s := certificate.AsSecret(clusterName, owner)
if err := ctrlclient.Create(ctx, s); err != nil {
return errors.WithStack(err)
}
certificate.Secret = s
}
return nil
}
// LookupOrGenerate is a convenience function that wraps cluster bootstrap certificate behavior.
func (c Certificates) LookupOrGenerate(ctx context.Context, ctrlclient client.Client, clusterName client.ObjectKey, owner metav1.OwnerReference) error {
return c.LookupOrGenerateCached(ctx, nil, ctrlclient, clusterName, owner)
}
// LookupOrGenerateCached is a convenience function that wraps cluster bootstrap certificate behavior.
// During lookup we first try to lookup the certificate secret via the secretCachingClient. If we get a NotFound error
// we fall back to the regular uncached client.
func (c Certificates) LookupOrGenerateCached(ctx context.Context, secretCachingClient, ctrlclient client.Client, clusterName client.ObjectKey, owner metav1.OwnerReference) error {
// Find the certificates that exist
if err := c.LookupCached(ctx, secretCachingClient, ctrlclient, clusterName); err != nil {
return err
}
// Generate the certificates that don't exist
if err := c.Generate(); err != nil {
return err
}
// Save any certificates that have been generated
return c.SaveGenerated(ctx, ctrlclient, clusterName, owner)
}
// Certificate represents a single certificate CA.
type Certificate struct {
Generated bool
External bool
Purpose Purpose
KeyPair *certs.KeyPair
CertFile, KeyFile string
Secret *corev1.Secret
}
// Hashes hashes all the certificates stored in a CA certificate.
func (c *Certificate) Hashes() ([]string, error) {
certificates, err := cert.ParseCertsPEM(c.KeyPair.Cert)
if err != nil {
return nil, errors.Wrapf(err, "unable to parse %s certificate", c.Purpose)
}
out := make([]string, 0)
for _, c := range certificates {
out = append(out, hashCert(c))
}
return out, nil
}
// hashCert calculates the sha256 of certificate.
func hashCert(certificate *x509.Certificate) string {
spkiHash := sha256.Sum256(certificate.RawSubjectPublicKeyInfo)
return "sha256:" + strings.ToLower(hex.EncodeToString(spkiHash[:]))
}
// AsSecret converts a single certificate into a Kubernetes secret.
func (c *Certificate) AsSecret(clusterName client.ObjectKey, owner metav1.OwnerReference) *corev1.Secret {
s := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: clusterName.Namespace,
Name: Name(clusterName.Name, c.Purpose),
Labels: map[string]string{
clusterv1.ClusterNameLabel: clusterName.Name,
},
},
Data: map[string][]byte{
TLSKeyDataName: c.KeyPair.Key,
TLSCrtDataName: c.KeyPair.Cert,
},
Type: clusterv1.ClusterSecretType,
}
if c.Generated {
s.SetOwnerReferences([]metav1.OwnerReference{owner})
}
return s
}
// AsFiles converts the certificate to a slice of Files that may have 0, 1 or 2 Files.
func (c *Certificate) AsFiles() []bootstrapv1.File {
out := make([]bootstrapv1.File, 0)
if len(c.KeyPair.Cert) > 0 {
out = append(out, bootstrapv1.File{
Path: c.CertFile,
Owner: rootOwnerValue,
Permissions: "0640",
Content: string(c.KeyPair.Cert),
})
}
if len(c.KeyPair.Key) > 0 {
out = append(out, bootstrapv1.File{
Path: c.KeyFile,
Owner: rootOwnerValue,
Permissions: "0600",
Content: string(c.KeyPair.Key),
})
}
return out
}
// Generate generates a certificate.
func (c *Certificate) Generate() error {
// Do not generate the APIServerEtcdClient key pair. It is user supplied
if c.Purpose == APIServerEtcdClient {
return nil
}
generator := generateCACert
if c.Purpose == ServiceAccount {
generator = generateServiceAccountKeys
}
kp, err := generator()
if err != nil {
return err
}
c.KeyPair = kp
c.Generated = true
return nil
}
// AsFiles converts a slice of certificates into bootstrap files.
func (c Certificates) AsFiles() []bootstrapv1.File {
certFiles := make([]bootstrapv1.File, 0)
if clusterCA := c.GetByPurpose(ClusterCA); clusterCA != nil {
certFiles = append(certFiles, clusterCA.AsFiles()...)
}
if etcdCA := c.GetByPurpose(EtcdCA); etcdCA != nil {
certFiles = append(certFiles, etcdCA.AsFiles()...)
}
if frontProxyCA := c.GetByPurpose(FrontProxyCA); frontProxyCA != nil {
certFiles = append(certFiles, frontProxyCA.AsFiles()...)
}
if serviceAccountKey := c.GetByPurpose(ServiceAccount); serviceAccountKey != nil {
certFiles = append(certFiles, serviceAccountKey.AsFiles()...)
}
// these will only exist if external etcd was defined and supplied by the user
if apiserverEtcdClientCert := c.GetByPurpose(APIServerEtcdClient); apiserverEtcdClientCert != nil {
certFiles = append(certFiles, apiserverEtcdClientCert.AsFiles()...)
}
return certFiles
}
func secretToKeyPair(s *corev1.Secret) (*certs.KeyPair, error) {
c, exists := s.Data[TLSCrtDataName]
if !exists {
return nil, errors.Errorf("missing data for key %s", TLSCrtDataName)
}
// In some cases (external etcd) it's ok if the etcd.key does not exist.
// TODO: some other function should ensure that the certificates we need exist.
key, exists := s.Data[TLSKeyDataName]
if !exists {
key = []byte("")
}
return &certs.KeyPair{
Cert: c,
Key: key,
}, nil
}
func generateCACert() (*certs.KeyPair, error) {
x509Cert, privKey, err := newCertificateAuthority()
if err != nil {
return nil, err
}
return &certs.KeyPair{
Cert: certs.EncodeCertPEM(x509Cert),
Key: certs.EncodePrivateKeyPEM(privKey),
}, nil
}
func generateServiceAccountKeys() (*certs.KeyPair, error) {
saCreds, err := certs.NewPrivateKey()
if err != nil {
return nil, err
}
saPub, err := certs.EncodePublicKeyPEM(&saCreds.PublicKey)
if err != nil {
return nil, err
}
return &certs.KeyPair{
Cert: saPub,
Key: certs.EncodePrivateKeyPEM(saCreds),
}, nil
}
// newCertificateAuthority creates new certificate and private key for the certificate authority.
func newCertificateAuthority() (*x509.Certificate, *rsa.PrivateKey, error) {
key, err := certs.NewPrivateKey()
if err != nil {
return nil, nil, err
}
c, err := newSelfSignedCACert(key)
if err != nil {
return nil, nil, err
}
return c, key, nil
}
// newSelfSignedCACert creates a CA certificate.
func newSelfSignedCACert(key *rsa.PrivateKey) (*x509.Certificate, error) {
cfg := certs.Config{
CommonName: "kubernetes",
}
now := time.Now().UTC()
tmpl := x509.Certificate{
SerialNumber: new(big.Int).SetInt64(0),
Subject: pkix.Name{
CommonName: cfg.CommonName,
Organization: cfg.Organization,
},
NotBefore: now.Add(time.Minute * -5),
NotAfter: now.Add(time.Hour * 24 * 365 * 10), // 10 years
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
MaxPathLenZero: true,
BasicConstraintsValid: true,
MaxPathLen: 0,
IsCA: true,
}
b, err := x509.CreateCertificate(rand.Reader, &tmpl, &tmpl, key.Public(), key)
if err != nil {
return nil, errors.Wrapf(err, "failed to create self signed CA certificate: %+v", tmpl)
}
c, err := x509.ParseCertificate(b)
return c, errors.WithStack(err)
}