From: Dan Mick Date: Wed, 10 Jul 2013 23:41:24 +0000 (-0700) Subject: mon,auth: AuthMonitor, KeyRing: add Formatter-dumps of auth info X-Git-Tag: v0.67-rc1~106 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=83bcde34c9b80e9e3d983e477df4e1662f9ef5ed;p=ceph.git mon,auth: AuthMonitor, KeyRing: add Formatter-dumps of auth info Signed-off-by: Dan Mick 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 --- diff --git a/src/auth/KeyRing.cc b/src/auth/KeyRing.cc index 96dba74a043e..56655392bae6 100644 --- a/src/auth/KeyRing.cc +++ b/src/auth/KeyRing.cc @@ -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::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::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 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()); } diff --git a/src/auth/KeyRing.h b/src/auth/KeyRing.h index 85b14a99f78f..6b041ab37ce3 100644 --- a/src/auth/KeyRing.h +++ b/src/auth/KeyRing.h @@ -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 diff --git a/src/mon/AuthMonitor.cc b/src/mon/AuthMonitor.cc index 8988046954ab..37179858443b 100644 --- a/src/mon/AuthMonitor.cc +++ b/src/mon/AuthMonitor.cc @@ -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 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 f(new_formatter(format)); + MonSession *session = m->get_session(); if (!session || (!mon->_allowed_command(session, cmdmap))) {