Skip to content

Commit

Permalink
feat(storage): per-operation options / signed URLs (#9208)
Browse files Browse the repository at this point in the history
Support per-operation `google::cloud::Options` for operations related to
creating signed URLs.
  • Loading branch information
coryan authored Jun 8, 2022
1 parent d1bf4bc commit d1ad40f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
2 changes: 2 additions & 0 deletions google/cloud/storage/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -2829,6 +2829,7 @@ class Client {
std::string bucket_name,
std::string object_name,
Options&&... options) {
auto const span = MakeSpan(std::forward<Options>(options)...);
internal::V2SignUrlRequest request(std::move(verb), std::move(bucket_name),
std::move(object_name));
request.set_multiple_options(std::forward<Options>(options)...);
Expand Down Expand Up @@ -2892,6 +2893,7 @@ class Client {
std::string bucket_name,
std::string object_name,
Options&&... options) {
auto const span = MakeSpan(std::forward<Options>(options)...);
internal::V4SignUrlRequest request(std::move(verb), std::move(bucket_name),
std::move(object_name));
request.set_multiple_options(std::forward<Options>(options)...);
Expand Down
11 changes: 9 additions & 2 deletions google/cloud/storage/client_sign_url_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace storage {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace {

using ::google::cloud::internal::CurrentOptions;
using ::google::cloud::storage::testing::canonical_errors::TransientError;
using ::testing::HasSubstr;
using ::testing::Return;
Expand Down Expand Up @@ -92,12 +93,15 @@ TEST_F(CreateSignedUrlTest, V2SignRemote) {
EXPECT_CALL(*mock_, SignBlob)
.WillOnce(Return(StatusOr<internal::SignBlobResponse>(TransientError())))
.WillOnce([&expected_signed_blob](internal::SignBlobRequest const&) {
EXPECT_EQ(CurrentOptions().get<AuthorityOption>(), "a-default");
EXPECT_EQ(CurrentOptions().get<UserProjectOption>(), "u-p-test");
return make_status_or(
internal::SignBlobResponse{"test-key-id", expected_signed_blob});
});
auto client = ClientForMock();
StatusOr<std::string> actual =
client.CreateV2SignedUrl("GET", "test-bucket", "test-object");
client.CreateV2SignedUrl("GET", "test-bucket", "test-object",
Options{}.set<UserProjectOption>("u-p-test"));
ASSERT_STATUS_OK(actual);
EXPECT_THAT(*actual, HasSubstr(expected_signed_blob_safe));
}
Expand Down Expand Up @@ -235,12 +239,15 @@ TEST_F(CreateSignedUrlTest, V4SignRemote) {
EXPECT_CALL(*mock_, SignBlob)
.WillOnce(Return(StatusOr<internal::SignBlobResponse>(TransientError())))
.WillOnce([&expected_signed_blob](internal::SignBlobRequest const&) {
EXPECT_EQ(CurrentOptions().get<AuthorityOption>(), "a-default");
EXPECT_EQ(CurrentOptions().get<UserProjectOption>(), "u-p-test");
return make_status_or(
internal::SignBlobResponse{"test-key-id", expected_signed_blob});
});
auto client = ClientForMock();
StatusOr<std::string> actual =
client.CreateV4SignedUrl("GET", "test-bucket", "test-object");
client.CreateV4SignedUrl("GET", "test-bucket", "test-object",
Options{}.set<UserProjectOption>("u-p-test"));
ASSERT_STATUS_OK(actual);
EXPECT_THAT(*actual, HasSubstr(expected_signed_blob_hex));
}
Expand Down
38 changes: 38 additions & 0 deletions google/cloud/storage/internal/signed_url_requests.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,25 @@ class V2SignUrlRequest {
}

V2SignUrlRequest& set_multiple_options() { return *this; }
template <typename... T>
V2SignUrlRequest& set_multiple_options(google::cloud::Options const&&,
T&&... tail) {
return set_multiple_options(std::forward<T>(tail)...);
}
template <typename... T>
V2SignUrlRequest& set_multiple_options(google::cloud::Options const&,
T&&... tail) {
return set_multiple_options(std::forward<T>(tail)...);
}
template <typename... T>
V2SignUrlRequest& set_multiple_options(google::cloud::Options&&,
T&&... tail) {
return set_multiple_options(std::forward<T>(tail)...);
}
template <typename... T>
V2SignUrlRequest& set_multiple_options(google::cloud::Options&, T&&... tail) {
return set_multiple_options(std::forward<T>(tail)...);
}

private:
static std::chrono::system_clock::time_point DefaultExpirationTime();
Expand Down Expand Up @@ -258,6 +277,25 @@ class V4SignUrlRequest {
}

V4SignUrlRequest& set_multiple_options() { return *this; }
template <typename... T>
V4SignUrlRequest& set_multiple_options(google::cloud::Options const&&,
T&&... tail) {
return set_multiple_options(std::forward<T>(tail)...);
}
template <typename... T>
V4SignUrlRequest& set_multiple_options(google::cloud::Options const&,
T&&... tail) {
return set_multiple_options(std::forward<T>(tail)...);
}
template <typename... T>
V4SignUrlRequest& set_multiple_options(google::cloud::Options&&,
T&&... tail) {
return set_multiple_options(std::forward<T>(tail)...);
}
template <typename... T>
V4SignUrlRequest& set_multiple_options(google::cloud::Options&, T&&... tail) {
return set_multiple_options(std::forward<T>(tail)...);
}

Status Validate();

Expand Down

0 comments on commit d1ad40f

Please sign in to comment.