Browse Source

[trac925] Loading the key ring

Michal 'vorner' Vaner 14 years ago
parent
commit
929a9cae2b
2 changed files with 37 additions and 10 deletions
  1. 31 5
      src/lib/server_common/keyring.cc
  2. 6 5
      src/lib/server_common/tests/keyring_test.cc

+ 31 - 5
src/lib/server_common/keyring.cc

@@ -14,21 +14,47 @@
 
 #include <server_common/keyring.h>
 
+using namespace isc::dns;
+using namespace isc::data;
+
 namespace isc {
 namespace server_common {
 
-boost::shared_ptr<dns::TSIGKeyRing> keyring;
+typedef boost::shared_ptr<TSIGKeyRing> KeyringPtr;
+
+KeyringPtr keyring;
+
+namespace {
+
+void
+updateKeyring(const std::string&, ConstElementPtr data) {
+    ConstElementPtr list(data->get("keys"));
+    KeyringPtr load(new TSIGKeyRing);
+    for (size_t i(0); i < list->size(); ++ i) {
+        load->add(TSIGKey(list->get(i)->stringValue()));
+    }
+    keyring.swap(load);
+}
+
+}
 
 void
 initKeyring(config::ModuleCCSession& session) {
-    // TODO
-    (void) session;
+    if (keyring) {
+        // We are already initialized
+        return;
+    }
+    session.addRemoteConfig("tsig_keys", updateKeyring);
 }
 
 void
 deinitKeyring(config::ModuleCCSession& session) {
-    // TODO
-    (void) session;
+    if (!keyring) {
+        // Not initialized, ignore it
+        return;
+    }
+    keyring.reset();
+    session.removeRemoteConfig("tsig_keys");
 }
 
 }

+ 6 - 5
src/lib/server_common/tests/keyring_test.cc

@@ -51,7 +51,7 @@ public:
                                                 "/tsig_keys.spec").
                              getFullSpec()));
         session.getMessages()->add(createAnswer(0, Element::fromJSON(
-            "{\"keys\": [\"key:MTIzNAo=:sha1\"]}")));
+            "{\"keys\": [\"key:MTIzNAo=:hmac-sha1\"]}")));
         // Now load it
         EXPECT_NO_THROW(initKeyring(*mccs));
         EXPECT_NE(keyring, boost::shared_ptr<TSIGKeyRing>()) <<
@@ -74,9 +74,9 @@ TEST_F(KeyringTest, keyring) {
 
     {
         SCOPED_TRACE("Update");
-        session.getMessages()->add(createCommand("config_update",
-                                                 Element::fromJSON(
-            "{\"keys\": [\"another:MTIzNAo=:sha256\"]}")));
+        session.addMessage(createCommand("config_update", Element::fromJSON(
+            "{\"keys\": [\"another:MTIzNAo=:hmac-sha256\"]}")),
+                           "tsig_keys", "*");
         mccs->checkCommand();
 
         // Make sure it no longer contains the original key
@@ -114,12 +114,13 @@ TEST_F(KeyringTest, initTwice) {
             "it even throws at it";
     }
     EXPECT_EQ(backup, keyring) << "The second init replaced the data";
+    deinitKeyring(*mccs);
 }
 
 // deinit when not initialized
 TEST_F(KeyringTest, extraDeinit) {
     // It is NULL before
-    EXPECT_EQ(keyring, boost::shared_ptr<TSIGKeyRing>()) <<
+    EXPECT_EQ(boost::shared_ptr<TSIGKeyRing>(), keyring) <<
         "Someone forgot to deinit it before";
     // Check that it doesn't get confused when we do not have it initialized
     EXPECT_NO_THROW(deinitKeyring(*mccs));