This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 173
/
pki.go
242 lines (204 loc) · 7.26 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
/*
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 pki
import (
cryptorand "crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"net"
"k8s.io/client-go/util/cert"
tenancyv1alpha1 "sigs.k8s.io/multi-tenancy/incubator/virtualcluster/pkg/apis/tenancy/v1alpha1"
"sigs.k8s.io/multi-tenancy/incubator/virtualcluster/pkg/syncer/conversion"
pkiutil "sigs.k8s.io/multi-tenancy/incubator/virtualcluster/pkg/util/pki"
)
const (
defaultClusterDomain = "cluster.local"
)
type CrtKeyPair struct {
Crt *x509.Certificate
Key *rsa.PrivateKey
}
type ClusterCAGroup struct {
RootCA *CrtKeyPair
APIServer *CrtKeyPair
ETCD *CrtKeyPair
CtrlMgrKbCfg string // the kubeconfig used by controller-manager
AdminKbCfg string // the kubeconfig used by admin user
ServiceAccountPrivateKey *rsa.PrivateKey
}
// NewAPIServerCertAndKey creates crt and key for apiserver using ca.
func NewAPIServerCrtAndKey(ca *CrtKeyPair, vc *tenancyv1alpha1.VirtualCluster, apiserverDomain string, apiserverIPs ...string) (*CrtKeyPair, error) {
clusterDomain := defaultClusterDomain
if vc.Spec.ClusterDomain != "" {
clusterDomain = vc.Spec.ClusterDomain
}
// create AltNames with defaults DNSNames/IPs
altNames := &cert.AltNames{
DNSNames: []string{
"kubernetes",
"kubernetes.default",
"kubernetes.default.svc",
fmt.Sprintf("kubernetes.default.svc.%s", clusterDomain),
apiserverDomain,
// add virtual cluster name (i.e. namespace) for vn-agent
vc.Name,
},
}
for _, ip := range apiserverIPs {
if ip != "" {
altNames.IPs = append(altNames.IPs, net.ParseIP(ip))
}
}
config := &pkiutil.CertConfig{
Config: cert.Config{
CommonName: conversion.ToClusterKey(vc),
AltNames: *altNames,
Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
},
}
apiCert, apiKey, err := pkiutil.NewCertAndKey(ca.Crt, ca.Key, config)
if err != nil {
return nil, fmt.Errorf("fail to create apiserver crt and key: %v", err)
}
rsaKey, ok := apiKey.(*rsa.PrivateKey)
if !ok {
return nil, errors.New("fail to assert rsa private key")
}
return &CrtKeyPair{apiCert, rsaKey}, nil
}
// NewAPIServerKubeletClientCertAndKey creates certificate for the apiservers to connect to the
// kubelets securely, signed by the ca.
func NewAPIServerKubeletClientCertAndKey(ca *CrtKeyPair) (*x509.Certificate, *rsa.PrivateKey, error) {
config := &pkiutil.CertConfig{
Config: cert.Config{
CommonName: "kube-apiserver-kubelet-client",
Organization: []string{"system:masters"},
Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
},
}
apiClientCert, apiClientKey, err := pkiutil.NewCertAndKey(ca.Crt, ca.Key, config)
if err != nil {
return nil, nil, fmt.Errorf("failure while creating API server kubelet client key and certificate: %v", err)
}
rsaKey, ok := apiClientKey.(*rsa.PrivateKey)
if !ok {
return nil, nil, errors.New("fail to assert rsa private key")
}
return apiClientCert, rsaKey, nil
}
// NewEtcdServerCrtAndKey creates new crt-key pair using ca for etcd
func NewEtcdServerCrtAndKey(ca *CrtKeyPair, etcdDomains []string) (*CrtKeyPair, error) {
// create AltNames with defaults DNSNames/IPs
altNames := &cert.AltNames{
DNSNames: etcdDomains,
IPs: []net.IP{net.ParseIP("127.0.0.1")},
}
config := &pkiutil.CertConfig{
Config: cert.Config{
CommonName: "kube-etcd",
AltNames: *altNames,
// all peers will use this crt-key pair as well
Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
},
}
etcdServerCert, etcdServerKey, err := pkiutil.NewCertAndKey(ca.Crt, ca.Key, config)
if err != nil {
return nil, fmt.Errorf("fail to create etcd crt and key: %v", err)
}
rsaKey, ok := etcdServerKey.(*rsa.PrivateKey)
if !ok {
return nil, errors.New("fail to assert rsa private key")
}
return &CrtKeyPair{etcdServerCert, rsaKey}, nil
}
// NewEtcdHealthcheckClientCertAndKey creates certificate for liveness probes to healthcheck etcd,
// signed by the given ca.
func NewEtcdHealthcheckClientCertAndKey(ca *CrtKeyPair) (*x509.Certificate, *rsa.PrivateKey, error) {
config := &pkiutil.CertConfig{
Config: cert.Config{
CommonName: "kube-etcd-healthcheck-client",
Organization: []string{"system:masters"},
Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
},
}
etcdHealcheckClientCert, etcdHealcheckClientKey, err := pkiutil.NewCertAndKey(ca.Crt, ca.Key, config)
if err != nil {
return nil, nil, fmt.Errorf("failure while creating etcd healthcheck client key and certificate: %v", err)
}
rsaKey, ok := etcdHealcheckClientKey.(*rsa.PrivateKey)
if !ok {
return nil, nil, errors.New("fail to assert rsa private key")
}
return etcdHealcheckClientCert, rsaKey, nil
}
// NewServiceAccountSigningKey creates rsa key for signing service account tokens.
func NewServiceAccountSigningKey() (*rsa.PrivateKey, error) {
// The key does NOT exist, let's generate it now
saSigningKey, err := newPrivateKey()
if err != nil {
return nil, fmt.Errorf("failure while creating service account token signing key: %v", err)
}
return saSigningKey, nil
}
// NewFrontProxyClientCertAndKey creates crt-key pair for proxy client using ca.
func NewFrontProxyClientCertAndKey(ca *CrtKeyPair) (*CrtKeyPair, error) {
config := &pkiutil.CertConfig{
Config: cert.Config{
CommonName: "front-proxy-client",
Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
},
}
frontProxyClientCert, frontProxyClientKey, err := pkiutil.NewCertAndKey(ca.Crt, ca.Key, config)
if err != nil {
return nil, fmt.Errorf("fail to create crt and key for front-proxy: %v", err)
}
rsaKey, ok := frontProxyClientKey.(*rsa.PrivateKey)
if !ok {
return nil, errors.New("fail to assert rsa private key")
}
return &CrtKeyPair{frontProxyClientCert, rsaKey}, nil
}
// NewClientCrtAndKey creates crt-key pair for client
func NewClientCrtAndKey(user string, ca *CrtKeyPair, groups []string) (*CrtKeyPair, error) {
config := &pkiutil.CertConfig{
Config: cert.Config{
CommonName: user,
Organization: groups,
Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
},
}
crt, key, err := pkiutil.NewCertAndKey(ca.Crt, ca.Key, config)
if err != nil {
return nil, err
}
rsaKey, ok := key.(*rsa.PrivateKey)
if !ok {
return nil, errors.New("fail to assert rsa private key")
}
return &CrtKeyPair{crt, rsaKey}, nil
}
// EncodePrivateKeyPEM returns PEM-encoded private key data
func EncodePrivateKeyPEM(key *rsa.PrivateKey) []byte {
block := pem.Block{
Type: pkiutil.RSAPrivateKeyBlockType,
Bytes: x509.MarshalPKCS1PrivateKey(key),
}
return pem.EncodeToMemory(&block)
}
// newPrivateKey creates an RSA private key
func newPrivateKey() (*rsa.PrivateKey, error) {
return rsa.GenerateKey(cryptorand.Reader, 2048)
}