Add website HTTPS requests to Coulobre Panel
This commit is contained in:
1 parent
43d03d207e
commit
4d6aa0c281
14 files changed
+368
-3
No files matched your search
@@ -0,0 +1,50 @@
|
||||
# Generated by Django 5.2.19 on 2026-11-21 01:46
|
||||
|
||||
import django.db.models.deletion
|
||||
import django.utils.timezone
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
("hosting", "0017_customer_impersonation"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name="WebHTTPS",
|
||||
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)),
|
||||
(
|
||||
"plan",
|
||||
models.OneToOneField(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="https",
|
||||
to="hosting.webprovisioningplan",
|
||||
),
|
||||
),
|
||||
],
|
||||
options={
|
||||
"verbose_name": "website HTTPS request",
|
||||
},
|
||||
),
|
||||
]
|
||||
@@ -290,6 +290,15 @@ class WebProvisioningPlan(models.Model):
|
||||
and self.primary_domain == self.account.primary_domain
|
||||
)
|
||||
|
||||
@property
|
||||
def can_enable_https(self):
|
||||
activation = getattr(self, "activation", None)
|
||||
return (
|
||||
self.can_export
|
||||
and activation is not None
|
||||
and activation.state == "configured"
|
||||
)
|
||||
|
||||
|
||||
class ProvisioningJobManager(models.Manager):
|
||||
def queue(self, **target):
|
||||
@@ -334,6 +343,15 @@ class WebActivation(ProvisioningJob):
|
||||
)
|
||||
|
||||
|
||||
class WebHTTPS(ProvisioningJob):
|
||||
plan = models.OneToOneField(
|
||||
WebProvisioningPlan, on_delete=models.CASCADE, related_name="https"
|
||||
)
|
||||
|
||||
class Meta:
|
||||
verbose_name = "website HTTPS request"
|
||||
|
||||
|
||||
class DatabaseProvisioning(ProvisioningJob):
|
||||
database = models.OneToOneField(
|
||||
HostingDatabase, on_delete=models.CASCADE, related_name="provisioning"
|
||||
|
||||
@@ -10,6 +10,12 @@ def activate_local_site(site):
|
||||
)
|
||||
|
||||
|
||||
def enable_local_https(site):
|
||||
apply_on_local_node(
|
||||
site, "/run/coulobre-https-apply.sock", "web node", "HTTPS request"
|
||||
)
|
||||
|
||||
|
||||
def provision_local_database(database):
|
||||
apply_on_local_node(
|
||||
database,
|
||||
|
||||
@@ -13,10 +13,12 @@ from .models import (
|
||||
DnsPublication,
|
||||
MailboxProvisioning,
|
||||
WebActivation,
|
||||
WebHTTPS,
|
||||
WebProvisioningPlan,
|
||||
)
|
||||
from .node_client import (
|
||||
activate_local_site,
|
||||
enable_local_https,
|
||||
provision_local_database,
|
||||
provision_local_mailbox,
|
||||
publish_local_zone,
|
||||
@@ -24,6 +26,7 @@ from .node_client import (
|
||||
|
||||
REDISPATCH_AFTER = timedelta(minutes=5)
|
||||
WEB_ACTIVATION_QUEUE = "coulobre-web-activation"
|
||||
WEB_HTTPS_QUEUE = "coulobre-web-https"
|
||||
DATABASE_PROVISIONING_QUEUE = "coulobre-database-provisioning"
|
||||
DNS_PUBLISHING_QUEUE = "coulobre-dns-publishing"
|
||||
MAILBOX_PROVISIONING_QUEUE = "coulobre-mailbox-provisioning"
|
||||
@@ -72,6 +75,12 @@ def dispatch_web_activations():
|
||||
)
|
||||
|
||||
|
||||
@shared_task
|
||||
def dispatch_web_https():
|
||||
if settings.LOCAL_WEB_HTTPS:
|
||||
dispatch_provisioning_jobs(WebHTTPS, enable_web_https, WEB_HTTPS_QUEUE)
|
||||
|
||||
|
||||
@shared_task
|
||||
def dispatch_database_provisioning():
|
||||
if settings.LOCAL_DATABASE_PROVISIONING:
|
||||
@@ -139,6 +148,27 @@ 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 enable_web_https(request_id):
|
||||
if settings.LOCAL_WEB_HTTPS:
|
||||
run_provisioning_job(
|
||||
WebHTTPS.objects.select_related(
|
||||
"plan__account", "plan__activation"
|
||||
),
|
||||
request_id,
|
||||
apply_web_https,
|
||||
"Could not contact the local HTTPS service. "
|
||||
"Check its socket and try again.",
|
||||
)
|
||||
|
||||
|
||||
def apply_web_https(provisioning):
|
||||
plan = provisioning.plan
|
||||
if not plan.can_enable_https:
|
||||
raise ValueError("Activate the site for the current domain first.")
|
||||
enable_local_https(WebSite(plan.account_id, plan.primary_domain))
|
||||
|
||||
|
||||
@shared_task(acks_late=True, reject_on_worker_lost=True)
|
||||
def provision_database(provisioning_id):
|
||||
if settings.LOCAL_DATABASE_PROVISIONING:
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<h1>Apache preview</h1>
|
||||
<p class="heading-meta domain">{{ plan.primary_domain }}</p>
|
||||
</div>
|
||||
{% if not plan.completed_at or manage_activation and plan.activation and not plan.activation.finished_at %}
|
||||
{% if not plan.completed_at or manage_activation and plan.activation and not plan.activation.finished_at or manage_https and plan.https and not plan.https.finished_at %}
|
||||
<a class="button secondary" href="{% url 'panel:plan' plan.pk %}">Refresh status</a>
|
||||
{% endif %}
|
||||
</header>
|
||||
@@ -66,4 +66,20 @@
|
||||
{% endif %}
|
||||
{% endwith %}
|
||||
{% endif %}
|
||||
{% if manage_https %}
|
||||
{% with provisioning=plan.https %}
|
||||
{% if provisioning or local_https_enabled and plan.can_enable_https %}
|
||||
<section class="surface" aria-labelledby="https-heading">
|
||||
<header class="section-heading">
|
||||
<h2 id="https-heading">Website HTTPS</h2>
|
||||
{% if local_https_enabled and plan.can_enable_https %}
|
||||
{% url 'panel:plan_https' plan.pk as https_action %}
|
||||
{% endif %}
|
||||
{% include "hosting/resource_provisioning.html" with enabled=https_action action=https_action resource=plan.primary_domain provision_label="Enable HTTPS" retry_label="Retry HTTPS" %}
|
||||
</header>
|
||||
{% if provisioning.problem %}<p class="empty">{{ provisioning.problem }}</p>{% endif %}
|
||||
</section>
|
||||
{% endif %}
|
||||
{% endwith %}
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
@@ -12,7 +12,7 @@
|
||||
<form method="post" action="{{ action }}">
|
||||
{% csrf_token %}
|
||||
{% if provisioning %}
|
||||
<button class="button secondary" type="submit" aria-label="Retry provisioning {{ resource }}">Retry provisioning</button>
|
||||
<button class="button secondary" type="submit" aria-label="{{ retry_label|default:'Retry provisioning' }} {{ resource }}">{{ retry_label|default:"Retry provisioning" }}</button>
|
||||
{% else %}
|
||||
<button class="button secondary" type="submit" aria-label="{{ provision_label }} {{ resource }}">{{ provision_label }}</button>
|
||||
{% endif %}
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.models import Permission
|
||||
from django.test import TestCase, override_settings
|
||||
from django.urls import reverse
|
||||
from django.utils import timezone
|
||||
|
||||
from coulobre.web_node import WebSite
|
||||
|
||||
from .models import (
|
||||
HostingAccount,
|
||||
WebActivation,
|
||||
WebHTTPS,
|
||||
WebProvisioningPlan,
|
||||
)
|
||||
from .tasks import WEB_HTTPS_QUEUE, dispatch_web_https, enable_web_https
|
||||
|
||||
|
||||
@override_settings(LOCAL_WEB_HTTPS=True)
|
||||
class WebHTTPSTests(TestCase):
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
cls.operator = get_user_model().objects.create_superuser("operator")
|
||||
cls.account = HostingAccount.objects.create(
|
||||
name="Example site", primary_domain="site.example"
|
||||
)
|
||||
cls.plan = WebProvisioningPlan.objects.create(
|
||||
account=cls.account,
|
||||
primary_domain=cls.account.primary_domain,
|
||||
apache_config="Rendered Apache configuration",
|
||||
completed_at=timezone.now(),
|
||||
)
|
||||
|
||||
def setUp(self):
|
||||
self.client.force_login(self.operator)
|
||||
self.url = reverse("panel:plan_https", args=[self.plan.pk])
|
||||
self.detail = reverse("panel:plan", args=[self.plan.pk])
|
||||
|
||||
def activate_site(self):
|
||||
WebActivation.objects.create(
|
||||
plan=self.plan, finished_at=timezone.now()
|
||||
)
|
||||
|
||||
def test_requires_an_active_site_for_the_current_domain(self):
|
||||
self.assertEqual(self.client.post(self.url).status_code, 409)
|
||||
self.assertNotContains(self.client.get(self.detail), "Enable HTTPS")
|
||||
self.activate_site()
|
||||
self.account.primary_domain = "new.example"
|
||||
self.account.save(update_fields=["primary_domain"])
|
||||
self.assertEqual(self.client.post(self.url).status_code, 409)
|
||||
self.assertFalse(WebHTTPS.objects.exists())
|
||||
|
||||
def test_operator_can_queue_once_and_track_https(self):
|
||||
self.activate_site()
|
||||
self.assertContains(self.client.get(self.detail), "Enable HTTPS")
|
||||
self.assertEqual(self.client.get(self.url).status_code, 405)
|
||||
self.assertRedirects(self.client.post(self.url), self.detail)
|
||||
self.client.post(self.url)
|
||||
request = WebHTTPS.objects.get(plan=self.plan)
|
||||
self.assertContains(self.client.get(self.detail), "Refresh status")
|
||||
|
||||
with patch(
|
||||
"coulobre.hosting.tasks.enable_web_https.apply_async"
|
||||
) as send:
|
||||
dispatch_web_https()
|
||||
send.assert_called_once_with(
|
||||
args=[request.pk], queue=WEB_HTTPS_QUEUE, retry=False
|
||||
)
|
||||
|
||||
with patch("coulobre.hosting.tasks.enable_local_https") as enable:
|
||||
enable_web_https(request.pk)
|
||||
enable_web_https(request.pk)
|
||||
enable.assert_called_once_with(
|
||||
WebSite(self.account.pk, self.account.primary_domain)
|
||||
)
|
||||
request.refresh_from_db()
|
||||
self.assertEqual(request.state, "configured")
|
||||
self.assertNotContains(self.client.get(self.detail), "Enable HTTPS")
|
||||
|
||||
def test_failed_attempt_can_be_retried(self):
|
||||
self.activate_site()
|
||||
request = WebHTTPS.objects.create(plan=self.plan)
|
||||
with patch(
|
||||
"coulobre.hosting.tasks.enable_local_https",
|
||||
side_effect=ValueError("Check the HTTP-01 route."),
|
||||
):
|
||||
enable_web_https(request.pk)
|
||||
response = self.client.get(self.detail)
|
||||
self.assertContains(response, "Check the HTTP-01 route.")
|
||||
self.assertContains(response, "Retry HTTPS")
|
||||
|
||||
self.assertRedirects(self.client.post(self.url), self.detail)
|
||||
request.refresh_from_db()
|
||||
self.assertEqual(request.state, "queued")
|
||||
self.assertIsNone(request.started_at)
|
||||
self.assertEqual(request.problem, "")
|
||||
|
||||
def test_worker_rechecks_the_domain_before_requesting_a_certificate(self):
|
||||
self.activate_site()
|
||||
request = WebHTTPS.objects.create(plan=self.plan)
|
||||
self.account.primary_domain = "new.example"
|
||||
self.account.save(update_fields=["primary_domain"])
|
||||
|
||||
with patch("coulobre.hosting.tasks.enable_local_https") as enable:
|
||||
enable_web_https(request.pk)
|
||||
enable.assert_not_called()
|
||||
request.refresh_from_db()
|
||||
self.assertEqual(request.state, "failed")
|
||||
|
||||
def test_activation_permission_does_not_grant_https_access(self):
|
||||
self.activate_site()
|
||||
reader = get_user_model().objects.create_user("reader", is_staff=True)
|
||||
reader.user_permissions.add(
|
||||
*Permission.objects.filter(
|
||||
content_type__app_label="hosting",
|
||||
codename__in=(
|
||||
"view_hostingaccount",
|
||||
"view_webprovisioningplan",
|
||||
"add_webactivation",
|
||||
),
|
||||
)
|
||||
)
|
||||
self.client.force_login(reader)
|
||||
self.assertNotContains(self.client.get(self.detail), "Enable HTTPS")
|
||||
self.assertEqual(self.client.post(self.url).status_code, 403)
|
||||
|
||||
@override_settings(LOCAL_WEB_HTTPS=False)
|
||||
def test_feature_flag_disables_requests(self):
|
||||
self.activate_site()
|
||||
self.assertNotContains(self.client.get(self.detail), "Enable HTTPS")
|
||||
self.assertEqual(self.client.post(self.url).status_code, 404)
|
||||
@@ -121,6 +121,11 @@ urlpatterns = [
|
||||
views.RequestWebActivation.as_view(),
|
||||
name="plan_activate",
|
||||
),
|
||||
path(
|
||||
"plans/<int:pk>/https/",
|
||||
views.RequestWebHTTPS.as_view(),
|
||||
name="plan_https",
|
||||
),
|
||||
path(
|
||||
"plans/<int:pk>/smoke-bundle/",
|
||||
views.DownloadWebPlan.as_view(),
|
||||
|
||||
@@ -55,6 +55,7 @@ from .models import (
|
||||
MailboxProvisioning,
|
||||
Reseller,
|
||||
WebActivation,
|
||||
WebHTTPS,
|
||||
WebProvisioningPlan,
|
||||
)
|
||||
from .reseller_accounts import create_reseller_account
|
||||
@@ -71,6 +72,7 @@ VIEW_DATABASES = "hosting.view_hostingdatabase"
|
||||
VIEW_DNS_ZONES = "hosting.view_hostingdnszone"
|
||||
VIEW_MAILBOXES = "hosting.view_hostingmailbox"
|
||||
ACTIVATE_WEB = (VIEW_ACCOUNTS, VIEW_PLANS, "hosting.add_webactivation")
|
||||
ENABLE_WEB_HTTPS = (VIEW_ACCOUNTS, VIEW_PLANS, "hosting.add_webhttps")
|
||||
PROVISION_DATABASE = (
|
||||
VIEW_ACCOUNTS,
|
||||
VIEW_DATABASES,
|
||||
@@ -717,7 +719,7 @@ class PlanList(TenantScopeMixin, PermissionRequiredMixin, ListView):
|
||||
|
||||
class PlanDetail(TenantScopeMixin, PermissionRequiredMixin, DetailView):
|
||||
queryset = WebProvisioningPlan.objects.select_related(
|
||||
"account", "activation"
|
||||
"account", "activation", "https"
|
||||
)
|
||||
permission_required = VIEW_PLANS
|
||||
reseller_relation = "account__reseller"
|
||||
@@ -730,6 +732,9 @@ class PlanDetail(TenantScopeMixin, PermissionRequiredMixin, DetailView):
|
||||
manage_activation=not is_tenant(user)
|
||||
and user.has_perms(ACTIVATE_WEB),
|
||||
local_activation_enabled=settings.LOCAL_WEB_ACTIVATION,
|
||||
manage_https=not is_tenant(user)
|
||||
and user.has_perms(ENABLE_WEB_HTTPS),
|
||||
local_https_enabled=settings.LOCAL_WEB_HTTPS,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
@@ -755,6 +760,28 @@ class RequestWebActivation(
|
||||
return redirect("panel:plan", pk=plan.pk)
|
||||
|
||||
|
||||
class RequestWebHTTPS(
|
||||
OperatorOnlyMixin, PermissionRequiredMixin, SingleObjectMixin, View
|
||||
):
|
||||
queryset = WebProvisioningPlan.objects.select_related(
|
||||
"account", "activation"
|
||||
)
|
||||
permission_required = ENABLE_WEB_HTTPS
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
if not settings.LOCAL_WEB_HTTPS:
|
||||
raise Http404
|
||||
plan = self.get_object()
|
||||
if not plan.can_enable_https:
|
||||
return HttpResponse(
|
||||
"Activate the site for the current domain first.",
|
||||
status=409,
|
||||
content_type="text/plain",
|
||||
)
|
||||
WebHTTPS.objects.queue(plan=plan)
|
||||
return redirect("panel:plan", pk=plan.pk)
|
||||
|
||||
|
||||
class DownloadWebPlan(OperatorOnlyMixin, PlanDetail):
|
||||
manifest = False
|
||||
|
||||
|
||||
@@ -119,6 +119,7 @@ CELERY_BROKER_TRANSPORT_OPTIONS = {"confirm_publish": True}
|
||||
CELERY_TASK_DEFAULT_QUEUE = "coulobre"
|
||||
CELERY_TASK_IGNORE_RESULT = True
|
||||
LOCAL_WEB_ACTIVATION = os.environ.get("COULOBRE_LOCAL_WEB_ACTIVATION") == "1"
|
||||
LOCAL_WEB_HTTPS = os.environ.get("COULOBRE_LOCAL_WEB_HTTPS") == "1"
|
||||
LOCAL_DATABASE_PROVISIONING = (
|
||||
os.environ.get("COULOBRE_LOCAL_DATABASE_PROVISIONING") == "1"
|
||||
)
|
||||
@@ -139,6 +140,12 @@ if LOCAL_WEB_ACTIVATION:
|
||||
"schedule": 5.0,
|
||||
"options": {"expires": 4.0},
|
||||
}
|
||||
if LOCAL_WEB_HTTPS:
|
||||
CELERY_BEAT_SCHEDULE["dispatch-web-https"] = {
|
||||
"task": "coulobre.hosting.tasks.dispatch_web_https",
|
||||
"schedule": 5.0,
|
||||
"options": {"expires": 4.0},
|
||||
}
|
||||
if LOCAL_DATABASE_PROVISIONING:
|
||||
CELERY_BEAT_SCHEDULE["dispatch-database-provisioning"] = {
|
||||
"task": "coulobre.hosting.tasks.dispatch_database_provisioning",
|
||||
|
||||
Reference in new issue
Block a user