Repository navigation
fix(auth): inherit universe domain and fail fast in CAB builders #15172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
0b5fd21
dc4b988
d09692b
feda4a5
28a7abc
3eae595
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,9 +47,11 @@ | |
| import com.google.auth.oauth2.GoogleCredentials; | ||
| import com.google.auth.oauth2.OAuth2CredentialsWithRefresh; | ||
| import com.google.auth.oauth2.OAuth2Utils; | ||
| import com.google.auth.oauth2.ServiceAccountCredentials; | ||
| import com.google.auth.oauth2.StsRequestHandler; | ||
| import com.google.auth.oauth2.StsTokenExchangeRequest; | ||
| import com.google.auth.oauth2.StsTokenExchangeResponse; | ||
| import com.google.auth.oauth2.UserCredentials; | ||
| import com.google.common.annotations.VisibleForTesting; | ||
| import com.google.common.base.Strings; | ||
| import com.google.common.util.concurrent.AbstractFuture; | ||
|
|
@@ -97,11 +99,16 @@ | |
| * the upper bound of permissions that the credential can access. You must also provide a source | ||
| * credential which will be used to acquire the downscoped credential. | ||
| * | ||
| * <p>The factory can be configured with options such as the {@code refreshMargin} and {@code | ||
| * minimumTokenLifetime}. The {@code refreshMargin} controls how far in advance of the underlying | ||
| * credentials' expiry a refresh is attempted. The {@code minimumTokenLifetime} ensures that | ||
| * generated tokens have a minimum usable lifespan. See the {@link | ||
| * ClientSideCredentialAccessBoundaryFactory.Builder} class for more details on these options. | ||
| * <p>On the first call to {@link #generateToken(CredentialAccessBoundary)}, and whenever the cached | ||
| * intermediate access token approaches expiry, this factory refreshes the source credential and | ||
| * exchanges it with the Security Token Service (STS) for an intermediate access token and an access | ||
| * boundary session key. Each {@link #generateToken(CredentialAccessBoundary)} call then locally | ||
| * serializes and encrypts the {@link CredentialAccessBoundary} rules with that session key and | ||
| * appends them to the intermediate token, returning an {@link AccessToken} whose expiration matches | ||
| * the intermediate token. The {@code refreshMargin} and {@code minimumTokenLifetime} options | ||
| * control when the cached intermediate token is refreshed asynchronously or synchronously. See the | ||
| * {@link ClientSideCredentialAccessBoundaryFactory.Builder} class for more details on these | ||
| * options. | ||
| * | ||
| * <p>Usage: | ||
| * | ||
|
|
@@ -134,9 +141,17 @@ | |
| * System.out.printf("Blob %s retrieved.", blob.getBlobId()); | ||
| * </code></pre> | ||
| * | ||
| * Note that {@link OAuth2CredentialsWithRefresh} can instead be used to consume the downscoped | ||
| * token, allowing for automatic token refreshes by providing a {@link | ||
| * OAuth2CredentialsWithRefresh.OAuth2RefreshHandler}. | ||
| * <p>For automatic token refreshes, consume the downscoped token with {@link | ||
| * 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, this factory inherits the universe domain from the | ||
| * source credential by default (or validates it against {@link Builder#setUniverseDomain(String)} | ||
| * when explicitly set). On the consumer side, configure {@link | ||
| * OAuth2CredentialsWithRefresh.Builder#setUniverseDomain(String)} for automatic refreshes, or wrap | ||
| * a single token with {@link GoogleCredentials#create(String, AccessToken)} (which does not | ||
| * auto-refresh and throws {@link IllegalStateException} from {@code refreshAccessToken()}, so | ||
| * callers must obtain a new token and create a new credential before expiry). | ||
| */ | ||
| @NullMarked | ||
| public class ClientSideCredentialAccessBoundaryFactory { | ||
|
|
@@ -181,6 +196,16 @@ private ClientSideCredentialAccessBoundaryFactory(Builder builder) { | |
| /** | ||
| * 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 | ||
| * minimumTokenLifetime}, this method synchronously refreshes the source credential and exchanges | ||
| * it at the STS endpoint ({@code https://sts.<universeDomain>/v1/token}) for a new intermediate | ||
| * access token and session key before generating the token. If the remaining lifetime is between | ||
| * {@code minimumTokenLifetime} and {@code refreshMargin}, it starts an asynchronous background | ||
| * refresh and uses the currently cached intermediate token and session key for this call. It then | ||
| * locally serializes and encrypts {@code accessBoundary} with the active session key and appends | ||
| * it to the intermediate token. The returned {@link AccessToken} shares the expiration time of | ||
| * that intermediate token. | ||
| * | ||
| * @param accessBoundary The credential access boundary that defines the restrictions for the | ||
| * generated CAB token. | ||
| * @return The downscoped access token in an {@link AccessToken} object | ||
|
|
@@ -605,7 +630,7 @@ public static Builder newBuilder() { | |
| public static class Builder { | ||
| private GoogleCredentials sourceCredential; | ||
| private HttpTransportFactory transportFactory; | ||
| private String universeDomain; | ||
| private @Nullable String universeDomain; | ||
| private String tokenExchangeEndpoint; | ||
| private Duration minimumTokenLifetime; | ||
| private Duration refreshMargin; | ||
|
|
@@ -616,6 +641,18 @@ private Builder() {} | |
| /** | ||
| * Sets the required source credential. | ||
| * | ||
| * <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://docs.cloud.google.com/iam/docs/create-downscoped-short-lived-credentials#before-you-begin">Create | ||
| * a downscoped short-lived credential</a>) and service account OAuth2 token refresh is only | ||
| * supported in {@link Credentials#GOOGLE_DEFAULT_UNIVERSE}. {@link UserCredentials} are also | ||
| * only supported in {@link Credentials#GOOGLE_DEFAULT_UNIVERSE}. Wrap a service account in | ||
| * {@link com.google.auth.oauth2.ImpersonatedCredentials ImpersonatedCredentials}, or use | ||
| * credentials such as {@link com.google.auth.oauth2.ComputeEngineCredentials | ||
| * ComputeEngineCredentials} or {@link com.google.auth.oauth2.ExternalAccountCredentials | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: |
||
| * ExternalAccountCredentials} (for Workforce Identity Federation, service account impersonation | ||
| * must be configured). | ||
| * | ||
| * @param sourceCredential the {@code GoogleCredentials} to set. This is a | ||
| * <strong>required</strong> parameter. | ||
| * @return this {@code Builder} object for chaining. | ||
|
|
@@ -693,13 +730,18 @@ public Builder setHttpTransportFactory(HttpTransportFactory transportFactory) { | |
| } | ||
|
|
||
| /** | ||
| * Sets the optional universe domain. | ||
| * Sets an optional expected universe domain. | ||
| * | ||
| * <p>When omitted, {@code null}, or blank, the universe domain is inherited from the source | ||
| * credential. When explicitly set, {@link #build()} validates that it matches the source | ||
| * credential's universe domain. | ||
| * | ||
| * @param universeDomain the universe domain to set | ||
| * @param universeDomain the expected universe domain, or {@code null} to inherit from the | ||
| * source credential | ||
| * @return this {@code Builder} object | ||
| */ | ||
| @CanIgnoreReturnValue | ||
| public Builder setUniverseDomain(String universeDomain) { | ||
| public Builder setUniverseDomain(@Nullable String universeDomain) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because this class is
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
| this.universeDomain = universeDomain; | ||
| return this; | ||
| } | ||
|
|
@@ -720,10 +762,14 @@ public Builder setClock(Clock clock) { | |
| * builder configuration. | ||
| * | ||
| * @return A new {@code ClientSideCredentialAccessBoundaryFactory} instance. | ||
| * @throws IllegalStateException if the builder is not properly configured (e.g., if the source | ||
| * credential is not set). | ||
| * @throws IllegalArgumentException if the refresh margin is not at least one minute longer than | ||
| * the minimum token lifetime. | ||
| * @throws NullPointerException if {@code sourceCredential} is not set. | ||
| * @throws IllegalStateException if the source credential's universe domain cannot be retrieved | ||
| * or Tink AEAD registration fails. | ||
| * @throws IllegalArgumentException if the source credential's universe domain is {@code null} | ||
| * or blank, if an explicitly configured universe domain does not match the source | ||
| * credential, if a {@link ServiceAccountCredentials} or {@link UserCredentials} is used | ||
| * directly in a non-default universe domain, or if the refresh margin is not at least one | ||
| * minute longer than the minimum token lifetime. | ||
| */ | ||
| public ClientSideCredentialAccessBoundaryFactory build() { | ||
| checkNotNull(sourceCredential, "Source credential must not be null."); | ||
|
|
@@ -734,22 +780,61 @@ public ClientSideCredentialAccessBoundaryFactory build() { | |
| getFromServiceLoader(HttpTransportFactory.class, OAuth2Utils.HTTP_TRANSPORT_FACTORY); | ||
| } | ||
|
|
||
| // Default to GDU when not supplied. | ||
| if (Strings.isNullOrEmpty(universeDomain)) { | ||
| this.universeDomain = Credentials.GOOGLE_DEFAULT_UNIVERSE; | ||
| String sourceUniverseDomain; | ||
| try { | ||
| sourceUniverseDomain = sourceCredential.getUniverseDomain(); | ||
| } catch (IOException e) { | ||
| // Throwing an IOException would be a breaking change, so wrap it here. | ||
| throw new IllegalStateException( | ||
| "Error occurred when attempting to retrieve source credential universe domain.", e); | ||
| } | ||
|
|
||
| // Ensure source credential's universe domain matches. | ||
| try { | ||
| if (!universeDomain.equals(sourceCredential.getUniverseDomain())) { | ||
| if (sourceUniverseDomain == null || sourceUniverseDomain.trim().isEmpty()) { | ||
| throw new IllegalArgumentException( | ||
| "The source credential's universe domain must not be null or empty."); | ||
| } | ||
|
|
||
| String resolvedUniverseDomain; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: because |
||
| if (universeDomain == null || universeDomain.trim().isEmpty()) { | ||
| // Inherit the source credential's universe domain when not explicitly supplied. | ||
| resolvedUniverseDomain = sourceUniverseDomain; | ||
| } else { | ||
| // Validate explicit universe domain against the source credential. | ||
| if (!universeDomain.equals(sourceUniverseDomain)) { | ||
| throw new IllegalArgumentException( | ||
| "The client side access boundary credential's universe domain must be the same as the source " | ||
| + "credential."); | ||
| } | ||
| } catch (IOException e) { | ||
| // Throwing an IOException would be a breaking change, so wrap it here. | ||
| throw new IllegalStateException( | ||
| "Error occurred when attempting to retrieve source credential universe domain.", e); | ||
| resolvedUniverseDomain = universeDomain; | ||
| } | ||
|
|
||
| if (!Credentials.GOOGLE_DEFAULT_UNIVERSE.equals(resolvedUniverseDomain)) { | ||
| // Credential Access Boundaries require a Google-issued OAuth 2.0 access token, not a | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: this comment about self-signed JWTs applies to |
||
| // self-signed JWT | ||
| // (https://docs.cloud.google.com/iam/docs/create-downscoped-short-lived-credentials#before-you-begin). | ||
| if (sourceCredential instanceof ServiceAccountCredentials) { | ||
| throw new IllegalArgumentException( | ||
| String.format( | ||
| "ServiceAccountCredentials cannot be used directly as a source credential for" | ||
| + " ClientSideCredentialAccessBoundaryFactory in universe domain %s because" | ||
| + " OAuth2 access token refresh is only supported in %s. Wrap the service" | ||
| + " account in ImpersonatedCredentials, or use credentials such as" | ||
| + " ComputeEngineCredentials or ExternalAccountCredentials (for Workforce" | ||
| + " Identity Federation, service account impersonation must be configured)" | ||
| + " instead.", | ||
| resolvedUniverseDomain, Credentials.GOOGLE_DEFAULT_UNIVERSE)); | ||
| } | ||
| if (sourceCredential instanceof UserCredentials) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error message says |
||
| throw new IllegalArgumentException( | ||
| String.format( | ||
| "UserCredentials cannot be used as a source credential for" | ||
| + " ClientSideCredentialAccessBoundaryFactory in universe domain %s because" | ||
| + " UserCredentials are only supported in %s. Use credentials such as" | ||
| + " ImpersonatedCredentials, ComputeEngineCredentials, or" | ||
| + " ExternalAccountCredentials (for Workforce Identity Federation, service" | ||
| + " account impersonation must be configured) instead.", | ||
| resolvedUniverseDomain, Credentials.GOOGLE_DEFAULT_UNIVERSE)); | ||
| } | ||
| } | ||
|
|
||
| // Use default values for refreshMargin if not provided. | ||
|
|
@@ -769,7 +854,7 @@ public ClientSideCredentialAccessBoundaryFactory build() { | |
| "Refresh margin must be at least one minute longer than the minimum token lifetime."); | ||
| } | ||
|
|
||
| this.tokenExchangeEndpoint = String.format(TOKEN_EXCHANGE_URL_FORMAT, universeDomain); | ||
| this.tokenExchangeEndpoint = String.format(TOKEN_EXCHANGE_URL_FORMAT, resolvedUniverseDomain); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: now that omitting |
||
| return new ClientSideCredentialAccessBoundaryFactory(this); | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reads like
UserCredentialswork for client-side CAB ingoogleapis.com. Unlike server-sideDownscopedCredentials, STS client-side CAB rejects user account tokens in every universe, includinggoogleapis.com.