forked from kubernetes-retired/bootkube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tls.go
115 lines (99 loc) · 2.97 KB
/
tls.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
package asset
import (
"crypto/rsa"
"crypto/x509"
"net"
"github.com/kubernetes-incubator/bootkube/pkg/tlsutil"
)
func newTLSAssets(caCert *x509.Certificate, caPrivKey *rsa.PrivateKey, altNames tlsutil.AltNames) ([]Asset, error) {
var (
assets []Asset
err error
)
if caCert == nil {
caPrivKey, caCert, err = newCACert()
if err != nil {
return assets, err
}
}
apiKey, apiCert, err := newAPIKeyAndCert(caCert, caPrivKey, altNames)
if err != nil {
return assets, err
}
saPrivKey, err := tlsutil.NewPrivateKey()
if err != nil {
return assets, err
}
saPubKey, err := tlsutil.EncodePublicKeyPEM(&saPrivKey.PublicKey)
if err != nil {
return assets, err
}
kubeletKey, kubeletCert, err := newKubeletKeyAndCert(caCert, caPrivKey)
if err != nil {
return assets, err
}
assets = append(assets, []Asset{
{Name: AssetPathCAKey, Data: tlsutil.EncodePrivateKeyPEM(caPrivKey)},
{Name: AssetPathCACert, Data: tlsutil.EncodeCertificatePEM(caCert)},
{Name: AssetPathAPIServerKey, Data: tlsutil.EncodePrivateKeyPEM(apiKey)},
{Name: AssetPathAPIServerCert, Data: tlsutil.EncodeCertificatePEM(apiCert)},
{Name: AssetPathServiceAccountPrivKey, Data: tlsutil.EncodePrivateKeyPEM(saPrivKey)},
{Name: AssetPathServiceAccountPubKey, Data: saPubKey},
{Name: AssetPathKubeletKey, Data: tlsutil.EncodePrivateKeyPEM(kubeletKey)},
{Name: AssetPathKubeletCert, Data: tlsutil.EncodeCertificatePEM(kubeletCert)},
}...)
return assets, nil
}
func newCACert() (*rsa.PrivateKey, *x509.Certificate, error) {
key, err := tlsutil.NewPrivateKey()
if err != nil {
return nil, nil, err
}
config := tlsutil.CertConfig{
CommonName: "kube-ca",
Organization: []string{"kube-aws"},
}
cert, err := tlsutil.NewSelfSignedCACertificate(config, key)
if err != nil {
return nil, nil, err
}
return key, cert, err
}
func newAPIKeyAndCert(caCert *x509.Certificate, caPrivKey *rsa.PrivateKey, altNames tlsutil.AltNames) (*rsa.PrivateKey, *x509.Certificate, error) {
key, err := tlsutil.NewPrivateKey()
if err != nil {
return nil, nil, err
}
altNames.IPs = append(altNames.IPs, net.ParseIP("10.3.0.1"))
altNames.DNSNames = append(altNames.DNSNames, []string{
"kubernetes",
"kubernetes.default",
"kubernetes.default.svc",
"kubernetes.default.svc.cluster.local",
}...)
config := tlsutil.CertConfig{
CommonName: "kube-apiserver",
Organization: []string{"kube-master"},
AltNames: altNames,
}
cert, err := tlsutil.NewSignedCertificate(config, key, caCert, caPrivKey)
if err != nil {
return nil, nil, err
}
return key, cert, err
}
func newKubeletKeyAndCert(caCert *x509.Certificate, caPrivKey *rsa.PrivateKey) (*rsa.PrivateKey, *x509.Certificate, error) {
key, err := tlsutil.NewPrivateKey()
if err != nil {
return nil, nil, err
}
config := tlsutil.CertConfig{
CommonName: "kubelet",
Organization: []string{"kube-node"},
}
cert, err := tlsutil.NewSignedCertificate(config, key, caCert, caPrivKey)
if err != nil {
return nil, nil, err
}
return key, cert, err
}