]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
auth: simplify KeyRing map type, authmon add/importing
authorSage Weil <sage@newdream.net>
Thu, 4 Feb 2010 19:03:27 +0000 (11:03 -0800)
committerSage Weil <sage@newdream.net>
Thu, 4 Feb 2010 19:03:27 +0000 (11:03 -0800)
src/auth/KeyRing.cc
src/auth/KeyRing.h
src/mon/AuthMonitor.cc
src/mon/AuthMonitor.h

index 4468ff58ee6f5b4577b136314babf19126811fd0..61a89d808df1936a682600506f3fb479726a3e8d 100644 (file)
@@ -64,15 +64,10 @@ bool KeyRing::load(const char *filename_list)
 
 void KeyRing::print(ostream& out)
 {
-  for (map<string, EntityAuth>::iterator p = keys.begin();
+  for (map<EntityName, EntityAuth>::iterator p = keys.begin();
        p != keys.end();
        ++p) {
-    string n = p->first;
-    if (n.empty()) {
-      out << "<default key>" << std::endl;
-    } else {
-      out << n << std::endl;
-    }
+    out << p->first << std::endl;
     out << "\tkey: " << p->second.key << std::endl;
 
     for (map<string, bufferlist>::iterator q = p->second.caps.begin();
@@ -88,7 +83,7 @@ void KeyRing::print(ostream& out)
 
 void KeyRing::import(KeyRing& other)
 {
-  for (map<string, EntityAuth>::iterator p = other.keys.begin();
+  for (map<EntityName, EntityAuth>::iterator p = other.keys.begin();
        p != other.keys.end();
        ++p) {
     dout(10) << " importing " << p->first << " " << p->second << dendl;
index fee6471a2aa62a084e48c2068992d866f1d0129e..533328c9d4d34c1fcdd7b2bf156e3375eff1730e 100644 (file)
 #include "auth/Auth.h"
 
 class KeyRing : public KeyStore {
-  map<string, EntityAuth> keys;
+  map<EntityName, EntityAuth> keys;
 
 public:
-  map<string, EntityAuth>& get_keys() { return keys; }  // yuck
+  map<EntityName, EntityAuth>& get_keys() { return keys; }  // yuck
 
   bool load(const char *filename);
   void print(ostream& out);
 
   // accessors
   bool get_auth(EntityName& name, EntityAuth &a) {
-    string n = name.to_str();
-    if (keys.count(n)) {
-      a = keys[n];
+    if (keys.count(name)) {
+      a = keys[name];
       return true;
     }
     return false;
   }
   bool get_secret(EntityName& name, CryptoKey& secret) {
-    string n = name.to_str();
-    if (keys.count(n)) {
-      secret = keys[n].key;
+    if (keys.count(name)) {
+      secret = keys[name].key;
       return true;
     }
     return false;
@@ -52,12 +50,10 @@ public:
 
   // modifiers
   void add(EntityName& name, EntityAuth &a) {
-    string s = name.to_str();
-    keys[s] = a;
+    keys[name] = a;
   }
   void set_caps(EntityName& name, map<string, bufferlist>& caps) {
-    string s = name.to_str();
-    keys[s].caps = caps;
+    keys[name].caps = caps;
   }
   void import(KeyRing& other);
 
index 88a59c478bcf1815a7c3825f568f1bc44b15ad24..d658322f106518053bc6de0eca32f4bb2b097f0d 100644 (file)
@@ -114,10 +114,8 @@ void AuthMonitor::create_initial(bufferlist& bl)
       } catch (buffer::error *err) {
         cerr << "error reading file " << g_conf.keyring << std::endl;
       }
-      if (read_ok) {
-       string def;
-       import_keyring(keyring, def);
-      }
+      if (read_ok)
+       import_keyring(keyring);
     }
   }
 
@@ -478,24 +476,13 @@ bool AuthMonitor::preprocess_command(MMonCommand *m)
   return true;
 }
 
-void AuthMonitor::import_keyring(KeyRing& keyring, string& def)
+void AuthMonitor::import_keyring(KeyRing& keyring)
 {
-  for (map<string, EntityAuth>::iterator p = keyring.get_keys().begin();
+  for (map<EntityName, EntityAuth>::iterator p = keyring.get_keys().begin();
        p != keyring.get_keys().end();
        p++) {
     KeyServerData::Incremental auth_inc;
-    if (p->first.empty()) {
-      if (def.empty())
-       continue;
-      if (!auth_inc.name.from_str(def)) {
-       dout(0) << "bad entity name" << def << dendl;
-      }
-    } else {
-      if (!auth_inc.name.from_str(p->first)) {
-       dout(0) << "bad entity name " << p->first << dendl;
-       continue;
-      }
-    }
+    auth_inc.name = p->first;
     auth_inc.auth = p->second; 
     auth_inc.op = KeyServerData::AUTH_INC_ADD;
     dout(10) << " importing " << auth_inc.name << " " << auth_inc.auth << dendl;
@@ -511,12 +498,27 @@ bool AuthMonitor::prepare_command(MMonCommand *m)
 
   // nothing here yet
   if (m->cmd.size() > 1) {
-    if (m->cmd[1] == "add" && m->cmd.size() >= 2) {
-      string entity_name;
+    if (m->cmd[1] == "import") {
+      bufferlist bl = m->get_data();
+      bufferlist::iterator iter = bl.begin();
+      KeyRing keyring;
+      try {
+        ::decode(keyring, iter);
+      } catch (buffer::error *err) {
+        ss << "error decoding keyring";
+        rs = -EINVAL;
+        goto done;
+      }
+      import_keyring(keyring);
+      ss << "imported keyring";
+      getline(ss, rs);
+      paxos->wait_for_commit(new Monitor::C_Command(mon, m, 0, rs, paxos->get_version()));
+      return true;
+    }
+    else if (m->cmd[1] == "add" && m->cmd.size() >= 2) {
       KeyServerData::Incremental auth_inc;
       if (m->cmd.size() >= 3) {
-        entity_name = m->cmd[2];
-        if (!auth_inc.name.from_str(entity_name)) {
+        if (!auth_inc.name.from_str(m->cmd[2])) {
           ss << "bad entity name";
           rs = -EINVAL;
           goto done;
@@ -535,12 +537,21 @@ bool AuthMonitor::prepare_command(MMonCommand *m)
         goto done;
       }
 
-      import_keyring(keyring, entity_name);
-      ss << "updated";
+      if (!keyring.get_auth(auth_inc.name, auth_inc.auth)) {
+       ss << "key for " << auth_inc.name << " not found in provided keyring";
+        rs = -EINVAL;
+        goto done;
+      }
+      auth_inc.op = KeyServerData::AUTH_INC_ADD;
+      dout(10) << " importing " << auth_inc.name << " " << auth_inc.auth << dendl;
+      push_cephx_inc(auth_inc);
+
+      ss << "added key for " << auth_inc.name;
       getline(ss, rs);
       paxos->wait_for_commit(new Monitor::C_Command(mon, m, 0, rs, paxos->get_version()));
       return true;
-    } else if (m->cmd[1] == "del" && m->cmd.size() >= 3) {
+    }
+    else if (m->cmd[1] == "del" && m->cmd.size() >= 3) {
       string name = m->cmd[2];
       KeyServerData::Incremental auth_inc;
       auth_inc.name.from_str(name);
@@ -556,11 +567,13 @@ bool AuthMonitor::prepare_command(MMonCommand *m)
       getline(ss, rs);
       paxos->wait_for_commit(new Monitor::C_Command(mon, m, 0, rs, paxos->get_version()));
       return true;
-    } else if (m->cmd[1] == "list") {
+    }
+    else if (m->cmd[1] == "list") {
       mon->key_server.list_secrets(ss);
       err = 0;
       goto done;
-    } else {
+    }
+    else {
       auth_usage(ss);
     }
   } else {
index e5bb5e70b7fbebf5d7ed515034c1cb563de21108..b97e178faafc15ae02fa9ef135a61ceba2004f9d 100644 (file)
@@ -79,7 +79,7 @@ private:
   uint64_t max_global_id;
   uint64_t last_allocated_id;
 
-  void import_keyring(KeyRing& keyring, string& def);
+  void import_keyring(KeyRing& keyring);
 
   void push_cephx_inc(KeyServerData::Incremental& auth_inc) {
     Incremental inc;