Skip to content

Commit df3f1a9

Browse files
Razvan Becheriufxdupont
authored andcommitted
[#2101] load hooks using name only
1 parent 05707e6 commit df3f1a9

File tree

6 files changed

+83
-0
lines changed

6 files changed

+83
-0
lines changed

doc/sphinx/arm/hooks.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,33 @@ configuration would be:
213213
because the parameters specified for the library (or the files those
214214
parameters point to) may have changed.
215215

216+
Since Kea-2.7.5, the server is able to load hooks specified only by name, if
217+
they reside in the default install location (the path is OS specific).
218+
219+
::
220+
221+
"hooks-libraries": [
222+
{
223+
"library": "first_custom_hooks_example.so"
224+
},
225+
{
226+
"library": "second_custom_hooks_example.so"
227+
}
228+
]
229+
230+
This snipper (on Ubuntu 24.04) is equivalent to:
231+
232+
::
233+
234+
"hooks-libraries": [
235+
{
236+
"library": "/usr/lib/x86_64-linux-gnu/kea/hooks/first_custom_hooks_example.so"
237+
},
238+
{
239+
"library": "/usr/lib/x86_64-linux-gnu/kea/hooks/second_custom_hooks_example.so"
240+
}
241+
]
242+
216243
Libraries may have additional parameters that are not mandatory, in the
217244
sense that there may be libraries that do not require them. However, for any
218245
given library there is often a requirement to specify a certain

src/lib/dhcpsrv/tests/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ AM_CPPFLAGS += -DTEST_DATA_BUILDDIR=\"$(abs_top_builddir)/src/lib/dhcpsrv/tests\
66
AM_CPPFLAGS += -DDHCP_DATA_DIR=\"$(abs_top_builddir)/src/lib/dhcpsrv/tests\"
77
AM_CPPFLAGS += -DKEA_LFC_BUILD_DIR=\"$(abs_top_builddir)/src/bin/lfc\"
88
AM_CPPFLAGS += -DINSTALL_PROG=\"$(abs_top_srcdir)/install-sh\"
9+
AM_CPPFLAGS += -DDEFAULT_HOOKS_PATH=\"$(libdir)/kea/hooks\"
910

1011
AM_CXXFLAGS = $(KEA_CXXFLAGS)
1112

src/lib/dhcpsrv/tests/dhcp_parsers_unittest.cc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <exceptions/exceptions.h>
3535
#include <hooks/hooks_parser.h>
3636
#include <hooks/hooks_manager.h>
37+
#include <util/filesystem.h>
3738
#include <testutils/gtest_utils.h>
3839
#include <testutils/test_to_element.h>
3940

@@ -52,6 +53,7 @@ using namespace isc::dhcp;
5253
using namespace isc::dhcp::test;
5354
using namespace isc::hooks;
5455
using namespace isc::test;
56+
using namespace isc::util::file;
5557

5658
namespace {
5759

@@ -327,11 +329,13 @@ class ParseConfigTest : public ::testing::Test {
327329
ParseConfigTest()
328330
:family_(AF_INET6) {
329331
reset_context();
332+
HooksLibrariesParser::default_hooks_path_ = string(DEFAULT_HOOKS_PATH);
330333
}
331334

332335
~ParseConfigTest() {
333336
reset_context();
334337
CfgMgr::instance().clear();
338+
HooksLibrariesParser::default_hooks_path_ = string(DEFAULT_HOOKS_PATH);
335339
}
336340

337341
/// @brief Parses a configuration.
@@ -2277,6 +2281,45 @@ TEST_F(ParseConfigTest, oneHooksLibrary) {
22772281
EXPECT_EQ(CALLOUT_LIBRARY_1, hooks_libraries[0]);
22782282
}
22792283

2284+
// hooks-libraries element that contains a single library with relative path.
2285+
TEST_F(ParseConfigTest, oneHooksLibraryRelativePath) {
2286+
// Check that no libraries are currently loaded
2287+
vector<string> hooks_libraries = HooksManager::getLibraryNames();
2288+
EXPECT_TRUE(hooks_libraries.empty());
2289+
2290+
Path path(CALLOUT_LIBRARY_1);
2291+
2292+
HooksLibrariesParser::default_hooks_path_ = path.parentPath();
2293+
2294+
// Configuration with hooks-libraries set to a single library.
2295+
string config = setHooksLibrariesConfig(path.filename().c_str());
2296+
2297+
// Verify that the configuration string parses.
2298+
const int rcode = parseConfiguration(config);
2299+
ASSERT_TRUE(rcode == 0) << error_text_;
2300+
2301+
config = setHooksLibrariesConfig(CALLOUT_LIBRARY_1);
2302+
2303+
// Verify that the configuration object unparses.
2304+
ConstElementPtr expected;
2305+
ASSERT_NO_THROW(expected =
2306+
Element::fromJSON(config)->get("hooks-libraries"));
2307+
ASSERT_TRUE(expected);
2308+
const HooksConfig& cfg =
2309+
CfgMgr::instance().getStagingCfg()->getHooksConfig();
2310+
runToElementTest<HooksConfig>(expected, cfg);
2311+
2312+
// Check that the parser recorded a single library.
2313+
isc::hooks::HookLibsCollection libraries = getLibraries();
2314+
ASSERT_EQ(1, libraries.size());
2315+
EXPECT_EQ(CALLOUT_LIBRARY_1, libraries[0].first);
2316+
2317+
// Check that the change was propagated to the hooks manager.
2318+
hooks_libraries = HooksManager::getLibraryNames();
2319+
ASSERT_EQ(1, hooks_libraries.size());
2320+
EXPECT_EQ(CALLOUT_LIBRARY_1, hooks_libraries[0]);
2321+
}
2322+
22802323
// hooks-libraries element that contains two libraries
22812324
TEST_F(ParseConfigTest, twoHooksLibraries) {
22822325
// Check that no libraries are currently loaded

src/lib/hooks/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
SUBDIRS = . tests
22

33
AM_CPPFLAGS = -I$(top_builddir)/src/lib -I$(top_srcdir)/src/lib
4+
AM_CPPFLAGS += -DDEFAULT_HOOKS_PATH=\"$(libdir)/kea/hooks\"
45
AM_CPPFLAGS += $(BOOST_INCLUDES)
56
AM_CXXFLAGS = $(KEA_CXXFLAGS)
67

src/lib/hooks/hooks_parser.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ namespace hooks {
2323

2424
// @todo use the flat style, split into list and item
2525

26+
string HooksLibrariesParser::default_hooks_path_ = string(DEFAULT_HOOKS_PATH);
27+
2628
void
2729
HooksLibrariesParser::parse(HooksConfig& libraries, ConstElementPtr value) {
2830
// Initialize.
@@ -77,6 +79,11 @@ HooksLibrariesParser::parse(HooksConfig& libraries, ConstElementPtr value) {
7779
entry_item.second->getPosition() << ")");
7880
}
7981

82+
// If only the name of the library was provided, add the full path.
83+
if (libname.find("/") == string::npos) {
84+
libname = HooksLibrariesParser::default_hooks_path_ + "/" + libname;
85+
}
86+
8087
// Note we have found the library name.
8188
lib_found = true;
8289
continue;

src/lib/hooks/hooks_parser.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ class HooksLibrariesParser : public isc::data::SimpleParser {
5757
/// @param libraries parsed libraries information will be stored here
5858
/// @param value pointer to the content to be parsed
5959
void parse(HooksConfig& libraries, isc::data::ConstElementPtr value);
60+
61+
/// @brief The default installation path for hooks, used to generate full
62+
/// path if only the hook name is provided.
63+
static std::string default_hooks_path_;
6064
};
6165

6266
} // namespace isc::hooks

0 commit comments

Comments
 (0)