forked from Azure/acs-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pki.go
196 lines (166 loc) · 5.42 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
package acsengine
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"errors"
"fmt"
"math/big"
"net"
"os"
"time"
)
const (
ValidityDuration = time.Hour * 24 * 365 * 2
PkiKeySize = 4096
)
type PkiKeyCertPair struct {
CertificatePem string
PrivateKeyPem string
}
func CreatePki(extraFQDNs []string, extraIPs []net.IP, clusterDomain string, caPair *PkiKeyCertPair) (*PkiKeyCertPair, *PkiKeyCertPair, *PkiKeyCertPair, error) {
start := time.Now()
defer func(s time.Time) {
fmt.Fprintf(os.Stderr, "cert creation took %s\n", 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
)
errors := make(chan error)
var err error
caCertificate, err = pemToCertificate(caPair.CertificatePem)
if err != nil {
return nil, nil, nil, err
}
caPrivateKey, err = pemToKey(caPair.PrivateKeyPem)
if err != nil {
return nil, nil, nil, err
}
go func() {
var err error
apiServerCertificate, apiServerPrivateKey, err = createCertificate("apiserver", caCertificate, caPrivateKey, true, extraFQDNs, extraIPs)
errors <- err
}()
go func() {
var err error
clientCertificate, clientPrivateKey, err = createCertificate("client", caCertificate, caPrivateKey, false, nil, nil)
errors <- err
}()
go func() {
var err error
kubeConfigCertificate, kubeConfigPrivateKey, err = createCertificate("client", caCertificate, caPrivateKey, false, nil, nil)
errors <- err
}()
e1 := <-errors
e2 := <-errors
e3 := <-errors
if e1 != nil {
return nil, nil, nil, e1
}
if e2 != nil {
return nil, nil, nil, e2
}
if e3 != nil {
return nil, nil, nil, e2
}
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))},
nil
}
func createCertificate(commonName string, caCertificate *x509.Certificate, caPrivateKey *rsa.PrivateKey, isServer bool, extraFQDNs []string, extraIPs []net.IP) (*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 isCA {
template.KeyUsage |= x509.KeyUsageCertSign
template.IsCA = isCA
} else if isServer {
extraIPs = append(extraIPs, net.ParseIP("10.0.0.1"))
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, err := 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)
}