Serve website aliases over HTTP and HTTPS #38
No files matched your search
@@ -15,6 +15,7 @@ from .models import (
|
||||
HostingDnsZone,
|
||||
HostingMailbox,
|
||||
HostingMailDomain,
|
||||
WebsiteDomain,
|
||||
)
|
||||
|
||||
|
||||
@@ -154,6 +155,19 @@ def domain_conflicts(domain):
|
||||
conflicts.append(
|
||||
{"kind": kind, "domain": domain, "account_id": owner}
|
||||
)
|
||||
alias_owner = (
|
||||
WebsiteDomain.objects.filter(domain=domain, is_primary=False)
|
||||
.values_list("account_id", flat=True)
|
||||
.first()
|
||||
)
|
||||
if alias_owner is not None:
|
||||
conflicts.append(
|
||||
{
|
||||
"kind": "website_alias",
|
||||
"domain": domain,
|
||||
"account_id": alias_owner,
|
||||
}
|
||||
)
|
||||
return conflicts
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
# Generated by Django 5.2.19 on 2026-11-21 14:24
|
||||
|
||||
import django.db.models.deletion
|
||||
import django.utils.timezone
|
||||
from django.db import migrations, models
|
||||
|
||||
import coulobre.hosting.fields
|
||||
|
||||
|
||||
def reserve_existing_primary_domains(apps, schema_editor):
|
||||
account_model = apps.get_model("hosting", "HostingAccount")
|
||||
domain_model = apps.get_model("hosting", "WebsiteDomain")
|
||||
alias = schema_editor.connection.alias
|
||||
accounts = account_model.objects.using(alias).values_list(
|
||||
"pk", "primary_domain"
|
||||
)
|
||||
domain_model.objects.using(alias).bulk_create(
|
||||
(
|
||||
domain_model(account_id=account_id, domain=domain, is_primary=True)
|
||||
for account_id, domain in accounts.iterator()
|
||||
),
|
||||
batch_size=500,
|
||||
)
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
("hosting", "0028_backup_archive_retention"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name="WebsiteDomain",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.BigAutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
),
|
||||
),
|
||||
(
|
||||
"domain",
|
||||
coulobre.hosting.fields.DomainNameField(
|
||||
max_length=253, unique=True
|
||||
),
|
||||
),
|
||||
("is_primary", models.BooleanField(default=False)),
|
||||
(
|
||||
"account",
|
||||
models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="website_domains",
|
||||
to="hosting.hostingaccount",
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="WebsiteAlias",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.BigAutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
),
|
||||
),
|
||||
(
|
||||
"requested_at",
|
||||
models.DateTimeField(default=django.utils.timezone.now),
|
||||
),
|
||||
(
|
||||
"last_dispatched_at",
|
||||
models.DateTimeField(blank=True, null=True),
|
||||
),
|
||||
("started_at", models.DateTimeField(blank=True, null=True)),
|
||||
("finished_at", models.DateTimeField(blank=True, null=True)),
|
||||
("problem", models.TextField(blank=True)),
|
||||
(
|
||||
"primary_domain",
|
||||
coulobre.hosting.fields.DomainNameField(max_length=253),
|
||||
),
|
||||
(
|
||||
"operation",
|
||||
models.CharField(
|
||||
choices=[
|
||||
("activate", "Activate alias"),
|
||||
("https", "Enable HTTPS"),
|
||||
("remove", "Remove alias"),
|
||||
],
|
||||
default="activate",
|
||||
max_length=8,
|
||||
),
|
||||
),
|
||||
("http_active", models.BooleanField(default=False)),
|
||||
("https_active", models.BooleanField(default=False)),
|
||||
(
|
||||
"reservation",
|
||||
models.OneToOneField(
|
||||
on_delete=django.db.models.deletion.PROTECT,
|
||||
related_name="alias",
|
||||
to="hosting.websitedomain",
|
||||
),
|
||||
),
|
||||
],
|
||||
options={
|
||||
"permissions": [
|
||||
("manage_website_aliases", "Can manage website aliases")
|
||||
],
|
||||
"indexes": [
|
||||
models.Index(
|
||||
fields=["operation", "finished_at", "requested_at"],
|
||||
name="hosting_web_operati_d0d207_idx",
|
||||
)
|
||||
],
|
||||
},
|
||||
),
|
||||
migrations.RunPython(
|
||||
reserve_existing_primary_domains, migrations.RunPython.noop
|
||||
),
|
||||
]
|
||||
@@ -1,12 +1,13 @@
|
||||
import uuid
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.validators import (
|
||||
MaxValueValidator,
|
||||
MinValueValidator,
|
||||
RegexValidator,
|
||||
)
|
||||
from django.db import models
|
||||
from django.db import models, router, transaction
|
||||
from django.urls import reverse
|
||||
from django.utils import timezone
|
||||
|
||||
@@ -206,9 +207,82 @@ class HostingAccount(models.Model):
|
||||
def __str__(self):
|
||||
return str(self.primary_domain)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
update_fields = kwargs.get("update_fields")
|
||||
if update_fields is not None and "primary_domain" not in update_fields:
|
||||
return super().save(*args, **kwargs)
|
||||
|
||||
self.primary_domain = self._meta.get_field("primary_domain").to_python(
|
||||
self.primary_domain
|
||||
)
|
||||
using = kwargs.get("using") or router.db_for_write(
|
||||
type(self), instance=self
|
||||
)
|
||||
with transaction.atomic(using=using):
|
||||
super().save(*args, **kwargs)
|
||||
claims = WebsiteDomain.objects.using(using).filter(account=self)
|
||||
primary = claims.filter(is_primary=True).first()
|
||||
if primary is None:
|
||||
claims.create(
|
||||
domain=self.primary_domain, account=self, is_primary=True
|
||||
)
|
||||
elif primary.domain != self.primary_domain:
|
||||
if claims.filter(is_primary=False).exists():
|
||||
raise ValidationError(
|
||||
{
|
||||
"primary_domain": (
|
||||
"Remove website aliases before changing the "
|
||||
"primary domain."
|
||||
),
|
||||
}
|
||||
)
|
||||
primary.domain = self.primary_domain
|
||||
primary.save(update_fields=["domain"], using=using)
|
||||
|
||||
def get_absolute_url(self):
|
||||
return reverse("panel:account", kwargs={"pk": self.pk})
|
||||
|
||||
def clean(self):
|
||||
super().clean()
|
||||
if not self.primary_domain:
|
||||
return
|
||||
if WebsiteDomain.objects.filter(
|
||||
domain=self.primary_domain, is_primary=False
|
||||
).exists():
|
||||
raise ValidationError(
|
||||
{"primary_domain": "This domain is unavailable."}
|
||||
)
|
||||
|
||||
if self.pk:
|
||||
claims = self.website_domains
|
||||
if (
|
||||
claims.filter(is_primary=False).exists()
|
||||
and claims.filter(is_primary=True)
|
||||
.exclude(domain=self.primary_domain)
|
||||
.exists()
|
||||
):
|
||||
raise ValidationError(
|
||||
{
|
||||
"primary_domain": (
|
||||
"Remove website aliases before changing the "
|
||||
"primary domain."
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class WebsiteDomain(models.Model):
|
||||
account = models.ForeignKey(
|
||||
HostingAccount,
|
||||
on_delete=models.CASCADE,
|
||||
related_name="website_domains",
|
||||
)
|
||||
domain = DomainNameField(max_length=253, unique=True)
|
||||
is_primary = models.BooleanField(default=False)
|
||||
|
||||
def __str__(self):
|
||||
return self.domain
|
||||
|
||||
|
||||
class HostingDatabase(models.Model):
|
||||
account = models.ForeignKey(
|
||||
@@ -438,6 +512,31 @@ class WebHTTPS(ProvisioningJob):
|
||||
verbose_name = "website HTTPS request"
|
||||
|
||||
|
||||
class WebsiteAlias(ProvisioningJob):
|
||||
class Operation(models.TextChoices):
|
||||
ACTIVATE = "activate", "Activate alias"
|
||||
HTTPS = "https", "Enable HTTPS"
|
||||
REMOVE = "remove", "Remove alias"
|
||||
|
||||
reservation = models.OneToOneField(
|
||||
WebsiteDomain, on_delete=models.PROTECT, related_name="alias"
|
||||
)
|
||||
primary_domain = DomainNameField(max_length=253)
|
||||
operation = models.CharField(
|
||||
max_length=8, choices=Operation, default=Operation.ACTIVATE
|
||||
)
|
||||
http_active = models.BooleanField(default=False)
|
||||
https_active = models.BooleanField(default=False)
|
||||
|
||||
class Meta:
|
||||
indexes = [
|
||||
models.Index(fields=("operation", "finished_at", "requested_at"))
|
||||
]
|
||||
permissions = [
|
||||
("manage_website_aliases", "Can manage website aliases")
|
||||
]
|
||||
|
||||
|
||||
class SiteSFTP(ProvisioningJob):
|
||||
account = models.OneToOneField(
|
||||
HostingAccount, on_delete=models.CASCADE, related_name="sftp"
|
||||
|
||||
@@ -12,6 +12,7 @@ from coulobre.account_restore_request import (
|
||||
from coulobre.mail_node import MailboxQuotaUpdate
|
||||
from coulobre.web_node import (
|
||||
MAX_STORAGE_BYTES,
|
||||
WebAliasRequest,
|
||||
WebQuotaRequest,
|
||||
WebStorageRequest,
|
||||
)
|
||||
@@ -55,6 +56,15 @@ def enable_local_https(site):
|
||||
)
|
||||
|
||||
|
||||
def update_local_website_alias(alias, operation):
|
||||
apply_on_local_node(
|
||||
WebAliasRequest(alias, operation),
|
||||
"/run/coulobre-web-apply.sock",
|
||||
"web node",
|
||||
"website alias",
|
||||
)
|
||||
|
||||
|
||||
def update_local_sftp(keys):
|
||||
apply_on_local_node(
|
||||
keys,
|
||||
|
||||
@@ -3,6 +3,7 @@ from uuid import UUID
|
||||
|
||||
from celery import shared_task
|
||||
from django.conf import settings
|
||||
from django.db import transaction
|
||||
from django.db.models import Q
|
||||
from django.utils import timezone
|
||||
|
||||
@@ -19,7 +20,7 @@ from coulobre.account_restore_request import (
|
||||
)
|
||||
from coulobre.dns_node import DnsZone
|
||||
from coulobre.sftp_keys import SFTPKeys
|
||||
from coulobre.web_node import WebSite
|
||||
from coulobre.web_node import WebAlias, WebSite
|
||||
|
||||
from .account_backups import queue_due_account_backups
|
||||
from .backup_restores import (
|
||||
@@ -41,6 +42,7 @@ from .models import (
|
||||
WebActivation,
|
||||
WebHTTPS,
|
||||
WebProvisioningPlan,
|
||||
WebsiteAlias,
|
||||
WebsiteRestoreJob,
|
||||
)
|
||||
from .node_client import (
|
||||
@@ -57,6 +59,7 @@ from .node_client import (
|
||||
set_local_site_storage_limit,
|
||||
update_local_mailbox_quota,
|
||||
update_local_sftp,
|
||||
update_local_website_alias,
|
||||
)
|
||||
from .sftp import site_is_active
|
||||
|
||||
@@ -117,6 +120,15 @@ def dispatch_web_activations():
|
||||
dispatch_provisioning_jobs(
|
||||
WebActivation, activate_web_plan, WEB_ACTIVATION_QUEUE
|
||||
)
|
||||
dispatch_provisioning_jobs(
|
||||
WebsiteAlias,
|
||||
update_website_alias,
|
||||
WEB_ACTIVATION_QUEUE,
|
||||
operation__in=(
|
||||
WebsiteAlias.Operation.ACTIVATE,
|
||||
WebsiteAlias.Operation.REMOVE,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@shared_task
|
||||
@@ -139,6 +151,13 @@ def dispatch_site_storage_limits():
|
||||
def dispatch_web_https():
|
||||
if settings.LOCAL_WEB_HTTPS:
|
||||
dispatch_provisioning_jobs(WebHTTPS, enable_web_https, WEB_HTTPS_QUEUE)
|
||||
if settings.LOCAL_WEB_ACTIVATION:
|
||||
dispatch_provisioning_jobs(
|
||||
WebsiteAlias,
|
||||
enable_website_alias_https,
|
||||
WEB_HTTPS_QUEUE,
|
||||
operation=WebsiteAlias.Operation.HTTPS,
|
||||
)
|
||||
|
||||
|
||||
@shared_task
|
||||
@@ -261,6 +280,68 @@ def apply_web_activation(activation):
|
||||
activate_local_site(WebSite(plan.account_id, plan.primary_domain))
|
||||
|
||||
|
||||
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
||||
def update_website_alias(alias_id):
|
||||
if settings.LOCAL_WEB_ACTIVATION:
|
||||
jobs = WebsiteAlias.objects.select_related(
|
||||
"reservation__account"
|
||||
).exclude(operation=WebsiteAlias.Operation.HTTPS)
|
||||
run_provisioning_job(
|
||||
jobs,
|
||||
alias_id,
|
||||
apply_website_alias,
|
||||
"Could not contact the local web node. "
|
||||
"Check its socket and try again.",
|
||||
)
|
||||
|
||||
|
||||
def apply_website_alias(job):
|
||||
alias = queued_website_alias(job)
|
||||
if job.operation == WebsiteAlias.Operation.REMOVE:
|
||||
update_local_website_alias(alias, "remove_alias")
|
||||
with transaction.atomic():
|
||||
reservation = job.reservation
|
||||
job.delete()
|
||||
reservation.delete()
|
||||
return {}
|
||||
update_local_website_alias(alias, "apply_alias")
|
||||
return {"http_active": True}
|
||||
|
||||
|
||||
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
||||
def enable_website_alias_https(alias_id):
|
||||
if settings.LOCAL_WEB_ACTIVATION and settings.LOCAL_WEB_HTTPS:
|
||||
jobs = WebsiteAlias.objects.select_related(
|
||||
"reservation__account"
|
||||
).filter(operation=WebsiteAlias.Operation.HTTPS)
|
||||
run_provisioning_job(
|
||||
jobs,
|
||||
alias_id,
|
||||
apply_alias_https,
|
||||
"Could not contact the local HTTPS service. "
|
||||
"Check its socket and try again.",
|
||||
)
|
||||
|
||||
|
||||
def apply_alias_https(job):
|
||||
enable_local_https(queued_website_alias(job))
|
||||
return {"https_active": True}
|
||||
|
||||
|
||||
def queued_website_alias(job):
|
||||
account = job.reservation.account
|
||||
if (
|
||||
job.operation != WebsiteAlias.Operation.REMOVE
|
||||
and job.primary_domain != account.primary_domain
|
||||
):
|
||||
raise ValueError(
|
||||
"The primary domain changed before the alias request ran."
|
||||
)
|
||||
return WebAlias(
|
||||
WebSite(account.pk, job.primary_domain), job.reservation.domain
|
||||
)
|
||||
|
||||
|
||||
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
||||
def measure_site_storage(request_id):
|
||||
if settings.LOCAL_WEB_ACTIVATION:
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
from django import forms
|
||||
from django.core.exceptions import PermissionDenied, ValidationError
|
||||
from django.db import IntegrityError, transaction
|
||||
|
||||
from coulobre.web_node import WebAlias, WebSite
|
||||
|
||||
from .models import WebsiteAlias, WebsiteDomain
|
||||
from .resource_requests import lock_hosting_account
|
||||
from .sftp import site_is_active
|
||||
|
||||
|
||||
class WebsiteAliasForm(forms.Form):
|
||||
domain = forms.CharField(
|
||||
label="Alias hostname",
|
||||
max_length=253,
|
||||
help_text=(
|
||||
"Point this hostname's DNS to the same destination as the "
|
||||
"primary domain."
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def queue_website_alias(account_id, domain):
|
||||
account = lock_hosting_account(account_id, customer_id=None)
|
||||
if not site_is_active(account):
|
||||
raise ValidationError(
|
||||
"Activate the website for the primary domain first."
|
||||
)
|
||||
try:
|
||||
alias = WebAlias(WebSite(account.pk, account.primary_domain), domain)
|
||||
except ValueError as error:
|
||||
raise ValidationError({"domain": str(error)}) from error
|
||||
|
||||
try:
|
||||
with transaction.atomic():
|
||||
reservation = WebsiteDomain.objects.create(
|
||||
account=account, domain=alias.domain
|
||||
)
|
||||
except IntegrityError:
|
||||
if not WebsiteDomain.objects.filter(domain=alias.domain).exists():
|
||||
raise
|
||||
raise ValidationError(
|
||||
{"domain": "This domain is unavailable."}
|
||||
) from None
|
||||
WebsiteAlias.objects.create(
|
||||
reservation=reservation, primary_domain=account.primary_domain
|
||||
)
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def queue_alias_operation(account_id, alias_id, operation, *, allow_https):
|
||||
account = lock_hosting_account(account_id, customer_id=None)
|
||||
aliases = WebsiteAlias.objects.filter(reservation__account=account)
|
||||
alias = aliases.get(pk=alias_id)
|
||||
if alias.finished_at is None:
|
||||
raise ValidationError("Wait for the current alias request to finish.")
|
||||
|
||||
if operation == "retry":
|
||||
if not alias.problem:
|
||||
return
|
||||
operation = alias.operation
|
||||
if operation == WebsiteAlias.Operation.HTTPS:
|
||||
if not allow_https:
|
||||
raise PermissionDenied
|
||||
if alias.https_active:
|
||||
return
|
||||
if not alias.http_active:
|
||||
raise ValidationError("Activate the alias before enabling HTTPS.")
|
||||
WebsiteAlias.objects.requeue(
|
||||
aliases.filter(pk=alias.pk), operation=operation
|
||||
)
|
||||
Reference in new issue
Block a user