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 "auth/Crypto.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(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 ldout(s->cct, 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 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(*static_cast<RGWObjectCtx *>(s->obj_ctx),
110 bucket_tenant, bucket_name,
111 bucket_info, nullptr);
116 ldout(cct, 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 bool TempURLEngine::is_expired(const std::string& expires) const
127 const utime_t now = ceph_clock_now();
128 const uint64_t expiration = (uint64_t)strict_strtoll(expires.c_str(),
131 dout(5) << "failed to parse temp_url_expires: " << err << dendl;
135 if (expiration <= (uint64_t)now.sec()) {
136 dout(5) << "temp url expired: " << expiration << " <= " << now.sec() << dendl;
143 std::string extract_swift_subuser(const std::string& swift_user_name) {
144 size_t pos = swift_user_name.find(':');
145 if (std::string::npos == pos) {
146 return swift_user_name;
148 return swift_user_name.substr(pos + 1);
152 class TempURLEngine::SignatureHelper
155 static constexpr uint32_t output_size =
156 CEPH_CRYPTO_HMACSHA1_DIGESTSIZE * 2 + 1;
158 unsigned char dest[CEPH_CRYPTO_HMACSHA1_DIGESTSIZE]; // 20
159 char dest_str[output_size];
162 SignatureHelper() = default;
164 const char* calc(const std::string& key,
165 const boost::string_view& method,
166 const boost::string_view& path,
167 const std::string& expires) {
169 using ceph::crypto::HMACSHA1;
170 using UCHARPTR = const unsigned char*;
172 HMACSHA1 hmac((UCHARPTR) key.c_str(), key.size());
173 hmac.Update((UCHARPTR) method.data(), method.size());
174 hmac.Update((UCHARPTR) "\n", 1);
175 hmac.Update((UCHARPTR) expires.c_str(), expires.size());
176 hmac.Update((UCHARPTR) "\n", 1);
177 hmac.Update((UCHARPTR) path.data(), path.size());
180 buf_to_hex((UCHARPTR) dest, sizeof(dest), dest_str);
185 bool is_equal_to(const std::string& rhs) const {
186 /* never allow out-of-range exception */
187 if (rhs.size() < (output_size - 1)) {
190 return rhs.compare(0 /* pos */, output_size, dest_str) == 0;
193 }; /* TempURLEngine::SignatureHelper */
195 class TempURLEngine::PrefixableSignatureHelper
196 : private TempURLEngine::SignatureHelper {
197 using base_t = SignatureHelper;
199 const boost::string_view decoded_uri;
200 const boost::string_view object_name;
201 boost::string_view no_obj_uri;
203 const boost::optional<const std::string&> prefix;
206 PrefixableSignatureHelper(const std::string& _decoded_uri,
207 const std::string& object_name,
208 const boost::optional<const std::string&> prefix)
209 : decoded_uri(_decoded_uri),
210 object_name(object_name),
212 /* Transform: v1/acct/cont/obj - > v1/acct/cont/
214 * NOTE(rzarzynski): we really want to substr() on boost::string_view,
215 * not std::string. Otherwise we would end with no_obj_uri referencing
218 decoded_uri.substr(0, decoded_uri.length() - object_name.length());
221 const char* calc(const std::string& key,
222 const boost::string_view& method,
223 const boost::string_view& path,
224 const std::string& expires) {
226 return base_t::calc(key, method, path, expires);
228 const auto prefixed_path = \
229 string_cat_reserve("prefix:", no_obj_uri, *prefix);
230 return base_t::calc(key, method, prefixed_path, expires);
234 bool is_equal_to(const std::string& rhs) const {
235 bool is_auth_ok = base_t::is_equal_to(rhs);
237 if (prefix && is_auth_ok) {
238 const auto prefix_uri = string_cat_reserve(no_obj_uri, *prefix);
239 is_auth_ok = boost::algorithm::starts_with(decoded_uri, prefix_uri);
244 }; /* TempURLEngine::PrefixableSignatureHelper */
246 TempURLEngine::result_t
247 TempURLEngine::authenticate(const req_state* const s) const
249 if (! is_applicable(s)) {
250 return result_t::deny();
253 /* NOTE(rzarzynski): RGWHTTPArgs::get(), in contrast to RGWEnv::get(),
254 * never returns nullptr. If the requested parameter is absent, we will
255 * get the empty string. */
256 const std::string& temp_url_sig = s->info.args.get("temp_url_sig");
257 const std::string& temp_url_expires = s->info.args.get("temp_url_expires");
259 if (temp_url_sig.empty() || temp_url_expires.empty()) {
260 return result_t::deny();
263 /* Though, for prefixed tempurls we need to differentiate between empty
264 * prefix and lack of prefix. Empty prefix means allowance for whole
266 const boost::optional<const std::string&> temp_url_prefix = \
267 s->info.args.get_optional("temp_url_prefix");
269 RGWUserInfo owner_info;
271 get_owner_info(s, owner_info);
273 ldout(cct, 5) << "cannot get user_info of account's owner" << dendl;
274 return result_t::reject();
277 if (owner_info.temp_url_keys.empty()) {
278 ldout(cct, 5) << "user does not have temp url key set, aborting" << dendl;
279 return result_t::reject();
282 if (is_expired(temp_url_expires)) {
283 ldout(cct, 5) << "temp url link expired" << dendl;
284 return result_t::reject(-EPERM);
287 /* We need to verify two paths because of compliance with Swift, Tempest
288 * and old versions of RadosGW. The second item will have the prefix
289 * of Swift API entry point removed. */
291 /* XXX can we search this ONCE? */
292 const size_t pos = g_conf->rgw_swift_url_prefix.find_last_not_of('/') + 1;
293 const boost::string_view ref_uri = s->decoded_uri;
294 const std::array<boost::string_view, 2> allowed_paths = {
296 ref_uri.substr(pos + 1)
299 /* Account owner calculates the signature also against a HTTP method. */
300 boost::container::static_vector<boost::string_view, 3> allowed_methods;
301 if (strcmp("HEAD", s->info.method) == 0) {
302 /* HEAD requests are specially handled. */
303 /* TODO: after getting a newer boost (with static_vector supporting
304 * initializers lists), get back to the good notation:
305 * allowed_methods = {"HEAD", "GET", "PUT" };
306 * Just for now let's use emplace_back to construct the vector. */
307 allowed_methods.emplace_back("HEAD");
308 allowed_methods.emplace_back("GET");
309 allowed_methods.emplace_back("PUT");
310 } else if (strlen(s->info.method) > 0) {
311 allowed_methods.emplace_back(s->info.method);
314 /* Need to try each combination of keys, allowed path and methods. */
315 PrefixableSignatureHelper sig_helper {
321 for (const auto& kv : owner_info.temp_url_keys) {
322 const int temp_url_key_num = kv.first;
323 const string& temp_url_key = kv.second;
325 if (temp_url_key.empty()) {
329 for (const auto& path : allowed_paths) {
330 for (const auto& method : allowed_methods) {
331 const char* const local_sig = sig_helper.calc(temp_url_key, method,
332 path, temp_url_expires);
334 ldout(s->cct, 20) << "temp url signature [" << temp_url_key_num
335 << "] (calculated): " << local_sig
338 if (sig_helper.is_equal_to(temp_url_sig)) {
339 auto apl = apl_factory->create_apl_turl(cct, s, owner_info);
340 return result_t::grant(std::move(apl));
342 ldout(s->cct, 5) << "temp url signature mismatch: " << local_sig
343 << " != " << temp_url_sig << dendl;
349 return result_t::reject();
354 bool ExternalTokenEngine::is_applicable(const std::string& token) const noexcept
358 } else if (g_conf->rgw_swift_auth_url.empty()) {
365 ExternalTokenEngine::result_t
366 ExternalTokenEngine::authenticate(const std::string& token,
367 const req_state* const s) const
369 if (! is_applicable(token)) {
370 return result_t::deny();
373 std::string auth_url = g_conf->rgw_swift_auth_url;
374 if (auth_url.back() != '/') {
375 auth_url.append("/");
378 auth_url.append("token");
379 char url_buf[auth_url.size() + 1 + token.length() + 1];
380 sprintf(url_buf, "%s/%s", auth_url.c_str(), token.c_str());
382 RGWHTTPHeadersCollector validator(cct, { "X-Auth-Groups", "X-Auth-Ttl" });
384 ldout(cct, 10) << "rgw_swift_validate_token url=" << url_buf << dendl;
386 int ret = validator.process(url_buf);
391 std::string swift_user;
393 std::vector<std::string> swift_groups;
394 get_str_vec(validator.get_header_value("X-Auth-Groups"),
397 if (0 == swift_groups.size()) {
398 return result_t::deny(-EPERM);
400 swift_user = std::move(swift_groups[0]);
402 } catch (std::out_of_range) {
403 /* The X-Auth-Groups header isn't present in the response. */
404 return result_t::deny(-EPERM);
407 if (swift_user.empty()) {
408 return result_t::deny(-EPERM);
411 ldout(cct, 10) << "swift user=" << swift_user << dendl;
413 RGWUserInfo tmp_uinfo;
414 ret = rgw_get_user_info_by_swift(store, swift_user, tmp_uinfo);
416 ldout(cct, 0) << "NOTICE: couldn't map swift user" << dendl;
420 auto apl = apl_factory->create_apl_local(cct, s, tmp_uinfo,
421 extract_swift_subuser(swift_user));
422 return result_t::grant(std::move(apl));
425 static int build_token(const string& swift_user,
427 const uint64_t nonce,
428 const utime_t& expiration,
431 ::encode(swift_user, bl);
433 ::encode(expiration, bl);
435 bufferptr p(CEPH_CRYPTO_HMACSHA1_DIGESTSIZE);
437 char buf[bl.length() * 2 + 1];
438 buf_to_hex((const unsigned char *)bl.c_str(), bl.length(), buf);
439 dout(20) << "build_token token=" << buf << dendl;
441 char k[CEPH_CRYPTO_HMACSHA1_DIGESTSIZE];
442 memset(k, 0, sizeof(k));
443 const char *s = key.c_str();
444 for (int i = 0; i < (int)key.length(); i++, s++) {
445 k[i % CEPH_CRYPTO_HMACSHA1_DIGESTSIZE] |= *s;
447 calc_hmac_sha1(k, sizeof(k), bl.c_str(), bl.length(), p.c_str());
455 static int encode_token(CephContext *cct, string& swift_user, string& key,
460 int ret = get_random_bytes((char *)&nonce, sizeof(nonce));
464 utime_t expiration = ceph_clock_now();
465 expiration += cct->_conf->rgw_swift_token_expiration;
467 return build_token(swift_user, key, nonce, expiration, bl);
471 /* AUTH_rgwtk (signed token): engine */
472 bool SignedTokenEngine::is_applicable(const std::string& token) const noexcept
477 return token.compare(0, 10, "AUTH_rgwtk") == 0;
481 SignedTokenEngine::result_t
482 SignedTokenEngine::authenticate(const std::string& token,
483 const req_state* const s) const
485 if (! is_applicable(token)) {
486 return result_t::deny(-EPERM);
489 /* Effective token string is the part after the prefix. */
490 const std::string etoken = token.substr(strlen("AUTH_rgwtk"));
491 const size_t etoken_len = etoken.length();
493 if (etoken_len & 1) {
494 ldout(cct, 0) << "NOTICE: failed to verify token: odd token length="
495 << etoken_len << dendl;
499 ceph::bufferptr p(etoken_len/2);
500 int ret = hex_to_buf(etoken.c_str(), p.c_str(), etoken_len);
505 ceph::bufferlist tok_bl;
510 std::string swift_user;
513 /*const*/ auto iter = tok_bl.begin();
515 ::decode(swift_user, iter);
516 ::decode(nonce, iter);
517 ::decode(expiration, iter);
518 } catch (buffer::error& err) {
519 ldout(cct, 0) << "NOTICE: failed to decode token" << dendl;
523 const utime_t now = ceph_clock_now();
524 if (expiration < now) {
525 ldout(cct, 0) << "NOTICE: old timed out token was used now=" << now
526 << " token.expiration=" << expiration
528 return result_t::deny(-EPERM);
531 RGWUserInfo user_info;
532 ret = rgw_get_user_info_by_swift(store, swift_user, user_info);
537 ldout(cct, 10) << "swift_user=" << swift_user << dendl;
539 const auto siter = user_info.swift_keys.find(swift_user);
540 if (siter == std::end(user_info.swift_keys)) {
541 return result_t::deny(-EPERM);
544 const auto swift_key = siter->second;
546 bufferlist local_tok_bl;
547 ret = build_token(swift_user, swift_key.key, nonce, expiration, local_tok_bl);
552 if (local_tok_bl.length() != tok_bl.length()) {
553 ldout(cct, 0) << "NOTICE: tokens length mismatch:"
554 << " tok_bl.length()=" << tok_bl.length()
555 << " local_tok_bl.length()=" << local_tok_bl.length()
557 return result_t::deny(-EPERM);
560 if (memcmp(local_tok_bl.c_str(), tok_bl.c_str(),
561 local_tok_bl.length()) != 0) {
562 char buf[local_tok_bl.length() * 2 + 1];
564 buf_to_hex(reinterpret_cast<const unsigned char *>(local_tok_bl.c_str()),
565 local_tok_bl.length(), buf);
567 ldout(cct, 0) << "NOTICE: tokens mismatch tok=" << buf << dendl;
568 return result_t::deny(-EPERM);
571 auto apl = apl_factory->create_apl_local(cct, s, user_info,
572 extract_swift_subuser(swift_user));
573 return result_t::grant(std::move(apl));
576 } /* namespace swift */
577 } /* namespace auth */
578 } /* namespace rgw */
581 void RGW_SWIFT_Auth_Get::execute()
585 const char *key = s->info.env->get("HTTP_X_AUTH_KEY");
586 const char *user = s->info.env->get("HTTP_X_AUTH_USER");
588 s->prot_flags |= RGW_REST_SWIFT;
593 RGWAccessKey *swift_key;
594 map<string, RGWAccessKey>::iterator siter;
596 string swift_url = g_conf->rgw_swift_url;
597 string swift_prefix = g_conf->rgw_swift_url_prefix;
601 * We did not allow an empty Swift prefix before, but we want it now.
602 * So, we take rgw_swift_url_prefix = "/" to yield the empty prefix.
603 * The rgw_swift_url_prefix = "" is the default and yields "/swift"
604 * in a backwards-compatible way.
606 if (swift_prefix.size() == 0) {
607 swift_prefix = DEFAULT_SWIFT_PREFIX;
608 } else if (swift_prefix == "/") {
609 swift_prefix.clear();
611 if (swift_prefix[0] != '/') {
612 swift_prefix.insert(0, "/");
616 if (swift_url.size() == 0) {
617 bool add_port = false;
618 const char *server_port = s->info.env->get("SERVER_PORT_SECURE");
619 const char *protocol;
621 add_port = (strcmp(server_port, "443") != 0);
624 server_port = s->info.env->get("SERVER_PORT");
625 add_port = (strcmp(server_port, "80") != 0);
628 const char *host = s->info.env->get("HTTP_HOST");
630 dout(0) << "NOTICE: server is misconfigured, missing rgw_swift_url_prefix or rgw_swift_url, HTTP_HOST is not set" << dendl;
634 swift_url = protocol;
635 swift_url.append("://");
636 swift_url.append(host);
637 if (add_port && !strchr(host, ':')) {
638 swift_url.append(":");
639 swift_url.append(server_port);
648 if ((ret = rgw_get_user_info_by_swift(store, user_str, info)) < 0)
654 siter = info.swift_keys.find(user_str);
655 if (siter == info.swift_keys.end()) {
659 swift_key = &siter->second;
661 if (swift_key->key.compare(key) != 0) {
662 dout(0) << "NOTICE: RGW_SWIFT_Auth_Get::execute(): bad swift key" << dendl;
667 if (!g_conf->rgw_swift_tenant_name.empty()) {
668 tenant_path = "/AUTH_";
669 tenant_path.append(g_conf->rgw_swift_tenant_name);
670 } else if (g_conf->rgw_swift_account_in_url) {
671 tenant_path = "/AUTH_";
672 tenant_path.append(info.user_id.to_str());
675 dump_header(s, "X-Storage-Url", swift_url + swift_prefix + "/v1" +
678 using rgw::auth::swift::encode_token;
679 if ((ret = encode_token(s->cct, swift_key->id, swift_key->key, bl)) < 0)
683 static constexpr size_t PREFIX_LEN = sizeof("AUTH_rgwtk") - 1;
684 char token_val[PREFIX_LEN + bl.length() * 2 + 1];
686 snprintf(token_val, PREFIX_LEN + 1, "AUTH_rgwtk");
687 buf_to_hex((const unsigned char *)bl.c_str(), bl.length(),
688 token_val + PREFIX_LEN);
690 dump_header(s, "X-Storage-Token", token_val);
691 dump_header(s, "X-Auth-Token", token_val);
694 ret = STATUS_NO_CONTENT;
697 set_req_state_err(s, ret);
702 int RGWHandler_SWIFT_Auth::init(RGWRados *store, struct req_state *state,
703 rgw::io::BasicClient *cio)
705 state->dialect = "swift-auth";
706 state->formatter = new JSONFormatter;
707 state->format = RGW_FORMAT_JSON;
709 return RGWHandler::init(store, state, cio);
712 int RGWHandler_SWIFT_Auth::authorize()
717 RGWOp *RGWHandler_SWIFT_Auth::op_get()
719 return new RGW_SWIFT_Auth_Get;