1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab ft=cpp
4 #ifndef CEPH_RGW_AUTH_S3_H
5 #define CEPH_RGW_AUTH_S3_H
12 #include <boost/algorithm/string.hpp>
13 #include <boost/container/static_vector.hpp>
14 #include <boost/utility/string_ref.hpp>
15 #include <boost/utility/string_view.hpp>
17 #include "common/sstring.hh"
18 #include "rgw_common.h"
19 #include "rgw_rest_s3.h"
21 #include "rgw_auth_filters.h"
22 #include "rgw_auth_keystone.h"
29 static constexpr auto RGW_AUTH_GRACE
= std::chrono::minutes
{15};
31 // returns true if the request time is within RGW_AUTH_GRACE of the current time
32 bool is_time_skew_ok(time_t t
);
34 class STSAuthStrategy
: public rgw::auth::Strategy
,
35 public rgw::auth::RemoteApplier::Factory
,
36 public rgw::auth::LocalApplier::Factory
,
37 public rgw::auth::RoleApplier::Factory
{
38 typedef rgw::auth::IdentityApplier::aplptr_t aplptr_t
;
40 rgw::auth::ImplicitTenants
& implicit_tenant_context
;
44 aplptr_t
create_apl_remote(CephContext
* const cct
,
45 const req_state
* const s
,
46 rgw::auth::RemoteApplier::acl_strategy_t
&& acl_alg
,
47 const rgw::auth::RemoteApplier::AuthInfo
&info
49 auto apl
= rgw::auth::add_sysreq(cct
, ctl
, s
,
50 rgw::auth::RemoteApplier(cct
, ctl
, std::move(acl_alg
), info
,
51 implicit_tenant_context
,
52 rgw::auth::ImplicitTenants::IMPLICIT_TENANTS_S3
));
53 return aplptr_t(new decltype(apl
)(std::move(apl
)));
56 aplptr_t
create_apl_local(CephContext
* const cct
,
57 const req_state
* const s
,
58 const RGWUserInfo
& user_info
,
59 const std::string
& subuser
,
60 const boost::optional
<uint32_t>& perm_mask
) const override
{
61 auto apl
= rgw::auth::add_sysreq(cct
, ctl
, s
,
62 rgw::auth::LocalApplier(cct
, user_info
, subuser
, perm_mask
));
63 return aplptr_t(new decltype(apl
)(std::move(apl
)));
66 aplptr_t
create_apl_role(CephContext
* const cct
,
67 const req_state
* const s
,
68 const string
& role_name
,
69 const rgw_user
& user_id
,
70 const vector
<std::string
>& role_policies
) const override
{
71 auto apl
= rgw::auth::add_sysreq(cct
, ctl
, s
,
72 rgw::auth::RoleApplier(cct
, role_name
, user_id
, role_policies
));
73 return aplptr_t(new decltype(apl
)(std::move(apl
)));
77 STSAuthStrategy(CephContext
* const cct
,
79 rgw::auth::ImplicitTenants
& implicit_tenant_context
,
80 AWSEngine::VersionAbstractor
* const ver_abstractor
)
82 implicit_tenant_context(implicit_tenant_context
),
83 sts_engine(cct
, ctl
, *ver_abstractor
,
84 static_cast<rgw::auth::LocalApplier::Factory
*>(this),
85 static_cast<rgw::auth::RemoteApplier::Factory
*>(this),
86 static_cast<rgw::auth::RoleApplier::Factory
*>(this)) {
87 if (cct
->_conf
->rgw_s3_auth_use_sts
) {
88 add_engine(Control::SUFFICIENT
, sts_engine
);
92 const char* get_name() const noexcept override
{
93 return "rgw::auth::s3::STSAuthStrategy";
97 class ExternalAuthStrategy
: public rgw::auth::Strategy
,
98 public rgw::auth::RemoteApplier::Factory
{
99 typedef rgw::auth::IdentityApplier::aplptr_t aplptr_t
;
101 rgw::auth::ImplicitTenants
& implicit_tenant_context
;
103 using keystone_config_t
= rgw::keystone::CephCtxConfig
;
104 using keystone_cache_t
= rgw::keystone::TokenCache
;
105 using secret_cache_t
= rgw::auth::keystone::SecretCache
;
106 using EC2Engine
= rgw::auth::keystone::EC2Engine
;
108 boost::optional
<EC2Engine
> keystone_engine
;
109 LDAPEngine ldap_engine
;
111 aplptr_t
create_apl_remote(CephContext
* const cct
,
112 const req_state
* const s
,
113 rgw::auth::RemoteApplier::acl_strategy_t
&& acl_alg
,
114 const rgw::auth::RemoteApplier::AuthInfo
&info
116 auto apl
= rgw::auth::add_sysreq(cct
, ctl
, s
,
117 rgw::auth::RemoteApplier(cct
, ctl
, std::move(acl_alg
), info
,
118 implicit_tenant_context
,
119 rgw::auth::ImplicitTenants::IMPLICIT_TENANTS_S3
));
120 /* TODO(rzarzynski): replace with static_ptr. */
121 return aplptr_t(new decltype(apl
)(std::move(apl
)));
125 ExternalAuthStrategy(CephContext
* const cct
,
127 rgw::auth::ImplicitTenants
& implicit_tenant_context
,
128 AWSEngine::VersionAbstractor
* const ver_abstractor
)
130 implicit_tenant_context(implicit_tenant_context
),
131 ldap_engine(cct
, ctl
, *ver_abstractor
,
132 static_cast<rgw::auth::RemoteApplier::Factory
*>(this)) {
134 if (cct
->_conf
->rgw_s3_auth_use_keystone
&&
135 ! cct
->_conf
->rgw_keystone_url
.empty()) {
137 keystone_engine
.emplace(cct
, ver_abstractor
,
138 static_cast<rgw::auth::RemoteApplier::Factory
*>(this),
139 keystone_config_t::get_instance(),
140 keystone_cache_t::get_instance
<keystone_config_t
>(),
141 secret_cache_t::get_instance());
142 add_engine(Control::SUFFICIENT
, *keystone_engine
);
146 if (ldap_engine
.valid()) {
147 add_engine(Control::SUFFICIENT
, ldap_engine
);
151 const char* get_name() const noexcept override
{
152 return "rgw::auth::s3::AWSv2ExternalAuthStrategy";
157 template <class AbstractorT
,
158 bool AllowAnonAccessT
= false>
159 class AWSAuthStrategy
: public rgw::auth::Strategy
,
160 public rgw::auth::LocalApplier::Factory
{
161 typedef rgw::auth::IdentityApplier::aplptr_t aplptr_t
;
163 static_assert(std::is_base_of
<rgw::auth::s3::AWSEngine::VersionAbstractor
,
165 "AbstractorT must be a subclass of rgw::auth::s3::VersionAbstractor");
168 AbstractorT ver_abstractor
;
170 S3AnonymousEngine anonymous_engine
;
171 ExternalAuthStrategy external_engines
;
172 STSAuthStrategy sts_engine
;
173 LocalEngine local_engine
;
175 aplptr_t
create_apl_local(CephContext
* const cct
,
176 const req_state
* const s
,
177 const RGWUserInfo
& user_info
,
178 const std::string
& subuser
,
179 const boost::optional
<uint32_t>& perm_mask
) const override
{
180 auto apl
= rgw::auth::add_sysreq(cct
, ctl
, s
,
181 rgw::auth::LocalApplier(cct
, user_info
, subuser
, perm_mask
));
182 /* TODO(rzarzynski): replace with static_ptr. */
183 return aplptr_t(new decltype(apl
)(std::move(apl
)));
187 using engine_map_t
= std::map
<std::string
, std::reference_wrapper
<const Engine
>>;
188 void add_engines(const std::vector
<std::string
>& auth_order
,
189 engine_map_t eng_map
)
191 auto ctrl_flag
= Control::SUFFICIENT
;
192 for (const auto &eng
: auth_order
) {
193 // fallback to the last engine, in case of multiple engines, since ctrl
194 // flag is sufficient for others, error from earlier engine is returned
195 if (&eng
== &auth_order
.back() && eng_map
.size() > 1) {
196 ctrl_flag
= Control::FALLBACK
;
198 if (const auto kv
= eng_map
.find(eng
);
199 kv
!= eng_map
.end()) {
200 add_engine(ctrl_flag
, kv
->second
);
205 auto parse_auth_order(CephContext
* const cct
)
207 std::vector
<std::string
> result
;
209 const std::set
<std::string_view
> allowed_auth
= { "sts", "external", "local" };
210 std::vector
<std::string
> default_order
= { "sts", "external", "local" };
211 // supplied strings may contain a space, so let's bypass that
212 boost::split(result
, cct
->_conf
->rgw_s3_auth_order
,
213 boost::is_any_of(", "), boost::token_compress_on
);
215 if (std::any_of(result
.begin(), result
.end(),
216 [allowed_auth
](std::string_view s
)
217 { return allowed_auth
.find(s
) == allowed_auth
.end();})){
218 return default_order
;
223 AWSAuthStrategy(CephContext
* const cct
,
224 rgw::auth::ImplicitTenants
& implicit_tenant_context
,
228 anonymous_engine(cct
,
229 static_cast<rgw::auth::LocalApplier::Factory
*>(this)),
230 external_engines(cct
, ctl
, implicit_tenant_context
, &ver_abstractor
),
231 sts_engine(cct
, ctl
, implicit_tenant_context
, &ver_abstractor
),
232 local_engine(cct
, ctl
, ver_abstractor
,
233 static_cast<rgw::auth::LocalApplier::Factory
*>(this)) {
234 /* The anonymous auth. */
235 if (AllowAnonAccessT
) {
236 add_engine(Control::SUFFICIENT
, anonymous_engine
);
239 auto auth_order
= parse_auth_order(cct
);
240 engine_map_t engine_map
;
243 if (! sts_engine
.is_empty()) {
244 engine_map
.insert(std::make_pair("sts", std::cref(sts_engine
)));
247 /* The external auth. */
248 if (! external_engines
.is_empty()) {
249 engine_map
.insert(std::make_pair("external", std::cref(external_engines
)));
251 /* The local auth. */
252 if (cct
->_conf
->rgw_s3_auth_use_rados
) {
253 engine_map
.insert(std::make_pair("local", std::cref(local_engine
)));
256 add_engines(auth_order
, engine_map
);
259 const char* get_name() const noexcept override
{
260 return "rgw::auth::s3::AWSAuthStrategy";
265 class AWSv4ComplMulti
: public rgw::auth::Completer
,
266 public rgw::io::DecoratedRestfulClient
<rgw::io::RestfulClient
*>,
267 public std::enable_shared_from_this
<AWSv4ComplMulti
> {
268 using io_base_t
= rgw::io::DecoratedRestfulClient
<rgw::io::RestfulClient
*>;
269 using signing_key_t
= sha256_digest_t
;
271 CephContext
* const cct
;
273 const boost::string_view date
;
274 const boost::string_view credential_scope
;
275 const signing_key_t signing_key
;
278 size_t data_offset_in_stream
= 0;
279 size_t data_length
= 0;
280 std::string signature
;
282 ChunkMeta(const size_t data_starts_in_stream
,
283 const size_t data_length
,
284 const boost::string_ref signature
)
285 : data_offset_in_stream(data_starts_in_stream
),
286 data_length(data_length
),
287 signature(signature
.to_string()) {
290 explicit ChunkMeta(const boost::string_view
& signature
)
291 : signature(signature
.to_string()) {
295 static constexpr size_t SIG_SIZE
= 64;
297 /* Let's suppose the data length fields can't exceed uint64_t. */
298 static constexpr size_t META_MAX_SIZE
= \
299 sarrlen("\r\nffffffffffffffff;chunk-signature=") + SIG_SIZE
+ sarrlen("\r\n");
301 /* The metadata size of for the last, empty chunk. */
302 static constexpr size_t META_MIN_SIZE
= \
303 sarrlen("0;chunk-signature=") + SIG_SIZE
+ sarrlen("\r\n");
305 /* Detect whether a given stream_pos fits in boundaries of a chunk. */
306 bool is_new_chunk_in_stream(size_t stream_pos
) const;
308 /* Get the remaining data size. */
309 size_t get_data_size(size_t stream_pos
) const;
311 const std::string
& get_signature() const {
315 /* Factory: create an object representing metadata of first, initial chunk
317 static ChunkMeta
create_first(const boost::string_view
& seed_signature
) {
318 return ChunkMeta(seed_signature
);
321 /* Factory: parse a block of META_MAX_SIZE bytes and creates an object
322 * representing non-first chunk in a stream. As the process is sequential
323 * and depends on the previous chunk, caller must pass it. */
324 static std::pair
<ChunkMeta
, size_t> create_next(CephContext
* cct
,
331 boost::container::static_vector
<char, ChunkMeta::META_MAX_SIZE
> parsing_buf
;
332 ceph::crypto::SHA256
* sha256_hash
;
333 std::string prev_chunk_signature
;
335 bool is_signature_mismatched();
336 std::string
calc_chunk_signature(const std::string
& payload_hash
) const;
339 /* We need the constructor to be public because of the std::make_shared that
340 * is employed by the create() method. */
341 AWSv4ComplMulti(const req_state
* const s
,
342 boost::string_view date
,
343 boost::string_view credential_scope
,
344 boost::string_view seed_signature
,
345 const signing_key_t
& signing_key
)
346 : io_base_t(nullptr),
348 date(std::move(date
)),
349 credential_scope(std::move(credential_scope
)),
350 signing_key(signing_key
),
352 /* The evolving state. */
353 chunk_meta(ChunkMeta::create_first(seed_signature
)),
355 sha256_hash(calc_hash_sha256_open_stream()),
356 prev_chunk_signature(std::move(seed_signature
)) {
361 calc_hash_sha256_close_stream(&sha256_hash
);
365 /* rgw::io::DecoratedRestfulClient. */
366 size_t recv_body(char* buf
, size_t max
) override
;
368 /* rgw::auth::Completer. */
369 void modify_request_state(const DoutPrefixProvider
* dpp
, req_state
* s_rw
) override
;
370 bool complete() override
;
373 static cmplptr_t
create(const req_state
* s
,
374 boost::string_view date
,
375 boost::string_view credential_scope
,
376 boost::string_view seed_signature
,
377 const boost::optional
<std::string
>& secret_key
);
381 class AWSv4ComplSingle
: public rgw::auth::Completer
,
382 public rgw::io::DecoratedRestfulClient
<rgw::io::RestfulClient
*>,
383 public std::enable_shared_from_this
<AWSv4ComplSingle
> {
384 using io_base_t
= rgw::io::DecoratedRestfulClient
<rgw::io::RestfulClient
*>;
386 CephContext
* const cct
;
387 const char* const expected_request_payload_hash
;
388 ceph::crypto::SHA256
* sha256_hash
= nullptr;
391 /* Defined in rgw_auth_s3.cc because of get_v4_exp_payload_hash(). We need
392 * the constructor to be public because of the std::make_shared employed by
393 * the create() method. */
394 explicit AWSv4ComplSingle(const req_state
* const s
);
396 ~AWSv4ComplSingle() {
398 calc_hash_sha256_close_stream(&sha256_hash
);
402 /* rgw::io::DecoratedRestfulClient. */
403 size_t recv_body(char* buf
, size_t max
) override
;
405 /* rgw::auth::Completer. */
406 void modify_request_state(const DoutPrefixProvider
* dpp
, req_state
* s_rw
) override
;
407 bool complete() override
;
410 static cmplptr_t
create(const req_state
* s
,
411 const boost::optional
<std::string
>&);
416 } /* namespace auth */
417 } /* namespace rgw */
419 void rgw_create_s3_canonical_header(
421 const char *content_md5
,
422 const char *content_type
,
424 const meta_map_t
& meta_map
,
425 const meta_map_t
& qs_map
,
426 const char *request_uri
,
427 const std::map
<std::string
, std::string
>& sub_resources
,
428 std::string
& dest_str
);
429 bool rgw_create_s3_canonical_header(const req_info
& info
,
430 utime_t
*header_time
, /* out */
431 std::string
& dest
, /* out */
433 static inline std::tuple
<bool, std::string
, utime_t
>
434 rgw_create_s3_canonical_header(const req_info
& info
, const bool qsr
) {
438 const bool ok
= rgw_create_s3_canonical_header(info
, &header_time
, dest
, qsr
);
439 return std::make_tuple(ok
, dest
, header_time
);
446 static constexpr char AWS4_HMAC_SHA256_STR
[] = "AWS4-HMAC-SHA256";
447 static constexpr char AWS4_HMAC_SHA256_PAYLOAD_STR
[] = "AWS4-HMAC-SHA256-PAYLOAD";
449 static constexpr char AWS4_EMPTY_PAYLOAD_HASH
[] = \
450 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
452 static constexpr char AWS4_UNSIGNED_PAYLOAD_HASH
[] = "UNSIGNED-PAYLOAD";
454 static constexpr char AWS4_STREAMING_PAYLOAD_HASH
[] = \
455 "STREAMING-AWS4-HMAC-SHA256-PAYLOAD";
457 int parse_v4_credentials(const req_info
& info
, /* in */
458 boost::string_view
& access_key_id
, /* out */
459 boost::string_view
& credential_scope
, /* out */
460 boost::string_view
& signedheaders
, /* out */
461 boost::string_view
& signature
, /* out */
462 boost::string_view
& date
, /* out */
463 boost::string_view
& session_token
, /* out */
464 const bool using_qs
); /* in */
466 static inline bool char_needs_aws4_escaping(const char c
, bool encode_slash
)
468 if ((c
>= 'a' && c
<= 'z') ||
469 (c
>= 'A' && c
<= 'Z') ||
470 (c
>= '0' && c
<= '9')) {
482 if (c
== '/' && !encode_slash
)
488 static inline std::string
aws4_uri_encode(const std::string
& src
, bool encode_slash
)
492 for (const std::string::value_type c
: src
) {
493 if (char_needs_aws4_escaping(c
, encode_slash
)) {
494 rgw_uri_escape_char(c
, result
);
503 static inline std::string
aws4_uri_recode(const boost::string_view
& src
, bool encode_slash
)
505 std::string decoded
= url_decode(src
);
506 return aws4_uri_encode(decoded
, encode_slash
);
509 static inline std::string
get_v4_canonical_uri(const req_info
& info
) {
510 /* The code should normalize according to RFC 3986 but S3 does NOT do path
511 * normalization that SigV4 typically does. This code follows the same
512 * approach that boto library. See auth.py:canonical_uri(...). */
514 std::string canonical_uri
= aws4_uri_recode(info
.request_uri_aws4
, false);
516 if (canonical_uri
.empty()) {
519 boost::replace_all(canonical_uri
, "+", "%20");
522 return canonical_uri
;
525 static inline const string
calc_v4_payload_hash(const string
& payload
)
527 ceph::crypto::SHA256
* sha256_hash
= calc_hash_sha256_open_stream();
528 calc_hash_sha256_update_stream(sha256_hash
, payload
.c_str(), payload
.length());
529 const auto payload_hash
= calc_hash_sha256_close_stream(&sha256_hash
);
533 static inline const char* get_v4_exp_payload_hash(const req_info
& info
)
535 /* In AWSv4 the hash of real, transferred payload IS NOT necessary to form
536 * a Canonical Request, and thus verify a Signature. x-amz-content-sha256
537 * header lets get the information very early -- before seeing first byte
538 * of HTTP body. As a consequence, we can decouple Signature verification
539 * from payload's fingerprint check. */
540 const char *expected_request_payload_hash
= \
541 info
.env
->get("HTTP_X_AMZ_CONTENT_SHA256");
543 if (!expected_request_payload_hash
) {
544 /* An HTTP client MUST send x-amz-content-sha256. The single exception
545 * is the case of using the Query Parameters where "UNSIGNED-PAYLOAD"
546 * literals are used for crafting Canonical Request:
548 * You don't include a payload hash in the Canonical Request, because
549 * when you create a presigned URL, you don't know the payload content
550 * because the URL is used to upload an arbitrary payload. Instead, you
551 * use a constant string UNSIGNED-PAYLOAD. */
552 expected_request_payload_hash
= AWS4_UNSIGNED_PAYLOAD_HASH
;
555 return expected_request_payload_hash
;
558 static inline bool is_v4_payload_unsigned(const char* const exp_payload_hash
)
560 return boost::equals(exp_payload_hash
, AWS4_UNSIGNED_PAYLOAD_HASH
);
563 static inline bool is_v4_payload_empty(const req_state
* const s
)
565 /* from rfc2616 - 4.3 Message Body
567 * "The presence of a message-body in a request is signaled by the inclusion
568 * of a Content-Length or Transfer-Encoding header field in the request's
569 * message-headers." */
570 return s
->content_length
== 0 &&
571 s
->info
.env
->get("HTTP_TRANSFER_ENCODING") == nullptr;
574 static inline bool is_v4_payload_streamed(const char* const exp_payload_hash
)
576 return boost::equals(exp_payload_hash
, AWS4_STREAMING_PAYLOAD_HASH
);
579 std::string
get_v4_canonical_qs(const req_info
& info
, bool using_qs
);
581 boost::optional
<std::string
>
582 get_v4_canonical_headers(const req_info
& info
,
583 const boost::string_view
& signedheaders
,
585 bool force_boto2_compat
);
587 extern sha256_digest_t
588 get_v4_canon_req_hash(CephContext
* cct
,
589 const boost::string_view
& http_verb
,
590 const std::string
& canonical_uri
,
591 const std::string
& canonical_qs
,
592 const std::string
& canonical_hdrs
,
593 const boost::string_view
& signed_hdrs
,
594 const boost::string_view
& request_payload_hash
);
596 AWSEngine::VersionAbstractor::string_to_sign_t
597 get_v4_string_to_sign(CephContext
* cct
,
598 const boost::string_view
& algorithm
,
599 const boost::string_view
& request_date
,
600 const boost::string_view
& credential_scope
,
601 const sha256_digest_t
& canonreq_hash
);
603 extern AWSEngine::VersionAbstractor::server_signature_t
604 get_v4_signature(const boost::string_view
& credential_scope
,
605 CephContext
* const cct
,
606 const boost::string_view
& secret_key
,
607 const AWSEngine::VersionAbstractor::string_to_sign_t
& string_to_sign
);
609 extern AWSEngine::VersionAbstractor::server_signature_t
610 get_v2_signature(CephContext
*,
611 const std::string
& secret_key
,
612 const AWSEngine::VersionAbstractor::string_to_sign_t
& string_to_sign
);
614 } /* namespace auth */
615 } /* namespace rgw */