Skip to content

Commit 9d67db5

Browse files
committed
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.<universeDomain>/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.
1 parent 038e3b5 commit 9d67db5

2 files changed

Lines changed: 199 additions & 2 deletions

File tree

‎google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@ protected ExternalAccountCredentials(ExternalAccountCredentials.Builder builder)
243243
this.clientSecret = builder.clientSecret;
244244

245245
if (builder.tokenUrl == null) {
246-
this.tokenUrl = DEFAULT_TOKEN_URL.replace("{UNIVERSE_DOMAIN}", this.getUniverseDomain());
246+
// Recompute the default STS URL from the final universe domain when no custom URL is set.
247+
this.tokenUrl = defaultTokenUrlForUniverse(this.getUniverseDomain());
247248
} else {
248249
this.tokenUrl = builder.tokenUrl;
249250
}
@@ -790,6 +791,11 @@ int getLifetime() {
790791
}
791792
}
792793

794+
/** Returns the standard STS token exchange URL for {@code universeDomain}. */
795+
private static String defaultTokenUrlForUniverse(String universeDomain) {
796+
return DEFAULT_TOKEN_URL.replace("{UNIVERSE_DOMAIN}", universeDomain);
797+
}
798+
793799
/** Base builder for external account credentials. */
794800
public abstract static class Builder extends GoogleCredentials.Builder {
795801

@@ -825,7 +831,13 @@ protected Builder(ExternalAccountCredentials credentials) {
825831
this.transportFactory = credentials.transportFactory;
826832
this.audience = credentials.audience;
827833
this.subjectTokenType = credentials.subjectTokenType;
828-
this.tokenUrl = credentials.tokenUrl;
834+
// Clear tokenUrl when it matches the default STS URL for the credential's current universe
835+
// so build() recomputes it if toBuilder().setUniverseDomain(...) changes the universe.
836+
// Non-default URLs (e.g. mTLS STS or proxies) and explicit setTokenUrl() calls are preserved.
837+
this.tokenUrl =
838+
defaultTokenUrlForUniverse(credentials.getUniverseDomain()).equals(credentials.tokenUrl)
839+
? null
840+
: credentials.tokenUrl;
829841
this.tokenInfoUrl = credentials.tokenInfoUrl;
830842
this.serviceAccountImpersonationUrl = credentials.serviceAccountImpersonationUrl;
831843
this.targetServiceAccountEmail = credentials.targetServiceAccountEmail;

‎google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java‎

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,14 @@
4242
import static org.junit.jupiter.api.Assertions.assertTrue;
4343

4444
import com.google.api.client.http.HttpTransport;
45+
import com.google.api.client.http.LowLevelHttpRequest;
46+
import com.google.api.client.http.LowLevelHttpResponse;
4547
import com.google.api.client.json.GenericJson;
48+
import com.google.api.client.json.Json;
4649
import com.google.api.client.json.JsonParser;
50+
import com.google.api.client.testing.http.MockHttpTransport;
51+
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
52+
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
4753
import com.google.api.client.util.Clock;
4854
import com.google.api.client.util.SecurityUtils;
4955
import com.google.auth.TestUtils;
@@ -59,6 +65,7 @@
5965
import java.math.BigDecimal;
6066
import java.net.URI;
6167
import java.security.KeyStore;
68+
import java.util.ArrayList;
6269
import java.util.Arrays;
6370
import java.util.Date;
6471
import java.util.HashMap;
@@ -634,6 +641,184 @@ void constructor_builder_defaultTokenUrlwithUniverseDomain() {
634641
assertEquals("https://sts.testdomain.org/v1/token", credentials.getTokenUrl());
635642
}
636643

644+
@Test
645+
void toBuilder_setUniverseDomain_updatesDefaultTokenUrl() {
646+
HashMap<String, Object> credentialSource = new HashMap<>();
647+
credentialSource.put("file", "file");
648+
649+
IdentityPoolCredentials gduCredentials =
650+
IdentityPoolCredentials.newBuilder()
651+
.setHttpTransportFactory(transportFactory)
652+
.setAudience(
653+
"//iam.googleapis.com/locations/global/workforcePools/pool/providers/provider")
654+
.setSubjectTokenType("subjectTokenType")
655+
.setCredentialSource(new TestCredentialSource(credentialSource))
656+
.build();
657+
658+
assertEquals(STS_URL, gduCredentials.getTokenUrl());
659+
assertEquals(GOOGLE_DEFAULT_UNIVERSE, gduCredentials.getUniverseDomain());
660+
661+
// Retargeting a GDU credential must replace its defaulted sts.googleapis.com URL.
662+
IdentityPoolCredentials tpcCredentials =
663+
gduCredentials.toBuilder().setUniverseDomain("testdomain.org").build();
664+
665+
assertEquals("testdomain.org", tpcCredentials.getUniverseDomain());
666+
assertEquals("https://sts.testdomain.org/v1/token", tpcCredentials.getTokenUrl());
667+
668+
// Chained retargeting must also recompute because tpcCredentials still uses its universe
669+
// default.
670+
IdentityPoolCredentials secondTpcCredentials =
671+
tpcCredentials.toBuilder().setUniverseDomain("other-universe.org").build();
672+
673+
assertEquals("other-universe.org", secondTpcCredentials.getUniverseDomain());
674+
assertEquals("https://sts.other-universe.org/v1/token", secondTpcCredentials.getTokenUrl());
675+
}
676+
677+
@Test
678+
void toBuilder_setUniverseDomain_updatesDefaultTokenUrlForAwsAndPluggableAuth() {
679+
// Verify other ExternalAccountCredentials subclasses inherit the same toBuilder() tokenUrl
680+
// reset.
681+
Map<String, Object> awsSourceMap = new HashMap<>();
682+
awsSourceMap.put("environment_id", "aws1");
683+
awsSourceMap.put(
684+
"regional_cred_verification_url",
685+
"https://sts.{region}.amazonaws.com?Action=GetCallerIdentity&Version=2011-06-15");
686+
AwsCredentials awsCredentials =
687+
AwsCredentials.newBuilder()
688+
.setHttpTransportFactory(transportFactory)
689+
.setAudience("audience")
690+
.setSubjectTokenType("subjectTokenType")
691+
.setCredentialSource(new AwsCredentialSource(awsSourceMap))
692+
.build();
693+
AwsCredentials rebuiltAws =
694+
awsCredentials.toBuilder().setUniverseDomain("testdomain.org").build();
695+
assertEquals("https://sts.testdomain.org/v1/token", rebuiltAws.getTokenUrl());
696+
697+
Map<String, Object> executableMap = new HashMap<>();
698+
executableMap.put("command", "/bin/echo token");
699+
Map<String, Object> pluggableSourceMap = new HashMap<>();
700+
pluggableSourceMap.put("executable", executableMap);
701+
PluggableAuthCredentials pluggableCredentials =
702+
PluggableAuthCredentials.newBuilder()
703+
.setHttpTransportFactory(transportFactory)
704+
.setAudience("audience")
705+
.setSubjectTokenType("subjectTokenType")
706+
.setCredentialSource(new PluggableAuthCredentialSource(pluggableSourceMap))
707+
.build();
708+
PluggableAuthCredentials rebuiltPluggable =
709+
pluggableCredentials.toBuilder().setUniverseDomain("testdomain.org").build();
710+
assertEquals("https://sts.testdomain.org/v1/token", rebuiltPluggable.getTokenUrl());
711+
}
712+
713+
@Test
714+
void toBuilder_fromGduJson_setUniverseDomain_updatesTokenUrl() throws IOException {
715+
// Standard gcloud credential files include token_url=https://sts.googleapis.com/v1/token;
716+
// switching universe domain via toBuilder() must still recompute it.
717+
GenericJson gduJson = buildJsonIdentityPoolCredential();
718+
assertEquals(STS_URL, gduJson.get("token_url"));
719+
720+
ExternalAccountCredentials gduCredentials =
721+
ExternalAccountCredentials.fromJson(gduJson, transportFactory);
722+
ExternalAccountCredentials tpcCredentials =
723+
(ExternalAccountCredentials)
724+
gduCredentials.toBuilder().setUniverseDomain("testdomain.org").build();
725+
726+
assertEquals("testdomain.org", tpcCredentials.getUniverseDomain());
727+
assertEquals("https://sts.testdomain.org/v1/token", tpcCredentials.getTokenUrl());
728+
}
729+
730+
@Test
731+
void toBuilder_setUniverseDomain_preservesCustomTokenUrl() {
732+
HashMap<String, Object> credentialSource = new HashMap<>();
733+
credentialSource.put("file", "file");
734+
735+
// Inherited custom URLs (e.g. mTLS STS) must not be overwritten when changing universe domain.
736+
IdentityPoolCredentials customUrlCredentials =
737+
IdentityPoolCredentials.newBuilder()
738+
.setHttpTransportFactory(transportFactory)
739+
.setAudience(
740+
"//iam.googleapis.com/locations/global/workforcePools/pool/providers/provider")
741+
.setSubjectTokenType("subjectTokenType")
742+
.setTokenUrl(STS_MTLS_URL)
743+
.setCredentialSource(new TestCredentialSource(credentialSource))
744+
.build();
745+
746+
IdentityPoolCredentials rebuiltWithInheritedCustomUrl =
747+
customUrlCredentials.toBuilder().setUniverseDomain("testdomain.org").build();
748+
assertEquals("testdomain.org", rebuiltWithInheritedCustomUrl.getUniverseDomain());
749+
assertEquals(STS_MTLS_URL, rebuiltWithInheritedCustomUrl.getTokenUrl());
750+
751+
IdentityPoolCredentials gduCredentials =
752+
IdentityPoolCredentials.newBuilder()
753+
.setHttpTransportFactory(transportFactory)
754+
.setAudience(
755+
"//iam.googleapis.com/locations/global/workforcePools/pool/providers/provider")
756+
.setSubjectTokenType("subjectTokenType")
757+
.setCredentialSource(new TestCredentialSource(credentialSource))
758+
.build();
759+
760+
// Explicit setTokenUrl() on toBuilder() must take precedence in either setter order.
761+
String customUrl = "https://sts.custom.example.com/v1/token";
762+
IdentityPoolCredentials setTokenUrlBeforeUniverse =
763+
gduCredentials.toBuilder()
764+
.setTokenUrl(customUrl)
765+
.setUniverseDomain("testdomain.org")
766+
.build();
767+
assertEquals(customUrl, setTokenUrlBeforeUniverse.getTokenUrl());
768+
769+
IdentityPoolCredentials setTokenUrlAfterUniverse =
770+
gduCredentials.toBuilder()
771+
.setUniverseDomain("testdomain.org")
772+
.setTokenUrl(customUrl)
773+
.build();
774+
assertEquals(customUrl, setTokenUrlAfterUniverse.getTokenUrl());
775+
}
776+
777+
@Test
778+
void toBuilder_setUniverseDomain_exchangesTokenAgainstUpdatedStsUrl() throws IOException {
779+
// End-to-end check that STS requests are actually sent to the recomputed universe URL.
780+
List<String> requestedUrls = new ArrayList<>();
781+
HttpTransportFactory recordingTransportFactory =
782+
() ->
783+
new MockHttpTransport() {
784+
@Override
785+
public LowLevelHttpRequest buildRequest(String method, String url) {
786+
requestedUrls.add(url);
787+
return new MockLowLevelHttpRequest(url) {
788+
@Override
789+
public LowLevelHttpResponse execute() {
790+
return new MockLowLevelHttpResponse()
791+
.setContentType(Json.MEDIA_TYPE)
792+
.setContent(
793+
"{\"access_token\":\"tpcAccessToken\",\"issued_token_type\":"
794+
+ "\"urn:ietf:params:oauth:token-type:access_token\","
795+
+ "\"token_type\":\"Bearer\",\"expires_in\":3600}");
796+
}
797+
};
798+
}
799+
};
800+
801+
TestExternalAccountCredentials gduCredentials =
802+
(TestExternalAccountCredentials)
803+
TestExternalAccountCredentials.newBuilder()
804+
.setHttpTransportFactory(recordingTransportFactory)
805+
.setAudience("audience")
806+
.setSubjectTokenType("subjectTokenType")
807+
.setCredentialSource(new TestCredentialSource(FILE_CREDENTIAL_SOURCE_MAP))
808+
.build();
809+
810+
TestExternalAccountCredentials tpcCredentials =
811+
(TestExternalAccountCredentials)
812+
gduCredentials.toBuilder().setUniverseDomain("testdomain.org").build();
813+
814+
StsTokenExchangeRequest stsRequest =
815+
StsTokenExchangeRequest.newBuilder("subjectToken", "subjectTokenType").build();
816+
AccessToken accessToken = tpcCredentials.exchangeExternalCredentialForAccessToken(stsRequest);
817+
818+
assertEquals("tpcAccessToken", accessToken.getTokenValue());
819+
assertEquals(Arrays.asList("https://sts.testdomain.org/v1/token"), requestedUrls);
820+
}
821+
637822
@Test
638823
void constructor_builder_subjectTokenTypeEnum() {
639824
HashMap<String, Object> credentialSource = new HashMap<>();

0 commit comments

Comments
 (0)