-
Notifications
You must be signed in to change notification settings - Fork 787
/
create.go
490 lines (425 loc) · 16.6 KB
/
create.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
package create
import (
"fmt"
"time"
"github.com/jenkins-x/jx/v2/pkg/errorutil"
"github.com/banzaicloud/bank-vaults/operator/pkg/apis/vault/v1alpha1"
"github.com/banzaicloud/bank-vaults/operator/pkg/client/clientset/versioned"
"github.com/jenkins-x/jx/v2/pkg/cloud"
"github.com/jenkins-x/jx/v2/pkg/cloud/amazon/session"
awsvault "github.com/jenkins-x/jx/v2/pkg/cloud/amazon/vault"
"github.com/jenkins-x/jx/v2/pkg/cloud/gke"
gkevault "github.com/jenkins-x/jx/v2/pkg/cloud/gke/vault"
"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/kube/vault"
"github.com/jenkins-x/jx/v2/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/jenkins-x/jx/v2/pkg/versionstream"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"k8s.io/client-go/kubernetes"
)
const (
autoCreateTableName = "vault-data"
)
// VaultCreationParam encapsulates the parameters needed to create a Vault instance.
type VaultCreationParam struct {
VaultName string
ClusterName string
Namespace string
ServiceAccountName string
KubeProvider string
SecretsPathPrefix string
CreateCloudResources bool
Boot bool
BatchMode bool
VaultOperatorClient versioned.Interface
KubeClient kubernetes.Interface
VersionResolver versionstream.VersionResolver
FileHandles util.IOFileHandles
GKE *GKEParam
AWS *AWSParam
}
// GKEParam encapsulates the parameters needed to create a Vault instance on GKE.
type GKEParam struct {
ProjectID string
Zone string
BucketName string
KeyringName string
KeyName string
RecreateBucket bool
}
// GKEParam encapsulates the parameters needed to create a Vault instance on AWS.
type AWSParam struct {
IAMUsername string
S3Bucket string
S3Region string
S3Prefix string
TemplatesDir string
DynamoDBTable string
DynamoDBRegion string
KMSKeyID string
KMSRegion string
AccessKeyID string
SecretAccessKey string
AutoCreate bool
}
// VaultCreator defines the interface to create and update Vault instances
type VaultCreator interface {
CreateOrUpdateVault(param VaultCreationParam) error
}
type defaultVaultCreator struct {
}
// NewVaultCreator creates an instance of the default VaultCreator.
func NewVaultCreator() VaultCreator {
return &defaultVaultCreator{}
}
func (p *VaultCreationParam) validate() error {
var validationErrors []error
if p.VaultName == "" {
validationErrors = append(validationErrors, errors.New("the Vault name needs to be provided"))
}
if p.Namespace == "" {
validationErrors = append(validationErrors, errors.New("the namespace to create the Vault instance into needs to be provided"))
}
if p.KubeClient == nil {
validationErrors = append(validationErrors, errors.New("a kube client needs to be provided"))
}
if p.VaultOperatorClient == nil {
validationErrors = append(validationErrors, errors.New("a vault operator client needs to be provided"))
}
if p.KubeProvider == "" {
validationErrors = append(validationErrors, errors.New("a kube/cloud provider needs be provided"))
}
if p.KubeProvider == cloud.GKE {
if p.GKE == nil {
validationErrors = append(validationErrors, errors.Errorf("%s selected as kube provider, but no %s specific parameters provided", cloud.GKE, cloud.GKE))
}
err := p.GKE.validate()
if err != nil {
validationErrors = append(validationErrors, err)
}
}
if p.KubeProvider == cloud.AWS {
if p.AWS == nil {
validationErrors = append(validationErrors, errors.Errorf("%s selected as kube provider, but no %s specific parameters provided", cloud.AWS, cloud.AWS))
}
if err := p.AWS.validate(); err != nil {
validationErrors = append(validationErrors, err)
}
}
return errorutil.CombineErrors(validationErrors...)
}
func (p *GKEParam) validate() error {
var validationErrors []error
if p == nil {
return nil
}
if p.ProjectID == "" {
validationErrors = append(validationErrors, errors.New("the GKE project ID needs to be provided"))
}
if p.Zone == "" {
validationErrors = append(validationErrors, errors.New("the GKE zone needs to be provided"))
}
return errorutil.CombineErrors(validationErrors...)
}
func (p *AWSParam) validate() error {
var validationErrors []error
if p == nil {
return nil
}
if p.TemplatesDir == "" {
validationErrors = append(validationErrors, errors.New("the cloud formation template dir needs to be provided"))
}
if p.AccessKeyID == "" {
validationErrors = append(validationErrors, errors.New("the AccessKeyID needs to be provided"))
}
if p.SecretAccessKey == "" {
validationErrors = append(validationErrors, errors.New("the SecretAccessKey needs to be provided"))
}
return errorutil.CombineErrors(validationErrors...)
}
// CreateOrUpdateVault creates or updates a Vault instance in the specified namespace.
func (v *defaultVaultCreator) CreateOrUpdateVault(param VaultCreationParam) error {
err := param.validate()
if err != nil {
return err
}
vaultAuthServiceAccount, err := v.createAuthServiceAccount(param.KubeClient, param.VaultName, param.ServiceAccountName, param.Namespace)
if err != nil {
return errors.Wrap(err, "creating Vault authentication service account")
}
log.Logger().Debugf("Created service account '%s' for Vault authentication", util.ColorInfo(vaultAuthServiceAccount))
images, err := v.dockerImages(param.VersionResolver)
if err != nil {
return errors.Wrap(err, "loading docker images from versions repository")
}
vaultCRD, err := vault.NewVaultCRD(param.KubeClient, param.VaultName, param.Namespace, images, vaultAuthServiceAccount, param.Namespace, param.SecretsPathPrefix)
err = v.setCloudProviderSpecificSettings(vaultCRD, param)
if err != nil {
return errors.Wrap(err, "unable to set cloud provider specific Vault configuration")
}
err = vault.CreateOrUpdateVault(vaultCRD, param.VaultOperatorClient, param.Namespace)
if err != nil {
return errors.Wrap(err, "creating vault")
}
// wait for vault service to become ready before finishing the provisioning
err = services.WaitForService(param.KubeClient, param.VaultName, param.Namespace, 1*time.Minute)
if err != nil {
return errors.Wrap(err, "waiting for vault service")
}
return nil
}
func (v *defaultVaultCreator) dockerImages(resolver versionstream.VersionResolver) (map[string]string, error) {
images := map[string]string{
vault.BankVaultsImage: "",
vault.VaultImage: "",
}
for image := range images {
version, err := resolver.ResolveDockerImage(image)
if err != nil {
return images, errors.Wrapf(err, "resolving docker image %q", image)
}
images[image] = version
}
return images, nil
}
// createAuthServiceAccount creates a Service Account for the Auth service for vault
func (v *defaultVaultCreator) createAuthServiceAccount(client kubernetes.Interface, vaultName, serviceAccountName string, namespace string) (string, error) {
if serviceAccountName == "" {
serviceAccountName = v.authServiceAccountName(vaultName)
}
_, err := serviceaccount.CreateServiceAccount(client, namespace, serviceAccountName)
if err != nil {
return "", errors.Wrap(err, "creating vault auth service account")
}
return serviceAccountName, nil
}
// authServiceAccountName creates a service account name for a given vault and cluster name
func (v *defaultVaultCreator) authServiceAccountName(vaultName string) string {
return fmt.Sprintf("%s-%s", vaultName, "auth-sa")
}
func (v *defaultVaultCreator) setCloudProviderSpecificSettings(vaultCRD *v1alpha1.Vault, param VaultCreationParam) error {
var cloudProviderConfig vault.CloudProviderConfig
var err error
if param.CreateCloudResources {
switch param.KubeProvider {
case cloud.GKE:
cloudProviderConfig, err = v.vaultGKEConfig(vaultCRD, param)
case cloud.AWS, cloud.EKS:
cloudProviderConfig, err = v.vaultAWSConfig(vaultCRD, param)
default:
err = errors.Errorf("vault is not supported on cloud provider %s", param.KubeProvider)
}
if err != nil {
return errors.Wrapf(err, "unable to apply cloud provider config")
}
} else {
log.Logger().Warn("Upgrading Vault CRD from within the pipeline. No changes to the cloud provider specific configuration will occur.")
existingVaultCRD, err := vault.GetVault(param.VaultOperatorClient, vaultCRD.Name, param.Namespace)
if err != nil {
return errors.Wrapf(err, "expected to find existing Vault configuration")
}
cloudProviderConfig, err = v.extractCloudProviderConfig(existingVaultCRD)
if err != nil {
return errors.Wrapf(err, "unable to extract cloud provider specific configuration from Vault CRD %s", vaultCRD.Name)
}
}
vaultCRD.Spec.Config["storage"] = cloudProviderConfig.Storage
vaultCRD.Spec.Config["seal"] = cloudProviderConfig.Seal
vaultCRD.Spec.UnsealConfig = cloudProviderConfig.UnsealConfig
vaultCRD.Spec.CredentialsConfig = cloudProviderConfig.CredentialsConfig
return nil
}
func (v *defaultVaultCreator) vaultGKEConfig(vaultCRD *v1alpha1.Vault, param VaultCreationParam) (vault.CloudProviderConfig, error) {
gcloud := &gke.GCloud{}
err := gcloud.Login("", true)
if err != nil {
return vault.CloudProviderConfig{}, errors.Wrap(err, "login into GCP")
}
args := []string{"config", "set", "project", param.GKE.ProjectID}
cmd := util.Command{
Name: "gcloud",
Args: args,
}
_, err = cmd.RunWithoutRetry()
if err != nil {
return vault.CloudProviderConfig{}, err
}
log.Logger().Debugf("Ensure KMS API is enabled")
err = gcloud.EnableAPIs(param.GKE.ProjectID, "cloudkms")
if err != nil {
return vault.CloudProviderConfig{}, errors.Wrap(err, "unable to enable 'cloudkms' API")
}
log.Logger().Debugf("Creating GCP service account for Vault backend")
gcpServiceAccountSecretName, err := gkevault.CreateVaultGCPServiceAccount(gcloud, param.KubeClient, vaultCRD.Name, param.Namespace, param.ClusterName, param.GKE.ProjectID)
if err != nil {
return vault.CloudProviderConfig{}, errors.Wrap(err, "creating GCP service account")
}
log.Logger().Debugf("'%s' service account created", util.ColorInfo(gcpServiceAccountSecretName))
log.Logger().Debugf("Setting up GCP KMS configuration")
kmsConfig, err := gkevault.CreateKmsConfig(gcloud, vaultCRD.Name, param.GKE.KeyringName, param.GKE.KeyName, param.GKE.ProjectID)
if err != nil {
return vault.CloudProviderConfig{}, errors.Wrap(err, "creating KMS configuration")
}
log.Logger().Debugf("KMS Key '%s' created in keying '%s'", util.ColorInfo(kmsConfig.Key), util.ColorInfo(kmsConfig.Keyring))
vaultBucket, err := gkevault.CreateBucket(gcloud, vaultCRD.Name, param.GKE.BucketName, param.GKE.ProjectID, param.GKE.Zone, param.GKE.RecreateBucket, param.BatchMode, param.FileHandles)
if err != nil {
return vault.CloudProviderConfig{}, errors.Wrap(err, "creating Vault GCS data bucket")
}
log.Logger().Infof("GCS bucket '%s' was created for Vault backend", util.ColorInfo(vaultBucket))
gcpConfig := &vault.GCPConfig{
ProjectId: param.GKE.ProjectID,
KmsKeyring: kmsConfig.Keyring,
KmsKey: kmsConfig.Key,
KmsLocation: kmsConfig.Location,
GcsBucket: vaultBucket,
}
return vault.PrepareGKEVaultCRD(gcpServiceAccountSecretName, gcpConfig)
}
func (v *defaultVaultCreator) vaultAWSConfig(vaultCRD *v1alpha1.Vault, param VaultCreationParam) (vault.CloudProviderConfig, error) {
_, clusterRegion, err := session.GetCurrentlyConnectedRegionAndClusterName()
if err != nil {
return vault.CloudProviderConfig{}, errors.Wrap(err, "finding default AWS region")
}
v.applyDefaultRegionIfEmpty(param.AWS, clusterRegion)
if param.AWS.AutoCreate {
domain := "jenkins-x-domain"
username := param.AWS.IAMUsername
if username == "" {
username = "vault_" + clusterRegion
}
bucketName := param.AWS.S3Bucket
if bucketName == "" {
bucketName = "vault-unseal." + param.AWS.S3Region + "." + domain
}
valueUUID, err := uuid.NewV4()
if err != nil {
return vault.CloudProviderConfig{}, errors.Wrapf(err, "Generating UUID failed")
}
// Create suffix to apply to resources
suffixString := valueUUID.String()[:7]
var kmsID, s3Name, tableName, accessID, secretKey *string
if param.Boot {
accessID, secretKey, kmsID, s3Name, tableName, err = awsvault.CreateVaultResourcesBoot(awsvault.ResourceCreationOpts{
Region: clusterRegion,
Domain: domain,
Username: username,
BucketName: bucketName,
TableName: autoCreateTableName,
AWSTemplatesDir: param.AWS.TemplatesDir,
AccessKeyID: param.AWS.AccessKeyID,
SecretAccessKey: param.AWS.SecretAccessKey,
UniqueSuffix: suffixString,
})
} else {
// left for non-boot clusters until deprecation
accessID, secretKey, kmsID, s3Name, tableName, err = awsvault.CreateVaultResources(awsvault.ResourceCreationOpts{
Region: clusterRegion,
Domain: domain,
Username: username,
BucketName: bucketName,
TableName: autoCreateTableName,
})
}
if err != nil {
return vault.CloudProviderConfig{}, errors.Wrap(err, "an error occurred while creating the vaultCRD resources")
}
if s3Name != nil {
param.AWS.S3Bucket = *s3Name
}
if kmsID != nil {
param.AWS.KMSKeyID = *kmsID
}
if tableName != nil {
param.AWS.DynamoDBTable = *tableName
}
if accessID != nil {
param.AWS.AccessKeyID = *accessID
}
if secretKey != nil {
param.AWS.SecretAccessKey = *secretKey
}
} else {
if param.AWS.S3Bucket == "" {
return vault.CloudProviderConfig{}, fmt.Errorf("missing S3 bucket flag")
}
if param.AWS.KMSKeyID == "" {
return vault.CloudProviderConfig{}, fmt.Errorf("missing AWS KMS key id flag")
}
if param.AWS.AccessKeyID == "" {
return vault.CloudProviderConfig{}, fmt.Errorf("missing AWS access key id flag")
}
if param.AWS.SecretAccessKey == "" {
return vault.CloudProviderConfig{}, fmt.Errorf("missing AWS secret access key flag")
}
}
awsServiceAccountSecretName, err := awsvault.StoreAWSCredentialsIntoSecret(param.KubeClient, param.AWS.AccessKeyID, param.AWS.SecretAccessKey, vaultCRD.Name, param.Namespace)
if err != nil {
return vault.CloudProviderConfig{}, errors.Wrap(err, "storing the service account credentials into a secret")
}
vaultConfig := vault.AWSConfig{
AutoCreate: param.AWS.AutoCreate,
DynamoDBTable: param.AWS.DynamoDBTable,
DynamoDBRegion: param.AWS.DynamoDBRegion,
AccessKeyID: param.AWS.AccessKeyID,
SecretAccessKey: param.AWS.SecretAccessKey,
ProvidedIAMUsername: param.AWS.IAMUsername,
AWSUnsealConfig: v1alpha1.AWSUnsealConfig{
KMSKeyID: param.AWS.KMSKeyID,
KMSRegion: param.AWS.KMSRegion,
S3Bucket: param.AWS.S3Bucket,
S3Prefix: param.AWS.S3Prefix,
S3Region: param.AWS.S3Region,
},
}
return vault.PrepareAWSVaultCRD(awsServiceAccountSecretName, &vaultConfig)
}
func (v *defaultVaultCreator) extractCloudProviderConfig(vaultCRD *v1alpha1.Vault) (vault.CloudProviderConfig, error) {
var cloudProviderConfig = vault.CloudProviderConfig{}
storageConfig := vaultCRD.Spec.Config["storage"]
if storageConfig == nil {
return cloudProviderConfig, errors.Errorf("unable to find storage config in Vault CRD %s", vaultCRD.Name)
}
storage, ok := storageConfig.(map[string]interface{})
if !ok {
return cloudProviderConfig, errors.Errorf("unexpected storage config in Vault CRD %s: %v", vaultCRD.Name, storageConfig)
}
sealConfig := vaultCRD.Spec.Config["seal"]
if sealConfig == nil {
return cloudProviderConfig, errors.Errorf("unable to find seal config in Vault CRD %s", vaultCRD.Name)
}
seal, ok := sealConfig.(map[string]interface{})
if !ok {
return cloudProviderConfig, errors.Errorf("unexpected seal config in Vault CRD %s: %v", vaultCRD.Name, sealConfig)
}
cloudProviderConfig = vault.CloudProviderConfig{
Storage: storage,
Seal: seal,
UnsealConfig: vaultCRD.Spec.UnsealConfig,
CredentialsConfig: vaultCRD.Spec.CredentialsConfig,
}
return cloudProviderConfig, nil
}
// applyDefaultRegionIfEmpty applies the default region to all AWS resources
func (v *defaultVaultCreator) applyDefaultRegionIfEmpty(awsParam *AWSParam, defaultRegion string) {
if awsParam.DynamoDBRegion == "" {
log.Logger().Infof("DynamoDBRegion not specified, defaulting to %s", util.ColorInfo(defaultRegion))
if awsParam.DynamoDBRegion == "" {
awsParam.DynamoDBRegion = defaultRegion
}
}
if awsParam.KMSRegion == "" {
log.Logger().Infof("KMSRegion not specified, defaulting to %s", util.ColorInfo(defaultRegion))
if awsParam.KMSRegion == "" {
awsParam.KMSRegion = defaultRegion
}
}
if awsParam.S3Region == "" {
log.Logger().Infof("S3Region not specified, defaulting to %s", util.ColorInfo(defaultRegion))
if awsParam.S3Region == "" {
awsParam.S3Region = defaultRegion
}
}
}