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