]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[3826] addressed review comments
authorRazvan Becheriu <razvan@isc.org>
Thu, 31 Jul 2025 08:54:52 +0000 (11:54 +0300)
committerRazvan Becheriu <razvan@isc.org>
Thu, 31 Jul 2025 14:22:37 +0000 (14:22 +0000)
16 files changed:
ChangeLog
src/bin/admin/tests/mysql_tests.sh.in
src/bin/admin/tests/pgsql_tests.sh.in
src/hooks/dhcp/lease_cmds/lease_cmds.cc
src/hooks/dhcp/lease_cmds/libloadtests/lease_cmds4_unittest.cc
src/hooks/dhcp/lease_cmds/libloadtests/lease_cmds6_unittest.cc
src/lib/dhcpsrv/memfile_lease_mgr.cc
src/lib/dhcpsrv/memfile_lease_storage.h
src/lib/mysql/mysql_constants.h
src/lib/pgsql/pgsql_connection.h
src/share/database/scripts/mysql/dhcpdb_create.mysql
src/share/database/scripts/mysql/meson.build
src/share/database/scripts/mysql/upgrade_031_to_032.sh.in [new file with mode: 0755]
src/share/database/scripts/pgsql/dhcpdb_create.pgsql
src/share/database/scripts/pgsql/meson.build
src/share/database/scripts/pgsql/upgrade_030_to_031.sh.in [new file with mode: 0755]

