Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -242,8 +243,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 = String.format(builder.defaultTokenUrlFormat, this.getUniverseDomain());
} else {
this.tokenUrl = builder.tokenUrl;
}
Expand Down Expand Up @@ -790,12 +793,37 @@ int getLifetime() {
}
}

/**
* 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.
*
* <p>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. */
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;
Expand Down Expand Up @@ -825,8 +853,23 @@ 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
// 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).
this.serviceAccountImpersonationUrl = credentials.serviceAccountImpersonationUrl;
this.targetServiceAccountEmail = credentials.targetServiceAccountEmail;
this.credentialSource = credentials.credentialSource;
Expand Down Expand Up @@ -892,7 +935,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
Expand Down
Loading
Loading