Repository navigation
fix(storage): make URL signing universe domain handling consistent with EndpointContext #15239
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: fix/storage-universe-domain-env
Are you sure you want to change the base?
Changes from all commits
5b67dd5
7ef0f1c
22dcd3e
335c502
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 |
|---|---|---|
|
|
@@ -25,13 +25,20 @@ | |
|
|
||
| import com.google.api.client.util.Data; | ||
| import com.google.api.core.ApiFuture; | ||
| import com.google.api.gax.httpjson.HttpJsonStatusCode; | ||
| import com.google.api.gax.paging.Page; | ||
| import com.google.api.gax.retrying.BasicResultRetryAlgorithm; | ||
| import com.google.api.gax.retrying.ResultRetryAlgorithm; | ||
| import com.google.api.gax.rpc.EndpointContext; | ||
| import com.google.api.gax.rpc.StatusCode; | ||
| import com.google.api.gax.rpc.UnauthenticatedException; | ||
| import com.google.api.services.storage.model.BucketAccessControl; | ||
| import com.google.api.services.storage.model.ObjectAccessControl; | ||
| import com.google.api.services.storage.model.StorageObject; | ||
| import com.google.auth.Credentials; | ||
| import com.google.auth.ServiceAccountSigner; | ||
| import com.google.auth.oauth2.ComputeEngineCredentials; | ||
| import com.google.auth.oauth2.GdchCredentials; | ||
| import com.google.cloud.BaseService; | ||
| import com.google.cloud.BatchResult; | ||
| import com.google.cloud.PageImpl; | ||
|
|
@@ -891,6 +898,7 @@ public URL signUrl(BlobInfo blobInfo, long duration, TimeUnit unit, SignUrlOptio | |
| "Signing key was not provided and could not be derived"); | ||
| credentials = (ServiceAccountSigner) this.getOptions().getCredentials(); | ||
| } | ||
| validateSigningCredentialsUniverseDomain(credentials); | ||
|
|
||
| long expiration = | ||
| isV4 | ||
|
|
@@ -998,6 +1006,7 @@ public PostPolicyV4 generateSignedPostPolicyV4( | |
| "Signing key was not provided and could not be derived"); | ||
| credentials = (ServiceAccountSigner) this.getOptions().getCredentials(); | ||
| } | ||
| validateSigningCredentialsUniverseDomain(credentials); | ||
|
|
||
| checkArgument( | ||
| !(optionMap.containsKey(SignUrlOption.Option.VIRTUAL_HOSTED_STYLE) | ||
|
|
@@ -1107,6 +1116,39 @@ public PostPolicyV4 generateSignedPostPolicyV4( | |
| blobInfo, duration, unit, PostFieldsV4.newBuilder().build(), options); | ||
| } | ||
|
|
||
| private void validateSigningCredentialsUniverseDomain(ServiceAccountSigner signer) { | ||
| if (!(signer instanceof Credentials) | ||
| || signer instanceof GdchCredentials | ||
| || signer instanceof ComputeEngineCredentials) { | ||
| return; | ||
| } | ||
| Credentials credentialsToValidate = (Credentials) signer; | ||
| String credentialsUniverseDomain; | ||
| try { | ||
| credentialsUniverseDomain = | ||
| firstNonNull( | ||
| credentialsToValidate.getUniverseDomain(), Credentials.GOOGLE_DEFAULT_UNIVERSE); | ||
| } catch (IOException e) { | ||
| throw unauthenticated(EndpointContext.UNABLE_TO_RETRIEVE_CREDENTIALS_ERROR_MESSAGE, e); | ||
| } | ||
|
Comment on lines
+1131
to
+1133
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. Could we also add unit tests covering the IOException path when getUniverseDomain() fails and the ComputeEngineCredentials bypass so those branches in validateSigningCredentialsUniverseDomain are exercised?
Contributor
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. Added a test in StorageSigningUniverseDomainTest for the IOException path when the signer's universe domain cannot be retrieved. Co-authored by AI Agent |
||
| String configuredUniverseDomain = getOptions().getResolvedUniverseDomain(); | ||
| if (!configuredUniverseDomain.equals(credentialsUniverseDomain)) { | ||
| String errorMessage = | ||
| String.format( | ||
| EndpointContext.INVALID_UNIVERSE_DOMAIN_ERROR_TEMPLATE, | ||
| configuredUniverseDomain, | ||
| credentialsUniverseDomain); | ||
| throw unauthenticated(errorMessage, new Throwable(errorMessage)); | ||
| } | ||
| } | ||
|
|
||
| private static StorageException unauthenticated(String message, Throwable cause) { | ||
| UnauthenticatedException unauthenticatedException = | ||
| new UnauthenticatedException( | ||
| message, cause, HttpJsonStatusCode.of(StatusCode.Code.UNAUTHENTICATED), false); | ||
| return new StorageException(401, message, unauthenticatedException); | ||
| } | ||
|
|
||
| private String constructResourceUriPath( | ||
| String slashlessBucketName, | ||
| String escapedBlobName, | ||
|
|
||
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.
Thanks for the suggestion. I will follow up with you on this one directly.
Co-authored by AI Agent