From: Francis Dupont Date: Fri, 28 Dec 2018 00:53:56 +0000 (+0100) Subject: [30-implement-control-socket-for-ddns-2] Ported more code and some tests X-Git-Tag: 208-move-logging-from-global-objects-to-global-params_base~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2c6124fb1f87115c2667a59ce29a441d76495377;p=thirdparty%2Fkea.git [30-implement-control-socket-for-ddns-2] Ported more code and some tests --- diff --git a/src/bin/d2/d2_cfg_mgr.cc b/src/bin/d2/d2_cfg_mgr.cc index cb163d1fbc..a79a839d08 100644 --- a/src/bin/d2/d2_cfg_mgr.cc +++ b/src/bin/d2/d2_cfg_mgr.cc @@ -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()); diff --git a/src/bin/d2/d2_cfg_mgr.h b/src/bin/d2/d2_cfg_mgr.h index 83f9d167c8..a3e1ec3ee8 100644 --- a/src/bin/d2/d2_cfg_mgr.h +++ b/src/bin/d2/d2_cfg_mgr.h @@ -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 diff --git a/src/bin/d2/d2_config.h b/src/bin/d2/d2_config.h index e92bb9b263..713c37496e 100644 --- a/src/bin/d2/d2_config.h +++ b/src/bin/d2/d2_config.h @@ -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" diff --git a/src/bin/d2/d2_controller.cc b/src/bin/d2/d2_controller.cc index d86857db09..93fbd63c65 100644 --- a/src/bin/d2/d2_controller.cc +++ b/src/bin/d2/d2_controller.cc @@ -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; diff --git a/src/bin/d2/d2_simple_parser.cc b/src/bin/d2/d2_simple_parser.cc index 89654d8593..2344680b7e 100644 --- a/src/bin/d2/d2_simple_parser.cc +++ b/src/bin/d2/d2_simple_parser.cc @@ -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, diff --git a/src/bin/d2/parser_context.h b/src/bin/d2/parser_context.h index e1fc414e62..682043ba4f 100644 --- a/src/bin/d2/parser_context.h +++ b/src/bin/d2/parser_context.h @@ -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. diff --git a/src/bin/d2/tests/d2_cfg_mgr_unittests.cc b/src/bin/d2/tests/d2_cfg_mgr_unittests.cc index 2579b2ef46..f663b5696f 100644 --- a/src/bin/d2/tests/d2_cfg_mgr_unittests.cc +++ b/src/bin/d2/tests/d2_cfg_mgr_unittests.cc @@ -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, ":1.181-193: got unexpected " + SYNTAX_ERROR(config, ":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, ":1.139-156: got unexpected" + SYNTAX_ERROR(config, ":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);