fix: correct RDATA encoding and batch processing in CoreDNS MySQL backend 🐛

- Fix dnspython silently relativizing in-zone FQDN targets to '@' by
  calling rdata.to_text(origin=origin, relativize=False); CoreDNS MySQL
  requires absolute FQDNs in RDATA and was serving '.' for any CNAME/MX
  pointing to the zone apex
- Reorder write_zone to delete stale records before inserting new ones
  so a brief NXDOMAIN is preferred over briefly serving duplicate records
- Rework save-queue batch loop: keep batch open until queue is empty
  rather than closing after a fixed timeout, so sequential DA zone pushes
  accumulate into a single batch
- Add managed_by='directadmin' to _ensure_zone_exists for new and
  legacy NULL rows
This commit is contained in:
2026-02-25 15:43:08 +13:00
parent 83fbb03cad
commit 0b31b75789
7 changed files with 149 additions and 264 deletions

View File

@@ -199,90 +199,3 @@ def test_write_zone_migrates_null_managed_by(mysql_backend):
assert zone.managed_by == "directadmin"
session.close()
# ---------------------------------------------------------------------------
# _relativize_name — apex/in-zone/external normalisation for CoreDNS MySQL
# ---------------------------------------------------------------------------
def test_relativize_apex_symbol(mysql_backend):
assert mysql_backend._relativize_name("example.com", "@") == "@"
def test_relativize_dot(mysql_backend):
assert mysql_backend._relativize_name("example.com", ".") == "@"
def test_relativize_zone_fqdn_to_apex(mysql_backend):
"""Full zone FQDN must become '@' — storing it as-is causes CoreDNS to serve '.'."""
assert mysql_backend._relativize_name("example.com", "example.com.") == "@"
def test_relativize_in_zone_subdomain(mysql_backend):
assert mysql_backend._relativize_name("example.com", "mail.example.com.") == "mail"
def test_relativize_external_fqdn_unchanged(mysql_backend):
assert mysql_backend._relativize_name("example.com", "mail.google.com.") == "mail.google.com."
def test_relativize_already_relative_unchanged(mysql_backend):
assert mysql_backend._relativize_name("example.com", "mail") == "mail"
# ---------------------------------------------------------------------------
# MX record normalization via write_zone
# ---------------------------------------------------------------------------
MX_APEX_ZONE = """\
$ORIGIN example.com.
$TTL 300
example.com. 300 IN SOA ns.example.com. admin.example.com. (2023 3600 1800 604800 86400)
example.com. 300 IN MX 0 example.com.
example.com. 300 IN MX 10 mail.google.com.
"""
MX_RELATIVE_ZONE = """\
$ORIGIN example.com.
$TTL 300
example.com. 300 IN SOA ns.example.com. admin.example.com. (2023 3600 1800 604800 86400)
example.com. 300 IN MX 0 @
example.com. 300 IN MX 10 mail.google.com.
"""
def _get_mx_data(mysql_backend, zone_name="example.com"):
session = mysql_backend.Session()
zone = session.execute(
select(Zone).filter_by(zone_name=zone_name + ".")
).scalar_one_or_none()
records = (
session.execute(
select(Record).filter_by(zone_id=zone.id, type="MX")
).scalars().all()
)
result = {r.data for r in records}
session.close()
return result
def test_mx_apex_fqdn_stored_as_at_symbol(mysql_backend):
"""MX pointing to zone FQDN must be stored as '0 @'."""
mysql_backend.write_zone("example.com", MX_APEX_ZONE)
mx_data = _get_mx_data(mysql_backend)
assert "0 @" in mx_data
assert not any("example.com" in d for d in mx_data)
def test_mx_apex_at_symbol_stored_as_at_symbol(mysql_backend):
"""MX '0 @' (already relative) must remain '0 @'."""
mysql_backend.write_zone("example.com", MX_RELATIVE_ZONE)
mx_data = _get_mx_data(mysql_backend)
assert "0 @" in mx_data
def test_mx_external_fqdn_stored_unchanged(mysql_backend):
"""External MX target must be stored as absolute FQDN."""
mysql_backend.write_zone("example.com", MX_APEX_ZONE)
mx_data = _get_mx_data(mysql_backend)
assert "10 mail.google.com." in mx_data