Manage DNS zones and export BIND9 manifests from hosting accounts

This commit is contained in:
parkel committed 2026-11-19 23:11:39 +01:00
1 parent 74b7dcedbe
commit 4fcbf723de
11 files changed
+595 -9

No files matched your search

+130 -2
View File
@@ -7,11 +7,20 @@ from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse_lazy
from django.utils.functional import cached_property
from django.views import View
from django.views.generic import CreateView, DetailView, ListView, UpdateView
from django.views.generic import CreateView, DetailView, FormView, ListView, UpdateView
from django.views.generic.detail import SingleObjectMixin
from coulobre.dns_node import MAX_SOA_SERIAL
from .access import OperatorOnlyMixin, ResellerScopeMixin, reseller_for_user
from .models import HostingAccount, HostingDatabase, HostingPlan, WebProvisioningPlan
from .dns_forms import DnsZoneEditForm, DnsZoneForm
from .models import (
HostingAccount,
HostingDatabase,
HostingDnsZone,
HostingPlan,
WebProvisioningPlan,
)
from .reseller_accounts import create_reseller_account
from .web_node import build_web_smoke_bundle, serialize_deployment_manifest
@@ -21,6 +30,7 @@ VIEW_PLANS = "hosting.view_webprovisioningplan"
VIEW_HOSTING_PLANS = "hosting.view_hostingplan"
VIEW_RESELLERS = "hosting.view_reseller"
VIEW_DATABASES = "hosting.view_hostingdatabase"
VIEW_DNS_ZONES = "hosting.view_hostingdnszone"
class AccountList(ResellerScopeMixin, PermissionRequiredMixin, ListView):
@@ -150,6 +160,12 @@ class AccountDetail(ResellerScopeMixin, PermissionRequiredMixin, DetailView):
)[:10]
if reseller is not None or user.has_perm(VIEW_DATABASES):
context["databases"] = self.object.databases.all()
if reseller is not None or user.has_perm(VIEW_DNS_ZONES):
zones = list(self.object.dns_zones.all())
context["dns_zones"] = zones
context["has_primary_zone"] = any(
zone.domain == self.object.primary_domain for zone in zones
)
return context
@@ -196,6 +212,118 @@ class DownloadDatabaseManifest(OperatorOnlyMixin, PermissionRequiredMixin, View)
)
class DnsZoneEditor(OperatorOnlyMixin, PermissionRequiredMixin, FormView):
template_name = "hosting/dns_zone_form.html"
def get_form_kwargs(self):
return {**super().get_form_kwargs(), "domain": self.domain}
def get_context_data(self, **kwargs):
return super().get_context_data(
account=self.account, domain=self.domain, **kwargs
)
def get_success_url(self):
return self.account.get_absolute_url()
class DnsZoneCreate(DnsZoneEditor):
form_class = DnsZoneForm
permission_required = (VIEW_ACCOUNTS, VIEW_DNS_ZONES, "hosting.add_hostingdnszone")
extra_context = {"page_title": "Add DNS zone"}
@cached_property
def account(self):
return get_object_or_404(HostingAccount, pk=self.kwargs["pk"])
@property
def domain(self):
return self.account.primary_domain
def get_initial(self):
return {"responsible_name": f"hostmaster.{self.domain}"}
def form_valid(self, form):
try:
with transaction.atomic():
HostingDnsZone.objects.create(
account=self.account, domain=self.domain, **form.cleaned_data
)
except IntegrityError:
if not HostingDnsZone.objects.filter(domain=self.domain).exists():
raise
form.add_error(None, "A DNS zone already exists for this domain.")
return self.form_invalid(form)
return super().form_valid(form)
class DnsZoneEdit(DnsZoneEditor):
form_class = DnsZoneEditForm
permission_required = (
VIEW_ACCOUNTS,
VIEW_DNS_ZONES,
"hosting.change_hostingdnszone",
)
extra_context = {"page_title": "Edit DNS zone"}
@cached_property
def zone(self):
return get_object_or_404(
HostingDnsZone.objects.select_related("account"), pk=self.kwargs["pk"]
)
@property
def account(self):
return self.zone.account
@property
def domain(self):
return self.zone.domain
def get_initial(self):
return {
"serial": self.zone.serial,
"nameservers": "\n".join(self.zone.nameservers),
"responsible_name": self.zone.responsible_name,
"addresses": "\n".join(
f"{record['name']} {record['address']}"
for record in self.zone.addresses
),
}
def form_valid(self, form):
values = form.cleaned_data
serial = values["serial"]
if all(getattr(self.zone, field) == value for field, value in values.items()):
return super().form_valid(form)
next_serial = (serial + 1) % (MAX_SOA_SERIAL + 1)
updated = HostingDnsZone.objects.filter(pk=self.zone.pk, serial=serial).update(
**(values | {"serial": next_serial})
)
if not updated:
form.add_error(
None,
"This DNS zone changed while you were editing it. Reopen the form and try again.",
)
return self.form_invalid(form)
return super().form_valid(form)
class DownloadDnsZoneManifest(OperatorOnlyMixin, PermissionRequiredMixin, View):
permission_required = (VIEW_ACCOUNTS, VIEW_DNS_ZONES)
def get(self, request, pk):
zone = get_object_or_404(HostingDnsZone, pk=pk)
return JsonResponse(
zone.spec.manifest(),
headers={
"Cache-Control": "private, no-store",
"Content-Disposition": f'attachment; filename="coulobre-zone-{zone.pk}.json"',
},
)
class HostingPlanList(OperatorOnlyMixin, PermissionRequiredMixin, ListView):
model = HostingPlan
permission_required = VIEW_HOSTING_PLANS