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
+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]:
|
||||
|
||||
Reference in new issue
Block a user