]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw: decode RGWUserInfo using decode_json
authorYehuda Sadeh <yehuda@inktank.com>
Wed, 6 Feb 2013 20:48:01 +0000 (12:48 -0800)
committerYehuda Sadeh <yehuda@inktank.com>
Fri, 22 Mar 2013 18:23:07 +0000 (11:23 -0700)
RGWUserInfo can be decoded using json_decode, and fix
the ->dump() method to match that decoding.

Signed-off-by: Yehuda Sadeh <yehuda@inktank.com>
src/Makefile.am
src/rgw/rgw_admin.cc
src/rgw/rgw_common.cc
src/rgw/rgw_common.h
src/rgw/rgw_dencoder.cc
src/rgw/rgw_json_enc.cc [new file with mode: 0644]

index fd54df867e214e5a6e98dabb95ea2c425c59c344..81015e7fcd617b5fbc80769441d4425d2330a050 100644 (file)
@@ -131,7 +131,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} perfglue/disabled_heap_profiler.cc
 ceph_dencoder_CXXFLAGS = ${AM_CXXFLAGS}
@@ -356,6 +359,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 \
@@ -402,7 +406,7 @@ ceph_rgw_multiparser_CXXFLAGS = ${AM_CXXFLAGS}
 ceph_rgw_multiparser_LDADD = $(my_radosgw_ldadd)
 bin_DEBUGPROGRAMS += ceph_rgw_multiparser
 
-ceph_rgw_jsonparser_SOURCES = rgw/rgw_jsonparser.cc
+ceph_rgw_jsonparser_SOURCES = rgw/rgw_jsonparser.cc rgw/rgw_common.cc rgw/rgw_json_enc.cc
 ceph_rgw_jsonparser_CXXFLAGS = ${CRYPTO_CXXFLAGS} ${AM_CXXFLAGS}
 ceph_rgw_jsonparser_LDADD = $(my_radosgw_ldadd)
 bin_DEBUGPROGRAMS += ceph_rgw_jsonparser
index d7e7130b5d4d574e0baca7c5163aff7961dfbf62..46aa4d449b24a4e8a5d0252be99a83e6976a4cbe 100644 (file)
@@ -181,48 +181,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;
@@ -362,63 +320,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)
index a7b8d697c68bf427ef03c149f44bbe74c2be3283..fdd48bc7cf7826e80db200d1e761f320318fc17d 100644 (file)
@@ -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;
@@ -640,7 +644,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;
   }
@@ -752,6 +756,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);
index 3e12f14f7e838077afdd26887d4c65e8f5d3f5e3..e2b25ebbe2b88d20c41c70b4d3df4c249a301f9b 100644 (file)
@@ -273,6 +273,7 @@ enum http_op {
 };
 
 class RGWAccessControlPolicy;
+class JSONObj;
 
 struct RGWAccessKey {
   string id;
@@ -296,7 +297,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);
 
@@ -319,7 +324,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);
 
@@ -328,10 +336,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);
 
@@ -347,6 +355,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);
 
@@ -448,6 +458,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();
index 00015ff022cbf9fc0a78d334248fe15bff3829f9..1f4e35227888ddbcbc7c87eab2f05af2985aa8b1 100644 (file)
@@ -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 (file)
index 0000000..c6465a2
--- /dev/null
@@ -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);
+}
+
+