From df3f1a9164fd6498648fbfc46591918bbda119d6 Mon Sep 17 00:00:00 2001 From: Razvan Becheriu Date: Thu, 19 Dec 2024 16:50:20 +0200 Subject: [#2101] load hooks using name only --- src/lib/dhcpsrv/tests/Makefile.am | 1 + src/lib/dhcpsrv/tests/dhcp_parsers_unittest.cc | 43 ++++++++++++++++++++++++++ src/lib/hooks/Makefile.am | 1 + src/lib/hooks/hooks_parser.cc | 7 +++++ src/lib/hooks/hooks_parser.h | 4 +++ 5 files changed, 56 insertions(+) (limited to 'src') diff --git a/src/lib/dhcpsrv/tests/Makefile.am b/src/lib/dhcpsrv/tests/Makefile.am index 289a8fdc9f..bc9b150ab0 100644 --- a/src/lib/dhcpsrv/tests/Makefile.am +++ b/src/lib/dhcpsrv/tests/Makefile.am @@ -6,6 +6,7 @@ AM_CPPFLAGS += -DTEST_DATA_BUILDDIR=\"$(abs_top_builddir)/src/lib/dhcpsrv/tests\ AM_CPPFLAGS += -DDHCP_DATA_DIR=\"$(abs_top_builddir)/src/lib/dhcpsrv/tests\" AM_CPPFLAGS += -DKEA_LFC_BUILD_DIR=\"$(abs_top_builddir)/src/bin/lfc\" AM_CPPFLAGS += -DINSTALL_PROG=\"$(abs_top_srcdir)/install-sh\" +AM_CPPFLAGS += -DDEFAULT_HOOKS_PATH=\"$(libdir)/kea/hooks\" AM_CXXFLAGS = $(KEA_CXXFLAGS) diff --git a/src/lib/dhcpsrv/tests/dhcp_parsers_unittest.cc b/src/lib/dhcpsrv/tests/dhcp_parsers_unittest.cc index 9ea17057c3..af1ef3daca 100644 --- a/src/lib/dhcpsrv/tests/dhcp_parsers_unittest.cc +++ b/src/lib/dhcpsrv/tests/dhcp_parsers_unittest.cc @@ -34,6 +34,7 @@ #include #include #include +#include #include #include @@ -52,6 +53,7 @@ using namespace isc::dhcp; using namespace isc::dhcp::test; using namespace isc::hooks; using namespace isc::test; +using namespace isc::util::file; namespace { @@ -327,11 +329,13 @@ public: ParseConfigTest() :family_(AF_INET6) { reset_context(); + HooksLibrariesParser::default_hooks_path_ = string(DEFAULT_HOOKS_PATH); } ~ParseConfigTest() { reset_context(); CfgMgr::instance().clear(); + HooksLibrariesParser::default_hooks_path_ = string(DEFAULT_HOOKS_PATH); } /// @brief Parses a configuration. @@ -2277,6 +2281,45 @@ TEST_F(ParseConfigTest, oneHooksLibrary) { EXPECT_EQ(CALLOUT_LIBRARY_1, hooks_libraries[0]); } +// hooks-libraries element that contains a single library with relative path. +TEST_F(ParseConfigTest, oneHooksLibraryRelativePath) { + // Check that no libraries are currently loaded + vector hooks_libraries = HooksManager::getLibraryNames(); + EXPECT_TRUE(hooks_libraries.empty()); + + Path path(CALLOUT_LIBRARY_1); + + HooksLibrariesParser::default_hooks_path_ = path.parentPath(); + + // Configuration with hooks-libraries set to a single library. + string config = setHooksLibrariesConfig(path.filename().c_str()); + + // Verify that the configuration string parses. + const int rcode = parseConfiguration(config); + ASSERT_TRUE(rcode == 0) << error_text_; + + config = setHooksLibrariesConfig(CALLOUT_LIBRARY_1); + + // Verify that the configuration object unparses. + ConstElementPtr expected; + ASSERT_NO_THROW(expected = + Element::fromJSON(config)->get("hooks-libraries")); + ASSERT_TRUE(expected); + const HooksConfig& cfg = + CfgMgr::instance().getStagingCfg()->getHooksConfig(); + runToElementTest(expected, cfg); + + // Check that the parser recorded a single library. + isc::hooks::HookLibsCollection libraries = getLibraries(); + ASSERT_EQ(1, libraries.size()); + EXPECT_EQ(CALLOUT_LIBRARY_1, libraries[0].first); + + // Check that the change was propagated to the hooks manager. + hooks_libraries = HooksManager::getLibraryNames(); + ASSERT_EQ(1, hooks_libraries.size()); + EXPECT_EQ(CALLOUT_LIBRARY_1, hooks_libraries[0]); +} + // hooks-libraries element that contains two libraries TEST_F(ParseConfigTest, twoHooksLibraries) { // Check that no libraries are currently loaded diff --git a/src/lib/hooks/Makefile.am b/src/lib/hooks/Makefile.am index 44a61fd3d1..94143c251b 100644 --- a/src/lib/hooks/Makefile.am +++ b/src/lib/hooks/Makefile.am @@ -1,6 +1,7 @@ SUBDIRS = . tests AM_CPPFLAGS = -I$(top_builddir)/src/lib -I$(top_srcdir)/src/lib +AM_CPPFLAGS += -DDEFAULT_HOOKS_PATH=\"$(libdir)/kea/hooks\" AM_CPPFLAGS += $(BOOST_INCLUDES) AM_CXXFLAGS = $(KEA_CXXFLAGS) diff --git a/src/lib/hooks/hooks_parser.cc b/src/lib/hooks/hooks_parser.cc index 56029896fe..62c47b13e8 100644 --- a/src/lib/hooks/hooks_parser.cc +++ b/src/lib/hooks/hooks_parser.cc @@ -23,6 +23,8 @@ namespace hooks { // @todo use the flat style, split into list and item +string HooksLibrariesParser::default_hooks_path_ = string(DEFAULT_HOOKS_PATH); + void HooksLibrariesParser::parse(HooksConfig& libraries, ConstElementPtr value) { // Initialize. @@ -77,6 +79,11 @@ HooksLibrariesParser::parse(HooksConfig& libraries, ConstElementPtr value) { entry_item.second->getPosition() << ")"); } + // If only the name of the library was provided, add the full path. + if (libname.find("/") == string::npos) { + libname = HooksLibrariesParser::default_hooks_path_ + "/" + libname; + } + // Note we have found the library name. lib_found = true; continue; diff --git a/src/lib/hooks/hooks_parser.h b/src/lib/hooks/hooks_parser.h index 441e56c1f1..0fea1fe26b 100644 --- a/src/lib/hooks/hooks_parser.h +++ b/src/lib/hooks/hooks_parser.h @@ -57,6 +57,10 @@ public: /// @param libraries parsed libraries information will be stored here /// @param value pointer to the content to be parsed void parse(HooksConfig& libraries, isc::data::ConstElementPtr value); + + /// @brief The default installation path for hooks, used to generate full + /// path if only the hook name is provided. + static std::string default_hooks_path_; }; } // namespace isc::hooks -- cgit v1.2.3