fix(dhcp): raise when the Kea reservation search fails
create_dhcp_reservation searched for an existing reservation so a retry updates it, but a failed search was logged at debug level and the add went ahead: a write based on a read that did not happen. Raise a RuntimeError naming searchReservation before anything is written. Closes #40
This commit is contained in:
1 parent
901df1b523
commit
0f730a4b9d
3 files changed
+39
-6
No files matched your search
@@ -43,6 +43,11 @@ All notable changes to this driver. The format follows
|
|||||||
Dnsmasq ran: a transient API error made the firewall look as if it had no DNS
|
Dnsmasq ran: a transient API error made the firewall look as if it had no DNS
|
||||||
records. `sync_dns_zone()` raises that API error instead of "No active DNS
|
records. `sync_dns_zone()` raises that API error instead of "No active DNS
|
||||||
service (Unbound or Dnsmasq) detected" (#39).
|
service (Unbound or Dnsmasq) detected" (#39).
|
||||||
|
- `create_dhcp_reservation()` raises `RuntimeError` naming `searchReservation` when
|
||||||
|
the search for an existing reservation fails, and writes nothing. It used to add
|
||||||
|
the reservation anyway: a retry then failed with "Kea rejected DHCP reservation"
|
||||||
|
instead of the real error, or left two reservations for one address, which Kea
|
||||||
|
refuses to load (#40).
|
||||||
|
|
||||||
## [0.5.0] - 2026-10-23
|
## [0.5.0] - 2026-10-23
|
||||||
|
|
||||||
|
|||||||
@@ -1453,7 +1453,8 @@ class OPNsenseDriver(
|
|||||||
:param hostname: optional hostname to record on the reservation.
|
:param hostname: optional hostname to record on the reservation.
|
||||||
:raises ValueError: if no Kea subnet contains ``ip``.
|
:raises ValueError: if no Kea subnet contains ``ip``.
|
||||||
:raises RuntimeError: if Kea rejects the reservation (validation
|
:raises RuntimeError: if Kea rejects the reservation (validation
|
||||||
errors), the Kea plugin isn't installed/enabled, or ``ip`` is
|
errors), the Kea plugin isn't installed/enabled, the search for an
|
||||||
|
existing reservation fails (nothing is written then), or ``ip`` is
|
||||||
already reserved for another MAC (left untouched; the caller decides).
|
already reserved for another MAC (left untouched; the caller decides).
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
@@ -1514,12 +1515,12 @@ class OPNsenseDriver(
|
|||||||
|
|
||||||
def _kea_own_reservation(self, ip: str, mac: str) -> dict[str, Any] | None:
|
def _kea_own_reservation(self, ip: str, mac: str) -> dict[str, Any] | None:
|
||||||
"""*mac*'s reservation for *ip*, updated in place on a retry; raises if another
|
"""*mac*'s reservation for *ip*, updated in place on a retry; raises if another
|
||||||
MAC holds *ip* (#26)."""
|
MAC holds *ip* (#26) or the search fails (#40)."""
|
||||||
try:
|
try:
|
||||||
own, other = split_by_mac(self._kea_reservations_at(ip), mac)
|
rows = self._kea_reservations_at(ip)
|
||||||
except Exception as exc:
|
except RequestException as exc:
|
||||||
logger.debug("Kea reservation search failed, proceeding to add: %s", exc)
|
raise RuntimeError(f"Kea searchReservation for {ip} failed: {exc}") from exc
|
||||||
return None
|
own, other = split_by_mac(rows, mac)
|
||||||
if own is None and other:
|
if own is None and other:
|
||||||
raise RuntimeError(f"Kea already reserves {ip} for {other}, not {normalize_mac(mac)}")
|
raise RuntimeError(f"Kea already reserves {ip} for {other}, not {normalize_mac(mac)}")
|
||||||
return own
|
return own
|
||||||
|
|||||||
@@ -2030,6 +2030,33 @@ class TestCreateDhcpReservation:
|
|||||||
|
|
||||||
driver.session.post.assert_not_called()
|
driver.session.post.assert_not_called()
|
||||||
|
|
||||||
|
# The search is what makes a retry update its reservation instead of adding a
|
||||||
|
# second one for the address: a write must not follow a search that failed (#40).
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"error",
|
||||||
|
[
|
||||||
|
requests.exceptions.HTTPError("500 Server Error"),
|
||||||
|
requests.exceptions.ConnectionError("reset by peer"),
|
||||||
|
],
|
||||||
|
ids=["http-error", "connection-error"],
|
||||||
|
)
|
||||||
|
def test_failed_reservation_search_raises_before_any_write(self, driver, error):
|
||||||
|
driver.session.get.return_value = _make_json_response(KEA_SUBNETS_RESPONSE)
|
||||||
|
driver.session.post.side_effect = [
|
||||||
|
error, # searchReservation
|
||||||
|
_make_json_response({"result": "saved", "uuid": "new-uuid-123"}),
|
||||||
|
_make_json_response({"status": "ok"}),
|
||||||
|
]
|
||||||
|
|
||||||
|
with pytest.raises(RuntimeError, match="searchReservation") as raised:
|
||||||
|
driver.create_dhcp_reservation(mac="02:aa:bb:cc:dd:ee", ip="172.22.8.253")
|
||||||
|
|
||||||
|
assert raised.value.__cause__ is error
|
||||||
|
assert [c.args[0] for c in driver.session.post.call_args_list] == [
|
||||||
|
"https://opnsense.example.com/api/kea/dhcpv4/searchReservation"
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# delete_dhcp_reservation_and_lease()
|
# delete_dhcp_reservation_and_lease()
|
||||||
|
|||||||
Reference in new issue
Block a user