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
@@ -1280,6 +1280,22 @@ Each request captures the reviewed SOA serial and records. You can edit the
|
|||||||
zone while it publishes. Publish the newer revision after the current request
|
zone while it publishes. Publish the newer revision after the current request
|
||||||
finishes. Use **Retry publishing** if the current revision failed.
|
finishes. Use **Retry publishing** if the current revision failed.
|
||||||
|
|
||||||
|
To let the customer and the assigned reseller edit a zone's records, enable
|
||||||
|
**Delegate record editing** on that zone. They can also publish the zone when
|
||||||
|
Coulobre publishing is enabled. The operator controls the authoritative
|
||||||
|
nameservers, SOA responsible name, and automatic mail DNS setting. In
|
||||||
|
**Additional records**, enter one fully qualified name, type, and value per
|
||||||
|
line. Use `@` for the zone apex.
|
||||||
|
|
||||||
|
```text
|
||||||
|
www.example.net CNAME example.net.
|
||||||
|
_sip._tcp.example.net SRV 10 0 5060 sip.example.net.
|
||||||
|
@ CAA 0 issue "letsencrypt.org"
|
||||||
|
```
|
||||||
|
|
||||||
|
Upgrade a separate BIND9 node to the same Coulobre version before publishing
|
||||||
|
zones with these records.
|
||||||
|
|
||||||
## Provision local mailboxes from the panel
|
## Provision local mailboxes from the panel
|
||||||
|
|
||||||
To provision Postfix and Dovecot on another host, use the
|
To provision Postfix and Dovecot on another host, use the
|
||||||
|
|||||||
+22
-3
@@ -349,6 +349,24 @@ Supply TXT values as plain JSON strings. The renderer quotes the values and
|
|||||||
splits long UTF-8 strings into DNS character strings. DKIM readers concatenate
|
splits long UTF-8 strings into DNS character strings. DKIM readers concatenate
|
||||||
the strings in the TXT record. [RFC 1035](https://www.rfc-editor.org/rfc/rfc1035.html) and [RFC 6376](https://www.rfc-editor.org/rfc/rfc6376.html)
|
the strings in the TXT record. [RFC 1035](https://www.rfc-editor.org/rfc/rfc1035.html) and [RFC 6376](https://www.rfc-editor.org/rfc/rfc6376.html)
|
||||||
|
|
||||||
|
### Include CNAME, SRV, and CAA records in a manifest
|
||||||
|
|
||||||
|
Version 3 adds an `extra_records` array and retains the two arrays from
|
||||||
|
version 2. The panel emits version 3 for zones that use an extra record type.
|
||||||
|
The node still accepts version 1 and 2 manifests.
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"name": "_sip._tcp.example.net",
|
||||||
|
"record_type": "SRV",
|
||||||
|
"value": "10 0 5060 sip.example.net."
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Use fully qualified names for CNAME targets and SRV target hosts. Use `.`
|
||||||
|
as an SRV target for an unavailable service. A CNAME cannot share its owner
|
||||||
|
with any other record or form a loop within the zone.
|
||||||
|
|
||||||
### Apply a zone manifest
|
### Apply a zone manifest
|
||||||
|
|
||||||
Install `bind9` and `bind9-utils` on the DNS node. Use BIND9's default
|
Install `bind9` and `bind9-utils` on the DNS node. Use BIND9's default
|
||||||
@@ -386,7 +404,7 @@ sudo systemctl enable --now coulobre-dns-apply.socket
|
|||||||
```
|
```
|
||||||
|
|
||||||
Grant the socket group only to the service identity responsible for publishing
|
Grant the socket group only to the service identity responsible for publishing
|
||||||
zones. Send one version 1 or 2 zone manifest as UTF-8 JSON to
|
zones. Send one version 1, 2, or 3 zone manifest as UTF-8 JSON to
|
||||||
`/run/coulobre-dns-apply.sock`, with a limit of 1 MiB. Shut down the writing
|
`/run/coulobre-dns-apply.sock`, with a limit of 1 MiB. Shut down the writing
|
||||||
half of the connection and read the JSON reply to EOF. For a valid manifest,
|
half of the connection and read the JSON reply to EOF. For a valid manifest,
|
||||||
the reply identifies the domain and SOA serial. A `configured` reply means
|
the reply identifies the domain and SOA serial. A `configured` reply means
|
||||||
@@ -810,8 +828,9 @@ python manage.py import_directadmin_zone user.admin.customer.tar.zst 1
|
|||||||
python manage.py import_directadmin_zone user.admin.customer.tar.zst 1 --apply
|
python manage.py import_directadmin_zone user.admin.customer.tar.zst 1 --apply
|
||||||
```
|
```
|
||||||
|
|
||||||
Coulobre uses its own TTL and SOA timers. Record types outside the mapping,
|
Coulobre imports A, AAAA, MX, TXT, CNAME, SRV, and CAA with its own TTL and
|
||||||
such as CNAME, SRV, delegated NS, and DNSSEC records, appear in the preview.
|
SOA timers. Record types outside the mapping, such as delegated NS and DNSSEC
|
||||||
|
records, appear in the preview.
|
||||||
Use `--apply --skip-unsupported` only after deciding how the omitted records
|
Use `--apply --skip-unsupported` only after deciding how the omitted records
|
||||||
will be served. A differing destination zone blocks the import. An identical
|
will be served. A differing destination zone blocks the import. An identical
|
||||||
zone can be previewed or reapplied without creating another copy. Adjust
|
zone can be previewed or reapplied without creating another copy. Adjust
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import dns.zone
|
|||||||
|
|
||||||
from .directadmin import MAX_METADATA_SIZE
|
from .directadmin import MAX_METADATA_SIZE
|
||||||
from .directadmin_payload import read_resource
|
from .directadmin_payload import read_resource
|
||||||
from .dns_node import AddressRecord, DnsZone, MxRecord, TxtRecord
|
from .dns_node import AddressRecord, DnsZone, ExtraRecord, MxRecord, TxtRecord
|
||||||
from .domain_names import canonical_domain
|
from .domain_names import canonical_domain
|
||||||
|
|
||||||
|
|
||||||
@@ -49,7 +49,8 @@ def convert_zone(text, domain):
|
|||||||
if master in nameservers:
|
if master in nameservers:
|
||||||
nameservers.remove(master)
|
nameservers.remove(master)
|
||||||
nameservers.insert(0, master)
|
nameservers.insert(0, master)
|
||||||
addresses, mx_records, txt_records, unsupported = [], [], [], []
|
addresses, mx_records, txt_records, extra_records = [], [], [], []
|
||||||
|
unsupported = []
|
||||||
for owner, records in source.iterate_rdatasets():
|
for owner, records in source.iterate_rdatasets():
|
||||||
name = str(owner)
|
name = str(owner)
|
||||||
record_type = dns.rdatatype.to_text(records.rdtype)
|
record_type = dns.rdatatype.to_text(records.rdtype)
|
||||||
@@ -72,6 +73,10 @@ def convert_zone(text, domain):
|
|||||||
name, b"".join(record.strings).decode("utf-8")
|
name, b"".join(record.strings).decode("utf-8")
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
case "CNAME" | "SRV" | "CAA":
|
||||||
|
extra_records.append(
|
||||||
|
ExtraRecord(name, record_type, record.to_text())
|
||||||
|
)
|
||||||
case _:
|
case _:
|
||||||
unsupported.append(
|
unsupported.append(
|
||||||
{
|
{
|
||||||
@@ -97,5 +102,6 @@ def convert_zone(text, domain):
|
|||||||
tuple(addresses),
|
tuple(addresses),
|
||||||
tuple(mx_records),
|
tuple(mx_records),
|
||||||
tuple(txt_records),
|
tuple(txt_records),
|
||||||
|
tuple(extra_records),
|
||||||
)
|
)
|
||||||
return zone, unsupported
|
return zone, unsupported
|
||||||
+111
-5
@@ -6,6 +6,9 @@ import sys
|
|||||||
from dataclasses import asdict, dataclass
|
from dataclasses import asdict, dataclass
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
import dns.exception
|
||||||
|
import dns.name
|
||||||
|
import dns.rdata
|
||||||
import idna
|
import idna
|
||||||
|
|
||||||
from .domain_names import canonical_domain
|
from .domain_names import canonical_domain
|
||||||
@@ -14,6 +17,8 @@ from .node_retirement import RetirementRequest, parse_retirement
|
|||||||
|
|
||||||
BASE_MANIFEST_VERSION = 1
|
BASE_MANIFEST_VERSION = 1
|
||||||
MAIL_RECORD_MANIFEST_VERSION = 2
|
MAIL_RECORD_MANIFEST_VERSION = 2
|
||||||
|
EXTENDED_MANIFEST_VERSION = 3
|
||||||
|
EXTRA_RECORD_TYPES = ("CNAME", "SRV", "CAA")
|
||||||
MANIFEST_KIND = "bind9_zone"
|
MANIFEST_KIND = "bind9_zone"
|
||||||
MAX_SOA_SERIAL = 2**32 - 1
|
MAX_SOA_SERIAL = 2**32 - 1
|
||||||
TXT_CHUNK_SIZE = 255
|
TXT_CHUNK_SIZE = 255
|
||||||
@@ -168,6 +173,62 @@ class TxtRecord:
|
|||||||
return f"{self.name}. IN TXT {text}"
|
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):
|
def read_records(data, key, record_type):
|
||||||
records = data.get(key, [])
|
records = data.get(key, [])
|
||||||
if not isinstance(records, list):
|
if not isinstance(records, list):
|
||||||
@@ -184,6 +245,7 @@ class DnsZone:
|
|||||||
addresses: tuple[AddressRecord, ...] = ()
|
addresses: tuple[AddressRecord, ...] = ()
|
||||||
mx_records: tuple[MxRecord, ...] = ()
|
mx_records: tuple[MxRecord, ...] = ()
|
||||||
txt_records: tuple[TxtRecord, ...] = ()
|
txt_records: tuple[TxtRecord, ...] = ()
|
||||||
|
extra_records: tuple[ExtraRecord, ...] = ()
|
||||||
|
|
||||||
def __post_init__(self):
|
def __post_init__(self):
|
||||||
object.__setattr__(self, "domain", dns_name(self.domain))
|
object.__setattr__(self, "domain", dns_name(self.domain))
|
||||||
@@ -215,6 +277,37 @@ class DnsZone:
|
|||||||
"Each in-zone nameserver needs an address record."
|
"Each in-zone nameserver needs an address record."
|
||||||
)
|
)
|
||||||
self.check_mail_records(address_names)
|
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):
|
def check_mail_records(self, address_names):
|
||||||
if any(
|
if any(
|
||||||
@@ -249,6 +342,7 @@ class DnsZone:
|
|||||||
if type(version) is not int or version not in (
|
if type(version) is not int or version not in (
|
||||||
BASE_MANIFEST_VERSION,
|
BASE_MANIFEST_VERSION,
|
||||||
MAIL_RECORD_MANIFEST_VERSION,
|
MAIL_RECORD_MANIFEST_VERSION,
|
||||||
|
EXTENDED_MANIFEST_VERSION,
|
||||||
):
|
):
|
||||||
raise ValueError("Unsupported DNS zone manifest version.")
|
raise ValueError("Unsupported DNS zone manifest version.")
|
||||||
fields = {
|
fields = {
|
||||||
@@ -260,8 +354,10 @@ class DnsZone:
|
|||||||
"responsible_name",
|
"responsible_name",
|
||||||
"addresses",
|
"addresses",
|
||||||
}
|
}
|
||||||
if version == MAIL_RECORD_MANIFEST_VERSION:
|
if version >= MAIL_RECORD_MANIFEST_VERSION:
|
||||||
fields.update(("mx_records", "txt_records"))
|
fields.update(("mx_records", "txt_records"))
|
||||||
|
if version == EXTENDED_MANIFEST_VERSION:
|
||||||
|
fields.add("extra_records")
|
||||||
if data.keys() != fields:
|
if data.keys() != fields:
|
||||||
raise ValueError("Expected a BIND9 zone manifest.")
|
raise ValueError("Expected a BIND9 zone manifest.")
|
||||||
if not isinstance(data["nameservers"], list):
|
if not isinstance(data["nameservers"], list):
|
||||||
@@ -274,13 +370,18 @@ class DnsZone:
|
|||||||
addresses=read_records(data, "addresses", AddressRecord),
|
addresses=read_records(data, "addresses", AddressRecord),
|
||||||
mx_records=read_records(data, "mx_records", MxRecord),
|
mx_records=read_records(data, "mx_records", MxRecord),
|
||||||
txt_records=read_records(data, "txt_records", TxtRecord),
|
txt_records=read_records(data, "txt_records", TxtRecord),
|
||||||
|
extra_records=read_records(data, "extra_records", ExtraRecord),
|
||||||
)
|
)
|
||||||
|
|
||||||
def manifest_identity(self):
|
def manifest_identity(self):
|
||||||
version = (
|
version = (
|
||||||
MAIL_RECORD_MANIFEST_VERSION
|
EXTENDED_MANIFEST_VERSION
|
||||||
if self.mx_records or self.txt_records
|
if self.extra_records
|
||||||
else BASE_MANIFEST_VERSION
|
else (
|
||||||
|
MAIL_RECORD_MANIFEST_VERSION
|
||||||
|
if self.mx_records or self.txt_records
|
||||||
|
else BASE_MANIFEST_VERSION
|
||||||
|
)
|
||||||
)
|
)
|
||||||
return {
|
return {
|
||||||
"version": version,
|
"version": version,
|
||||||
@@ -295,11 +396,15 @@ class DnsZone:
|
|||||||
"responsible_name": self.responsible_name,
|
"responsible_name": self.responsible_name,
|
||||||
"addresses": [asdict(record) for record in self.addresses],
|
"addresses": [asdict(record) for record in self.addresses],
|
||||||
}
|
}
|
||||||
if manifest["version"] == MAIL_RECORD_MANIFEST_VERSION:
|
if manifest["version"] >= MAIL_RECORD_MANIFEST_VERSION:
|
||||||
manifest.update(
|
manifest.update(
|
||||||
mx_records=[asdict(record) for record in self.mx_records],
|
mx_records=[asdict(record) for record in self.mx_records],
|
||||||
txt_records=[asdict(record) for record in self.txt_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
|
return manifest
|
||||||
|
|
||||||
def render(self):
|
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.mx_records)
|
||||||
lines.extend(record.render() for record in self.txt_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"
|
return "\n".join(lines) + "\n"
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -36,6 +36,14 @@ def is_tenant(user):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def manages_account(user, account):
|
||||||
|
customer = customer_for_user(user)
|
||||||
|
if customer is not None:
|
||||||
|
return account.customer_id == customer.pk
|
||||||
|
reseller = reseller_for_user(user)
|
||||||
|
return reseller is not None and account.reseller_id == reseller.pk
|
||||||
|
|
||||||
|
|
||||||
def panel_access(request):
|
def panel_access(request):
|
||||||
customer = customer_for_user(request.user) is not None
|
customer = customer_for_user(request.user) is not None
|
||||||
reseller = not customer and reseller_for_user(request.user) is not None
|
reseller = not customer and reseller_for_user(request.user) is not None
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ from coulobre.dns_node import (
|
|||||||
MAX_SOA_SERIAL,
|
MAX_SOA_SERIAL,
|
||||||
AddressRecord,
|
AddressRecord,
|
||||||
DnsZone,
|
DnsZone,
|
||||||
|
ExtraRecord,
|
||||||
MxRecord,
|
MxRecord,
|
||||||
TxtRecord,
|
TxtRecord,
|
||||||
)
|
)
|
||||||
@@ -69,6 +70,20 @@ class DnsZoneForm(forms.Form):
|
|||||||
"Coulobre DNS node."
|
"Coulobre DNS node."
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
extra_records = forms.CharField(
|
||||||
|
label="CNAME, SRV and CAA records",
|
||||||
|
required=False,
|
||||||
|
widget=forms.Textarea(attrs={"rows": 4, "spellcheck": "false"}),
|
||||||
|
help_text=(
|
||||||
|
"Enter a name, type and value per line, such as "
|
||||||
|
'@ CAA 0 issue "example.net".'
|
||||||
|
),
|
||||||
|
)
|
||||||
|
delegate_dns = forms.BooleanField(
|
||||||
|
label="Delegate record editing",
|
||||||
|
help_text="Allow the customer and assigned reseller to edit records.",
|
||||||
|
required=False,
|
||||||
|
)
|
||||||
|
|
||||||
def __init__(self, *args, domain, **kwargs):
|
def __init__(self, *args, domain, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
@@ -100,6 +115,20 @@ class DnsZoneForm(forms.Form):
|
|||||||
def clean_txt_records(self):
|
def clean_txt_records(self):
|
||||||
return self.clean_mail_records("txt_records", self.parse_txt_record)
|
return self.clean_mail_records("txt_records", self.parse_txt_record)
|
||||||
|
|
||||||
|
def clean_extra_records(self):
|
||||||
|
return self.clean_mail_records(
|
||||||
|
"extra_records", self.parse_extra_record
|
||||||
|
)
|
||||||
|
|
||||||
|
def parse_extra_record(self, line):
|
||||||
|
try:
|
||||||
|
name, record_type, value = line.split(maxsplit=2)
|
||||||
|
except ValueError:
|
||||||
|
raise ValueError("Enter a name, type, and value.") from None
|
||||||
|
return ExtraRecord(
|
||||||
|
self.domain if name == "@" else name, record_type.upper(), value
|
||||||
|
)
|
||||||
|
|
||||||
def clean_mail_records(self, field, parse):
|
def clean_mail_records(self, field, parse):
|
||||||
records = []
|
records = []
|
||||||
for number, line in enumerate(
|
for number, line in enumerate(
|
||||||
@@ -152,6 +181,7 @@ class DnsZoneForm(forms.Form):
|
|||||||
addresses=data["addresses"],
|
addresses=data["addresses"],
|
||||||
mx_records=data["mx_records"],
|
mx_records=data["mx_records"],
|
||||||
txt_records=data["txt_records"],
|
txt_records=data["txt_records"],
|
||||||
|
extra_records=data["extra_records"],
|
||||||
)
|
)
|
||||||
except ValueError as error:
|
except ValueError as error:
|
||||||
raise forms.ValidationError(str(error)) from error
|
raise forms.ValidationError(str(error)) from error
|
||||||
@@ -162,6 +192,7 @@ class DnsZoneForm(forms.Form):
|
|||||||
addresses=manifest["addresses"],
|
addresses=manifest["addresses"],
|
||||||
mx_records=manifest.get("mx_records", []),
|
mx_records=manifest.get("mx_records", []),
|
||||||
txt_records=manifest.get("txt_records", []),
|
txt_records=manifest.get("txt_records", []),
|
||||||
|
extra_records=manifest.get("extra_records", []),
|
||||||
)
|
)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|||||||
@@ -1,28 +1,38 @@
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from django.core.exceptions import PermissionDenied
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
|
|
||||||
from coulobre.dns_node import DNS_REQUEST_LIMIT
|
from coulobre.dns_node import DNS_REQUEST_LIMIT
|
||||||
|
|
||||||
|
from .access import is_tenant, manages_account
|
||||||
from .models import DnsPublication, HostingDnsZone
|
from .models import DnsPublication, HostingDnsZone
|
||||||
from .node_client import encode_manifest
|
from .node_client import encode_manifest
|
||||||
from .resource_requests import lock_hosting_account, require_open_account
|
from .resource_requests import lock_hosting_account, require_open_account
|
||||||
|
|
||||||
|
|
||||||
def queue_dns_publication(zone_id, expected_serial):
|
def queue_dns_publication(zone_id, expected_serial, *, actor=None):
|
||||||
account_id = HostingDnsZone.objects.values_list(
|
account_id = HostingDnsZone.objects.values_list(
|
||||||
"account_id", flat=True
|
"account_id", flat=True
|
||||||
).get(pk=zone_id)
|
).get(pk=zone_id)
|
||||||
return publish_for_account(account_id, zone_id, expected_serial)
|
return publish_for_account(
|
||||||
|
account_id, zone_id, expected_serial, actor=actor
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@transaction.atomic
|
@transaction.atomic
|
||||||
def publish_for_account(account_id, zone_id, expected_serial):
|
def publish_for_account(account_id, zone_id, expected_serial, *, actor=None):
|
||||||
account = lock_hosting_account(account_id, customer_id=None)
|
account = lock_hosting_account(account_id, customer_id=None)
|
||||||
require_open_account(account)
|
|
||||||
zone = HostingDnsZone.objects.select_for_update().get(
|
zone = HostingDnsZone.objects.select_for_update().get(
|
||||||
pk=zone_id, account=account
|
pk=zone_id, account=account
|
||||||
)
|
)
|
||||||
|
if (
|
||||||
|
actor is not None
|
||||||
|
and is_tenant(actor)
|
||||||
|
and (not zone.delegate_dns or not manages_account(actor, account))
|
||||||
|
):
|
||||||
|
raise PermissionDenied
|
||||||
|
require_open_account(account)
|
||||||
if str(zone.serial) != expected_serial:
|
if str(zone.serial) != expected_serial:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"This zone changed before publication. "
|
"This zone changed before publication. "
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
# Generated by Django 5.2.19 on 2026-11-22 09:52
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
dependencies = [
|
||||||
|
("hosting", "0042_website_php_versions"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="hostingdnszone",
|
||||||
|
name="delegate_dns",
|
||||||
|
field=models.BooleanField(default=False),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="hostingdnszone",
|
||||||
|
name="extra_records",
|
||||||
|
field=models.JSONField(default=list),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -23,6 +23,7 @@ from coulobre.dns_node import (
|
|||||||
MAX_SOA_SERIAL,
|
MAX_SOA_SERIAL,
|
||||||
AddressRecord,
|
AddressRecord,
|
||||||
DnsZone,
|
DnsZone,
|
||||||
|
ExtraRecord,
|
||||||
MxRecord,
|
MxRecord,
|
||||||
TxtRecord,
|
TxtRecord,
|
||||||
)
|
)
|
||||||
@@ -533,6 +534,8 @@ class HostingDnsZone(models.Model):
|
|||||||
addresses = models.JSONField(default=list)
|
addresses = models.JSONField(default=list)
|
||||||
mx_records = models.JSONField(default=list)
|
mx_records = models.JSONField(default=list)
|
||||||
txt_records = models.JSONField(default=list)
|
txt_records = models.JSONField(default=list)
|
||||||
|
extra_records = models.JSONField(default=list)
|
||||||
|
delegate_dns = models.BooleanField(default=False)
|
||||||
manage_mail_dns = models.BooleanField(default=False)
|
manage_mail_dns = models.BooleanField(default=False)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
@@ -563,6 +566,9 @@ class HostingDnsZone(models.Model):
|
|||||||
txt_records=tuple(
|
txt_records=tuple(
|
||||||
TxtRecord.from_manifest(item) for item in self.txt_records
|
TxtRecord.from_manifest(item) for item in self.txt_records
|
||||||
),
|
),
|
||||||
|
extra_records=tuple(
|
||||||
|
ExtraRecord.from_manifest(item) for item in self.extra_records
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -154,14 +154,14 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<th scope="col">Domain</th>
|
<th scope="col">Domain</th>
|
||||||
<th scope="col">SOA serial</th>
|
<th scope="col">SOA serial</th>
|
||||||
{% if is_operator %}<th scope="col">Deployment</th>{% endif %}
|
{% if is_operator or tenant_dns_editing %}<th scope="col">Deployment</th>{% endif %}
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for zone in dns_zones %}
|
{% for zone in dns_zones %}
|
||||||
<tr>
|
<tr>
|
||||||
<td class="domain">
|
<td class="domain">
|
||||||
{% if is_operator and perms.hosting.change_hostingdnszone %}
|
{% if is_operator and perms.hosting.change_hostingdnszone or tenant_dns_editing and zone.delegate_dns %}
|
||||||
<a class="record-link" href="{% url 'panel:dns_zone_edit' zone.pk %}" aria-label="Edit DNS zone for {{ zone.domain }}">{{ zone.domain }}</a>
|
<a class="record-link" href="{% url 'panel:dns_zone_edit' zone.pk %}" aria-label="Edit DNS zone for {{ zone.domain }}">{{ zone.domain }}</a>
|
||||||
{% else %}
|
{% else %}
|
||||||
{{ zone.domain }}
|
{{ zone.domain }}
|
||||||
@@ -180,6 +180,12 @@
|
|||||||
<p class="heading-meta">{{ zone.publication.problem }}</p>
|
<p class="heading-meta">{{ zone.publication.problem }}</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</td>
|
</td>
|
||||||
|
{% elif tenant_dns_editing %}
|
||||||
|
<td>
|
||||||
|
{% if zone.delegate_dns %}
|
||||||
|
{% include "hosting/dns_publication.html" with publication=zone.publication local_dns_enabled=tenant_dns_publishing %}
|
||||||
|
{% else %}Managed by the operator{% endif %}
|
||||||
|
</td>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|||||||
@@ -34,6 +34,9 @@ ZONE = """$TTL 3600
|
|||||||
mail IN A 192.0.2.4
|
mail IN A 192.0.2.4
|
||||||
_dmarc IN TXT "v=DMARC1;" " p=reject"
|
_dmarc IN TXT "v=DMARC1;" " p=reject"
|
||||||
www IN CNAME @
|
www IN CNAME @
|
||||||
|
_sip._tcp IN SRV 10 20 5060 mail
|
||||||
|
@ IN CAA 0 issue "example.net"
|
||||||
|
@ IN HTTPS 1 .
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
@@ -158,10 +161,17 @@ class DirectAdminZoneTests(TestCase):
|
|||||||
def test_preview_and_explicit_import_do_not_publish_dns(self):
|
def test_preview_and_explicit_import_do_not_publish_dns(self):
|
||||||
preview = self.import_zone()
|
preview = self.import_zone()
|
||||||
self.assertEqual(preview["state"], "review")
|
self.assertEqual(preview["state"], "review")
|
||||||
self.assertEqual(preview["unsupported_records"][0]["type"], "CNAME")
|
self.assertEqual(preview["unsupported_records"][0]["type"], "HTTPS")
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
preview["zone"]["txt_records"][0]["text"], "v=DMARC1; p=reject"
|
preview["zone"]["txt_records"][0]["text"], "v=DMARC1; p=reject"
|
||||||
)
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
{
|
||||||
|
record["record_type"]
|
||||||
|
for record in preview["zone"]["extra_records"]
|
||||||
|
},
|
||||||
|
{"CNAME", "SRV", "CAA"},
|
||||||
|
)
|
||||||
with self.assertRaisesMessage(CommandError, "Review unsupported"):
|
with self.assertRaisesMessage(CommandError, "Review unsupported"):
|
||||||
self.import_zone(apply=True)
|
self.import_zone(apply=True)
|
||||||
self.assertFalse(HostingDnsZone.objects.exists())
|
self.assertFalse(HostingDnsZone.objects.exists())
|
||||||
|
|||||||
@@ -8,7 +8,13 @@ from unittest.mock import patch
|
|||||||
|
|
||||||
from django.test import SimpleTestCase
|
from django.test import SimpleTestCase
|
||||||
|
|
||||||
from coulobre.dns_node import DNS_REQUEST_LIMIT, DnsZone, TxtRecord, main
|
from coulobre.dns_node import (
|
||||||
|
DNS_REQUEST_LIMIT,
|
||||||
|
DnsZone,
|
||||||
|
ExtraRecord,
|
||||||
|
TxtRecord,
|
||||||
|
main,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class DnsZoneManifestTests(SimpleTestCase):
|
class DnsZoneManifestTests(SimpleTestCase):
|
||||||
@@ -43,6 +49,36 @@ class DnsZoneManifestTests(SimpleTestCase):
|
|||||||
self.assertEqual(zone.manifest()["version"], 1)
|
self.assertEqual(zone.manifest()["version"], 1)
|
||||||
self.assertEqual(DnsZone.from_manifest(zone.manifest()), zone)
|
self.assertEqual(DnsZone.from_manifest(zone.manifest()), zone)
|
||||||
|
|
||||||
|
def test_rejects_cname_conflicts_and_loops(self):
|
||||||
|
for records, message in (
|
||||||
|
(
|
||||||
|
[ExtraRecord("bücher.example", "CNAME", "elsewhere.example.")],
|
||||||
|
"cannot share its name",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
[
|
||||||
|
ExtraRecord(
|
||||||
|
"a.bücher.example", "CNAME", "b.xn--bcher-kva.example."
|
||||||
|
),
|
||||||
|
ExtraRecord(
|
||||||
|
"b.bücher.example", "CNAME", "a.xn--bcher-kva.example."
|
||||||
|
),
|
||||||
|
],
|
||||||
|
"create a loop",
|
||||||
|
),
|
||||||
|
):
|
||||||
|
payload = self.manifest | {
|
||||||
|
"version": 3,
|
||||||
|
"mx_records": [],
|
||||||
|
"txt_records": [],
|
||||||
|
"extra_records": [record.__dict__ for record in records],
|
||||||
|
}
|
||||||
|
with (
|
||||||
|
self.subTest(records=records),
|
||||||
|
self.assertRaisesMessage(ValueError, message),
|
||||||
|
):
|
||||||
|
DnsZone.from_manifest(payload)
|
||||||
|
|
||||||
def test_accepts_a_single_external_nameserver_without_address_records(
|
def test_accepts_a_single_external_nameserver_without_address_records(
|
||||||
self,
|
self,
|
||||||
):
|
):
|
||||||
|
|||||||
@@ -136,6 +136,52 @@ class DnsPublicationTests(TestCase):
|
|||||||
)
|
)
|
||||||
self.assertFalse(DnsPublication.objects.exists())
|
self.assertFalse(DnsPublication.objects.exists())
|
||||||
|
|
||||||
|
def test_delegated_tenant_can_edit_records_and_publish_only_owned_zones(
|
||||||
|
self,
|
||||||
|
):
|
||||||
|
self.zone.delegate_dns = True
|
||||||
|
self.zone.save(update_fields=["delegate_dns"])
|
||||||
|
self.client.force_login(self.customer)
|
||||||
|
self.assertContains(self.client.get(self.detail), "Publish zone")
|
||||||
|
edit = reverse("panel:dns_zone_edit", args=[self.zone.pk])
|
||||||
|
self.assertRedirects(
|
||||||
|
self.client.post(
|
||||||
|
edit,
|
||||||
|
{
|
||||||
|
"serial": 1,
|
||||||
|
"nameservers": "ns.attacker.example",
|
||||||
|
"responsible_name": "unauthorized.example",
|
||||||
|
"manage_mail_dns": "on",
|
||||||
|
"addresses": "site.example 192.0.2.1",
|
||||||
|
"extra_records": (
|
||||||
|
"www.site.example CNAME site.example.\n"
|
||||||
|
"_sip._tcp.site.example SRV 10 0 5060 site.example.\n"
|
||||||
|
'@ CAA 0 issue "example.net"'
|
||||||
|
),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
self.detail,
|
||||||
|
)
|
||||||
|
self.zone.refresh_from_db()
|
||||||
|
self.assertTrue(self.zone.delegate_dns)
|
||||||
|
self.assertEqual(self.zone.nameservers, ["ns.provider.example"])
|
||||||
|
self.assertEqual(self.zone.responsible_name, "hostmaster.site.example")
|
||||||
|
self.assertFalse(self.zone.manage_mail_dns)
|
||||||
|
self.assertEqual(self.zone.spec.manifest()["version"], 3)
|
||||||
|
self.assertRedirects(
|
||||||
|
self.client.post(self.url, {"serial": 2}), self.detail
|
||||||
|
)
|
||||||
|
candidate = DnsPublication.objects.get(zone=self.zone)
|
||||||
|
self.assertEqual(candidate.manifest, self.zone.spec.manifest())
|
||||||
|
|
||||||
|
outsider = get_user_model().objects.create_user("outsider")
|
||||||
|
Customer.objects.create(user=outsider)
|
||||||
|
self.client.force_login(outsider)
|
||||||
|
self.assertEqual(
|
||||||
|
self.client.post(self.url, {"serial": 2}).status_code, 403
|
||||||
|
)
|
||||||
|
self.assertEqual(self.client.get(edit).status_code, 403)
|
||||||
|
|
||||||
@override_settings(LOCAL_DNS_PUBLISHING=False)
|
@override_settings(LOCAL_DNS_PUBLISHING=False)
|
||||||
def test_disabled_publishing_keeps_manifest_downloads_available(self):
|
def test_disabled_publishing_keeps_manifest_downloads_available(self):
|
||||||
self.assertNotContains(self.client.get(self.detail), "Publish zone")
|
self.assertNotContains(self.client.get(self.detail), "Publish zone")
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ from .access import (
|
|||||||
TenantScopeMixin,
|
TenantScopeMixin,
|
||||||
customer_for_user,
|
customer_for_user,
|
||||||
is_tenant,
|
is_tenant,
|
||||||
|
manages_account,
|
||||||
reseller_for_user,
|
reseller_for_user,
|
||||||
)
|
)
|
||||||
from .account_backup_forms import (
|
from .account_backup_forms import (
|
||||||
@@ -539,6 +540,14 @@ class AccountDetail(TenantScopeMixin, PermissionRequiredMixin, DetailView):
|
|||||||
context["databases"] = databases
|
context["databases"] = databases
|
||||||
if tenant or user.has_perm(VIEW_DNS_ZONES):
|
if tenant or user.has_perm(VIEW_DNS_ZONES):
|
||||||
zone_list = self.object.dns_zones.all()
|
zone_list = self.object.dns_zones.all()
|
||||||
|
if tenant and getattr(self.request, "impersonation", None) is None:
|
||||||
|
zone_list = zone_list.select_related("publication").defer(
|
||||||
|
"publication__manifest"
|
||||||
|
)
|
||||||
|
context["tenant_dns_editing"] = True
|
||||||
|
context["tenant_dns_publishing"] = (
|
||||||
|
settings.LOCAL_DNS_PUBLISHING
|
||||||
|
)
|
||||||
if not tenant and user.has_perms(PUBLISH_DNS):
|
if not tenant and user.has_perms(PUBLISH_DNS):
|
||||||
zone_list = zone_list.select_related("publication").defer(
|
zone_list = zone_list.select_related("publication").defer(
|
||||||
"publication__manifest"
|
"publication__manifest"
|
||||||
@@ -1611,7 +1620,7 @@ class RequestDatabaseProvisioning(
|
|||||||
return redirect(database.account)
|
return redirect(database.account)
|
||||||
|
|
||||||
|
|
||||||
class DnsZoneEditor(OperatorOnlyMixin, PermissionRequiredMixin, FormView):
|
class DnsZoneEditor(PermissionRequiredMixin, FormView):
|
||||||
template_name = "hosting/dns_zone_form.html"
|
template_name = "hosting/dns_zone_form.html"
|
||||||
|
|
||||||
def get_form_kwargs(self):
|
def get_form_kwargs(self):
|
||||||
@@ -1626,7 +1635,7 @@ class DnsZoneEditor(OperatorOnlyMixin, PermissionRequiredMixin, FormView):
|
|||||||
return self.account.get_absolute_url()
|
return self.account.get_absolute_url()
|
||||||
|
|
||||||
|
|
||||||
class DnsZoneCreate(DnsZoneEditor):
|
class DnsZoneCreate(OperatorOnlyMixin, DnsZoneEditor):
|
||||||
form_class = DnsZoneForm
|
form_class = DnsZoneForm
|
||||||
permission_required = (
|
permission_required = (
|
||||||
VIEW_ACCOUNTS,
|
VIEW_ACCOUNTS,
|
||||||
@@ -1678,6 +1687,27 @@ class DnsZoneEdit(DnsZoneEditor):
|
|||||||
)
|
)
|
||||||
extra_context = {"page_title": "Edit DNS zone"}
|
extra_context = {"page_title": "Edit DNS zone"}
|
||||||
|
|
||||||
|
def has_permission(self):
|
||||||
|
if is_tenant(self.request.user):
|
||||||
|
return getattr(self.request, "impersonation", None) is None and (
|
||||||
|
self.zone.delegate_dns
|
||||||
|
and manages_account(self.request.user, self.account)
|
||||||
|
)
|
||||||
|
return super().has_permission()
|
||||||
|
|
||||||
|
def get_form(self, form_class=None):
|
||||||
|
form = super().get_form(form_class)
|
||||||
|
if is_tenant(self.request.user):
|
||||||
|
for field in (
|
||||||
|
"nameservers",
|
||||||
|
"responsible_name",
|
||||||
|
"delegate_dns",
|
||||||
|
"manage_mail_dns",
|
||||||
|
):
|
||||||
|
form.fields[field].disabled = True
|
||||||
|
form.fields[field].widget = forms.HiddenInput()
|
||||||
|
return form
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def zone(self):
|
def zone(self):
|
||||||
return get_object_or_404(
|
return get_object_or_404(
|
||||||
@@ -1697,6 +1727,7 @@ class DnsZoneEdit(DnsZoneEditor):
|
|||||||
return {
|
return {
|
||||||
"serial": self.zone.serial,
|
"serial": self.zone.serial,
|
||||||
"manage_mail_dns": self.zone.manage_mail_dns,
|
"manage_mail_dns": self.zone.manage_mail_dns,
|
||||||
|
"delegate_dns": self.zone.delegate_dns,
|
||||||
"nameservers": "\n".join(self.zone.nameservers),
|
"nameservers": "\n".join(self.zone.nameservers),
|
||||||
"responsible_name": self.zone.responsible_name,
|
"responsible_name": self.zone.responsible_name,
|
||||||
"addresses": "\n".join(
|
"addresses": "\n".join(
|
||||||
@@ -1710,6 +1741,10 @@ class DnsZoneEdit(DnsZoneEditor):
|
|||||||
"txt_records": "\n".join(
|
"txt_records": "\n".join(
|
||||||
format_txt_record(record) for record in self.zone.txt_records
|
format_txt_record(record) for record in self.zone.txt_records
|
||||||
),
|
),
|
||||||
|
"extra_records": "\n".join(
|
||||||
|
f"{record['name']} {record['record_type']} {record['value']}"
|
||||||
|
for record in self.zone.extra_records
|
||||||
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
@@ -1728,9 +1763,23 @@ class DnsZoneEdit(DnsZoneEditor):
|
|||||||
self.account.pk, customer_id=None
|
self.account.pk, customer_id=None
|
||||||
)
|
)
|
||||||
require_open_account(account)
|
require_open_account(account)
|
||||||
updated = HostingDnsZone.objects.filter(
|
zones = HostingDnsZone.objects.filter(
|
||||||
pk=self.zone.pk, serial=serial
|
pk=self.zone.pk, serial=serial
|
||||||
).update(**(values | {"serial": next_serial}))
|
)
|
||||||
|
if is_tenant(self.request.user):
|
||||||
|
if not manages_account(self.request.user, account):
|
||||||
|
raise PermissionDenied
|
||||||
|
zones = zones.filter(delegate_dns=True)
|
||||||
|
values = {
|
||||||
|
key: values[key]
|
||||||
|
for key in (
|
||||||
|
"addresses",
|
||||||
|
"mx_records",
|
||||||
|
"txt_records",
|
||||||
|
"extra_records",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
updated = zones.update(**(values | {"serial": next_serial}))
|
||||||
except ValidationError as error:
|
except ValidationError as error:
|
||||||
form.add_error(None, error)
|
form.add_error(None, error)
|
||||||
return self.form_invalid(form)
|
return self.form_invalid(form)
|
||||||
@@ -1762,14 +1811,21 @@ class DownloadDnsZoneManifest(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class RequestDnsPublication(OperatorOnlyMixin, PermissionRequiredMixin, View):
|
class RequestDnsPublication(PermissionRequiredMixin, View):
|
||||||
permission_required = PUBLISH_DNS
|
permission_required = PUBLISH_DNS
|
||||||
|
|
||||||
|
def has_permission(self):
|
||||||
|
if is_tenant(self.request.user):
|
||||||
|
return getattr(self.request, "impersonation", None) is None
|
||||||
|
return super().has_permission()
|
||||||
|
|
||||||
def post(self, request, pk):
|
def post(self, request, pk):
|
||||||
if not settings.LOCAL_DNS_PUBLISHING:
|
if not settings.LOCAL_DNS_PUBLISHING:
|
||||||
raise Http404
|
raise Http404
|
||||||
try:
|
try:
|
||||||
zone = queue_dns_publication(pk, request.POST.get("serial"))
|
zone = queue_dns_publication(
|
||||||
|
pk, request.POST.get("serial"), actor=request.user
|
||||||
|
)
|
||||||
except (
|
except (
|
||||||
HostingDnsZone.DoesNotExist,
|
HostingDnsZone.DoesNotExist,
|
||||||
HostingAccount.DoesNotExist,
|
HostingAccount.DoesNotExist,
|
||||||
|
|||||||
Reference in new issue
Block a user