66 lines
2.2 KiB
Python
66 lines
2.2 KiB
Python
from django.conf import settings
|
|
from django.core.exceptions import PermissionDenied
|
|
from django.db import transaction
|
|
from django.db.models import Q
|
|
|
|
from coulobre.dns_node import DNS_REQUEST_LIMIT
|
|
|
|
from .access import is_tenant, manages_account
|
|
from .domain_policies import require_managed
|
|
from .models import DnsPublication, HostingDnsZone
|
|
from .node_client import encode_manifest
|
|
from .resource_requests import lock_hosting_account, require_open_account
|
|
|
|
|
|
def queue_dns_publication(zone_id, expected_serial, *, actor=None):
|
|
account_id = HostingDnsZone.objects.values_list(
|
|
"account_id", flat=True
|
|
).get(pk=zone_id)
|
|
return publish_for_account(
|
|
account_id, zone_id, expected_serial, actor=actor
|
|
)
|
|
|
|
|
|
@transaction.atomic
|
|
def publish_for_account(account_id, zone_id, expected_serial, *, actor=None):
|
|
account = lock_hosting_account(account_id, customer_id=None)
|
|
zone = HostingDnsZone.objects.select_for_update().get(
|
|
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)
|
|
require_managed(account, zone.domain, "dns_managed")
|
|
if str(zone.serial) != expected_serial:
|
|
raise ValueError(
|
|
"This zone changed before publication. "
|
|
"Reload the account and try again."
|
|
)
|
|
queue_zone_snapshot(zone)
|
|
if settings.MANAGE_MAIL_DNS and zone.manage_mail_dns:
|
|
account.mail_domains.exclude(dkim_record="").filter(
|
|
Q(domain=zone.domain) | Q(domain__endswith=f".{zone.domain}")
|
|
).update(dns_sync_requested=True)
|
|
return zone
|
|
|
|
|
|
def queue_zone_snapshot(zone):
|
|
manifest = zone.spec.manifest()
|
|
if len(encode_manifest(manifest)) > DNS_REQUEST_LIMIT:
|
|
raise ValueError(
|
|
"Local DNS publishing accepts zone manifests up to 1 MiB."
|
|
)
|
|
|
|
jobs = DnsPublication.objects
|
|
snapshot = {"manifest": manifest, "serial": zone.serial}
|
|
job, created = jobs.get_or_create(zone=zone, defaults=snapshot)
|
|
if not created:
|
|
jobs.requeue(
|
|
jobs.filter(pk=job.pk).exclude(serial=zone.serial, problem=""),
|
|
**snapshot,
|
|
)
|