Skip to content

Commit

Permalink
feat(storage): Respect custom endpoint for SignedUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
bajajneha27 committed May 9, 2024
1 parent 8c959c5 commit 3623449
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion google/cloud/storage/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ StatusOr<std::string> Client::SignUrlV2(
std::string signature = curl.MakeEscapedString(encoded).get();

std::ostringstream os;
os << "https://storage.googleapis.com/" << request.bucket_name();
os << ExternalUrl() << request.bucket_name();
if (!request.object_name().empty()) {
os << '/' << curl.MakeEscapedString(request.object_name()).get();
}
Expand Down Expand Up @@ -481,6 +481,10 @@ std::string CreateRandomPrefixName(std::string const& prefix) {
"abcdefghijklmnopqrstuvwxyz");
}

std::string Client::ExternalUrl() {
return connection_->client_options().endpoint() + "/";
}

namespace internal {

Client ClientImplDetails::CreateWithDecorations(
Expand Down
2 changes: 2 additions & 0 deletions google/cloud/storage/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -3492,6 +3492,8 @@ class Client {
StatusOr<PolicyDocumentV4Result> SignPolicyDocumentV4(
internal::PolicyDocumentV4Request request);

std::string ExternalUrl();

std::shared_ptr<internal::StorageConnection> connection_;
};

Expand Down
11 changes: 11 additions & 0 deletions google/cloud/storage/client_sign_url_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ TEST_F(CreateSignedUrlTest, V2SignRemote) {
EXPECT_THAT(*actual, HasSubstr(expected_signed_blob_safe));
}

/// @test Verify that CreateV2SignedUrl() respects the custom endpoint.
TEST_F(CreateSignedUrlTest, V2SignCustomEndpoint) {
std::string custom_endpoint = "https://storage.mydomain.com";

Client client(Options{}.set<RestEndpointOption>(custom_endpoint));
StatusOr<std::string> actual =
client.CreateV2SignedUrl("GET", "test-bucket", "test-object");
ASSERT_STATUS_OK(actual);
EXPECT_THAT(*actual, HasSubstr(custom_endpoint));
}

// This is a placeholder service account JSON file that is inactive. It's fine
// for it to be public.
constexpr char kJsonKeyfileContentsForV4[] = R"""({
Expand Down

0 comments on commit 3623449

Please sign in to comment.