Repository navigation
fix(auth): reject non-default universe refreshes for ServiceAccountCredentials and UserCredentials - #15245
Conversation
There was a problem hiding this comment.
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.
| 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)); | ||
| } |
There was a problem hiding this comment.
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));
}| if (universeDomain != null && !universeDomain.trim().isEmpty()) { | ||
| builder.setUniverseDomain(universeDomain); | ||
| } |
There was a problem hiding this comment.
Since universeDomain is trimmed early, we can simplify this check and avoid setting an untrimmed string to the builder.
| 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"}; |
There was a problem hiding this comment.
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.
| String[] universeDomains = {null, "", " ", "googleapis.com"}; | |
| String[] universeDomains = {null, "", " ", "googleapis.com", " googleapis.com "}; |
References
- 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.
eaab13a to
62f691b
Compare
Updates OAuth2 token refresh and JSON loading for non-default universe domains:
ServiceAccountCredentials.refreshAccessToken()whengetUniverseDomain()is notgoogleapis.com, since OAuth2 token exchange is only supported ingoogleapis.com, and clarifies in Javadoc thatrefreshAccessToken()only applies to the OAuth2 access token flow (not self-signed JWT authentication).authorized_userJSON with a non-defaultuniverse_domaininUserCredentials.fromJson(), and rejectsUserCredentialsrefreshes whenevergetUniverseDomain()is notgoogleapis.com.ClientSideCredentialAccessBoundaryFactoryTest.fetchIntermediateCredentials_withCustomUniverseDomainandDownscopedCredentialsTest.refreshAccessToken_withCustomUniverseDomain) to use a custom-universeGoogleCredentialsstub. Those two tests previously constructed a non-default-universeServiceAccountCredentialsand calledrefreshAccessToken()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.