Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions java-storage/google-cloud-storage/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
<groupId>com.google.api</groupId>
<artifactId>gax-grpc</artifactId>
</dependency>
<dependency>
<groupId>com.google.api</groupId>
<artifactId>gax-httpjson</artifactId>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-credentials</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {

Copy link
Copy Markdown
Contributor Author

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

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

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.

Could we also add unit tests covering the IOException path when getUniverseDomain() fails and the ComputeEngineCredentials bypass so those branches in validateSigningCredentialsUniverseDomain are exercised?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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,
Expand Down
Loading
Loading