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