]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1170] Updated leaseX-add commands
authorFrancis Dupont <fdupont@isc.org>
Tue, 28 Apr 2020 12:28:30 +0000 (14:28 +0200)
committerFrancis Dupont <fdupont@isc.org>
Thu, 7 May 2020 16:32:34 +0000 (18:32 +0200)
ChangeLog
src/hooks/dhcp/lease_cmds/lease_cmds.cc
src/hooks/dhcp/lease_cmds/tests/lease_cmds_unittest.cc

index 6c221c586b80db487d744e0a9b5442976c4fc67a..1376af960e6faaebc4092f5feb4dffe3fe3050df 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+1746.  [bug]           fdupont
+       lease4-add and lease6-add commands now return an error when the
+       lease already exists.
+       (Gitlab #1170)
+
 1745.  [bug]           fdupont
        Made state model and external sockets thread safe.
        (Gitlab #1095)
index eaf88bf90cf7be2ea852a0b64b59b3801e6a1562..b54761ed86f1e38b3e60b1bffdc74c8faf2cefe1 100644 (file)
@@ -10,6 +10,7 @@
 #include <cc/command_interpreter.h>
 #include <cc/data.h>
 #include <asiolink/io_address.h>
+#include <database/db_exceptions.h>
 #include <dhcpsrv/cfgmgr.h>
 #include <dhcpsrv/dhcpsrv_exceptions.h>
 #include <dhcpsrv/lease_mgr.h>
@@ -391,7 +392,9 @@ LeaseCmdsImpl::leaseAddHandler(CalloutHandle& handle) {
             lease4 = parser.parse(config, cmd_args_, force_create);
 
             if (lease4) {
-                LeaseMgrFactory::instance().addLease(lease4);
+                if (!LeaseMgrFactory::instance().addLease(lease4)) {
+                    isc_throw(db::DuplicateEntry, "IPv4 lease already exists.");
+                }
                 resp << "Lease for address " << lease4->addr_.toText()
                      << ", subnet-id " << lease4->subnet_id_ << " added.";
             }
@@ -401,7 +404,9 @@ LeaseCmdsImpl::leaseAddHandler(CalloutHandle& handle) {
             lease6 = parser.parse(config, cmd_args_, force_create);
 
             if (lease6) {
-                LeaseMgrFactory::instance().addLease(lease6);
+                if (!LeaseMgrFactory::instance().addLease(lease6)) {
+                    isc_throw(db::DuplicateEntry, "IPv6 lease already exists.");
+                }
                 if (lease6->type_ == Lease::TYPE_NA) {
                     resp << "Lease for address " << lease6->addr_.toText()
                          << ", subnet-id " << lease6->subnet_id_ << " added.";
@@ -1814,5 +1819,5 @@ LeaseCmds::LeaseCmds()
     :impl_(new LeaseCmdsImpl()) {
 }
 
-};
-};
+} // end of namespace lease_cmds
+} // end of namespace isc
index 5cb21b2eddb51b881a3b092051ab0a7f2bb4c3db..5e3178b1b50e3753aab0dba6e2f6658bee7c9220 100644 (file)
@@ -847,6 +847,29 @@ TEST_F(LeaseCmdsTest, Lease4Add) {
 
 }
 
+// Check that a lease4 is not added when it already exists.
+TEST_F(LeaseCmdsTest, Lease4AddExisting) {
+
+    // Initialize lease manager (false = v4, true = add leases)
+    initLeaseMgr(false, true);
+
+    // Check that the lease manager pointer is there.
+    ASSERT_TRUE(lmptr_);
+
+    // Now send the command.
+    string txt =
+        "{\n"
+        "    \"command\": \"lease4-add\",\n"
+        "    \"arguments\": {"
+        "        \"subnet-id\": 44,\n"
+        "        \"ip-address\": \"192.0.2.1\",\n"
+        "        \"hw-address\": \"1a:1b:1c:1d:1e:1f\"\n"
+        "    }\n"
+        "}";
+    string exp_rsp = "IPv4 lease already exists.";
+    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);
+}
+
 // Check that subnet-id is optional. If not specified, Kea should select
 // it on its own.
 TEST_F(LeaseCmdsTest, Lease4AddSubnetIdMissing) {
@@ -1291,6 +1314,30 @@ TEST_F(LeaseCmdsTest, Lease6Add) {
     EXPECT_FALSE(l->getContext());
 }
 
+// Check that a lease6 is not added when it already exists.
+TEST_F(LeaseCmdsTest, Lease6AddExisting) {
+
+    // Initialize lease manager (true = v6, true = add leases)
+    initLeaseMgr(true, true);
+
+    // Check that the lease manager pointer is there.
+    ASSERT_TRUE(lmptr_);
+
+    // Now send the command.
+    string txt =
+        "{\n"
+        "    \"command\": \"lease6-add\",\n"
+        "    \"arguments\": {"
+        "        \"subnet-id\": 66,\n"
+        "        \"ip-address\": \"2001:db8:1::1\",\n"
+        "        \"duid\": \"1a:1b:1c:1d:1e:1f\",\n"
+        "        \"iaid\": 1234\n"
+        "    }\n"
+        "}";
+    string exp_rsp = "IPv6 lease already exists.";
+    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);
+}
+
 // Check that subnet-id is optional. If not specified, Kea should select
 // it on its own.
 TEST_F(LeaseCmdsTest, Lease6AddSubnetIdMissing) {