forked from cert-manager/cert-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vault.go
515 lines (416 loc) · 15.5 KB
/
vault.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
Copyright 2020 The cert-manager 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 vault
import (
"context"
"crypto/x509"
"errors"
"fmt"
"net/http"
"path"
"path/filepath"
"strings"
"time"
vault "github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/sdk/helper/certutil"
authv1 "k8s.io/api/authentication/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corelisters "k8s.io/client-go/listers/core/v1"
"k8s.io/utils/pointer"
v1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
cmmeta "github.com/cert-manager/cert-manager/pkg/apis/meta/v1"
"github.com/cert-manager/cert-manager/pkg/util/pki"
)
var _ Interface = &Vault{}
// ClientBuilder is a function type that returns a new Interface.
// Can be used in tests to create a mock signer of Vault certificate requests.
type ClientBuilder func(namespace string, _ func(ns string) CreateToken, _ corelisters.SecretLister, _ v1.GenericIssuer) (Interface, error)
// Interface implements various high level functionality related to connecting
// with a Vault server, verifying its status and signing certificate request for
// Vault's certificate.
type Interface interface {
Sign(csrPEM []byte, duration time.Duration) (certPEM []byte, caPEM []byte, err error)
IsVaultInitializedAndUnsealed() error
}
// Client implements functionality to talk to a Vault server.
type Client interface {
NewRequest(method, requestPath string) *vault.Request
RawRequest(r *vault.Request) (*vault.Response, error)
SetToken(v string)
}
// For mocking purposes.
type CreateToken func(ctx context.Context, saName string, req *authv1.TokenRequest, opts metav1.CreateOptions) (*authv1.TokenRequest, error)
// Vault implements Interface and holds a Vault issuer, secrets lister and a
// Vault client.
type Vault struct {
createToken CreateToken // Uses the same namespace as below.
secretsLister corelisters.SecretLister
issuer v1.GenericIssuer
namespace string
// The pattern below, of namespaced and non-namespaced Vault clients, is copied from Hashicorp Nomad:
// https://github.com/hashicorp/nomad/blob/6e4410a9b13ce167bc7ef53da97c621b5c9dcd12/nomad/vault.go#L180-L190
// client is the Vault API client used for Namespace-relative integrations
// with the Vault API (anything except `/v1/sys`).
// The namespace feature is only available in Vault Enterprise.
// The namespace HTTP header (X-Vault-Namespace) is ignored by the open source version of Vault.
// See https://www.vaultproject.io/docs/enterprise/namespaces
client Client
// clientSys is the Vault API client used for non-Namespace-relative integrations
// with the Vault API (anything involving `/v1/sys`). This client is never configured
// with a Vault namespace, because these endpoints may return errors if a namespace
// header is provided
// See https://developer.hashicorp.com/vault/docs/enterprise/namespaces#root-only-api-paths
clientSys Client
}
// New returns a new Vault instance with the given namespace, issuer and
// secrets lister.
// Returned errors may be network failures and should be considered for
// retrying.
func New(namespace string, createTokenFn func(ns string) CreateToken, secretsLister corelisters.SecretLister, issuer v1.GenericIssuer) (Interface, error) {
v := &Vault{
createToken: createTokenFn(namespace),
secretsLister: secretsLister,
namespace: namespace,
issuer: issuer,
}
cfg, err := v.newConfig()
if err != nil {
return nil, err
}
client, err := vault.NewClient(cfg)
if err != nil {
return nil, fmt.Errorf("error initializing Vault client: %s", err.Error())
}
// Set the Vault namespace.
// An empty namespace string will cause the client to not send the namespace related HTTP headers to Vault.
clientNS := client.WithNamespace(issuer.GetSpec().Vault.Namespace)
// Use the (maybe) namespaced client to authenticate.
// If a Vault namespace is configured, then the authentication endpoints are
// expected to be in that namespace.
if err := v.setToken(clientNS); err != nil {
return nil, err
}
// A client for use with namespaced API paths
v.client = clientNS
// Create duplicate Vault client without a namespace, for interacting with root-only API paths.
// For backwards compatibility, this client will use the token from the namespaced client,
// although this is probably unnecessary / bad practice, since we only
// interact with the sys/health endpoint which is an unauthenticated endpoint:
// https://github.com/hashicorp/vault/issues/209#issuecomment-102485565.
v.clientSys = clientNS.WithNamespace("")
return v, nil
}
// Sign will connect to a Vault instance to sign a certificate signing request.
func (v *Vault) Sign(csrPEM []byte, duration time.Duration) (cert []byte, ca []byte, err error) {
csr, err := pki.DecodeX509CertificateRequestBytes(csrPEM)
if err != nil {
return nil, nil, fmt.Errorf("failed to decode CSR for signing: %s", err)
}
parameters := map[string]string{
"common_name": csr.Subject.CommonName,
"alt_names": strings.Join(csr.DNSNames, ","),
"ip_sans": strings.Join(pki.IPAddressesToString(csr.IPAddresses), ","),
"uri_sans": strings.Join(pki.URLsToString(csr.URIs), ","),
"ttl": duration.String(),
"csr": string(csrPEM),
"exclude_cn_from_sans": "true",
}
vaultIssuer := v.issuer.GetSpec().Vault
url := path.Join("/v1", vaultIssuer.Path)
request := v.client.NewRequest("POST", url)
if err := request.SetJSONBody(parameters); err != nil {
return nil, nil, fmt.Errorf("failed to build vault request: %s", err)
}
resp, err := v.client.RawRequest(request)
if err != nil {
return nil, nil, fmt.Errorf("failed to sign certificate by vault: %s", err)
}
defer resp.Body.Close()
vaultResult := certutil.Secret{}
err = resp.DecodeJSON(&vaultResult)
if err != nil {
return nil, nil, fmt.Errorf("failed to decode response returned by vault: %s", err)
}
return extractCertificatesFromVaultCertificateSecret(&vaultResult)
}
func (v *Vault) setToken(client Client) error {
// IMPORTANT: Because of backwards compatibility with older versions that
// incorrectly allowed multiple authentication methods to be specified at
// the time of validation, we must still allow multiple authentication methods
// to be specified.
// In terms of implementation, we will use the first authentication method.
// The order of precedence is: tokenSecretRef, appRole, kubernetes
tokenRef := v.issuer.GetSpec().Vault.Auth.TokenSecretRef
if tokenRef != nil {
token, err := v.tokenRef(tokenRef.Name, v.namespace, tokenRef.Key)
if err != nil {
return err
}
client.SetToken(token)
return nil
}
appRole := v.issuer.GetSpec().Vault.Auth.AppRole
if appRole != nil {
token, err := v.requestTokenWithAppRoleRef(client, appRole)
if err != nil {
return err
}
client.SetToken(token)
return nil
}
kubernetesAuth := v.issuer.GetSpec().Vault.Auth.Kubernetes
if kubernetesAuth != nil {
token, err := v.requestTokenWithKubernetesAuth(client, kubernetesAuth)
if err != nil {
return fmt.Errorf("while requesting a Vault token using the Kubernetes auth: %w", err)
}
client.SetToken(token)
return nil
}
return fmt.Errorf("error initializing Vault client: tokenSecretRef, appRoleSecretRef, or Kubernetes auth role not set")
}
func (v *Vault) newConfig() (*vault.Config, error) {
cfg := vault.DefaultConfig()
cfg.Address = v.issuer.GetSpec().Vault.Server
caBundle, err := v.caBundle()
if err != nil {
return nil, fmt.Errorf("failed to load vault CA bundle: %w", err)
}
// If no CA bundle was loaded, return early and don't modify the vault config
// further. This will cause the vault client to use the system root CA
// bundle.
if len(caBundle) == 0 {
return cfg, nil
}
caCertPool := x509.NewCertPool()
ok := caCertPool.AppendCertsFromPEM(caBundle)
if !ok {
return nil, fmt.Errorf("no Vault CA bundles loaded, check bundle contents")
}
cfg.HttpClient.Transport.(*http.Transport).TLSClientConfig.RootCAs = caCertPool
return cfg, nil
}
// caBundle returns the CA bundle for the Vault server. Can be used in Vault
// client configs to trust the connection to the Vault server. If no custom CA
// bundle is configured, an empty byte slice is returned.
// Assumes the in-line and Secret CA bundles are not both defined.
// If the `key` of the Secret CA bundle is not defined, its value defaults to
// `ca.crt`.
func (v *Vault) caBundle() ([]byte, error) {
if len(v.issuer.GetSpec().Vault.CABundle) > 0 {
return v.issuer.GetSpec().Vault.CABundle, nil
}
ref := v.issuer.GetSpec().Vault.CABundleSecretRef
if ref == nil {
return nil, nil
}
secret, err := v.secretsLister.Secrets(v.namespace).Get(ref.Name)
if err != nil {
return nil, fmt.Errorf("could not access secret '%s/%s': %s", v.namespace, ref.Name, err)
}
var key string
if ref.Key != "" {
key = ref.Key
} else {
key = cmmeta.TLSCAKey
}
certBytes, ok := secret.Data[key]
if !ok {
return nil, fmt.Errorf("no data for %q in secret '%s/%s'", key, v.namespace, ref.Name)
}
return certBytes, nil
}
func (v *Vault) tokenRef(name, namespace, key string) (string, error) {
secret, err := v.secretsLister.Secrets(namespace).Get(name)
if err != nil {
return "", err
}
if key == "" {
key = v1.DefaultVaultTokenAuthSecretKey
}
keyBytes, ok := secret.Data[key]
if !ok {
return "", fmt.Errorf("no data for %q in secret '%s/%s'", key, name, namespace)
}
token := string(keyBytes)
token = strings.TrimSpace(token)
return token, nil
}
func (v *Vault) appRoleRef(appRole *v1.VaultAppRole) (roleId, secretId string, err error) {
roleId = strings.TrimSpace(appRole.RoleId)
secret, err := v.secretsLister.Secrets(v.namespace).Get(appRole.SecretRef.Name)
if err != nil {
return "", "", err
}
key := appRole.SecretRef.Key
keyBytes, ok := secret.Data[key]
if !ok {
return "", "", fmt.Errorf("no data for %q in secret '%s/%s'", key, v.namespace, appRole.SecretRef.Name)
}
secretId = string(keyBytes)
secretId = strings.TrimSpace(secretId)
return roleId, secretId, nil
}
func (v *Vault) requestTokenWithAppRoleRef(client Client, appRole *v1.VaultAppRole) (string, error) {
roleId, secretId, err := v.appRoleRef(appRole)
if err != nil {
return "", err
}
parameters := map[string]string{
"role_id": roleId,
"secret_id": secretId,
}
authPath := appRole.Path
if authPath == "" {
authPath = "approle"
}
url := path.Join("/v1", "auth", authPath, "login")
request := client.NewRequest("POST", url)
err = request.SetJSONBody(parameters)
if err != nil {
return "", fmt.Errorf("error encoding Vault parameters: %s", err.Error())
}
resp, err := client.RawRequest(request)
if err != nil {
return "", fmt.Errorf("error logging in to Vault server: %s", err.Error())
}
defer resp.Body.Close()
vaultResult := vault.Secret{}
if err := resp.DecodeJSON(&vaultResult); err != nil {
return "", fmt.Errorf("unable to decode JSON payload: %s", err.Error())
}
token, err := vaultResult.TokenID()
if err != nil {
return "", fmt.Errorf("unable to read token: %s", err.Error())
}
if token == "" {
return "", errors.New("no token returned")
}
return token, nil
}
func (v *Vault) requestTokenWithKubernetesAuth(client Client, kubernetesAuth *v1.VaultKubernetesAuth) (string, error) {
var jwt string
switch {
case kubernetesAuth.SecretRef.Name != "":
secret, err := v.secretsLister.Secrets(v.namespace).Get(kubernetesAuth.SecretRef.Name)
if err != nil {
return "", err
}
key := kubernetesAuth.SecretRef.Key
if key == "" {
key = v1.DefaultVaultTokenAuthSecretKey
}
keyBytes, ok := secret.Data[key]
if !ok {
return "", fmt.Errorf("no data for %q in secret '%s/%s'", key, v.namespace, kubernetesAuth.SecretRef.Name)
}
jwt = string(keyBytes)
case kubernetesAuth.ServiceAccountRef != nil:
aud := "vault://"
if v.issuer.GetNamespace() != "" {
aud += v.issuer.GetNamespace() + "/"
}
aud += v.issuer.GetName()
tokenrequest, err := v.createToken(context.Background(), kubernetesAuth.ServiceAccountRef.Name, &authv1.TokenRequest{
Spec: authv1.TokenRequestSpec{
// The audience is generated by cert-manager and can't be
// configured by the user for security reasons. The format is:
// "vault://<namespace>/<issuer-name>" (for an Issuer)
// "vault://<issuer-name>" (for a ClusterIssuer)
Audiences: []string{aud},
// Since the JWT is only used to authenticate with Vault and is
// immediately discarded, let's use the minimal duration
// possible. 10 minutes is the minimum allowed by the Kubernetes
// API.
ExpirationSeconds: pointer.Int64(600),
},
}, metav1.CreateOptions{})
if err != nil {
return "", fmt.Errorf("while requesting a token for the service account %s/%s: %s", v.issuer.GetNamespace(), kubernetesAuth.ServiceAccountRef.Name, err.Error())
}
jwt = tokenrequest.Status.Token
default:
return "", fmt.Errorf("programmer mistake: both serviceAccountRef and tokenRef.name are empty")
}
parameters := map[string]string{
"role": kubernetesAuth.Role,
"jwt": jwt,
}
mountPath := kubernetesAuth.Path
if mountPath == "" {
mountPath = v1.DefaultVaultKubernetesAuthMountPath
}
url := filepath.Join(mountPath, "login")
request := client.NewRequest("POST", url)
err := request.SetJSONBody(parameters)
if err != nil {
return "", fmt.Errorf("error encoding Vault parameters: %s", err.Error())
}
resp, err := client.RawRequest(request)
if err != nil {
return "", fmt.Errorf("error calling Vault server: %s", err.Error())
}
defer resp.Body.Close()
vaultResult := vault.Secret{}
err = resp.DecodeJSON(&vaultResult)
if err != nil {
return "", fmt.Errorf("unable to decode JSON payload: %s", err.Error())
}
token, err := vaultResult.TokenID()
if err != nil {
return "", fmt.Errorf("unable to read token: %s", err.Error())
}
return token, nil
}
func extractCertificatesFromVaultCertificateSecret(secret *certutil.Secret) ([]byte, []byte, error) {
parsedBundle, err := certutil.ParsePKIMap(secret.Data)
if err != nil {
return nil, nil, fmt.Errorf("failed to decode response returned by vault: %s", err)
}
vbundle, err := parsedBundle.ToCertBundle()
if err != nil {
return nil, nil, fmt.Errorf("unable to convert certificate bundle to PEM bundle: %s", err.Error())
}
bundle, err := pki.ParseSingleCertificateChainPEM([]byte(
strings.Join(append(
vbundle.CAChain,
vbundle.IssuingCA,
vbundle.Certificate,
), "\n")))
if err != nil {
return nil, nil, fmt.Errorf("failed to parse certificate chain from vault: %w", err)
}
return bundle.ChainPEM, bundle.CAPEM, nil
}
func (v *Vault) IsVaultInitializedAndUnsealed() error {
healthURL := path.Join("/v1", "sys", "health")
healthRequest := v.clientSys.NewRequest("GET", healthURL)
healthResp, err := v.clientSys.RawRequest(healthRequest)
if healthResp != nil {
defer healthResp.Body.Close()
}
// 429 = if unsealed and standby
// 472 = if disaster recovery mode replication secondary and active
// 473 = if performance standby
if err != nil {
switch {
case healthResp == nil:
return err
case healthResp.StatusCode == 429, healthResp.StatusCode == 472, healthResp.StatusCode == 473:
return nil
default:
return fmt.Errorf("error calling Vault %s: %w", healthURL, err)
}
}
return nil
}