Skip to content

Commit

Permalink
Fix id token datasource (#4058) (#7457)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Oct 7, 2020
1 parent 0804649 commit 8f4f163
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 18 deletions.
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

0 comments on commit 8f4f163

Please sign in to comment.