]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-dencoder: MonCap[s]
authorSage Weil <sage@newdream.net>
Tue, 7 Feb 2012 19:44:40 +0000 (11:44 -0800)
committerSage Weil <sage@newdream.net>
Wed, 8 Feb 2012 21:12:18 +0000 (13:12 -0800)
Need some better test instances for MonCaps...

Signed-off-by: Sage Weil <sage@newdream.net>
src/mon/MonCaps.cc
src/mon/MonCaps.h
src/test/encoding/types.h

index c4abbe686ca82815c17d9230c1ff75295c15468d..92e7fdb4776a4413ce06f9914d7ff26d68044143 100644 (file)
@@ -15,6 +15,7 @@
 #include <errno.h>
 #include "common/config.h"
 #include "common/debug.h"
+#include "common/Formatter.h"
 #include "MonCaps.h"
 #include "mon_types.h"
 
@@ -285,3 +286,94 @@ bool MonCaps::check_privileges(int service, int req_perms, uint64_t req_auid)
     return false;
   return true;
 }
+
+// ----
+
+string rwx_to_string(rwx_t r)
+{
+  string s;
+  if (r & MON_CAP_R)
+    s += "r";
+  else
+    s += "-";
+  if (r & MON_CAP_W)
+    s += "w";
+  else
+    s += "-";
+  if (r & MON_CAP_X)
+    s += "x";
+  else
+    s += "-";
+  return s;
+}
+
+// ----
+
+void MonCap::dump(Formatter *f) const
+{
+  f->dump_string("allow", rwx_to_string(allow));
+  f->dump_string("deny", rwx_to_string(deny));
+}
+
+void MonCap::generate_test_instances(list<MonCap*>& o)
+{
+  o.push_back(new MonCap);
+  o.push_back(new MonCap(MON_CAP_R, MON_CAP_W));
+  o.push_back(new MonCap(MON_CAP_RW, MON_CAP_ALL));
+}
+
+// ----
+
+void MonCaps::encode(bufferlist& bl) const
+{
+  __u8 v = 2;
+  ::encode(v, bl);
+  ::encode(text, bl);
+  ::encode(default_action, bl);
+  ::encode(services_map, bl);
+  ::encode(pool_auid_map, bl);
+  ::encode(allow_all, bl);
+  ::encode(auid, bl);
+  ::encode(cmd_allow, bl);
+}
+
+void MonCaps::decode(bufferlist::iterator& bl)
+{
+  __u8 v;
+  ::decode(v, bl);
+  ::decode(text, bl);
+  ::decode(default_action, bl);
+  ::decode(services_map, bl);
+  ::decode(pool_auid_map, bl);
+  ::decode(allow_all, bl);
+  ::decode(auid, bl);
+  ::decode(cmd_allow, bl);
+}
+
+void MonCaps::dump(Formatter *f) const
+{
+  f->dump_string("text", text);
+  f->dump_string("default_action", rwx_to_string(default_action));
+  f->dump_int("allow_all", allow_all);
+  f->dump_unsigned("auid", auid);
+  f->open_object_section("services_map");
+  for (map<int,MonCap>::const_iterator p = services_map.begin(); p != services_map.end(); ++p) {
+    f->open_object_section("moncap");
+    p->second.dump(f);
+    f->close_section();
+  }
+  f->close_section();
+  f->open_object_section("pool_auid_map");
+  for (map<int,MonCap>::const_iterator p = pool_auid_map.begin(); p != pool_auid_map.end(); ++p) {
+    f->open_object_section("moncap");
+    p->second.dump(f);
+    f->close_section();
+  }
+  f->close_section();   
+}
+
+void MonCaps::generate_test_instances(list<MonCaps*>& o)
+{
+  o.push_back(new MonCaps);
+  // FIXME.
+}
index 3738c787f29195504dc359cc34c18eb1788027c0..bdd05d236d8d032e44a0d17e8d487b896adc9ae4 100644 (file)
@@ -29,7 +29,6 @@
 #define CEPH_MONCAPS_H
 
 #include "include/types.h"
-//#include ""
 
 #define MON_CAP_R 0x1
 #define MON_CAP_W 0x2
@@ -44,7 +43,9 @@ typedef __u8 rwx_t;
 struct MonCap {
   rwx_t allow;
   rwx_t deny;
+
   MonCap() : allow(0), deny(0) {}
+  MonCap(rwx_t a, rwx_t d) : allow(a), deny(d) {}
 
   void encode(bufferlist& bl) const {
     ::encode(allow, bl);
@@ -55,6 +56,8 @@ struct MonCap {
     ::decode(allow, bl);
     ::decode(deny, bl);
   }
+  void dump(Formatter *f) const;
+  static void generate_test_instances(list<MonCap*>& o);
 };
 WRITE_RAW_ENCODER(MonCap);
 
@@ -63,21 +66,24 @@ struct MonCaps {
   rwx_t default_action;
   map<int, MonCap> services_map;
   map<int, MonCap> pool_auid_map;
-  bool get_next_token(string s, size_t& pos, string& token);
-  bool is_rwx(string& token, rwx_t& cap_val);
-  int get_service_id(string& token);
   bool allow_all;
   uint64_t auid;
 
   // command whitelist
   list<list<string> > cmd_allow;
 
+  bool get_next_token(string s, size_t& pos, string& token);
+  bool is_rwx(string& token, rwx_t& cap_val);
+  int get_service_id(string& token);
+
 public:
   MonCaps()
     : text(), default_action(0),
       allow_all(false), auid(CEPH_AUTH_UID_DEFAULT)
   {}
+
   const string& get_str() const { return text; }
+
   bool parse(bufferlist::iterator& iter);
   rwx_t get_caps(int service) const;
   bool check_privileges(int service, int req_perm,
@@ -85,33 +91,15 @@ public:
   void set_allow_all(bool allow) { allow_all = allow; }
   void set_auid(uint64_t uid) { auid = uid; }
 
-  void encode(bufferlist& bl) const {
-    __u8 v = 2;
-    ::encode(v, bl);
-    ::encode(text, bl);
-    ::encode(default_action, bl);
-    ::encode(services_map, bl);
-    ::encode(pool_auid_map, bl);
-    ::encode(allow_all, bl);
-    ::encode(auid, bl);
-    ::encode(cmd_allow, bl);
-  }
-
-  void decode(bufferlist::iterator& bl) {
-    __u8 v;
-    ::decode(v, bl);
-    ::decode(text, bl);
-    ::decode(default_action, bl);
-    ::decode(services_map, bl);
-    ::decode(pool_auid_map, bl);
-    ::decode(allow_all, bl);
-    ::decode(auid, bl);
-    ::decode(cmd_allow, bl);
-  }
+  void encode(bufferlist& bl) const;
+  void decode(bufferlist::iterator& bl);
+  void dump(Formatter *f) const;
+  static void generate_test_instances(list<MonCaps*>& o);
 };
 WRITE_CLASS_ENCODER(MonCaps);
 
 inline ostream& operator<<(ostream& out, const MonCaps& m) {
   return out << m.get_str();
 }
+
 #endif
index 9053ad41d5e83e4c91e0953de3112de482735248..20795434df35577089ece8f7c8755156e3ece37e 100644 (file)
@@ -55,6 +55,10 @@ TYPE(PGMap)
 #include "mon/MonMap.h"
 TYPE(MonMap)
 
+#include "mon/MonCaps.h"
+TYPE(MonCap)
+TYPE(MonCaps)
+
 // --- messages ---
 #include "messages/MAuth.h"
 MESSAGE(MAuth)