Skip to content

Commit

Permalink
Fix condition to fetch id token from API (GoogleCloudPlatform#8195)
Browse files Browse the repository at this point in the history
  • Loading branch information
golemiso authored and ericayyliu committed Jul 26, 2023
1 parent 43a0871 commit c380294
Showing 1 changed file with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,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 @@ -93,7 +94,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 c380294

Please sign in to comment.