Skip to content

fix(auth): fix CAB refresh with non GDU universe domain - #15172

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

lqiu96 wants to merge 2 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:

  • 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 (https://cloud.google.com/iam/docs/create-downscoped-short-lived-credentials#before-you-begin).
  • Aligns ClientSideCredentialAccessBoundaryFactory.Builder whitespace-only universeDomain normalization and @Nullable annotation with DownscopedCredentials.Builder.
  • Consolidates Javadocs across ClientSideCredentialAccessBoundaryFactory, DownscopedCredentials, and OAuth2CredentialsWithRefresh for non-default universe usage, and adds CAB builder 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.

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.

2 participants