From bb826877d219b658b472a46113002a5b09836a9f Mon Sep 17 00:00:00 2001
From: Peter van Arkel
Date: Tue, 1 Dec 2026 00:50:45 +0100
Subject: [PATCH] Fix customer mail, DNS and usage pages
---
src/coulobre/hosting/domain_policies.py | 24 +++-
src/coulobre/hosting/domain_services.py | 42 +++++--
src/coulobre/hosting/service_credentials.py | 8 ++
.../static/hosting/dns-record-editor.js | 2 +
src/coulobre/hosting/static/hosting/panel.css | 48 ++++++--
.../templates/hosting/dns_record_editor.html | 6 +-
.../templates/hosting/dns_record_table.html | 3 +-
.../templates/hosting/domain_workspace.html | 27 +++--
.../templates/hosting/website_usage.html | 64 +++++++----
.../hosting/test_public_operations.py | 35 +++++-
src/coulobre/hosting/test_workspaces.py | 104 ++++++++++++++++++
src/coulobre/hosting/views.py | 16 +--
src/coulobre/hosting/website_usage_views.py | 28 ++++-
13 files changed, 348 insertions(+), 59 deletions(-)
diff --git a/src/coulobre/hosting/domain_policies.py b/src/coulobre/hosting/domain_policies.py
index 5a936cb..7d87953 100644
--- a/src/coulobre/hosting/domain_policies.py
+++ b/src/coulobre/hosting/domain_policies.py
@@ -7,7 +7,29 @@ def policy_for(account, domain):
policy = DomainServicePolicy.objects.filter(
account=account, domain=domain
).first()
- return policy or DomainServicePolicy(account=account, domain=domain)
+ if policy is not None:
+ return policy
+ return DomainServicePolicy(
+ account=account,
+ domain=domain,
+ mail_managed=(
+ domain == account.primary_domain
+ or account.mail_domains.filter(domain=domain).exists()
+ ),
+ )
+
+
+def managed_mail_domains(account):
+ domains = {account.primary_domain}
+ domains.update(account.mail_domains.values_list("domain", flat=True))
+ for domain, enabled in account.domainservicepolicy_set.values_list(
+ "domain", "mail_managed"
+ ):
+ if enabled:
+ domains.add(domain)
+ else:
+ domains.discard(domain)
+ return sorted(domains)
def require_managed(account, domain, field):
diff --git a/src/coulobre/hosting/domain_services.py b/src/coulobre/hosting/domain_services.py
index 3104c1a..fcb6ada 100644
--- a/src/coulobre/hosting/domain_services.py
+++ b/src/coulobre/hosting/domain_services.py
@@ -1,4 +1,5 @@
from django import forms
+from django.conf import settings
from django.contrib import messages
from django.core.exceptions import PermissionDenied, ValidationError
from django.db import transaction
@@ -14,15 +15,17 @@ from .access import customer_for_user
from .account_setup import website_records
from .dns_defaults import zone_defaults
from .dns_forms import zone_record_rows
-from .domain_policies import policy_for
+from .domain_policies import managed_mail_domains, policy_for
from .independent_websites import account_websites
from .installation_features import panel_feature_allowed
from .models import DomainServicePolicy, HostingAccount
from .resource_requests import lock_hosting_account, require_open_account
+from .service_credentials import CredentialKind, show_credential_action
from .sftp import site_is_active
DNS_PUBLISHING = "LOCAL_DNS_PUBLISHING"
MAIL_PROVISIONING = "LOCAL_MAILBOX_PROVISIONING"
+MAIL_PAGES = {"email", "forwarders", "replies", "spam"}
class DomainServicesForm(forms.Form):
@@ -111,14 +114,30 @@ def domain_workspace(request, pk, page):
account = get_object_or_404(
HostingAccount, pk=pk, customer=customer, retired_at__isnull=True
)
- domains = allowed_domains(account)
+ if page == "dns":
+ domains = list(
+ account.dns_zones.order_by("domain").values_list(
+ "domain", flat=True
+ )
+ )
+ elif page in MAIL_PAGES:
+ domains = sorted(
+ set(managed_mail_domains(account)) & set(allowed_domains(account))
+ )
+ else:
+ domains = allowed_domains(account)
+
+ default = account.primary_domain
+ if domains and default not in domains:
+ default = domains[0]
+ selection = request.GET.get("domain")
try:
- domain = canonical_domain(
- request.GET.get("domain", account.primary_domain)
+ domain = (
+ canonical_domain(selection) if selection is not None else default
)
except ValueError as error:
raise Http404 from error
- if domain not in domains:
+ if selection is not None and domain not in domains:
raise Http404
policy = policy_for(account, domain)
initial = {
@@ -167,7 +186,7 @@ def domain_workspace(request, pk, page):
)
mail_domain = (
account.mail_domains.filter(domain=domain).first()
- if page in {"dns", "email", "forwarders", "replies", "spam"}
+ if page == "dns" or page in MAIL_PAGES
else None
)
titles = {
@@ -184,6 +203,9 @@ def domain_workspace(request, pk, page):
from .roundcube import roundcube_available
defaults = zone_defaults() if page == "dns" else None
+ show_mailbox_credentials = show_credential_action(
+ request, CredentialKind.MAILBOX
+ )
return render(
request,
"hosting/domain_workspace.html",
@@ -205,14 +227,18 @@ def domain_workspace(request, pk, page):
)
if page == "dns" and not policy.dns_managed
else (),
- "zone_defaults": defaults,
"mail_domain": mail_domain,
"mail_enabled": panel_feature_allowed(
"LOCAL_MAILBOX_PROVISIONING"
),
"can_edit_routing": mail_policy_available("routing"),
"can_edit_mailbox": mail_policy_available("vacation"),
- "roundcube_enabled": roundcube_available(request),
+ "show_mailbox_credentials": show_mailbox_credentials,
+ "roundcube_enabled": (
+ show_mailbox_credentials and roundcube_available(request)
+ ),
+ "mail_imap_host": settings.MAIL_IMAP_HOST,
+ "mail_submission_host": settings.MAIL_SUBMISSION_HOST,
"mailboxes": mail_domain.mailboxes.select_related("provisioning")
if mail_domain
else (),
diff --git a/src/coulobre/hosting/service_credentials.py b/src/coulobre/hosting/service_credentials.py
index e8bb723..9a1db24 100644
--- a/src/coulobre/hosting/service_credentials.py
+++ b/src/coulobre/hosting/service_credentials.py
@@ -103,6 +103,14 @@ def can_rotate_credentials(user, kind):
)
+def show_credential_action(request, kind):
+ return (
+ getattr(request, "impersonation", None) is None
+ and enabled_credential_access(request, kind)
+ and can_reveal_credentials(request.user, kind)
+ )
+
+
@transaction.atomic
@sensitive_variables()
def verify_credential_access(
diff --git a/src/coulobre/hosting/static/hosting/dns-record-editor.js b/src/coulobre/hosting/static/hosting/dns-record-editor.js
index 5a59453..67c137d 100644
--- a/src/coulobre/hosting/static/hosting/dns-record-editor.js
+++ b/src/coulobre/hosting/static/hosting/dns-record-editor.js
@@ -39,6 +39,8 @@ function appendRecord(record) {
priority.required = usesPriority;
priority.disabled = !usesPriority;
value.required = type.value !== "TXT";
+ value.rows = type.value === "TXT" ? 4 : 1;
+ value.classList.toggle("dns-txt-value", type.value === "TXT");
valueLabel.textContent = valueLabels[type.value];
};
type.addEventListener("change", updateFields);
diff --git a/src/coulobre/hosting/static/hosting/panel.css b/src/coulobre/hosting/static/hosting/panel.css
index e27f72a..4738634 100644
--- a/src/coulobre/hosting/static/hosting/panel.css
+++ b/src/coulobre/hosting/static/hosting/panel.css
@@ -1043,9 +1043,9 @@ dd {
}
.dns-edit-row {
- display: flex;
- flex-wrap: wrap;
- align-items: end;
+ display: grid;
+ grid-template-columns: minmax(0, 30%) 5rem minmax(0, 1fr) auto;
+ align-items: start;
gap: 0.5rem;
min-width: 0;
margin: 0;
@@ -1054,12 +1054,44 @@ dd {
border-radius: 0.5rem;
}
-.dns-edit-row label { flex: 1 1 8rem; margin: 0; }
+.dns-edit-row label { min-width: 0; margin: 0; }
.dns-edit-row label[hidden] { display: none; }
-.dns-edit-row label:has(select, input[type="number"]) { flex: 0 1 6rem; }
-.dns-edit-row label:has(textarea) { flex: 2 1 12rem; }
-.dns-edit-row textarea { display: block; min-height: 2.45rem; resize: vertical; }
-.dns-record-table code { white-space: pre-wrap; overflow-wrap: anywhere; }
+.dns-edit-row > button { margin-top: 1.4em; }
+.dns-edit-value {
+ display: flex;
+ gap: 0.5rem;
+}
+.dns-edit-value > label { flex: 1; }
+.dns-edit-value > [data-priority-label] { flex: 0 0 5rem; }
+.dns-edit-row textarea {
+ display: block;
+ min-height: 2.45rem;
+ overflow-wrap: anywhere;
+ resize: vertical;
+}
+.dns-edit-row .dns-txt-value { field-sizing: content; min-height: 5rem; }
+
+.dns-record-table { table-layout: fixed; }
+.dns-record-table .dns-record-name { width: 30%; }
+.dns-record-table .dns-record-type { width: 5rem; }
+.dns-record-table th, .dns-record-table code { overflow-wrap: anywhere; }
+.dns-record-table code { white-space: pre-wrap; }
+
+.usage-table code, .usage-table th, .usage-error-log {
+ overflow-wrap: anywhere;
+}
+
+.usage-error-log {
+ padding: 0 1.2rem 1rem;
+ white-space: pre-wrap;
+}
+
+@media (max-width: 720px) {
+ .dns-edit-row { grid-template-columns: minmax(0, 1fr) 5rem; }
+ .dns-edit-value { grid-column: 1 / -1; }
+ .dns-edit-row > button { margin-top: 0; justify-self: start; }
+ .dns-record-table .dns-record-type { width: 3.5rem; }
+}
.reseller-editor fieldset {
min-width: 0;
diff --git a/src/coulobre/hosting/templates/hosting/dns_record_editor.html b/src/coulobre/hosting/templates/hosting/dns_record_editor.html
index 0a5362f..f134600 100644
--- a/src/coulobre/hosting/templates/hosting/dns_record_editor.html
+++ b/src/coulobre/hosting/templates/hosting/dns_record_editor.html
@@ -11,8 +11,10 @@
Type
A AAAA CNAME MX TXT SRV CAA
- Priority
- Value
+
+ Priority
+ Value
+
Remove
diff --git a/src/coulobre/hosting/templates/hosting/dns_record_table.html b/src/coulobre/hosting/templates/hosting/dns_record_table.html
index acb2cae..3363621 100644
--- a/src/coulobre/hosting/templates/hosting/dns_record_table.html
+++ b/src/coulobre/hosting/templates/hosting/dns_record_table.html
@@ -1,10 +1,11 @@
{% if records %}
+ {% if not show_mailbox_credentials and mailboxes %}
+ Mailbox passwords cannot be viewed in this panel. Use your full email address as your username.
+ {% if mail_imap_host or mail_submission_host %}
+
+ {% if mail_imap_host %}
Incoming mail (IMAP over TLS) {{ mail_imap_host }} · port 993
{% endif %}
+ {% if mail_submission_host %}
Outgoing mail (SMTP with STARTTLS) {{ mail_submission_host }} · port 587 {% endif %}
+
+ {% endif %}
+ {% endif %}
{% if mail_domain %}Check email DNS
{% endif %}
{% endif %}
diff --git a/src/coulobre/hosting/templates/hosting/website_usage.html b/src/coulobre/hosting/templates/hosting/website_usage.html
index dfa2f54..a8069c2 100644
--- a/src/coulobre/hosting/templates/hosting/website_usage.html
+++ b/src/coulobre/hosting/templates/hosting/website_usage.html
@@ -1,4 +1,5 @@
{% extends "hosting/workspace_page.html" %}
+{% load tz %}
{% block title %}Usage · {{ account.name }}{% endblock %}
{% block page_title %}Usage for {{ account.name }}{% endblock %}
{% block page_actions %}
@@ -18,37 +19,58 @@
{% if not complete and total_traffic is not None %} · Partial{% endif %}
-{% for site, snapshot, quota in sites %}
-
- {{ site.domain }}
+{% for site, snapshot, quota, access_entries in sites %}
+
+
{% if snapshot %}
- Last checked {{ snapshot.observed_at }}
{% if snapshot.report.error %}
- {{ snapshot.report.error }}
+ {{ snapshot.report.error }}
{% else %}
-
- Website storage used {{ snapshot.report.storage_bytes|filesizeformat }}{% if quota is not None %} of {{ quota }} MiB{% endif %}
- HTTP response data for {{ snapshot.report.month }} (UTC)
- {% if snapshot.report.traffic_bytes is not None %}{{ snapshot.report.traffic_bytes|filesizeformat }}{% else %}Unavailable{% endif %}{% if snapshot.report.partial %} · Partial{% endif %}
+
+
Website storage used {{ snapshot.report.storage_bytes|filesizeformat }}{% if quota is not None %} of {{ quota }} MiB{% endif %}
+
HTTP response data for {{ snapshot.report.month }} (UTC)
+ {% if snapshot.report.traffic_bytes is not None %}{{ snapshot.report.traffic_bytes|filesizeformat }}{% else %}Unavailable{% endif %}{% if snapshot.report.partial %} · Partial{% endif %}
- Recent access log
- {% for line in snapshot.report.access_lines %}{{ line }} {% empty %}No entries.
{% endfor %}
- Recent error log
- {% for line in snapshot.report.error_lines %}{{ line }} {% empty %}No entries.
{% endfor %}
+
+ {% if access_entries %}
+
+ {% else %}No entries.
{% endif %}
+
+ {% if snapshot.report.error_lines %}{% for line in snapshot.report.error_lines %}{{ line }}{% if not forloop.last %}
+{% endif %}{% endfor %} {% else %}No entries.
{% endif %}
{% endif %}
{% else %}
- No measurement yet.
+ No measurement yet.
{% endif %}
+{% empty %}
+No active websites.
{% endfor %}
{% if mailboxes is not None %}
-
- Mailbox storage
-
- {% for mailbox in mailboxes %}
- {{ mailbox }} {% if mailbox.usage_bytes is not None %}{{ mailbox.usage_bytes|filesizeformat }} used · {% endif %}{{ mailbox.quota_mib|default_if_none:"Unlimited" }}{% if mailbox.quota_mib %} MiB{% endif %}
- {% empty %}No mailboxes.
{% endfor %}
-
+
+
+ {% if mailboxes %}
+
+ {% else %}No mailboxes.
{% endif %}
{% endif %}
diff --git a/src/coulobre/hosting/test_public_operations.py b/src/coulobre/hosting/test_public_operations.py
index 8718670..0b7576d 100644
--- a/src/coulobre/hosting/test_public_operations.py
+++ b/src/coulobre/hosting/test_public_operations.py
@@ -7,6 +7,7 @@ from unittest.mock import patch
from django.contrib.auth import get_user_model
from django.test import SimpleTestCase, TestCase, override_settings
from django.urls import reverse
+from django.utils import timezone
from coulobre.dns_catalog import render_catalog
from coulobre.mail_node import MailboxSpec, MailboxUsageRequest
@@ -14,7 +15,7 @@ from coulobre.mail_queue import QUEUE_NAMES, summarize_queue
from coulobre.web_node import WebSite
from coulobre.web_node_usage import read_site_log, without_usage_logs
-from .models import Customer, HostingAccount, HostingMailDomain
+from .models import Customer, HostingAccount, HostingMailDomain, WebsiteUsage
class ServiceInspectionTests(SimpleTestCase):
@@ -136,6 +137,38 @@ class PublicOperationsAccessTests(TestCase):
self.assertEqual(self.client.get(url).status_code, 404)
self.assertEqual(self.client.post(url).status_code, 404)
+ def test_usage_formats_access_entries_and_preserves_saved_log_text(self):
+ long_path = "/" + "directory-" * 20
+ url = reverse("panel:website_usage", args=[self.account.pk])
+ site = WebSite(self.account.pk, self.account.primary_domain)
+ WebsiteUsage.objects.create(
+ account=self.account,
+ node_key=site.node_key,
+ domain=site.domain,
+ observed_at=timezone.now(),
+ report={
+ "month": timezone.now().strftime("%Y-%m"),
+ "storage_bytes": 2048,
+ "traffic_bytes": 1024,
+ "access_lines": [
+ f"1735689600 1024 200 GET {long_path}",
+ "unrecognised access entry",
+ ],
+ "error_lines": [""],
+ "partial": False,
+ },
+ )
+ self.client.force_login(self.customer)
+ with patch(
+ "coulobre.hosting.website_usage_views.managed_sites",
+ return_value=[site],
+ ):
+ response = self.client.get(url)
+ self.assertContains(response, "1 Jan 2025, 00:00:00")
+ self.assertContains(response, long_path)
+ self.assertContains(response, "unrecognised access entry")
+ self.assertContains(response, "<log message>")
+
def test_queue_is_visible_to_operators_only(self):
url = reverse("panel:mail_queue", args=[self.domain.pk])
self.client.force_login(self.customer)
diff --git a/src/coulobre/hosting/test_workspaces.py b/src/coulobre/hosting/test_workspaces.py
index d6de8ec..234e650 100644
--- a/src/coulobre/hosting/test_workspaces.py
+++ b/src/coulobre/hosting/test_workspaces.py
@@ -10,10 +10,17 @@ from django_otp.plugins.otp_totp.models import TOTPDevice
from .models import (
Customer,
+ DomainServicePolicy,
HostingAccount,
+ HostingDnsZone,
+ HostingMailbox,
+ HostingMailDomain,
HostingPlan,
+ MailboxProvisioning,
PanelInvitation,
Reseller,
+ WebsiteAlias,
+ WebsiteDomain,
WebsiteScheduledJob,
WebsiteUsage,
)
@@ -126,6 +133,103 @@ class UserDeletionTests(TestCase):
class WorkspaceTests(TestCase):
+ def test_domain_selectors_list_only_domains_managed_by_the_service(self):
+ user = get_user_model().objects.create_user("customer")
+ account = HostingAccount.objects.create(
+ name="Example",
+ primary_domain="example.test",
+ customer=Customer.objects.create(user=user),
+ )
+ self.client.force_login(user)
+ alias = "www.example.test"
+ for name in (alias, "mailing.example.test"):
+ WebsiteAlias.objects.create(
+ reservation=WebsiteDomain.objects.create(
+ account=account, domain=name
+ ),
+ primary_domain=account.primary_domain,
+ )
+ DomainServicePolicy.objects.create(
+ account=account, domain="mailing.example.test", mail_managed=True
+ )
+ HostingMailDomain.objects.create(
+ account=account, domain="existing.example.test"
+ )
+ for name in (account.primary_domain, "delegated.example.test"):
+ HostingDnsZone.objects.create(
+ account=account,
+ domain=name,
+ nameservers=["ns.provider.test"],
+ responsible_name="hostmaster.example.test",
+ )
+
+ pages = {
+ "domains": [alias, "mailing.example.test"],
+ "dns": ["delegated.example.test"],
+ "forwarders": ["existing.example.test", "mailing.example.test"],
+ }
+ for page, available in pages.items():
+ path = reverse("panel:domain_workspace", args=[account.pk, page])
+ with self.subTest(page=page):
+ response = self.client.get(path)
+ self.assertEqual(response.status_code, 200)
+ for domain in available:
+ self.assertContains(response, f'