From: Yehuda Sadeh Date: Fri, 26 Apr 2013 23:07:21 +0000 (-0700) Subject: rgw: modify metadata RESTful implementation X-Git-Tag: v0.67-rc1~128^2~121 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=bf612f04f73fa085e885d5b56054bcea2b238d5b;p=ceph.git rgw: modify metadata RESTful implementation REST handler should derive from RGWHandler_Auth_S3, other changes. Signed-off-by: Yehuda Sadeh --- diff --git a/src/rgw/rgw_common.h b/src/rgw/rgw_common.h index 3ab0c97ec4fb..1d4659371a6c 100644 --- a/src/rgw/rgw_common.h +++ b/src/rgw/rgw_common.h @@ -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; diff --git a/src/rgw/rgw_rest.cc b/src/rgw/rgw_rest.cc index ba38a034c94b..3c70b84ea62b 100644 --- a/src/rgw/rgw_rest.cc +++ b/src/rgw/rgw_rest.cc @@ -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; diff --git a/src/rgw/rgw_rest.h b/src/rgw/rgw_rest.h index d8e91a250540..ce474b0d6d55 100644 --- a/src/rgw/rgw_rest.h +++ b/src/rgw/rgw_rest.h @@ -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; } }; diff --git a/src/rgw/rgw_rest_metadata.cc b/src/rgw/rgw_rest_metadata.cc index 8844835dacc7..5a4bc3cbec73 100644 --- a/src/rgw/rgw_rest_metadata.cc +++ b/src/rgw/rgw_rest_metadata.cc @@ -19,122 +19,85 @@ #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 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::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 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::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() { diff --git a/src/rgw/rgw_rest_metadata.h b/src/rgw/rgw_rest_metadata.h index f83cc9c288f4..5bf47bb3a92c 100644 --- a/src/rgw/rgw_rest_metadata.h +++ b/src/rgw/rgw_rest_metadata.h @@ -14,6 +14,18 @@ #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 { diff --git a/src/rgw/rgw_rest_s3.cc b/src/rgw/rgw_rest_s3.cc index 2268a2781c83..30f350bd9b8d 100644 --- a/src/rgw/rgw_rest_s3.cc +++ b/src/rgw/rgw_rest_s3.cc @@ -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 == '?') {