forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcloud.go
157 lines (132 loc) · 3.91 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
package gke
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
)
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"}
)
func GetOrCreateServiceAccount(serviceAccount string, projectId string, clusterConfigDir string) (string, error) {
args := []string{"iam",
"service-accounts",
"list",
"--filter",
serviceAccount,
"--project",
projectId}
output, err := util.RunCommandWithOutput("", "gcloud", args...)
if err != nil {
return "", err
}
if output == "Listed 0 items." {
log.Infof("Unable to find service account %s, checking if we have enough permission to create\n", 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", serviceAccount)
args = []string{"iam",
"service-accounts",
"create",
serviceAccount,
"--project",
projectId}
err = util.RunCommand("", "gcloud", args...)
if err != nil {
return "", err
}
// assign roles to service account
for _, role := range REQUIRED_SERVICE_ACCOUNT_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}
err = util.RunCommand("", "gcloud", args...)
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")
args = []string{"iam",
"service-accounts",
"keys",
"create",
keyPath,
"--iam-account",
fmt.Sprintf("%s@%s.iam.gserviceaccount.com", serviceAccount, projectId),
"--project",
projectId}
err = util.RunCommand("", "gcloud", args...)
if err != nil {
return "", err
}
}
return keyPath, nil
}
func EnableApis(apis ...string) error {
args := []string{"services", "enable"}
args = append(args, apis...)
log.Infof("Lets ensure we have container and compute enabled on your project via: %s\n", util.ColorInfo("gcloud "+strings.Join(args, " ")))
err := util.RunCommand("", "gcloud", args...)
if err != nil {
return err
}
return nil
}
func Login(serviceAccountKeyPath string, skipLogin bool) error {
if serviceAccountKeyPath != "" {
if _, err := os.Stat(serviceAccountKeyPath); os.IsNotExist(err) {
return errors.New("Unable to locate service account " + serviceAccountKeyPath)
}
err := util.RunCommand("", "gcloud", "auth", "activate-service-account", "--key-file", serviceAccountKeyPath)
if err != nil {
return err
}
} else if !skipLogin {
err := util.RunCommand("", "gcloud", "auth", "login", "--brief")
if err != nil {
return err
}
}
return nil
}
func CheckPermission(perm string, projectId string) (bool, error) {
// 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}
output, err := util.RunCommandWithOutput("", "gcloud", args...)
if err != nil {
return false, err
}
return strings.Contains(output, perm), nil
}