From 42390b7f0ac9ebf0ce139459925c57df0bfba2e0 Mon Sep 17 00:00:00 2001 From: Lawrence Qiu Date: Mon, 23 Nov 2026 15:37:07 +0000 Subject: [PATCH 1/5] fix(auth): recompute default tokenUrl when universeDomain changes via toBuilder When cloning an ExternalAccountCredentials instance via toBuilder(), clear tokenUrl if it equals the default STS URL for the credential's current universe domain so build() recomputes https://sts./v1/token for the updated universe domain. Non-default custom token URLs and explicit setTokenUrl() overrides on the builder are preserved regardless of setter ordering. --- .../oauth2/ExternalAccountCredentials.java | 23 +- .../ExternalAccountCredentialsTest.java | 230 ++++++++++++++++++ 2 files changed, 250 insertions(+), 3 deletions(-) diff --git a/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java b/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java index de329a4e0e7f..8635d913911f 100644 --- a/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java +++ b/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java @@ -242,8 +242,10 @@ protected ExternalAccountCredentials(ExternalAccountCredentials.Builder builder) this.clientId = builder.clientId; this.clientSecret = builder.clientSecret; + // Precedence: an explicit or non-default custom tokenUrl always takes priority. Otherwise, when + // builder.tokenUrl is null, compute the default STS URL from the final universe domain. if (builder.tokenUrl == null) { - this.tokenUrl = DEFAULT_TOKEN_URL.replace("{UNIVERSE_DOMAIN}", this.getUniverseDomain()); + this.tokenUrl = defaultTokenUrlForUniverse(this.getUniverseDomain()); } else { this.tokenUrl = builder.tokenUrl; } @@ -790,6 +792,11 @@ int getLifetime() { } } + /** Returns the standard STS token exchange URL for {@code universeDomain}. */ + private static String defaultTokenUrlForUniverse(String universeDomain) { + return DEFAULT_TOKEN_URL.replace("{UNIVERSE_DOMAIN}", universeDomain); + } + /** Base builder for external account credentials. */ public abstract static class Builder extends GoogleCredentials.Builder { @@ -825,7 +832,16 @@ protected Builder(ExternalAccountCredentials credentials) { this.transportFactory = credentials.transportFactory; this.audience = credentials.audience; this.subjectTokenType = credentials.subjectTokenType; - this.tokenUrl = credentials.tokenUrl; + // Built credentials always have tokenUrl populated, either with a custom override or with the + // default STS URL for credentials.getUniverseDomain() (from builder defaults or gcloud JSON). + // Clear tokenUrl to null when it equals that universe default so the configured + // universeDomain + // on toBuilder() can recompute it. Any non-default tokenUrl (e.g. mTLS STS or proxy) stays + // non-null and keeps priority over universeDomain, as do explicit setTokenUrl() calls. + this.tokenUrl = + defaultTokenUrlForUniverse(credentials.getUniverseDomain()).equals(credentials.tokenUrl) + ? null + : credentials.tokenUrl; this.tokenInfoUrl = credentials.tokenInfoUrl; this.serviceAccountImpersonationUrl = credentials.serviceAccountImpersonationUrl; this.targetServiceAccountEmail = credentials.targetServiceAccountEmail; @@ -892,7 +908,8 @@ public Builder setSubjectTokenType(SubjectTokenTypes subjectTokenType) { } /** - * Sets the Security Token Service token exchange endpoint. + * Sets the Security Token Service token exchange endpoint. When set, this custom URL takes + * precedence over the default URL derived from {@code universeDomain}. * * @param tokenUrl the Security Token Service token exchange url to set * @return this {@code Builder} object diff --git a/google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java b/google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java index cc6b2cc280c7..787870d57307 100644 --- a/google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java +++ b/google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java @@ -42,8 +42,14 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import com.google.api.client.http.HttpTransport; +import com.google.api.client.http.LowLevelHttpRequest; +import com.google.api.client.http.LowLevelHttpResponse; import com.google.api.client.json.GenericJson; +import com.google.api.client.json.Json; import com.google.api.client.json.JsonParser; +import com.google.api.client.testing.http.MockHttpTransport; +import com.google.api.client.testing.http.MockLowLevelHttpRequest; +import com.google.api.client.testing.http.MockLowLevelHttpResponse; import com.google.api.client.util.Clock; import com.google.api.client.util.SecurityUtils; import com.google.auth.TestUtils; @@ -59,6 +65,7 @@ import java.math.BigDecimal; import java.net.URI; import java.security.KeyStore; +import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.HashMap; @@ -634,6 +641,229 @@ void constructor_builder_defaultTokenUrlwithUniverseDomain() { assertEquals("https://sts.testdomain.org/v1/token", credentials.getTokenUrl()); } + @Test + void toBuilder_setUniverseDomain_updatesDefaultTokenUrl() { + HashMap credentialSource = new HashMap<>(); + credentialSource.put("file", "file"); + + IdentityPoolCredentials gduCredentials = + IdentityPoolCredentials.newBuilder() + .setHttpTransportFactory(transportFactory) + .setAudience( + "//iam.googleapis.com/locations/global/workforcePools/pool/providers/provider") + .setSubjectTokenType("subjectTokenType") + .setCredentialSource(new TestCredentialSource(credentialSource)) + .build(); + + assertEquals(STS_URL, gduCredentials.getTokenUrl()); + assertEquals(GOOGLE_DEFAULT_UNIVERSE, gduCredentials.getUniverseDomain()); + + // Retargeting a GDU credential must replace its defaulted sts.googleapis.com URL. + IdentityPoolCredentials customUniverseCredentials = + gduCredentials.toBuilder().setUniverseDomain("testdomain.org").build(); + + assertEquals("testdomain.org", customUniverseCredentials.getUniverseDomain()); + assertEquals("https://sts.testdomain.org/v1/token", customUniverseCredentials.getTokenUrl()); + } + + @Test + void toBuilder_setUniverseDomain_chained_updatesDefaultTokenUrl() { + HashMap credentialSource = new HashMap<>(); + credentialSource.put("file", "file"); + + IdentityPoolCredentials firstUniverseCredentials = + IdentityPoolCredentials.newBuilder() + .setHttpTransportFactory(transportFactory) + .setAudience( + "//iam.googleapis.com/locations/global/workforcePools/pool/providers/provider") + .setSubjectTokenType("subjectTokenType") + .setCredentialSource(new TestCredentialSource(credentialSource)) + .setUniverseDomain("testdomain.org") + .build(); + + assertEquals("https://sts.testdomain.org/v1/token", firstUniverseCredentials.getTokenUrl()); + + // Chained retargeting must also recompute because the credential still uses its universe + // default. + IdentityPoolCredentials secondUniverseCredentials = + firstUniverseCredentials.toBuilder().setUniverseDomain("other-universe.org").build(); + + assertEquals("other-universe.org", secondUniverseCredentials.getUniverseDomain()); + assertEquals( + "https://sts.other-universe.org/v1/token", secondUniverseCredentials.getTokenUrl()); + } + + @Test + void toBuilder_setUniverseDomain_updatesDefaultTokenUrlForAws() { + Map awsSourceMap = new HashMap<>(); + awsSourceMap.put("environment_id", "aws1"); + awsSourceMap.put( + "regional_cred_verification_url", + "https://sts.{region}.amazonaws.com?Action=GetCallerIdentity&Version=2011-06-15"); + AwsCredentials awsCredentials = + AwsCredentials.newBuilder() + .setHttpTransportFactory(transportFactory) + .setAudience("audience") + .setSubjectTokenType("subjectTokenType") + .setCredentialSource(new AwsCredentialSource(awsSourceMap)) + .build(); + AwsCredentials rebuiltAws = + awsCredentials.toBuilder().setUniverseDomain("testdomain.org").build(); + assertEquals("https://sts.testdomain.org/v1/token", rebuiltAws.getTokenUrl()); + } + + @Test + void toBuilder_setUniverseDomain_updatesDefaultTokenUrlForPluggableAuth() { + Map executableMap = new HashMap<>(); + executableMap.put("command", "/bin/echo token"); + Map pluggableSourceMap = new HashMap<>(); + pluggableSourceMap.put("executable", executableMap); + PluggableAuthCredentials pluggableCredentials = + PluggableAuthCredentials.newBuilder() + .setHttpTransportFactory(transportFactory) + .setAudience("audience") + .setSubjectTokenType("subjectTokenType") + .setCredentialSource(new PluggableAuthCredentialSource(pluggableSourceMap)) + .build(); + PluggableAuthCredentials rebuiltPluggable = + pluggableCredentials.toBuilder().setUniverseDomain("testdomain.org").build(); + assertEquals("https://sts.testdomain.org/v1/token", rebuiltPluggable.getTokenUrl()); + } + + @Test + void toBuilder_fromGduJson_setUniverseDomain_updatesTokenUrl() throws IOException { + // Standard gcloud credential files include token_url=https://sts.googleapis.com/v1/token; + // switching universe domain via toBuilder() must still recompute it. + GenericJson gduJson = buildJsonIdentityPoolCredential(); + assertEquals(STS_URL, gduJson.get("token_url")); + + ExternalAccountCredentials gduCredentials = + ExternalAccountCredentials.fromJson(gduJson, transportFactory); + ExternalAccountCredentials customUniverseCredentials = + (ExternalAccountCredentials) + gduCredentials.toBuilder().setUniverseDomain("testdomain.org").build(); + + assertEquals("testdomain.org", customUniverseCredentials.getUniverseDomain()); + assertEquals("https://sts.testdomain.org/v1/token", customUniverseCredentials.getTokenUrl()); + } + + @Test + void toBuilder_setUniverseDomain_preservesInheritedCustomTokenUrl() { + HashMap credentialSource = new HashMap<>(); + credentialSource.put("file", "file"); + + // Inherited custom URLs (e.g. mTLS STS) must take precedence over universe domain changes. + IdentityPoolCredentials customUrlCredentials = + IdentityPoolCredentials.newBuilder() + .setHttpTransportFactory(transportFactory) + .setAudience( + "//iam.googleapis.com/locations/global/workforcePools/pool/providers/provider") + .setSubjectTokenType("subjectTokenType") + .setTokenUrl(STS_MTLS_URL) + .setCredentialSource(new TestCredentialSource(credentialSource)) + .build(); + + IdentityPoolCredentials rebuiltWithInheritedCustomUrl = + customUrlCredentials.toBuilder().setUniverseDomain("testdomain.org").build(); + assertEquals("testdomain.org", rebuiltWithInheritedCustomUrl.getUniverseDomain()); + assertEquals(STS_MTLS_URL, rebuiltWithInheritedCustomUrl.getTokenUrl()); + } + + @Test + void toBuilder_setTokenUrlBeforeSetUniverseDomain_preservesCustomTokenUrl() { + HashMap credentialSource = new HashMap<>(); + credentialSource.put("file", "file"); + + IdentityPoolCredentials gduCredentials = + IdentityPoolCredentials.newBuilder() + .setHttpTransportFactory(transportFactory) + .setAudience( + "//iam.googleapis.com/locations/global/workforcePools/pool/providers/provider") + .setSubjectTokenType("subjectTokenType") + .setCredentialSource(new TestCredentialSource(credentialSource)) + .build(); + + String customUrl = "https://sts.custom.example.com/v1/token"; + IdentityPoolCredentials rebuilt = + gduCredentials.toBuilder() + .setTokenUrl(customUrl) + .setUniverseDomain("testdomain.org") + .build(); + assertEquals("testdomain.org", rebuilt.getUniverseDomain()); + assertEquals(customUrl, rebuilt.getTokenUrl()); + } + + @Test + void toBuilder_setTokenUrlAfterSetUniverseDomain_preservesCustomTokenUrl() { + HashMap credentialSource = new HashMap<>(); + credentialSource.put("file", "file"); + + IdentityPoolCredentials gduCredentials = + IdentityPoolCredentials.newBuilder() + .setHttpTransportFactory(transportFactory) + .setAudience( + "//iam.googleapis.com/locations/global/workforcePools/pool/providers/provider") + .setSubjectTokenType("subjectTokenType") + .setCredentialSource(new TestCredentialSource(credentialSource)) + .build(); + + String customUrl = "https://sts.custom.example.com/v1/token"; + IdentityPoolCredentials rebuilt = + gduCredentials.toBuilder() + .setUniverseDomain("testdomain.org") + .setTokenUrl(customUrl) + .build(); + assertEquals("testdomain.org", rebuilt.getUniverseDomain()); + assertEquals(customUrl, rebuilt.getTokenUrl()); + } + + @Test + void toBuilder_setUniverseDomain_exchangesTokenAgainstUpdatedStsUrl() throws IOException { + // End-to-end check that STS requests are actually sent to the recomputed universe URL. + List requestedUrls = new ArrayList<>(); + HttpTransportFactory recordingTransportFactory = + () -> + new MockHttpTransport() { + @Override + public LowLevelHttpRequest buildRequest(String method, String url) { + requestedUrls.add(url); + return new MockLowLevelHttpRequest(url) { + @Override + public LowLevelHttpResponse execute() { + return new MockLowLevelHttpResponse() + .setContentType(Json.MEDIA_TYPE) + .setContent( + "{\"access_token\":\"customUniverseAccessToken\"," + + "\"issued_token_type\":" + + "\"urn:ietf:params:oauth:token-type:access_token\"," + + "\"token_type\":\"Bearer\",\"expires_in\":3600}"); + } + }; + } + }; + + TestExternalAccountCredentials gduCredentials = + (TestExternalAccountCredentials) + TestExternalAccountCredentials.newBuilder() + .setHttpTransportFactory(recordingTransportFactory) + .setAudience("audience") + .setSubjectTokenType("subjectTokenType") + .setCredentialSource(new TestCredentialSource(FILE_CREDENTIAL_SOURCE_MAP)) + .build(); + + TestExternalAccountCredentials customUniverseCredentials = + (TestExternalAccountCredentials) + gduCredentials.toBuilder().setUniverseDomain("testdomain.org").build(); + + StsTokenExchangeRequest stsRequest = + StsTokenExchangeRequest.newBuilder("subjectToken", "subjectTokenType").build(); + AccessToken accessToken = + customUniverseCredentials.exchangeExternalCredentialForAccessToken(stsRequest); + + assertEquals("customUniverseAccessToken", accessToken.getTokenValue()); + assertEquals(Arrays.asList("https://sts.testdomain.org/v1/token"), requestedUrls); + } + @Test void constructor_builder_subjectTokenTypeEnum() { HashMap credentialSource = new HashMap<>(); From 630b940ddb1c40d1b319f1cbf14c96d835a253fa Mon Sep 17 00:00:00 2001 From: Lawrence Qiu Date: Tue, 1 Dec 2026 21:26:23 +0000 Subject: [PATCH 2/5] test(auth): cover non-default universe plain toBuilder().build() and tidy comment --- .../oauth2/ExternalAccountCredentials.java | 6 ++--- .../ExternalAccountCredentialsTest.java | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java b/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java index 8635d913911f..9e88935b5a26 100644 --- a/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java +++ b/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java @@ -835,9 +835,9 @@ protected Builder(ExternalAccountCredentials credentials) { // Built credentials always have tokenUrl populated, either with a custom override or with the // default STS URL for credentials.getUniverseDomain() (from builder defaults or gcloud JSON). // Clear tokenUrl to null when it equals that universe default so the configured - // universeDomain - // on toBuilder() can recompute it. Any non-default tokenUrl (e.g. mTLS STS or proxy) stays - // non-null and keeps priority over universeDomain, as do explicit setTokenUrl() calls. + // universeDomain on toBuilder() can recompute it. Any non-default tokenUrl (e.g. mTLS STS or + // proxy) stays non-null and keeps priority over universeDomain, as do explicit setTokenUrl() + // calls. this.tokenUrl = defaultTokenUrlForUniverse(credentials.getUniverseDomain()).equals(credentials.tokenUrl) ? null diff --git a/google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java b/google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java index 787870d57307..8033182b3e11 100644 --- a/google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java +++ b/google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java @@ -693,6 +693,31 @@ void toBuilder_setUniverseDomain_chained_updatesDefaultTokenUrl() { "https://sts.other-universe.org/v1/token", secondUniverseCredentials.getTokenUrl()); } + @Test + void toBuilder_nonDefaultUniverse_withoutSetUniverseDomain_preservesDefaultTokenUrl() { + HashMap credentialSource = new HashMap<>(); + credentialSource.put("file", "file"); + + IdentityPoolCredentials customUniverseCredentials = + IdentityPoolCredentials.newBuilder() + .setHttpTransportFactory(transportFactory) + .setAudience( + "//iam.googleapis.com/locations/global/workforcePools/pool/providers/provider") + .setSubjectTokenType("subjectTokenType") + .setCredentialSource(new TestCredentialSource(credentialSource)) + .setUniverseDomain("testdomain.org") + .build(); + + assertEquals("https://sts.testdomain.org/v1/token", customUniverseCredentials.getTokenUrl()); + + // Plain toBuilder().build() clears tokenUrl to null when it matches the universe default and + // must reconstruct the same non-default STS URL when universeDomain is unchanged. + IdentityPoolCredentials rebuilt = customUniverseCredentials.toBuilder().build(); + + assertEquals("testdomain.org", rebuilt.getUniverseDomain()); + assertEquals("https://sts.testdomain.org/v1/token", rebuilt.getTokenUrl()); + } + @Test void toBuilder_setUniverseDomain_updatesDefaultTokenUrlForAws() { Map awsSourceMap = new HashMap<>(); From 1ee7b4bec52d1aed16d01101e319708ba8bcdd2e Mon Sep 17 00:00:00 2001 From: Lawrence Qiu Date: Tue, 1 Dec 2026 22:07:54 +0000 Subject: [PATCH 3/5] test(auth): simplify ExternalAccountCredentials toBuilder tests with shared helpers --- .../ExternalAccountCredentialsTest.java | 205 +++++------------- 1 file changed, 56 insertions(+), 149 deletions(-) diff --git a/google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java b/google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java index 8033182b3e11..5dc1c505d6cc 100644 --- a/google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java +++ b/google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java @@ -641,24 +641,13 @@ void constructor_builder_defaultTokenUrlwithUniverseDomain() { assertEquals("https://sts.testdomain.org/v1/token", credentials.getTokenUrl()); } + // Covers IdentityPoolCredentials, AwsCredentials, and PluggableAuthCredentials because all + // subclasses inherit ExternalAccountCredentials.Builder(ExternalAccountCredentials) without + // overriding tokenUrl or universeDomain handling. @Test void toBuilder_setUniverseDomain_updatesDefaultTokenUrl() { - HashMap credentialSource = new HashMap<>(); - credentialSource.put("file", "file"); - - IdentityPoolCredentials gduCredentials = - IdentityPoolCredentials.newBuilder() - .setHttpTransportFactory(transportFactory) - .setAudience( - "//iam.googleapis.com/locations/global/workforcePools/pool/providers/provider") - .setSubjectTokenType("subjectTokenType") - .setCredentialSource(new TestCredentialSource(credentialSource)) - .build(); - - assertEquals(STS_URL, gduCredentials.getTokenUrl()); - assertEquals(GOOGLE_DEFAULT_UNIVERSE, gduCredentials.getUniverseDomain()); + IdentityPoolCredentials gduCredentials = newIdentityPoolBuilder().build(); - // Retargeting a GDU credential must replace its defaulted sts.googleapis.com URL. IdentityPoolCredentials customUniverseCredentials = gduCredentials.toBuilder().setUniverseDomain("testdomain.org").build(); @@ -668,23 +657,9 @@ void toBuilder_setUniverseDomain_updatesDefaultTokenUrl() { @Test void toBuilder_setUniverseDomain_chained_updatesDefaultTokenUrl() { - HashMap credentialSource = new HashMap<>(); - credentialSource.put("file", "file"); - IdentityPoolCredentials firstUniverseCredentials = - IdentityPoolCredentials.newBuilder() - .setHttpTransportFactory(transportFactory) - .setAudience( - "//iam.googleapis.com/locations/global/workforcePools/pool/providers/provider") - .setSubjectTokenType("subjectTokenType") - .setCredentialSource(new TestCredentialSource(credentialSource)) - .setUniverseDomain("testdomain.org") - .build(); - - assertEquals("https://sts.testdomain.org/v1/token", firstUniverseCredentials.getTokenUrl()); + newIdentityPoolBuilder().setUniverseDomain("testdomain.org").build(); - // Chained retargeting must also recompute because the credential still uses its universe - // default. IdentityPoolCredentials secondUniverseCredentials = firstUniverseCredentials.toBuilder().setUniverseDomain("other-universe.org").build(); @@ -695,75 +670,27 @@ void toBuilder_setUniverseDomain_chained_updatesDefaultTokenUrl() { @Test void toBuilder_nonDefaultUniverse_withoutSetUniverseDomain_preservesDefaultTokenUrl() { - HashMap credentialSource = new HashMap<>(); - credentialSource.put("file", "file"); - - IdentityPoolCredentials customUniverseCredentials = - IdentityPoolCredentials.newBuilder() - .setHttpTransportFactory(transportFactory) - .setAudience( - "//iam.googleapis.com/locations/global/workforcePools/pool/providers/provider") - .setSubjectTokenType("subjectTokenType") - .setCredentialSource(new TestCredentialSource(credentialSource)) - .setUniverseDomain("testdomain.org") - .build(); - - assertEquals("https://sts.testdomain.org/v1/token", customUniverseCredentials.getTokenUrl()); + GenericJson nonDefaultJson = buildJsonIdentityPoolCredential(); + nonDefaultJson.put("universe_domain", "testdomain.org"); + nonDefaultJson.put("token_url", "https://sts.testdomain.org/v1/token"); + ExternalAccountCredentials credentials = + ExternalAccountCredentials.fromJson(nonDefaultJson, transportFactory); - // Plain toBuilder().build() clears tokenUrl to null when it matches the universe default and - // must reconstruct the same non-default STS URL when universeDomain is unchanged. - IdentityPoolCredentials rebuilt = customUniverseCredentials.toBuilder().build(); + ExternalAccountCredentials rebuilt = + (ExternalAccountCredentials) credentials.toBuilder().build(); assertEquals("testdomain.org", rebuilt.getUniverseDomain()); assertEquals("https://sts.testdomain.org/v1/token", rebuilt.getTokenUrl()); } @Test - void toBuilder_setUniverseDomain_updatesDefaultTokenUrlForAws() { - Map awsSourceMap = new HashMap<>(); - awsSourceMap.put("environment_id", "aws1"); - awsSourceMap.put( - "regional_cred_verification_url", - "https://sts.{region}.amazonaws.com?Action=GetCallerIdentity&Version=2011-06-15"); - AwsCredentials awsCredentials = - AwsCredentials.newBuilder() - .setHttpTransportFactory(transportFactory) - .setAudience("audience") - .setSubjectTokenType("subjectTokenType") - .setCredentialSource(new AwsCredentialSource(awsSourceMap)) - .build(); - AwsCredentials rebuiltAws = - awsCredentials.toBuilder().setUniverseDomain("testdomain.org").build(); - assertEquals("https://sts.testdomain.org/v1/token", rebuiltAws.getTokenUrl()); - } - - @Test - void toBuilder_setUniverseDomain_updatesDefaultTokenUrlForPluggableAuth() { - Map executableMap = new HashMap<>(); - executableMap.put("command", "/bin/echo token"); - Map pluggableSourceMap = new HashMap<>(); - pluggableSourceMap.put("executable", executableMap); - PluggableAuthCredentials pluggableCredentials = - PluggableAuthCredentials.newBuilder() - .setHttpTransportFactory(transportFactory) - .setAudience("audience") - .setSubjectTokenType("subjectTokenType") - .setCredentialSource(new PluggableAuthCredentialSource(pluggableSourceMap)) - .build(); - PluggableAuthCredentials rebuiltPluggable = - pluggableCredentials.toBuilder().setUniverseDomain("testdomain.org").build(); - assertEquals("https://sts.testdomain.org/v1/token", rebuiltPluggable.getTokenUrl()); - } - - @Test - void toBuilder_fromGduJson_setUniverseDomain_updatesTokenUrl() throws IOException { + void toBuilder_fromGduJson_setUniverseDomain_updatesTokenUrl() { // Standard gcloud credential files include token_url=https://sts.googleapis.com/v1/token; // switching universe domain via toBuilder() must still recompute it. GenericJson gduJson = buildJsonIdentityPoolCredential(); - assertEquals(STS_URL, gduJson.get("token_url")); - ExternalAccountCredentials gduCredentials = ExternalAccountCredentials.fromJson(gduJson, transportFactory); + ExternalAccountCredentials customUniverseCredentials = (ExternalAccountCredentials) gduCredentials.toBuilder().setUniverseDomain("testdomain.org").build(); @@ -774,103 +701,53 @@ void toBuilder_fromGduJson_setUniverseDomain_updatesTokenUrl() throws IOExceptio @Test void toBuilder_setUniverseDomain_preservesInheritedCustomTokenUrl() { - HashMap credentialSource = new HashMap<>(); - credentialSource.put("file", "file"); - - // Inherited custom URLs (e.g. mTLS STS) must take precedence over universe domain changes. IdentityPoolCredentials customUrlCredentials = - IdentityPoolCredentials.newBuilder() - .setHttpTransportFactory(transportFactory) - .setAudience( - "//iam.googleapis.com/locations/global/workforcePools/pool/providers/provider") - .setSubjectTokenType("subjectTokenType") - .setTokenUrl(STS_MTLS_URL) - .setCredentialSource(new TestCredentialSource(credentialSource)) - .build(); + newIdentityPoolBuilder().setTokenUrl(STS_MTLS_URL).build(); - IdentityPoolCredentials rebuiltWithInheritedCustomUrl = + IdentityPoolCredentials rebuilt = customUrlCredentials.toBuilder().setUniverseDomain("testdomain.org").build(); - assertEquals("testdomain.org", rebuiltWithInheritedCustomUrl.getUniverseDomain()); - assertEquals(STS_MTLS_URL, rebuiltWithInheritedCustomUrl.getTokenUrl()); + + assertEquals("testdomain.org", rebuilt.getUniverseDomain()); + assertEquals(STS_MTLS_URL, rebuilt.getTokenUrl()); } @Test void toBuilder_setTokenUrlBeforeSetUniverseDomain_preservesCustomTokenUrl() { - HashMap credentialSource = new HashMap<>(); - credentialSource.put("file", "file"); - - IdentityPoolCredentials gduCredentials = - IdentityPoolCredentials.newBuilder() - .setHttpTransportFactory(transportFactory) - .setAudience( - "//iam.googleapis.com/locations/global/workforcePools/pool/providers/provider") - .setSubjectTokenType("subjectTokenType") - .setCredentialSource(new TestCredentialSource(credentialSource)) - .build(); - + IdentityPoolCredentials gduCredentials = newIdentityPoolBuilder().build(); String customUrl = "https://sts.custom.example.com/v1/token"; + IdentityPoolCredentials rebuilt = gduCredentials.toBuilder() .setTokenUrl(customUrl) .setUniverseDomain("testdomain.org") .build(); + assertEquals("testdomain.org", rebuilt.getUniverseDomain()); assertEquals(customUrl, rebuilt.getTokenUrl()); } @Test void toBuilder_setTokenUrlAfterSetUniverseDomain_preservesCustomTokenUrl() { - HashMap credentialSource = new HashMap<>(); - credentialSource.put("file", "file"); - - IdentityPoolCredentials gduCredentials = - IdentityPoolCredentials.newBuilder() - .setHttpTransportFactory(transportFactory) - .setAudience( - "//iam.googleapis.com/locations/global/workforcePools/pool/providers/provider") - .setSubjectTokenType("subjectTokenType") - .setCredentialSource(new TestCredentialSource(credentialSource)) - .build(); - + IdentityPoolCredentials gduCredentials = newIdentityPoolBuilder().build(); String customUrl = "https://sts.custom.example.com/v1/token"; + IdentityPoolCredentials rebuilt = gduCredentials.toBuilder() .setUniverseDomain("testdomain.org") .setTokenUrl(customUrl) .build(); + assertEquals("testdomain.org", rebuilt.getUniverseDomain()); assertEquals(customUrl, rebuilt.getTokenUrl()); } @Test void toBuilder_setUniverseDomain_exchangesTokenAgainstUpdatedStsUrl() throws IOException { - // End-to-end check that STS requests are actually sent to the recomputed universe URL. List requestedUrls = new ArrayList<>(); - HttpTransportFactory recordingTransportFactory = - () -> - new MockHttpTransport() { - @Override - public LowLevelHttpRequest buildRequest(String method, String url) { - requestedUrls.add(url); - return new MockLowLevelHttpRequest(url) { - @Override - public LowLevelHttpResponse execute() { - return new MockLowLevelHttpResponse() - .setContentType(Json.MEDIA_TYPE) - .setContent( - "{\"access_token\":\"customUniverseAccessToken\"," - + "\"issued_token_type\":" - + "\"urn:ietf:params:oauth:token-type:access_token\"," - + "\"token_type\":\"Bearer\",\"expires_in\":3600}"); - } - }; - } - }; - TestExternalAccountCredentials gduCredentials = (TestExternalAccountCredentials) TestExternalAccountCredentials.newBuilder() - .setHttpTransportFactory(recordingTransportFactory) + .setHttpTransportFactory(recordingStsTransportFactory(requestedUrls)) .setAudience("audience") .setSubjectTokenType("subjectTokenType") .setCredentialSource(new TestCredentialSource(FILE_CREDENTIAL_SOURCE_MAP)) @@ -1568,6 +1445,36 @@ void validateServiceAccountImpersonationUrls_invalidUrls() { ((ExternalAccountCredentials) rebuilt.getSourceCredentials()).getServiceAccountEmail()); } + private IdentityPoolCredentials.Builder newIdentityPoolBuilder() { + return IdentityPoolCredentials.newBuilder() + .setHttpTransportFactory(transportFactory) + .setAudience("//iam.googleapis.com/locations/global/workforcePools/pool/providers/provider") + .setSubjectTokenType("subjectTokenType") + .setCredentialSource(new TestCredentialSource(FILE_CREDENTIAL_SOURCE_MAP)); + } + + private static HttpTransportFactory recordingStsTransportFactory(List requestedUrls) { + return () -> + new MockHttpTransport() { + @Override + public LowLevelHttpRequest buildRequest(String method, String url) { + requestedUrls.add(url); + return new MockLowLevelHttpRequest(url) { + @Override + public LowLevelHttpResponse execute() { + return new MockLowLevelHttpResponse() + .setContentType(Json.MEDIA_TYPE) + .setContent( + "{\"access_token\":\"customUniverseAccessToken\"," + + "\"issued_token_type\":" + + "\"urn:ietf:params:oauth:token-type:access_token\"," + + "\"token_type\":\"Bearer\",\"expires_in\":3600}"); + } + }; + } + }; + } + private GenericJson buildJsonIdentityPoolCredential() { GenericJson json = new GenericJson(); json.put( From 4399e99a18af7c7b2c75e250d8a7eb3b90827140 Mon Sep 17 00:00:00 2001 From: Lawrence Qiu Date: Tue, 1 Dec 2026 22:14:30 +0000 Subject: [PATCH 4/5] docs(auth): note serviceAccountImpersonationUrl override handling and cover GDU plain toBuilder().build() --- .../google/auth/oauth2/ExternalAccountCredentials.java | 2 ++ .../auth/oauth2/ExternalAccountCredentialsTest.java | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java b/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java index 9e88935b5a26..6ff418dc87cd 100644 --- a/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java +++ b/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java @@ -843,6 +843,8 @@ protected Builder(ExternalAccountCredentials credentials) { ? null : credentials.tokenUrl; this.tokenInfoUrl = credentials.tokenInfoUrl; + // Intentionally preserved verbatim as an explicit full-URL override (consistent with + // ImpersonatedCredentials.iamEndpointOverride and sibling auth libraries). this.serviceAccountImpersonationUrl = credentials.serviceAccountImpersonationUrl; this.targetServiceAccountEmail = credentials.targetServiceAccountEmail; this.credentialSource = credentials.credentialSource; diff --git a/google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java b/google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java index 5dc1c505d6cc..5ece50903f69 100644 --- a/google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java +++ b/google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java @@ -668,6 +668,16 @@ void toBuilder_setUniverseDomain_chained_updatesDefaultTokenUrl() { "https://sts.other-universe.org/v1/token", secondUniverseCredentials.getTokenUrl()); } + @Test + void toBuilder_withoutSetUniverseDomain_preservesDefaultTokenUrl() { + IdentityPoolCredentials gduCredentials = newIdentityPoolBuilder().build(); + + IdentityPoolCredentials rebuilt = gduCredentials.toBuilder().build(); + + assertEquals(GOOGLE_DEFAULT_UNIVERSE, rebuilt.getUniverseDomain()); + assertEquals(STS_URL, rebuilt.getTokenUrl()); + } + @Test void toBuilder_nonDefaultUniverse_withoutSetUniverseDomain_preservesDefaultTokenUrl() { GenericJson nonDefaultJson = buildJsonIdentityPoolCredential(); From f91f2ea3c9378ca008d5c62e9c47db60c7cd07f0 Mon Sep 17 00:00:00 2001 From: Lawrence Qiu Date: Wed, 2 Dec 2026 20:40:13 +0000 Subject: [PATCH 5/5] fix(auth): also recompute mTLS STS tokenUrl when universeDomain changes via toBuilder --- .../oauth2/ExternalAccountCredentials.java | 53 ++++-- .../ExternalAccountCredentialsTest.java | 162 +++++++++++++++--- 2 files changed, 177 insertions(+), 38 deletions(-) diff --git a/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java b/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java index 6ff418dc87cd..3363a374f5c4 100644 --- a/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java +++ b/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java @@ -71,7 +71,8 @@ public abstract class ExternalAccountCredentials extends GoogleCredentials { static final String EXECUTABLE_SOURCE_KEY = "executable"; - static final String DEFAULT_TOKEN_URL = "https://sts.{UNIVERSE_DOMAIN}/v1/token"; + static final String DEFAULT_STS_TOKEN_URL_FORMAT = "https://sts.%s/v1/token"; + static final String DEFAULT_MTLS_STS_TOKEN_URL_FORMAT = "https://sts.mtls.%s/v1/token"; static final String PROGRAMMATIC_METRICS_HEADER_VALUE = "programmatic"; private final String transportFactoryClassName; @@ -245,7 +246,7 @@ protected ExternalAccountCredentials(ExternalAccountCredentials.Builder builder) // Precedence: an explicit or non-default custom tokenUrl always takes priority. Otherwise, when // builder.tokenUrl is null, compute the default STS URL from the final universe domain. if (builder.tokenUrl == null) { - this.tokenUrl = defaultTokenUrlForUniverse(this.getUniverseDomain()); + this.tokenUrl = String.format(builder.defaultTokenUrlFormat, this.getUniverseDomain()); } else { this.tokenUrl = builder.tokenUrl; } @@ -792,9 +793,24 @@ int getLifetime() { } } - /** Returns the standard STS token exchange URL for {@code universeDomain}. */ - private static String defaultTokenUrlForUniverse(String universeDomain) { - return DEFAULT_TOKEN_URL.replace("{UNIVERSE_DOMAIN}", universeDomain); + /** + * Checks whether {@code tokenUrl} matches the standard or mTLS default STS URL for {@code + * universeDomain} and returns its format string, or {@code null} if it is a custom override. + * + *

