1#include <s3api/clients/client.hpp>
8#include <unordered_map>
10#include <userver/clients/http/client.hpp>
11#include <userver/http/common_headers.hpp>
12#include <userver/s3api/models/request.hpp>
13#include <userver/utest/utest.hpp>
15#include <s3api/s3_connection.hpp>
17USERVER_NAMESPACE_BEGIN
22constexpr http::
headers::PredefinedHeader kFormatMetadataHeader{
"x-amz-meta-format"};
23constexpr http::
headers::PredefinedHeader kSourceMetadataHeader{
"x-amz-meta-source"};
25class UnusedHttpClient
final :
public clients::http::Client {
27 clients::http::Request CreateRequest()
override {
throw std::logic_error(
"HTTP request is not expected"); }
32 std::unordered_map<std::string, std::string> Auth(
const Request&)
const override {
return {}; }
34 std::unordered_map<std::string, std::string> Sign(
const Request& request, std::time_t)
const override {
36 if (it == request.headers
.end()) {
37 throw std::logic_error(
"Host header is missing");
39 signed_host = it->second;
43 mutable std::string signed_host;
48 std::unordered_map<std::string, std::string> Auth(
const Request& request)
const override {
50 const auto none_match = request.headers
.find(http::
headers::kIfNoneMatch
);
51 if_match = match == request.headers
.end() ?
"" : match->second;
52 if_none_match = none_match == request.headers
.end() ?
"" : none_match->second;
53 headers
= request.headers;
54 throw std::runtime_error(
"Authentication captured the conditional request");
57 std::unordered_map<std::string, std::string> Sign(
const Request&, std::time_t)
const override {
58 throw std::logic_error(
"Presigning is not expected");
61 mutable std::string if_match;
62 mutable std::string if_none_match;
63 mutable clients::http::Headers headers;
66UTEST(S3ConditionalWrite, SignsCreationAndReplacementPreconditions) {
67 UnusedHttpClient http_client;
70 S3ConnectionType::kHttps,
74 auto authenticator = std::make_shared<ConditionalHeadersAuthenticator>();
75 ClientImpl client{std::move(connection), authenticator,
"bucket"};
77 EXPECT_THROW(client.PutObjectConditional(
"head",
"data", std::nullopt), std::runtime_error);
78 EXPECT_EQ(authenticator->if_none_match,
"*");
79 EXPECT_TRUE(authenticator->if_match.empty());
81 EXPECT_THROW(client.PutObjectConditional(
"head",
"data",
"\"previous\""), std::runtime_error);
82 EXPECT_EQ(authenticator->if_match,
"\"previous\"");
83 EXPECT_TRUE(authenticator->if_none_match.empty());
85 EXPECT_THROW(client.PutObjectConditional(
"head",
"data",
""), std::invalid_argument);
88UTEST(S3ConditionalWrite, SignsMetadataWithCreationAndReplacementPreconditions) {
89 UnusedHttpClient http_client;
92 S3ConnectionType::kHttps,
96 auto authenticator = std::make_shared<ConditionalHeadersAuthenticator>();
97 ClientImpl client{std::move(connection), authenticator,
"bucket"};
98 const Client::Meta meta{{
"format",
"envelope-v2"}, {
"source",
"test"}};
100 for (
const auto& etag : {std::optional<std::string>{}, std::optional<std::string>{
"\"previous\""}}) {
101 EXPECT_THROW(client.PutObjectConditional(
"head",
"data", etag, meta), std::runtime_error);
102 EXPECT_EQ(authenticator->headers.at(kFormatMetadataHeader),
"envelope-v2");
103 EXPECT_EQ(authenticator->headers.at(kSourceMetadataHeader),
"test");
104 EXPECT_EQ(authenticator->if_match, etag.value_or(
""));
105 EXPECT_EQ(authenticator->if_none_match, etag ?
"" :
"*");
108 for (
const auto& empty_meta : {std::optional<Client::Meta>{}, std::optional<Client::Meta>{Client::Meta{}}}) {
109 EXPECT_THROW(client.PutObjectConditional(
"head",
"data", std::nullopt, empty_meta), std::runtime_error);
110 EXPECT_EQ(authenticator->headers.count(kFormatMetadataHeader), 0);
111 EXPECT_EQ(authenticator->headers.count(kSourceMetadataHeader), 0);
112 EXPECT_EQ(authenticator->if_none_match,
"*");
114 EXPECT_THROW(client.PutObjectConditional(
"head",
"data",
"", meta), std::invalid_argument);
115 EXPECT_THROW(client.Client::PutObjectConditional(
"head",
"data", std::nullopt, meta), std::logic_error);
116 EXPECT_THROW(client.Client::PutObjectConditional(
"head",
"data", std::nullopt, std::nullopt), std::runtime_error);
117 EXPECT_EQ(authenticator->if_none_match,
"*");
120TEST(S3PresignedUrl, NormalizesEndpointHostForDownloadAndUpload) {
121 UnusedHttpClient http_client;
122 const auto expires_at = std::chrono::system_clock::time_point{std::chrono::seconds{1'700'000'000}};
124 struct EndpointCase {
125 const char* endpoint;
126 const char* expected_host;
127 const char* expected_endpoint_path;
130 constexpr std::array endpoints{
131 EndpointCase{
"s3.example.com",
"bucket.s3.example.com",
""},
132 EndpointCase{
"http://s3.example.com",
"bucket.s3.example.com",
""},
133 EndpointCase{
"https://s3.example.com",
"bucket.s3.example.com",
""},
134 EndpointCase{
"s3.example.com/mds-s3",
"bucket.s3.example.com",
"/mds-s3"},
135 EndpointCase{
"http://localhost:41871/s3mds",
"bucket.localhost:41871",
"/s3mds"},
136 EndpointCase{
"https://localhost:41871/mds-s3-staff/",
"bucket.localhost:41871",
"/mds-s3-staff"},
139 for (
const auto& [endpoint, expected_host, expected_endpoint_path] : endpoints) {
140 auto connection = std::make_shared<S3Connection>(
142 S3ConnectionType::kHttps,
144 ConnectionCfg{std::chrono::milliseconds{1000}}
146 auto authenticator = std::make_shared<HostCapturingAuthenticator>();
147 ClientImpl client{std::move(connection), authenticator,
"bucket"};
149 for (
const auto* protocol : std::array{
"http://",
"https://"}) {
151 expected_url = std::string{protocol} + expected_host + expected_endpoint_path +
"/path/to/object";
153 client.GenerateDownloadUrlVirtualHostAddressing(
"path/to/object", expires_at, protocol),
156 EXPECT_EQ(authenticator->signed_host, expected_host);
159 client.GenerateUploadUrlVirtualHostAddressing(
168 EXPECT_EQ(authenticator->signed_host, expected_host);