-
Notifications
You must be signed in to change notification settings - Fork 787
/
gcloud.go
744 lines (652 loc) · 17.7 KB
/
gcloud.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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
package gke
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/jenkins-x/jx/pkg/kube"
yaml "gopkg.in/yaml.v2"
"time"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
)
// KmsLocation indicates the location used by the Google KMS service
const KmsLocation = "global"
var (
REQUIRED_SERVICE_ACCOUNT_ROLES = []string{"roles/compute.instanceAdmin.v1",
"roles/iam.serviceAccountActor",
"roles/container.clusterAdmin",
"roles/container.admin",
"roles/container.developer",
"roles/storage.objectAdmin",
"roles/editor"}
)
// ClusterName gets the cluster name from the current context
// Note that this just reads the ClusterName from the local kube config, which can be renamed (but is unlikely to happen)
func ClusterName(kuber kube.Kuber) (string, error) {
config, _, err := kuber.LoadConfig()
if err != nil {
return "", err
}
context := kube.CurrentContext(config)
if context == nil {
return "", errors.New("kube context was nil")
}
// context.Cluster will likely be in the form gke_<accountName>_<region>_<clustername>
// Trim off the crud from the beginning context.Cluster
return GetSimplifiedClusterName(context.Cluster), nil
}
// ShortClusterName returns a short clusters name. Eg, if ClusterName would return tweetypie-jenkinsx-dev, ShortClusterName
// would return tweetypie. This is needed because GCP has character limits on things like service accounts (6-30 chars)
// and combining a long cluster name and a long vault name exceeds this limit
func ShortClusterName(kuber kube.Kuber) (string, error) {
clusterName, err := ClusterName(kuber)
return strings.Split(clusterName, "-")[0], err
}
// GetSimplifiedClusterName get the simplified cluster name from the long-winded context cluster name that gets generated
// GKE cluster names as defined in the kube config are of the form gke_<projectname>_<region>_<clustername>
// This method will return <clustername> in the above
func GetSimplifiedClusterName(complexClusterName string) string {
split := strings.Split(complexClusterName, "_")
return split[len(split)-1]
}
// ClusterZone retrives the zone of GKE cluster description
func ClusterZone(cluster string) (string, error) {
args := []string{"container",
"clusters",
"describe",
cluster}
cmd := util.Command{
Name: "gcloud",
Args: args,
}
output, err := cmd.RunWithoutRetry()
if err != nil {
return "", err
}
zone, err := parseClusterZone(output)
if err != nil {
return "", err
}
return zone, nil
}
func parseClusterZone(clusterInfo string) (string, error) {
ci := struct {
Zone string `yaml:"zone"`
}{}
err := yaml.Unmarshal([]byte(clusterInfo), &ci)
if err != nil {
return "", errors.Wrap(err, "extracting cluster zone from cluster info")
}
return ci.Zone, nil
}
// BucketExists checks if a Google Storage bucket exists
func BucketExists(projectID string, bucketName string) (bool, error) {
fullBucketName := fmt.Sprintf("gs://%s", bucketName)
args := []string{"ls"}
if projectID != "" {
args = append(args, "-p")
args = append(args, projectID)
}
cmd := util.Command{
Name: "gsutil",
Args: args,
}
output, err := cmd.RunWithoutRetry()
if err != nil {
log.Infof("Error checking bucket exists: %s, %s\n", output, err)
return false, err
}
return strings.Contains(output, fullBucketName), nil
}
// CreateBucket creates a new Google Storage bucket
func CreateBucket(projectID string, bucketName string, location string) error {
fullBucketName := fmt.Sprintf("gs://%s", bucketName)
args := []string{"mb", "-l", location}
if projectID != "" {
args = append(args, "-p")
args = append(args, projectID)
}
args = append(args, fullBucketName)
cmd := util.Command{
Name: "gsutil",
Args: args,
}
output, err := cmd.RunWithoutRetry()
if err != nil {
log.Infof("Error creating bucket: %s, %s\n", output, err)
return err
}
return nil
}
// FindBucket finds a Google Storage bucket
func FindBucket(bucketName string) bool {
fullBucketName := fmt.Sprintf("gs://%s", bucketName)
args := []string{"list", "-b", fullBucketName}
cmd := util.Command{
Name: "gsutil",
Args: args,
}
_, err := cmd.RunWithoutRetry()
if err != nil {
return false
}
return true
}
// DeleteAllObjectsInBucket deletes all objects in a Google Storage bucket
func DeleteAllObjectsInBucket(bucketName string) error {
found := FindBucket(bucketName)
if !found {
return nil // nothing to delete
}
fullBucketName := fmt.Sprintf("gs://%s", bucketName)
args := []string{"-m", "rm", "-r", fullBucketName}
cmd := util.Command{
Name: "gsutil",
Args: args,
}
_, err := cmd.RunWithoutRetry()
if err != nil {
return err
}
return nil
}
// DeleteBucket deletes a Google storage bucket
func DeleteBucket(bucketName string) error {
found := FindBucket(bucketName)
if !found {
return nil // nothing to delete
}
fullBucketName := fmt.Sprintf("gs://%s", bucketName)
args := []string{"rb", fullBucketName}
cmd := util.Command{
Name: "gsutil",
Args: args,
}
_, err := cmd.RunWithoutRetry()
if err != nil {
return err
}
return nil
}
// GetRegionFromZone parses the region from a GCP zone name
func GetRegionFromZone(zone string) string {
return zone[0 : len(zone)-2]
}
// FindServiceAccount checks if a service account exists
func FindServiceAccount(serviceAccount string, projectID string) bool {
args := []string{"iam",
"service-accounts",
"list",
"--filter",
serviceAccount,
"--project",
projectID}
cmd := util.Command{
Name: "gcloud",
Args: args,
}
output, err := cmd.RunWithoutRetry()
if err != nil {
return false
}
if output == "Listed 0 items." {
return false
}
return true
}
// GetOrCreateServiceAccount retrieves or creates a GCP service account. It will return the path to the file where the service
// account token is stored
func GetOrCreateServiceAccount(serviceAccount string, projectID string, clusterConfigDir string, roles []string) (string, error) {
if projectID == "" {
return "", errors.New("cannot get/create a service account without a projectId")
}
found := FindServiceAccount(serviceAccount, projectID)
if !found {
log.Infof("Unable to find service account %s, checking if we have enough permission to create\n", util.ColorInfo(serviceAccount))
// if it doesn't check to see if we have permissions to create (assign roles) to a service account
hasPerm, err := CheckPermission("resourcemanager.projects.setIamPolicy", projectID)
if err != nil {
return "", err
}
if !hasPerm {
return "", errors.New("User does not have the required role 'resourcemanager.projects.setIamPolicy' to configure a service account")
}
// create service
log.Infof("Creating service account %s\n", util.ColorInfo(serviceAccount))
args := []string{"iam",
"service-accounts",
"create",
serviceAccount,
"--project",
projectID}
cmd := util.Command{
Name: "gcloud",
Args: args,
}
_, err = cmd.RunWithoutRetry()
if err != nil {
return "", err
}
// assign roles to service account
for _, role := range roles {
log.Infof("Assigning role %s\n", role)
args = []string{"projects",
"add-iam-policy-binding",
projectID,
"--member",
fmt.Sprintf("serviceAccount:%s@%s.iam.gserviceaccount.com", serviceAccount, projectID),
"--role",
role,
"--project",
projectID}
cmd := util.Command{
Name: "gcloud",
Args: args,
}
_, err := cmd.RunWithoutRetry()
if err != nil {
return "", err
}
}
} else {
log.Info("Service Account exists\n")
}
os.MkdirAll(clusterConfigDir, os.ModePerm)
keyPath := filepath.Join(clusterConfigDir, fmt.Sprintf("%s.key.json", serviceAccount))
if _, err := os.Stat(keyPath); os.IsNotExist(err) {
log.Info("Downloading service account key\n")
err := CreateServiceAccountKey(serviceAccount, projectID, keyPath)
if err != nil {
log.Infof("Exceeds the maximum number of keys on service account %s\n",
util.ColorInfo(serviceAccount))
err := CleanupServiceAccountKeys(serviceAccount, projectID)
if err != nil {
return "", errors.Wrap(err, "cleaning up the service account keys")
}
err = CreateServiceAccountKey(serviceAccount, projectID, keyPath)
if err != nil {
return "", errors.Wrap(err, "creating service account key")
}
}
} else {
log.Info("Key already exists")
}
return keyPath, nil
}
// CreateServiceAccountKey creates a new service account key and downloads into the given file
func CreateServiceAccountKey(serviceAccount string, projectID string, keyPath string) error {
args := []string{"iam",
"service-accounts",
"keys",
"create",
keyPath,
"--iam-account",
fmt.Sprintf("%s@%s.iam.gserviceaccount.com", serviceAccount, projectID),
"--project",
projectID}
cmd := util.Command{
Name: "gcloud",
Args: args,
}
_, err := cmd.RunWithoutRetry()
if err != nil {
return errors.Wrap(err, "creating a new service account key")
}
return nil
}
// GetServiceAccountKeys returns all keys of a service account
func GetServiceAccountKeys(serviceAccount string, projectID string) ([]string, error) {
keys := []string{}
account := fmt.Sprintf("%s@%s.iam.gserviceaccount.com", serviceAccount, projectID)
args := []string{"iam",
"service-accounts",
"keys",
"list",
"--iam-account",
account,
"--project",
projectID}
cmd := util.Command{
Name: "gcloud",
Args: args,
}
output, err := cmd.RunWithoutRetry()
if err != nil {
return keys, errors.Wrapf(err, "listing the keys of the service account '%s'", account)
}
scanner := bufio.NewScanner(strings.NewReader(output))
// Skip the first line with the header information
scanner.Scan()
for scanner.Scan() {
keyFields := strings.Fields(scanner.Text())
if len(keyFields) > 0 {
keys = append(keys, keyFields[0])
}
}
return keys, nil
}
// DeleteServiceAccountKey deletes a service account key
func DeleteServiceAccountKey(serviceAccount string, projectID string, key string) error {
account := fmt.Sprintf("%s@%s.iam.gserviceaccount.com", serviceAccount, projectID)
args := []string{"iam",
"service-accounts",
"keys",
"delete",
key,
"--iam-account",
account,
"--project",
projectID,
"--quiet"}
cmd := util.Command{
Name: "gcloud",
Args: args,
}
_, err := cmd.RunWithoutRetry()
if err != nil {
return errors.Wrapf(err, "deleting the key '%s'from service account '%s'", key, account)
}
return nil
}
// CleanupServiceAccountKeys remove all keys from given service account
func CleanupServiceAccountKeys(serviceAccount string, projectID string) error {
keys, err := GetServiceAccountKeys(serviceAccount, projectID)
if err != nil {
return errors.Wrap(err, "retrieving the service account keys")
}
log.Infof("Cleaning up the keys of the service account %s\n", util.ColorInfo(serviceAccount))
for _, key := range keys {
err := DeleteServiceAccountKey(serviceAccount, projectID, key)
if err != nil {
log.Infof("Cannot delete the key %s from service account %s: %v\n",
util.ColorWarning(key), util.ColorInfo(serviceAccount), err)
} else {
log.Infof("Key %s was removed form service account %s\n",
util.ColorInfo(key), util.ColorInfo(serviceAccount))
}
}
return nil
}
// DeleteServiceAccount deletes a service account and its role bindings
func DeleteServiceAccount(serviceAccount string, projectID string, roles []string) error {
found := FindServiceAccount(serviceAccount, projectID)
if !found {
return nil // nothing to delete
}
// remove roles to service account
for _, role := range roles {
log.Infof("Removing role %s\n", role)
args := []string{"projects",
"remove-iam-policy-binding",
projectID,
"--member",
fmt.Sprintf("serviceAccount:%s@%s.iam.gserviceaccount.com", serviceAccount, projectID),
"--role",
role,
"--project",
projectID}
cmd := util.Command{
Name: "gcloud",
Args: args,
}
_, err := cmd.RunWithoutRetry()
if err != nil {
return err
}
}
args := []string{"iam",
"service-accounts",
"delete",
fmt.Sprintf("%s@%s.iam.gserviceaccount.com", serviceAccount, projectID),
"--project",
projectID}
cmd := util.Command{
Name: "gcloud",
Args: args,
}
_, err := cmd.RunWithoutRetry()
if err != nil {
return err
}
return nil
}
// GetEnabledApis returns which services have the API enabled
func GetEnabledApis(projectID string) ([]string, error) {
args := []string{"services", "list", "--enabled"}
if projectID != "" {
args = append(args, "--project")
args = append(args, projectID)
}
apis := []string{}
cmd := util.Command{
Name: "gcloud",
Args: args,
}
out, err := cmd.RunWithoutRetry()
if err != nil {
return nil, err
}
lines := strings.Split(string(out), "\n")
for _, l := range lines {
if strings.Contains(l, "NAME") {
continue
}
fields := strings.Fields(l)
apis = append(apis, fields[0])
}
return apis, nil
}
// EnableAPIs enables APIs for the given services
func EnableAPIs(projectID string, apis ...string) error {
enabledApis, err := GetEnabledApis(projectID)
if err != nil {
return err
}
toEnableArray := []string{}
for _, toEnable := range apis {
fullName := fmt.Sprintf("%s.googleapis.com", toEnable)
if !util.Contains(enabledApis, fullName) {
toEnableArray = append(toEnableArray, toEnable)
}
}
if len(toEnableArray) == 0 {
log.Infof("No apis to enable\n")
return nil
}
args := []string{"services", "enable"}
args = append(args, toEnableArray...)
if projectID != "" {
args = append(args, "--project")
args = append(args, projectID)
}
log.Infof("Lets ensure we have container and compute enabled on your project via: %s\n", util.ColorInfo("gcloud "+strings.Join(args, " ")))
cmd := util.Command{
Name: "gcloud",
Args: args,
}
_, err = cmd.RunWithoutRetry()
if err != nil {
return err
}
return nil
}
// Login login an user into Google account. It skips the interactive login using the
// browser when the skipLogin flag is active
func Login(serviceAccountKeyPath string, skipLogin bool) error {
if serviceAccountKeyPath != "" {
log.Infof("Activating service account %s\n", util.ColorInfo(serviceAccountKeyPath))
if _, err := os.Stat(serviceAccountKeyPath); os.IsNotExist(err) {
return errors.New("Unable to locate service account " + serviceAccountKeyPath)
}
cmd := util.Command{
Name: "gcloud",
Args: []string{"auth", "activate-service-account", "--key-file", serviceAccountKeyPath},
}
_, err := cmd.RunWithoutRetry()
if err != nil {
return err
}
// GCP IAM changes can take up to 80 seconds to propagate
retry(10, 10*time.Second, func() error {
log.Infof("Checking for readiness...\n")
projects, err := GetGoogleProjects()
if err != nil {
return err
}
if len(projects) == 0 {
return errors.New("service account not ready yet")
}
return nil
})
} else if !skipLogin {
cmd := util.Command{
Name: "gcloud",
Args: []string{"auth", "login", "--brief"},
}
_, err := cmd.RunWithoutRetry()
if err != nil {
return err
}
}
return nil
}
func retry(attempts int, sleep time.Duration, fn func() error) error {
if err := fn(); err != nil {
if s, ok := err.(stop); ok {
// Return the original error for later checking
return s.error
}
if attempts--; attempts > 0 {
time.Sleep(sleep)
return retry(attempts, 2*sleep, fn)
}
return err
}
return nil
}
type stop struct {
error
}
// CheckPermission checks permission on the given project
func CheckPermission(perm string, projectID string) (bool, error) {
if projectID == "" {
return false, errors.New("cannot check permission without a projectId")
}
// if it doesn't check to see if we have permissions to create (assign roles) to a service account
args := []string{"iam",
"list-testable-permissions",
fmt.Sprintf("//cloudresourcemanager.googleapis.com/projects/%s", projectID),
"--filter",
perm}
cmd := util.Command{
Name: "gcloud",
Args: args,
}
output, err := cmd.RunWithoutRetry()
if err != nil {
return false, err
}
return strings.Contains(output, perm), nil
}
// CreateKmsKeyring creates a new KMS keyring
func CreateKmsKeyring(keyringName string, projectID string) error {
if keyringName == "" {
return errors.New("provided keyring name is empty")
}
if IsKmsKeyringAvailable(keyringName, projectID) {
return nil
}
args := []string{"kms",
"keyrings",
"create",
keyringName,
"--location",
KmsLocation,
"--project",
projectID,
}
cmd := util.Command{
Name: "gcloud",
Args: args,
}
_, err := cmd.RunWithoutRetry()
if err != nil {
return errors.Wrap(err, "creating kms keyring")
}
return nil
}
// IsKmsKeyringAvailable checks if the KMS keyring is already available
func IsKmsKeyringAvailable(keyringName string, projectID string) bool {
args := []string{"kms",
"keyrings",
"describe",
keyringName,
"--location",
KmsLocation,
"--project",
projectID,
}
cmd := util.Command{
Name: "gcloud",
Args: args,
}
_, err := cmd.RunWithoutRetry()
if err != nil {
return false
}
return true
}
// CreateKmsKey creates a new KMS key in the given keyring
func CreateKmsKey(keyName string, keyringName string, projectID string) error {
if IsKmsKeyAvailable(keyName, keyringName, projectID) {
return nil
}
args := []string{"kms",
"keys",
"create",
keyName,
"--location",
KmsLocation,
"--keyring",
keyringName,
"--purpose",
"encryption",
"--project",
projectID,
}
cmd := util.Command{
Name: "gcloud",
Args: args,
}
_, err := cmd.RunWithoutRetry()
if err != nil {
return errors.Wrapf(err, "creating kms key '%s' into keyring '%s'", keyName, keyringName)
}
return nil
}
// IsKmsKeyAvailable checks if the KMS key is already available
func IsKmsKeyAvailable(keyName string, keyringName string, projectID string) bool {
args := []string{"kms",
"keys",
"describe",
keyName,
"--location",
KmsLocation,
"--keyring",
keyringName,
"--project",
projectID,
}
cmd := util.Command{
Name: "gcloud",
Args: args,
}
_, err := cmd.RunWithoutRetry()
if err != nil {
return false
}
return true
}