Support delegated DNS editing and additional record types
This commit is contained in:
1 parent
aba83d299d
commit
a8b51ce629
14 files changed
+402
-24
No files matched your search
+111
-5
@@ -6,6 +6,9 @@ import sys
|
||||
from dataclasses import asdict, dataclass
|
||||
from pathlib import Path
|
||||
|
||||
import dns.exception
|
||||
import dns.name
|
||||
import dns.rdata
|
||||
import idna
|
||||
|
||||
from .domain_names import canonical_domain
|
||||
@@ -14,6 +17,8 @@ from .node_retirement import RetirementRequest, parse_retirement
|
||||
|
||||
BASE_MANIFEST_VERSION = 1
|
||||
MAIL_RECORD_MANIFEST_VERSION = 2
|
||||
EXTENDED_MANIFEST_VERSION = 3
|
||||
EXTRA_RECORD_TYPES = ("CNAME", "SRV", "CAA")
|
||||
MANIFEST_KIND = "bind9_zone"
|
||||
MAX_SOA_SERIAL = 2**32 - 1
|
||||
TXT_CHUNK_SIZE = 255
|
||||
@@ -168,6 +173,62 @@ class TxtRecord:
|
||||
return f"{self.name}. IN TXT {text}"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ExtraRecord:
|
||||
name: str
|
||||
record_type: str
|
||||
value: str
|
||||
|
||||
def __post_init__(self):
|
||||
name = txt_owner_name(self.name)
|
||||
if (
|
||||
self.record_type not in EXTRA_RECORD_TYPES
|
||||
or not isinstance(self.value, str)
|
||||
or not self.value
|
||||
or not self.value.isprintable()
|
||||
):
|
||||
raise ValueError("Enter a CNAME, SRV, or CAA record value.")
|
||||
if self.record_type == "SRV" and not re.match(
|
||||
r"^_[a-z0-9-]+\._[a-z0-9-]+\.", name
|
||||
):
|
||||
raise ValueError("SRV names start with _service._protocol.")
|
||||
try:
|
||||
value = dns.rdata.from_text(
|
||||
"IN",
|
||||
self.record_type,
|
||||
self.value,
|
||||
origin=dns.name.root,
|
||||
relativize=False,
|
||||
)
|
||||
except (dns.exception.DNSException, ValueError):
|
||||
raise ValueError(
|
||||
f"Invalid {self.record_type} record value."
|
||||
) from None
|
||||
if self.record_type == "CNAME":
|
||||
target = txt_owner_name(value.target.to_text())
|
||||
text = f"{target}."
|
||||
elif self.record_type == "SRV" and value.target != dns.name.root:
|
||||
target = dns_name(value.target.to_text())
|
||||
text = f"{value.priority} {value.weight} {value.port} {target}."
|
||||
else:
|
||||
text = value.to_text()
|
||||
object.__setattr__(self, "name", name)
|
||||
object.__setattr__(self, "value", text)
|
||||
|
||||
@classmethod
|
||||
def from_manifest(cls, data):
|
||||
if not isinstance(data, dict) or data.keys() != {
|
||||
"name",
|
||||
"record_type",
|
||||
"value",
|
||||
}:
|
||||
raise ValueError("Expected a DNS record name, type, and value.")
|
||||
return cls(**data)
|
||||
|
||||
def render(self):
|
||||
return f"{self.name}. IN {self.record_type} {self.value}"
|
||||
|
||||
|
||||
def read_records(data, key, record_type):
|
||||
records = data.get(key, [])
|
||||
if not isinstance(records, list):
|
||||
@@ -184,6 +245,7 @@ class DnsZone:
|
||||
addresses: tuple[AddressRecord, ...] = ()
|
||||
mx_records: tuple[MxRecord, ...] = ()
|
||||
txt_records: tuple[TxtRecord, ...] = ()
|
||||
extra_records: tuple[ExtraRecord, ...] = ()
|
||||
|
||||
def __post_init__(self):
|
||||
object.__setattr__(self, "domain", dns_name(self.domain))
|
||||
@@ -215,6 +277,37 @@ class DnsZone:
|
||||
"Each in-zone nameserver needs an address record."
|
||||
)
|
||||
self.check_mail_records(address_names)
|
||||
self.check_extra_records(address_names)
|
||||
|
||||
def check_extra_records(self, address_names):
|
||||
ordinary = (
|
||||
address_names
|
||||
| {self.domain}
|
||||
| {record.name for record in (*self.mx_records, *self.txt_records)}
|
||||
)
|
||||
aliases = {}
|
||||
for record in self.extra_records:
|
||||
if not in_zone(record.name, self.domain):
|
||||
raise ValueError("DNS record names must belong to the zone.")
|
||||
if record.record_type == "CNAME":
|
||||
if record.name in aliases:
|
||||
raise ValueError("A CNAME name may only have one target.")
|
||||
aliases[record.name] = txt_owner_name(record.value)
|
||||
else:
|
||||
ordinary.add(record.name)
|
||||
if ordinary.intersection(aliases):
|
||||
raise ValueError(
|
||||
"A CNAME cannot share its name with another record."
|
||||
)
|
||||
visited = set()
|
||||
for name in aliases:
|
||||
chain = set()
|
||||
while name in aliases and name not in visited:
|
||||
if name in chain:
|
||||
raise ValueError("The CNAME records create a loop.")
|
||||
chain.add(name)
|
||||
name = aliases[name]
|
||||
visited.update(chain)
|
||||
|
||||
def check_mail_records(self, address_names):
|
||||
if any(
|
||||
@@ -249,6 +342,7 @@ class DnsZone:
|
||||
if type(version) is not int or version not in (
|
||||
BASE_MANIFEST_VERSION,
|
||||
MAIL_RECORD_MANIFEST_VERSION,
|
||||
EXTENDED_MANIFEST_VERSION,
|
||||
):
|
||||
raise ValueError("Unsupported DNS zone manifest version.")
|
||||
fields = {
|
||||
@@ -260,8 +354,10 @@ class DnsZone:
|
||||
"responsible_name",
|
||||
"addresses",
|
||||
}
|
||||
if version == MAIL_RECORD_MANIFEST_VERSION:
|
||||
if version >= MAIL_RECORD_MANIFEST_VERSION:
|
||||
fields.update(("mx_records", "txt_records"))
|
||||
if version == EXTENDED_MANIFEST_VERSION:
|
||||
fields.add("extra_records")
|
||||
if data.keys() != fields:
|
||||
raise ValueError("Expected a BIND9 zone manifest.")
|
||||
if not isinstance(data["nameservers"], list):
|
||||
@@ -274,13 +370,18 @@ class DnsZone:
|
||||
addresses=read_records(data, "addresses", AddressRecord),
|
||||
mx_records=read_records(data, "mx_records", MxRecord),
|
||||
txt_records=read_records(data, "txt_records", TxtRecord),
|
||||
extra_records=read_records(data, "extra_records", ExtraRecord),
|
||||
)
|
||||
|
||||
def manifest_identity(self):
|
||||
version = (
|
||||
MAIL_RECORD_MANIFEST_VERSION
|
||||
if self.mx_records or self.txt_records
|
||||
else BASE_MANIFEST_VERSION
|
||||
EXTENDED_MANIFEST_VERSION
|
||||
if self.extra_records
|
||||
else (
|
||||
MAIL_RECORD_MANIFEST_VERSION
|
||||
if self.mx_records or self.txt_records
|
||||
else BASE_MANIFEST_VERSION
|
||||
)
|
||||
)
|
||||
return {
|
||||
"version": version,
|
||||
@@ -295,11 +396,15 @@ class DnsZone:
|
||||
"responsible_name": self.responsible_name,
|
||||
"addresses": [asdict(record) for record in self.addresses],
|
||||
}
|
||||
if manifest["version"] == MAIL_RECORD_MANIFEST_VERSION:
|
||||
if manifest["version"] >= MAIL_RECORD_MANIFEST_VERSION:
|
||||
manifest.update(
|
||||
mx_records=[asdict(record) for record in self.mx_records],
|
||||
txt_records=[asdict(record) for record in self.txt_records],
|
||||
)
|
||||
if self.extra_records:
|
||||
manifest["extra_records"] = [
|
||||
asdict(record) for record in self.extra_records
|
||||
]
|
||||
return manifest
|
||||
|
||||
def render(self):
|
||||
@@ -316,6 +421,7 @@ class DnsZone:
|
||||
)
|
||||
lines.extend(record.render() for record in self.mx_records)
|
||||
lines.extend(record.render() for record in self.txt_records)
|
||||
lines.extend(record.render() for record in self.extra_records)
|
||||
return "\n".join(lines) + "\n"
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user