Skip to content

fix(auth): inherit universe domain and fail fast in CAB builders - #15172

Open
lqiu96 wants to merge 5 commits into
googleapis:mainfrom
lqiu96:fix/auth-cab-universe-refresh
Open

lqiu96 wants to merge 5 commits into
googleapis:mainfrom
lqiu96:fix/auth-cab-universe-refresh

Conversation

@lqiu96

@lqiu96 lqiu96 commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Updates Credential Access Boundary builders and Javadocs for non-default universe domains:

  • Inherits sourceCredential.getUniverseDomain() in ClientSideCredentialAccessBoundaryFactory.Builder.build() and DownscopedCredentials.Builder.build() when setUniverseDomain() is omitted, null, or blank, and validates equality when explicitly set.
  • Fails fast in both CAB builders when given a ServiceAccountCredentials or UserCredentials in a non-default universe domain, since Credential Access Boundaries require an OAuth 2.0 access token rather than a self-signed JWT (https://docs.cloud.google.com/iam/docs/create-downscoped-short-lived-credentials#before-you-begin) and UserCredentials are only supported in googleapis.com.
  • Annotates ClientSideCredentialAccessBoundaryFactory.Builder.universeDomain and setUniverseDomain as @Nullable, consolidates CAB and OAuth2CredentialsWithRefresh Javadocs, and adds CAB builder, inheritance, and fail-closed unit tests.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces changes to restrict and handle non-default universe domains across various credential types, specifically preventing the direct use of ServiceAccountCredentials and UserCredentials in non-default universes unless wrapped or configured with custom token URIs. The feedback focuses on improving the robustness of the universe domain checks across multiple files (such as UserCredentials, ServiceAccountCredentials, DownscopedCredentials, and ClientSideCredentialAccessBoundaryFactory) by handling potential null values, trimming whitespace, and performing case-insensitive comparisons instead of direct .equals() checks.

@lqiu96
lqiu96 marked this pull request as ready for review November 23, 2026 21:22
@lqiu96
lqiu96 requested review from a team as code owners November 23, 2026 21:22
@lqiu96
lqiu96 requested a review from nbayati November 23, 2026 21:22
&& isGoogleDefaultUniverseUri(tokenServerUri)) {
throw new IOException(
String.format(
"Cannot refresh UserCredentials against %s when universe domain is %s. Configure a"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fromJson() rejects any non-default universe_domain with "UserCredentials are not supported in universes other than googleapis.com", even when a custom token_uri is set. This refresh check disagrees: a programmatically built UserCredentials can still refresh in a non-default universe as long as tokenServerUri isn't a Google host, and the error message tells users to configure a non-default tokenServerUri.

Is allowing a custom tokenServerUri here intentional? If so, it'd help to clarify why it differs from fromJson() and reword the message so it doesn't read like a standard setup step. If not, should this reject non-default universes the same way fromJson() does?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to reject all non-default universe refreshes unconditionally, matching fromJson().

}

private static boolean isGoogleDefaultUniverseUri(@Nullable URI uri) {
if (uri == null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commit bfb41be9 notes that null URIs are blocked for non-default universe refreshes, but this returns false when uri is null or host is null/empty. Those skip the check in doRefreshAccessToken() and fail later with a NullPointerException in GenericUrl instead of throwing the IOException. Nothing goes over the network either way, but it'd be good to return IOException as intended and add a test for a hostless URI.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, removed isGoogleDefaultUniverseUri() and added hostless URI test cases.

* token, allowing for automatic token refreshes by providing a {@link
* OAuth2CredentialsWithRefresh.OAuth2RefreshHandler}.
*
* <p>When operating in a non-default universe domain, wrap the downscoped token with {@link

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sits right below the note recommending OAuth2CredentialsWithRefresh for automatic token refreshes, but GoogleCredentials.create(String, AccessToken) returns a credential that can't refresh (refreshAccessToken() throws IllegalStateException once the token nears expiry). Could we call out that this wrapper doesn't auto-refresh, and note how callers in a non-default universe should handle expiry?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated Javadoc to note that GoogleCredentials.create(String, AccessToken) does not auto-refresh and how to handle expiry.

* token, allowing for automatic token refreshes by providing a {@link
* OAuth2CredentialsWithRefresh.OAuth2RefreshHandler}.
*
* <p>When operating in a non-default universe domain, wrap the downscoped token with {@link

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the Javadoc in ClientSideCredentialAccessBoundaryFactory: GoogleCredentials.create(String, AccessToken) doesn't auto-refresh, so it'd help to note that here too.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@lqiu96
lqiu96 force-pushed the fix/auth-cab-universe-refresh branch 2 times, most recently from 3552edd to 2a590a2 Compare November 25, 2026 20:05
@lqiu96
lqiu96 changed the base branch from main to fix/auth-sa-user-universe-refresh November 25, 2026 20:07
@lqiu96
lqiu96 force-pushed the fix/auth-sa-user-universe-refresh branch from eaab13a to 62f691b Compare November 25, 2026 20:19
@lqiu96
lqiu96 force-pushed the fix/auth-cab-universe-refresh branch from 2a590a2 to 02e2985 Compare November 25, 2026 20:21
lqiu96 added a commit that referenced this pull request Nov 30, 2026
…edentials and UserCredentials (#15245)

Updates OAuth2 token refresh and JSON loading for non-default universe
domains:

- Rejects `ServiceAccountCredentials.refreshAccessToken()` when
`getUniverseDomain()` is not `googleapis.com`, since OAuth2 token
exchange is only supported in `googleapis.com`, and clarifies in Javadoc
that `refreshAccessToken()` only applies to the OAuth2 access token flow
(not self-signed JWT authentication).
- Rejects `authorized_user` JSON with a non-default `universe_domain` in
`UserCredentials.fromJson()`, and rejects `UserCredentials` refreshes
whenever `getUniverseDomain()` is not `googleapis.com`.
- Updates two pre-existing custom-universe CAB unit tests
(`ClientSideCredentialAccessBoundaryFactoryTest.fetchIntermediateCredentials_withCustomUniverseDomain`
and
`DownscopedCredentialsTest.refreshAccessToken_withCustomUniverseDomain`)
to use a custom-universe `GoogleCredentials` stub. Those two tests
previously constructed a non-default-universe
`ServiceAccountCredentials` and called `refreshAccessToken()` on it, so
updating them here keeps CI green. All CAB production changes and new
CAB tests live in the stacked follow-up PR #15172.
@lqiu96
lqiu96 deleted the branch googleapis:fix/auth-sa-user-universe-refresh November 30, 2026 17:07
@lqiu96 lqiu96 closed this Nov 30, 2026
…s in CAB builders and clarify Javadocs

- Fails fast in ClientSideCredentialAccessBoundaryFactory.Builder.build() and DownscopedCredentials.Builder.build() when given a ServiceAccountCredentials in a non-default universe domain, since Credential Access Boundaries require an OAuth 2.0 access token rather than a self-signed JWT.
- Aligns ClientSideCredentialAccessBoundaryFactory.Builder whitespace-only universeDomain normalization and @nullable annotation with DownscopedCredentials.Builder.
- Consolidates CAB and OAuth2CredentialsWithRefresh Javadocs for non-default universe usage and adds CAB builder and fail-closed unit tests.
@lqiu96 lqiu96 reopened this Nov 30, 2026
@lqiu96
lqiu96 changed the base branch from fix/auth-sa-user-universe-refresh to main November 30, 2026 17:17
@lqiu96
lqiu96 force-pushed the fix/auth-cab-universe-refresh branch from 02e2985 to 0b5fd21 Compare November 30, 2026 17:17
@lqiu96
lqiu96 requested review from a team as code owners November 30, 2026 17:17

@nbayati nbayati left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM on the CAB factory changes and Javadocs. Deferring to @lsirac for confirmation on the expected non-default universe behavior.

@lqiu96
lqiu96 requested review from lsirac and removed request for a team December 1, 2026 17:43
@lqiu96
lqiu96 removed request for a team December 1, 2026 17:43
* OAuth2CredentialsWithRefresh} and an {@link OAuth2CredentialsWithRefresh.OAuth2RefreshHandler}
* that obtains a fresh token from {@link #generateToken(CredentialAccessBoundary)} (typically via a
* token broker, or directly in-process by wrapping checked exceptions in {@link IOException}). When
* operating in a non-default universe domain, configure {@link

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mentions setting the universe domain on OAuth2CredentialsWithRefresh and GoogleCredentials#create, but leaves out that callers also have to call Builder#setUniverseDomain on this factory. Unlike ImpersonatedCredentials, leaving it unset defaults to googleapis.com and fails build() with a mismatch error.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

* Generates a downscoped access token given the {@link CredentialAccessBoundary}.
*
* <p>If no intermediate access token is cached, or if its remaining lifetime is within {@code
* refreshMargin} or {@code minimumTokenLifetime}, this method refreshes the source credential and

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reads like entering refreshMargin always blocks on STS and encrypts with the newly fetched session key. When remaining lifetime is between minimumTokenLifetime and refreshMargin, generateToken() starts an async background refresh and encrypts the returned token with the cached session key instead.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

*
* <p>In non-default universe domains, {@link ServiceAccountCredentials} cannot be used directly
* because Credential Access Boundaries require an OAuth 2.0 access token (see <a
* href="https://cloud.google.com/iam/docs/create-downscoped-short-lived-credentials#before-you-begin">Create

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: #15312 just moved these doc links to docs.cloud.google.com.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

* a downscoped short-lived credential</a>) and service account OAuth2 token refresh is only
* supported in {@link Credentials#GOOGLE_DEFAULT_UNIVERSE}. Wrap the service account in {@code
* ImpersonatedCredentials}, or use credentials such as {@code ComputeEngineCredentials} or
* {@code ExternalAccountCredentials}.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Direct workforce identity pools under ExternalAccountCredentials don't support Credential Access Boundaries unless service account impersonation is configured. Mentioning ExternalAccountCredentials without that caveat can mislead workforce pool callers.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

*/
@CanIgnoreReturnValue
public Builder setUniverseDomain(String universeDomain) {
public Builder setUniverseDomain(@Nullable String universeDomain) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this class is @NullMarked, adding @Nullable here writes a nullable parameter into the unannotated universeDomain field on Builder, which strict nullness checkers will flag.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

*
* <p>In non-default universe domains, {@link ServiceAccountCredentials} cannot be used directly
* because Credential Access Boundaries require an OAuth 2.0 access token (see <a
* href="https://cloud.google.com/iam/docs/create-downscoped-short-lived-credentials#before-you-begin">Create

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: #15312 just moved these doc links to docs.cloud.google.com.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

* a downscoped short-lived credential</a>) and service account OAuth2 token refresh is only
* supported in {@link Credentials#GOOGLE_DEFAULT_UNIVERSE}. Wrap the service account in {@link
* ImpersonatedCredentials}, or use credentials such as {@link ComputeEngineCredentials} or
* {@link ExternalAccountCredentials}.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Direct workforce identity pools under ExternalAccountCredentials don't support Credential Access Boundaries unless service account impersonation is configured. Mentioning ExternalAccountCredentials without that caveat can mislead workforce pool callers.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

* refreshing tokens for a custom universe domain should configure it explicitly via {@link
* Builder#setUniverseDomain(String)}. If lazy or delegated universe domain resolution is needed,
* subclass this class and override {@link #getUniverseDomain()}.
* Builder#setUniverseDomain(String)}.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was removing the sentence about subclassing and overriding getUniverseDomain() intentional? The getUniverseDomain() Javadoc below still says subclasses may override it for lazy or delegated resolution.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not intentional, restored.

intermediateCredentials.getIntermediateAccessToken().getTokenValue());
assertEquals(
String.format(TOKEN_EXCHANGE_URL_FORMAT, universeDomain),
mockStsTransportFactory.transport.getRequest().getUrl());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now MockStsTransport only checks that subject_token is non-null. Can we also assert that the STS request body passes "impersonated-access-token" from the impersonation call?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

assertEquals(stsTransportFactory.transport.getAccessToken(), accessToken.getTokenValue());
assertEquals(
String.format(TOKEN_EXCHANGE_URL_FORMAT, universeDomain),
stsTransportFactory.transport.getRequest().getUrl());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now MockStsTransport only checks that subject_token is non-null. Can we also assert that the STS request body passes "impersonated-access-token" from the impersonation call?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

- Inherit source credential universe domain when setUniverseDomain() is omitted, null, or blank in ClientSideCredentialAccessBoundaryFactory and DownscopedCredentials, validating explicit values against the source credential.
- Fail fast on non-default-universe UserCredentials in both CAB builders alongside ServiceAccountCredentials.
- Annotate ClientSideCredentialAccessBoundaryFactory.Builder.universeDomain as @nullable.
- Clarify ExternalAccountCredentials Workforce Identity Federation caveat, async vs. blocking refresh in generateToken(), build() @throws tags, and restore OAuth2CredentialsWithRefresh subclassing note.
- Update IAM doc links to docs.cloud.google.com and assert subject_token in impersonated CAB tests.
@lqiu96 lqiu96 changed the title fix(auth): fix CAB refresh with non GDU universe domain fix(auth): inherit universe domain and fail fast in CAB builders Dec 2, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants