]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#2006] Updated parser, doc and one unit test
authorFrancis Dupont <fdupont@isc.org>
Wed, 8 Dec 2021 17:54:55 +0000 (18:54 +0100)
committerRazvan Becheriu <razvan@isc.org>
Fri, 21 Jan 2022 17:15:38 +0000 (17:15 +0000)
doc/sphinx/arm/hooks-ha.rst
src/hooks/dhcp/high_availability/ha_config_parser.cc
src/hooks/dhcp/high_availability/tests/Makefile.am
src/hooks/dhcp/high_availability/tests/ha_config_unittest.cc
src/lib/http/basic_auth_config.cc

index 3dd873db56afc93973f30e17d543e2a75b30ce94..ed0f8ad108d030567f84b9b8e6308753e3e58983 100644 (file)
@@ -768,6 +768,10 @@ list:
    authentication. This parameter is ignored when the user ID is not specified or is empty.
    The password is optional; if not specified, an empty password is used.
 
+-  ``basic-auth-password-file`` - is an alternative to ``basic-auth-password``:
+   instead to put the password itself in the configuration file it is
+   in the file specifies by this parameter.
+
 -  ``role`` - denotes the role of the server in the HA setup. The
    following roles are supported in the load-balancing configuration:
    ``primary``, ``secondary``, and ``backup``. There must be exactly one
index 2d1a41033eb70198709ac000e7bf55def38e9a90..41fca75704a0252ccd51f49878de0344d628356f 100644 (file)
@@ -10,6 +10,7 @@
 #include <ha_log.h>
 #include <ha_service_states.h>
 #include <cc/dhcp_config_error.h>
+#include <util/file_utilities.h>
 #include <limits>
 #include <set>
 
@@ -271,8 +272,25 @@ HAConfigParser::parseInternal(const HAConfigPtr& config_storage,
         // Basic HTTP authentication password.
         std::string password;
         if ((*p)->contains("basic-auth-password")) {
+            if ((*p)->contains("basic-auth-password-file")) {
+                isc_throw(dhcp::DhcpConfigError, "only one of "
+                          << "basic-auth-password and "
+                          << "basic-auth-password-file parameter can be "
+                          << "configured in peer '"
+                          << cfg->getName() << "'");
+            }
             password = getString((*p), "basic-auth-password");
         }
+        if ((*p)->contains("basic-auth-password-file")) {
+            std::string password_file =
+                getString((*p), "basic-auth-password-file");
+            try {
+                password = util::file::getContent(password_file);
+            } catch (const std::exception& ex) {
+                isc_throw(dhcp::DhcpConfigError, "bad password file in peer '"
+                          << cfg->getName() << "': " << ex.what());
+            }
+        }
 
         // Basic HTTP authentication user.
         if ((*p)->contains("basic-auth-user")) {
index 322992a4bcb6ad42dd55d24123a8ba0c334d5cec..ab3f8e38e8660efa72b8ca46fd1e33ded799917d 100644 (file)
@@ -7,6 +7,8 @@ AM_CPPFLAGS += -DLIBDHCP_HA_SO=\"$(abs_top_builddir)/src/hooks/dhcp/high_availab
 AM_CPPFLAGS += -DINSTALL_PROG=\"$(abs_top_srcdir)/install-sh\"
 TEST_CA_DIR = $(abs_top_srcdir)/src/lib/asiolink/testutils/ca
 AM_CPPFLAGS += -DTEST_CA_DIR=\"$(TEST_CA_DIR)\"
+TEST_HTTP_DIR = $(abs_top_srcdir)/src/lib/http/tests/testdata
+AM_CPPFLAGS += -DTEST_HTTP_DIR=\"$(TEST_HTTP_DIR)\"
 
 AM_CXXFLAGS = $(KEA_CXXFLAGS)
 
index 4e06db426aef3df80b3887c14576f4e360849a83..a6dcb4ca912538f2e8987de8a2c7d1fdcc6d6705 100644 (file)
@@ -373,8 +373,9 @@ TEST_F(HAConfigTest, configurePassiveBackup) {
         "            {"
         "                \"name\": \"server3\","
         "                \"url\": \"http://127.0.0.1:8082/\","
-        "                \"basic-auth-user\": \"test\","
-        "                \"basic-auth-password\": \"123\\u00a3\","
+        "                \"basic-auth-user\": \"keatest\","
+        "                \"basic-auth-password-file\": \""
+                             TEST_HTTP_DIR "/hiddenp\","
         "                \"role\": \"backup\""
         "            }"
         "        ]"
@@ -408,7 +409,7 @@ TEST_F(HAConfigTest, configurePassiveBackup) {
     EXPECT_EQ("http://127.0.0.1:8082/", cfg->getUrl().toText());
     EXPECT_EQ(HAConfig::PeerConfig::BACKUP, cfg->getRole());
     ASSERT_TRUE(cfg->getBasicAuth());
-    EXPECT_EQ("dGVzdDoxMjPCow==", cfg->getBasicAuth()->getCredential());
+    EXPECT_EQ("a2VhdGVzdDpLZWFUZXN0", cfg->getBasicAuth()->getCredential());
 
     // Verify multi-threading default values.
     EXPECT_FALSE(impl->getConfig()->getEnableMultiThreading());
index 71f1df841cf1330b83664b728257ad5d5f17232f..67b5977b7a142e2e16ea758904f3a204e31fbdd6 100644 (file)
@@ -8,12 +8,9 @@
 
 #include <http/auth_log.h>
 #include <http/basic_auth_config.h>
+#include <util/file_utilities.h>
 #include <util/strutil.h>
 
-#include <cerrno>
-#include <cstring>
-#include <sys/stat.h>
-
 using namespace isc;
 using namespace isc::data;
 using namespace isc::dhcp;
@@ -112,37 +109,10 @@ BasicHttpAuthConfig::getFileContent(const std::string& file_name) const {
         path += file_name.substr(1);
     }
 
-    // Open the file.
-    int fd = ::open(path.c_str(), O_RDONLY);
-    if (fd < 0) {
-        isc_throw(DhcpConfigError, "can't open file '" << path << "': "
-                  << std::strerror(errno));
-    }
     try {
-        struct stat stats;
-        if (fstat(fd, &stats) < 0) {
-            isc_throw(DhcpConfigError, "can't stat file '" << path << "': "
-                      << std::strerror(errno));
-        }
-        if ((stats.st_mode & S_IFMT) != S_IFREG) {
-            isc_throw(DhcpConfigError, "'" << path
-                      << "' must be a regular file");
-        }
-        string content(stats.st_size, ' ');
-        ssize_t got = ::read(fd, &content[0], stats.st_size);
-        if (got < 0) {
-            isc_throw(DhcpConfigError, "can't read file '" << path << "': "
-                      << std::strerror(errno));
-        }
-        if (got != stats.st_size) {
-            isc_throw(DhcpConfigError, "can't read whole file '" << path
-                      << "' (got " << got << " of " << stats.st_size);
-        }
-        close(fd);
-        return (content);
-    } catch (const std::exception&) {
-        close(fd);
-        throw;
+        return (file::getContent(path));
+    } catch (const isc::BadValue& ex) {
+        isc_throw(DhcpConfigError, ex.what());
     }
 }