]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#3831] Agent uses restricted socket paths
authorThomas Markwalder <tmark@isc.org>
Fri, 16 May 2025 16:56:47 +0000 (12:56 -0400)
committerThomas Markwalder <tmark@isc.org>
Mon, 19 May 2025 12:12:55 +0000 (12:12 +0000)
modified:   ../../../doc/examples/agent/comments.json
modified:   ../../../doc/examples/agent/simple.json
modified:   ca_cfg_mgr.cc
modified:   ca_command_mgr.cc
modified:   simple_parser.cc
modified:   tests/ca_cfg_mgr_unittests.cc
modified:   tests/ca_command_mgr_unittests.cc
modified:   tests/ca_controller_unittests.cc
modified:   tests/get_config_unittest.cc
modified:   tests/parser_unittests.cc
modified:   tests/testdata/get_config.json

doc/examples/agent/comments.json
doc/examples/agent/simple.json
src/bin/agent/ca_cfg_mgr.cc
src/bin/agent/ca_command_mgr.cc
src/bin/agent/simple_parser.cc
src/bin/agent/tests/ca_cfg_mgr_unittests.cc
src/bin/agent/tests/ca_command_mgr_unittests.cc
src/bin/agent/tests/ca_controller_unittests.cc
src/bin/agent/tests/get_config_unittest.cc
src/bin/agent/tests/parser_unittests.cc
src/bin/agent/tests/testdata/get_config.json

index b382a66bc3674232ae5da3f51c9bdf1473dd3194..61f8980d8ade80c90b670ae396d1c13dbd7206c4 100644 (file)
@@ -53,7 +53,7 @@
             {
                 "comment": "control socket for DHCPv4 server",
                 "socket-type": "unix",
-                "socket-name": "/tmp/kea4-ctrl-socket"
+                "socket-name": "kea4-ctrl-socket"
             }
         },
 
index 10947d7eced1b728b3d0bbe7eb91d35fe79a2ae8..4b5b76a7b43e9dcb8e2262b2575783e13265f0f4 100644 (file)
             {
                 "comment": "socket to DHCPv4 server",
                 "socket-type": "unix",
-                "socket-name": "/tmp/kea4-ctrl-socket"
+                "socket-name": "kea4-ctrl-socket"
             },
 
             // Location of the DHCPv6 command channel socket.
             "dhcp6":
             {
                 "socket-type": "unix",
-                "socket-name": "/tmp/kea6-ctrl-socket"
+                "socket-name": "kea6-ctrl-socket"
             },
 
             // Location of the D2 command channel socket.
             "d2":
             {
                 "socket-type": "unix",
-                "socket-name": "/tmp/kea-ddns-ctrl-socket",
+                "socket-name": "kea-ddns-ctrl-socket",
                 "user-context": { "in-use": false }
             }
         },
index 95057f6231f8cb6138c92e3679e6ed5575925ce0..c07d69a3276c9fcbeb124d617b0e0576c8a8e49b 100644 (file)
@@ -206,9 +206,13 @@ CtrlAgentCfgContext::toElement() const {
     // Set control-sockets
     ElementPtr control_sockets = Element::createMap();
     for (auto const& si : ctrl_sockets_) {
-        ConstElementPtr socket = UserContext::toElement(si.second);
-        control_sockets->set(si.first, socket);
+        // Remove validated_path.
+        auto mutable_socket_info = boost::const_pointer_cast<Element>(
+                                   UserContext::toElement(si.second));
+        mutable_socket_info->remove("validated-socket-name");
+        control_sockets->set(si.first, mutable_socket_info);
     }
+
     ca->set("control-sockets", control_sockets);
     // Set Control-agent
     ElementPtr result = Element::createMap();
index 9411181dc46f75a4a09374e5e7921cdd96563daa..f22094aeb2f376f56f13381d37d1c9f577a616cb 100644 (file)
@@ -220,9 +220,15 @@ CtrlAgentCommandMgr::forwardCommand(const std::string& service,
                   " for the server type " << service);
     }
 
-    // If the configuration does its job properly the socket-name must be
-    // specified and must be a string value.
-    std::string socket_name = socket_info->get("socket-name")->stringValue();
+    // If the configuration does its job properly the validated-socket-name
+    // should be  present
+    if (!socket_info->get("validated-socket-name")) {
+        isc_throw(Unexpected, "validated-socket-name missing from "
+                  << " socket_info: " << socket_info->str()
+                  << " for the server type " << service);
+    }
+
+    auto socket_name = socket_info->get("validated-socket-name")->stringValue();
 
     // Forward command and receive reply.
     IOServicePtr io_service(new IOService());;
index b50cda14409c488b99af5b4edc620fb4aa984079..2b36bfd0667c88d53ddf854deb05086e01d02e14 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2017-2024 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2017-2025 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
 #include <asiolink/io_service_mgr.h>
 #include <cc/data.h>
 #include <cc/dhcp_config_error.h>
