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 id token datasource #7457

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/4058.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
iam: fixed `google_service_account_id_token` datasource to work with User ADCs and Impersonated Credentials
```
15 changes: 2 additions & 13 deletions google/data_source_google_service_account_id_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,8 @@ func dataSourceGoogleServiceAccountIdTokenRead(d *schema.ResourceData, meta inte
return fmt.Errorf("error calling getCredentials(): %v", err)
}

ts := creds.TokenSource

// If the source token is just an access_token, all we can do is use the iamcredentials api to get an id_token
if _, ok := ts.(staticTokenSource); ok {
// If the source credential is not a service account key, use the API to generate the idToken
if creds.JSON == nil {
// Use
// https://cloud.google.com/iam/docs/reference/credentials/rest/v1/projects.serviceAccounts/generateIdToken
service := config.NewIamCredentialsClient(userAgent)
Expand All @@ -100,15 +98,6 @@ func dataSourceGoogleServiceAccountIdTokenRead(d *schema.ResourceData, meta inte
return nil
}

tok, err := ts.Token()
if err != nil {
return fmt.Errorf("unable to get Token() from tokenSource: %v", err)
}

// only user-credential TokenSources have refreshTokens
if tok.RefreshToken != "" {
return fmt.Errorf("unsupported Credential Type supplied. Use serviceAccount credentials")
}
ctx := context.Background()
co := []option.ClientOption{}
if creds.JSON != nil {
Expand Down
4 changes: 4 additions & 0 deletions google/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,10 @@ func getTestProjectFromEnv() string {

// testAccPreCheck ensures at least one of the credentials env variables is set.
func getTestCredsFromEnv() string {
// Return empty string if GOOGLE_USE_DEFAULT_CREDENTIALS is set to true.
if multiEnvSearch(credsEnvVars) == "true" {
return ""
}
return multiEnvSearch(credsEnvVars)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ For more information see
```

## Example Usage - Service Account Impersonation.
`google_service_account_access_token` will use background impersonated credentials provided by [google_service_account_access_token](https://www.terraform.io/docs/providers/google/d/datasource_google_service_account_access_token.html).
`google_service_account_access_token` will use background impersonated credentials provided by [google_service_account_access_token](https://www.terraform.io/docs/providers/google/d/service_account_access_token.html).

Note: to use the following, you must grant `target_service_account` the
`roles/iam.serviceAccountTokenCreator` role on itself.
Expand Down
8 changes: 4 additions & 4 deletions website/google.erb
Original file line number Diff line number Diff line change
Expand Up @@ -730,10 +730,6 @@
<a href="/docs/providers/google/d/client_openid_userinfo.html">google_client_openid_userinfo</a>
</li>

<li>
<a href="/docs/providers/google/d/datasource_google_service_account_id_token.html">google_service_account_id_token</a>
</li>

<li>
<a href="/docs/providers/google/d/folder.html">google_folder</a>
</li>
Expand Down Expand Up @@ -782,6 +778,10 @@
<a href="/docs/providers/google/d/service_account_access_token.html">google_service_account_access_token</a>
</li>

<li>
<a href="/docs/providers/google/d/service_account_id_token.html">google_service_account_id_token</a>
</li>

<li>
<a href="/docs/providers/google/d/service_account_key.html">google_service_account_key</a>
</li>
Expand Down