forked from bsteciuk/kismatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pki.go
451 lines (411 loc) · 14.1 KB
/
pki.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
package install
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
"time"
"github.com/apprenda/kismatic/pkg/tls"
"github.com/apprenda/kismatic/pkg/util"
"github.com/cloudflare/cfssl/csr"
)
const (
adminUser = "admin"
adminGroup = "system:masters"
adminCertFilename = "admin"
adminCertFilenameKETPre133 = "admin"
serviceAccountCertFilename = "service-account"
serviceAccountCertCommonName = "kube-service-account"
schedulerCertFilenamePrefix = "kube-scheduler"
schedulerUser = "system:kube-scheduler"
controllerManagerCertFilenamePrefix = "kube-controller-manager"
controllerManagerUser = "system:kube-controller-manager"
kubeletUserPrefix = "system:node"
kubeletGroup = "system:nodes"
kubeAPIServerKubeletClientClientFilename = "apiserver-kubelet-client"
kubeAPIServerKubeletClientClientCommonName = "kube-apiserver-kubelet-client"
contivProxyServerCertFilename = "contiv-proxy-server"
proxyClientCACommonName = "proxyClientCA"
proxyClientCertFilename = "proxy-client"
proxyClientCertCommonName = "aggregator"
)
// The PKI provides a way for generating certificates for the cluster described by the Plan
type PKI interface {
CertificateAuthorityExists() (bool, error)
GenerateClusterCA(p *Plan) (*tls.CA, error)
GetClusterCA() (*tls.CA, error)
GenerateProxyClientCA(p *Plan) (*tls.CA, error)
GetProxyClientCA() (*tls.CA, error)
GenerateClusterCertificates(p *Plan, clusterCA *tls.CA, proxyClientCA *tls.CA) error
NodeCertificateExists(node Node) (bool, error)
GenerateNodeCertificate(plan *Plan, node Node, ca *tls.CA) error
GenerateCertificate(name string, validityPeriod string, commonName string, subjectAlternateNames []string, organizations []string, ca *tls.CA, overwrite bool) (bool, error)
}
// LocalPKI is a file-based PKI
type LocalPKI struct {
CACsr string
GeneratedCertsDirectory string
Log io.Writer
}
type certificateSpec struct {
description string
filename string
commonName string
subjectAlternateNames []string
organizations []string
ca *tls.CA
}
func (s certificateSpec) equal(other certificateSpec) bool {
prelimEqual := s.description == other.description &&
s.filename == other.filename &&
s.commonName == other.commonName &&
len(s.subjectAlternateNames) == len(other.subjectAlternateNames) &&
len(s.organizations) == len(other.organizations)
if !prelimEqual {
return false
}
// Compare subject alt. names
thisSAN := make([]string, len(s.subjectAlternateNames))
otherSAN := make([]string, len(other.subjectAlternateNames))
// Clone and sort
copy(thisSAN, s.subjectAlternateNames)
copy(otherSAN, other.subjectAlternateNames)
sort.Strings(thisSAN)
sort.Strings(otherSAN)
for _, x := range thisSAN {
for _, y := range otherSAN {
if x != y {
return false
}
}
}
// Compare organizations
thisOrgs := make([]string, len(s.organizations))
otherOrgs := make([]string, len(other.organizations))
// clone and sort
copy(thisOrgs, s.organizations)
copy(otherOrgs, other.organizations)
sort.Strings(thisOrgs)
sort.Strings(otherOrgs)
for _, x := range thisOrgs {
for _, y := range otherOrgs {
if x != y {
return false
}
}
}
return true
}
// CertificateAuthorityExists returns true if the CA for the cluster exists
func (lp *LocalPKI) CertificateAuthorityExists() (bool, error) {
return tls.CertKeyPairExists("ca", lp.GeneratedCertsDirectory)
}
// GenerateClusterCA creates a Certificate Authority for the cluster
func (lp *LocalPKI) GenerateClusterCA(p *Plan) (*tls.CA, error) {
exists, err := tls.CertKeyPairExists("ca", lp.GeneratedCertsDirectory)
if err != nil {
return nil, fmt.Errorf("error verifying CA certificate/key: %v", err)
}
if exists {
return lp.GetClusterCA()
}
// CA keypair doesn't exist, generate one
util.PrettyPrintOk(lp.Log, "Generating cluster Certificate Authority")
key, cert, err := tls.NewCACert(lp.CACsr, p.Cluster.Name, p.Cluster.Certificates.CAExpiry)
if err != nil {
return nil, fmt.Errorf("failed to create CA Cert: %v", err)
}
if err = tls.WriteCert(key, cert, "ca", lp.GeneratedCertsDirectory); err != nil {
return nil, fmt.Errorf("error writing CA files: %v", err)
}
return &tls.CA{
Cert: cert,
Key: key,
}, nil
}
// GetClusterCA returns the cluster CA
func (lp *LocalPKI) GetClusterCA() (*tls.CA, error) {
key, cert, err := tls.ReadCACert("ca", lp.GeneratedCertsDirectory)
if err != nil {
return nil, fmt.Errorf("error reading CA certificate/key: %v", err)
}
return &tls.CA{
Cert: cert,
Key: key,
}, nil
}
// GenerateProxyClientCA creates a Certificate Authority for the cluster
func (lp *LocalPKI) GenerateProxyClientCA(p *Plan) (*tls.CA, error) {
exists, err := tls.CertKeyPairExists("proxy-client-ca", lp.GeneratedCertsDirectory)
if err != nil {
return nil, fmt.Errorf("error verifying proxy-client CA certificate/key: %v", err)
}
if exists {
return lp.GetProxyClientCA()
}
// CA keypair doesn't exist, generate one
util.PrettyPrintOk(lp.Log, "Generating proxy-client Certificate Authority")
key, cert, err := tls.NewCACert(lp.CACsr, proxyClientCACommonName, p.Cluster.Certificates.CAExpiry)
if err != nil {
return nil, fmt.Errorf("failed to create proxy-client CA Cert: %v", err)
}
if err = tls.WriteCert(key, cert, "proxy-client-ca", lp.GeneratedCertsDirectory); err != nil {
return nil, fmt.Errorf("error writing proxy-client CA files: %v", err)
}
return &tls.CA{
Cert: cert,
Key: key,
}, nil
}
// GetProxyClientCA returns the cluster CA
func (lp *LocalPKI) GetProxyClientCA() (*tls.CA, error) {
key, cert, err := tls.ReadCACert("proxy-client-ca", lp.GeneratedCertsDirectory)
if err != nil {
return nil, fmt.Errorf("error reading proxy-client CA certificate/key: %v", err)
}
return &tls.CA{
Cert: cert,
Key: key,
}, nil
}
// GenerateClusterCertificates creates all certificates required for the cluster
// described in the plan file.
func (lp *LocalPKI) GenerateClusterCertificates(p *Plan, clusterCA *tls.CA, proxyClientCA *tls.CA) error {
if lp.Log == nil {
lp.Log = ioutil.Discard
}
manifest, err := p.certSpecs(clusterCA, proxyClientCA)
if err != nil {
return err
}
for _, s := range manifest {
exists, err := tls.CertKeyPairExists(s.filename, lp.GeneratedCertsDirectory)
if err != nil {
return err
}
// Pre-existing admin certificates from KET < 1.3.3 are not valid
// due to changes required for RBAC. Rename it if necessary.
if exists && s.filename == adminCertFilenameKETPre133 {
ok, err := renamePre133AdminCert(s.filename, lp.GeneratedCertsDirectory)
if err != nil {
return err
}
// We renamed it, so it doesn't exist anymore
if ok {
util.PrettyPrintWarn(lp.Log, "Existing admin certificate is invalid. Backing up and regenerating.")
exists = false
}
}
if exists {
warnings, err := tls.CertValid(s.commonName, s.subjectAlternateNames, s.organizations, s.filename, lp.GeneratedCertsDirectory)
if err != nil {
return err
}
if len(warnings) > 0 {
util.PrettyPrintErr(lp.Log, "Found certificate for %s, but it is not valid", s.description)
util.PrintValidationErrors(lp.Log, warnings)
return fmt.Errorf("invalid certificate found for %q", s.description)
}
// This cert is valid, move onto the next certificate
util.PrettyPrintOk(lp.Log, "Found valid certificate for %s", s.description)
continue
}
// Cert doesn't exist. Generate it
if err := generateCert(lp.GeneratedCertsDirectory, s, p.Cluster.Certificates.Expiry); err != nil {
return err
}
util.PrettyPrintOk(lp.Log, "Generated certificate for %s", s.description)
}
return nil
}
// Validates that the certificate was generated by us. If so, renames it
// to make a backup and returns true. Otherwise returns false.
func renamePre133AdminCert(filename, dir string) (bool, error) {
cert, err := tls.ReadCert(filename, dir)
if err != nil {
return false, fmt.Errorf("error reading admin certificate: %v", err)
}
// Ensure it was generated by us
if len(cert.Subject.Organization) == 1 && cert.Subject.Organization[0] == "Apprenda" &&
len(cert.Subject.OrganizationalUnit) == 1 && cert.Subject.OrganizationalUnit[0] == "Kismatic" &&
len(cert.Subject.Country) == 1 && cert.Subject.Country[0] == "US" &&
len(cert.Subject.Province) == 1 && cert.Subject.Province[0] == "NY" &&
len(cert.Subject.Locality) == 1 && cert.Subject.Locality[0] == "Troy" {
certFile := filepath.Join(dir, filename+".pem")
if err = os.Rename(certFile, certFile+".bak"); err != nil {
return false, fmt.Errorf("error backing up existing admin certificate: %v", err)
}
return true, nil
}
return false, nil
}
// ValidateClusterCertificates validates any certificates that already exist
// in the expected directory.
func (lp *LocalPKI) ValidateClusterCertificates(p *Plan) (warns []error, errs []error) {
if lp.Log == nil {
lp.Log = ioutil.Discard
}
manifest, err := p.certSpecs(nil, nil)
if err != nil {
return nil, []error{err}
}
for _, s := range manifest {
exists, err := tls.CertKeyPairExists(s.filename, lp.GeneratedCertsDirectory)
if err != nil {
errs = append(errs, err)
continue
}
if !exists {
continue // nothing to validate... move on
}
warn, err := tls.CertValid(s.commonName, s.subjectAlternateNames, s.organizations, s.filename, lp.GeneratedCertsDirectory)
if err != nil {
errs = append(errs, err)
}
if len(warn) > 0 {
warns = append(warns, warn...)
}
}
return warns, errs
}
// NodeCertificateExists returns true if the node's key and certificate exist
func (lp *LocalPKI) NodeCertificateExists(node Node) (bool, error) {
return tls.CertKeyPairExists(node.Host, lp.GeneratedCertsDirectory)
}
// GenerateNodeCertificate creates a private key and certificate for the given node
func (lp *LocalPKI) GenerateNodeCertificate(plan *Plan, node Node, ca *tls.CA) error {
m, err := node.certSpecs(*plan, ca)
if err != nil {
return err
}
for _, s := range m {
exists, err := tls.CertKeyPairExists(s.filename, lp.GeneratedCertsDirectory)
if err != nil {
return err
}
if exists {
warn, err := tls.CertValid(s.commonName, s.subjectAlternateNames, s.organizations, s.filename, lp.GeneratedCertsDirectory)
if err != nil {
return err
}
if len(warn) > 0 {
util.PrettyPrintErr(lp.Log, "Found certificate for %s, but it is not valid", s.description)
util.PrintValidationErrors(lp.Log, warn)
return fmt.Errorf("invalid certificate found for %q", s.description)
}
// This cert is valid, move on
util.PrettyPrintOk(lp.Log, "Found valid certificate for %s", s.description)
continue
}
// Cert doesn't exist. Generate it
if err := generateCert(lp.GeneratedCertsDirectory, s, plan.Cluster.Certificates.Expiry); err != nil {
return err
}
util.PrettyPrintOk(lp.Log, "Generated certificate for %s", s.description)
}
return nil
}
// GenerateCertificate creates a private key and certificate for the given name, CN, subjectAlternateNames and organizations
// If cert exists, will not fail
// Pass overwrite to replace an existing cert
func (lp *LocalPKI) GenerateCertificate(name string, validityPeriod string, commonName string, subjectAlternateNames []string, organizations []string, ca *tls.CA, overwrite bool) (bool, error) {
if name == "" {
return false, fmt.Errorf("name cannot be empty")
}
if validityPeriod == "" {
return false, fmt.Errorf("validityPeriod cannot be empty")
}
if ca == nil {
return false, fmt.Errorf("ca cannot be nil")
}
exists, err := tls.CertKeyPairExists(name, lp.GeneratedCertsDirectory)
if err != nil {
return false, fmt.Errorf("could not determine if certificate for %s exists: %v", name, err)
}
// if exists and overwrite == false return
// otherwise cert will be created and replaced if exists
if exists && !overwrite {
return true, nil
}
spec := certificateSpec{
description: name,
filename: name,
commonName: commonName,
subjectAlternateNames: subjectAlternateNames,
organizations: organizations,
ca: ca,
}
if err := generateCert(lp.GeneratedCertsDirectory, spec, validityPeriod); err != nil {
return exists, fmt.Errorf("could not generate certificate %s: %v", name, err)
}
return exists, nil
}
func generateCert(certDir string, spec certificateSpec, expiryStr string) error {
expiry, err := time.ParseDuration(expiryStr)
if err != nil {
return fmt.Errorf("%q is not a valid duration for certificate expiry", expiryStr)
}
req := csr.CertificateRequest{
CN: spec.commonName,
KeyRequest: &csr.BasicKeyRequest{
A: "rsa",
S: 2048,
},
}
if len(spec.subjectAlternateNames) > 0 {
req.Hosts = spec.subjectAlternateNames
}
for _, org := range spec.organizations {
name := csr.Name{O: org}
req.Names = append(req.Names, name)
}
key, cert, err := tls.NewCert(spec.ca, req, expiry)
if err != nil {
return fmt.Errorf("error generating certs for %q: %v", spec.description, err)
}
if err = tls.WriteCert(key, cert, spec.filename, certDir); err != nil {
return fmt.Errorf("error writing cert for %q: %v", spec.description, err)
}
return nil
}
func clusterCertsSubjectAlternateNames(plan Plan) ([]string, error) {
kubeServiceIP, err := getKubernetesServiceIP(&plan)
if err != nil {
return nil, fmt.Errorf("Error getting kubernetes service IP: %v", err)
}
defaultCertHosts := []string{
"kubernetes",
"kubernetes.default",
"kubernetes.default.svc",
"kubernetes.default.svc.cluster.local",
"127.0.0.1",
kubeServiceIP,
}
return defaultCertHosts, nil
}
func contains(x string, xs []string) bool {
for _, s := range xs {
if x == s {
return true
}
}
return false
}
func containsAny(x []string, xs []string) bool {
for _, s := range x {
if contains(s, xs) {
return true
}
}
return false
}
func certSpecInManifest(spec certificateSpec, manifest []certificateSpec) bool {
for _, s := range manifest {
if s.equal(spec) {
return true
}
}
return false
}