]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw: modify metadata RESTful implementation
authorYehuda Sadeh <yehuda@inktank.com>
Fri, 26 Apr 2013 23:07:21 +0000 (16:07 -0700)
committerYehuda Sadeh <yehuda@inktank.com>
Wed, 15 May 2013 01:52:32 +0000 (18:52 -0700)
REST handler should derive from RGWHandler_Auth_S3,
other changes.

Signed-off-by: Yehuda Sadeh <yehuda@inktank.com>
src/rgw/rgw_common.h
src/rgw/rgw_rest.cc
src/rgw/rgw_rest.h
src/rgw/rgw_rest_metadata.cc
src/rgw/rgw_rest_metadata.h
src/rgw/rgw_rest_s3.cc

index 3ab0c97ec4fbc2b2d164d99b51a74c2dfee997d9..1d4659371a6c72a94e18892dd0bf4d0e14c3bb50 100644 (file)
@@ -646,6 +646,7 @@ struct req_state {
    int format;
    ceph::Formatter *formatter;
    string decoded_uri;
+   string effective_uri;
    string request_uri;
    string script_uri;
    string request_params;
@@ -671,7 +672,6 @@ struct req_state {
    rgw_bucket bucket;
    string bucket_name_str;
    string object_str;
-   string meta_section_str;
    ACLOwner bucket_owner;
    ACLOwner owner;
 
index ba38a034c94bd2a4f969111471e9c43eb2c71ed4..3c70b84ea62bcf3adab5264f88bc734cc319ad51 100644 (file)
@@ -1173,8 +1173,10 @@ void RGWRESTMgr::register_default_mgr(RGWRESTMgr *mgr)
   default_mgr = mgr;
 }
 
-RGWRESTMgr *RGWRESTMgr::get_resource_mgr(struct req_state *s, const string& uri)
+RGWRESTMgr *RGWRESTMgr::get_resource_mgr(struct req_state *s, const string& uri, string *out_uri)
 {
+  *out_uri = uri;
+
   if (resources_by_size.empty())
     return this;
 
@@ -1186,7 +1188,7 @@ RGWRESTMgr *RGWRESTMgr::get_resource_mgr(struct req_state *s, const string& uri)
        (resource.size() == iter->first ||
         resource[iter->first] == '/')) {
       string suffix = uri.substr(iter->first);
-      return resource_mgrs[resource]->get_resource_mgr(s, suffix);
+      return resource_mgrs[resource]->get_resource_mgr(s, suffix, out_uri);
     }
   }
 
