Skip to content

Commit

Permalink
Fix condition to fetch id token from API (#8195) (#5825)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Jun 28, 2023
1 parent 699167d commit 00351e2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
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

0 comments on commit 00351e2

Please sign in to comment.