diff options
author | Yehuda Sadeh <yehuda@inktank.com> | 2013-02-06 12:48:01 -0800 |
---|---|---|
committer | Yehuda Sadeh <yehuda@inktank.com> | 2013-02-11 17:05:06 -0800 |
commit | e5907228022f3b213698eaf957317a4f0881e723 (patch) | |
tree | 2bebe5d52a9f7d650d075a1a4eaf79a0631b2008 | |
parent | bfb35d3275803c1e22f740d5b2687de2cf617044 (diff) | |
download | ceph-e5907228022f3b213698eaf957317a4f0881e723.tar.gz |
rgw: decode RGWUserInfo using decode_json
RGWUserInfo can be decoded using json_decode, and fix
the ->dump() method to match that decoding.
Signed-off-by: Yehuda Sadeh <yehuda@inktank.com>
-rw-r--r-- | src/Makefile.am | 8 | ||||
-rw-r--r-- | src/rgw/rgw_admin.cc | 99 | ||||
-rw-r--r-- | src/rgw/rgw_common.cc | 32 | ||||
-rw-r--r-- | src/rgw/rgw_common.h | 14 | ||||
-rw-r--r-- | src/rgw/rgw_dencoder.cc | 284 | ||||
-rw-r--r-- | src/rgw/rgw_json_enc.cc | 475 |
6 files changed, 527 insertions, 385 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 6a9bb147824..50164d13b4f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -126,7 +126,10 @@ osdmaptool_LDADD = $(LIBGLOBAL_LDA) bin_PROGRAMS += monmaptool crushtool osdmaptool rgw_dencoder_src = rgw/rgw_dencoder.cc \ - rgw/rgw_acl.cc + rgw/rgw_acl.cc \ + rgw/rgw_common.cc \ + rgw/rgw_json.cc \ + rgw/rgw_json_enc.cc ceph_dencoder_SOURCES = test/encoding/ceph_dencoder.cc ${rgw_dencoder_src} ceph_dencoder_CXXFLAGS = ${AM_CXXFLAGS} @@ -351,6 +354,7 @@ librgw_a_SOURCES = \ rgw/rgw_xml.cc \ rgw/rgw_usage.cc \ rgw/rgw_json.cc \ + rgw/rgw_json_enc.cc \ rgw/rgw_user.cc \ rgw/rgw_tools.cc \ rgw/rgw_rados.cc \ @@ -397,7 +401,7 @@ rgw_multiparser_CXXFLAGS = ${AM_CXXFLAGS} rgw_multiparser_LDADD = $(my_radosgw_ldadd) bin_DEBUGPROGRAMS += rgw_multiparser -rgw_jsonparser_SOURCES = rgw/rgw_jsonparser.cc +rgw_jsonparser_SOURCES = rgw/rgw_jsonparser.cc rgw/rgw_common.cc rgw/rgw_json_enc.cc rgw_jsonparser_CXXFLAGS = ${CRYPTO_CXXFLAGS} ${AM_CXXFLAGS} rgw_jsonparser_LDADD = $(my_radosgw_ldadd) bin_DEBUGPROGRAMS += rgw_jsonparser diff --git a/src/rgw/rgw_admin.cc b/src/rgw/rgw_admin.cc index 860a4d19eba..91edd397764 100644 --- a/src/rgw/rgw_admin.cc +++ b/src/rgw/rgw_admin.cc @@ -177,48 +177,6 @@ static uint32_t str_to_perm(const char *str) return 0; // unreachable } -struct rgw_flags_desc { - uint32_t mask; - const char *str; -}; - -static struct rgw_flags_desc rgw_perms[] = { - { RGW_PERM_FULL_CONTROL, "full-control" }, - { RGW_PERM_READ | RGW_PERM_WRITE, "read-write" }, - { RGW_PERM_READ, "read" }, - { RGW_PERM_WRITE, "write" }, - { RGW_PERM_READ_ACP, "read-acp" }, - { RGW_PERM_WRITE_ACP, "read-acp" }, - { 0, NULL } -}; - -static void perm_to_str(uint32_t mask, char *buf, int len) -{ - const char *sep = ""; - int pos = 0; - if (!mask) { - snprintf(buf, len, "<none>"); - return; - } - while (mask) { - uint32_t orig_mask = mask; - for (int i = 0; rgw_perms[i].mask; i++) { - struct rgw_flags_desc *desc = &rgw_perms[i]; - if ((mask & desc->mask) == desc->mask) { - pos += snprintf(buf + pos, len - pos, "%s%s", sep, desc->str); - if (pos == len) - return; - sep = ", "; - mask &= ~desc->mask; - if (!mask) - return; - } - } - if (mask == orig_mask) // no change - break; - } -} - static int get_cmd(const char *cmd, const char *prev_cmd, bool *need_more) { *need_more = false; @@ -355,63 +313,10 @@ string escape_str(string& src, char c) static void show_user_info(RGWUserInfo& info, Formatter *formatter) { - map<string, RGWAccessKey>::iterator kiter; - map<string, RGWSubUser>::iterator uiter; - - - formatter->open_object_section("user_info"); - - formatter->dump_string("user_id", info.user_id); - formatter->dump_string("display_name", info.display_name); - formatter->dump_string("email", info.user_email); - formatter->dump_int("suspended", (int)info.suspended); - formatter->dump_int("max_buckets", (int)info.max_buckets); - - // subusers - formatter->open_array_section("subusers"); - for (uiter = info.subusers.begin(); uiter != info.subusers.end(); ++uiter) { - RGWSubUser& u = uiter->second; - formatter->open_object_section("user"); - formatter->dump_format("id", "%s:%s", info.user_id.c_str(), u.name.c_str()); - char buf[256]; - perm_to_str(u.perm_mask, buf, sizeof(buf)); - formatter->dump_string("permissions", buf); - formatter->close_section(); - formatter->flush(cout); - } - formatter->close_section(); - - // keys - formatter->open_array_section("keys"); - for (kiter = info.access_keys.begin(); kiter != info.access_keys.end(); ++kiter) { - RGWAccessKey& k = kiter->second; - const char *sep = (k.subuser.empty() ? "" : ":"); - const char *subuser = (k.subuser.empty() ? "" : k.subuser.c_str()); - formatter->open_object_section("key"); - formatter->dump_format("user", "%s%s%s", info.user_id.c_str(), sep, subuser); - formatter->dump_string("access_key", k.id); - formatter->dump_string("secret_key", k.key); - formatter->close_section(); - } - formatter->close_section(); - - formatter->open_array_section("swift_keys"); - for (kiter = info.swift_keys.begin(); kiter != info.swift_keys.end(); ++kiter) { - RGWAccessKey& k = kiter->second; - const char *sep = (k.subuser.empty() ? "" : ":"); - const char *subuser = (k.subuser.empty() ? "" : k.subuser.c_str()); - formatter->open_object_section("key"); - formatter->dump_format("user", "%s%s%s", info.user_id.c_str(), sep, subuser); - formatter->dump_string("secret_key", k.key); - formatter->close_section(); - } - formatter->close_section(); - - info.caps.dump(formatter); - - formatter->close_section(); + info.dump(formatter); formatter->flush(cout); cout << std::endl; + } static int create_bucket(string bucket_str, string& user_id, string& display_name) diff --git a/src/rgw/rgw_common.cc b/src/rgw/rgw_common.cc index 8564fdd985a..722ab232a76 100644 --- a/src/rgw/rgw_common.cc +++ b/src/rgw/rgw_common.cc @@ -1,8 +1,11 @@ #include <errno.h> +#include "json_spirit/json_spirit.h" + #include "rgw_common.h" #include "rgw_acl.h" #include "rgw_string.h" +#include "rgw_json.h" #include "common/ceph_crypto.h" #include "common/armor.h" @@ -231,6 +234,7 @@ bool parse_iso8601(const char *s, struct tm *t) { memset(t, 0, sizeof(*t)); const char *p = strptime(s, "%Y-%m-%dT%T", t); + if (!p) { dout(0) << "parse_iso8601 failed" << dendl; return false; @@ -618,7 +622,7 @@ int RGWUserCaps::get_cap(const string& cap, string& type, uint32_t *pperm) uint32_t perm = 0; if (pos < (int)cap.size() - 1) { cap_perm = cap.substr(pos + 1); - int r = parse_cap_perm(cap_perm, &perm); + int r = RGWUserCaps::parse_cap_perm(cap_perm, &perm); if (r < 0) return r; } @@ -730,6 +734,32 @@ void RGWUserCaps::dump(Formatter *f) const f->close_section(); } +struct RGWUserCap { + string type; + uint32_t perm; + + void decode_json(JSONObj *obj) { + JSONDecoder::decode_json("type", type, obj); + string perm_str; + JSONDecoder::decode_json("perm", perm_str, obj); + if (RGWUserCaps::parse_cap_perm(perm_str, &perm) < 0) { + throw JSONDecoder::err("failed to parse permissions"); + } + } +}; + +void RGWUserCaps::decode_json(JSONObj *obj) +{ + list<RGWUserCap> caps_list; + decode_json_obj(caps_list, obj); + + list<RGWUserCap>::iterator iter; + for (iter = caps_list.begin(); iter != caps_list.end(); ++iter) { + RGWUserCap& cap = *iter; + caps[cap.type] = cap.perm; + } +} + int RGWUserCaps::check_cap(const string& cap, uint32_t perm) { map<string, uint32_t>::iterator iter = caps.find(cap); diff --git a/src/rgw/rgw_common.h b/src/rgw/rgw_common.h index cd1ebaa71f6..3a01e2ab49c 100644 --- a/src/rgw/rgw_common.h +++ b/src/rgw/rgw_common.h @@ -271,6 +271,7 @@ enum http_op { }; class RGWAccessControlPolicy; +class JSONObj; struct RGWAccessKey { string id; @@ -294,7 +295,11 @@ struct RGWAccessKey { DECODE_FINISH(bl); } void dump(Formatter *f) const; + void dump(Formatter *f, const string& user, bool swift) const; static void generate_test_instances(list<RGWAccessKey*>& o); + + void decode_json(JSONObj *obj); + void decode_json(JSONObj *obj, bool swift); }; WRITE_CLASS_ENCODER(RGWAccessKey); @@ -317,7 +322,10 @@ struct RGWSubUser { DECODE_FINISH(bl); } void dump(Formatter *f) const; + void dump(Formatter *f, const string& user) const; static void generate_test_instances(list<RGWSubUser*>& o); + + void decode_json(JSONObj *obj); }; WRITE_CLASS_ENCODER(RGWSubUser); @@ -326,10 +334,10 @@ class RGWUserCaps map<string, uint32_t> caps; int get_cap(const string& cap, string& type, uint32_t *perm); - int parse_cap_perm(const string& str, uint32_t *perm); int add_cap(const string& cap); int remove_cap(const string& cap); public: + static int parse_cap_perm(const string& str, uint32_t *perm); int add_from_string(const string& str); int remove_from_string(const string& str); @@ -345,6 +353,8 @@ public: } int check_cap(const string& cap, uint32_t perm); void dump(Formatter *f) const; + + void decode_json(JSONObj *obj); }; WRITE_CLASS_ENCODER(RGWUserCaps); @@ -446,6 +456,8 @@ struct RGWUserInfo void dump(Formatter *f) const; static void generate_test_instances(list<RGWUserInfo*>& o); + void decode_json(JSONObj *obj); + void clear() { user_id.clear(); display_name.clear(); diff --git a/src/rgw/rgw_dencoder.cc b/src/rgw/rgw_dencoder.cc index 00015ff022c..1f4e3522788 100644 --- a/src/rgw/rgw_dencoder.cc +++ b/src/rgw/rgw_dencoder.cc @@ -20,15 +20,6 @@ void RGWObjManifestPart::generate_test_instances(std::list<RGWObjManifestPart*>& o.push_back(p); } -void RGWObjManifestPart::dump(Formatter *f) const -{ - f->open_object_section("loc"); - loc.dump(f); - f->close_section(); - f->dump_unsigned("loc_ofs", loc_ofs); - f->dump_unsigned("size", size); -} - void RGWObjManifest::generate_test_instances(std::list<RGWObjManifest*>& o) { RGWObjManifest *m = new RGWObjManifest; @@ -47,20 +38,6 @@ void RGWObjManifest::generate_test_instances(std::list<RGWObjManifest*>& o) o.push_back(new RGWObjManifest); } -void RGWObjManifest::dump(Formatter *f) const -{ - map<uint64_t, RGWObjManifestPart>::const_iterator iter = objs.begin(); - f->open_array_section("objs"); - for (; iter != objs.end(); ++iter) { - f->dump_unsigned("ofs", iter->first); - f->open_object_section("part"); - iter->second.dump(f); - f->close_section(); - } - f->close_section(); - f->dump_unsigned("obj_size", obj_size); -} - void rgw_log_entry::generate_test_instances(list<rgw_log_entry*>& o) { rgw_log_entry *e = new rgw_log_entry; @@ -83,28 +60,6 @@ void rgw_log_entry::generate_test_instances(list<rgw_log_entry*>& o) o.push_back(new rgw_log_entry); } -void rgw_log_entry::dump(Formatter *f) const -{ - f->dump_string("object_owner", object_owner); - f->dump_string("bucket_owner", bucket_owner); - f->dump_string("bucket", bucket); - f->dump_stream("time") << time; - f->dump_string("remote_addr", remote_addr); - f->dump_string("user", user); - f->dump_string("obj", obj); - f->dump_string("op", op); - f->dump_string("uri", uri); - f->dump_string("http_status", http_status); - f->dump_string("error_code", error_code); - f->dump_unsigned("bytes_sent", bytes_sent); - f->dump_unsigned("bytes_received", bytes_received); - f->dump_unsigned("obj_size", obj_size); - f->dump_stream("total_time") << total_time; - f->dump_string("user_agent", user_agent); - f->dump_string("referrer", referrer); - f->dump_string("bucket_id", bucket_id); -} - void rgw_intent_log_entry::generate_test_instances(list<rgw_intent_log_entry*>& o) { rgw_intent_log_entry *e = new rgw_intent_log_entry; @@ -115,16 +70,6 @@ void rgw_intent_log_entry::generate_test_instances(list<rgw_intent_log_entry*>& o.push_back(new rgw_intent_log_entry); } -void rgw_intent_log_entry::dump(Formatter *f) const -{ - f->open_object_section("obj"); - obj.dump(f); - f->close_section(); - f->dump_stream("op_time") << op_time; - f->dump_unsigned("intent", intent); -} - - void ACLPermission::generate_test_instances(list<ACLPermission*>& o) { ACLPermission *p = new ACLPermission; @@ -133,11 +78,6 @@ void ACLPermission::generate_test_instances(list<ACLPermission*>& o) o.push_back(new ACLPermission); } -void ACLPermission::dump(Formatter *f) const -{ - f->dump_int("flags", flags); -} - void ACLGranteeType::generate_test_instances(list<ACLGranteeType*>& o) { ACLGranteeType *t = new ACLGranteeType; @@ -146,11 +86,6 @@ void ACLGranteeType::generate_test_instances(list<ACLGranteeType*>& o) o.push_back(new ACLGranteeType); } -void ACLGranteeType::dump(Formatter *f) const -{ - f->dump_unsigned("type", type); -} - /* the following is copied here from rgw_acl_s3.cc, to avoid having to have excessive linking with everything it needs */ @@ -189,23 +124,6 @@ void ACLGrant::generate_test_instances(list<ACLGrant*>& o) o.push_back(new ACLGrant); } -void ACLGrant::dump(Formatter *f) const -{ - f->open_object_section("type"); - type.dump(f); - f->close_section(); - - f->dump_string("id", id); - f->dump_string("email", email); - - f->open_object_section("permission"); - permission.dump(f); - f->close_section(); - - f->dump_string("name", name); - f->dump_int("group", (int)group); -} - void RGWAccessControlList::generate_test_instances(list<RGWAccessControlList*>& o) { RGWAccessControlList *acl = new RGWAccessControlList(NULL); @@ -224,41 +142,6 @@ void RGWAccessControlList::generate_test_instances(list<RGWAccessControlList*>& o.push_back(new RGWAccessControlList(NULL)); } -void RGWAccessControlList::dump(Formatter *f) const -{ - map<string, int>::const_iterator acl_user_iter = acl_user_map.begin(); - f->open_array_section("acl_user_map"); - for (; acl_user_iter != acl_user_map.end(); ++acl_user_iter) { - f->open_object_section("entry"); - f->dump_string("user", acl_user_iter->first); - f->dump_int("acl", acl_user_iter->second); - f->close_section(); - } - f->close_section(); - - map<uint32_t, int>::const_iterator acl_group_iter = acl_group_map.begin(); - f->open_array_section("acl_group_map"); - for (; acl_group_iter != acl_group_map.end(); ++acl_group_iter) { - f->open_object_section("entry"); - f->dump_unsigned("group", acl_group_iter->first); - f->dump_int("acl", acl_group_iter->second); - f->close_section(); - } - f->close_section(); - - multimap<string, ACLGrant>::const_iterator giter = grant_map.begin(); - f->open_array_section("grant_map"); - for (; giter != grant_map.end(); ++giter) { - f->open_object_section("entry"); - f->dump_string("id", giter->first); - f->open_object_section("grant"); - giter->second.dump(f); - f->close_section(); - f->close_section(); - } - f->close_section(); -} - void ACLOwner::generate_test_instances(list<ACLOwner*>& o) { ACLOwner *owner = new ACLOwner; @@ -268,12 +151,6 @@ void ACLOwner::generate_test_instances(list<ACLOwner*>& o) o.push_back(new ACLOwner); } -void ACLOwner::dump(Formatter *f) const -{ - f->dump_string("id", id); - f->dump_string("display_name", display_name); -} - void RGWAccessControlPolicy::generate_test_instances(list<RGWAccessControlPolicy*>& o) { list<RGWAccessControlList *> acl_list; @@ -300,17 +177,6 @@ void RGWAccessControlPolicy::generate_test_instances(list<RGWAccessControlPolicy } -void RGWAccessControlPolicy::dump(Formatter *f) const -{ - f->open_object_section("acl"); - acl.dump(f); - f->close_section(); - f->open_object_section("owner"); - owner.dump(f); - f->close_section(); -} - - void ObjectMetaInfo::generate_test_instances(list<ObjectMetaInfo*>& o) { ObjectMetaInfo *m = new ObjectMetaInfo; @@ -319,12 +185,6 @@ void ObjectMetaInfo::generate_test_instances(list<ObjectMetaInfo*>& o) o.push_back(new ObjectMetaInfo); } -void ObjectMetaInfo::dump(Formatter *f) const -{ - f->dump_unsigned("size", size); - f->dump_stream("mtime") << mtime; -} - void ObjectCacheInfo::generate_test_instances(list<ObjectCacheInfo*>& o) { ObjectCacheInfo *i = new ObjectCacheInfo; @@ -345,56 +205,11 @@ void ObjectCacheInfo::generate_test_instances(list<ObjectCacheInfo*>& o) o.push_back(new ObjectCacheInfo); } -void ObjectCacheInfo::dump(Formatter *f) const -{ - f->dump_int("status", status); - f->dump_unsigned("flags", flags); - f->open_object_section("data"); - f->dump_unsigned("length", data.length()); - f->close_section(); - - map<string, bufferlist>::const_iterator iter = xattrs.begin(); - f->open_array_section("xattrs"); - for (; iter != xattrs.end(); ++iter) { - f->dump_string("name", iter->first); - f->open_object_section("value"); - f->dump_unsigned("length", iter->second.length()); - f->close_section(); - } - f->close_section(); - - f->open_array_section("rm_xattrs"); - for (iter = rm_xattrs.begin(); iter != rm_xattrs.end(); ++iter) { - f->dump_string("name", iter->first); - f->open_object_section("value"); - f->dump_unsigned("length", iter->second.length()); - f->close_section(); - } - f->close_section(); - f->open_object_section("meta"); - meta.dump(f); - f->close_section(); - -} - void RGWCacheNotifyInfo::generate_test_instances(list<RGWCacheNotifyInfo*>& o) { o.push_back(new RGWCacheNotifyInfo); } -void RGWCacheNotifyInfo::dump(Formatter *f) const -{ - f->dump_unsigned("op", op); - f->open_object_section("obj"); - obj.dump(f); - f->close_section(); - f->open_object_section("obj_info"); - obj_info.dump(f); - f->close_section(); - f->dump_unsigned("ofs", ofs); - f->dump_string("ns", ns); -} - void RGWAccessKey::generate_test_instances(list<RGWAccessKey*>& o) { RGWAccessKey *k = new RGWAccessKey; @@ -405,13 +220,6 @@ void RGWAccessKey::generate_test_instances(list<RGWAccessKey*>& o) o.push_back(new RGWAccessKey); } -void RGWAccessKey::dump(Formatter *f) const -{ - f->dump_string("id", id); - f->dump_string("key", key); - f->dump_string("subuser", subuser); -} - void RGWSubUser::generate_test_instances(list<RGWSubUser*>& o) { RGWSubUser *u = new RGWSubUser; @@ -421,12 +229,6 @@ void RGWSubUser::generate_test_instances(list<RGWSubUser*>& o) o.push_back(new RGWSubUser); } -void RGWSubUser::dump(Formatter *f) const -{ - f->dump_string("name", name); - f->dump_unsigned("perm_mask", perm_mask); -} - void RGWUserInfo::generate_test_instances(list<RGWUserInfo*>& o) { RGWUserInfo *i = new RGWUserInfo; @@ -450,45 +252,6 @@ void RGWUserInfo::generate_test_instances(list<RGWUserInfo*>& o) o.push_back(new RGWUserInfo); } -void RGWUserInfo::dump(Formatter *f) const -{ - f->dump_unsigned("auid", auid); - f->dump_string("user_id", user_id); - f->dump_string("display_name", display_name); - f->dump_string("user_email", user_email); - - map<string, RGWAccessKey>::const_iterator aiter = access_keys.begin(); - f->open_array_section("access_keys"); - for (; aiter != access_keys.end(); ++aiter) { - f->open_object_section("entry"); - f->dump_string("uid", aiter->first); - f->open_object_section("access_key"); - aiter->second.dump(f); - f->close_section(); - f->close_section(); - } - - aiter = swift_keys.begin(); - for (; aiter != swift_keys.end(); ++aiter) { - f->open_object_section("entry"); - f->dump_string("subuser", aiter->first); - f->open_object_section("key"); - aiter->second.dump(f); - f->close_section(); - f->close_section(); - } - map<string, RGWSubUser>::const_iterator siter = subusers.begin(); - for (; siter != subusers.end(); ++siter) { - f->open_object_section("entry"); - f->dump_string("id", siter->first); - f->open_object_section("subuser"); - siter->second.dump(f); - f->close_section(); - f->close_section(); - } - f->dump_int("suspended", (int)suspended); -} - void rgw_bucket::generate_test_instances(list<rgw_bucket*>& o) { rgw_bucket *b = new rgw_bucket("name", "pool", "marker", "123"); @@ -496,14 +259,6 @@ void rgw_bucket::generate_test_instances(list<rgw_bucket*>& o) o.push_back(new rgw_bucket); } -void rgw_bucket::dump(Formatter *f) const -{ - f->dump_string("name", name); - f->dump_string("pool", pool); - f->dump_string("marker", marker); - f->dump_string("bucket_id", bucket_id); -} - void RGWBucketInfo::generate_test_instances(list<RGWBucketInfo*>& o) { RGWBucketInfo *i = new RGWBucketInfo; @@ -514,15 +269,6 @@ void RGWBucketInfo::generate_test_instances(list<RGWBucketInfo*>& o) o.push_back(new RGWBucketInfo); } -void RGWBucketInfo::dump(Formatter *f) const -{ - f->open_object_section("bucket"); - bucket.dump(f); - f->close_section(); - f->dump_string("owner", owner); - f->dump_unsigned("flags", flags); -} - void RGWBucketEnt::generate_test_instances(list<RGWBucketEnt*>& o) { RGWBucketEnt *e = new RGWBucketEnt; @@ -534,17 +280,6 @@ void RGWBucketEnt::generate_test_instances(list<RGWBucketEnt*>& o) o.push_back(new RGWBucketEnt); } -void RGWBucketEnt::dump(Formatter *f) const -{ - f->open_object_section("bucket"); - bucket.dump(f); - f->close_section(); - f->dump_unsigned("size", size); - f->dump_unsigned("size_rounded", size_rounded); - f->dump_stream("mtime") << mtime; - f->dump_unsigned("count", count); -} - void RGWUploadPartInfo::generate_test_instances(list<RGWUploadPartInfo*>& o) { RGWUploadPartInfo *i = new RGWUploadPartInfo; @@ -555,14 +290,6 @@ void RGWUploadPartInfo::generate_test_instances(list<RGWUploadPartInfo*>& o) o.push_back(new RGWUploadPartInfo); } -void RGWUploadPartInfo::dump(Formatter *f) const -{ - f->dump_unsigned("num", num); - f->dump_unsigned("size", size); - f->dump_string("etag", etag); - f->dump_stream("modified") << modified; -} - void rgw_obj::generate_test_instances(list<rgw_obj*>& o) { rgw_bucket b = rgw_bucket("bucket", "pool", "marker", "10"); @@ -571,14 +298,3 @@ void rgw_obj::generate_test_instances(list<rgw_obj*>& o) o.push_back(new rgw_obj); } -void rgw_obj::dump(Formatter *f) const -{ - f->open_object_section("bucket"); - bucket.dump(f); - f->close_section(); - f->dump_string("key", key); - f->dump_string("ns", ns); - f->dump_string("object", object); -} - - diff --git a/src/rgw/rgw_json_enc.cc b/src/rgw/rgw_json_enc.cc new file mode 100644 index 00000000000..c6465a29e85 --- /dev/null +++ b/src/rgw/rgw_json_enc.cc @@ -0,0 +1,475 @@ + +#include "rgw_common.h" +#include "rgw_rados.h" +#include "rgw_log.h" +#include "rgw_acl.h" +#include "rgw_acl_s3.h" +#include "rgw_cache.h" + +#include "common/ceph_json.h" +#include "common/Formatter.h" + +void RGWObjManifestPart::dump(Formatter *f) const +{ + f->open_object_section("loc"); + loc.dump(f); + f->close_section(); + f->dump_unsigned("loc_ofs", loc_ofs); + f->dump_unsigned("size", size); +} + +void RGWObjManifest::dump(Formatter *f) const +{ + map<uint64_t, RGWObjManifestPart>::const_iterator iter = objs.begin(); + f->open_array_section("objs"); + for (; iter != objs.end(); ++iter) { + f->dump_unsigned("ofs", iter->first); + f->open_object_section("part"); + iter->second.dump(f); + f->close_section(); + } + f->close_section(); + f->dump_unsigned("obj_size", obj_size); +} + +void rgw_log_entry::dump(Formatter *f) const +{ + f->dump_string("object_owner", object_owner); + f->dump_string("bucket_owner", bucket_owner); + f->dump_string("bucket", bucket); + f->dump_stream("time") << time; + f->dump_string("remote_addr", remote_addr); + f->dump_string("user", user); + f->dump_string("obj", obj); + f->dump_string("op", op); + f->dump_string("uri", uri); + f->dump_string("http_status", http_status); + f->dump_string("error_code", error_code); + f->dump_unsigned("bytes_sent", bytes_sent); + f->dump_unsigned("bytes_received", bytes_received); + f->dump_unsigned("obj_size", obj_size); + f->dump_stream("total_time") << total_time; + f->dump_string("user_agent", user_agent); + f->dump_string("referrer", referrer); + f->dump_string("bucket_id", bucket_id); +} + +void rgw_intent_log_entry::dump(Formatter *f) const +{ + f->open_object_section("obj"); + obj.dump(f); + f->close_section(); + f->dump_stream("op_time") << op_time; + f->dump_unsigned("intent", intent); +} + + +void ACLPermission::dump(Formatter *f) const +{ + f->dump_int("flags", flags); +} + +void ACLGranteeType::dump(Formatter *f) const +{ + f->dump_unsigned("type", type); +} + +void ACLGrant::dump(Formatter *f) const +{ + f->open_object_section("type"); + type.dump(f); + f->close_section(); + + f->dump_string("id", id); + f->dump_string("email", email); + + f->open_object_section("permission"); + permission.dump(f); + f->close_section(); + + f->dump_string("name", name); + f->dump_int("group", (int)group); +} + +void RGWAccessControlList::dump(Formatter *f) const +{ + map<string, int>::const_iterator acl_user_iter = acl_user_map.begin(); + f->open_array_section("acl_user_map"); + for (; acl_user_iter != acl_user_map.end(); ++acl_user_iter) { + f->open_object_section("entry"); + f->dump_string("user", acl_user_iter->first); + f->dump_int("acl", acl_user_iter->second); + f->close_section(); + } + f->close_section(); + + map<uint32_t, int>::const_iterator acl_group_iter = acl_group_map.begin(); + f->open_array_section("acl_group_map"); + for (; acl_group_iter != acl_group_map.end(); ++acl_group_iter) { + f->open_object_section("entry"); + f->dump_unsigned("group", acl_group_iter->first); + f->dump_int("acl", acl_group_iter->second); + f->close_section(); + } + f->close_section(); + + multimap<string, ACLGrant>::const_iterator giter = grant_map.begin(); + f->open_array_section("grant_map"); + for (; giter != grant_map.end(); ++giter) { + f->open_object_section("entry"); + f->dump_string("id", giter->first); + f->open_object_section("grant"); + giter->second.dump(f); + f->close_section(); + f->close_section(); + } + f->close_section(); +} + +void ACLOwner::dump(Formatter *f) const +{ + f->dump_string("id", id); + f->dump_string("display_name", display_name); +} + +void RGWAccessControlPolicy::dump(Formatter *f) const +{ + f->open_object_section("acl"); + acl.dump(f); + f->close_section(); + f->open_object_section("owner"); + owner.dump(f); + f->close_section(); +} + +void ObjectMetaInfo::dump(Formatter *f) const +{ + f->dump_unsigned("size", size); + f->dump_stream("mtime") << mtime; +} + +void ObjectCacheInfo::dump(Formatter *f) const +{ + f->dump_int("status", status); + f->dump_unsigned("flags", flags); + f->open_object_section("data"); + f->dump_unsigned("length", data.length()); + f->close_section(); + + map<string, bufferlist>::const_iterator iter = xattrs.begin(); + f->open_array_section("xattrs"); + for (; iter != xattrs.end(); ++iter) { + f->dump_string("name", iter->first); + f->open_object_section("value"); + f->dump_unsigned("length", iter->second.length()); + f->close_section(); + } + f->close_section(); + + f->open_array_section("rm_xattrs"); + for (iter = rm_xattrs.begin(); iter != rm_xattrs.end(); ++iter) { + f->dump_string("name", iter->first); + f->open_object_section("value"); + f->dump_unsigned("length", iter->second.length()); + f->close_section(); + } + f->close_section(); + f->open_object_section("meta"); + meta.dump(f); + f->close_section(); + +} + +void RGWCacheNotifyInfo::dump(Formatter *f) const +{ + f->dump_unsigned("op", op); + f->open_object_section("obj"); + obj.dump(f); + f->close_section(); + f->open_object_section("obj_info"); + obj_info.dump(f); + f->close_section(); + f->dump_unsigned("ofs", ofs); + f->dump_string("ns", ns); +} + +void RGWAccessKey::dump(Formatter *f) const +{ + f->open_object_section("key"); + f->dump_string("access_key", id); + f->dump_string("secret_key", key); + f->dump_string("subuser", subuser); + f->close_section(); +} + +void RGWAccessKey::dump(Formatter *f, const string& user, bool swift) const +{ + f->open_object_section("key"); + string u = user; + if (!subuser.empty()) { + u.append(":"); + u.append(subuser); + } + f->dump_string("user", u); + if (!swift) { + f->dump_string("access_key", id); + } + f->dump_string("secret_key", key); + f->close_section(); +} + +void RGWAccessKey::decode_json(JSONObj *obj) { + JSONDecoder::decode_json("access_key", id, obj, true); + JSONDecoder::decode_json("secret_key", key, obj, true); + if (!JSONDecoder::decode_json("subuser", subuser, obj)) { + string user; + JSONDecoder::decode_json("user", user, obj, true); + int pos = user.find(':'); + if (pos >= 0) { + subuser = user.substr(pos + 1); + } + } +} + +void RGWAccessKey::decode_json(JSONObj *obj, bool swift) { + if (!swift) { + decode_json(obj); + return; + } + + if (!JSONDecoder::decode_json("subuser", subuser, obj)) { + string user; + JSONDecoder::decode_json("user", user, obj, true); + int pos = user.find(':'); + if (pos >= 0) { + subuser = user.substr(pos + 1); + } + } + JSONDecoder::decode_json("secret_key", key, obj, true); +} + +struct rgw_flags_desc { + uint32_t mask; + const char *str; +}; + +static struct rgw_flags_desc rgw_perms[] = { + { RGW_PERM_FULL_CONTROL, "full-control" }, + { RGW_PERM_READ | RGW_PERM_WRITE, "read-write" }, + { RGW_PERM_READ, "read" }, + { RGW_PERM_WRITE, "write" }, + { RGW_PERM_READ_ACP, "read-acp" }, + { RGW_PERM_WRITE_ACP, "read-acp" }, + { 0, NULL } +}; + +static void perm_to_str(uint32_t mask, char *buf, int len) +{ + const char *sep = ""; + int pos = 0; + if (!mask) { + snprintf(buf, len, "<none>"); + return; + } + while (mask) { + uint32_t orig_mask = mask; + for (int i = 0; rgw_perms[i].mask; i++) { + struct rgw_flags_desc *desc = &rgw_perms[i]; + if ((mask & desc->mask) == desc->mask) { + pos += snprintf(buf + pos, len - pos, "%s%s", sep, desc->str); + if (pos == len) + return; + sep = ", "; + mask &= ~desc->mask; + if (!mask) + return; + } + } + if (mask == orig_mask) // no change + break; + } +} + +void RGWSubUser::dump(Formatter *f) const +{ + f->open_object_section("subuser"); + f->dump_string("id", name); + char buf[256]; + perm_to_str(perm_mask, buf, sizeof(buf)); + f->dump_string("permissions", buf); + f->close_section(); +} + +void RGWSubUser::dump(Formatter *f, const string& user) const +{ + f->open_object_section("subuser"); + string s = user; + s.append(":"); + s.append(name); + f->dump_string("id", s); + char buf[256]; + perm_to_str(perm_mask, buf, sizeof(buf)); + f->dump_string("permissions", buf); + f->close_section(); +} + +static uint32_t str_to_perm(const string& s) +{ + if (s.compare("read") == 0) + return RGW_PERM_READ; + else if (s.compare("write") == 0) + return RGW_PERM_WRITE; + else if (s.compare("read-write") == 0) + return RGW_PERM_READ | RGW_PERM_WRITE; + else if (s.compare("full-control") == 0) + return RGW_PERM_FULL_CONTROL; + return 0; +} + +void RGWSubUser::decode_json(JSONObj *obj) +{ + string uid; + JSONDecoder::decode_json("id", uid, obj); + int pos = uid.find(':'); + if (pos >= 0) + name = uid.substr(pos + 1); + string perm_str; + JSONDecoder::decode_json("permissions", perm_str, obj); + perm_mask = str_to_perm(perm_str); +} + +void RGWUserInfo::dump(Formatter *f) const +{ + f->open_object_section("user_info"); + + f->dump_string("user_id", user_id); + f->dump_string("display_name", display_name); + f->dump_string("email", user_email); + f->dump_int("suspended", (int)suspended); + f->dump_int("max_buckets", (int)max_buckets); + + f->dump_unsigned("auid", auid); + + map<string, RGWSubUser>::const_iterator siter = subusers.begin(); + f->open_array_section("subusers"); + for (; siter != subusers.end(); ++siter) { + siter->second.dump(f, user_id); + } + f->close_section(); + + map<string, RGWAccessKey>::const_iterator aiter = access_keys.begin(); + f->open_array_section("keys"); + for (; aiter != access_keys.end(); ++aiter) { + aiter->second.dump(f, user_id, false); + } + f->close_section(); + + aiter = swift_keys.begin(); + f->open_array_section("swift_keys"); + for (; aiter != swift_keys.end(); ++aiter) { + aiter->second.dump(f, user_id, true); + } + f->close_section(); + + caps.dump(f); + + f->close_section(); +} + + +struct SwiftKeyEntry { + RGWAccessKey key; + + void decode_json(JSONObj *obj) { + key.decode_json(obj, true); + } +}; + +void RGWUserInfo::decode_json(JSONObj *obj) +{ + JSONDecoder::decode_json("user_id", user_id, obj, true); + JSONDecoder::decode_json("display_name", display_name, obj); + JSONDecoder::decode_json("email", user_email, obj); + bool susp; + JSONDecoder::decode_json("suspended", susp, obj); + suspended = (__u8)susp; + JSONDecoder::decode_json("max_buckets", max_buckets, obj); + JSONDecoder::decode_json("auid", auid, obj); + + list<RGWAccessKey> akeys_list; + JSONDecoder::decode_json("keys", akeys_list, obj); + + list<RGWAccessKey>::iterator iter; + for (iter = akeys_list.begin(); iter != akeys_list.end(); ++iter) { + RGWAccessKey& e = *iter; + access_keys[e.id] = e; + } + + list<SwiftKeyEntry> skeys_list; + list<SwiftKeyEntry>::iterator skiter; + JSONDecoder::decode_json("swift_keys", skeys_list, obj); + + for (skiter = skeys_list.begin(); skiter != skeys_list.end(); ++skiter) { + SwiftKeyEntry& e = *skiter; + swift_keys[e.key.subuser] = e.key; + } + + list<RGWSubUser> susers_list; + list<RGWSubUser>::iterator siter; + JSONDecoder::decode_json("subusers", susers_list, obj); + + for (siter = susers_list.begin(); siter != susers_list.end(); ++siter) { + RGWSubUser& e = *siter; + subusers[e.name] = e; + } + + JSONDecoder::decode_json("caps", caps, obj); +} + +void rgw_bucket::dump(Formatter *f) const +{ + f->dump_string("name", name); + f->dump_string("pool", pool); + f->dump_string("marker", marker); + f->dump_string("bucket_id", bucket_id); +} + +void RGWBucketInfo::dump(Formatter *f) const +{ + f->open_object_section("bucket"); + bucket.dump(f); + f->close_section(); + f->dump_string("owner", owner); + f->dump_unsigned("flags", flags); +} + +void RGWBucketEnt::dump(Formatter *f) const +{ + f->open_object_section("bucket"); + bucket.dump(f); + f->close_section(); + f->dump_unsigned("size", size); + f->dump_unsigned("size_rounded", size_rounded); + f->dump_stream("mtime") << mtime; + f->dump_unsigned("count", count); +} + +void RGWUploadPartInfo::dump(Formatter *f) const +{ + f->dump_unsigned("num", num); + f->dump_unsigned("size", size); + f->dump_string("etag", etag); + f->dump_stream("modified") << modified; +} + +void rgw_obj::dump(Formatter *f) const +{ + f->open_object_section("bucket"); + bucket.dump(f); + f->close_section(); + f->dump_string("key", key); + f->dump_string("ns", ns); + f->dump_string("object", object); +} + + |