@@ -1286,7 +1288,7 @@ RGWHandler *RGWREST::get_handler(RGWRados *store, struct req_state *s, RGWClient
   if (*init_error < 0)
     return NULL;
 
-  RGWRESTMgr *m = mgr.get_resource_mgr(s, s->decoded_uri);
+  RGWRESTMgr *m = mgr.get_resource_mgr(s, s->decoded_uri, &s->effective_uri);
   if (!m) {
     *init_error = -ERR_METHOD_NOT_ALLOWED;
     return NULL;
index d8e91a2505405f9eb0705f4f7de6bb90587602b2..ce474b0d6d55f14961f0dd6eda79e8866ddd81b1 100644 (file)
@@ -275,7 +275,7 @@ public:
   void register_resource(string resource, RGWRESTMgr *mgr);
   void register_default_mgr(RGWRESTMgr *mgr);
 
-  virtual RGWRESTMgr *get_resource_mgr(struct req_state *s, const string& uri);
+  virtual RGWRESTMgr *get_resource_mgr(struct req_state *s, const string& uri, string *out_uri);
   virtual RGWHandler *get_handler(struct req_state *s) { return NULL; }
   virtual void put_handler(RGWHandler *handler) { delete handler; }
 };
index 8844835dacc72476a0934698fd50edc6c137d067..5a4bc3cbec7320c3e457bbb71e157592955cf6f7 100644 (file)
 #include "common/errno.h"
 #define dout_subsys ceph_subsys_rgw
 
-int RGWHandler_Metadata::init_from_header(struct req_state *s) {
-  const char *p;
-  const string prefix = "/admin/metadata";
-  string section, req;
-
-  p = s->request_params.c_str();
-  if(p[0] != '\0'){
-    s->args.set(p);
-    s->args.parse();
-  }
-
-  /*Allocate a JSON formatter*/
-  s->format = RGW_FORMAT_JSON;
-  s->formatter = new JSONFormatter(false);
-
-  /*Get the section name*/  
-  if (s->decoded_uri.compare(0, prefix.length(), prefix) != 0) {
-    /*This is a safety check, may never happen*/
-    return -EINVAL;
-  } else {
-    unsigned start_off = prefix.length();
-
-    start_off += (s->decoded_uri.length() > prefix.length())?1:0;
-    s->meta_section_str.assign(s->decoded_uri, 
-                               start_off, 
-                               string::npos);
-  }
-  return 0;
-}
-
-int RGWHandler_Metadata::init(RGWRados *store, 
-                              struct req_state *state, 
-                              RGWClientIO *cio) {
-  int ret = init_from_header(state);
-  if (ret < 0) 
-    return ret;
-
-  return RGWHandler_ObjStore::init(store, state, cio);
-}
 
-int RGWHandler_Metadata::authorize() {
-  int ret = RGW_Auth_S3::authorize(store, s);
-  if (ret < 0) {
-    return ret;
-  }
-  /*TODO : check if the user is admin?*/
-  return 0;
+const char *RGWOp_Metadata_Get::name() {
+  return "get_metadata";
 }
 
-const char *RGWOp_Metadata_Get::name() {
+static inline void frame_metadata_key(req_state *s, string& out) {
   bool exists;
+  string key = s->args.get("key", &exists);
 
-  (void)s->args.get("key", &exists);
-  if (exists) {
-    return "get_metadata";
+  string metadata_key;
+  string section;
+  if (s->bucket_name) {
+    section = s->bucket_name;
   } else {
-    return "list_metadata";
+    section = key;
+    key.clear();
   }
-}
 
-static inline void frame_metadata_key(string& section, string& key, string& out) {
   out = (section + string(":") + key);
+
+  if (!key.empty()) {
+    out += string(":") + key;
+  }
 }
 
 void RGWOp_Metadata_Get::execute() {
-  bool exists;
-  string key = s->args.get("key", &exists);
-  unsigned num_params = s->args.get_num_params();
   string metadata_key;
 
-  frame_metadata_key(s->meta_section_str, key, metadata_key);
-  if ((num_params > 0) && !exists) {
-    /*Invalid parameter*/
-    http_ret = -EINVAL;
+  frame_metadata_key(s, metadata_key);
+
+  /* Get keys */
+  http_ret = store->meta_mgr->get(metadata_key, s->formatter);
+  if (http_ret < 0) {
+    dout(5) << "ERROR: can't get key: " << cpp_strerror(http_ret) << dendl;
     return;
   }
-  if (!exists) {
-    /* List keys */
-    void *handle;
-    int max = 1000;
 
-    http_ret = store->meta_mgr->list_keys_init(metadata_key, &handle);
-    if (http_ret < 0) {
-      dout(5) << "ERROR: can't get key: " << cpp_strerror(http_ret) << dendl;
-      return;
-    }
+  http_ret = 0;
+}
 
-    bool truncated;
+const char *RGWOp_Metadata_List::name() {
+  return "list_metadata";
+}
 
-    s->formatter->open_array_section("keys");
+void RGWOp_Metadata_List::execute() {
+  string metadata_key;
 
-    do {
-      list<string> keys;
-      http_ret = store->meta_mgr->list_keys_next(handle, max, keys, &truncated);
-      if (http_ret < 0) {
-        dout(5) << "ERROR: lists_keys_next(): " << cpp_strerror(http_ret) << dendl;
-        return;
-      }
+  frame_metadata_key(s, metadata_key);
+  /* List keys */
+  void *handle;
+  int max = 1000;
 
-      for (list<string>::iterator iter = keys.begin(); iter != keys.end(); ++iter) {
-        s->formatter->dump_string("key", *iter);
-      }
+  http_ret = store->meta_mgr->list_keys_init(metadata_key, &handle);
+  if (http_ret < 0) {
+    dout(5) << "ERROR: can't get key: " << cpp_strerror(http_ret) << dendl;
+    return;
+  }
 
-    } while (truncated);
+  bool truncated;
 
-    s->formatter->close_section();
+  s->formatter->open_array_section("keys");
 
-    store->meta_mgr->list_keys_complete(handle);
-  } else {
-    /* Get keys */
-    http_ret = store->meta_mgr->get(metadata_key, s->formatter);
+  do {
+    list<string> keys;
+    http_ret = store->meta_mgr->list_keys_next(handle, max, keys, &truncated);
     if (http_ret < 0) {
-      dout(5) << "ERROR: can't get key: " << cpp_strerror(http_ret) << dendl;
+      dout(5) << "ERROR: lists_keys_next(): " << cpp_strerror(http_ret) << dendl;
       return;
     }
-  }
+
+    for (list<string>::iterator iter = keys.begin(); iter != keys.end(); ++iter) {
+      s->formatter->dump_string("key", *iter);
+    }
+
+  } while (truncated);
+
+  s->formatter->close_section();
+
+  store->meta_mgr->list_keys_complete(handle);
 
   http_ret = 0;
 }
@@ -166,7 +129,6 @@ int RGWOp_Metadata_Put::get_data(bufferlist& bl) {
 
 void RGWOp_Metadata_Put::execute() {
   bufferlist bl;
-  string key = s->args.get("key");
   string metadata_key;
 
   http_ret = get_data(bl);
@@ -174,7 +136,7 @@ void RGWOp_Metadata_Put::execute() {
     return;
   }
   
-  frame_metadata_key(s->meta_section_str, key, metadata_key);
+  frame_metadata_key(s, metadata_key);
   
   http_ret = store->meta_mgr->put(metadata_key, bl);
   if (http_ret < 0) {
@@ -185,10 +147,9 @@ void RGWOp_Metadata_Put::execute() {
 }
 
 void RGWOp_Metadata_Delete::execute() {
-  string key = s->args.get("key");
   string metadata_key;
 
-  frame_metadata_key(s->meta_section_str, key, metadata_key);
+  frame_metadata_key(s, metadata_key);
   http_ret = store->meta_mgr->remove(metadata_key);
   if (http_ret < 0) {
     dout(5) << "ERROR: can't remove key: " << cpp_strerror(http_ret) << dendl;
@@ -198,7 +159,10 @@ void RGWOp_Metadata_Delete::execute() {
 }
 
 RGWOp *RGWHandler_Metadata::op_get() {
-  return new RGWOp_Metadata_Get;
+  if (s->args.exists("key"))
+    return new RGWOp_Metadata_Get;
+  else
+    return new RGWOp_Metadata_List;
 }
 
 RGWOp *RGWHandler_Metadata::op_put() {
index f83cc9c288f46784d0500a525acf3f11a6f68866..5bf47bb3a92c28e52b2d7bac461336b864d85830 100644 (file)
 #ifndef CEPH_RGW_REST_METADATA_H
 #define CEPH_RGW_REST_METADATA_H
 
+class RGWOp_Metadata_List : public RGWRESTOp {
+public:
+  RGWOp_Metadata_List() {}
+  ~RGWOp_Metadata_List() {}
+
+  int check_caps(RGWUserCaps& caps) {
+    return caps.check_cap("metadata", RGW_CAP_READ);
+  }
+  void execute();
+  virtual const char *name();
+};
+
 class RGWOp_Metadata_Get : public RGWRESTOp {
 public:
   RGWOp_Metadata_Get() {}
@@ -51,21 +63,18 @@ public:
   virtual const char *name() { return "remove_metadata"; }
 };
 
-class RGWHandler_Metadata : public RGWHandler_ObjStore {
+class RGWHandler_Metadata : public RGWHandler_Auth_S3 {
 protected:
-  int init_from_header(struct req_state *state);
   RGWOp *op_get();
   RGWOp *op_put();
   RGWOp *op_delete();
+
+  int read_permissions(RGWOp*) {
+    return 0;
+  }
 public:
-  RGWHandler_Metadata() : RGWHandler_ObjStore() {}
+  RGWHandler_Metadata() : RGWHandler_Auth_S3() {}
   virtual ~RGWHandler_Metadata() {}
-
-  virtual int validate_bucket_name(const std::string& bucket) { return 0; }
-  virtual int validate_object_name(const std::string& object) { return 0; }
-
-  virtual int init(RGWRados *store, struct req_state *state, RGWClientIO *cio);
-  virtual int authorize();
 };
 
 class RGWRESTMgr_Metadata : public RGWRESTMgr {
index 2268a2781c83f8adf81c88a48698951b04f05d12..30f350bd9b8d5cb6f61cb54b290f49c5b6991fbc 100644 (file)
@@ -1685,7 +1685,7 @@ int RGWHandler_ObjStore_S3::init_from_header(struct req_state *s, int default_fo
   string req;
   string first;
 
-  const char *req_name = s->decoded_uri.c_str();
+  const char *req_name = s->effective_uri.c_str();
   const char *p;
 
   if (*req_name == '?') {