]> git.proxmox.com Git - ceph.git/blame - ceph/src/rgw/rgw_swift_auth.cc
import ceph 15.2.10
[ceph.git] / ceph / src / rgw / rgw_swift_auth.cc
CommitLineData
7c673cae 1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
9f95a23c 2// vim: ts=8 sw=2 smarttab ft=cpp
7c673cae
FG
3
4#include <array>
494da23a 5#include <algorithm>
7c673cae 6
d2e6a577 7#include <boost/utility/string_view.hpp>
7c673cae 8#include <boost/container/static_vector.hpp>
d2e6a577
FG
9#include <boost/algorithm/string/predicate.hpp>
10#include <boost/algorithm/string.hpp>
7c673cae
FG
11
12#include "rgw_swift_auth.h"
13#include "rgw_rest.h"
14
15#include "common/ceph_crypto.h"
16#include "common/Clock.h"
17
11fdf7f2 18#include "include/random.h"
7c673cae
FG
19
20#include "rgw_client_io.h"
21#include "rgw_http_client.h"
22#include "include/str_list.h"
23
24#define dout_context g_ceph_context
25#define dout_subsys ceph_subsys_rgw
26
27#define DEFAULT_SWIFT_PREFIX "/swift"
28
29using namespace ceph::crypto;
30
31
32namespace rgw {
33namespace auth {
34namespace swift {
35
36/* TempURL: applier */
11fdf7f2 37void TempURLApplier::modify_request_state(const DoutPrefixProvider* dpp, req_state* s) const /* in/out */
7c673cae
FG
38{
39 bool inline_exists = false;
40 const std::string& filename = s->info.args.get("filename");
41
42 s->info.args.get("inline", &inline_exists);
43 if (inline_exists) {
44 s->content_disp.override = "inline";
45 } else if (!filename.empty()) {
46 std::string fenc;
47 url_encode(filename, fenc);
48 s->content_disp.override = "attachment; filename=\"" + fenc + "\"";
49 } else {
50 std::string fenc;
51 url_encode(s->object.name, fenc);
52 s->content_disp.fallback = "attachment; filename=\"" + fenc + "\"";
53 }
54
11fdf7f2 55 ldpp_dout(dpp, 20) << "finished applying changes to req_state for TempURL: "
7c673cae
FG
56 << " content_disp override " << s->content_disp.override
57 << " content_disp fallback " << s->content_disp.fallback
58 << dendl;
59
60}
61
62/* TempURL: engine */
63bool TempURLEngine::is_applicable(const req_state* const s) const noexcept
64{
65 return s->info.args.exists("temp_url_sig") ||
66 s->info.args.exists("temp_url_expires");
67}
68
11fdf7f2 69void TempURLEngine::get_owner_info(const DoutPrefixProvider* dpp, const req_state* const s,
7c673cae
FG
70 RGWUserInfo& owner_info) const
71{
72 /* We cannot use req_state::bucket_name because it isn't available
73 * now. It will be initialized in RGWHandler_REST_SWIFT::postauth_init(). */
74 const string& bucket_name = s->init_state.url_bucket;
75
76 /* TempURL requires that bucket and object names are specified. */
77 if (bucket_name.empty() || s->object.empty()) {
78 throw -EPERM;
79 }
80
81 /* TempURL case is completely different than the Keystone auth - you may
82 * get account name only through extraction from URL. In turn, knowledge
83 * about account is neccessary to obtain its bucket tenant. Without that,
84 * the access would be limited to accounts with empty tenant. */
85 string bucket_tenant;
86 if (!s->account_name.empty()) {
87 RGWUserInfo uinfo;
88 bool found = false;
89
90 const rgw_user uid(s->account_name);
91 if (uid.tenant.empty()) {
92 const rgw_user tenanted_uid(uid.id, uid.id);
93
9f95a23c 94 if (ctl->user->get_info_by_uid(tenanted_uid, &uinfo, s->yield) >= 0) {
7c673cae
FG
95 /* Succeeded. */
96 bucket_tenant = uinfo.user_id.tenant;
97 found = true;
98 }
99 }
100
9f95a23c 101 if (!found && ctl->user->get_info_by_uid(uid, &uinfo, s->yield) < 0) {
7c673cae
FG
102 throw -EPERM;
103 } else {
104 bucket_tenant = uinfo.user_id.tenant;
105 }
106 }
107
9f95a23c
TL
108 rgw_bucket b;
109 b.tenant = std::move(bucket_tenant);
110 b.name = std::move(bucket_name);
111
7c673cae
FG
112 /* Need to get user info of bucket owner. */
113 RGWBucketInfo bucket_info;
9f95a23c
TL
114 RGWSI_MetaBackend_CtxParams bectx_params = RGWSI_MetaBackend_CtxParams_SObj(s->sysobj_ctx);
115 int ret = ctl->bucket->read_bucket_info(b, &bucket_info, null_yield, RGWBucketCtl::BucketInstance::GetParams()
116 .set_bectx_params(bectx_params));
7c673cae
FG
117 if (ret < 0) {
118 throw ret;
119 }
120
11fdf7f2 121 ldpp_dout(dpp, 20) << "temp url user (bucket owner): " << bucket_info.owner
7c673cae
FG
122 << dendl;
123
9f95a23c 124 if (ctl->user->get_info_by_uid(bucket_info.owner, &owner_info, s->yield) < 0) {
7c673cae
FG
125 throw -EPERM;
126 }
127}
128
11fdf7f2
TL
129std::string TempURLEngine::convert_from_iso8601(std::string expires) const
130{
131 /* Swift's TempURL allows clients to send the expiration as ISO8601-
132 * compatible strings. Though, only plain UNIX timestamp are taken
133 * for the HMAC calculations. We need to make the conversion. */
134 struct tm date_t;
135 if (!parse_iso8601(expires.c_str(), &date_t, nullptr, true)) {
9f95a23c 136 return expires;
11fdf7f2
TL
137 } else {
138 return std::to_string(internal_timegm(&date_t));
139 }
140}
141
7c673cae
FG
142bool TempURLEngine::is_expired(const std::string& expires) const
143{
144 string err;
145 const utime_t now = ceph_clock_now();
146 const uint64_t expiration = (uint64_t)strict_strtoll(expires.c_str(),
147 10, &err);
148 if (!err.empty()) {
149 dout(5) << "failed to parse temp_url_expires: " << err << dendl;
150 return true;
151 }
152
153 if (expiration <= (uint64_t)now.sec()) {
154 dout(5) << "temp url expired: " << expiration << " <= " << now.sec() << dendl;
155 return true;
156 }
157
158 return false;
159}
160
494da23a
TL
161bool TempURLEngine::is_disallowed_header_present(const req_info& info) const
162{
163 static const auto headers = {
164 "HTTP_X_OBJECT_MANIFEST",
165 };
166
167 return std::any_of(std::begin(headers), std::end(headers),
168 [&info](const char* header) {
169 return info.env->exists(header);
170 });
171}
172
173std::string extract_swift_subuser(const std::string& swift_user_name)
174{
7c673cae
FG
175 size_t pos = swift_user_name.find(':');
176 if (std::string::npos == pos) {
177 return swift_user_name;
178 } else {
179 return swift_user_name.substr(pos + 1);
180 }
181}
182
183class TempURLEngine::SignatureHelper
184{
185private:
186 static constexpr uint32_t output_size =
187 CEPH_CRYPTO_HMACSHA1_DIGESTSIZE * 2 + 1;
188
189 unsigned char dest[CEPH_CRYPTO_HMACSHA1_DIGESTSIZE]; // 20
190 char dest_str[output_size];
191
192public:
193 SignatureHelper() = default;
194
195 const char* calc(const std::string& key,
d2e6a577
FG
196 const boost::string_view& method,
197 const boost::string_view& path,
7c673cae
FG
198 const std::string& expires) {
199
200 using ceph::crypto::HMACSHA1;
201 using UCHARPTR = const unsigned char*;
202
203 HMACSHA1 hmac((UCHARPTR) key.c_str(), key.size());
204 hmac.Update((UCHARPTR) method.data(), method.size());
205 hmac.Update((UCHARPTR) "\n", 1);
206 hmac.Update((UCHARPTR) expires.c_str(), expires.size());
207 hmac.Update((UCHARPTR) "\n", 1);
208 hmac.Update((UCHARPTR) path.data(), path.size());
209 hmac.Final(dest);
210
211 buf_to_hex((UCHARPTR) dest, sizeof(dest), dest_str);
212
213 return dest_str;
214 }
215
216 bool is_equal_to(const std::string& rhs) const {
217 /* never allow out-of-range exception */
218 if (rhs.size() < (output_size - 1)) {
219 return false;
220 }
221 return rhs.compare(0 /* pos */, output_size, dest_str) == 0;
222 }
223
224}; /* TempURLEngine::SignatureHelper */
225
d2e6a577
FG
226class TempURLEngine::PrefixableSignatureHelper
227 : private TempURLEngine::SignatureHelper {
228 using base_t = SignatureHelper;
229
230 const boost::string_view decoded_uri;
231 const boost::string_view object_name;
232 boost::string_view no_obj_uri;
233
234 const boost::optional<const std::string&> prefix;
235
236public:
3efd9988 237 PrefixableSignatureHelper(const std::string& _decoded_uri,
d2e6a577
FG
238 const std::string& object_name,
239 const boost::optional<const std::string&> prefix)
3efd9988 240 : decoded_uri(_decoded_uri),
d2e6a577
FG
241 object_name(object_name),
242 prefix(prefix) {
3efd9988
FG
243 /* Transform: v1/acct/cont/obj - > v1/acct/cont/
244 *
245 * NOTE(rzarzynski): we really want to substr() on boost::string_view,
246 * not std::string. Otherwise we would end with no_obj_uri referencing
247 * a temporary. */
d2e6a577
FG
248 no_obj_uri = \
249 decoded_uri.substr(0, decoded_uri.length() - object_name.length());
250 }
251
252 const char* calc(const std::string& key,
253 const boost::string_view& method,
254 const boost::string_view& path,
255 const std::string& expires) {
256 if (!prefix) {
257 return base_t::calc(key, method, path, expires);
258 } else {
259 const auto prefixed_path = \
260 string_cat_reserve("prefix:", no_obj_uri, *prefix);
261 return base_t::calc(key, method, prefixed_path, expires);
262 }
263 }
264
265 bool is_equal_to(const std::string& rhs) const {
266 bool is_auth_ok = base_t::is_equal_to(rhs);
267
268 if (prefix && is_auth_ok) {
269 const auto prefix_uri = string_cat_reserve(no_obj_uri, *prefix);
270 is_auth_ok = boost::algorithm::starts_with(decoded_uri, prefix_uri);
271 }
272
273 return is_auth_ok;
274 }
275}; /* TempURLEngine::PrefixableSignatureHelper */
276
7c673cae 277TempURLEngine::result_t
11fdf7f2 278TempURLEngine::authenticate(const DoutPrefixProvider* dpp, const req_state* const s) const
7c673cae
FG
279{
280 if (! is_applicable(s)) {
281 return result_t::deny();
282 }
283
d2e6a577
FG
284 /* NOTE(rzarzynski): RGWHTTPArgs::get(), in contrast to RGWEnv::get(),
285 * never returns nullptr. If the requested parameter is absent, we will
286 * get the empty string. */
287 const std::string& temp_url_sig = s->info.args.get("temp_url_sig");
11fdf7f2
TL
288 const std::string& temp_url_expires = \
289 convert_from_iso8601(s->info.args.get("temp_url_expires"));
7c673cae
FG
290
291 if (temp_url_sig.empty() || temp_url_expires.empty()) {
292 return result_t::deny();
293 }
294
d2e6a577
FG
295 /* Though, for prefixed tempurls we need to differentiate between empty
296 * prefix and lack of prefix. Empty prefix means allowance for whole
297 * container. */
298 const boost::optional<const std::string&> temp_url_prefix = \
299 s->info.args.get_optional("temp_url_prefix");
300
7c673cae
FG
301 RGWUserInfo owner_info;
302 try {
11fdf7f2 303 get_owner_info(dpp, s, owner_info);
7c673cae 304 } catch (...) {
11fdf7f2 305 ldpp_dout(dpp, 5) << "cannot get user_info of account's owner" << dendl;
7c673cae
FG
306 return result_t::reject();
307 }
308
309 if (owner_info.temp_url_keys.empty()) {
11fdf7f2 310 ldpp_dout(dpp, 5) << "user does not have temp url key set, aborting" << dendl;
7c673cae
FG
311 return result_t::reject();
312 }
313
314 if (is_expired(temp_url_expires)) {
11fdf7f2 315 ldpp_dout(dpp, 5) << "temp url link expired" << dendl;
224ce89b 316 return result_t::reject(-EPERM);
7c673cae
FG
317 }
318
494da23a
TL
319 if (is_disallowed_header_present(s->info)) {
320 ldout(cct, 5) << "temp url rejected due to disallowed header" << dendl;
321 return result_t::reject(-EINVAL);
322 }
323
7c673cae
FG
324 /* We need to verify two paths because of compliance with Swift, Tempest
325 * and old versions of RadosGW. The second item will have the prefix
326 * of Swift API entry point removed. */
327
328 /* XXX can we search this ONCE? */
11fdf7f2 329 const size_t pos = g_conf()->rgw_swift_url_prefix.find_last_not_of('/') + 1;
d2e6a577
FG
330 const boost::string_view ref_uri = s->decoded_uri;
331 const std::array<boost::string_view, 2> allowed_paths = {
7c673cae
FG
332 ref_uri,
333 ref_uri.substr(pos + 1)
334 };
335
336 /* Account owner calculates the signature also against a HTTP method. */
d2e6a577 337 boost::container::static_vector<boost::string_view, 3> allowed_methods;
7c673cae
FG
338 if (strcmp("HEAD", s->info.method) == 0) {
339 /* HEAD requests are specially handled. */
340 /* TODO: after getting a newer boost (with static_vector supporting
341 * initializers lists), get back to the good notation:
342 * allowed_methods = {"HEAD", "GET", "PUT" };
343 * Just for now let's use emplace_back to construct the vector. */
344 allowed_methods.emplace_back("HEAD");
345 allowed_methods.emplace_back("GET");
346 allowed_methods.emplace_back("PUT");
347 } else if (strlen(s->info.method) > 0) {
348 allowed_methods.emplace_back(s->info.method);
349 }
350
351 /* Need to try each combination of keys, allowed path and methods. */
d2e6a577
FG
352 PrefixableSignatureHelper sig_helper {
353 s->decoded_uri,
354 s->object.name,
355 temp_url_prefix
356 };
357
7c673cae
FG
358 for (const auto& kv : owner_info.temp_url_keys) {
359 const int temp_url_key_num = kv.first;
360 const string& temp_url_key = kv.second;
361
362 if (temp_url_key.empty()) {
363 continue;
364 }
365
366 for (const auto& path : allowed_paths) {
367 for (const auto& method : allowed_methods) {
368 const char* const local_sig = sig_helper.calc(temp_url_key, method,
369 path, temp_url_expires);
370
11fdf7f2 371 ldpp_dout(dpp, 20) << "temp url signature [" << temp_url_key_num
7c673cae
FG
372 << "] (calculated): " << local_sig
373 << dendl;
374
375 if (sig_helper.is_equal_to(temp_url_sig)) {
376 auto apl = apl_factory->create_apl_turl(cct, s, owner_info);
377 return result_t::grant(std::move(apl));
378 } else {
11fdf7f2 379 ldpp_dout(dpp, 5) << "temp url signature mismatch: " << local_sig
7c673cae
FG
380 << " != " << temp_url_sig << dendl;
381 }
382 }
383 }
384 }
385
386 return result_t::reject();
387}
388
389
390/* External token */
391bool ExternalTokenEngine::is_applicable(const std::string& token) const noexcept
392{
393 if (token.empty()) {
394 return false;
11fdf7f2 395 } else if (g_conf()->rgw_swift_auth_url.empty()) {
7c673cae
FG
396 return false;
397 } else {
398 return true;
399 }
400}
401
402ExternalTokenEngine::result_t
11fdf7f2
TL
403ExternalTokenEngine::authenticate(const DoutPrefixProvider* dpp,
404 const std::string& token,
7c673cae
FG
405 const req_state* const s) const
406{
407 if (! is_applicable(token)) {
408 return result_t::deny();
409 }
410
11fdf7f2 411 std::string auth_url = g_conf()->rgw_swift_auth_url;
31f18b77 412 if (auth_url.back() != '/') {
7c673cae
FG
413 auth_url.append("/");
414 }
415
416 auth_url.append("token");
417 char url_buf[auth_url.size() + 1 + token.length() + 1];
418 sprintf(url_buf, "%s/%s", auth_url.c_str(), token.c_str());
419
11fdf7f2 420 RGWHTTPHeadersCollector validator(cct, "GET", url_buf, { "X-Auth-Groups", "X-Auth-Ttl" });
7c673cae 421
11fdf7f2 422 ldpp_dout(dpp, 10) << "rgw_swift_validate_token url=" << url_buf << dendl;
7c673cae 423
9f95a23c 424 int ret = validator.process(null_yield);
7c673cae
FG
425 if (ret < 0) {
426 throw ret;
427 }
428
429 std::string swift_user;
430 try {
431 std::vector<std::string> swift_groups;
432 get_str_vec(validator.get_header_value("X-Auth-Groups"),
433 ",", swift_groups);
434
435 if (0 == swift_groups.size()) {
31f18b77 436 return result_t::deny(-EPERM);
7c673cae
FG
437 } else {
438 swift_user = std::move(swift_groups[0]);
439 }
11fdf7f2 440 } catch (const std::out_of_range&) {
7c673cae 441 /* The X-Auth-Groups header isn't present in the response. */
31f18b77 442 return result_t::deny(-EPERM);
7c673cae
FG
443 }
444
445 if (swift_user.empty()) {
31f18b77 446 return result_t::deny(-EPERM);
7c673cae
FG
447 }
448
11fdf7f2 449 ldpp_dout(dpp, 10) << "swift user=" << swift_user << dendl;
7c673cae
FG
450
451 RGWUserInfo tmp_uinfo;
9f95a23c 452 ret = ctl->user->get_info_by_swift(swift_user, &tmp_uinfo, s->yield);
7c673cae 453 if (ret < 0) {
11fdf7f2 454 ldpp_dout(dpp, 0) << "NOTICE: couldn't map swift user" << dendl;
7c673cae
FG
455 throw ret;
456 }
457
458 auto apl = apl_factory->create_apl_local(cct, s, tmp_uinfo,
11fdf7f2
TL
459 extract_swift_subuser(swift_user),
460 boost::none);
7c673cae
FG
461 return result_t::grant(std::move(apl));
462}
463
464static int build_token(const string& swift_user,
465 const string& key,
466 const uint64_t nonce,
467 const utime_t& expiration,
468 bufferlist& bl)
469{
11fdf7f2
TL
470 using ceph::encode;
471 encode(swift_user, bl);
472 encode(nonce, bl);
473 encode(expiration, bl);
7c673cae
FG
474
475 bufferptr p(CEPH_CRYPTO_HMACSHA1_DIGESTSIZE);
476
477 char buf[bl.length() * 2 + 1];
478 buf_to_hex((const unsigned char *)bl.c_str(), bl.length(), buf);
479 dout(20) << "build_token token=" << buf << dendl;
480
481 char k[CEPH_CRYPTO_HMACSHA1_DIGESTSIZE];
92f5a8d4
TL
482 // FIPS zeroization audit 20191116: this memset is not intended to
483 // wipe out a secret after use.
7c673cae
FG
484 memset(k, 0, sizeof(k));
485 const char *s = key.c_str();
486 for (int i = 0; i < (int)key.length(); i++, s++) {
487 k[i % CEPH_CRYPTO_HMACSHA1_DIGESTSIZE] |= *s;
488 }
489 calc_hmac_sha1(k, sizeof(k), bl.c_str(), bl.length(), p.c_str());
92f5a8d4 490 ::ceph::crypto::zeroize_for_security(k, sizeof(k));
7c673cae
FG
491
492 bl.append(p);
493
494 return 0;
495
496}
497
498static int encode_token(CephContext *cct, string& swift_user, string& key,
499 bufferlist& bl)
500{
11fdf7f2 501 const auto nonce = ceph::util::generate_random_number<uint64_t>();
7c673cae
FG
502
503 utime_t expiration = ceph_clock_now();
504 expiration += cct->_conf->rgw_swift_token_expiration;
505
506 return build_token(swift_user, key, nonce, expiration, bl);
507}
508
509
510/* AUTH_rgwtk (signed token): engine */
511bool SignedTokenEngine::is_applicable(const std::string& token) const noexcept
512{
513 if (token.empty()) {
514 return false;
515 } else {
516 return token.compare(0, 10, "AUTH_rgwtk") == 0;
517 }
518}
519
520SignedTokenEngine::result_t
11fdf7f2
TL
521SignedTokenEngine::authenticate(const DoutPrefixProvider* dpp,
522 const std::string& token,
7c673cae
FG
523 const req_state* const s) const
524{
525 if (! is_applicable(token)) {
31f18b77 526 return result_t::deny(-EPERM);
7c673cae
FG
527 }
528
529 /* Effective token string is the part after the prefix. */
530 const std::string etoken = token.substr(strlen("AUTH_rgwtk"));
531 const size_t etoken_len = etoken.length();
532
533 if (etoken_len & 1) {
11fdf7f2 534 ldpp_dout(dpp, 0) << "NOTICE: failed to verify token: odd token length="
7c673cae
FG
535 << etoken_len << dendl;
536 throw -EINVAL;
537 }
538
539 ceph::bufferptr p(etoken_len/2);
540 int ret = hex_to_buf(etoken.c_str(), p.c_str(), etoken_len);
541 if (ret < 0) {
542 throw ret;
543 }
544
545 ceph::bufferlist tok_bl;
546 tok_bl.append(p);
547
548 uint64_t nonce;
549 utime_t expiration;
550 std::string swift_user;
551
552 try {
11fdf7f2 553 auto iter = tok_bl.cbegin();
7c673cae 554
11fdf7f2
TL
555 using ceph::decode;
556 decode(swift_user, iter);
557 decode(nonce, iter);
558 decode(expiration, iter);
7c673cae 559 } catch (buffer::error& err) {
11fdf7f2 560 ldpp_dout(dpp, 0) << "NOTICE: failed to decode token" << dendl;
7c673cae
FG
561 throw -EINVAL;
562 }
563
564 const utime_t now = ceph_clock_now();
565 if (expiration < now) {
11fdf7f2 566 ldpp_dout(dpp, 0) << "NOTICE: old timed out token was used now=" << now
7c673cae
FG
567 << " token.expiration=" << expiration
568 << dendl;
31f18b77 569 return result_t::deny(-EPERM);
7c673cae
FG
570 }
571
572 RGWUserInfo user_info;
9f95a23c 573 ret = ctl->user->get_info_by_swift(swift_user, &user_info, s->yield);
7c673cae
FG
574 if (ret < 0) {
575 throw ret;
576 }
577
11fdf7f2 578 ldpp_dout(dpp, 10) << "swift_user=" << swift_user << dendl;
7c673cae
FG
579
580 const auto siter = user_info.swift_keys.find(swift_user);
581 if (siter == std::end(user_info.swift_keys)) {
31f18b77 582 return result_t::deny(-EPERM);
7c673cae
FG
583 }
584
585 const auto swift_key = siter->second;
586
587 bufferlist local_tok_bl;
588 ret = build_token(swift_user, swift_key.key, nonce, expiration, local_tok_bl);
589 if (ret < 0) {
590 throw ret;
591 }
592
593 if (local_tok_bl.length() != tok_bl.length()) {
11fdf7f2 594 ldpp_dout(dpp, 0) << "NOTICE: tokens length mismatch:"
7c673cae
FG
595 << " tok_bl.length()=" << tok_bl.length()
596 << " local_tok_bl.length()=" << local_tok_bl.length()
597 << dendl;
31f18b77 598 return result_t::deny(-EPERM);
7c673cae
FG
599 }
600
601 if (memcmp(local_tok_bl.c_str(), tok_bl.c_str(),
602 local_tok_bl.length()) != 0) {
603 char buf[local_tok_bl.length() * 2 + 1];
604
605 buf_to_hex(reinterpret_cast<const unsigned char *>(local_tok_bl.c_str()),
606 local_tok_bl.length(), buf);
607
11fdf7f2 608 ldpp_dout(dpp, 0) << "NOTICE: tokens mismatch tok=" << buf << dendl;
31f18b77 609 return result_t::deny(-EPERM);
7c673cae
FG
610 }
611
612 auto apl = apl_factory->create_apl_local(cct, s, user_info,
11fdf7f2
TL
613 extract_swift_subuser(swift_user),
614 boost::none);
7c673cae
FG
615 return result_t::grant(std::move(apl));
616}
617
618} /* namespace swift */
619} /* namespace auth */
620} /* namespace rgw */
621
622
623void RGW_SWIFT_Auth_Get::execute()
624{
625 int ret = -EPERM;
626
627 const char *key = s->info.env->get("HTTP_X_AUTH_KEY");
628 const char *user = s->info.env->get("HTTP_X_AUTH_USER");
629
630 s->prot_flags |= RGW_REST_SWIFT;
631
632 string user_str;
633 RGWUserInfo info;
634 bufferlist bl;
635 RGWAccessKey *swift_key;
636 map<string, RGWAccessKey>::iterator siter;
637
11fdf7f2
TL
638 string swift_url = g_conf()->rgw_swift_url;
639 string swift_prefix = g_conf()->rgw_swift_url_prefix;
7c673cae
FG
640 string tenant_path;
641
642 /*
643 * We did not allow an empty Swift prefix before, but we want it now.
644 * So, we take rgw_swift_url_prefix = "/" to yield the empty prefix.
645 * The rgw_swift_url_prefix = "" is the default and yields "/swift"
646 * in a backwards-compatible way.
647 */
648 if (swift_prefix.size() == 0) {
649 swift_prefix = DEFAULT_SWIFT_PREFIX;
650 } else if (swift_prefix == "/") {
651 swift_prefix.clear();
652 } else {
653 if (swift_prefix[0] != '/') {
654 swift_prefix.insert(0, "/");
655 }
656 }
657
658 if (swift_url.size() == 0) {
659 bool add_port = false;
660 const char *server_port = s->info.env->get("SERVER_PORT_SECURE");
661 const char *protocol;
662 if (server_port) {
663 add_port = (strcmp(server_port, "443") != 0);
664 protocol = "https";
665 } else {
666 server_port = s->info.env->get("SERVER_PORT");
667 add_port = (strcmp(server_port, "80") != 0);
668 protocol = "http";
669 }
670 const char *host = s->info.env->get("HTTP_HOST");
671 if (!host) {
672 dout(0) << "NOTICE: server is misconfigured, missing rgw_swift_url_prefix or rgw_swift_url, HTTP_HOST is not set" << dendl;
673 ret = -EINVAL;
674 goto done;
675 }
676 swift_url = protocol;
677 swift_url.append("://");
678 swift_url.append(host);
679 if (add_port && !strchr(host, ':')) {
680 swift_url.append(":");
681 swift_url.append(server_port);
682 }
683 }
684
685 if (!key || !user)
686 goto done;
687
688 user_str = user;
689
9f95a23c 690 if ((ret = store->ctl()->user->get_info_by_swift(user_str, &info, s->yield)) < 0)
7c673cae
FG
691 {
692 ret = -EACCES;
693 goto done;
694 }
695
696 siter = info.swift_keys.find(user_str);
697 if (siter == info.swift_keys.end()) {
698 ret = -EPERM;
699 goto done;
700 }
701 swift_key = &siter->second;
702
703 if (swift_key->key.compare(key) != 0) {
704 dout(0) << "NOTICE: RGW_SWIFT_Auth_Get::execute(): bad swift key" << dendl;
705 ret = -EPERM;
706 goto done;
707 }
708
11fdf7f2 709 if (!g_conf()->rgw_swift_tenant_name.empty()) {
7c673cae 710 tenant_path = "/AUTH_";
11fdf7f2
TL
711 tenant_path.append(g_conf()->rgw_swift_tenant_name);
712 } else if (g_conf()->rgw_swift_account_in_url) {
7c673cae
FG
713 tenant_path = "/AUTH_";
714 tenant_path.append(info.user_id.to_str());
715 }
716
717 dump_header(s, "X-Storage-Url", swift_url + swift_prefix + "/v1" +
718 tenant_path);
719
720 using rgw::auth::swift::encode_token;
721 if ((ret = encode_token(s->cct, swift_key->id, swift_key->key, bl)) < 0)
722 goto done;
723
724 {
725 static constexpr size_t PREFIX_LEN = sizeof("AUTH_rgwtk") - 1;
726 char token_val[PREFIX_LEN + bl.length() * 2 + 1];
727
728 snprintf(token_val, PREFIX_LEN + 1, "AUTH_rgwtk");
729 buf_to_hex((const unsigned char *)bl.c_str(), bl.length(),
730 token_val + PREFIX_LEN);
731
732 dump_header(s, "X-Storage-Token", token_val);
733 dump_header(s, "X-Auth-Token", token_val);
734 }
735
736 ret = STATUS_NO_CONTENT;
737
738done:
739 set_req_state_err(s, ret);
740 dump_errno(s);
741 end_header(s);
742}
743
9f95a23c 744int RGWHandler_SWIFT_Auth::init(rgw::sal::RGWRadosStore *store, struct req_state *state,
7c673cae
FG
745 rgw::io::BasicClient *cio)
746{
747 state->dialect = "swift-auth";
748 state->formatter = new JSONFormatter;
749 state->format = RGW_FORMAT_JSON;
750
751 return RGWHandler::init(store, state, cio);
752}
753
11fdf7f2 754int RGWHandler_SWIFT_Auth::authorize(const DoutPrefixProvider *dpp)
7c673cae
FG
755{
756 return 0;
757}
758
759RGWOp *RGWHandler_SWIFT_Auth::op_get()
760{
761 return new RGW_SWIFT_Auth_Get;
762}
763