Built credentials only store the resolved {@code tokenUrl}, so {@code + * Builder(ExternalAccountCredentials)} uses this check to decide whether {@code + * toBuilder().setUniverseDomain(...)} should recompute the default (preserving standard vs. mTLS) + * or leave a custom URL pinned. + */ + private static @Nullable String matchingDefaultTokenUrlFormat( + String tokenUrl, String universeDomain) { + if (String.format(DEFAULT_STS_TOKEN_URL_FORMAT, universeDomain).equals(tokenUrl)) { + return DEFAULT_STS_TOKEN_URL_FORMAT; + } + if (String.format(DEFAULT_MTLS_STS_TOKEN_URL_FORMAT, universeDomain).equals(tokenUrl)) { + return DEFAULT_MTLS_STS_TOKEN_URL_FORMAT; + } + return null; } /** Base builder for external account credentials. */ @@ -803,6 +819,11 @@ public abstract static class Builder extends GoogleCredentials.Builder { protected @Nullable String audience; protected @Nullable String subjectTokenType; protected @Nullable String tokenUrl; + // Format used when tokenUrl is null at build() time. Fresh builders default to the standard + // (non-mTLS) STS endpoint. Builder(ExternalAccountCredentials) overrides this to + // DEFAULT_MTLS_STS_TOKEN_URL_FORMAT when copying a credential with a default mTLS STS URL so + // toBuilder() preserves mTLS across universe changes. + private String defaultTokenUrlFormat = DEFAULT_STS_TOKEN_URL_FORMAT; protected @Nullable String tokenInfoUrl; protected @Nullable CredentialSource credentialSource; protected @Nullable EnvironmentProvider environmentProvider; @@ -833,15 +854,19 @@ protected Builder(ExternalAccountCredentials credentials) { this.audience = credentials.audience; this.subjectTokenType = credentials.subjectTokenType; // Built credentials always have tokenUrl populated, either with a custom override or with the - // default STS URL for credentials.getUniverseDomain() (from builder defaults or gcloud JSON). - // Clear tokenUrl to null when it equals that universe default so the configured - // universeDomain on toBuilder() can recompute it. Any non-default tokenUrl (e.g. mTLS STS or - // proxy) stays non-null and keeps priority over universeDomain, as do explicit setTokenUrl() - // calls. - this.tokenUrl = - defaultTokenUrlForUniverse(credentials.getUniverseDomain()).equals(credentials.tokenUrl) - ? null - : credentials.tokenUrl; + // standard or mTLS default STS URL for credentials.getUniverseDomain() (from builder defaults + // or gcloud JSON). Clear tokenUrl to null and keep the matching format when it equals one of + // those universe defaults so toBuilder().setUniverseDomain(...) recomputes it. Any custom + // tokenUrl stays non-null and keeps priority over universeDomain, as do explicit + // setTokenUrl() calls. + String matchedFormat = + matchingDefaultTokenUrlFormat(credentials.tokenUrl, credentials.getUniverseDomain()); + if (matchedFormat != null) { + this.tokenUrl = null; + this.defaultTokenUrlFormat = matchedFormat; + } else { + this.tokenUrl = credentials.tokenUrl; + } this.tokenInfoUrl = credentials.tokenInfoUrl; // Intentionally preserved verbatim as an explicit full-URL override (consistent with // ImpersonatedCredentials.iamEndpointOverride and sibling auth libraries). diff --git a/google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java b/google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java index 5ece50903f69..d96871f96c5d 100644 --- a/google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java +++ b/google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java @@ -80,6 +80,7 @@ class ExternalAccountCredentialsTest extends BaseSerializationTest { private static final String STS_URL = "https://sts.googleapis.com/v1/token"; private static final String STS_MTLS_URL = "https://sts.mtls.googleapis.com/v1/token"; + private static final String CUSTOM_STS_URL = "https://sts.custom.example.com/v1/token"; private static final String GOOGLE_DEFAULT_UNIVERSE = "googleapis.com"; private static KeyStore createPopulatedKeyStore() { @@ -656,7 +657,7 @@ void toBuilder_setUniverseDomain_updatesDefaultTokenUrl() { } @Test - void toBuilder_setUniverseDomain_chained_updatesDefaultTokenUrl() { + void toBuilder_nonDefaultUniverse_setUniverseDomain_updatesDefaultTokenUrl() { IdentityPoolCredentials firstUniverseCredentials = newIdentityPoolBuilder().setUniverseDomain("testdomain.org").build(); @@ -709,70 +710,181 @@ void toBuilder_fromGduJson_setUniverseDomain_updatesTokenUrl() { assertEquals("https://sts.testdomain.org/v1/token", customUniverseCredentials.getTokenUrl()); } + @Test + void toBuilder_setUniverseDomain_updatesDefaultMtlsTokenUrl() { + IdentityPoolCredentials mtlsCredentials = + newIdentityPoolBuilder().setTokenUrl(STS_MTLS_URL).build(); + + IdentityPoolCredentials customUniverseCredentials = + mtlsCredentials.toBuilder().setUniverseDomain("testdomain.org").build(); + + assertEquals("testdomain.org", customUniverseCredentials.getUniverseDomain()); + assertEquals( + "https://sts.mtls.testdomain.org/v1/token", customUniverseCredentials.getTokenUrl()); + } + + @Test + void toBuilder_nonDefaultUniverseMtls_setUniverseDomain_updatesDefaultMtlsTokenUrl() { + IdentityPoolCredentials firstUniverseCredentials = + newIdentityPoolBuilder() + .setUniverseDomain("testdomain.org") + .setTokenUrl("https://sts.mtls.testdomain.org/v1/token") + .build(); + + IdentityPoolCredentials secondUniverseCredentials = + firstUniverseCredentials.toBuilder().setUniverseDomain("other-universe.org").build(); + + assertEquals("other-universe.org", secondUniverseCredentials.getUniverseDomain()); + assertEquals( + "https://sts.mtls.other-universe.org/v1/token", secondUniverseCredentials.getTokenUrl()); + } + + @Test + void toBuilder_mtls_withoutSetUniverseDomain_preservesDefaultMtlsTokenUrl() { + IdentityPoolCredentials mtlsCredentials = + newIdentityPoolBuilder().setTokenUrl(STS_MTLS_URL).build(); + + IdentityPoolCredentials rebuilt = mtlsCredentials.toBuilder().build(); + + assertEquals(GOOGLE_DEFAULT_UNIVERSE, rebuilt.getUniverseDomain()); + assertEquals(STS_MTLS_URL, rebuilt.getTokenUrl()); + } + + @Test + void toBuilder_nonDefaultUniverseMtls_withoutSetUniverseDomain_preservesDefaultMtlsTokenUrl() { + GenericJson nonDefaultJson = buildJsonIdentityPoolCredential(); + nonDefaultJson.put("universe_domain", "testdomain.org"); + nonDefaultJson.put("token_url", "https://sts.mtls.testdomain.org/v1/token"); + ExternalAccountCredentials credentials = + ExternalAccountCredentials.fromJson(nonDefaultJson, transportFactory); + + ExternalAccountCredentials rebuilt = + (ExternalAccountCredentials) credentials.toBuilder().build(); + + assertEquals("testdomain.org", rebuilt.getUniverseDomain()); + assertEquals("https://sts.mtls.testdomain.org/v1/token", rebuilt.getTokenUrl()); + } + + @Test + void toBuilder_mtls_setTokenUrl_overridesMtlsDefault() { + IdentityPoolCredentials mtlsCredentials = + newIdentityPoolBuilder().setTokenUrl(STS_MTLS_URL).build(); + + IdentityPoolCredentials rebuilt = + mtlsCredentials.toBuilder() + .setTokenUrl(CUSTOM_STS_URL) + .setUniverseDomain("testdomain.org") + .build(); + + assertEquals("testdomain.org", rebuilt.getUniverseDomain()); + assertEquals(CUSTOM_STS_URL, rebuilt.getTokenUrl()); + } + @Test void toBuilder_setUniverseDomain_preservesInheritedCustomTokenUrl() { IdentityPoolCredentials customUrlCredentials = - newIdentityPoolBuilder().setTokenUrl(STS_MTLS_URL).build(); + newIdentityPoolBuilder().setTokenUrl(CUSTOM_STS_URL).build(); IdentityPoolCredentials rebuilt = customUrlCredentials.toBuilder().setUniverseDomain("testdomain.org").build(); assertEquals("testdomain.org", rebuilt.getUniverseDomain()); - assertEquals(STS_MTLS_URL, rebuilt.getTokenUrl()); + assertEquals(CUSTOM_STS_URL, rebuilt.getTokenUrl()); + } + + @Test + void toBuilder_mismatchedUniverseAndDefaultTokenUrl_setUniverseDomain_preservesTokenUrl() { + IdentityPoolCredentials mismatchedCredentials = + newIdentityPoolBuilder().setUniverseDomain("testdomain.org").setTokenUrl(STS_URL).build(); + + IdentityPoolCredentials rebuilt = + mismatchedCredentials.toBuilder().setUniverseDomain("other-universe.org").build(); + + assertEquals("other-universe.org", rebuilt.getUniverseDomain()); + assertEquals(STS_URL, rebuilt.getTokenUrl()); } @Test void toBuilder_setTokenUrlBeforeSetUniverseDomain_preservesCustomTokenUrl() { IdentityPoolCredentials gduCredentials = newIdentityPoolBuilder().build(); - String customUrl = "https://sts.custom.example.com/v1/token"; IdentityPoolCredentials rebuilt = gduCredentials.toBuilder() - .setTokenUrl(customUrl) + .setTokenUrl(CUSTOM_STS_URL) .setUniverseDomain("testdomain.org") .build(); assertEquals("testdomain.org", rebuilt.getUniverseDomain()); - assertEquals(customUrl, rebuilt.getTokenUrl()); + assertEquals(CUSTOM_STS_URL, rebuilt.getTokenUrl()); } @Test void toBuilder_setTokenUrlAfterSetUniverseDomain_preservesCustomTokenUrl() { IdentityPoolCredentials gduCredentials = newIdentityPoolBuilder().build(); - String customUrl = "https://sts.custom.example.com/v1/token"; IdentityPoolCredentials rebuilt = gduCredentials.toBuilder() .setUniverseDomain("testdomain.org") - .setTokenUrl(customUrl) + .setTokenUrl(CUSTOM_STS_URL) .build(); assertEquals("testdomain.org", rebuilt.getUniverseDomain()); - assertEquals(customUrl, rebuilt.getTokenUrl()); + assertEquals(CUSTOM_STS_URL, rebuilt.getTokenUrl()); + } + + @Test + void toBuilder_setUniverseDomain_preservesServiceAccountImpersonationUrl() { + IdentityPoolCredentials gduCredentials = + newIdentityPoolBuilder() + .setServiceAccountImpersonationUrl(SERVICE_ACCOUNT_IMPERSONATION_URL) + .build(); + + IdentityPoolCredentials customUniverseCredentials = + gduCredentials.toBuilder().setUniverseDomain("testdomain.org").build(); + + assertEquals( + SERVICE_ACCOUNT_IMPERSONATION_URL, + customUniverseCredentials.getServiceAccountImpersonationUrl()); + } + + @Test + void toBuilder_setUniverseDomain_withImpersonation_updatesSourceTokenUrl() { + IdentityPoolCredentials gduCredentials = + newIdentityPoolBuilder() + .setServiceAccountImpersonationUrl(SERVICE_ACCOUNT_IMPERSONATION_URL) + .build(); + assertNotNull(gduCredentials.getImpersonatedCredentials()); + + IdentityPoolCredentials customUniverseCredentials = + gduCredentials.toBuilder().setUniverseDomain("testdomain.org").build(); + + ImpersonatedCredentials impersonated = customUniverseCredentials.getImpersonatedCredentials(); + assertNotNull(impersonated); + ExternalAccountCredentials sourceCredentials = + (ExternalAccountCredentials) impersonated.getSourceCredentials(); + assertEquals("testdomain.org", sourceCredentials.getUniverseDomain()); + assertEquals("https://sts.testdomain.org/v1/token", sourceCredentials.getTokenUrl()); } @Test void toBuilder_setUniverseDomain_exchangesTokenAgainstUpdatedStsUrl() throws IOException { List requestedUrls = new ArrayList<>(); - TestExternalAccountCredentials gduCredentials = - (TestExternalAccountCredentials) - TestExternalAccountCredentials.newBuilder() - .setHttpTransportFactory(recordingStsTransportFactory(requestedUrls)) - .setAudience("audience") - .setSubjectTokenType("subjectTokenType") - .setCredentialSource(new TestCredentialSource(FILE_CREDENTIAL_SOURCE_MAP)) - .build(); + String expectedAccessToken = "customUniverseAccessToken"; + IdentityPoolCredentials gduCredentials = + newIdentityPoolBuilder() + .setHttpTransportFactory( + recordingStsTransportFactory(requestedUrls, expectedAccessToken)) + .build(); - TestExternalAccountCredentials customUniverseCredentials = - (TestExternalAccountCredentials) - gduCredentials.toBuilder().setUniverseDomain("testdomain.org").build(); + IdentityPoolCredentials customUniverseCredentials = + gduCredentials.toBuilder().setUniverseDomain("testdomain.org").build(); StsTokenExchangeRequest stsRequest = StsTokenExchangeRequest.newBuilder("subjectToken", "subjectTokenType").build(); AccessToken accessToken = customUniverseCredentials.exchangeExternalCredentialForAccessToken(stsRequest); - assertEquals("customUniverseAccessToken", accessToken.getTokenValue()); + assertEquals(expectedAccessToken, accessToken.getTokenValue()); assertEquals(Arrays.asList("https://sts.testdomain.org/v1/token"), requestedUrls); } @@ -1463,7 +1575,8 @@ private IdentityPoolCredentials.Builder newIdentityPoolBuilder() { .setCredentialSource(new TestCredentialSource(FILE_CREDENTIAL_SOURCE_MAP)); } - private static HttpTransportFactory recordingStsTransportFactory(List requestedUrls) { + private static HttpTransportFactory recordingStsTransportFactory( + List requestedUrls, String accessToken) { return () -> new MockHttpTransport() { @Override @@ -1475,8 +1588,9 @@ public LowLevelHttpResponse execute() { return new MockLowLevelHttpResponse() .setContentType(Json.MEDIA_TYPE) .setContent( - "{\"access_token\":\"customUniverseAccessToken\"," - + "\"issued_token_type\":" + "{\"access_token\":\"" + + accessToken + + "\",\"issued_token_type\":" + "\"urn:ietf:params:oauth:token-type:access_token\"," + "\"token_type\":\"Bearer\",\"expires_in\":3600}"); }