Skip to content

Commit 092c36f

Browse files
Initial commit for service account impersonation in different universe (#14064) (#23063)
[upstream:7303f23ddbd506ffdd2b98c3ec1a4223de6a44f8] Signed-off-by: Modular Magician <[email protected]>
1 parent c8b6700 commit 092c36f

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

.changelog/14064.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
provider: supported service account impersonation in different universes through credential file
3+
```

google/provider/universe/universe_domain_storage_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,29 @@ func TestAccUniverseDomainStorage(t *testing.T) {
3434

3535
universeDomain := envvar.GetTestUniverseDomainFromEnv(t)
3636
bucketName := acctest.TestBucketName(t)
37+
region := envvar.GetTestRegionFromEnv()
3738

3839
acctest.VcrTest(t, resource.TestCase{
3940
PreCheck: func() { acctest.AccTestPreCheck(t) },
4041
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
4142
CheckDestroy: testAccStorageBucketDestroyProducer(t),
4243
Steps: []resource.TestStep{
4344
{
44-
Config: testAccUniverseDomain_bucket(universeDomain, bucketName),
45+
Config: testAccUniverseDomain_bucket(universeDomain, bucketName, region),
4546
},
4647
},
4748
})
4849
}
4950

50-
func testAccUniverseDomain_bucket(universeDomain string, bucketName string) string {
51+
func testAccUniverseDomain_bucket(universeDomain string, bucketName string, region string) string {
5152
return fmt.Sprintf(`
5253
provider "google" {
5354
universe_domain = "%s"
5455
}
5556
5657
resource "google_storage_bucket" "foo" {
5758
name = "%s"
58-
location = "US"
59+
location = "%s"
5960
}
6061
6162
data "google_storage_bucket" "bar" {
@@ -64,7 +65,7 @@ data "google_storage_bucket" "bar" {
6465
google_storage_bucket.foo,
6566
]
6667
}
67-
`, universeDomain, bucketName)
68+
`, universeDomain, bucketName, region)
6869
}
6970

7071
func testAccStorageBucketDestroyProducer(t *testing.T) func(s *terraform.State) error {

google/transport/config.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ import (
3030
"strings"
3131
"time"
3232

33+
"cloud.google.com/go/auth/credentials"
34+
"cloud.google.com/go/auth/credentials/impersonate"
35+
"cloud.google.com/go/auth/oauth2adapt"
3336
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
3437
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
3538

@@ -2375,10 +2378,33 @@ func (c *Config) GetCredentials(clientScopes []string, initialCredentialsOnly bo
23752378
}
23762379

23772380
if c.ImpersonateServiceAccount != "" && !initialCredentialsOnly {
2378-
opts := []option.ClientOption{option.WithCredentialsJSON([]byte(contents)), option.ImpersonateCredentials(c.ImpersonateServiceAccount, c.ImpersonateServiceAccountDelegates...), option.WithScopes(clientScopes...)}
2379-
creds, err := transport.Creds(context.TODO(), opts...)
2381+
jsonCreds, err := credentials.DetectDefault(&credentials.DetectOptions{
2382+
Scopes: clientScopes,
2383+
CredentialsJSON: []byte(contents),
2384+
})
23802385
if err != nil {
2381-
return googleoauth.Credentials{}, err
2386+
return googleoauth.Credentials{}, fmt.Errorf("error loading credentials: %s", err)
2387+
}
2388+
2389+
impersonateOpts := &impersonate.CredentialsOptions{
2390+
TargetPrincipal: c.ImpersonateServiceAccount,
2391+
Scopes: clientScopes,
2392+
Delegates: c.ImpersonateServiceAccountDelegates,
2393+
Credentials: jsonCreds,
2394+
}
2395+
2396+
if c.UniverseDomain != "" && c.UniverseDomain != "googleapis.com" {
2397+
impersonateOpts.UniverseDomain = c.UniverseDomain
2398+
}
2399+
2400+
authCred, err := impersonate.NewCredentials(impersonateOpts)
2401+
if err != nil {
2402+
return googleoauth.Credentials{}, fmt.Errorf("error loading credentials: %s", err)
2403+
}
2404+
2405+
creds := oauth2adapt.Oauth2CredentialsFromAuthCredentials(authCred)
2406+
if err != nil {
2407+
return googleoauth.Credentials{}, fmt.Errorf("error loading credentials: %s", err)
23822408
}
23832409
return *creds, nil
23842410
}

0 commit comments

Comments
 (0)