index 5d7b9a003f1e1dc15084d32ddfd24cf204d9d449..e8834390b5bcf3fd5d326f8bd64a1108ac42f5c4 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2382.  [build]         razvan
+       Implemented the 'lease6-get-by-hw-address' command used to query
+       IPv6 leases by HW Address.
+       (Gitlab #3826)
+
 Kea 3.1.0 (development) released on July 30, 2025
 
 2381.  [build]         razvan
index c4331707b1e421a3caf65b12c452064072dad66c..4a48050ef0a00f311cc2dd786d5a7fc0efa4397b 100755 (executable)
@@ -157,7 +157,7 @@ mysql_db_version_test() {
     run_command \
         "${kea_admin}" db-version mysql -u "${db_user}" -p "${db_password}" -n "${db_name}"
     version="${OUTPUT}"
-    assert_str_eq "31.0" "${version}" "Expected kea-admin to return %s, returned value was %s"
+    assert_str_eq "32.0" "${version}" "Expected kea-admin to return %s, returned value was %s"
 
     # Let's wipe the whole database
     mysql_wipe
@@ -1200,7 +1200,7 @@ mysql_upgrade_test() {
 
     # Verify that the upgraded schema reports the latest version.
     version=$("${kea_admin}" db-version mysql -u "${db_user}" -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}")
-    assert_str_eq "31.0" "${version}" "Expected kea-admin to return %s, returned value was %s"
+    assert_str_eq "32.0" "${version}" "Expected kea-admin to return %s, returned value was %s"
 
     # Let's check that the new tables are indeed there.
 
index e20b6ad02554d92ec945998bfd5c34d5276cfcbb..b287c2335381de0b9ae566cae23c96ac72f9c8e5 100755 (executable)
@@ -158,7 +158,7 @@ pgsql_db_version_test() {
     run_command \
         "${kea_admin}" db-version pgsql -u "${db_user}" -p "${db_password}" -n "${db_name}"
     version="${OUTPUT}"
-    assert_str_eq "30.0" "${version}" "Expected kea-admin to return %s, returned value was %s"
+    assert_str_eq "31.0" "${version}" "Expected kea-admin to return %s, returned value was %s"
 
     # Let's wipe the whole database
     pgsql_wipe
@@ -1171,7 +1171,7 @@ pgsql_upgrade_test() {
 
     # Verify upgraded schema reports the latest version.
     version=$("${kea_admin}" db-version pgsql -u "${db_user}" -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}")
-    assert_str_eq "30.0" "${version}" 'Expected kea-admin to return %s, returned value was %s'
+    assert_str_eq "31.0" "${version}" 'Expected kea-admin to return %s, returned value was %s'
 
     # Check 1.0 to 2.0 upgrade
     pgsql_upgrade_1_0_to_2_0_test
index b4a4f84aeb1b889e5c708e588f3d2e91458a73a3..c0fc5a7869ddc32024199f2e9448dc2283a67bf8 100644 (file)
@@ -1597,6 +1597,10 @@ LeaseCmdsImpl::leaseGetByHwAddressHandler(CalloutHandle& handle) {
             isc_throw(BadValue, "'hw-address' parameter must be a string");
         }
 
+        if (!v4 && hw_address->stringValue().empty()) {
+            isc_throw(BadValue, "'hw-address' parameter must not be empty");
+        }
+
         HWAddr hwaddr = HWAddr::fromText(hw_address->stringValue());
 
         ElementPtr leases_json = Element::createList();
index a4fb0a61d3cd34141b24343bb33b9c078c1381e3..a970224bcb71ab34f63af142d29d59b4b96e7516 100644 (file)
@@ -1794,6 +1794,16 @@ void Lease4CmdsTest::testLease4GetByHwAddressFind0() {
         "}";
     string exp_rsp = "0 IPv4 lease(s) found.";
     testCommand(cmd, CONTROL_RESULT_EMPTY, exp_rsp);
+
+    // Empty HWAddr.
+    cmd =
+        "{\n"
+        "    \"command\": \"lease4-get-by-hw-address\",\n"
+        "    \"arguments\": {"
+        "        \"hw-address\": \"\"\n"
+        "    }\n"
+        "}";
+    testCommand(cmd, CONTROL_RESULT_EMPTY, exp_rsp);
 }
 
 void Lease4CmdsTest::testLease4GetByHwAddressFind2() {
index 1b01ca69ac79e6d4d189963474528c03203621f0..cbcd6a3fc29be8318a0db082a2b8c388c71a8be0 100644 (file)
@@ -2014,6 +2014,17 @@ void Lease6CmdsTest::testLease6GetByHwAddressParams() {
     exp_rsp = "'hw-address' parameter must be a string";
     testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
 
+    // Empty HWAddr.
+    cmd =
+        "{\n"
+        "    \"command\": \"lease6-get-by-hw-address\",\n"
+        "    \"arguments\": {"
+        "        \"hw-address\": \"\"\n"
+        "    }\n"
+        "}";
+    exp_rsp = "'hw-address' parameter must not be empty";
+    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
+
     // Simply bad value.
     cmd =
         "{\n"
@@ -2071,10 +2082,10 @@ void Lease6CmdsTest::testLease6GetByHwAddressFind2() {
     // Let's check if the response makes any sense.
     ConstElementPtr lease = leases->get(0);
     ASSERT_TRUE(lease);
-    checkLease6(lease, "2001:db8:1::1", 0, 66, "42:42:42:42:42:42:42:42", "08:08:08:08:08:08");
+    checkLease6(lease, "2001:db8:2::1", 0, 99, "42:42:42:42:42:42:42:42", "08:08:08:08:08:08");
     lease = leases->get(1);
     ASSERT_TRUE(lease);
-    checkLease6(lease, "2001:db8:2::1", 0, 99, "42:42:42:42:42:42:42:42", "08:08:08:08:08:08");
+    checkLease6(lease, "2001:db8:1::1", 0, 66, "42:42:42:42:42:42:42:42", "08:08:08:08:08:08");
 }
 
 void Lease6CmdsTest::testLease6GetByDuidParams() {
index 84eef4842b4fba41af523f6d7cd36d15e3350bab..e6096e956460522283f12bfc912ad939c7f2da01 100644 (file)
@@ -1459,13 +1459,11 @@ Memfile_LeaseMgr::getLease6Internal(Lease::Type type,
 void
 Memfile_LeaseMgr::getLease6Internal(const HWAddr& hwaddr,
                                     Lease6Collection& collection) const {
-    // Using composite index by 'hw address' and 'subnet id'. It is
-    // ok to use it for searching by the 'hw address' only.
-    const Lease6StorageHWAddressSubnetIdIndex& idx =
-        storage6_.get<HWAddressSubnetIdIndexTag>();
-    std::pair<Lease6StorageHWAddressSubnetIdIndex::const_iterator,
-              Lease6StorageHWAddressSubnetIdIndex::const_iterator> l
-        = idx.equal_range(boost::make_tuple(hwaddr.hwaddr_));
+    const Lease6StorageHWAddressIndex& idx =
+        storage6_.get<HWAddressIndexTag>();
+    std::pair<Lease6StorageHWAddressIndex::const_iterator,
+              Lease6StorageHWAddressIndex::const_iterator> l
+        = idx.equal_range(hwaddr.hwaddr_);
 
     BOOST_FOREACH(auto const& lease, l) {
         collection.push_back(Lease6Ptr(new Lease6(*lease)));
index 006bafff2ec7e5cdcf960ba488b047f9ce40195c..8907f4f5210ecb364a76d3b4ea8a5e039d87243a 100644 (file)
@@ -37,6 +37,9 @@ struct ExpirationIndexTag { };
 /// @brief Tag for indexes by HW address, subnet-id tuple.
 struct HWAddressSubnetIdIndexTag { };
 
+/// @brief Tag for indexes by HW address.
+struct HWAddressIndexTag { };
+
 /// @brief Tag for indexes by client-id, subnet-id tuple.
 struct ClientIdSubnetIdIndexTag { };
 
@@ -176,25 +179,15 @@ typedef boost::multi_index_container<
             >
         >,
 
-        // Specification of the eight index starts here.
-        boost::multi_index::ordered_non_unique<
-            boost::multi_index::tag<HWAddressSubnetIdIndexTag>,
-            // This is a composite index that combines two attributes of the
-            // Lease6 object: hardware address and subnet id.
-            boost::multi_index::composite_key<
-                Lease6,
-                // The hardware address is held in the hwaddr_ member of the
-                // Lease4 object, which is a HWAddr object. Boost does not
-                // provide a key extractor for getting a member of a member,
-                // so we need a simple method for that.
-                boost::multi_index::const_mem_fun<Lease, const std::vector<uint8_t>&,
-                                                  &Lease::getHWAddrVector>,
-                // The subnet id is held in the subnet_id_ member of Lease6
-                // class. Note that the subnet_id_ is defined in the base
-                // class (Lease) so we have to point to this class rather
-                // than derived class: Lease6.
-                boost::multi_index::member<Lease, SubnetID, &Lease::subnet_id_>
-            >
+        // Specification of the eighth index starts here.
+        boost::multi_index::hashed_non_unique<
+            boost::multi_index::tag<HWAddressIndexTag>,
+            // The hardware address is held in the hwaddr_ member of the
+            // Lease6 object, which is a HWAddr object. Boost does not
+            // provide a key extractor for getting a member of a member,
+            // so we need a simple method for that.
+            boost::multi_index::const_mem_fun<Lease, const std::vector<uint8_t>&,
+                                              &Lease::getHWAddrVector>
         >
     >
 > Lease6Storage; // Specify the type name of this container.
@@ -364,9 +357,9 @@ typedef Lease6Storage::index<DuidIaidTypeIndexTag>::type Lease6StorageDuidIaidTy
 /// @brief DHCPv6 lease storage index by expiration time.
 typedef Lease6Storage::index<ExpirationIndexTag>::type Lease6StorageExpirationIndex;
 
-/// @brief DHCPv6 lease storage index by HW address and subnet-id.
-typedef Lease6Storage::index<HWAddressSubnetIdIndexTag>::type
-Lease6StorageHWAddressSubnetIdIndex;
+/// @brief DHCPv6 lease storage index by HW address.
+typedef Lease6Storage::index<HWAddressIndexTag>::type
+Lease6StorageHWAddressIndex;
 
 /// @brief DHCPv6 lease storage index by subnet-id.
 typedef Lease6Storage::index<SubnetIdIndexTag>::type Lease6StorageSubnetIdIndex;
index 1fad37a884349478c492f3722e886034644471ad..9197842fb64e2f85156b4d4597ff66e2a4c9e72a 100644 (file)
@@ -52,7 +52,7 @@ const int MLM_MYSQL_FETCH_FAILURE = 0;
 
 /// @name Current database schema version values.
 //@{
-const uint32_t MYSQL_SCHEMA_VERSION_MAJOR = 31;
+const uint32_t MYSQL_SCHEMA_VERSION_MAJOR = 32;
 const uint32_t MYSQL_SCHEMA_VERSION_MINOR = 0;
 
 //@}
index c6c8ab3928ad9cb2909cb41e4a785b7512739ae9..50984f7ace5a5f64981e636a70fcd158138773d7 100644 (file)
@@ -18,7 +18,7 @@ namespace isc {
 namespace db {
 
 /// @brief Define the PostgreSQL backend version.
-const uint32_t PGSQL_SCHEMA_VERSION_MAJOR = 30;
+const uint32_t PGSQL_SCHEMA_VERSION_MAJOR = 31;
 const uint32_t PGSQL_SCHEMA_VERSION_MINOR = 0;
 
 // Maximum number of parameters that can be used a statement
index c16b4c11be3385c7415d0b4cfbc145e878ea3e1c..1e1497ddba7d5dd4bb064aed812831896115578c 100644 (file)
@@ -6451,6 +6451,17 @@ UPDATE schema_version
 
 -- This line concludes the schema upgrade to version 31.0.
 
+-- This line starts the schema upgrade to version 32.0.
+
+# Create index for searching leases by hwaddr.
+CREATE INDEX lease6_by_hwaddr ON lease6 (hwaddr);
+
+-- Update the schema version number.
+UPDATE schema_version
+    SET version = '32', minor = '0';
+
+-- This line concludes the schema upgrade to version 32.0.
+
 # Notes:
 #
 # Indexes
index 647591aac69e33cdb633b83c0f963930a5ea66bd..61fce59c79e217669e287a1c624e306d02c64045 100644 (file)
@@ -72,6 +72,7 @@ upgrade_scripts = [
     'upgrade_028_to_029.sh',
     'upgrade_029_to_030.sh',
     'upgrade_030_to_031.sh',
+    'upgrade_031_to_032.sh',
 ]
 list = run_command(
     GRABBER,
diff --git a/src/share/database/scripts/mysql/upgrade_031_to_032.sh.in b/src/share/database/scripts/mysql/upgrade_031_to_032.sh.in
new file mode 100755 (executable)
index 0000000..5214696
--- /dev/null
@@ -0,0 +1,68 @@
+#!/bin/sh
+
+# Copyright (C) 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
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+# Exit with error if commands exit with non-zero and if undefined variables are
+# used.
+set -eu
+
+# shellcheck disable=SC2034
+# SC2034: ... appears unused. Verify use (or export if used externally).
+prefix="@prefix@"
+
+# Include utilities based on location of this script. Check for sources first,
+# so that the unexpected situations with weird paths fall on the default
+# case of installed.
+script_path=$(cd "$(dirname "${0}")" && pwd)
+if test "${script_path}" = "@abs_top_builddir@/src/share/database/scripts/mysql"; then
+    # shellcheck source=./src/bin/admin/admin-utils.sh.in
+    . "@abs_top_builddir@/src/bin/admin/admin-utils.sh"
+else
+    # shellcheck source=./src/bin/admin/admin-utils.sh.in
+    . "@datarootdir@/@PACKAGE_NAME@/scripts/admin-utils.sh"
+fi
+
+# Check only major version to allow for intermediary backported schema changes.
+version=$(mysql_version "${@}" | cut -d '.' -f 1)
+if test "${version}" != '31'; then
+    printf 'This script upgrades 31.* to 32.0. '
+    printf 'Reported version is %s. Skipping upgrade.\n' "${version}"
+    exit 0
+fi
+
+# Get the schema name from database argument. We need this to
+# query information_schema for the right database.
+for arg in "${@}"
+do
+    if ! printf '%s' "${arg}" | grep -Eq -- '^--'
+    then
+        schema="$arg"
+        break
+    fi
+done
+
+# Make sure we have the schema.
+if [ -z "$schema" ]
+then
+    printf "Could not find database schema name in cmd line args: %s\n" "${*}"
+    exit 255
+fi
+
+mysql "$@" <<EOF
+
+-- This line starts the schema upgrade to version 32.0.
+
+# Create index for searching leases by hwaddr.
+CREATE INDEX lease6_by_hwaddr ON lease6 (hwaddr);
+
+-- Update the schema version number.
+UPDATE schema_version
+    SET version = '32', minor = '0';
+
+-- This line concludes the schema upgrade to version 32.0.
+
+EOF
index 4fbbf1290a56eb59bc1f84d0e273b95559b22911..1a3a85c0fc30a84e644b881f3cd36b2ebbb6cfd5 100644 (file)
@@ -6728,6 +6728,17 @@ UPDATE schema_version
 
 -- This line concludes the schema upgrade to version 30.0.
 
+-- This line starts the schema upgrade to version 31.0.
+
+-- Create index for searching leases by hwaddr.
+CREATE INDEX lease6_by_hwaddr ON lease6 (hwaddr);
+
+-- Update the schema version number.
+UPDATE schema_version
+    SET version = '31', minor = '0';
+
+-- This line concludes the schema upgrade to version 31.0.
+
 -- Commit the script transaction.
 COMMIT;
 
index e71bebf151c2dafbeaadb850b77d8c60d4f9a5b4..c20a9da7820b569de89f6a509f5287c7e1a877aa 100644 (file)
@@ -66,6 +66,7 @@ upgrade_scripts = [
     'upgrade_027_to_028.sh',
     'upgrade_028_to_029.sh',
     'upgrade_029_to_030.sh',
+    'upgrade_030_to_031.sh',
 ]
 list = run_command(
     GRABBER,
diff --git a/src/share/database/scripts/pgsql/upgrade_030_to_031.sh.in b/src/share/database/scripts/pgsql/upgrade_030_to_031.sh.in
new file mode 100755 (executable)
index 0000000..0395c3f
--- /dev/null
@@ -0,0 +1,54 @@
+#!/bin/sh
+
+# Copyright (C) 2024-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
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+# Exit with error if commands exit with non-zero and if undefined variables are
+# used.
+set -eu
+
+# shellcheck disable=SC2034
+# SC2034: ... appears unused. Verify use (or export if used externally).
+prefix="@prefix@"
+
+# Include utilities based on location of this script. Check for sources first,
+# so that the unexpected situations with weird paths fall on the default
+# case of installed.
+script_path=$(cd "$(dirname "${0}")" && pwd)
+if test "${script_path}" = "@abs_top_builddir@/src/share/database/scripts/pgsql"; then
+    # shellcheck source=./src/bin/admin/admin-utils.sh.in
+    . "@abs_top_builddir@/src/bin/admin/admin-utils.sh"
+else
+    # shellcheck source=./src/bin/admin/admin-utils.sh.in
+    . "@datarootdir@/@PACKAGE_NAME@/scripts/admin-utils.sh"
+fi
+
+# Check only major version to allow for intermediary backported schema changes.
+version=$(pgsql_version "${@}" | cut -d '.' -f 1)
+if test "${version}" != '30'; then
+    printf 'This script upgrades 30.* to 31.0. '
+    printf 'Reported version is %s. Skipping upgrade.\n' "${version}"
+    exit 0
+fi
+
+psql "$@" >/dev/null <<EOF
+START TRANSACTION;
+
+-- This line starts the schema upgrade to version 31.0.
+
+-- Create index for searching leases by hwaddr.
+CREATE INDEX lease6_by_hwaddr ON lease6 (hwaddr);
+
+-- Update the schema version number.
+UPDATE schema_version
+    SET version = '31', minor = '0';
+
+-- This line concludes the schema upgrade to version 31.0.
+
+-- Commit the script transaction.
+COMMIT;
+
+EOF