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