userver: /data/code/userver/libraries/s3api/src/s3api/clients/client_test.cpp Source File
Loading...
Searching...
No Matches
client_test.cpp
1#include <s3api/clients/client.hpp>
2
3#include <array>
4#include <chrono>
5#include <memory>
6#include <stdexcept>
7#include <string>
8#include <unordered_map>
9
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>
14
15#include <s3api/s3_connection.hpp>
16
17USERVER_NAMESPACE_BEGIN
18
19namespace s3api {
20namespace {
21
22constexpr http::headers::PredefinedHeader kFormatMetadataHeader{"x-amz-meta-format"};
23constexpr http::headers::PredefinedHeader kSourceMetadataHeader{"x-amz-meta-source"};
24
25class UnusedHttpClient final : public clients::http::Client {
26public:
27 clients::http::Request CreateRequest() override { throw std::logic_error("HTTP request is not expected"); }
28};
29
30class HostCapturingAuthenticator final : public authenticators::Authenticator {
31public:
32 std::unordered_map<std::string, std::string> Auth(const Request&) const override { return {}; }
33
34 std::unordered_map<std::string, std::string> Sign(const Request& request, std::time_t) const override {
35 const auto it = request.headers.find(http::headers::kHost);
36 if (it == request.headers.end()) {
37 throw std::logic_error("Host header is missing");
38 }
39 signed_host = it->second;
40 return {};
41 }
42
43 mutable std::string signed_host;
44};
45
46class ConditionalHeadersAuthenticator final : public authenticators::Authenticator {
47public:
48 std::unordered_map<std::string, std::string> Auth(const Request& request) const override {
49 const auto match = request.headers.find(http::headers::kIfMatch);
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");
55 }
56
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");
59 }
60
61 mutable std::string if_match;
62 mutable std::string if_none_match;
63 mutable clients::http::Headers headers;
64};
65
66UTEST(S3ConditionalWrite, SignsCreationAndReplacementPreconditions) {
67 UnusedHttpClient http_client;
68 auto connection = std::make_shared<S3Connection>(
69 http_client,
70 S3ConnectionType::kHttps,
71 "s3.example.com",
72 ConnectionCfg{std::chrono::milliseconds{1000}}
73 );
74 auto authenticator = std::make_shared<ConditionalHeadersAuthenticator>();
75 ClientImpl client{std::move(connection), authenticator, "bucket"};
76
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());
80
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());
84
85 EXPECT_THROW(client.PutObjectConditional("head", "data", ""), std::invalid_argument);
86}
87
88UTEST(S3ConditionalWrite, SignsMetadataWithCreationAndReplacementPreconditions) {
89 UnusedHttpClient http_client;
90 auto connection = std::make_shared<S3Connection>(
91 http_client,
92 S3ConnectionType::kHttps,
93 "s3.example.com",
94 ConnectionCfg{std::chrono::milliseconds{1000}}
95 );
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"}};
99
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 ? "" : "*");
106 }
107
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, "*");
113 }
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, "*");
118}
119
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}};
123
124 struct EndpointCase {
125 const char* endpoint;
126 const char* expected_host;
127 const char* expected_endpoint_path;
128 };
129 // Secdist endpoints may include a scheme and path; sign only Host and keep the path in the URL.
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"},
137 };
138
139 for (const auto& [endpoint, expected_host, expected_endpoint_path] : endpoints) {
140 auto connection = std::make_shared<S3Connection>(
141 http_client,
142 S3ConnectionType::kHttps,
143 endpoint,
144 ConnectionCfg{std::chrono::milliseconds{1000}}
145 );
146 auto authenticator = std::make_shared<HostCapturingAuthenticator>();
147 ClientImpl client{std::move(connection), authenticator, "bucket"};
148
149 for (const auto* protocol : std::array{"http://", "https://"}) {
150 const auto
151 expected_url = std::string{protocol} + expected_host + expected_endpoint_path + "/path/to/object";
152 EXPECT_EQ(
153 client.GenerateDownloadUrlVirtualHostAddressing("path/to/object", expires_at, protocol),
154 expected_url
155 );
156 EXPECT_EQ(authenticator->signed_host, expected_host);
157
158 EXPECT_EQ(
159 client.GenerateUploadUrlVirtualHostAddressing(
160 "content",
161 "text/plain",
162 "path/to/object",
163 expires_at,
164 protocol
165 ),
166 expected_url
167 );
168 EXPECT_EQ(authenticator->signed_host, expected_host);
169 }
170 }
171}
172
173} // namespace
174} // namespace s3api
175
176USERVER_NAMESPACE_END