]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[30-implement-control-socket-for-ddns-2] Ported more code and some tests
authorFrancis Dupont <fdupont@isc.org>
Fri, 28 Dec 2018 00:53:56 +0000 (01:53 +0100)
committerFrancis Dupont <fdupont@isc.org>
Thu, 3 Jan 2019 09:05:03 +0000 (04:05 -0500)
src/bin/d2/d2_cfg_mgr.cc
src/bin/d2/d2_cfg_mgr.h
src/bin/d2/d2_config.h
src/bin/d2/d2_controller.cc
src/bin/d2/d2_simple_parser.cc
src/bin/d2/parser_context.h
src/bin/d2/tests/d2_cfg_mgr_unittests.cc

index cb163d1fbc38f556cef19308509613e89767dceb..a79a839d08bedf93b6d6003faf8f80fe4b7a1d72 100644 (file)
@@ -34,7 +34,8 @@ D2CfgContext::D2CfgContext()
     : d2_params_(new D2Params()),
       forward_mgr_(new DdnsDomainListMgr("forward-ddns")),
       reverse_mgr_(new DdnsDomainListMgr("reverse-ddns")),
-      keys_(new TSIGKeyInfoMap()) {
+      keys_(new TSIGKeyInfoMap()),
+      control_socket_(ConstElementPtr()) {
 }
 
 D2CfgContext::D2CfgContext(const D2CfgContext& rhs) : ConfigBase(rhs) {
@@ -50,6 +51,8 @@ D2CfgContext::D2CfgContext(const D2CfgContext& rhs) : ConfigBase(rhs) {
     }
 
     keys_ = rhs.keys_;
+
+    control_socket_ = rhs.control_socket_;
 }
 
 D2CfgContext::~D2CfgContext() {
@@ -94,6 +97,10 @@ D2CfgContext::toElement() const {
         tsig_keys->add(key->second->toElement());
     }
     d2->set("tsig-keys", tsig_keys);
+    // Set control-socket (skip if null as empty is not legal)
+    if (!isNull(control_socket_)) {
+        d2->set("control-socket", UserContext::toElement(control_socket_));
+    }
     // Set DhcpDdns
     ElementPtr result = Element::createMap();
     result->set("DhcpDdns", d2);
@@ -238,6 +245,11 @@ D2CfgMgr::getD2Params() {
     return (getD2CfgContext()->getD2Params());
 }
 
+const isc::data::ConstElementPtr
+D2CfgMgr::getControlSocketInfo() {
+    return (getD2CfgContext()->getControlSocketInfo());
+}
+
 std::string
 D2CfgMgr::getConfigSummary(const uint32_t) {
     return (getD2Params()->getConfigSummary());
index 83f9d167c8d4351a0f078432a005a018ae6e5f3b..a3e1ec3ee81729d7e22b2f03666849684e7c99b7 100644 (file)
@@ -90,6 +90,18 @@ public:
         keys_ = keys;
     }
 
+    /// @brief Returns information about control socket
+    /// @return pointer to the Element that holds control-socket map
+    const isc::data::ConstElementPtr getControlSocketInfo() const {
+        return (control_socket_);
+    }
+
+    /// @brief Sets information about the control socket
+    /// @param control_socket Element that holds control-socket map
+    void setControlSocketInfo(const isc::data::ConstElementPtr& control_socket) {
+        control_socket_ = control_socket;
+    }
+
     /// @brief Unparse a configuration object
     ///
     /// @return a pointer to a configuration
@@ -112,8 +124,11 @@ private:
     /// @brief Reverse domain list manager.
     DdnsDomainListMgrPtr reverse_mgr_;
 
-    /// @brief Storage for the map of TSIGKeyInfos
+    /// @brief Storage for the map of TSIGKeyInfos.
     TSIGKeyInfoMapPtr keys_;
+
+    /// @brief Pointer to the control-socket information.
+    isc::data::ConstElementPtr control_socket_;
 };
 
 /// @brief Defines a pointer for DdnsDomain instances.
@@ -254,6 +269,11 @@ public:
     /// @return reference to const D2ParamsPtr
     const D2ParamsPtr& getD2Params();
 
+    /// @brief Convenience method fetches information about control socket
+    /// from context
+    /// @return pointer to the Element that holds control-socket map
+    const isc::data::ConstElementPtr getControlSocketInfo();
+
     /// @brief Returns configuration summary in the textual format.
     ///
     /// @param selection Bitfield which describes the parts of the configuration
index e92bb9b263f6d3cd68892cef7d865fac764350f7..713c37496eec08e000bfd4f73762576f2a4afa71 100644 (file)
@@ -77,7 +77,7 @@ namespace d2 {
 ///  "interface" : "eth1" ,
 ///  "ip-address" : "192.168.1.33" ,
 ///  "port" : 88 ,
-///  "control-socket": 
+///  "control-socket":
 ///  {
 ///    "socket-type": "unix" ,
 ///    "socket-name": "/tmp/d2-ctrl-socket"
index d86857db09ec83ed3df41d0d1519e4813a5ff113..93fbd63c65da2a1a25ba93e668c0930c0993dac4 100644 (file)
@@ -46,7 +46,7 @@ D2Controller::D2Controller()
     : DControllerBase(d2_app_name_, d2_bin_name_) {
 }
 
-isc::data::ConstElementPtr 
+isc::data::ConstElementPtr
 D2Controller::parseFile(const std::string& file_name) {
     isc::data::ConstElementPtr elements;
 
index 89654d85938356878b0aacbe0c8d2fee529de5b3..2344680b7e81047a42b433fe213bc521ce90b9d8 100644 (file)
@@ -265,6 +265,15 @@ void D2SimpleParser::parse(const D2CfgContextPtr& ctx,
         ctx->setContext(user);
     }
 
+    ConstElementPtr socket = config->get("control-socket");
+    if (socket) {
+        if (socket->getType() != Element::map) {
+            isc_throw(D2CfgError, "Specified control-socket is expected to be a map"
+                      ", i.e. a structure defined within { }");
+        }
+        ctx->setControlSocketInfo(socket);
+    }
+
     // Attempt to create the new client config. This ought to fly as
     // we already validated everything.
     D2ParamsPtr params(new D2Params(ip_address, port, dns_server_timeout,
index e1fc414e623a3850e140efc0df62e5de3c522cd2..682043ba4f60b2b0f6fb0f7f1170b10cbd013725 100644 (file)
@@ -39,7 +39,7 @@ public:
 
     /// @brief Defines currently supported scopes
     ///
-    /// D2Parser may eventually support multiple levels of parsing scope. 
+    /// D2Parser may eventually support multiple levels of parsing scope.
     /// Currently it supports only the D2 module scope which expects the data
     /// to be parsed to be a map containing the DhcpDdns element and its
     /// constituents.
index 2579b2ef46a50b83face60aa9217732703a4134f..f663b5696fa5d5248639e4fd6b5ad107fb44293d 100644 (file)
@@ -80,9 +80,9 @@ public:
             " \"dns-server-timeout\": " << dns_server_timeout << " , "
             " \"ncr-protocol\": \"" << ncr_protocol << "\" , "
             " \"ncr-format\": \"" << ncr_format << "\", "
-            "\"tsig-keys\": [], "
-            "\"forward-ddns\" : {}, "
-            "\"reverse-ddns\" : {} "
+            " \"tsig-keys\": [], "
+            " \"forward-ddns\" : {}, "
+            " \"reverse-ddns\" : {} "
             "}";
 
         return (config.str());
@@ -261,9 +261,9 @@ TEST_F(D2CfgMgrTest, defaultValues) {
             " \"dns-server-timeout\": 333 , "
             " \"ncr-protocol\": \"UDP\" , "
             " \"ncr-format\": \"JSON\", "
-            "\"tsig-keys\": [], "
-            "\"forward-ddns\" : {}, "
-            "\"reverse-ddns\" : {} "
+            " \"tsig-keys\": [], "
+            " \"forward-ddns\" : {}, "
+            " \"reverse-ddns\" : {} "
             "}";
 
     RUN_CONFIG_OK(config);
@@ -279,9 +279,9 @@ TEST_F(D2CfgMgrTest, defaultValues) {
             " \"dns-server-timeout\": 333 , "
             " \"ncr-protocol\": \"UDP\" , "
             " \"ncr-format\": \"JSON\", "
-            "\"tsig-keys\": [], "
-            "\"forward-ddns\" : {}, "
-            "\"reverse-ddns\" : {} "
+            " \"tsig-keys\": [], "
+            " \"forward-ddns\" : {}, "
+            " \"reverse-ddns\" : {} "
             "}";
 
     RUN_CONFIG_OK(config);
@@ -296,9 +296,9 @@ TEST_F(D2CfgMgrTest, defaultValues) {
             " \"port\": 777 , "
             " \"ncr-protocol\": \"UDP\" , "
             " \"ncr-format\": \"JSON\", "
-            "\"tsig-keys\": [], "
-            "\"forward-ddns\" : {}, "
-            "\"reverse-ddns\" : {} "
+            " \"tsig-keys\": [], "
+            " \"forward-ddns\" : {}, "
+            " \"reverse-ddns\" : {} "
             "}";
 
     RUN_CONFIG_OK(config);
@@ -313,9 +313,9 @@ TEST_F(D2CfgMgrTest, defaultValues) {
             " \"port\": 777 , "
             " \"dns-server-timeout\": 333 , "
             " \"ncr-format\": \"JSON\", "
-            "\"tsig-keys\": [], "
-            "\"forward-ddns\" : {}, "
-            "\"reverse-ddns\" : {} "
+            " \"tsig-keys\": [], "
+            " \"forward-ddns\" : {}, "
+            " \"reverse-ddns\" : {} "
             "}";
 
     RUN_CONFIG_OK(config);
@@ -331,9 +331,9 @@ TEST_F(D2CfgMgrTest, defaultValues) {
             " \"port\": 777 , "
             " \"dns-server-timeout\": 333 , "
             " \"ncr-protocol\": \"UDP\", "
-            "\"tsig-keys\": [], "
-            "\"forward-ddns\" : {}, "
-            "\"reverse-ddns\" : {} "
+            " \"tsig-keys\": [], "
+            " \"forward-ddns\" : {}, "
+            " \"reverse-ddns\" : {} "
             "}";
 
     RUN_CONFIG_OK(config);
@@ -353,13 +353,13 @@ TEST_F(D2CfgMgrTest, unsupportedTopLevelItems) {
             " \"dns-server-timeout\": 333 , "
             " \"ncr-protocol\": \"UDP\" , "
             " \"ncr-format\": \"JSON\", "
-            "\"tsig-keys\": [], "
-            "\"forward-ddns\" : {}, "
-            "\"reverse-ddns\" : {}, "
-            "\"bogus-param\" : true "
+            " \"tsig-keys\": [], "
+            " \"forward-ddns\" : {}, "
+            " \"reverse-ddns\" : {}, "
+            " \"bogus-param\" : true "
             "}";
 
-    SYNTAX_ERROR(config, "<string>:1.181-193: got unexpected "
+    SYNTAX_ERROR(config, "<string>:1.185-197: got unexpected "
                          "keyword \"bogus-param\" in DhcpDdns map.");
 
     // Check that unsupported top level objects fails.  For
@@ -371,14 +371,14 @@ TEST_F(D2CfgMgrTest, unsupportedTopLevelItems) {
             " \"dns-server-timeout\": 333 , "
             " \"ncr-protocol\": \"UDP\" , "
             " \"ncr-format\": \"JSON\", "
-            "\"tsig-keys\": [], "
-            "\"bogus-object-one\" : {}, "
-            "\"forward-ddns\" : {}, "
-            "\"reverse-ddns\" : {}, "
-            "\"bogus-object-two\" : {} "
+            " \"tsig-keys\": [], "
+            " \"bogus-object-one\" : {}, "
+            " \"forward-ddns\" : {}, "
+            " \"reverse-ddns\" : {}, "
+            " \"bogus-object-two\" : {} "
             "}";
 
-    SYNTAX_ERROR(config, "<string>:1.139-156: got unexpected"
+    SYNTAX_ERROR(config, "<string>:1.141-158: got unexpected"
                          " keyword \"bogus-object-one\" in DhcpDdns map.");
 }
 
@@ -426,6 +426,8 @@ TEST_F(D2CfgMgrTest, invalidEntry) {
                          " unexpected constant string, expecting JSON");
 }
 
+// Control socket tests in d2_process_unittests.cc
+
 // DdnsDomainList and TSIGKey tests moved to d2_simple_parser_unittest.cc
 
 /// @brief Tests construction of D2CfgMgr
@@ -462,9 +464,13 @@ TEST_F(D2CfgMgrTest, fullConfig) {
     std::string config = "{ "
                         "\"ip-address\" : \"192.168.1.33\" , "
                         "\"port\" : 88 , "
-                        " \"dns-server-timeout\": 333 , "
-                        " \"ncr-protocol\": \"UDP\" , "
-                        " \"ncr-format\": \"JSON\", "
+                        "\"dns-server-timeout\": 333 , "
+                        "\"ncr-protocol\": \"UDP\" , "
+                        "\"ncr-format\": \"JSON\", "
+                        "\"control-socket\" : {"
+                        " \"socket-type\" : \"unix\" ,"
+                        " \"socket-name\" : \"/tmp/d2-ctrl-channel\" "
+                        "},"
                         "\"tsig-keys\": ["
                         "{"
                         "  \"name\": \"d2_key.example.com\" , "
@@ -533,6 +539,16 @@ TEST_F(D2CfgMgrTest, fullConfig) {
     EXPECT_EQ(dhcp_ddns::NCR_UDP, d2_params->getNcrProtocol());
     EXPECT_EQ(dhcp_ddns::FMT_JSON, d2_params->getNcrFormat());
 
+    // Verify that the control socket can be retrieved.
+    ConstElementPtr ctrl_sock = context->getControlSocketInfo();
+    ASSERT_TRUE(ctrl_sock);
+    ASSERT_EQ(Element::map, ctrl_sock->getType());
+    EXPECT_EQ(2, ctrl_sock->size());
+    ASSERT_TRUE(ctrl_sock->get("socket-type"));
+    EXPECT_EQ("\"unix\"", ctrl_sock->get("socket-type")->str());
+    ASSERT_TRUE(ctrl_sock->get("socket-name"));
+    EXPECT_EQ("\"/tmp/d2-ctrl-channel\"", ctrl_sock->get("socket-name")->str());
+
     // Verify that the forward manager can be retrieved.
     DdnsDomainListMgrPtr mgr = context->getForwardMgr();
     ASSERT_TRUE(mgr);
@@ -941,6 +957,11 @@ TEST_F(D2CfgMgrTest, comments) {
                         "\"comment\": \"D2 config\" , "
                         "\"ip-address\" : \"192.168.1.33\" , "
                         "\"port\" : 88 , "
+                        "\"control-socket\": {"
+                        " \"comment\": \"Control channel\" , "
+                        " \"socket-type\": \"unix\" ,"
+                        " \"socket-name\": \"/tmp/d2-ctrl-channel\" "
+                        "},"
                         "\"tsig-keys\": ["
                         "{"
                         "  \"user-context\": { "
@@ -976,6 +997,13 @@ TEST_F(D2CfgMgrTest, comments) {
     ASSERT_TRUE(ctx->get("comment"));
     EXPECT_EQ("\"D2 config\"", ctx->get("comment")->str());
 
+    // Check control socket.
+    ConstElementPtr ctrl_sock = d2_context->getControlSocketInfo();
+    ASSERT_TRUE(ctrl_sock);
+    ASSERT_TRUE(ctrl_sock->get("user-context"));
+    EXPECT_EQ("{ \"comment\": \"Control channel\" }",
+              ctrl_sock->get("user-context")->str());
+
     // Check TSIG keys.
     TSIGKeyInfoMapPtr keys = d2_context->getKeys();
     ASSERT_TRUE(keys);