Repository navigation
Conversation
There was a problem hiding this comment.
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.
| && isGoogleDefaultUniverseUri(tokenServerUri)) { | ||
| throw new IOException( | ||
| String.format( | ||
| "Cannot refresh UserCredentials against %s when universe domain is %s. Configure a" |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Updated to reject all non-default universe refreshes unconditionally, matching fromJson().
| } | ||
|
|
||
| private static boolean isGoogleDefaultUniverseUri(@Nullable URI uri) { | ||
| if (uri == null) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Same as the Javadoc in ClientSideCredentialAccessBoundaryFactory: GoogleCredentials.create(String, AccessToken) doesn't auto-refresh, so it'd help to note that here too.
3552edd to
2a590a2
Compare
eaab13a to
62f691b
Compare
2a590a2 to
02e2985
Compare
…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.
…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.
02e2985 to
0b5fd21
Compare
Updates Credential Access Boundary builders and Javadocs for non-default universe domains:
ClientSideCredentialAccessBoundaryFactory.Builder.build()andDownscopedCredentials.Builder.build()when given aServiceAccountCredentialsin 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).ClientSideCredentialAccessBoundaryFactory.Builderwhitespace-onlyuniverseDomainnormalization and@Nullableannotation withDownscopedCredentials.Builder.ClientSideCredentialAccessBoundaryFactory,DownscopedCredentials, andOAuth2CredentialsWithRefreshfor non-default universe usage, and adds CAB builder and fail-closed unit tests.