fix(dns): raise when the host overrides cannot be read
The Unbound and Dnsmasq override reads logged any error and returned an empty list. sync_dns_zone took that for "no overrides": it deleted nothing and added every record again, so a transient API error left each record twice on the firewall. The reads now raise, which stops the sync before it writes and lets get_dns_entries callers tell a failed read from an empty one. Closes #14
This commit is contained in:
1 parent
4e377923db
commit
4fc7b2424e
4 files changed
+44
-10
No files matched your search
@@ -16,6 +16,9 @@ All notable changes to this driver. The format follows
|
|||||||
- `is_alive()` checks the host and port named in `base_url` (`https://fw:8443`) instead
|
- `is_alive()` checks the host and port named in `base_url` (`https://fw:8443`) instead
|
||||||
of always port 443 on the hostname, so a firewall whose API listens elsewhere no
|
of always port 443 on the hostname, so a firewall whose API listens elsewhere no
|
||||||
longer reads as dead (#15).
|
longer reads as dead (#15).
|
||||||
|
- `sync_dns_zone()` and `get_dns_entries()` raise when the host overrides cannot be
|
||||||
|
read. A transient API error used to read as "no overrides": the sync then deleted
|
||||||
|
nothing and added every record a second time (#14).
|
||||||
- `configure_snmp_agent()` works on OPNsense 26.x again. The configure step sent
|
- `configure_snmp_agent()` works on OPNsense 26.x again. The configure step sent
|
||||||
fields the os-net-snmp model does not have (`contact`, `location`, `sysobjid`,
|
fields the os-net-snmp model does not have (`contact`, `location`, `sysobjid`,
|
||||||
`bindip`) and `listen` in the shape `get` returns rather than as a string, which
|
`bindip`) and `listen` in the shape `get` returns rather than as a string, which
|
||||||
|
|||||||
@@ -2823,7 +2823,7 @@ class OPNsenseDriver(
|
|||||||
|
|
||||||
Checks whether Unbound (DNS Resolver) or Dnsmasq (DNS Forwarder) is
|
Checks whether Unbound (DNS Resolver) or Dnsmasq (DNS Forwarder) is
|
||||||
running and returns host overrides from whichever is active. If neither
|
running and returns host overrides from whichever is active. If neither
|
||||||
is running, returns an empty list.
|
is running, returns an empty list. A failed read of the overrides raises.
|
||||||
|
|
||||||
Each entry::
|
Each entry::
|
||||||
|
|
||||||
@@ -2849,7 +2849,6 @@ class OPNsenseDriver(
|
|||||||
def _get_unbound_host_overrides(self) -> list[dict[str, Any]]:
|
def _get_unbound_host_overrides(self) -> list[dict[str, Any]]:
|
||||||
result: list[dict[str, Any]] = []
|
result: list[dict[str, Any]] = []
|
||||||
seen: set[tuple[str, str, str, str]] = set()
|
seen: set[tuple[str, str, str, str]] = set()
|
||||||
try:
|
|
||||||
data = self._post(
|
data = self._post(
|
||||||
"/api/unbound/settings/searchhostoverride",
|
"/api/unbound/settings/searchhostoverride",
|
||||||
{"current": 1, "rowCount": -1, "searchPhrase": ""},
|
{"current": 1, "rowCount": -1, "searchPhrase": ""},
|
||||||
@@ -2894,13 +2893,10 @@ class OPNsenseDriver(
|
|||||||
"service": "unbound",
|
"service": "unbound",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
except Exception as exc:
|
|
||||||
logger.warning("Failed to fetch Unbound host overrides: %s", exc)
|
|
||||||
return sorted(result, key=lambda e: e["fqdn"].lower())
|
return sorted(result, key=lambda e: e["fqdn"].lower())
|
||||||
|
|
||||||
def _get_dnsmasq_host_overrides(self) -> list[dict[str, Any]]:
|
def _get_dnsmasq_host_overrides(self) -> list[dict[str, Any]]:
|
||||||
result: list[dict[str, Any]] = []
|
result: list[dict[str, Any]] = []
|
||||||
try:
|
|
||||||
data = self._post(
|
data = self._post(
|
||||||
"/api/dnsmasq/settings/searchhostoverride",
|
"/api/dnsmasq/settings/searchhostoverride",
|
||||||
{"current": 1, "rowCount": -1, "searchPhrase": ""},
|
{"current": 1, "rowCount": -1, "searchPhrase": ""},
|
||||||
@@ -2922,8 +2918,6 @@ class OPNsenseDriver(
|
|||||||
"service": "dnsmasq",
|
"service": "dnsmasq",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
except Exception as exc:
|
|
||||||
logger.warning("Failed to fetch Dnsmasq host overrides: %s", exc)
|
|
||||||
return sorted(result, key=lambda e: e["fqdn"].lower())
|
return sorted(result, key=lambda e: e["fqdn"].lower())
|
||||||
|
|
||||||
def set_dns_entry(self, uuid: str, service: str, data: dict[str, Any]) -> dict[str, Any]:
|
def set_dns_entry(self, uuid: str, service: str, data: dict[str, Any]) -> dict[str, Any]:
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
"napalm_opnsense/listening.py::_service_of": 12,
|
"napalm_opnsense/listening.py::_service_of": 12,
|
||||||
"napalm_opnsense/listening.py::_socket": 19,
|
"napalm_opnsense/listening.py::_socket": 19,
|
||||||
"napalm_opnsense/opnsense.py::OPNsenseDriver._allow_snmp_sources": 11,
|
"napalm_opnsense/opnsense.py::OPNsenseDriver._allow_snmp_sources": 11,
|
||||||
"napalm_opnsense/opnsense.py::OPNsenseDriver._get_unbound_host_overrides": 17,
|
"napalm_opnsense/opnsense.py::OPNsenseDriver._get_unbound_host_overrides": 16,
|
||||||
"napalm_opnsense/opnsense.py::OPNsenseDriver._kea_read_list": 11,
|
"napalm_opnsense/opnsense.py::OPNsenseDriver._kea_read_list": 11,
|
||||||
"napalm_opnsense/opnsense.py::OPNsenseDriver.apply_dhcp_subnet": 21,
|
"napalm_opnsense/opnsense.py::OPNsenseDriver.apply_dhcp_subnet": 21,
|
||||||
"napalm_opnsense/opnsense.py::OPNsenseDriver.apply_firewall_rule": 11,
|
"napalm_opnsense/opnsense.py::OPNsenseDriver.apply_firewall_rule": 11,
|
||||||
@@ -31,7 +31,6 @@
|
|||||||
"napalm_opnsense/virtual_ips.py::carp_addresses": 14
|
"napalm_opnsense/virtual_ips.py::carp_addresses": 14
|
||||||
},
|
},
|
||||||
"function_lines": {
|
"function_lines": {
|
||||||
"napalm_opnsense/opnsense.py::OPNsenseDriver._get_unbound_host_overrides": 51,
|
|
||||||
"napalm_opnsense/opnsense.py::OPNsenseDriver._regenerate_web_cert": 51,
|
"napalm_opnsense/opnsense.py::OPNsenseDriver._regenerate_web_cert": 51,
|
||||||
"napalm_opnsense/opnsense.py::OPNsenseDriver.apply_dhcp_subnet": 60,
|
"napalm_opnsense/opnsense.py::OPNsenseDriver.apply_dhcp_subnet": 60,
|
||||||
"napalm_opnsense/opnsense.py::OPNsenseDriver.create_dhcp_reservation": 85,
|
"napalm_opnsense/opnsense.py::OPNsenseDriver.create_dhcp_reservation": 85,
|
||||||
@@ -57,7 +56,7 @@
|
|||||||
"napalm_opnsense.resolver_mixin"
|
"napalm_opnsense.resolver_mixin"
|
||||||
],
|
],
|
||||||
"python_file_lines": {
|
"python_file_lines": {
|
||||||
"napalm_opnsense/opnsense.py": 3083
|
"napalm_opnsense/opnsense.py": 3077
|
||||||
},
|
},
|
||||||
"untyped_public": [
|
"untyped_public": [
|
||||||
"napalm_opnsense/opnsense.py::OPNsenseDriver.get_snmp_config"
|
"napalm_opnsense/opnsense.py::OPNsenseDriver.get_snmp_config"
|
||||||
|
|||||||
@@ -2888,6 +2888,44 @@ class TestSyncDnsZone:
|
|||||||
|
|
||||||
assert len([p for p, _ in calls if p.endswith("addhostoverride")]) == 1
|
assert len([p for p, _ in calls if p.endswith("addhostoverride")]) == 1
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("service", ["unbound", "dnsmasq"])
|
||||||
|
def test_a_failed_read_raises_and_changes_nothing(self, driver, service):
|
||||||
|
"""#14: a sync on an inventory it could not read deleted nothing and
|
||||||
|
added every record a second time."""
|
||||||
|
calls = []
|
||||||
|
|
||||||
|
def _post(path, data=None):
|
||||||
|
calls.append(path)
|
||||||
|
if path.endswith("searchhostoverride"):
|
||||||
|
raise requests.exceptions.ConnectionError("reset by peer")
|
||||||
|
return {"result": "saved"}
|
||||||
|
|
||||||
|
driver._detect_active_dns = lambda: service
|
||||||
|
driver._post = _post
|
||||||
|
|
||||||
|
with pytest.raises(requests.exceptions.ConnectionError):
|
||||||
|
driver.sync_dns_zone(
|
||||||
|
"office.local",
|
||||||
|
[{"hostname": "nas", "ip": "172.22.40.8", "record_type": "A", "enabled": True}],
|
||||||
|
)
|
||||||
|
|
||||||
|
assert calls == [f"/api/{service}/settings/searchhostoverride"]
|
||||||
|
|
||||||
|
|
||||||
|
class TestHostOverrideReadFailure:
|
||||||
|
"""A failed read raises: an empty list would say the firewall has no overrides."""
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("service", ["unbound", "dnsmasq"])
|
||||||
|
def test_get_dns_entries_raises_when_the_search_fails(self, driver, service):
|
||||||
|
def _post(path, data=None):
|
||||||
|
raise requests.exceptions.HTTPError("500 Server Error")
|
||||||
|
|
||||||
|
driver._detect_active_dns = lambda: service
|
||||||
|
driver._post = _post
|
||||||
|
|
||||||
|
with pytest.raises(requests.exceptions.HTTPError):
|
||||||
|
driver.get_dns_entries()
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# uninstall_package – says what it cannot do
|
# uninstall_package – says what it cannot do
|
||||||
|
|||||||
Reference in new issue
Block a user