+#include <config/unix_command_config.h>
 #include <hooks/hooks_manager.h>
 #include <hooks/hooks_parser.h>
 #include <http/basic_auth_config.h>
 
 using namespace isc::data;
 using namespace isc::asiolink;
+using namespace isc::config;
 
 namespace isc {
 namespace agent {
@@ -149,7 +151,14 @@ AgentSimpleParser::parse(const CtrlAgentCfgContextPtr& ctx,
     if (ctrl_sockets) {
         auto const& sockets_map = ctrl_sockets->mapValue();
         for (auto const& cs : sockets_map) {
-            ctx->setControlSocketInfo(cs.second, cs.first);
+            // Add a validated socket name so we can suppress it in 
+            // toElement() but don't have to revalidate it every time we
+            // want to use it.
+            auto mutable_socket_info = boost::const_pointer_cast<Element>(cs.second);
+            std::string socket_name = mutable_socket_info->get("socket-name")->stringValue();
+            auto validated_name = UnixCommandConfig::validatePath(socket_name);
+            mutable_socket_info->set("validated-socket-name", Element::create(validated_name));
+            ctx->setControlSocketInfo(mutable_socket_info, cs.first);
         }
     }
 
index fee541074c767fde68bafbc444883a7d08e2587c..f873e373e2ec35c2318b6d8cf4c39b58c5dd4545 100644 (file)
@@ -7,6 +7,7 @@
 #include <config.h>
 #include <agent/ca_cfg_mgr.h>
 #include <agent/parser_context.h>
+#include <config/unix_command_config.h>
 #include <exceptions/exceptions.h>
 #include <process/testutils/d_test_stubs.h>
 #include <process/d_cfg_mgr.h>
@@ -14,6 +15,7 @@
 #include <agent/tests/test_callout_libraries.h>
 #include <agent/tests/test_data_files_config.h>
 #include <hooks/hooks_parser.h>
+#include <util/filesystem.h>
 #include <boost/pointer_cast.hpp>
 #include <boost/scoped_ptr.hpp>
 #include <gtest/gtest.h>
@@ -23,6 +25,8 @@ using namespace isc::data;
 using namespace isc::hooks;
 using namespace isc::http;
 using namespace isc::process;
+using namespace isc::config;
+using namespace isc::util;
 
 namespace  {
 
@@ -239,8 +243,8 @@ TEST(CtrlAgentCfgMgr, contextAuthConfigFile) {
 
     auth->setRealm("foobar");
     auth->setDirectory("/tmp");
-    auth->add("", "/tmp/foo", "", "/tmp/bar");
-    auth->add("", "/tmp/test", "", "/tmp/pwd");
+    auth->add("", "foo", "", "bar");
+    auth->add("", "test", "", "pwd");
 
     const HttpAuthConfigPtr& stored_auth = ctx.getAuthConfig();
     ASSERT_TRUE(stored_auth);
@@ -265,7 +269,7 @@ const char* AGENT_CONFIGS[] = {
     "    \"http-port\": 8001,\n"
     "    \"control-sockets\": {\n"
     "        \"dhcp4\": {\n"
-    "            \"socket-name\": \"/tmp/socket-v4\"\n"
+    "            \"socket-name\": \"socket-v4\"\n"
     "        }\n"
     "    }\n"
     "}",
@@ -276,13 +280,13 @@ const char* AGENT_CONFIGS[] = {
     "    \"http-port\": 8001,\n"
     "    \"control-sockets\": {\n"
     "        \"dhcp4\": {\n"
-    "            \"socket-name\": \"/tmp/socket-v4\"\n"
+    "            \"socket-name\": \"socket-v4\"\n"
     "        },\n"
     "        \"dhcp6\": {\n"
-    "            \"socket-name\": \"/tmp/socket-v6\"\n"
+    "            \"socket-name\": \"socket-v6\"\n"
     "        },\n"
     "        \"d2\": {\n"
-    "            \"socket-name\": \"/tmp/socket-d2\"\n"
+    "            \"socket-name\": \"socket-d2\"\n"
     "        }\n"
     "   }\n"
     "}",
@@ -295,7 +299,7 @@ const char* AGENT_CONFIGS[] = {
     "    \"http-port\": 8001,\n"
     "    \"control-sockets\": {\n"
     "        \"dhcp4\": {\n"
-    "            \"socket-name\": \"/tmp/socket-v4\"\n"
+    "            \"socket-name\": \"socket-v4\"\n"
     "        }\n"
     "   },\n"
     "    \"hooks-libraries\": ["
@@ -314,7 +318,7 @@ const char* AGENT_CONFIGS[] = {
     "    \"http-port\": 8001,\n"
     "    \"control-sockets\": {\n"
     "        \"d2\": {\n"
-    "            \"socket-name\": \"/tmp/socket-d2\"\n"
+    "            \"socket-name\": \"socket-d2\"\n"
     "        }\n"
     "    }\n"
     "}",
@@ -325,7 +329,7 @@ const char* AGENT_CONFIGS[] = {
     "    \"http-port\": 8001,\n"
     "    \"control-sockets\": {\n"
     "        \"dhcp6\": {\n"
-    "            \"socket-name\": \"/tmp/socket-v6\"\n"
+    "            \"socket-name\": \"socket-v6\"\n"
     "        }\n"
     "    }\n"
     "}",
@@ -349,7 +353,7 @@ const char* AGENT_CONFIGS[] = {
     "    },\n"
     "    \"control-sockets\": {\n"
     "        \"dhcp4\": {\n"
-    "            \"socket-name\": \"/tmp/socket-v4\"\n"
+    "            \"socket-name\": \"socket-v4\"\n"
     "        }\n"
     "    }\n"
     "}",
@@ -382,10 +386,10 @@ const char* AGENT_CONFIGS[] = {
     "    \"control-sockets\": {\n"
     "        \"dhcp4\": {\n"
     "            \"comment\": \"dhcp4 socket\",\n"
-    "            \"socket-name\": \"/tmp/socket-v4\"\n"
+    "            \"socket-name\": \"socket-v4\"\n"
     "        },\n"
     "        \"dhcp6\": {\n"
-    "            \"socket-name\": \"/tmp/socket-v6\",\n"
+    "            \"socket-name\": \"socket-v6\",\n"
     "            \"user-context\": { \"version\": 1 }\n"
     "        }\n"
     "    }\n"
@@ -420,7 +424,7 @@ const char* AGENT_CONFIGS[] = {
     "    },\n"
     "    \"control-sockets\": {\n"
     "        \"dhcp4\": {\n"
-    "            \"socket-name\": \"/tmp/socket-v4\"\n"
+    "            \"socket-name\": \"socket-v4\"\n"
     "        }\n"
     "    }\n"
     "}"
@@ -431,10 +435,12 @@ class AgentParserTest : public isc::process::ConfigParseTest {
 public:
     virtual void SetUp() {
         resetHooksPath();
+        setSocketTestPath();
     }
 
     virtual void TearDown() {
         resetHooksPath();
+        resetSocketPath();
     }
 
     /// @brief Sets the Hooks path from which hooks can be loaded.
@@ -450,6 +456,20 @@ public:
         HooksLibrariesParser::getHooksPath(true);
     }
 
+    /// @brief Sets the path in which the socket can be created.
+    /// @param explicit_path path to use as the socket path.
+    void setSocketTestPath(const std::string explicit_path = "") {
+        auto path = UnixCommandConfig::getSocketPath(true, (!explicit_path.empty() ?
+                                                     explicit_path : TEST_DATA_BUILDDIR));
+        UnixCommandConfig::setSocketPathPerms(file::getPermissions(path));
+    }
+
+    /// @brief Resets the socket path to the default.
+    void resetSocketPath() {
+        UnixCommandConfig::getSocketPath(true);
+        UnixCommandConfig::setSocketPathPerms();
+    }
+
     /// @brief Tries to load input text as a configuration
     ///
     /// @param config text containing input configuration
@@ -473,6 +493,20 @@ public:
         return (txt);
     }
 
+    /// @brief Make expected contents of socket info with socket path added.
+    ///
+    /// @param name name of the socket
+    /// @return expected string
+    std::string makeSocketStr(const std::string& name) {
+        std::ostringstream os;
+        os << "{ \"socket-name\": \""
+           << name << "\", \"socket-type\": \"unix\","
+           << " \"validated-socket-name\": \""
+           << UnixCommandConfig::getSocketPath() << "/" << name
+           << "\" }";
+        return (os.str());
+    }
+
     /// Configuration Manager (used in tests)
     NakedAgentCfgMgr cfg_mgr_;
 };
@@ -506,8 +540,7 @@ TEST_F(AgentParserTest, configParseSocketDhcp4) {
     ASSERT_TRUE(ctx);
     ConstElementPtr socket = ctx->getControlSocketInfo("dhcp4");
     ASSERT_TRUE(socket);
-    EXPECT_EQ("{ \"socket-name\": \"/tmp/socket-v4\", \"socket-type\": \"unix\" }",
-              socket->str());
+    EXPECT_EQ(makeSocketStr("socket-v4"), socket->str());
     EXPECT_FALSE(ctx->getControlSocketInfo("dhcp6"));
     EXPECT_FALSE(ctx->getControlSocketInfo("d2"));
 }
@@ -522,9 +555,7 @@ TEST_F(AgentParserTest, configParseSocketD2) {
     ASSERT_TRUE(ctx);
     ConstElementPtr socket = ctx->getControlSocketInfo("d2");
     ASSERT_TRUE(socket);
-    EXPECT_EQ("{ \"socket-name\": \"/tmp/socket-d2\", \"socket-type\": \"unix\" }",
-              socket->str());
-
+    EXPECT_EQ(makeSocketStr("socket-d2"), socket->str());
     EXPECT_FALSE(ctx->getControlSocketInfo("dhcp4"));
     EXPECT_FALSE(ctx->getControlSocketInfo("dhcp6"));
 }
@@ -539,8 +570,7 @@ TEST_F(AgentParserTest, configParseSocketDhcp6) {
     ASSERT_TRUE(ctx);
     ConstElementPtr socket = ctx->getControlSocketInfo("dhcp6");
     ASSERT_TRUE(socket);
-    EXPECT_EQ("{ \"socket-name\": \"/tmp/socket-v6\", \"socket-type\": \"unix\" }",
-              socket->str());
+    EXPECT_EQ(makeSocketStr("socket-v6"), socket->str());
     EXPECT_FALSE(ctx->getControlSocketInfo("dhcp4"));
     EXPECT_FALSE(ctx->getControlSocketInfo("d2"));
 }
@@ -555,14 +585,11 @@ TEST_F(AgentParserTest, configParse3Sockets) {
     ConstElementPtr socket4 = ctx->getControlSocketInfo("dhcp4");
     ConstElementPtr socket6 = ctx->getControlSocketInfo("dhcp6");
     ASSERT_TRUE(socket2);
-    EXPECT_EQ("{ \"socket-name\": \"/tmp/socket-d2\", \"socket-type\": \"unix\" }",
-              socket2->str());
+    EXPECT_EQ(makeSocketStr("socket-d2"), socket2->str());
     ASSERT_TRUE(socket4);
-    EXPECT_EQ("{ \"socket-name\": \"/tmp/socket-v4\", \"socket-type\": \"unix\" }",
-              socket4->str());
+    EXPECT_EQ(makeSocketStr("socket-v4"), socket4->str());
     ASSERT_TRUE(socket6);
-    EXPECT_EQ("{ \"socket-name\": \"/tmp/socket-v6\", \"socket-type\": \"unix\" }",
-              socket6->str());
+    EXPECT_EQ(makeSocketStr("socket-v6"), socket6->str());
 }
 
 // This test checks that the config file with hook library specified can be
index 4cef0426cd69a81286bfa36c8003c87d1d7f09fb..31f36bedefb3d48ab6a303aa02beedf55b108239 100644 (file)
@@ -15,7 +15,9 @@
 #include <asiolink/testutils/test_server_unix_socket.h>
 #include <cc/command_interpreter.h>
 #include <cc/data.h>
+#include <config/unix_command_config.h>
 #include <process/testutils/d_test_stubs.h>
+#include <util/filesystem.h>
 #include <boost/pointer_cast.hpp>
 #include <gtest/gtest.h>
 #include <testutils/sandbox.h>
@@ -28,6 +30,8 @@ using namespace isc::agent;
 using namespace isc::asiolink;
 using namespace isc::data;
 using namespace isc::process;
+using namespace isc::config;
+using namespace isc::util;
 
 namespace {
 
@@ -50,6 +54,7 @@ public:
         : DControllerTest(CtrlAgentController::instance),
           mgr_(CtrlAgentCommandMgr::instance()) {
         mgr_.deregisterAll();
+        setSocketTestPath();
         removeUnixSocketFile();
         initProcess();
     }
@@ -60,6 +65,7 @@ public:
     virtual ~CtrlAgentCommandMgrTest() {
         mgr_.deregisterAll();
         removeUnixSocketFile();
+        resetSocketPath();
     }
 
     /// @brief Verifies received answer
@@ -106,19 +112,22 @@ public:
     }
 
     /// @brief Returns socket file path.
-    ///
-    /// If the KEA_SOCKET_TEST_DIR environment variable is specified, the
-    /// socket file is created in the location pointed to by this variable.
-    /// Otherwise, it is created in the build directory.
     std::string unixSocketFilePath() {
-        std::string socket_path;
-        const char* env = getenv("KEA_SOCKET_TEST_DIR");
-        if (env) {
-            socket_path = std::string(env) + "/test-socket";
-        } else {
-            socket_path = sandbox.join("test-socket");
-        }
-        return (socket_path);
+        return (UnixCommandConfig::getSocketPath() + "/test-socket");
+    }
+
+    /// @brief Sets the path in which the socket can be created.
+    /// @param explicit_path path to use as the socket path.
+    void setSocketTestPath(const std::string explicit_path = "") {
+        auto path = UnixCommandConfig::getSocketPath(true, (!explicit_path.empty() ?
+                                                     explicit_path : TEST_DATA_BUILDDIR));
+        UnixCommandConfig::setSocketPathPerms(file::getPermissions(path));
+    }
+
+    /// @brief Resets the socket path to the default.
+    void resetSocketPath() {
+        UnixCommandConfig::getSocketPath(true);
+        UnixCommandConfig::setSocketPathPerms();
     }
 
     /// @brief Removes unix socket descriptor.
@@ -158,7 +167,7 @@ public:
         ASSERT_TRUE(ctx);
 
         ElementPtr control_socket = Element::createMap();
-        control_socket->set("socket-name",
+        control_socket->set("validated-socket-name",
                             Element::create(unixSocketFilePath()));
         ctx->setControlSocketInfo(control_socket, service);
     }
index c5b800d5cc29ca8497842507192fda1bf8e0a53e..26a335ee81f40b243a44e21cba519bf0731ea177 100644 (file)
@@ -12,7 +12,9 @@
 #include <agent/ca_command_mgr.h>
 #include <cc/data.h>
 #include <cc/command_interpreter.h>
+#include <config/unix_command_config.h>
 #include <process/testutils/d_test_stubs.h>
+#include <util/filesystem.h>
 #include <testutils/gtest_utils.h>
 
 #include <boost/pointer_cast.hpp>
@@ -27,6 +29,8 @@ using namespace isc::agent;
 using namespace isc::data;
 using namespace isc::http;
 using namespace isc::process;
+using namespace isc::config;
+using namespace isc::util;
 using namespace boost::posix_time;
 using namespace std;
 
@@ -40,11 +44,11 @@ const char* valid_agent_config =
     "  \"control-sockets\": {"
     "    \"dhcp4\": {"
     "      \"socket-type\": \"unix\","
-    "      \"socket-name\": \"/first/dhcp4/socket\""
+    "      \"socket-name\": \"first_socket4\""
     "    },"
     "    \"dhcp6\": {"
     "      \"socket-type\": \"unix\","
-    "      \"socket-name\": \"/first/dhcp6/socket\""
+    "      \"socket-name\": \"first_socket6\""
     "    }"
     "  }"
     "}";
@@ -61,6 +65,11 @@ public:
     /// @brief Constructor.
     CtrlAgentControllerTest()
         : DControllerTest(CtrlAgentController::instance) {
+        setSocketTestPath();
+    }
+
+    virtual ~CtrlAgentControllerTest() {
+        resetSocketPath();
     }
 
     /// @brief Returns pointer to CtrlAgentProcess instance.
@@ -86,6 +95,20 @@ public:
         return (p);
     }
 
+    /// @brief Sets the path in which the socket can be created.
+    /// @param explicit_path path to use as the socket path.
+    void setSocketTestPath(const std::string explicit_path = "") {
+        auto path = UnixCommandConfig::getSocketPath(true, (!explicit_path.empty() ?
+                                                     explicit_path : TEST_DATA_BUILDDIR));
+        UnixCommandConfig::setSocketPathPerms(file::getPermissions(path));
+    }
+
+    /// @brief Resets the socket path to the default.
+    void resetSocketPath() {
+        UnixCommandConfig::getSocketPath(true);
+        UnixCommandConfig::setSocketPathPerms();
+    }
+
     /// @brief Tests that socket info structure contains 'unix' socket-type
     /// value and the expected socket-name.
     ///
@@ -101,8 +124,7 @@ public:
         ASSERT_TRUE(sock_info->contains("socket-type"));
         EXPECT_EQ("unix", sock_info->get("socket-type")->stringValue());
         ASSERT_TRUE(sock_info->contains("socket-name"));
-        EXPECT_EQ(exp_socket_name,
-                  sock_info->get("socket-name")->stringValue());
+        EXPECT_EQ(exp_socket_name, sock_info->get("socket-name")->stringValue());
     }
 
     /// @brief Compares the status in the given parse result to a given value.
@@ -280,11 +302,11 @@ TEST_F(CtrlAgentControllerTest, successfulConfigUpdate) {
         "  \"control-sockets\": {"
         "    \"dhcp4\": {"
         "      \"socket-type\": \"unix\","
-        "      \"socket-name\": \"/second/dhcp4/socket\""
+        "      \"socket-name\": \"second_socket4\""
         "    },"
         "    \"dhcp6\": {"
         "      \"socket-type\": \"unix\","
-        "      \"socket-name\": \"/second/dhcp6/socket\""
+        "      \"socket-name\": \"second_socket6\""
         "    }"
         "  }"
         "}";
@@ -323,8 +345,8 @@ TEST_F(CtrlAgentControllerTest, successfulConfigUpdate) {
     EXPECT_EQ(8080, ctx->getHttpPort());
 
     // The forwarding configuration should have been updated too.
-    testUnixSocketInfo("dhcp4", "/second/dhcp4/socket");
-    testUnixSocketInfo("dhcp6", "/second/dhcp6/socket");
+    testUnixSocketInfo("dhcp4", "second_socket4");
+    testUnixSocketInfo("dhcp6", "second_socket6");
 
     // After the shutdown the HTTP listener no longer exists.
     CtrlAgentProcessPtr process = getCtrlAgentProcess();
@@ -346,11 +368,11 @@ TEST_F(CtrlAgentControllerTest, unsuccessfulConfigUpdate) {
         "  \"control-sockets\": {"
         "    \"dhcp4\": {"
         "      \"socket-type\": \"unix\","
-        "      \"socket-name\": \"/second/dhcp4/socket\""
+        "      \"socket-name\": \"second_socket4\""
         "    },"
         "    \"dhcp6\": {"
         "      \"socket-type\": \"unix\","
-        "      \"socket-name\": \"/second/dhcp6/socket\""
+        "      \"socket-name\": \"second_socket6\""
         "    }"
         "  }"
         "}";
@@ -389,8 +411,8 @@ TEST_F(CtrlAgentControllerTest, unsuccessfulConfigUpdate) {
     EXPECT_EQ(8081, ctx->getHttpPort());
 
     // Same for forwarding.
-    testUnixSocketInfo("dhcp4", "/first/dhcp4/socket");
-    testUnixSocketInfo("dhcp6", "/first/dhcp6/socket");
+    testUnixSocketInfo("dhcp4", "first_socket4");
+    testUnixSocketInfo("dhcp6", "first_socket6");
 
     // After the shutdown the HTTP listener no longer exists.
     CtrlAgentProcessPtr process = getCtrlAgentProcess();
@@ -412,11 +434,11 @@ TEST_F(CtrlAgentControllerTest, noListenerChangeHttp) {
         "  \"control-sockets\": {"
         "    \"dhcp4\": {"
         "      \"socket-type\": \"unix\","
-        "      \"socket-name\": \"/second/dhcp4/socket\""
+        "      \"socket-name\": \"second_socket4\""
         "    },"
         "    \"dhcp6\": {"
         "      \"socket-type\": \"unix\","
-        "      \"socket-name\": \"/second/dhcp6/socket\""
+        "      \"socket-name\": \"second_socket6\""
         "    }"
         "  }"
         "}";
@@ -471,8 +493,8 @@ TEST_F(CtrlAgentControllerTest, noListenerChangeHttp) {
     EXPECT_EQ(8081, ctx->getHttpPort());
 
     // The forwarding configuration should have been updated.
-    testUnixSocketInfo("dhcp4", "/second/dhcp4/socket");
-    testUnixSocketInfo("dhcp6", "/second/dhcp6/socket");
+    testUnixSocketInfo("dhcp4", "second_socket4");
+    testUnixSocketInfo("dhcp6", "second_socket6");
 
     CtrlAgentProcessPtr process = getCtrlAgentProcess();
     ASSERT_TRUE(process);
@@ -497,11 +519,11 @@ TEST_F(CtrlAgentControllerTest, noListenerChangeHttps) {
              << "  \"control-sockets\": {"
              << "    \"dhcp4\": {"
              << "      \"socket-type\": \"unix\","
-             << "      \"socket-name\": \"/first/dhcp4/socket\""
+             << "      \"socket-name\": \"first_socket4\""
              << "    },"
              << "    \"dhcp6\": {"
              << "      \"socket-type\": \"unix\","
-             << "      \"socket-name\": \"/first/dhcp6/socket\""
+             << "      \"socket-name\": \"first_socket6\""
              << "    }"
              << "  }"
              << "}";
@@ -516,11 +538,11 @@ TEST_F(CtrlAgentControllerTest, noListenerChangeHttps) {
              << "  \"control-sockets\": {"
              << "    \"dhcp4\": {"
              << "      \"socket-type\": \"unix\","
-             << "      \"socket-name\": \"/second/dhcp4/socket\""
+             << "      \"socket-name\": \"second_socket4\""
              << "    },"
              << "    \"dhcp6\": {"
              << "      \"socket-type\": \"unix\","
-             << "      \"socket-name\": \"/second/dhcp6/socket\""
+             << "      \"socket-name\": \"second_socket6\""
              << "    }"
              << "  }"
              << "}";
@@ -579,8 +601,8 @@ TEST_F(CtrlAgentControllerTest, noListenerChangeHttps) {
     EXPECT_EQ(8081, ctx->getHttpPort());
 
     // The forwarding configuration should have been updated.
-    testUnixSocketInfo("dhcp4", "/second/dhcp4/socket");
-    testUnixSocketInfo("dhcp6", "/second/dhcp6/socket");
+    testUnixSocketInfo("dhcp4", "second_socket4");
+    testUnixSocketInfo("dhcp6", "second_socket6");
 
     CtrlAgentProcessPtr process = getCtrlAgentProcess();
     ASSERT_TRUE(process);
@@ -604,11 +626,11 @@ TEST_F(CtrlAgentControllerTest, handleHttpToHttpsSwitch) {
              << "  \"control-sockets\": {"
              << "    \"dhcp4\": {"
              << "      \"socket-type\": \"unix\","
-             << "      \"socket-name\": \"/second/dhcp4/socket\""
+             << "      \"socket-name\": \"second_socket4\""
              << "    },"
              << "    \"dhcp6\": {"
              << "      \"socket-type\": \"unix\","
-             << "      \"socket-name\": \"/second/dhcp6/socket\""
+             << "      \"socket-name\": \"second_socket6\""
              << "    }"
              << "  }"
              << "}";
@@ -663,8 +685,8 @@ TEST_F(CtrlAgentControllerTest, handleHttpToHttpsSwitch) {
     EXPECT_EQ(8081, ctx->getHttpPort());
 
     // The forwarding configuration should have not been updated.
-    testUnixSocketInfo("dhcp4", "/first/dhcp4/socket");
-    testUnixSocketInfo("dhcp6", "/first/dhcp6/socket");
+    testUnixSocketInfo("dhcp4", "first_socket4");
+    testUnixSocketInfo("dhcp6", "first_socket6");
 
     CtrlAgentProcessPtr process = getCtrlAgentProcess();
     ASSERT_TRUE(process);
@@ -686,11 +708,11 @@ TEST_F(CtrlAgentControllerTest, handleHttpsToHttpSwitch) {
              << "  \"control-sockets\": {"
              << "    \"dhcp4\": {"
              << "      \"socket-type\": \"unix\","
-             << "      \"socket-name\": \"/first/dhcp4/socket\""
+             << "      \"socket-name\": \"first_socket4\""
              << "    },"
              << "    \"dhcp6\": {"
              << "      \"socket-type\": \"unix\","
-             << "      \"socket-name\": \"/first/dhcp6/socket\""
+             << "      \"socket-name\": \"first_socket6\""
              << "    }"
              << "  }"
              << "}";
@@ -703,11 +725,11 @@ TEST_F(CtrlAgentControllerTest, handleHttpsToHttpSwitch) {
              << "  \"control-sockets\": {"
              << "    \"dhcp4\": {"
              << "      \"socket-type\": \"unix\","
-             << "      \"socket-name\": \"/second/dhcp4/socket\""
+             << "      \"socket-name\": \"second_socket4\""
              << "    },"
              << "    \"dhcp6\": {"
              << "      \"socket-type\": \"unix\","
-             << "      \"socket-name\": \"/second/dhcp6/socket\""
+             << "      \"socket-name\": \"second_socket6\""
              << "    }"
              << "  }"
              << "}";
@@ -766,8 +788,8 @@ TEST_F(CtrlAgentControllerTest, handleHttpsToHttpSwitch) {
     EXPECT_EQ(8081, ctx->getHttpPort());
 
     // The forwarding configuration should have not been updated.
-    testUnixSocketInfo("dhcp4", "/first/dhcp4/socket");
-    testUnixSocketInfo("dhcp6", "/first/dhcp6/socket");
+    testUnixSocketInfo("dhcp4", "first_socket4");
+    testUnixSocketInfo("dhcp6", "first_socket6");
 
     CtrlAgentProcessPtr process = getCtrlAgentProcess();
     ASSERT_TRUE(process);
index 5a94d3848dc70960d97dd964f9a7b70e2d72ea04..eac18255a108be913f94690c0dfc2e2db5a0dd3a 100644 (file)
@@ -8,12 +8,14 @@
 
 #include <cc/data.h>
 #include <cc/command_interpreter.h>
+#include <config/unix_command_config.h>
 #include <testutils/user_context_utils.h>
 #include <process/redact_config.h>
 #include <process/testutils/d_test_stubs.h>
 #include <agent/ca_cfg_mgr.h>
 #include <agent/parser_context.h>
 #include <hooks/hooks_parser.h>
+#include <util/filesystem.h>
 #include <boost/scoped_ptr.hpp>
 #include <gtest/gtest.h>
 
@@ -31,6 +33,8 @@ using namespace isc::data;
 using namespace isc::process;
 using namespace isc::test;
 using namespace isc::hooks;
+using namespace isc::config;
+using namespace isc::util;
 
 namespace {
 
@@ -141,11 +145,13 @@ public:
         srv_.reset(new NakedAgentCfgMgr());
         // Create fresh context.
         resetConfiguration();
+        setSocketTestPath();
     }
 
     ~CtrlAgentGetCfgTest() {
         resetConfiguration();
         resetHooksPath();
+        resetSocketPath();
     }
 
     /// @brief Sets the Hooks path from which hooks can be loaded.
@@ -161,6 +167,20 @@ public:
         HooksLibrariesParser::getHooksPath(true);
     }
 
+    /// @brief Sets the path in which the socket can be created.
+    /// @param explicit_path path to use as the socket path.
+    void setSocketTestPath(const std::string explicit_path = "") {
+        auto path = UnixCommandConfig::getSocketPath(true, (!explicit_path.empty() ?
+                                                     explicit_path : TEST_DATA_BUILDDIR));
+        UnixCommandConfig::setSocketPathPerms(file::getPermissions(path));
+    }
+
+    /// @brief Resets the socket path to the default.
+    void resetSocketPath() {
+        UnixCommandConfig::getSocketPath(true);
+        UnixCommandConfig::setSocketPathPerms();
+    }
+
     /// @brief Parse and Execute configuration
     ///
     /// Parses a configuration and executes a configuration of the server.
index e9c19b27ba97b3ba60a47bc72d20ddbbcd3d8dc6..097fba08f817e19866b90be419e5607a901bbc0c 100644 (file)
@@ -136,15 +136,15 @@ TEST(ParserTest, keywordAgent) {
         "    \"control-sockets\": {"
         "        \"dhcp4\": {"
         "            \"socket-type\": \"unix\","
-        "            \"socket-name\": \"/tmp/kea4-ctrl-socket\""
+        "            \"socket-name\": \"kea4-ctrl-socket\""
         "        },"
         "        \"dhcp6\": {"
         "            \"socket-type\": \"unix\","
-        "            \"socket-name\": \"/tmp/kea6-ctrl-socket\""
+        "            \"socket-name\": \"kea6-ctrl-socket\""
         "        },"
         "        \"d2\": {"
         "            \"socket-type\": \"unix\","
-        "            \"socket-name\": \"/tmp/kea-ddns-ctrl-socket\""
+        "            \"socket-name\": \"kea-ddns-ctrl-socket\""
         "        }"
         "    },"
         "    \"hooks-libraries\": ["
@@ -173,15 +173,15 @@ TEST(ParserTest, keywordSubAgent) {
         "    \"control-sockets\": {"
         "        \"dhcp4\": {"
         "            \"socket-type\": \"unix\","
-        "            \"socket-name\": \"/tmp/kea4-ctrl-socket\""
+        "            \"socket-name\": \"kea4-ctrl-socket\""
         "        },"
         "        \"dhcp6\": {"
         "            \"socket-type\": \"unix\","
-        "            \"socket-name\": \"/tmp/kea6-ctrl-socket\""
+        "            \"socket-name\": \"kea6-ctrl-socket\""
         "        },"
         "        \"d2\": {"
         "            \"socket-type\": \"unix\","
-        "            \"socket-name\": \"/tmp/kea-ddns-ctrl-socket\""
+        "            \"socket-name\": \"kea-ddns-ctrl-socket\""
         "        }"
         "    },"
         "    \"hooks-libraries\": ["
@@ -888,15 +888,15 @@ TEST_F(TrailingCommasTest, tests) {
   "Control-agent": {
     "control-sockets": {
       "d2": {
-        "socket-name": "/tmp/kea-dhcp-ddns-ctrl.sock",
+        "socket-name": "kea-dhcp-ddns-ctrl.sock",
         "socket-type": "unix",
       },
       "dhcp4": {
-        "socket-name": "/tmp/kea-dhcp4-ctrl.sock",
+        "socket-name": "kea-dhcp4-ctrl.sock",
         "socket-type": "unix",
       },
       "dhcp6": {
-        "socket-name": "/tmp/kea-dhcp6-ctrl.sock",
+        "socket-name": "kea-dhcp6-ctrl.sock",
         "socket-type": "unix",
       },
     },
index b7afd468e63070cf78fb1322589b53f15ddf7dab..a021cb9756193219174b84bbb49b74727f3224f1 100644 (file)
         },
         "control-sockets": {
             "d2": {
-                "socket-name": "/tmp/kea-ddns-ctrl-socket",
+                "socket-name": "kea-ddns-ctrl-socket",
                 "socket-type": "unix",
                 "user-context": {
                     "in-use": false
                 }
             },
             "dhcp4": {
-                "socket-name": "/tmp/kea4-ctrl-socket",
+                "socket-name": "kea4-ctrl-socket",
                 "socket-type": "unix",
                 "user-context": {
                     "comment": "socket to DHCPv4 server"
                 }
             },
             "dhcp6": {
-                "socket-name": "/tmp/kea6-ctrl-socket",
+                "socket-name": "kea6-ctrl-socket",
                 "socket-type": "unix"
             }
         },