-
Notifications
You must be signed in to change notification settings - Fork 783
/
vault.go
580 lines (515 loc) · 17.9 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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
package vault
import (
"bytes"
"fmt"
"github.com/jenkins-x/jx/v2/pkg/util/json"
"k8s.io/apimachinery/pkg/types"
"github.com/banzaicloud/bank-vaults/operator/pkg/apis/vault/v1alpha1"
"github.com/banzaicloud/bank-vaults/operator/pkg/client/clientset/versioned"
vaultapi "github.com/hashicorp/vault/api"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/kube"
"github.com/jenkins-x/jx/v2/pkg/kube/cluster"
"github.com/jenkins-x/jx/v2/pkg/kube/naming"
"github.com/jenkins-x/jx/v2/pkg/kube/serviceaccount"
"github.com/jenkins-x/jx/v2/pkg/kube/services"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/jenkins-x/jx/v2/pkg/vault"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
const (
BankVaultsImage = "banzaicloud/bank-vaults"
VaultOperatorImage = "banzaicloud/vault-operator"
VaultImage = "vault"
gcpServiceAccountEnv = "GOOGLE_APPLICATION_CREDENTIALS"
gcpServiceAccountPath = "/etc/gcp/service-account.json"
awsServiceAccountEnv = "AWS_SHARED_CREDENTIALS_FILE"
awsServiceAccountPath = "/etc/aws/credentials"
vaultAuthName = "auth"
vaultAuthType = "kubernetes"
vaultAuthTTL = "1h"
vaultRoleName = "vault-auth"
vaultSecretEngines = "secrets"
defaultNumVaults = 1
defaultInternalVaultURL = "http://%s:" + vault.DefaultVaultPort
)
// GCPConfig keeps the configuration for Google Cloud
type GCPConfig struct {
ProjectId string
KmsKeyring string
KmsKey string
KmsLocation string
GcsBucket string
}
// GCSConfig Google Cloud Storage config for Vault backend
type GCSConfig struct {
Bucket string `json:"bucket"`
HaEnabled string `json:"ha_enabled"`
}
// AWSConfig keeps the vault configuration for AWS
type AWSConfig struct {
v1alpha1.AWSUnsealConfig
AutoCreate bool
DynamoDBTable string
DynamoDBRegion string
AccessKeyID string
SecretAccessKey string
ProvidedIAMUsername string
}
// AzureConfig keeps the vault configuration for Azure
type AzureConfig struct {
v1alpha1.AzureUnsealConfig
StorageAccountName string
StorageAccountKey string
ContainerName string
TenantID string
VaultName string
KeyName string
}
// DynamoDBConfig AWS DynamoDB config for Vault backend
type DynamoDBConfig struct {
HaEnabled string `json:"ha_enabled"`
Region string `json:"region"`
Table string `json:"table"`
AccessKeyID string `json:"access_key"`
SecretAccessKey string `json:"secret_key"`
}
// AzureStorageConfig Azure Storage config for Vault backend
type AzureStorageConfig struct {
AccountName string `json:"accountName"`
AccountKey string `json:"accountKey"`
ContainerName string `json:"container"`
}
// VaultAuths list of vault authentications
type VaultAuths []VaultAuth
// VaultAuth vault auth configuration
type VaultAuth struct {
Roles []VaultRole `json:"roles"`
Type string `json:"type"`
}
// VaultRole role configuration for VaultAuth
type VaultRole struct {
BoundServiceAccountNames string `json:"bound_service_account_names"`
BoundServiceAccountNamespaces string `json:"bound_service_account_namespaces"`
Name string `json:"name"`
Policies string `json:"policies"`
TTL string `json:"ttl"`
}
// VaultPolicies list of vault policies
type VaultPolicies []VaultPolicy
// VaultPolicy vault policy
type VaultPolicy struct {
Name string `json:"name"`
Rules string `json:"rules"`
}
// Tcp address for vault server
type Tcp struct {
Address string `json:"address"`
TlsDisable bool `json:"tls_disable"`
}
// Listener vault server listener
type Listener struct {
Tcp Tcp `json:"tcp"`
}
// Telemetry address for telemetry server
type Telemetry struct {
StatsdAddress string `json:"statsd_address"`
}
// Storage configuration for Vault storage
type Storage struct {
GCS *GCSConfig `json:"gcs,omitempty"`
DynamoDB *DynamoDBConfig `json:"dynamodb,omitempty"`
AzureStorage *AzureStorageConfig `json:"azure,omitempty"`
}
// SecretEngine configuration for secret engine
type SecretEngine struct {
vaultapi.MountInput
Path string `json:"path"`
}
// Seal configuration for Vault auto-unseal
type Seal struct {
GcpCkms *GCPSealConfig `json:"gcpckms,omitempty"`
AWSKms *AWSSealConfig `json:"awskms,omitempty"`
AzureKeyVault *AzureSealConfig `json:"azurekeyvault,omitempty"`
}
// GCPSealConfig Google Cloud KMS config for vault auto-unseal
type GCPSealConfig struct {
Credentials string `json:"credentials,omitempty"`
Project string `json:"project,omitempty"`
Region string `json:"region,omitempty"`
KeyRing string `json:"key_ring,omitempty"`
CryptoKey string `json:"crypto_key,omitempty"`
}
// AWSSealConfig AWS KMS config for vault auto-unseal
type AWSSealConfig struct {
Region string `json:"region,omitempty"`
AccessKey string `json:"access_key,omitempty"`
SecretKey string `json:"secret_key,omitempty"`
KmsKeyID string `json:"kms_key_id,omitempty"`
Endpoint string `json:"endpoint,omitempty"`
}
// AzureSealConfig Azure Key Vault config for vault auto-unseal
type AzureSealConfig struct {
TenantID string `json:"tenant_id,omitempty"`
VaultName string `json:"vault_name,omitempty"`
KeyName string `json:"key_name,omitempty"`
}
// CloudProviderConfig is a wrapper around the cloud provider specific elements of the Vault CRD configuration
type CloudProviderConfig struct {
Storage map[string]interface{}
Seal map[string]interface{}
UnsealConfig v1alpha1.UnsealConfig
CredentialsConfig v1alpha1.CredentialsConfig
}
// SystemVaultName returns the name of the system vault based on the cluster name
func SystemVaultName(kuber kube.Kuber) (string, error) {
clusterName, err := cluster.ShortName(kuber)
if err != nil {
return "", err
}
return SystemVaultNameForCluster(clusterName), nil
}
// SystemVaultNameForCluster returns the system vault name from a given cluster name
func SystemVaultNameForCluster(clusterName string) string {
shortClusterName := naming.ToValidNameTruncated(clusterName, 16)
fullName := fmt.Sprintf("%s-%s", vault.SystemVaultNamePrefix, shortClusterName)
return naming.ToValidNameTruncated(fullName, 22)
}
// PrepareGKEVaultCRD creates a new vault backed by GCP KMS and storage
func PrepareGKEVaultCRD(gcpServiceAccountSecretName string, gcpConfig *GCPConfig) (CloudProviderConfig, error) {
storage := Storage{
GCS: &GCSConfig{
Bucket: gcpConfig.GcsBucket,
HaEnabled: "true",
},
}
storageConfig, err := util.ToObjectMap(storage)
if err != nil {
return CloudProviderConfig{}, errors.Wrap(err, "error creating storage config")
}
seal := Seal{
GcpCkms: &GCPSealConfig{
Credentials: gcpServiceAccountPath,
Project: gcpConfig.ProjectId,
Region: gcpConfig.KmsLocation,
KeyRing: gcpConfig.KmsKeyring,
CryptoKey: gcpConfig.KmsKey,
},
}
sealConfig, err := util.ToObjectMap(seal)
if err != nil {
return CloudProviderConfig{}, errors.Wrap(err, "error creating seal config")
}
unsealConfig := v1alpha1.UnsealConfig{
Google: &v1alpha1.GoogleUnsealConfig{
KMSKeyRing: gcpConfig.KmsKeyring,
KMSCryptoKey: gcpConfig.KmsKey,
KMSLocation: gcpConfig.KmsLocation,
KMSProject: gcpConfig.ProjectId,
StorageBucket: gcpConfig.GcsBucket,
},
}
credentialsConfig := v1alpha1.CredentialsConfig{
Env: gcpServiceAccountEnv,
Path: gcpServiceAccountPath,
SecretName: gcpServiceAccountSecretName,
}
return CloudProviderConfig{storageConfig, sealConfig, unsealConfig, credentialsConfig}, nil
}
// PrepareAWSVaultCRD creates a new vault backed by AWS KMS and DynamoDB storage
func PrepareAWSVaultCRD(awsServiceAccountSecretName string, awsConfig *AWSConfig) (CloudProviderConfig, error) {
storage := Storage{
DynamoDB: &DynamoDBConfig{
HaEnabled: "true",
Region: awsConfig.DynamoDBRegion,
Table: awsConfig.DynamoDBTable,
AccessKeyID: awsConfig.AccessKeyID,
SecretAccessKey: awsConfig.SecretAccessKey,
},
}
storageConfig, err := util.ToObjectMap(storage)
if err != nil {
return CloudProviderConfig{}, errors.Wrap(err, "error creating storage config")
}
seal := Seal{
AWSKms: &AWSSealConfig{
Region: awsConfig.KMSRegion,
AccessKey: awsConfig.AccessKeyID,
SecretKey: awsConfig.SecretAccessKey,
KmsKeyID: awsConfig.KMSKeyID,
},
}
sealConfig, err := util.ToObjectMap(seal)
if err != nil {
return CloudProviderConfig{}, errors.Wrap(err, "error creating seal config")
}
unsealConfig := v1alpha1.UnsealConfig{
AWS: &awsConfig.AWSUnsealConfig,
}
credentialsConfig := v1alpha1.CredentialsConfig{
Env: awsServiceAccountEnv,
Path: awsServiceAccountPath,
SecretName: awsServiceAccountSecretName,
}
return CloudProviderConfig{storageConfig, sealConfig, unsealConfig, credentialsConfig}, nil
}
// PrepareAzureVaultCRD creates a new vault backed by Azure Key Vault and Azure Storage
func PrepareAzureVaultCRD(azureConfig *AzureConfig) (CloudProviderConfig, error) {
storage := Storage{
AzureStorage: &AzureStorageConfig{
AccountName: azureConfig.StorageAccountName,
AccountKey: azureConfig.StorageAccountKey,
ContainerName: azureConfig.ContainerName,
},
}
storageConfig, err := util.ToObjectMap(storage)
if err != nil {
return CloudProviderConfig{}, errors.Wrap(err, "error creating storage config")
}
seal := Seal{
AzureKeyVault: &AzureSealConfig{
TenantID: azureConfig.TenantID,
VaultName: azureConfig.VaultName,
KeyName: azureConfig.KeyName,
},
}
sealConfig, err := util.ToObjectMap(seal)
if err != nil {
return CloudProviderConfig{}, errors.Wrap(err, "error creating seal config")
}
unsealConfig := v1alpha1.UnsealConfig{
Azure: &azureConfig.AzureUnsealConfig,
}
credentialsConfig := v1alpha1.CredentialsConfig{}
return CloudProviderConfig{storageConfig, sealConfig, unsealConfig, credentialsConfig}, nil
}
// NewVaultCRD creates and initializes a new Vault instance.
func NewVaultCRD(kubeClient kubernetes.Interface, name string, ns string, images map[string]string,
authServiceAccount string, authServiceAccountNamespace string, secretsPathPrefix string) (*v1alpha1.Vault, error) {
err := createVaultServiceAccount(kubeClient, ns, name)
if err != nil {
return nil, errors.Wrapf(err, "creating the vault service account '%s'", name)
}
err = ensureVaultRoleBinding(kubeClient, ns, vaultRoleName, name, name)
if err != nil {
return nil, errors.Wrapf(err, "ensuring vault cluster role binding '%s' is created", name)
}
if secretsPathPrefix == "" {
secretsPathPrefix = vault.DefaultSecretsPathPrefix
}
pathRule := &vault.PathRule{
Path: []vault.PathPolicy{{
Prefix: secretsPathPrefix,
Capabilities: vault.DefaultSecretsCapabiltities,
}},
}
vaultRule, err := pathRule.String()
if err != nil {
return nil, errors.Wrap(err, "encoding the policies for secret path")
}
vault := &v1alpha1.Vault{
TypeMeta: metav1.TypeMeta{
Kind: "Vault",
APIVersion: "vault.banzaicloud.com/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: ns,
},
Spec: v1alpha1.VaultSpec{
Size: defaultNumVaults,
Image: images[VaultImage],
BankVaultsImage: images[BankVaultsImage],
ServiceType: string(v1.ServiceTypeClusterIP),
ServiceAccount: name,
Config: map[string]interface{}{
"api_addr": fmt.Sprintf("http://%s.%s:%s", name, ns, vault.DefaultVaultPort),
"disable_clustering": true,
"listener": Listener{
Tcp: Tcp{
Address: fmt.Sprintf("0.0.0.0:%s", vault.DefaultVaultPort),
TlsDisable: true,
},
},
"telemetry": Telemetry{
StatsdAddress: "localhost:9125",
},
"ui": true,
},
ExternalConfig: map[string]interface{}{
vaultAuthName: []VaultAuth{
{
Roles: []VaultRole{
{
BoundServiceAccountNames: authServiceAccount,
BoundServiceAccountNamespaces: authServiceAccountNamespace,
Name: authServiceAccount,
Policies: vault.PathRulesName,
TTL: vaultAuthTTL,
},
},
Type: vaultAuthType,
},
},
vault.PoliciesName: []VaultPolicy{
{
Name: vault.PathRulesName,
Rules: vaultRule,
},
},
vaultSecretEngines: []SecretEngine{
{
Path: vault.DefaultSecretsPath,
MountInput: vaultapi.MountInput{
Type: "kv",
Description: "KV secret engine",
Local: false,
SealWrap: false,
Options: map[string]string{
"version": "2",
},
Config: vaultapi.MountConfigInput{
ForceNoCache: true,
},
},
},
},
},
},
}
return vault, err
}
func createVaultServiceAccount(client kubernetes.Interface, namespace string, name string) error {
_, err := serviceaccount.CreateServiceAccount(client, namespace, name)
if err != nil {
return errors.Wrap(err, "creating vault service account")
}
return nil
}
func ensureVaultRoleBinding(client kubernetes.Interface, namespace string, roleName string,
roleBindingName string, serviceAccount string) error {
apiGroups := []string{"authentication.k8s.io"}
resources := []string{"tokenreviews"}
verbs := []string{"*"}
found := kube.IsClusterRole(client, roleName)
if found {
err := kube.DeleteClusterRole(client, roleName)
if err != nil {
return errors.Wrapf(err, "deleting the existing cluster role '%s'", roleName)
}
}
err := kube.CreateClusterRole(client, namespace, roleName, apiGroups, resources, verbs)
if err != nil {
return errors.Wrapf(err, "creating the cluster role '%s' for vault", roleName)
}
found = kube.IsClusterRoleBinding(client, roleBindingName)
if found {
err := kube.DeleteClusterRoleBinding(client, roleBindingName)
if err != nil {
return errors.Wrapf(err, "deleting the existing cluster role binding '%s'", roleBindingName)
}
}
err = kube.CreateClusterRoleBinding(client, namespace, roleBindingName, serviceAccount, roleName)
if err != nil {
return errors.Wrapf(err, "creating the cluster role binding '%s' for vault", roleBindingName)
}
return nil
}
// FindVault checks if a vault is available
func FindVault(vaultOperatorClient versioned.Interface, name string, ns string) bool {
_, err := GetVault(vaultOperatorClient, name, ns)
if err != nil {
log.Logger().Debugf("vault %s not found in namespace %s, err is %s", name, ns, err)
return false
}
return true
}
// GetVault gets a specific vault
func GetVault(vaultOperatorClient versioned.Interface, name string, ns string) (*v1alpha1.Vault, error) {
return vaultOperatorClient.VaultV1alpha1().Vaults(ns).Get(name, metav1.GetOptions{})
}
// GetVaults returns all vaults available in a given namespaces
func GetVaults(client kubernetes.Interface, vaultOperatorClient versioned.Interface, ns string, useIngressURL bool) ([]*vault.Vault, error) {
vaultList, err := vaultOperatorClient.VaultV1alpha1().Vaults(ns).List(metav1.ListOptions{})
if err != nil {
return nil, errors.Wrapf(err, "listing vaults in namespace '%s'", ns)
}
vaults := []*vault.Vault{}
for _, v := range vaultList.Items {
vaultName := v.Name
vaultAuthSaName := GetAuthSaName(v)
// default to using internal kubernetes service dns name for vault endpoint
vaultURL := fmt.Sprintf(defaultInternalVaultURL, vaultName)
if useIngressURL {
vaultURL, err = services.FindIngressURL(client, ns, vaultName)
if err != nil || vaultURL == "" {
log.Logger().Debugf("Cannot finding the vault ingress url for vault %s", vaultName)
// skip this vault since cannot be used without the ingress
continue
}
}
vault := vault.Vault{
Name: vaultName,
Namespace: ns,
URL: vaultURL,
ServiceAccountName: vaultAuthSaName,
}
vaults = append(vaults, &vault)
}
return vaults, nil
}
// DeleteVault delete a Vault resource
func DeleteVault(vaultOperatorClient versioned.Interface, name string, ns string) error {
return vaultOperatorClient.VaultV1alpha1().Vaults(ns).Delete(name, &metav1.DeleteOptions{})
}
// GetAuthSaName gets the Auth Service Account name for the vault
func GetAuthSaName(vault v1alpha1.Vault) string {
// This is nasty, but the ExternalConfig member of VaultSpec is just defined as a map[string]interface{} :-(
authArray := vault.Spec.ExternalConfig["auth"]
authObject := authArray.([]interface{})[0]
roleArray := authObject.(map[string]interface{})["roles"]
roleObject := roleArray.([]interface{})[0]
name := roleObject.(map[string]interface{})["name"]
return name.(string)
}
// CreateOrUpdateVault creates the specified Vault CRD if it does not exist or updates it otherwise.
func CreateOrUpdateVault(vault *v1alpha1.Vault, vaultOperatorClient versioned.Interface, ns string) error {
vaultExists := false
existingVault, err := GetVault(vaultOperatorClient, vault.Name, ns)
if err == nil {
vaultExists = true
} else {
statusError, ok := err.(*apierrors.StatusError)
if ok && statusError.ErrStatus.Code == 404 {
vaultExists = false
} else {
return errors.Wrapf(err, "unable to check for existence of vault '%s' in namespace '%s'", vault.Name, ns)
}
}
if vaultExists {
vaultName := existingVault.ObjectMeta.Name
patch, err := json.CreatePatch(existingVault, vault)
if err != nil {
return errors.Wrapf(err, "unable to create path for update of of vault '%s' in namespace '%s'", vault.Name, ns)
}
if bytes.Equal(patch, []byte("[]")) {
return nil
}
_, err = vaultOperatorClient.VaultV1alpha1().Vaults(ns).Patch(vaultName, types.JSONPatchType, patch)
} else {
_, err = vaultOperatorClient.VaultV1alpha1().Vaults(ns).Create(vault)
}
op := "create"
if vaultExists {
op = "update"
}
if err != nil {
return errors.Wrapf(err, "unable to %s vault '%s' in namespace '%s'", op, vault.Name, ns)
}
log.Logger().Infof("Vault '%s' in namespace '%s' %sd ", util.ColorInfo(vault.Name), util.ColorInfo(ns), op)
return nil
}