This repository has been archived by the owner on Sep 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 110
/
tls.go
319 lines (287 loc) · 10.5 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
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
// Copyright 2018 The vault-operator 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 operator
import (
"crypto/rsa"
"crypto/x509"
"fmt"
api "github.com/coreos/vault-operator/pkg/apis/vault/v1alpha1"
"github.com/coreos/vault-operator/pkg/util/k8sutil"
"github.com/coreos/vault-operator/pkg/util/tlsutil"
"github.com/coreos/vault-operator/pkg/util/vaultutil"
"k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var (
orgForTLSCert = []string{"coreos.com"}
defaultClusterDomain = "cluster.local"
)
// prepareDefaultVaultTLSSecrets creates the default secrets for the vault server's TLS assets.
// Currently we self-generate the CA, and use the self generated CA to sign all the TLS certs.
func (v *Vaults) prepareDefaultVaultTLSSecrets(vr *api.VaultService) (err error) {
defer func() {
if err != nil {
err = fmt.Errorf("prepare default vault TLS secrets failed: %v", err)
}
}()
// if TLS spec doesn't exist or secrets doesn't exist, then we can go create secrets.
// TODO: we won't need IsTLSConfigured() check once we have initializers.
if api.IsTLSConfigured(vr.Spec.TLS) {
// TODO: use secrets informer
_, err = v.kubecli.CoreV1().Secrets(vr.Namespace).Get(vr.Spec.TLS.Static.ServerSecret, metav1.GetOptions{})
if err == nil {
return nil
}
if !apierrors.IsNotFound(err) {
return err
}
}
// TODO: optional user pass-in CA.
caKey, caCrt, err := newCACert()
if err != nil {
return err
}
se, err := newVaultServerTLSSecret(vr, caKey, caCrt)
if err != nil {
return err
}
k8sutil.AddOwnerRefToObject(se, k8sutil.AsOwner(vr))
_, err = v.kubecli.CoreV1().Secrets(vr.Namespace).Create(se)
if err != nil && !apierrors.IsAlreadyExists(err) {
return err
}
se = newVaultClientTLSSecret(vr, caCrt)
k8sutil.AddOwnerRefToObject(se, k8sutil.AsOwner(vr))
_, err = v.kubecli.CoreV1().Secrets(vr.Namespace).Create(se)
if err != nil && !apierrors.IsAlreadyExists(err) {
return err
}
return nil
}
// prepareEtcdTLSSecrets creates three etcd TLS secrets (client, server, peer) containing TLS assets.
// Currently we self-generate the CA, and use the self generated CA to sign all the TLS certs.
func (v *Vaults) prepareEtcdTLSSecrets(vr *api.VaultService) (err error) {
defer func() {
if err != nil {
err = fmt.Errorf("prepare TLS secrets failed: %v", err)
}
}()
// TODO: use secrets informer
_, err = v.kubecli.CoreV1().Secrets(vr.Namespace).Get(k8sutil.EtcdClientTLSSecretName(vr.Name), metav1.GetOptions{})
if err == nil {
return nil
}
if !apierrors.IsNotFound(err) {
return err
}
// TODO: optional user pass-in CA.
caKey, caCrt, err := newCACert()
if err != nil {
return err
}
se, err := newEtcdClientTLSSecret(vr, caKey, caCrt)
if err != nil {
return err
}
k8sutil.AddOwnerRefToObject(se, k8sutil.AsOwner(vr))
_, err = v.kubecli.CoreV1().Secrets(vr.Namespace).Create(se)
if err != nil && !apierrors.IsAlreadyExists(err) {
return err
}
se, err = newEtcdServerTLSSecret(vr, caKey, caCrt)
if err != nil {
return err
}
k8sutil.AddOwnerRefToObject(se, k8sutil.AsOwner(vr))
_, err = v.kubecli.CoreV1().Secrets(vr.Namespace).Create(se)
if err != nil && !apierrors.IsAlreadyExists(err) {
return err
}
se, err = newEtcdPeerTLSSecret(vr, caKey, caCrt)
if err != nil {
return err
}
k8sutil.AddOwnerRefToObject(se, k8sutil.AsOwner(vr))
_, err = v.kubecli.CoreV1().Secrets(vr.Namespace).Create(se)
if err != nil && !apierrors.IsAlreadyExists(err) {
return err
}
return nil
}
// cleanupTLSSecrets cleans up etcd TLS secrets generated by operator for the given vault.
func (v *Vaults) cleanupEtcdTLSSecrets(vr *api.VaultService) (err error) {
defer func() {
if err != nil {
err = fmt.Errorf("cleanup TLS secrets failed: %v", err)
}
}()
name := k8sutil.EtcdClientTLSSecretName(vr.Name)
err = v.kubecli.CoreV1().Secrets(vr.Namespace).Delete(name, nil)
if err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("delete secret (%s) failed: %v", name, err)
}
name = k8sutil.EtcdServerTLSSecretName(vr.Name)
err = v.kubecli.CoreV1().Secrets(vr.Namespace).Delete(name, nil)
if err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("delete secret (%s) failed: %v", name, err)
}
name = k8sutil.EtcdPeerTLSSecretName(vr.Name)
err = v.kubecli.CoreV1().Secrets(vr.Namespace).Delete(name, nil)
if err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("delete secret (%s) failed: %v", name, err)
}
return nil
}
// newEtcdClientTLSSecret returns a secret containing etcd client TLS assets
func newEtcdClientTLSSecret(vr *api.VaultService, caKey *rsa.PrivateKey, caCrt *x509.Certificate) (*v1.Secret, error) {
return newTLSSecret(vr, caKey, caCrt, "etcd client", k8sutil.EtcdClientTLSSecretName(vr.Name), nil,
map[string]string{
"key": "etcd-client.key",
"cert": "etcd-client.crt",
"ca": "etcd-client-ca.crt",
})
}
// newEtcdServerTLSSecret returns a secret containing etcd server TLS assets
func newEtcdServerTLSSecret(vr *api.VaultService, caKey *rsa.PrivateKey, caCrt *x509.Certificate) (*v1.Secret, error) {
return newTLSSecret(vr, caKey, caCrt, "etcd server", k8sutil.EtcdServerTLSSecretName(vr.Name),
[]string{
"localhost",
fmt.Sprintf("*.%s.%s.svc", k8sutil.EtcdNameForVault(vr.Name), vr.Namespace),
fmt.Sprintf("%s-client", k8sutil.EtcdNameForVault(vr.Name)),
fmt.Sprintf("%s-client.%s", k8sutil.EtcdNameForVault(vr.Name), vr.Namespace),
fmt.Sprintf("%s-client.%s.svc", k8sutil.EtcdNameForVault(vr.Name), vr.Namespace),
// TODO: get rid of cluster domain
fmt.Sprintf("*.%s.%s.svc.%s", k8sutil.EtcdNameForVault(vr.Name), vr.Namespace, defaultClusterDomain),
fmt.Sprintf("%s-client.%s.svc.%s", k8sutil.EtcdNameForVault(vr.Name), vr.Namespace, defaultClusterDomain),
},
map[string]string{
"key": "server.key",
"cert": "server.crt",
"ca": "server-ca.crt",
})
}
// newEtcdPeerTLSSecret returns a secret containing etcd peer TLS assets
func newEtcdPeerTLSSecret(vr *api.VaultService, caKey *rsa.PrivateKey, caCrt *x509.Certificate) (*v1.Secret, error) {
return newTLSSecret(vr, caKey, caCrt, "etcd peer", k8sutil.EtcdPeerTLSSecretName(vr.Name),
[]string{
fmt.Sprintf("*.%s.%s.svc", k8sutil.EtcdNameForVault(vr.Name), vr.Namespace),
// TODO: get rid of cluster domain
fmt.Sprintf("*.%s.%s.svc.%s", k8sutil.EtcdNameForVault(vr.Name), vr.Namespace, defaultClusterDomain),
},
map[string]string{
"key": "peer.key",
"cert": "peer.crt",
"ca": "peer-ca.crt",
})
}
// cleanupDefaultVaultTLSSecrets cleans up any auto generated vault TLS secrets for the given vault cluster
func (v *Vaults) cleanupDefaultVaultTLSSecrets(vr *api.VaultService) (err error) {
defer func() {
if err != nil {
err = fmt.Errorf("cleanup vault TLS secrets failed: %v", err)
}
}()
name := api.DefaultVaultServerTLSSecretName(vr.Name)
err = v.kubecli.CoreV1().Secrets(vr.Namespace).Delete(name, nil)
if err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("delete secret (%s) failed: %v", name, err)
}
name = api.DefaultVaultClientTLSSecretName(vr.Name)
err = v.kubecli.CoreV1().Secrets(vr.Namespace).Delete(name, nil)
if err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("delete secret (%s) failed: %v", name, err)
}
return nil
}
// newVaultServerTLSSecret returns a secret containing vault server TLS assets
func newVaultServerTLSSecret(vr *api.VaultService, caKey *rsa.PrivateKey, caCrt *x509.Certificate) (*v1.Secret, error) {
return newTLSSecret(vr, caKey, caCrt, "vault server", api.DefaultVaultServerTLSSecretName(vr.Name),
[]string{
"localhost",
fmt.Sprintf("*.%s.pod", vr.Namespace),
fmt.Sprintf("%s.%s.svc", vr.Name, vr.Namespace),
},
map[string]string{
"key": vaultutil.ServerTLSKeyName,
"cert": vaultutil.ServerTLSCertName,
// The CA is not used by the server
"ca": "server-ca.crt",
})
}
// newVaultClientTLSSecret returns a secret containing vault client TLS assets.
// The client key and certificate are not generated since clients are not authenticated at the server
func newVaultClientTLSSecret(vr *api.VaultService, caCrt *x509.Certificate) *v1.Secret {
return &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: api.DefaultVaultClientTLSSecretName(vr.Name),
Labels: k8sutil.LabelsForVault(vr.Name),
},
Data: map[string][]byte{
api.CATLSCertName: tlsutil.EncodeCertificatePEM(caCrt),
},
}
}
// newTLSSecret is a common utility for creating a secret containing TLS assets.
func newTLSSecret(vr *api.VaultService, caKey *rsa.PrivateKey, caCrt *x509.Certificate, commonName, secretName string,
addrs []string, fieldMap map[string]string) (*v1.Secret, error) {
tc := tlsutil.CertConfig{
CommonName: commonName,
Organization: orgForTLSCert,
AltNames: tlsutil.NewAltNames(addrs),
}
key, crt, err := newKeyAndCert(caCrt, caKey, tc)
if err != nil {
return nil, fmt.Errorf("new TLS secret failed: %v", err)
}
secret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
Labels: k8sutil.LabelsForVault(vr.Name),
},
Data: map[string][]byte{
fieldMap["key"]: tlsutil.EncodePrivateKeyPEM(key),
fieldMap["cert"]: tlsutil.EncodeCertificatePEM(crt),
fieldMap["ca"]: tlsutil.EncodeCertificatePEM(caCrt),
},
}
return secret, nil
}
func newCACert() (*rsa.PrivateKey, *x509.Certificate, error) {
key, err := tlsutil.NewPrivateKey()
if err != nil {
return nil, nil, err
}
config := tlsutil.CertConfig{
CommonName: "vault operator CA",
Organization: orgForTLSCert,
}
cert, err := tlsutil.NewSelfSignedCACertificate(config, key)
if err != nil {
return nil, nil, err
}
return key, cert, err
}
func newKeyAndCert(caCert *x509.Certificate, caPrivKey *rsa.PrivateKey, config tlsutil.CertConfig) (*rsa.PrivateKey, *x509.Certificate, error) {
key, err := tlsutil.NewPrivateKey()
if err != nil {
return nil, nil, err
}
// TODO: tlsutil.NewSignedCertificate()create certs for both client and server auth. We can limit it stricter.
cert, err := tlsutil.NewSignedCertificate(config, key, caCert, caPrivKey)
if err != nil {
return nil, nil, err
}
return key, cert, nil
}