Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix condition to fetch id token from API #5825

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/8195.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resourcemanager: fixed handling of `google_service_account_id_token` when authenticated with GCE metadata credentials
```
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ func dataSourceGoogleServiceAccountIdTokenRead(d *schema.ResourceData, meta inte
return fmt.Errorf("error calling getCredentials(): %v", err)
}

// If the source credential is not a service account key, use the API to generate the idToken
if creds.JSON == nil {
targetServiceAccount := d.Get("target_service_account").(string)
// If a target service account is provided, use the API to generate the idToken
if targetServiceAccount != "" {
// Use
// https://cloud.google.com/iam/docs/reference/credentials/rest/v1/projects.serviceAccounts/generateIdToken
service := config.NewIamCredentialsClient(userAgent)
name := fmt.Sprintf("projects/-/serviceAccounts/%s", d.Get("target_service_account").(string))
name := fmt.Sprintf("projects/-/serviceAccounts/%s", targetServiceAccount)
tokenRequest := &iamcredentials.GenerateIdTokenRequest{
Audience: targetAudience,
IncludeEmail: d.Get("include_email").(bool),
Expand All @@ -95,7 +96,7 @@ func dataSourceGoogleServiceAccountIdTokenRead(d *schema.ResourceData, meta inte
return fmt.Errorf("error calling iamcredentials.GenerateIdToken: %v", err)
}

d.SetId(d.Get("target_service_account").(string))
d.SetId(targetServiceAccount)
if err := d.Set("id_token", at.Token); err != nil {
return fmt.Errorf("Error setting id_token: %s", err)
}
Expand Down