From 901df1b523451cd664e4272210708035cdf758bf Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Mon, 2 Nov 2026 08:35:49 +0100 Subject: [PATCH 1/4] fix(dns): raise when the DNS service detection fails _detect_active_dns caught every error from /api/core/service/search and returned None, so get_dns_entries() reported a transient API error as an empty list (no DNS service) and sync_dns_zone() blamed the DNS service. Let the request error propagate; None only when the list was read and neither Unbound nor Dnsmasq runs. Closes #39 --- CHANGELOG.md | 5 +++++ napalm_opnsense/opnsense.py | 22 ++++++++++++---------- tests/unit/test_driver.py | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 609136b..f8bbcf5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,11 @@ All notable changes to this driver. The format follows public key (`wireguard-wg0-aBcD1234`, URL-safe) instead of its position, so a peer added ahead of another no longer renumbers it (#32). Every WireGuard key changes once with this release. +- `get_dns_entries()` raises when the service list (`/api/core/service/search`) + cannot be read, instead of returning an empty list as if neither Unbound nor + 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 + service (Unbound or Dnsmasq) detected" (#39). ## [0.5.0] - 2026-10-23 diff --git a/napalm_opnsense/opnsense.py b/napalm_opnsense/opnsense.py index f48ff7d..d6e4b5a 100644 --- a/napalm_opnsense/opnsense.py +++ b/napalm_opnsense/opnsense.py @@ -2794,15 +2794,16 @@ class OPNsenseDriver( # ------------------------------------------------------------------ def _detect_active_dns(self) -> str | None: - """Return 'unbound' or 'dnsmasq' depending on which service is running.""" - try: - data = self._get("/api/core/service/search") - for row in data.get("rows", []): - name = (row.get("name") or "").lower() - if name in ("unbound", "dnsmasq") and row.get("running"): - return name - except Exception as exc: - logger.warning("DNS service detection failed: %s", exc) + """Return 'unbound' or 'dnsmasq' depending on which service is running. + + None only when the service list was read and neither runs; a failed + read raises, so it cannot pass for "no DNS service". + """ + data = self._get("/api/core/service/search") + for row in data.get("rows", []): + name = (row.get("name") or "").lower() + if name in ("unbound", "dnsmasq") and row.get("running"): + return name return None def get_dns_entries(self) -> list[dict[str, Any]]: @@ -2810,7 +2811,8 @@ class OPNsenseDriver( Checks whether Unbound (DNS Resolver) or Dnsmasq (DNS Forwarder) is running and returns host overrides from whichever is active. If neither - is running, returns an empty list. A failed read of the overrides raises. + is running, returns an empty list. A failed read of the service list + or of the overrides raises. Each entry:: diff --git a/tests/unit/test_driver.py b/tests/unit/test_driver.py index abaa5e6..62d1eee 100644 --- a/tests/unit/test_driver.py +++ b/tests/unit/test_driver.py @@ -3005,6 +3005,43 @@ class TestHostOverrideReadFailure: driver.get_dns_entries() +class TestDnsServiceDetection: + """A failed service search raises: None would say neither Unbound nor Dnsmasq runs (#39).""" + + def test_get_dns_entries_raises_when_the_service_search_fails(self, driver): + driver.session.get.side_effect = requests.exceptions.ConnectionError("reset by peer") + + with pytest.raises(requests.exceptions.ConnectionError): + driver.get_dns_entries() + + driver.session.post.assert_not_called() + + def test_sync_dns_zone_reports_the_api_error_and_writes_nothing(self, driver): + driver.session.get.side_effect = requests.exceptions.HTTPError("502 Bad Gateway") + + with pytest.raises(requests.exceptions.HTTPError, match="502"): + driver.sync_dns_zone( + "office.local", + [{"hostname": "nas", "ip": "172.22.40.8", "record_type": "A", "enabled": True}], + ) + + driver.session.post.assert_not_called() + + def test_get_dns_entries_is_empty_when_neither_service_runs(self, driver): + driver.session.get.return_value = _make_json_response( + { + "rows": [ + {"name": "unbound", "running": 0}, + {"name": "dnsmasq", "running": 0}, + {"name": "sshd", "running": 1}, + ] + } + ) + + assert driver.get_dns_entries() == [] + driver.session.post.assert_not_called() + + # --------------------------------------------------------------------------- # uninstall_package – says what it cannot do # --------------------------------------------------------------------------- -- 2.54.1 From 0f730a4b9d84ce74b79cf6cc7a8de8644c933fc9 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Mon, 2 Nov 2026 08:37:06 +0100 Subject: [PATCH 2/4] 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 --- CHANGELOG.md | 5 +++++ napalm_opnsense/opnsense.py | 13 +++++++------ tests/unit/test_driver.py | 27 +++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f8bbcf5..6e0655a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 records. `sync_dns_zone()` raises that API error instead of "No active DNS 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 diff --git a/napalm_opnsense/opnsense.py b/napalm_opnsense/opnsense.py index d6e4b5a..b421d1b 100644 --- a/napalm_opnsense/opnsense.py +++ b/napalm_opnsense/opnsense.py @@ -1453,7 +1453,8 @@ class OPNsenseDriver( :param hostname: optional hostname to record on the reservation. :raises ValueError: if no Kea subnet contains ``ip``. :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). """ try: @@ -1514,12 +1515,12 @@ class OPNsenseDriver( 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 holds *ip* (#26).""" + MAC holds *ip* (#26) or the search fails (#40).""" try: - own, other = split_by_mac(self._kea_reservations_at(ip), mac) - except Exception as exc: - logger.debug("Kea reservation search failed, proceeding to add: %s", exc) - return None + rows = self._kea_reservations_at(ip) + except RequestException as exc: + raise RuntimeError(f"Kea searchReservation for {ip} failed: {exc}") from exc + own, other = split_by_mac(rows, mac) if own is None and other: raise RuntimeError(f"Kea already reserves {ip} for {other}, not {normalize_mac(mac)}") return own diff --git a/tests/unit/test_driver.py b/tests/unit/test_driver.py index 62d1eee..9ce694b 100644 --- a/tests/unit/test_driver.py +++ b/tests/unit/test_driver.py @@ -2030,6 +2030,33 @@ class TestCreateDhcpReservation: 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() -- 2.54.1 From 96a4a139d077ac9071c312e9c363b85718bc5860 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Mon, 2 Nov 2026 08:37:58 +0100 Subject: [PATCH 3/4] fix(tests): keep the open() failure test off the network test_open_raises_connection_exception_on_error called open() on "unreachable.invalid" unpatched, so the unit test resolved the name through the host's resolver and depended on it failing fast. Patch requests.Session.get to refuse the connection and check that open() raises ConnectionException and leaves no session behind. Closes #41 --- CHANGELOG.md | 3 +++ tests/unit/test_driver.py | 10 ++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e0655a..1f1d8d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,9 @@ All notable changes to this driver. The format follows 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). +- The unit tests no longer reach the network: the test of a failed `open()` looked + up a real host name and waited for the resolver, so it hung or behaved differently + on a build host with a slow resolver or one that answers every name (#41). ## [0.5.0] - 2026-10-23 diff --git a/tests/unit/test_driver.py b/tests/unit/test_driver.py index 9ce694b..c539b2d 100644 --- a/tests/unit/test_driver.py +++ b/tests/unit/test_driver.py @@ -132,8 +132,14 @@ class TestOpenClose: password="s", optional_args={"verify": False}, ) - with pytest.raises(ConnectionException): - drv.open() + refused = requests.exceptions.ConnectionError("Connection refused") + with patch.object(requests.Session, "get", side_effect=refused) as get: + with pytest.raises(ConnectionException, match="Connection refused"): + drv.open() + + get.assert_called_once() + assert get.call_args.args[0] == "https://unreachable.invalid/api/core/system/status" + assert drv.session is None def test_close_clears_session(self, driver): driver.close() -- 2.54.1 From 6ac7a6f6a24bb9d0b08c09859f9404db1648690b Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Mon, 2 Nov 2026 08:51:21 +0100 Subject: [PATCH 4/4] refactor(dhcp): move the Kea subnet lookup into kea_reservations create_dhcp_reservation and _kea_subnet_uuid_for each walked the searchSubnet rows for the one holding an address. Both now call the pure subnet_containing(), which brings create_dhcp_reservation under the complexity limit and back below its recorded length after #40; the quality baseline records the shrunk entries. --- napalm_opnsense/kea_reservations.py | 16 +++++++++++++ napalm_opnsense/opnsense.py | 35 +++++++++++------------------ tests/quality_baseline.json | 5 ++--- tests/unit/test_kea_reservations.py | 25 ++++++++++++++++++++- 4 files changed, 55 insertions(+), 26 deletions(-) diff --git a/napalm_opnsense/kea_reservations.py b/napalm_opnsense/kea_reservations.py index dc064a4..5ed27ea 100644 --- a/napalm_opnsense/kea_reservations.py +++ b/napalm_opnsense/kea_reservations.py @@ -7,6 +7,7 @@ edited by hand (#26), so every lookup here checks both. from __future__ import annotations +from ipaddress import ip_address, ip_network from typing import Any from napalm_device_types.dhcp import normalize_mac @@ -19,6 +20,21 @@ def _mac_of(row: dict[str, Any], *fields: str) -> str: return normalize_mac(next((str(row[f]) for f in fields if row.get(f)), "")) +def subnet_containing(rows: list[dict[str, Any]], ip: str) -> dict[str, Any] | None: + """The first ``searchSubnet`` row whose subnet holds *ip*; None when none does. + + A row without a readable ``subnet`` is skipped. + """ + address = ip_address(ip) + for row in rows: + try: + if address in ip_network(str(row["subnet"]).strip(), strict=False): + return row + except (ValueError, KeyError): + continue + return None + + def split_by_mac(rows: list[dict[str, Any]], mac: str) -> tuple[dict[str, Any] | None, str]: """``(the row *mac* holds, the MAC of a row another host holds)`` among *rows*. diff --git a/napalm_opnsense/opnsense.py b/napalm_opnsense/opnsense.py index b421d1b..29977c3 100644 --- a/napalm_opnsense/opnsense.py +++ b/napalm_opnsense/opnsense.py @@ -41,7 +41,6 @@ import logging import re import socket import time -from ipaddress import ip_address, ip_network from typing import Any import requests @@ -64,7 +63,12 @@ from requests.exceptions import RequestException from napalm_opnsense.api_endpoint import ALIVE_TIMEOUT, api_endpoint from napalm_opnsense.ha import config_sync, dhcp_ha -from napalm_opnsense.kea_reservations import lease_held, reservation_unchanged, split_by_mac +from napalm_opnsense.kea_reservations import ( + lease_held, + reservation_unchanged, + split_by_mac, + subnet_containing, +) from napalm_opnsense.listening import parse_socket_statistics, wireguard_listen_ports from napalm_opnsense.ospf_mixin import OPNsenseOspfMixin from napalm_opnsense.ping_mixin import OPNsensePingMixin @@ -1462,19 +1466,10 @@ class OPNsenseDriver( except Exception as exc: raise RuntimeError(f"Kea DHCPv4 plugin unavailable: {exc}") from exc - ip_obj = ip_address(ip) - subnet_uuid = None - subnet_cidr = "" - for row in subnets: - try: - if ip_obj in ip_network(row["subnet"], strict=False): - subnet_uuid = row["uuid"] - subnet_cidr = str(row["subnet"]).strip() - break - except ValueError: - continue - if subnet_uuid is None: + subnet_row = subnet_containing(subnets, ip) + if subnet_row is None: raise ValueError(f"No Kea-managed subnet contains {ip}") + subnet_uuid, subnet_cidr = subnet_row["uuid"], str(subnet_row["subnet"]).strip() existing_row = self._kea_own_reservation(ip, mac) @@ -1632,14 +1627,10 @@ class OPNsenseDriver( return str(row["uuid"]) raise ValueError(f"No Kea-managed subnet matches {subnet}") - ip_obj = ip_address(ip) - for row in rows: - try: - if ip_obj in ip_network(row["subnet"], strict=False): - return str(row["uuid"]) - except (ValueError, KeyError): - continue - raise ValueError(f"No Kea-managed subnet contains {ip}") + row = subnet_containing(rows, ip) + if row is None: + raise ValueError(f"No Kea-managed subnet contains {ip}") + return str(row["uuid"]) def get_dhcp_reservations(self) -> list[dict[str, Any]]: """Return all Kea DHCPv4 static reservations, vendor-neutral. diff --git a/tests/quality_baseline.json b/tests/quality_baseline.json index a0c1215..4edd572 100644 --- a/tests/quality_baseline.json +++ b/tests/quality_baseline.json @@ -10,7 +10,6 @@ "napalm_opnsense/opnsense.py::OPNsenseDriver._kea_read_list": 11, "napalm_opnsense/opnsense.py::OPNsenseDriver.apply_dhcp_subnet": 21, "napalm_opnsense/opnsense.py::OPNsenseDriver.apply_firewall_rule": 11, - "napalm_opnsense/opnsense.py::OPNsenseDriver.create_dhcp_reservation": 12, "napalm_opnsense/opnsense.py::OPNsenseDriver.delete_dhcp_reservation_and_lease": 11, "napalm_opnsense/opnsense.py::OPNsenseDriver.get_bgp_neighbors": 16, "napalm_opnsense/opnsense.py::OPNsenseDriver.get_dhcp_leases": 15, @@ -33,7 +32,7 @@ "function_lines": { "napalm_opnsense/opnsense.py::OPNsenseDriver._regenerate_web_cert": 51, "napalm_opnsense/opnsense.py::OPNsenseDriver.apply_dhcp_subnet": 60, - "napalm_opnsense/opnsense.py::OPNsenseDriver.create_dhcp_reservation": 72, + "napalm_opnsense/opnsense.py::OPNsenseDriver.create_dhcp_reservation": 64, "napalm_opnsense/opnsense.py::OPNsenseDriver.delete_dhcp_reservation_and_lease": 79, "napalm_opnsense/opnsense.py::OPNsenseDriver.get_bgp_neighbors": 86, "napalm_opnsense/opnsense.py::OPNsenseDriver.get_dhcp_leases": 106, @@ -56,7 +55,7 @@ "napalm_opnsense.resolver_mixin" ], "python_file_lines": { - "napalm_opnsense/opnsense.py": 3064 + "napalm_opnsense/opnsense.py": 3058 }, "untyped_public": [ "napalm_opnsense/opnsense.py::OPNsenseDriver.get_snmp_config" diff --git a/tests/unit/test_kea_reservations.py b/tests/unit/test_kea_reservations.py index ffbc24e..fd22d97 100644 --- a/tests/unit/test_kea_reservations.py +++ b/tests/unit/test_kea_reservations.py @@ -1,6 +1,11 @@ """Matching Kea reservations and leases on address and MAC (#26).""" -from napalm_opnsense.kea_reservations import lease_held, reservation_unchanged, split_by_mac +from napalm_opnsense.kea_reservations import ( + lease_held, + reservation_unchanged, + split_by_mac, + subnet_containing, +) def _row(mac, uuid="u-1"): @@ -65,3 +70,21 @@ class TestReservationUnchanged: def test_the_same_reservation_is_unchanged(self): row = dict(self.WANTED, hw_address="02-AA-BB-CC-DD-EE", subnet="172.22.8.0/24") assert reservation_unchanged(row, self.WANTED, "172.22.8.0/24") is True + + +class TestSubnetContaining: + ROWS = [ + {"uuid": "s-broken", "subnet": "not-a-cidr"}, + {"uuid": "s-nocidr"}, + {"uuid": "s-8", "subnet": " 172.22.8.0/24 "}, + {"uuid": "s-wide", "subnet": "172.22.0.0/16"}, + ] + + def test_first_subnet_holding_the_address_wins(self): + assert subnet_containing(self.ROWS, "172.22.8.253") == self.ROWS[2] + + def test_rows_without_a_readable_subnet_are_skipped(self): + assert subnet_containing(self.ROWS, "172.22.9.1") == self.ROWS[3] + + def test_none_when_no_subnet_holds_the_address(self): + assert subnet_containing(self.ROWS, "10.99.99.99") is None -- 2.54.1