]> git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/rgw_rest_swift.h
48d306cde6d1ec0de8bd4cf8f4fdff59b2c45a1a
[ceph.git] / ceph / src / rgw / rgw_rest_swift.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #ifndef CEPH_RGW_REST_SWIFT_H
5 #define CEPH_RGW_REST_SWIFT_H
6 #define TIME_BUF_SIZE 128
7
8 #include <boost/optional.hpp>
9 #include <boost/utility/typed_in_place_factory.hpp>
10
11 #include "rgw_op.h"
12 #include "rgw_rest.h"
13 #include "rgw_swift_auth.h"
14 #include "rgw_http_errors.h"
15
16 #include <boost/utility/string_ref.hpp>
17
18 class RGWGetObj_ObjStore_SWIFT : public RGWGetObj_ObjStore {
19 int custom_http_ret = 0;
20 public:
21 RGWGetObj_ObjStore_SWIFT() {}
22 ~RGWGetObj_ObjStore_SWIFT() override {}
23
24 int verify_permission() override;
25 int get_params() override;
26 int send_response_data_error() override;
27 int send_response_data(bufferlist& bl, off_t ofs, off_t len) override;
28
29 void set_custom_http_response(const int http_ret) {
30 custom_http_ret = http_ret;
31 }
32
33 bool need_object_expiration() override {
34 return true;
35 }
36 };
37
38 class RGWListBuckets_ObjStore_SWIFT : public RGWListBuckets_ObjStore {
39 bool need_stats;
40 std::string prefix;
41
42 uint64_t get_default_max() const override {
43 return 0;
44 }
45 public:
46 RGWListBuckets_ObjStore_SWIFT() : need_stats(true) {}
47 ~RGWListBuckets_ObjStore_SWIFT() override {}
48
49 int get_params() override;
50 void send_response_begin(bool has_buckets) override;
51 void send_response_data(RGWUserBuckets& buckets) override;
52 void send_response_end() override;
53
54 bool should_get_stats() override { return need_stats; }
55 bool supports_account_metadata() override { return true; }
56 };
57
58 class RGWListBucket_ObjStore_SWIFT : public RGWListBucket_ObjStore {
59 string path;
60 public:
61 RGWListBucket_ObjStore_SWIFT() {
62 default_max = 10000;
63 }
64 ~RGWListBucket_ObjStore_SWIFT() override {}
65
66 int get_params() override;
67 void send_response() override;
68 bool need_container_stats() override { return true; }
69 };
70
71 class RGWStatAccount_ObjStore_SWIFT : public RGWStatAccount_ObjStore {
72 map<string, bufferlist> attrs;
73 public:
74 RGWStatAccount_ObjStore_SWIFT() {
75 }
76 ~RGWStatAccount_ObjStore_SWIFT() override {}
77
78 void execute() override;
79 void send_response() override;
80 };
81
82 class RGWStatBucket_ObjStore_SWIFT : public RGWStatBucket_ObjStore {
83 public:
84 RGWStatBucket_ObjStore_SWIFT() {}
85 ~RGWStatBucket_ObjStore_SWIFT() override {}
86
87 void send_response() override;
88 };
89
90 class RGWCreateBucket_ObjStore_SWIFT : public RGWCreateBucket_ObjStore {
91 protected:
92 bool need_metadata_upload() const override { return true; }
93 public:
94 RGWCreateBucket_ObjStore_SWIFT() {}
95 ~RGWCreateBucket_ObjStore_SWIFT() override {}
96
97 int get_params() override;
98 void send_response() override;
99 };
100
101 class RGWDeleteBucket_ObjStore_SWIFT : public RGWDeleteBucket_ObjStore {
102 public:
103 RGWDeleteBucket_ObjStore_SWIFT() {}
104 ~RGWDeleteBucket_ObjStore_SWIFT() override {}
105
106 void send_response() override;
107 };
108
109 class RGWPutObj_ObjStore_SWIFT : public RGWPutObj_ObjStore {
110 string lo_etag;
111 public:
112 RGWPutObj_ObjStore_SWIFT() {}
113 ~RGWPutObj_ObjStore_SWIFT() override {}
114
115 int verify_permission() override;
116 int get_params() override;
117 void send_response() override;
118 };
119
120 class RGWPutMetadataAccount_ObjStore_SWIFT : public RGWPutMetadataAccount_ObjStore {
121 public:
122 RGWPutMetadataAccount_ObjStore_SWIFT() {}
123 ~RGWPutMetadataAccount_ObjStore_SWIFT() override {}
124
125 int get_params() override;
126 void send_response() override;
127 };
128
129 class RGWPutMetadataBucket_ObjStore_SWIFT : public RGWPutMetadataBucket_ObjStore {
130 public:
131 RGWPutMetadataBucket_ObjStore_SWIFT() {}
132 ~RGWPutMetadataBucket_ObjStore_SWIFT() override {}
133
134 int get_params() override;
135 void send_response() override;
136 };
137
138 class RGWPutMetadataObject_ObjStore_SWIFT : public RGWPutMetadataObject_ObjStore {
139 public:
140 RGWPutMetadataObject_ObjStore_SWIFT() {}
141 ~RGWPutMetadataObject_ObjStore_SWIFT() override {}
142
143 int get_params() override;
144 void send_response() override;
145 bool need_object_expiration() override { return true; }
146 };
147
148 class RGWDeleteObj_ObjStore_SWIFT : public RGWDeleteObj_ObjStore {
149 public:
150 RGWDeleteObj_ObjStore_SWIFT() {}
151 ~RGWDeleteObj_ObjStore_SWIFT() override {}
152
153 int verify_permission() override;
154 int get_params() override;
155 bool need_object_expiration() override { return true; }
156 void send_response() override;
157 };
158
159 class RGWCopyObj_ObjStore_SWIFT : public RGWCopyObj_ObjStore {
160 bool sent_header;
161 protected:
162 void dump_copy_info();
163 public:
164 RGWCopyObj_ObjStore_SWIFT() : sent_header(false) {}
165 ~RGWCopyObj_ObjStore_SWIFT() override {}
166
167 int init_dest_policy() override;
168 int get_params() override;
169 void send_response() override;
170 void send_partial_response(off_t ofs) override;
171 };
172
173 class RGWGetACLs_ObjStore_SWIFT : public RGWGetACLs_ObjStore {
174 public:
175 RGWGetACLs_ObjStore_SWIFT() {}
176 ~RGWGetACLs_ObjStore_SWIFT() override {}
177
178 void send_response() override {}
179 };
180
181 class RGWPutACLs_ObjStore_SWIFT : public RGWPutACLs_ObjStore {
182 public:
183 RGWPutACLs_ObjStore_SWIFT() : RGWPutACLs_ObjStore() {}
184 ~RGWPutACLs_ObjStore_SWIFT() override {}
185
186 void send_response() override {}
187 };
188
189 class RGWOptionsCORS_ObjStore_SWIFT : public RGWOptionsCORS_ObjStore {
190 public:
191 RGWOptionsCORS_ObjStore_SWIFT() {}
192 ~RGWOptionsCORS_ObjStore_SWIFT() override {}
193
194 void send_response() override;
195 };
196
197 class RGWBulkDelete_ObjStore_SWIFT : public RGWBulkDelete_ObjStore {
198 public:
199 RGWBulkDelete_ObjStore_SWIFT() {}
200 ~RGWBulkDelete_ObjStore_SWIFT() override {}
201
202 int get_data(std::list<RGWBulkDelete::acct_path_t>& items,
203 bool * is_truncated) override;
204 void send_response() override;
205 };
206
207 class RGWBulkUploadOp_ObjStore_SWIFT : public RGWBulkUploadOp_ObjStore {
208 size_t conlen;
209 size_t curpos;
210
211 public:
212 RGWBulkUploadOp_ObjStore_SWIFT()
213 : conlen(0),
214 curpos(0) {
215 }
216 ~RGWBulkUploadOp_ObjStore_SWIFT() = default;
217
218 std::unique_ptr<StreamGetter> create_stream() override;
219 void send_response() override;
220 };
221
222 class RGWInfo_ObjStore_SWIFT : public RGWInfo_ObjStore {
223 protected:
224 struct info
225 {
226 bool is_admin_info;
227 function<void (Formatter&, const md_config_t&, RGWRados&)> list_data;
228 };
229
230 static const vector<pair<string, struct info>> swift_info;
231 public:
232 RGWInfo_ObjStore_SWIFT() {}
233 ~RGWInfo_ObjStore_SWIFT() override {}
234
235 void execute() override;
236 void send_response() override;
237 static void list_swift_data(Formatter& formatter, const md_config_t& config, RGWRados& store);
238 static void list_tempurl_data(Formatter& formatter, const md_config_t& config, RGWRados& store);
239 static void list_slo_data(Formatter& formatter, const md_config_t& config, RGWRados& store);
240 static bool is_expired(const std::string& expires, CephContext* cct);
241 };
242
243
244 class RGWFormPost : public RGWPostObj_ObjStore {
245 std::string get_current_filename() const override;
246 std::string get_current_content_type() const override;
247 std::size_t get_max_file_size() /*const*/;
248 bool is_next_file_to_upload() override;
249 bool is_integral();
250 bool is_non_expired();
251 void get_owner_info(const req_state* s,
252 RGWUserInfo& owner_info) const;
253
254 parts_collection_t ctrl_parts;
255 boost::optional<post_form_part> current_data_part;
256 std::string prefix;
257 bool stream_done = false;
258
259 class SignatureHelper;
260 public:
261 RGWFormPost() = default;
262 ~RGWFormPost() = default;
263
264 void init(RGWRados* store,
265 req_state* s,
266 RGWHandler* dialect_handler) override;
267
268 int get_params() override;
269 int get_data(ceph::bufferlist& bl, bool& again) override;
270 void send_response() override;
271
272 static bool is_formpost_req(req_state* const s);
273 };
274
275 class RGWFormPost::SignatureHelper
276 {
277 private:
278 static constexpr uint32_t output_size =
279 CEPH_CRYPTO_HMACSHA1_DIGESTSIZE * 2 + 1;
280
281 unsigned char dest[CEPH_CRYPTO_HMACSHA1_DIGESTSIZE]; // 20
282 char dest_str[output_size];
283
284 public:
285 SignatureHelper() = default;
286
287 const char* calc(const std::string& key,
288 const boost::string_ref& path_info,
289 const boost::string_ref& redirect,
290 const boost::string_ref& max_file_size,
291 const boost::string_ref& max_file_count,
292 const boost::string_ref& expires) {
293 using ceph::crypto::HMACSHA1;
294 using UCHARPTR = const unsigned char*;
295
296 HMACSHA1 hmac((UCHARPTR) key.data(), key.size());
297
298 hmac.Update((UCHARPTR) path_info.data(), path_info.size());
299 hmac.Update((UCHARPTR) "\n", 1);
300
301 hmac.Update((UCHARPTR) redirect.data(), redirect.size());
302 hmac.Update((UCHARPTR) "\n", 1);
303
304 hmac.Update((UCHARPTR) max_file_size.data(), max_file_size.size());
305 hmac.Update((UCHARPTR) "\n", 1);
306
307 hmac.Update((UCHARPTR) max_file_count.data(), max_file_count.size());
308 hmac.Update((UCHARPTR) "\n", 1);
309
310 hmac.Update((UCHARPTR) expires.data(), expires.size());
311
312 hmac.Final(dest);
313
314 buf_to_hex((UCHARPTR) dest, sizeof(dest), dest_str);
315
316 return dest_str;
317 }
318
319 const char* get_signature() const {
320 return dest_str;
321 }
322
323 bool is_equal_to(const std::string& rhs) const {
324 /* never allow out-of-range exception */
325 if (rhs.size() < (output_size - 1)) {
326 return false;
327 }
328 return rhs.compare(0 /* pos */, output_size, dest_str) == 0;
329 }
330
331 }; /* RGWFormPost::SignatureHelper */
332
333
334 class RGWSwiftWebsiteHandler {
335 RGWRados* const store;
336 req_state* const s;
337 RGWHandler_REST* const handler;
338
339 bool is_web_mode() const;
340 bool can_be_website_req() const;
341 bool is_web_dir() const;
342 bool is_index_present(const std::string& index);
343
344 int serve_errordoc(int http_ret, std::string error_doc);
345
346 RGWOp* get_ws_redirect_op();
347 RGWOp* get_ws_index_op();
348 RGWOp* get_ws_listing_op();
349 public:
350 RGWSwiftWebsiteHandler(RGWRados* const store,
351 req_state* const s,
352 RGWHandler_REST* const handler)
353 : store(store),
354 s(s),
355 handler(handler) {
356 }
357
358 int error_handler(const int err_no,
359 std::string* const error_content);
360 int retarget_bucket(RGWOp* op, RGWOp** new_op);
361 int retarget_object(RGWOp* op, RGWOp** new_op);
362 };
363
364
365 class RGWHandler_REST_SWIFT : public RGWHandler_REST {
366 friend class RGWRESTMgr_SWIFT;
367 friend class RGWRESTMgr_SWIFT_Info;
368 protected:
369 const rgw::auth::Strategy& auth_strategy;
370
371 virtual bool is_acl_op() {
372 return false;
373 }
374
375 static int init_from_header(struct req_state* s,
376 const std::string& frontend_prefix);
377 public:
378 RGWHandler_REST_SWIFT(const rgw::auth::Strategy& auth_strategy)
379 : auth_strategy(auth_strategy) {
380 }
381 ~RGWHandler_REST_SWIFT() override = default;
382
383 static int validate_bucket_name(const string& bucket);
384
385 int init(RGWRados *store, struct req_state *s, rgw::io::BasicClient *cio) override;
386 int authorize() override;
387 int postauth_init() override;
388
389 RGWAccessControlPolicy *alloc_policy() { return nullptr; /* return new RGWAccessControlPolicy_SWIFT; */ }
390 void free_policy(RGWAccessControlPolicy *policy) { delete policy; }
391 };
392
393 class RGWHandler_REST_Service_SWIFT : public RGWHandler_REST_SWIFT {
394 protected:
395 RGWOp *op_get() override;
396 RGWOp *op_head() override;
397 RGWOp *op_put() override;
398 RGWOp *op_post() override;
399 RGWOp *op_delete() override;
400 public:
401 using RGWHandler_REST_SWIFT::RGWHandler_REST_SWIFT;
402 ~RGWHandler_REST_Service_SWIFT() override = default;
403 };
404
405 class RGWHandler_REST_Bucket_SWIFT : public RGWHandler_REST_SWIFT {
406 /* We need the boost::optional here only because of handler's late
407 * initialization (see the init() method). */
408 boost::optional<RGWSwiftWebsiteHandler> website_handler;
409 protected:
410 bool is_obj_update_op() override {
411 return s->op == OP_POST;
412 }
413
414 RGWOp *get_obj_op(bool get_data);
415 RGWOp *op_get() override;
416 RGWOp *op_head() override;
417 RGWOp *op_put() override;
418 RGWOp *op_delete() override;
419 RGWOp *op_post() override;
420 RGWOp *op_options() override;
421 public:
422 using RGWHandler_REST_SWIFT::RGWHandler_REST_SWIFT;
423 ~RGWHandler_REST_Bucket_SWIFT() override = default;
424
425 int error_handler(int err_no, std::string *error_content) override {
426 return website_handler->error_handler(err_no, error_content);
427 }
428
429 int retarget(RGWOp* op, RGWOp** new_op) override {
430 return website_handler->retarget_bucket(op, new_op);
431 }
432
433 int init(RGWRados* const store,
434 struct req_state* const s,
435 rgw::io::BasicClient* const cio) override {
436 website_handler = boost::in_place<RGWSwiftWebsiteHandler>(store, s, this);
437 return RGWHandler_REST_SWIFT::init(store, s, cio);
438 }
439 };
440
441 class RGWHandler_REST_Obj_SWIFT : public RGWHandler_REST_SWIFT {
442 /* We need the boost::optional here only because of handler's late
443 * initialization (see the init() method). */
444 boost::optional<RGWSwiftWebsiteHandler> website_handler;
445 protected:
446 bool is_obj_update_op() override {
447 return s->op == OP_POST;
448 }
449
450 RGWOp *get_obj_op(bool get_data);
451 RGWOp *op_get() override;
452 RGWOp *op_head() override;
453 RGWOp *op_put() override;
454 RGWOp *op_delete() override;
455 RGWOp *op_post() override;
456 RGWOp *op_copy() override;
457 RGWOp *op_options() override;
458
459 public:
460 using RGWHandler_REST_SWIFT::RGWHandler_REST_SWIFT;
461 ~RGWHandler_REST_Obj_SWIFT() override = default;
462
463 int error_handler(int err_no, std::string *error_content) override {
464 return website_handler->error_handler(err_no, error_content);
465 }
466
467 int retarget(RGWOp* op, RGWOp** new_op) override {
468 return website_handler->retarget_object(op, new_op);
469 }
470
471 int init(RGWRados* const store,
472 struct req_state* const s,
473 rgw::io::BasicClient* const cio) override {
474 website_handler = boost::in_place<RGWSwiftWebsiteHandler>(store, s, this);
475 return RGWHandler_REST_SWIFT::init(store, s, cio);
476 }
477 };
478
479 class RGWRESTMgr_SWIFT : public RGWRESTMgr {
480 protected:
481 RGWRESTMgr* get_resource_mgr_as_default(struct req_state* const s,
482 const std::string& uri,
483 std::string* const out_uri) override {
484 return this->get_resource_mgr(s, uri, out_uri);
485 }
486
487 public:
488 RGWRESTMgr_SWIFT() = default;
489 ~RGWRESTMgr_SWIFT() override = default;
490
491 RGWHandler_REST *get_handler(struct req_state *s,
492 const rgw::auth::StrategyRegistry& auth_registry,
493 const std::string& frontend_prefix) override;
494 };
495
496
497 class RGWGetCrossDomainPolicy_ObjStore_SWIFT
498 : public RGWGetCrossDomainPolicy_ObjStore {
499 public:
500 RGWGetCrossDomainPolicy_ObjStore_SWIFT() = default;
501 ~RGWGetCrossDomainPolicy_ObjStore_SWIFT() override = default;
502
503 void send_response() override;
504 };
505
506 class RGWGetHealthCheck_ObjStore_SWIFT
507 : public RGWGetHealthCheck_ObjStore {
508 public:
509 RGWGetHealthCheck_ObjStore_SWIFT() = default;
510 ~RGWGetHealthCheck_ObjStore_SWIFT() override = default;
511
512 void send_response() override;
513 };
514
515 class RGWHandler_SWIFT_CrossDomain : public RGWHandler_REST {
516 public:
517 RGWHandler_SWIFT_CrossDomain() = default;
518 ~RGWHandler_SWIFT_CrossDomain() override = default;
519
520 RGWOp *op_get() override {
521 return new RGWGetCrossDomainPolicy_ObjStore_SWIFT();
522 }
523
524 int init(RGWRados* const store,
525 struct req_state* const state,
526 rgw::io::BasicClient* const cio) override {
527 state->dialect = "swift";
528 state->formatter = new JSONFormatter;
529 state->format = RGW_FORMAT_JSON;
530
531 return RGWHandler::init(store, state, cio);
532 }
533
534 int authorize() override {
535 return 0;
536 }
537
538 int postauth_init() override {
539 return 0;
540 }
541
542 int read_permissions(RGWOp *) override {
543 return 0;
544 }
545
546 virtual RGWAccessControlPolicy *alloc_policy() { return nullptr; }
547 virtual void free_policy(RGWAccessControlPolicy *policy) {}
548 };
549
550 class RGWRESTMgr_SWIFT_CrossDomain : public RGWRESTMgr {
551 protected:
552 RGWRESTMgr *get_resource_mgr(struct req_state* const s,
553 const std::string& uri,
554 std::string* const out_uri) override {
555 return this;
556 }
557
558 public:
559 RGWRESTMgr_SWIFT_CrossDomain() = default;
560 ~RGWRESTMgr_SWIFT_CrossDomain() override = default;
561
562 RGWHandler_REST* get_handler(struct req_state* const s,
563 const rgw::auth::StrategyRegistry&,
564 const std::string&) override {
565 s->prot_flags |= RGW_REST_SWIFT;
566 return new RGWHandler_SWIFT_CrossDomain;
567 }
568 };
569
570
571 class RGWHandler_SWIFT_HealthCheck : public RGWHandler_REST {
572 public:
573 RGWHandler_SWIFT_HealthCheck() = default;
574 ~RGWHandler_SWIFT_HealthCheck() override = default;
575
576 RGWOp *op_get() override {
577 return new RGWGetHealthCheck_ObjStore_SWIFT();
578 }
579
580 int init(RGWRados* const store,
581 struct req_state* const state,
582 rgw::io::BasicClient* const cio) override {
583 state->dialect = "swift";
584 state->formatter = new JSONFormatter;
585 state->format = RGW_FORMAT_JSON;
586
587 return RGWHandler::init(store, state, cio);
588 }
589
590 int authorize() override {
591 return 0;
592 }
593
594 int postauth_init() override {
595 return 0;
596 }
597
598 int read_permissions(RGWOp *) override {
599 return 0;
600 }
601
602 virtual RGWAccessControlPolicy *alloc_policy() { return nullptr; }
603 virtual void free_policy(RGWAccessControlPolicy *policy) {}
604 };
605
606 class RGWRESTMgr_SWIFT_HealthCheck : public RGWRESTMgr {
607 protected:
608 RGWRESTMgr *get_resource_mgr(struct req_state* const s,
609 const std::string& uri,
610 std::string* const out_uri) override {
611 return this;
612 }
613
614 public:
615 RGWRESTMgr_SWIFT_HealthCheck() = default;
616 ~RGWRESTMgr_SWIFT_HealthCheck() override = default;
617
618 RGWHandler_REST* get_handler(struct req_state* const s,
619 const rgw::auth::StrategyRegistry&,
620 const std::string&) override {
621 s->prot_flags |= RGW_REST_SWIFT;
622 return new RGWHandler_SWIFT_HealthCheck;
623 }
624 };
625
626
627 class RGWHandler_REST_SWIFT_Info : public RGWHandler_REST_SWIFT {
628 public:
629 using RGWHandler_REST_SWIFT::RGWHandler_REST_SWIFT;
630 ~RGWHandler_REST_SWIFT_Info() override = default;
631
632 RGWOp *op_get() override {
633 return new RGWInfo_ObjStore_SWIFT();
634 }
635
636 int init(RGWRados* const store,
637 struct req_state* const state,
638 rgw::io::BasicClient* const cio) override {
639 state->dialect = "swift";
640 state->formatter = new JSONFormatter;
641 state->format = RGW_FORMAT_JSON;
642
643 return RGWHandler::init(store, state, cio);
644 }
645
646 int authorize() override {
647 return 0;
648 }
649
650 int postauth_init() override {
651 return 0;
652 }
653
654 int read_permissions(RGWOp *) override {
655 return 0;
656 }
657 };
658
659 class RGWRESTMgr_SWIFT_Info : public RGWRESTMgr {
660 public:
661 RGWRESTMgr_SWIFT_Info() = default;
662 ~RGWRESTMgr_SWIFT_Info() override = default;
663
664 RGWHandler_REST *get_handler(struct req_state* s,
665 const rgw::auth::StrategyRegistry& auth_registry,
666 const std::string& frontend_prefix) override;
667 };
668
669 #endif