1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
6 #include <boost/utility/string_view.hpp>
7 #include <boost/container/static_vector.hpp>
8 #include <boost/algorithm/string/predicate.hpp>
9 #include <boost/algorithm/string.hpp>
11 #include "rgw_swift_auth.h"
14 #include "common/ceph_crypto.h"
15 #include "common/Clock.h"
17 #include "include/random.h"
19 #include "rgw_client_io.h"
20 #include "rgw_http_client.h"
21 #include "include/str_list.h"
23 #define dout_context g_ceph_context
24 #define dout_subsys ceph_subsys_rgw
26 #define DEFAULT_SWIFT_PREFIX "/swift"
28 using namespace ceph::crypto
;
35 /* TempURL: applier */
36 void TempURLApplier::modify_request_state(const DoutPrefixProvider
* dpp
, req_state
* s
) const /* in/out */
38 bool inline_exists
= false;
39 const std::string
& filename
= s
->info
.args
.get("filename");
41 s
->info
.args
.get("inline", &inline_exists
);
43 s
->content_disp
.override
= "inline";
44 } else if (!filename
.empty()) {
46 url_encode(filename
, fenc
);
47 s
->content_disp
.override
= "attachment; filename=\"" + fenc
+ "\"";
50 url_encode(s
->object
.name
, fenc
);
51 s
->content_disp
.fallback
= "attachment; filename=\"" + fenc
+ "\"";
54 ldpp_dout(dpp
, 20) << "finished applying changes to req_state for TempURL: "
55 << " content_disp override " << s
->content_disp
.override
56 << " content_disp fallback " << s
->content_disp
.fallback
62 bool TempURLEngine::is_applicable(const req_state
* const s
) const noexcept
64 return s
->info
.args
.exists("temp_url_sig") ||
65 s
->info
.args
.exists("temp_url_expires");
68 void TempURLEngine::get_owner_info(const DoutPrefixProvider
* dpp
, const req_state
* const s
,
69 RGWUserInfo
& owner_info
) const
71 /* We cannot use req_state::bucket_name because it isn't available
72 * now. It will be initialized in RGWHandler_REST_SWIFT::postauth_init(). */
73 const string
& bucket_name
= s
->init_state
.url_bucket
;
75 /* TempURL requires that bucket and object names are specified. */
76 if (bucket_name
.empty() || s
->object
.empty()) {
80 /* TempURL case is completely different than the Keystone auth - you may
81 * get account name only through extraction from URL. In turn, knowledge
82 * about account is neccessary to obtain its bucket tenant. Without that,
83 * the access would be limited to accounts with empty tenant. */
85 if (!s
->account_name
.empty()) {
89 const rgw_user
uid(s
->account_name
);
90 if (uid
.tenant
.empty()) {
91 const rgw_user
tenanted_uid(uid
.id
, uid
.id
);
93 if (rgw_get_user_info_by_uid(store
, tenanted_uid
, uinfo
) >= 0) {
95 bucket_tenant
= uinfo
.user_id
.tenant
;
100 if (!found
&& rgw_get_user_info_by_uid(store
, uid
, uinfo
) < 0) {
103 bucket_tenant
= uinfo
.user_id
.tenant
;
107 /* Need to get user info of bucket owner. */
108 RGWBucketInfo bucket_info
;
109 int ret
= store
->get_bucket_info(*s
->sysobj_ctx
,
110 bucket_tenant
, bucket_name
,
111 bucket_info
, nullptr);
116 ldpp_dout(dpp
, 20) << "temp url user (bucket owner): " << bucket_info
.owner
119 if (rgw_get_user_info_by_uid(store
, bucket_info
.owner
, owner_info
) < 0) {
124 std::string
TempURLEngine::convert_from_iso8601(std::string expires
) const
126 /* Swift's TempURL allows clients to send the expiration as ISO8601-
127 * compatible strings. Though, only plain UNIX timestamp are taken
128 * for the HMAC calculations. We need to make the conversion. */
130 if (!parse_iso8601(expires
.c_str(), &date_t
, nullptr, true)) {
131 return std::move(expires
);
133 return std::to_string(internal_timegm(&date_t
));
137 bool TempURLEngine::is_expired(const std::string
& expires
) const
140 const utime_t now
= ceph_clock_now();
141 const uint64_t expiration
= (uint64_t)strict_strtoll(expires
.c_str(),
144 dout(5) << "failed to parse temp_url_expires: " << err
<< dendl
;
148 if (expiration
<= (uint64_t)now
.sec()) {
149 dout(5) << "temp url expired: " << expiration
<< " <= " << now
.sec() << dendl
;
156 std::string
extract_swift_subuser(const std::string
& swift_user_name
) {
157 size_t pos
= swift_user_name
.find(':');
158 if (std::string::npos
== pos
) {
159 return swift_user_name
;
161 return swift_user_name
.substr(pos
+ 1);
165 class TempURLEngine::SignatureHelper
168 static constexpr uint32_t output_size
=
169 CEPH_CRYPTO_HMACSHA1_DIGESTSIZE
* 2 + 1;
171 unsigned char dest
[CEPH_CRYPTO_HMACSHA1_DIGESTSIZE
]; // 20
172 char dest_str
[output_size
];
175 SignatureHelper() = default;
177 const char* calc(const std::string
& key
,
178 const boost::string_view
& method
,
179 const boost::string_view
& path
,
180 const std::string
& expires
) {
182 using ceph::crypto::HMACSHA1
;
183 using UCHARPTR
= const unsigned char*;
185 HMACSHA1
hmac((UCHARPTR
) key
.c_str(), key
.size());
186 hmac
.Update((UCHARPTR
) method
.data(), method
.size());
187 hmac
.Update((UCHARPTR
) "\n", 1);
188 hmac
.Update((UCHARPTR
) expires
.c_str(), expires
.size());
189 hmac
.Update((UCHARPTR
) "\n", 1);
190 hmac
.Update((UCHARPTR
) path
.data(), path
.size());
193 buf_to_hex((UCHARPTR
) dest
, sizeof(dest
), dest_str
);
198 bool is_equal_to(const std::string
& rhs
) const {
199 /* never allow out-of-range exception */
200 if (rhs
.size() < (output_size
- 1)) {
203 return rhs
.compare(0 /* pos */, output_size
, dest_str
) == 0;
206 }; /* TempURLEngine::SignatureHelper */
208 class TempURLEngine::PrefixableSignatureHelper
209 : private TempURLEngine::SignatureHelper
{
210 using base_t
= SignatureHelper
;
212 const boost::string_view decoded_uri
;
213 const boost::string_view object_name
;
214 boost::string_view no_obj_uri
;
216 const boost::optional
<const std::string
&> prefix
;
219 PrefixableSignatureHelper(const std::string
& _decoded_uri
,
220 const std::string
& object_name
,
221 const boost::optional
<const std::string
&> prefix
)
222 : decoded_uri(_decoded_uri
),
223 object_name(object_name
),
225 /* Transform: v1/acct/cont/obj - > v1/acct/cont/
227 * NOTE(rzarzynski): we really want to substr() on boost::string_view,
228 * not std::string. Otherwise we would end with no_obj_uri referencing
231 decoded_uri
.substr(0, decoded_uri
.length() - object_name
.length());
234 const char* calc(const std::string
& key
,
235 const boost::string_view
& method
,
236 const boost::string_view
& path
,
237 const std::string
& expires
) {
239 return base_t::calc(key
, method
, path
, expires
);
241 const auto prefixed_path
= \
242 string_cat_reserve("prefix:", no_obj_uri
, *prefix
);
243 return base_t::calc(key
, method
, prefixed_path
, expires
);
247 bool is_equal_to(const std::string
& rhs
) const {
248 bool is_auth_ok
= base_t::is_equal_to(rhs
);
250 if (prefix
&& is_auth_ok
) {
251 const auto prefix_uri
= string_cat_reserve(no_obj_uri
, *prefix
);
252 is_auth_ok
= boost::algorithm::starts_with(decoded_uri
, prefix_uri
);
257 }; /* TempURLEngine::PrefixableSignatureHelper */
259 TempURLEngine::result_t
260 TempURLEngine::authenticate(const DoutPrefixProvider
* dpp
, const req_state
* const s
) const
262 if (! is_applicable(s
)) {
263 return result_t::deny();
266 /* NOTE(rzarzynski): RGWHTTPArgs::get(), in contrast to RGWEnv::get(),
267 * never returns nullptr. If the requested parameter is absent, we will
268 * get the empty string. */
269 const std::string
& temp_url_sig
= s
->info
.args
.get("temp_url_sig");
270 const std::string
& temp_url_expires
= \
271 convert_from_iso8601(s
->info
.args
.get("temp_url_expires"));
273 if (temp_url_sig
.empty() || temp_url_expires
.empty()) {
274 return result_t::deny();
277 /* Though, for prefixed tempurls we need to differentiate between empty
278 * prefix and lack of prefix. Empty prefix means allowance for whole
280 const boost::optional
<const std::string
&> temp_url_prefix
= \
281 s
->info
.args
.get_optional("temp_url_prefix");
283 RGWUserInfo owner_info
;
285 get_owner_info(dpp
, s
, owner_info
);
287 ldpp_dout(dpp
, 5) << "cannot get user_info of account's owner" << dendl
;
288 return result_t::reject();
291 if (owner_info
.temp_url_keys
.empty()) {
292 ldpp_dout(dpp
, 5) << "user does not have temp url key set, aborting" << dendl
;
293 return result_t::reject();
296 if (is_expired(temp_url_expires
)) {
297 ldpp_dout(dpp
, 5) << "temp url link expired" << dendl
;
298 return result_t::reject(-EPERM
);
301 /* We need to verify two paths because of compliance with Swift, Tempest
302 * and old versions of RadosGW. The second item will have the prefix
303 * of Swift API entry point removed. */
305 /* XXX can we search this ONCE? */
306 const size_t pos
= g_conf()->rgw_swift_url_prefix
.find_last_not_of('/') + 1;
307 const boost::string_view ref_uri
= s
->decoded_uri
;
308 const std::array
<boost::string_view
, 2> allowed_paths
= {
310 ref_uri
.substr(pos
+ 1)
313 /* Account owner calculates the signature also against a HTTP method. */
314 boost::container::static_vector
<boost::string_view
, 3> allowed_methods
;
315 if (strcmp("HEAD", s
->info
.method
) == 0) {
316 /* HEAD requests are specially handled. */
317 /* TODO: after getting a newer boost (with static_vector supporting
318 * initializers lists), get back to the good notation:
319 * allowed_methods = {"HEAD", "GET", "PUT" };
320 * Just for now let's use emplace_back to construct the vector. */
321 allowed_methods
.emplace_back("HEAD");
322 allowed_methods
.emplace_back("GET");
323 allowed_methods
.emplace_back("PUT");
324 } else if (strlen(s
->info
.method
) > 0) {
325 allowed_methods
.emplace_back(s
->info
.method
);
328 /* Need to try each combination of keys, allowed path and methods. */
329 PrefixableSignatureHelper sig_helper
{
335 for (const auto& kv
: owner_info
.temp_url_keys
) {
336 const int temp_url_key_num
= kv
.first
;
337 const string
& temp_url_key
= kv
.second
;
339 if (temp_url_key
.empty()) {
343 for (const auto& path
: allowed_paths
) {
344 for (const auto& method
: allowed_methods
) {
345 const char* const local_sig
= sig_helper
.calc(temp_url_key
, method
,
346 path
, temp_url_expires
);
348 ldpp_dout(dpp
, 20) << "temp url signature [" << temp_url_key_num
349 << "] (calculated): " << local_sig
352 if (sig_helper
.is_equal_to(temp_url_sig
)) {
353 auto apl
= apl_factory
->create_apl_turl(cct
, s
, owner_info
);
354 return result_t::grant(std::move(apl
));
356 ldpp_dout(dpp
, 5) << "temp url signature mismatch: " << local_sig
357 << " != " << temp_url_sig
<< dendl
;
363 return result_t::reject();
368 bool ExternalTokenEngine::is_applicable(const std::string
& token
) const noexcept
372 } else if (g_conf()->rgw_swift_auth_url
.empty()) {
379 ExternalTokenEngine::result_t
380 ExternalTokenEngine::authenticate(const DoutPrefixProvider
* dpp
,
381 const std::string
& token
,
382 const req_state
* const s
) const
384 if (! is_applicable(token
)) {
385 return result_t::deny();
388 std::string auth_url
= g_conf()->rgw_swift_auth_url
;
389 if (auth_url
.back() != '/') {
390 auth_url
.append("/");
393 auth_url
.append("token");
394 char url_buf
[auth_url
.size() + 1 + token
.length() + 1];
395 sprintf(url_buf
, "%s/%s", auth_url
.c_str(), token
.c_str());
397 RGWHTTPHeadersCollector
validator(cct
, "GET", url_buf
, { "X-Auth-Groups", "X-Auth-Ttl" });
399 ldpp_dout(dpp
, 10) << "rgw_swift_validate_token url=" << url_buf
<< dendl
;
401 int ret
= validator
.process();
406 std::string swift_user
;
408 std::vector
<std::string
> swift_groups
;
409 get_str_vec(validator
.get_header_value("X-Auth-Groups"),
412 if (0 == swift_groups
.size()) {
413 return result_t::deny(-EPERM
);
415 swift_user
= std::move(swift_groups
[0]);
417 } catch (const std::out_of_range
&) {
418 /* The X-Auth-Groups header isn't present in the response. */
419 return result_t::deny(-EPERM
);
422 if (swift_user
.empty()) {
423 return result_t::deny(-EPERM
);
426 ldpp_dout(dpp
, 10) << "swift user=" << swift_user
<< dendl
;
428 RGWUserInfo tmp_uinfo
;
429 ret
= rgw_get_user_info_by_swift(store
, swift_user
, tmp_uinfo
);
431 ldpp_dout(dpp
, 0) << "NOTICE: couldn't map swift user" << dendl
;
435 auto apl
= apl_factory
->create_apl_local(cct
, s
, tmp_uinfo
,
436 extract_swift_subuser(swift_user
),
438 return result_t::grant(std::move(apl
));
441 static int build_token(const string
& swift_user
,
443 const uint64_t nonce
,
444 const utime_t
& expiration
,
448 encode(swift_user
, bl
);
450 encode(expiration
, bl
);
452 bufferptr
p(CEPH_CRYPTO_HMACSHA1_DIGESTSIZE
);
454 char buf
[bl
.length() * 2 + 1];
455 buf_to_hex((const unsigned char *)bl
.c_str(), bl
.length(), buf
);
456 dout(20) << "build_token token=" << buf
<< dendl
;
458 char k
[CEPH_CRYPTO_HMACSHA1_DIGESTSIZE
];
459 memset(k
, 0, sizeof(k
));
460 const char *s
= key
.c_str();
461 for (int i
= 0; i
< (int)key
.length(); i
++, s
++) {
462 k
[i
% CEPH_CRYPTO_HMACSHA1_DIGESTSIZE
] |= *s
;
464 calc_hmac_sha1(k
, sizeof(k
), bl
.c_str(), bl
.length(), p
.c_str());
472 static int encode_token(CephContext
*cct
, string
& swift_user
, string
& key
,
475 const auto nonce
= ceph::util::generate_random_number
<uint64_t>();
477 utime_t expiration
= ceph_clock_now();
478 expiration
+= cct
->_conf
->rgw_swift_token_expiration
;
480 return build_token(swift_user
, key
, nonce
, expiration
, bl
);
484 /* AUTH_rgwtk (signed token): engine */
485 bool SignedTokenEngine::is_applicable(const std::string
& token
) const noexcept
490 return token
.compare(0, 10, "AUTH_rgwtk") == 0;
494 SignedTokenEngine::result_t
495 SignedTokenEngine::authenticate(const DoutPrefixProvider
* dpp
,
496 const std::string
& token
,
497 const req_state
* const s
) const
499 if (! is_applicable(token
)) {
500 return result_t::deny(-EPERM
);
503 /* Effective token string is the part after the prefix. */
504 const std::string etoken
= token
.substr(strlen("AUTH_rgwtk"));
505 const size_t etoken_len
= etoken
.length();
507 if (etoken_len
& 1) {
508 ldpp_dout(dpp
, 0) << "NOTICE: failed to verify token: odd token length="
509 << etoken_len
<< dendl
;
513 ceph::bufferptr
p(etoken_len
/2);
514 int ret
= hex_to_buf(etoken
.c_str(), p
.c_str(), etoken_len
);
519 ceph::bufferlist tok_bl
;
524 std::string swift_user
;
527 auto iter
= tok_bl
.cbegin();
530 decode(swift_user
, iter
);
532 decode(expiration
, iter
);
533 } catch (buffer::error
& err
) {
534 ldpp_dout(dpp
, 0) << "NOTICE: failed to decode token" << dendl
;
538 const utime_t now
= ceph_clock_now();
539 if (expiration
< now
) {
540 ldpp_dout(dpp
, 0) << "NOTICE: old timed out token was used now=" << now
541 << " token.expiration=" << expiration
543 return result_t::deny(-EPERM
);
546 RGWUserInfo user_info
;
547 ret
= rgw_get_user_info_by_swift(store
, swift_user
, user_info
);
552 ldpp_dout(dpp
, 10) << "swift_user=" << swift_user
<< dendl
;
554 const auto siter
= user_info
.swift_keys
.find(swift_user
);
555 if (siter
== std::end(user_info
.swift_keys
)) {
556 return result_t::deny(-EPERM
);
559 const auto swift_key
= siter
->second
;
561 bufferlist local_tok_bl
;
562 ret
= build_token(swift_user
, swift_key
.key
, nonce
, expiration
, local_tok_bl
);
567 if (local_tok_bl
.length() != tok_bl
.length()) {
568 ldpp_dout(dpp
, 0) << "NOTICE: tokens length mismatch:"
569 << " tok_bl.length()=" << tok_bl
.length()
570 << " local_tok_bl.length()=" << local_tok_bl
.length()
572 return result_t::deny(-EPERM
);
575 if (memcmp(local_tok_bl
.c_str(), tok_bl
.c_str(),
576 local_tok_bl
.length()) != 0) {
577 char buf
[local_tok_bl
.length() * 2 + 1];
579 buf_to_hex(reinterpret_cast<const unsigned char *>(local_tok_bl
.c_str()),
580 local_tok_bl
.length(), buf
);
582 ldpp_dout(dpp
, 0) << "NOTICE: tokens mismatch tok=" << buf
<< dendl
;
583 return result_t::deny(-EPERM
);
586 auto apl
= apl_factory
->create_apl_local(cct
, s
, user_info
,
587 extract_swift_subuser(swift_user
),
589 return result_t::grant(std::move(apl
));
592 } /* namespace swift */
593 } /* namespace auth */
594 } /* namespace rgw */
597 void RGW_SWIFT_Auth_Get::execute()
601 const char *key
= s
->info
.env
->get("HTTP_X_AUTH_KEY");
602 const char *user
= s
->info
.env
->get("HTTP_X_AUTH_USER");
604 s
->prot_flags
|= RGW_REST_SWIFT
;
609 RGWAccessKey
*swift_key
;
610 map
<string
, RGWAccessKey
>::iterator siter
;
612 string swift_url
= g_conf()->rgw_swift_url
;
613 string swift_prefix
= g_conf()->rgw_swift_url_prefix
;
617 * We did not allow an empty Swift prefix before, but we want it now.
618 * So, we take rgw_swift_url_prefix = "/" to yield the empty prefix.
619 * The rgw_swift_url_prefix = "" is the default and yields "/swift"
620 * in a backwards-compatible way.
622 if (swift_prefix
.size() == 0) {
623 swift_prefix
= DEFAULT_SWIFT_PREFIX
;
624 } else if (swift_prefix
== "/") {
625 swift_prefix
.clear();
627 if (swift_prefix
[0] != '/') {
628 swift_prefix
.insert(0, "/");
632 if (swift_url
.size() == 0) {
633 bool add_port
= false;
634 const char *server_port
= s
->info
.env
->get("SERVER_PORT_SECURE");
635 const char *protocol
;
637 add_port
= (strcmp(server_port
, "443") != 0);
640 server_port
= s
->info
.env
->get("SERVER_PORT");
641 add_port
= (strcmp(server_port
, "80") != 0);
644 const char *host
= s
->info
.env
->get("HTTP_HOST");
646 dout(0) << "NOTICE: server is misconfigured, missing rgw_swift_url_prefix or rgw_swift_url, HTTP_HOST is not set" << dendl
;
650 swift_url
= protocol
;
651 swift_url
.append("://");
652 swift_url
.append(host
);
653 if (add_port
&& !strchr(host
, ':')) {
654 swift_url
.append(":");
655 swift_url
.append(server_port
);
664 if ((ret
= rgw_get_user_info_by_swift(store
, user_str
, info
)) < 0)
670 siter
= info
.swift_keys
.find(user_str
);
671 if (siter
== info
.swift_keys
.end()) {
675 swift_key
= &siter
->second
;
677 if (swift_key
->key
.compare(key
) != 0) {
678 dout(0) << "NOTICE: RGW_SWIFT_Auth_Get::execute(): bad swift key" << dendl
;
683 if (!g_conf()->rgw_swift_tenant_name
.empty()) {
684 tenant_path
= "/AUTH_";
685 tenant_path
.append(g_conf()->rgw_swift_tenant_name
);
686 } else if (g_conf()->rgw_swift_account_in_url
) {
687 tenant_path
= "/AUTH_";
688 tenant_path
.append(info
.user_id
.to_str());
691 dump_header(s
, "X-Storage-Url", swift_url
+ swift_prefix
+ "/v1" +
694 using rgw::auth::swift::encode_token
;
695 if ((ret
= encode_token(s
->cct
, swift_key
->id
, swift_key
->key
, bl
)) < 0)
699 static constexpr size_t PREFIX_LEN
= sizeof("AUTH_rgwtk") - 1;
700 char token_val
[PREFIX_LEN
+ bl
.length() * 2 + 1];
702 snprintf(token_val
, PREFIX_LEN
+ 1, "AUTH_rgwtk");
703 buf_to_hex((const unsigned char *)bl
.c_str(), bl
.length(),
704 token_val
+ PREFIX_LEN
);
706 dump_header(s
, "X-Storage-Token", token_val
);
707 dump_header(s
, "X-Auth-Token", token_val
);
710 ret
= STATUS_NO_CONTENT
;
713 set_req_state_err(s
, ret
);
718 int RGWHandler_SWIFT_Auth::init(RGWRados
*store
, struct req_state
*state
,
719 rgw::io::BasicClient
*cio
)
721 state
->dialect
= "swift-auth";
722 state
->formatter
= new JSONFormatter
;
723 state
->format
= RGW_FORMAT_JSON
;
725 return RGWHandler::init(store
, state
, cio
);
728 int RGWHandler_SWIFT_Auth::authorize(const DoutPrefixProvider
*dpp
)
733 RGWOp
*RGWHandler_SWIFT_Auth::op_get()
735 return new RGW_SWIFT_Auth_Get
;