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