This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 522
/
pki.go
250 lines (213 loc) · 8.36 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
package helpers
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"errors"
"fmt"
"math/big"
"net"
"time"
"golang.org/x/sync/errgroup"
log "github.com/sirupsen/logrus"
)
const (
// ValidityDuration specifies the duration an TLS certificate is valid
ValidityDuration = time.Hour * 24 * 365 * 30
// PkiKeySize is the size in bytes of the PKI key
PkiKeySize = 4096
)
// PkiKeyCertPair represents an PKI public and private cert pair
type PkiKeyCertPair struct {
CertificatePem string
PrivateKeyPem string
}
// CreatePkiKeyCertPair generates a pair of PKI certificate and private key
func CreatePkiKeyCertPair(commonName string) (*PkiKeyCertPair, error) {
caCertificate, caPrivateKey, err := createCertificate(commonName, nil, nil, false, false, nil, nil, nil)
if err != nil {
return nil, err
}
caPair := &PkiKeyCertPair{CertificatePem: string(certificateToPem(caCertificate.Raw)), PrivateKeyPem: string(privateKeyToPem(caPrivateKey))}
return caPair, nil
}
// CreatePki creates PKI certificates
func CreatePki(extraFQDNs []string, extraIPs []net.IP, clusterDomain string, caPair *PkiKeyCertPair, masterCount int) (*PkiKeyCertPair, *PkiKeyCertPair, *PkiKeyCertPair, *PkiKeyCertPair, *PkiKeyCertPair, []*PkiKeyCertPair, error) {
start := time.Now()
defer func(s time.Time) {
log.Debugf("pki: PKI asset creation took %s", time.Since(s))
}(start)
extraFQDNs = append(extraFQDNs, fmt.Sprintf("kubernetes"))
extraFQDNs = append(extraFQDNs, fmt.Sprintf("kubernetes.default"))
extraFQDNs = append(extraFQDNs, fmt.Sprintf("kubernetes.default.svc"))
extraFQDNs = append(extraFQDNs, fmt.Sprintf("kubernetes.default.svc.%s", clusterDomain))
extraFQDNs = append(extraFQDNs, fmt.Sprintf("kubernetes.kube-system"))
extraFQDNs = append(extraFQDNs, fmt.Sprintf("kubernetes.kube-system.svc"))
extraFQDNs = append(extraFQDNs, fmt.Sprintf("kubernetes.kube-system.svc.%s", clusterDomain))
var (
caCertificate *x509.Certificate
caPrivateKey *rsa.PrivateKey
apiServerCertificate *x509.Certificate
apiServerPrivateKey *rsa.PrivateKey
clientCertificate *x509.Certificate
clientPrivateKey *rsa.PrivateKey
kubeConfigCertificate *x509.Certificate
kubeConfigPrivateKey *rsa.PrivateKey
etcdServerCertificate *x509.Certificate
etcdServerPrivateKey *rsa.PrivateKey
etcdClientCertificate *x509.Certificate
etcdClientPrivateKey *rsa.PrivateKey
etcdPeerCertPairs []*PkiKeyCertPair
)
var group errgroup.Group
var err error
caCertificate, err = pemToCertificate(caPair.CertificatePem)
if err != nil {
return nil, nil, nil, nil, nil, nil, err
}
caPrivateKey, err = pemToKey(caPair.PrivateKeyPem)
if err != nil {
return nil, nil, nil, nil, nil, nil, err
}
group.Go(func() (err error) {
apiServerCertificate, apiServerPrivateKey, err = createCertificate("apiserver", caCertificate, caPrivateKey, false, true, extraFQDNs, extraIPs, nil)
return err
})
group.Go(func() (err error) {
organization := make([]string, 1)
organization[0] = "system:masters"
clientCertificate, clientPrivateKey, err = createCertificate("client", caCertificate, caPrivateKey, false, false, nil, nil, organization)
return err
})
group.Go(func() (err error) {
organization := make([]string, 1)
organization[0] = "system:masters"
kubeConfigCertificate, kubeConfigPrivateKey, err = createCertificate("client", caCertificate, caPrivateKey, false, false, nil, nil, organization)
return err
})
group.Go(func() (err error) {
etcdServerCertificate, etcdServerPrivateKey, err = createCertificate("etcdserver", caCertificate, caPrivateKey, true, true, nil, extraIPs, nil)
return err
})
group.Go(func() (err error) {
etcdClientCertificate, etcdClientPrivateKey, err = createCertificate("etcdclient", caCertificate, caPrivateKey, true, false, nil, extraIPs, nil)
return err
})
etcdPeerCertPairs = make([]*PkiKeyCertPair, masterCount)
for i := 0; i < masterCount; i++ {
i := i
group.Go(func() (err error) {
etcdPeerCertificate, etcdPeerPrivateKey, err := createCertificate("etcdpeer", caCertificate, caPrivateKey, true, false, nil, extraIPs, nil)
etcdPeerCertPairs[i] = &PkiKeyCertPair{CertificatePem: string(certificateToPem(etcdPeerCertificate.Raw)), PrivateKeyPem: string(privateKeyToPem(etcdPeerPrivateKey))}
return err
})
}
if err := group.Wait(); err != nil {
return nil, nil, nil, nil, nil, nil, err
}
return &PkiKeyCertPair{CertificatePem: string(certificateToPem(apiServerCertificate.Raw)), PrivateKeyPem: string(privateKeyToPem(apiServerPrivateKey))},
&PkiKeyCertPair{CertificatePem: string(certificateToPem(clientCertificate.Raw)), PrivateKeyPem: string(privateKeyToPem(clientPrivateKey))},
&PkiKeyCertPair{CertificatePem: string(certificateToPem(kubeConfigCertificate.Raw)), PrivateKeyPem: string(privateKeyToPem(kubeConfigPrivateKey))},
&PkiKeyCertPair{CertificatePem: string(certificateToPem(etcdServerCertificate.Raw)), PrivateKeyPem: string(privateKeyToPem(etcdServerPrivateKey))},
&PkiKeyCertPair{CertificatePem: string(certificateToPem(etcdClientCertificate.Raw)), PrivateKeyPem: string(privateKeyToPem(etcdClientPrivateKey))},
etcdPeerCertPairs,
nil
}
func createCertificate(commonName string, caCertificate *x509.Certificate, caPrivateKey *rsa.PrivateKey, isEtcd bool, isServer bool, extraFQDNs []string, extraIPs []net.IP, organization []string) (*x509.Certificate, *rsa.PrivateKey, error) {
var err error
isCA := (caCertificate == nil)
now := time.Now()
template := x509.Certificate{
Subject: pkix.Name{CommonName: commonName},
NotBefore: now,
NotAfter: now.Add(ValidityDuration),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
BasicConstraintsValid: true,
}
if organization != nil {
template.Subject.Organization = organization
}
if isCA {
template.KeyUsage |= x509.KeyUsageCertSign
template.IsCA = isCA
} else if isEtcd {
if commonName == "etcdServer" {
template.IPAddresses = extraIPs
template.ExtKeyUsage = append(template.ExtKeyUsage, x509.ExtKeyUsageServerAuth)
} else if commonName == "etcdClient" {
template.IPAddresses = extraIPs
template.ExtKeyUsage = append(template.ExtKeyUsage, x509.ExtKeyUsageClientAuth)
} else {
template.IPAddresses = extraIPs
template.ExtKeyUsage = append(template.ExtKeyUsage, x509.ExtKeyUsageServerAuth)
template.ExtKeyUsage = append(template.ExtKeyUsage, x509.ExtKeyUsageClientAuth)
}
} else if isServer {
template.DNSNames = extraFQDNs
template.IPAddresses = extraIPs
template.ExtKeyUsage = append(template.ExtKeyUsage, x509.ExtKeyUsageServerAuth)
} else {
template.ExtKeyUsage = append(template.ExtKeyUsage, x509.ExtKeyUsageClientAuth)
}
snMax := new(big.Int).Lsh(big.NewInt(1), 128)
template.SerialNumber, err = rand.Int(rand.Reader, snMax)
if err != nil {
return nil, nil, err
}
privateKey, _ := rsa.GenerateKey(rand.Reader, PkiKeySize)
var privateKeyToUse *rsa.PrivateKey
var certificateToUse *x509.Certificate
if !isCA {
privateKeyToUse = caPrivateKey
certificateToUse = caCertificate
} else {
privateKeyToUse = privateKey
certificateToUse = &template
}
certDerBytes, err := x509.CreateCertificate(rand.Reader, &template, certificateToUse, &privateKey.PublicKey, privateKeyToUse)
if err != nil {
return nil, nil, err
}
certificate, err := x509.ParseCertificate(certDerBytes)
if err != nil {
return nil, nil, err
}
return certificate, privateKey, nil
}
func certificateToPem(derBytes []byte) []byte {
pemBlock := &pem.Block{
Type: "CERTIFICATE",
Bytes: derBytes,
}
pemBuffer := bytes.Buffer{}
pem.Encode(&pemBuffer, pemBlock)
return pemBuffer.Bytes()
}
func privateKeyToPem(privateKey *rsa.PrivateKey) []byte {
pemBlock := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
}
pemBuffer := bytes.Buffer{}
pem.Encode(&pemBuffer, pemBlock)
return pemBuffer.Bytes()
}
func pemToCertificate(raw string) (*x509.Certificate, error) {
cpb, _ := pem.Decode([]byte(raw))
if cpb == nil {
return nil, errors.New("The raw pem is not a valid PEM formatted block")
}
return x509.ParseCertificate(cpb.Bytes)
}
func pemToKey(raw string) (*rsa.PrivateKey, error) {
kpb, _ := pem.Decode([]byte(raw))
if kpb == nil {
return nil, errors.New("The raw pem is not a valid PEM formatted block")
}
return x509.ParsePKCS1PrivateKey(kpb.Bytes)
}