Skip to content

fix(auth): reject non-default universe refreshes for ServiceAccountCredentials and UserCredentials - #15245

Merged
lqiu96 merged 1 commit into
mainfrom
fix/auth-sa-user-universe-refresh
Nov 30, 2026
Merged

lqiu96 merged 1 commit into
mainfrom
fix/auth-sa-user-universe-refresh

Conversation

@lqiu96

@lqiu96 lqiu96 commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

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 fix(auth): fix CAB refresh with non GDU universe domain #15172.

@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 restricts OAuth2 access token refresh to the default Google universe domain for both ServiceAccountCredentials and UserCredentials, throwing an IOException if a non-default universe domain is used. It also adds corresponding unit tests to verify this behavior. The review feedback suggests trimming the universeDomain string in UserCredentials immediately after parsing to prevent unexpected exceptions due to leading or trailing whitespaces, simplifying the builder check accordingly, and adding a test case to verify whitespace handling.

Comment on lines +141 to +150
String universeDomain = (String) json.get("universe_domain");
if (universeDomain != null
&& !universeDomain.trim().isEmpty()
&& !Credentials.GOOGLE_DEFAULT_UNIVERSE.equals(universeDomain)) {
throw new IOException(
String.format(
"UserCredentials are not supported in universes other than %s (configured"
+ " universe_domain: %s).",
Credentials.GOOGLE_DEFAULT_UNIVERSE, 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.

medium

If universeDomain contains leading or trailing whitespace (e.g., " googleapis.com "), universeDomain.trim().isEmpty() will be false, but !Credentials.GOOGLE_DEFAULT_UNIVERSE.equals(universeDomain) will evaluate to true because the untrimmed string does not match "googleapis.com". This will cause an unexpected IOException to be thrown for what is conceptually the default universe domain.

Trimming universeDomain immediately after retrieving it from the JSON map resolves this issue and ensures consistent behavior.

    String universeDomain = (String) json.get("universe_domain");
    if (universeDomain != null) {
      universeDomain = universeDomain.trim();
    }
    if (universeDomain != null
        && !universeDomain.isEmpty()
        && !Credentials.GOOGLE_DEFAULT_UNIVERSE.equals(universeDomain)) {
      throw new IOException(
          String.format(
              "UserCredentials are not supported in universes other than %s (configured"
                  + " universe_domain: %s).",
              Credentials.GOOGLE_DEFAULT_UNIVERSE, universeDomain));
    }

Comment on lines +165 to +167
if (universeDomain != null && !universeDomain.trim().isEmpty()) {
builder.setUniverseDomain(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.

medium

Since universeDomain is trimmed early, we can simplify this check and avoid setting an untrimmed string to the builder.

Suggested change
if (universeDomain != null && !universeDomain.trim().isEmpty()) {
builder.setUniverseDomain(universeDomain);
}
if (universeDomain != null && !universeDomain.isEmpty()) {
builder.setUniverseDomain(universeDomain);
}


@Test
void fromJson_defaultOrBlankUniverse_succeeds() throws IOException {
String[] universeDomains = {null, "", " ", "googleapis.com"};

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.

medium

Before adding new test cases for input validation, please verify the existing test suite to ensure these scenarios are not already covered. If they are not covered, consider adding " googleapis.com " to the test cases to ensure that leading and trailing whitespaces are properly handled and normalized for the default universe domain.

Suggested change
String[] universeDomains = {null, "", " ", "googleapis.com"};
String[] universeDomains = {null, "", " ", "googleapis.com", " googleapis.com "};
References
  1. Before adding new test cases for input validation (such as non-numeric values or unrecognized properties), verify the existing test suite to ensure these scenarios are not already covered.

…edentials and UserCredentials

- Rejects ServiceAccountCredentials.refreshAccessToken() when getUniverseDomain() is not googleapis.com, and clarifies in Javadoc that refreshAccessToken() only handles OAuth2 token exchange (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 custom-universe DownscopedCredentials and ClientSideCredentialAccessBoundaryFactory tests to use a custom-universe GoogleCredentials stub instead of a non-default-universe ServiceAccountCredentials.
@lqiu96
lqiu96 force-pushed the fix/auth-sa-user-universe-refresh branch from eaab13a to 62f691b Compare November 25, 2026 20:19
@lqiu96 lqiu96 added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Nov 30, 2026
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Nov 30, 2026
@lqiu96
lqiu96 merged commit a46429d into main Nov 30, 2026
428 of 430 checks passed
@lqiu96
lqiu96 deleted the fix/auth-sa-user-universe-refresh branch November 30, 2026 17:07
@lqiu96
lqiu96 restored the fix/auth-sa-user-universe-refresh branch November 30, 2026 17:17
@lqiu96
lqiu96 deleted the fix/auth-sa-user-universe-refresh branch November 30, 2026 17:17
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