Skip to content

Commit 0e94e21

Browse files
authored
Allow for specify scopes for assigned service account (#401)
* draft commit * initial fix * split '0' 😕 * confused by my own comments 😢 * handle shorthand
1 parent f953612 commit 0e94e21

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

iterative/gcp/provider.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
3434
instanceZone := getRegion(d.Get("region").(string))
3535
instanceHddSize := int64(d.Get("instance_hdd_size").(int))
3636
instancePublicSshKey := fmt.Sprintf("%s:%s %s\n", "ubuntu", strings.TrimSpace(d.Get("ssh_public").(string)), "ubuntu")
37-
instanceServiceAccount := d.Get("instance_permission_set").(string)
37+
38+
serviceAccountEmail, serviceAccountScopes := getServiceAccountData(d.Get("instance_permission_set").(string))
3839

3940
instanceMetadata := map[string]string{}
4041
for key, value := range d.Get("metadata").(map[string]interface{}) {
@@ -199,7 +200,8 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
199200
MachineType: instanceMachineType.SelfLink,
200201
ServiceAccounts: []*gcp_compute.ServiceAccount{
201202
{
202-
Email: instanceServiceAccount,
203+
Email: serviceAccountEmail,
204+
Scopes: serviceAccountScopes,
203205
},
204206
},
205207
Disks: []*gcp_compute.AttachedDisk{
@@ -287,6 +289,21 @@ func ResourceMachineDelete(ctx context.Context, d *schema.ResourceData, m interf
287289
return nil
288290
}
289291

292+
func getServiceAccountData(saString string) (string, []string) {
293+
// ["SA email", "scopes=s1", "s2", ...]
294+
splitStr := strings.Split(saString, ",")
295+
serviceAccountEmail := splitStr[0]
296+
if len(splitStr) == 1 {
297+
// warn user about scopes?
298+
return serviceAccountEmail, nil
299+
}
300+
// ["scopes=s1", "s2"]
301+
splitStr[1] = strings.Split(splitStr[1], "=")[1]
302+
// ["s1", "s2", ...]
303+
serviceAccountScopes := splitStr[1:]
304+
return serviceAccountEmail, utils.CanonicalizeServiceScopes(serviceAccountScopes)
305+
}
306+
290307
func getProjectService() (string, *gcp_compute.Service, error) {
291308
var credentials *google.Credentials
292309
var err error

iterative/utils/helpers.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,47 @@ func LoadGCPCredentials() string {
3737
}
3838
return credentialsData
3939
}
40+
41+
// Better way than copying?
42+
// https://github.com/hashicorp/terraform-provider-google/blob/8a362008bd4d36b6a882eb53455f87305e6dff52/google/service_scope.go#L5-L48
43+
func canonicalizeServiceScope(scope string) string {
44+
// This is a convenience map of short names used by the gcloud tool
45+
// to the GCE auth endpoints they alias to.
46+
scopeMap := map[string]string{
47+
"bigquery": "https://www.googleapis.com/auth/bigquery",
48+
"cloud-platform": "https://www.googleapis.com/auth/cloud-platform",
49+
"cloud-source-repos": "https://www.googleapis.com/auth/source.full_control",
50+
"cloud-source-repos-ro": "https://www.googleapis.com/auth/source.read_only",
51+
"compute-ro": "https://www.googleapis.com/auth/compute.readonly",
52+
"compute-rw": "https://www.googleapis.com/auth/compute",
53+
"datastore": "https://www.googleapis.com/auth/datastore",
54+
"logging-write": "https://www.googleapis.com/auth/logging.write",
55+
"monitoring": "https://www.googleapis.com/auth/monitoring",
56+
"monitoring-read": "https://www.googleapis.com/auth/monitoring.read",
57+
"monitoring-write": "https://www.googleapis.com/auth/monitoring.write",
58+
"pubsub": "https://www.googleapis.com/auth/pubsub",
59+
"service-control": "https://www.googleapis.com/auth/servicecontrol",
60+
"service-management": "https://www.googleapis.com/auth/service.management.readonly",
61+
"sql": "https://www.googleapis.com/auth/sqlservice",
62+
"sql-admin": "https://www.googleapis.com/auth/sqlservice.admin",
63+
"storage-full": "https://www.googleapis.com/auth/devstorage.full_control",
64+
"storage-ro": "https://www.googleapis.com/auth/devstorage.read_only",
65+
"storage-rw": "https://www.googleapis.com/auth/devstorage.read_write",
66+
"taskqueue": "https://www.googleapis.com/auth/taskqueue",
67+
"trace": "https://www.googleapis.com/auth/trace.append",
68+
"useraccounts-ro": "https://www.googleapis.com/auth/cloud.useraccounts.readonly",
69+
"useraccounts-rw": "https://www.googleapis.com/auth/cloud.useraccounts",
70+
"userinfo-email": "https://www.googleapis.com/auth/userinfo.email",
71+
}
72+
if matchedURL, ok := scopeMap[scope]; ok {
73+
return matchedURL
74+
}
75+
return scope
76+
}
77+
func CanonicalizeServiceScopes(scopes []string) []string {
78+
cs := make([]string, len(scopes))
79+
for i, scope := range scopes {
80+
cs[i] = canonicalizeServiceScope(scope)
81+
}
82+
return cs
83+
}

0 commit comments

Comments
 (0)