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
+107
-73
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
|
||||
of always port 443 on the hostname, so a firewall whose API listens elsewhere no
|
||||
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
|
||||
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
|
||||
|
||||
+64
-70
@@ -2823,7 +2823,7 @@ 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.
|
||||
is running, returns an empty list. A failed read of the overrides raises.
|
||||
|
||||
Each entry::
|
||||
|
||||
@@ -2849,81 +2849,75 @@ class OPNsenseDriver(
|
||||
def _get_unbound_host_overrides(self) -> list[dict[str, Any]]:
|
||||
result: list[dict[str, Any]] = []
|
||||
seen: set[tuple[str, str, str, str]] = set()
|
||||
try:
|
||||
data = self._post(
|
||||
"/api/unbound/settings/searchhostoverride",
|
||||
{"current": 1, "rowCount": -1, "searchPhrase": ""},
|
||||
)
|
||||
rows = data.get("rows", []) or []
|
||||
# searchhostoverride lists alias records alongside the host override
|
||||
# they hang off, each under its own UUID. OPNsense flags them, so
|
||||
# skip them by that flag and report every remaining row as it is —
|
||||
# including two rows that happen to hold the same name and address.
|
||||
# Those are duplicates on the device, and sync_dns_zone can only
|
||||
# clear the copies it is told about.
|
||||
flagged = any("isAlias" in row for row in rows)
|
||||
for row in rows:
|
||||
if flagged and row.get("isAlias"):
|
||||
data = self._post(
|
||||
"/api/unbound/settings/searchhostoverride",
|
||||
{"current": 1, "rowCount": -1, "searchPhrase": ""},
|
||||
)
|
||||
rows = data.get("rows", []) or []
|
||||
# searchhostoverride lists alias records alongside the host override
|
||||
# they hang off, each under its own UUID. OPNsense flags them, so
|
||||
# skip them by that flag and report every remaining row as it is —
|
||||
# including two rows that happen to hold the same name and address.
|
||||
# Those are duplicates on the device, and sync_dns_zone can only
|
||||
# clear the copies it is told about.
|
||||
flagged = any("isAlias" in row for row in rows)
|
||||
for row in rows:
|
||||
if flagged and row.get("isAlias"):
|
||||
continue
|
||||
host = row.get("hostname", "") or ""
|
||||
domain = row.get("domain", "") or ""
|
||||
fqdn = f"{host}.{domain}" if host and domain else host or domain
|
||||
ip = row.get("server", "") or ""
|
||||
rr = row.get("rr", "A") or "A"
|
||||
if not flagged:
|
||||
# Releases without the flag give nothing to tell an alias
|
||||
# apart from a copy, so collapsing by content stays the
|
||||
# safer read there.
|
||||
key = (host.lower(), domain.lower(), ip, rr)
|
||||
if key in seen:
|
||||
continue
|
||||
host = row.get("hostname", "") or ""
|
||||
domain = row.get("domain", "") or ""
|
||||
fqdn = f"{host}.{domain}" if host and domain else host or domain
|
||||
ip = row.get("server", "") or ""
|
||||
rr = row.get("rr", "A") or "A"
|
||||
if not flagged:
|
||||
# Releases without the flag give nothing to tell an alias
|
||||
# apart from a copy, so collapsing by content stays the
|
||||
# safer read there.
|
||||
key = (host.lower(), domain.lower(), ip, rr)
|
||||
if key in seen:
|
||||
continue
|
||||
seen.add(key)
|
||||
# The auto-PTR flag is "addptr" since 25.x, "ptrrecord" before.
|
||||
ptr = row.get("addptr", row.get("ptrrecord", "1"))
|
||||
result.append(
|
||||
{
|
||||
"uuid": row.get("uuid", ""),
|
||||
"hostname": host,
|
||||
"domain": domain,
|
||||
"fqdn": fqdn,
|
||||
"ip": ip,
|
||||
"record_type": rr,
|
||||
"description": row.get("description", "") or "",
|
||||
"enabled": str(row.get("enabled", "1")) == "1",
|
||||
"ptrrecord": str(ptr) == "1",
|
||||
"service": "unbound",
|
||||
}
|
||||
)
|
||||
except Exception as exc:
|
||||
logger.warning("Failed to fetch Unbound host overrides: %s", exc)
|
||||
seen.add(key)
|
||||
# The auto-PTR flag is "addptr" since 25.x, "ptrrecord" before.
|
||||
ptr = row.get("addptr", row.get("ptrrecord", "1"))
|
||||
result.append(
|
||||
{
|
||||
"uuid": row.get("uuid", ""),
|
||||
"hostname": host,
|
||||
"domain": domain,
|
||||
"fqdn": fqdn,
|
||||
"ip": ip,
|
||||
"record_type": rr,
|
||||
"description": row.get("description", "") or "",
|
||||
"enabled": str(row.get("enabled", "1")) == "1",
|
||||
"ptrrecord": str(ptr) == "1",
|
||||
"service": "unbound",
|
||||
}
|
||||
)
|
||||
return sorted(result, key=lambda e: e["fqdn"].lower())
|
||||
|
||||
def _get_dnsmasq_host_overrides(self) -> list[dict[str, Any]]:
|
||||
result: list[dict[str, Any]] = []
|
||||
try:
|
||||
data = self._post(
|
||||
"/api/dnsmasq/settings/searchhostoverride",
|
||||
{"current": 1, "rowCount": -1, "searchPhrase": ""},
|
||||
data = self._post(
|
||||
"/api/dnsmasq/settings/searchhostoverride",
|
||||
{"current": 1, "rowCount": -1, "searchPhrase": ""},
|
||||
)
|
||||
for row in data.get("rows", []):
|
||||
host = row.get("host", "") or ""
|
||||
domain = row.get("domain", "") or ""
|
||||
fqdn = f"{host}.{domain}" if host and domain else host or domain
|
||||
result.append(
|
||||
{
|
||||
"uuid": row.get("uuid", ""),
|
||||
"hostname": host,
|
||||
"domain": domain,
|
||||
"fqdn": fqdn,
|
||||
"ip": row.get("ip", "") or "",
|
||||
"record_type": "A",
|
||||
"description": row.get("description", "") or "",
|
||||
"enabled": str(row.get("enabled", "1")) == "1",
|
||||
"service": "dnsmasq",
|
||||
}
|
||||
)
|
||||
for row in data.get("rows", []):
|
||||
host = row.get("host", "") or ""
|
||||
domain = row.get("domain", "") or ""
|
||||
fqdn = f"{host}.{domain}" if host and domain else host or domain
|
||||
result.append(
|
||||
{
|
||||
"uuid": row.get("uuid", ""),
|
||||
"hostname": host,
|
||||
"domain": domain,
|
||||
"fqdn": fqdn,
|
||||
"ip": row.get("ip", "") or "",
|
||||
"record_type": "A",
|
||||
"description": row.get("description", "") or "",
|
||||
"enabled": str(row.get("enabled", "1")) == "1",
|
||||
"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())
|
||||
|
||||
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::_socket": 19,
|
||||
"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.apply_dhcp_subnet": 21,
|
||||
"napalm_opnsense/opnsense.py::OPNsenseDriver.apply_firewall_rule": 11,
|
||||
@@ -31,7 +31,6 @@
|
||||
"napalm_opnsense/virtual_ips.py::carp_addresses": 14
|
||||
},
|
||||
"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.apply_dhcp_subnet": 60,
|
||||
"napalm_opnsense/opnsense.py::OPNsenseDriver.create_dhcp_reservation": 85,
|
||||
@@ -57,7 +56,7 @@
|
||||
"napalm_opnsense.resolver_mixin"
|
||||
],
|
||||
"python_file_lines": {
|
||||
"napalm_opnsense/opnsense.py": 3083
|
||||
"napalm_opnsense/opnsense.py": 3077
|
||||
},
|
||||
"untyped_public": [
|
||||
"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
|
||||
|
||||
@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
|
||||
|
||||
Reference in new issue
Block a user