]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
mon,auth: AuthMonitor, KeyRing: add Formatter-dumps of auth info
authorDan Mick <dan.mick@inktank.com>
Wed, 10 Jul 2013 23:41:24 +0000 (16:41 -0700)
committerDan Mick <dan.mick@inktank.com>
Thu, 11 Jul 2013 02:02:31 +0000 (19:02 -0700)
Signed-off-by: Dan Mick <dan.mick@inktank.com>
auth: KeyRing: encode_formatted() receives a label as first argument

Also, this allows us to standardize formatted output on the AuthMonitor,
so that all output starts with a section 'auth'.  Other subsystems using
the KeyRing class, can specify whatever section they prefer.

Signed-off-by: Joao Eduardo Luis <joao.luis@inktank.com>
src/auth/KeyRing.cc
src/auth/KeyRing.h
src/mon/AuthMonitor.cc

index 96dba74a043e401bccb998a73cf3ba7cb40b7423..56655392bae66c6edd90c4389927908bfa029662 100644 (file)
@@ -25,6 +25,7 @@
 #include "common/debug.h"
 #include "common/errno.h"
 #include "include/str_list.h"
+#include "common/Formatter.h"
 
 #define dout_subsys ceph_subsys_auth
 
@@ -130,6 +131,36 @@ void KeyRing::encode_plaintext(bufferlist& bl)
   bl.append(str);
 }
 
+void KeyRing::encode_formatted(string label, Formatter *f, bufferlist& bl)
+{
+  std::ostringstream(os);
+  f->open_array_section(label.c_str());
+  for (map<EntityName, EntityAuth>::iterator p = keys.begin();
+       p != keys.end();
+       ++p) {
+
+    f->open_object_section("auth_entities");
+    f->dump_string("entity", p->first.to_str().c_str());
+    std::ostringstream keyss;
+    keyss << p->second.key;
+    f->dump_string("key", keyss.str());
+    f->open_object_section("caps");
+    for (map<string, bufferlist>::iterator q = p->second.caps.begin();
+        q != p->second.caps.end();
+        ++q) {
+      bufferlist::iterator dataiter = q->second.begin();
+      string caps;
+      ::decode(caps, dataiter);
+      f->dump_string(q->first.c_str(), caps);
+    }
+    f->close_section();        /* caps */
+    f->close_section();        /* auth_entities */
+  }
+  f->close_section();  /* auth_dump */
+  f->flush(os);
+  bl.append(os.str());
+}
+
 void KeyRing::decode_plaintext(bufferlist::iterator& bli)
 {
   int ret;
@@ -152,7 +183,7 @@ void KeyRing::decode_plaintext(bufferlist::iterator& bli)
     map<string, bufferlist> caps;
     if (!ename.from_str(name)) {
       ostringstream oss;
-      oss << "bad entity name n keyring: " << name;
+      oss << "bad entity name in keyring: " << name;
       throw buffer::malformed_input(oss.str().c_str());
     }
 
index 85b14a99f78f144069ce2fb9a0b824465c2fc11d..6b041ab37ce3229b72c84ef6f0287fdaf653479d 100644 (file)
@@ -97,6 +97,7 @@ public:
   void decode(bufferlist::iterator& bl);
 
   void encode_plaintext(bufferlist& bl);
+  void encode_formatted(string label, Formatter *f, bufferlist& bl);
 };
 
 // don't use WRITE_CLASS_ENCODER macro because we don't have an encode
index 8988046954abf7cfdd3a00c00ded1c249154055d..37179858443be6bff99c150d3dbae7fd9e7a1b2d 100644 (file)
@@ -561,6 +561,10 @@ bool AuthMonitor::preprocess_command(MMonCommand *m)
     r = -EINVAL;
   }
 
+  string format;
+  cmd_getval(g_ceph_context, cmdmap, "format", format, string("plain"));
+  boost::scoped_ptr<Formatter> f(new_formatter(format));
+
   if (prefix == "auth export") {
     KeyRing keyring;
     export_keyring(keyring);
@@ -569,7 +573,10 @@ bool AuthMonitor::preprocess_command(MMonCommand *m)
       if (keyring.get_auth(entity, eauth)) {
        KeyRing kr;
        kr.add(entity, eauth);
-       kr.encode_plaintext(rdata);
+       if (f)
+         kr.encode_formatted("auth", f.get(), rdata);
+       else
+         kr.encode_plaintext(rdata);
        ss << "export " << eauth;
        r = 0;
       } else {
@@ -577,7 +584,11 @@ bool AuthMonitor::preprocess_command(MMonCommand *m)
        r = -ENOENT;
       }
     } else {
-      keyring.encode_plaintext(rdata);
+      if (f)
+       keyring.encode_formatted("auth", f.get(), rdata);
+      else
+       keyring.encode_plaintext(rdata);
+
       ss << "exported master keyring";
       r = 0;
     }
@@ -589,7 +600,10 @@ bool AuthMonitor::preprocess_command(MMonCommand *m)
       r = -ENOENT;
     } else {
       keyring.add(entity, entity_auth);
-      keyring.encode_plaintext(rdata);
+      if (f)
+       keyring.encode_formatted("auth", f.get(), rdata);
+      else
+       keyring.encode_plaintext(rdata);
       ss << "exported keyring for " << entity_name;
       r = 0;
     }
@@ -663,6 +677,10 @@ bool AuthMonitor::prepare_command(MMonCommand *m)
 
   cmd_getval(g_ceph_context, cmdmap, "prefix", prefix);
 
+  string format;
+  cmd_getval(g_ceph_context, cmdmap, "format", format, string("plain"));
+  boost::scoped_ptr<Formatter> f(new_formatter(format));
+
   MonSession *session = m->get_session();
   if (!session ||
       (!mon->_allowed_command(session, cmdmap))) {