fix(dhcp): match Kea reservations on address and MAC
driver / standard (pull_request) Successful in 15s
driver / lint (pull_request) Successful in 12s
driver / types (pull_request) Successful in 56s
driver / test (3.12) (pull_request) Successful in 1m16s
driver / test (3.13) (pull_request) Successful in 1m4s
driver / lowest (pull_request) Successful in 29s
driver / build (pull_request) Successful in 30s
driver / security (pull_request) Successful in 1m2s
CI / driver (pull_request) Successful in 2m45s
driver / publish (pull_request) Skipped
driver / standard (pull_request) Successful in 15s
driver / lint (pull_request) Successful in 12s
driver / types (pull_request) Successful in 56s
driver / test (3.12) (pull_request) Successful in 1m16s
driver / test (3.13) (pull_request) Successful in 1m4s
driver / lowest (pull_request) Successful in 29s
driver / build (pull_request) Successful in 30s
driver / security (pull_request) Successful in 1m2s
CI / driver (pull_request) Successful in 2m45s
driver / publish (pull_request) Skipped
create_dhcp_reservation and delete_dhcp_reservation_and_lease looked a reservation up by its address alone. A reservation another MAC held for the address, made by hand or left from an earlier host, was overwritten on create and removed on delete, together with that host's lease. Create now raises instead of taking it over, and delete leaves another MAC's reservation and lease in place and names that MAC in the new reservation_other_mac field. The matching lives in kea_reservations, a module under strict typing. Closes #26
This commit is contained in:
1 parent
4fc7b2424e
commit
ea0a52717e
6 files changed
+260
-64
No files matched your search
+89
-11
@@ -1846,6 +1846,18 @@ KEA_SUBNETS_RESPONSE = {
|
||||
}
|
||||
|
||||
|
||||
def _reservation_row(mac, ip="172.22.8.253", uuid="existing-uuid-456"):
|
||||
"""A searchReservation row, in the shape the os-kea plugin returns it."""
|
||||
return {
|
||||
"uuid": uuid,
|
||||
"subnet": "172.22.8.0/24",
|
||||
"ip_address": ip,
|
||||
"hw_address": mac,
|
||||
"hostname": "",
|
||||
"description": "[netork]",
|
||||
}
|
||||
|
||||
|
||||
class TestCreateDhcpReservation:
|
||||
def test_adds_new_reservation_when_none_exists(self, driver):
|
||||
driver.session.get.return_value = _make_json_response(KEA_SUBNETS_RESPONSE)
|
||||
@@ -1876,14 +1888,14 @@ class TestCreateDhcpReservation:
|
||||
def test_updates_existing_reservation_for_same_ip(self, driver):
|
||||
driver.session.get.return_value = _make_json_response(KEA_SUBNETS_RESPONSE)
|
||||
driver.session.post.side_effect = [
|
||||
_make_json_response(
|
||||
{"rows": [{"uuid": "existing-uuid-456", "ip_address": "172.22.8.253"}]}
|
||||
),
|
||||
_make_json_response({"rows": [_reservation_row("02-AA-BB-CC-DD-EE")]}),
|
||||
_make_json_response({"result": "saved", "uuid": "existing-uuid-456"}),
|
||||
_make_json_response({"status": "ok"}),
|
||||
]
|
||||
|
||||
driver.create_dhcp_reservation(mac="02:aa:bb:cc:dd:ee", ip="172.22.8.253")
|
||||
driver.create_dhcp_reservation(
|
||||
mac="02:aa:bb:cc:dd:ee", ip="172.22.8.253", hostname="new-vm"
|
||||
)
|
||||
|
||||
set_call = driver.session.post.call_args_list[1]
|
||||
assert (
|
||||
@@ -1932,7 +1944,6 @@ class TestCreateDhcpReservation:
|
||||
@pytest.mark.parametrize(
|
||||
"field, live_value",
|
||||
[
|
||||
("hw_address", "02:aa:bb:cc:dd:ff"),
|
||||
("hostname", "old-vm"),
|
||||
("description", "made by hand"),
|
||||
("subnet", "172.22.0.0/24"),
|
||||
@@ -1968,6 +1979,23 @@ class TestCreateDhcpReservation:
|
||||
"https://opnsense.example.com/api/kea/service/reconfigure",
|
||||
]
|
||||
|
||||
def test_does_not_take_over_another_macs_reservation(self, driver):
|
||||
"""#26: a reservation for the IP held by another MAC (made by hand, say)
|
||||
was overwritten with the new MAC and the netOrk tag. It now raises and
|
||||
leaves the firewall as it is; the caller decides."""
|
||||
driver.session.get.return_value = _make_json_response(KEA_SUBNETS_RESPONSE)
|
||||
driver.session.post.side_effect = [
|
||||
_make_json_response({"rows": [_reservation_row("02:11:22:33:44:55")]}),
|
||||
]
|
||||
|
||||
with pytest.raises(RuntimeError, match="02:11:22:33:44:55"):
|
||||
driver.create_dhcp_reservation(
|
||||
mac="02:aa:bb:cc:dd:ee", ip="172.22.8.253", hostname="new-vm"
|
||||
)
|
||||
|
||||
# searchReservation only -- no setReservation, no service/reconfigure.
|
||||
assert driver.session.post.call_count == 1
|
||||
|
||||
def test_raises_when_ip_not_in_any_kea_subnet(self, driver):
|
||||
driver.session.get.return_value = _make_json_response(KEA_SUBNETS_RESPONSE)
|
||||
|
||||
@@ -2012,7 +2040,7 @@ class TestDeleteDhcpReservationAndLease:
|
||||
def test_deletes_existing_reservation_and_lease(self, driver):
|
||||
driver.session.post.side_effect = [
|
||||
_make_json_response(
|
||||
{"rows": [{"uuid": "existing-uuid-456", "ip_address": "172.22.8.253"}]}
|
||||
{"rows": [_reservation_row("02:aa:bb:cc:dd:ee")]}
|
||||
), # searchReservation — match
|
||||
_make_json_response({"result": "deleted"}), # delReservation
|
||||
_make_json_response({"status": "ok"}), # service/reconfigure
|
||||
@@ -2031,6 +2059,7 @@ class TestDeleteDhcpReservationAndLease:
|
||||
"reservation_deleted": True,
|
||||
"lease_found": True,
|
||||
"lease_deleted": True,
|
||||
"reservation_other_mac": "",
|
||||
}
|
||||
del_res_call = driver.session.post.call_args_list[1]
|
||||
assert (
|
||||
@@ -2064,15 +2093,66 @@ class TestDeleteDhcpReservationAndLease:
|
||||
"reservation_deleted": False,
|
||||
"lease_found": False,
|
||||
"lease_deleted": False,
|
||||
"reservation_other_mac": "",
|
||||
}
|
||||
# Only searchReservation was called — no delReservation/reconfigure/delLease.
|
||||
assert driver.session.post.call_count == 1
|
||||
|
||||
def test_reservation_delete_raises_when_kea_rejects(self, driver):
|
||||
def test_leaves_another_macs_reservation_and_lease_alone(self, driver):
|
||||
"""#26: the IP was matched alone, so after an address was reused or
|
||||
edited by hand the call removed another host's reservation and lease."""
|
||||
driver.session.post.return_value = _make_json_response(
|
||||
{"rows": [_reservation_row("02:11:22:33:44:55")]}
|
||||
)
|
||||
driver.session.get.return_value = _make_json_response(
|
||||
{"rows": [{"address": "172.22.8.253", "hwaddr": "02:11:22:33:44:55"}]}
|
||||
)
|
||||
|
||||
result = driver.delete_dhcp_reservation_and_lease(
|
||||
mac="02:aa:bb:cc:dd:ee", ip="172.22.8.253"
|
||||
)
|
||||
|
||||
assert result == {
|
||||
"reservation_found": False,
|
||||
"reservation_deleted": False,
|
||||
"lease_found": False,
|
||||
"lease_deleted": False,
|
||||
"reservation_other_mac": "02:11:22:33:44:55",
|
||||
}
|
||||
# searchReservation only -- no delReservation, reconfigure or del_lease.
|
||||
assert [c.args[0] for c in driver.session.post.call_args_list] == [
|
||||
"https://opnsense.example.com/api/kea/dhcpv4/searchReservation"
|
||||
]
|
||||
|
||||
def test_deletes_its_own_reservation_next_to_another_macs(self, driver):
|
||||
"""The search is a phrase match, so it can list another MAC's row too."""
|
||||
driver.session.post.side_effect = [
|
||||
_make_json_response(
|
||||
{"rows": [{"uuid": "existing-uuid-456", "ip_address": "172.22.8.253"}]}
|
||||
{
|
||||
"rows": [
|
||||
_reservation_row("02:11:22:33:44:55", uuid="foreign-uuid"),
|
||||
_reservation_row("02:aa:bb:cc:dd:ee", uuid="own-uuid"),
|
||||
]
|
||||
}
|
||||
),
|
||||
_make_json_response({"result": "deleted"}),
|
||||
_make_json_response({"status": "ok"}),
|
||||
]
|
||||
driver.session.get.return_value = _make_json_response({"rows": []})
|
||||
|
||||
result = driver.delete_dhcp_reservation_and_lease(
|
||||
mac="02:aa:bb:cc:dd:ee", ip="172.22.8.253"
|
||||
)
|
||||
|
||||
assert result["reservation_deleted"] is True
|
||||
assert result["reservation_other_mac"] == "02:11:22:33:44:55"
|
||||
assert driver.session.post.call_args_list[1].args[0] == (
|
||||
"https://opnsense.example.com/api/kea/dhcpv4/delReservation/own-uuid"
|
||||
)
|
||||
|
||||
def test_reservation_delete_raises_when_kea_rejects(self, driver):
|
||||
driver.session.post.side_effect = [
|
||||
_make_json_response({"rows": [_reservation_row("02:AA:BB:CC:DD:EE")]}),
|
||||
_make_json_response({"result": "not found"}), # delReservation rejected
|
||||
]
|
||||
|
||||
@@ -2084,9 +2164,7 @@ class TestDeleteDhcpReservationAndLease:
|
||||
|
||||
def test_lease_delete_failure_is_non_fatal(self, driver):
|
||||
driver.session.post.side_effect = [
|
||||
_make_json_response(
|
||||
{"rows": [{"uuid": "existing-uuid-456", "ip_address": "172.22.8.253"}]}
|
||||
),
|
||||
_make_json_response({"rows": [_reservation_row("02:AA:BB:CC:DD:EE")]}),
|
||||
_make_json_response({"result": "deleted"}),
|
||||
_make_json_response({"status": "ok"}),
|
||||
]
|
||||
|
||||
Reference in new issue
Block a user