From b47ab872cebf425025ab98f5b1484f04c21fe32f Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Tue, 1 Dec 2026 00:50:45 +0100 Subject: [PATCH 1/3] Publish compatible RSA DKIM records without rotating keys --- docs/deployment.md | 15 ++++++ src/coulobre/hosting/mail_dns_setup.py | 35 ++++++++++---- src/coulobre/hosting/node_client.py | 4 +- src/coulobre/hosting/test_mail_dns.py | 63 ++++++++++++++++++++++++-- src/coulobre/mail_dns.py | 44 ++++++++++++++++-- src/coulobre/mail_node_dkim.py | 2 +- 6 files changed, 146 insertions(+), 17 deletions(-) diff --git a/docs/deployment.md b/docs/deployment.md index cca03d5..719df52 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -1051,6 +1051,21 @@ credential and archive operations. The separate legacy SSH transports for those operations are unavailable on a scoped receiver. Local mail delivery and IMAP keep using their existing configuration. +#### Refresh an existing managed DKIM record + +Upgrade the panels linked to a mail node before upgrading the mail node. +The updated panel accepts public keys from older mail nodes and publishes +the managed RSA key in SubjectPublicKeyInfo format. The existing private +key and selector stay in use. + +Open **Mail DNS check** for a managed mail domain and select +**Refresh mail DNS setup**. If Coulobre publishes the zone and manages its +mail records, it replaces the previous encoding when the public key +matches. A different key at the same selector requires operator review. +For third-party DNS, copy the updated DKIM TXT value to the DNS provider. +Check the authoritative answer, allow cached records to expire, and +verify that a newly delivered message passes DKIM. + ### Use SHA-2 for new managed database logins On a MariaDB build with `auth_mysql_sha2`, you can provision new managed diff --git a/src/coulobre/hosting/mail_dns_setup.py b/src/coulobre/hosting/mail_dns_setup.py index 8da6102..d0ef8f7 100644 --- a/src/coulobre/hosting/mail_dns_setup.py +++ b/src/coulobre/hosting/mail_dns_setup.py @@ -12,7 +12,12 @@ from coulobre.dns_node import ( TxtRecord, dns_name, ) -from coulobre.mail_dns import MANAGED_DKIM_SELECTOR, matches_version +from coulobre.mail_dns import ( + MANAGED_DKIM_SELECTOR, + matches_version, + normalize_rsa_dkim_record, + same_rsa_dkim_key, +) from .dns_publication import queue_zone_snapshot from .models import HostingDnsZone, HostingMailDomain, MailboxProvisioning @@ -53,7 +58,8 @@ def mail_dns_proposal(domain, public_key): if public_key: records.append( TxtRecord( - f"{MANAGED_DKIM_SELECTOR}._domainkey.{domain}", public_key + f"{MANAGED_DKIM_SELECTOR}._domainkey.{domain}", + normalize_rsa_dkim_record(public_key), ) ) return addresses, mx, tuple(records) @@ -104,7 +110,7 @@ def sync_mail_dns(mail_domain, *, public_key=None): if current is None or account.retired_at is not None: return if public_key is not None: - current.dkim_record = public_key + current.dkim_record = normalize_rsa_dkim_record(public_key) current.dns_sync_requested = True current.save(update_fields=["dkim_record", "dns_sync_requested"]) if not current.dns_sync_requested or account.services_suspended: @@ -173,15 +179,23 @@ def publish_mail_records(zone, domain, public_key): existing = spec.txt_records new_txt = [] + replacements = {} problem = "" for record in txt: version = record.text.split(" ")[0].rstrip(";") at_owner = tuple(item for item in existing if item.name == record.name) if version == "v=DKIM1": if at_owner and at_owner != (record,): - raise ValueError( - "The Coulobre DKIM selector already has a record." - ) + if len(at_owner) != 1 or not same_rsa_dkim_key( + at_owner[0].text, record.text + ): + raise ValueError( + "The Coulobre DKIM selector already has a record." + ) + previous = at_owner[0] + converted = normalize_rsa_dkim_record(previous.text) + if converted != previous.text: + replacements[previous] = replace(previous, text=converted) else: at_owner = tuple( item @@ -207,7 +221,9 @@ def publish_mail_records(zone, domain, public_key): spec.nameservers, (*spec.addresses, *spec.mx_records, *existing, *spec.extra_records), ) - changed = bool(additions or not current_mx or new_txt or alias) + changed = bool( + additions or not current_mx or new_txt or replacements or alias + ) if publication and publication.finished_at is None: if changed: raise MailDnsPublicationPendingError @@ -219,7 +235,10 @@ def publish_mail_records(zone, domain, public_key): serial=(spec.serial + 1) % (MAX_SOA_SERIAL + 1), addresses=spec.addresses + additions, mx_records=spec.mx_records + (mx if not current_mx else ()), - txt_records=spec.txt_records + tuple(new_txt), + txt_records=tuple( + replacements.get(record, record) for record in existing + ) + + tuple(new_txt), extra_records=spec.extra_records + ((alias,) if alias else ()), ) manifest = spec.manifest() diff --git a/src/coulobre/hosting/node_client.py b/src/coulobre/hosting/node_client.py index c7f26c6..167bd72 100644 --- a/src/coulobre/hosting/node_client.py +++ b/src/coulobre/hosting/node_client.py @@ -13,7 +13,7 @@ from coulobre.account_restore_request import ( RESTORE_TIMEOUT_SECONDS, ) from coulobre.linked_updates import ROLES, UNIT_DIR, read_probe -from coulobre.mail_dns import dkim_key +from coulobre.mail_dns import dkim_key, normalize_rsa_dkim_record from coulobre.mail_node import MailboxQuotaUpdate, MailboxSigningRequest from coulobre.node_probe import PROBE_BYTES, parse_status_probe from coulobre.php_pool_settings import PhpPoolSettings @@ -180,7 +180,7 @@ def provision_signing_mailbox(mailbox): or dkim_key(public)[0] != "ok" ): raise ValueError("The mail node returned an invalid DKIM public key.") - return public + return normalize_rsa_dkim_record(public) def update_local_mailbox_quota(mailbox): diff --git a/src/coulobre/hosting/test_mail_dns.py b/src/coulobre/hosting/test_mail_dns.py index 04615ad..f48a572 100644 --- a/src/coulobre/hosting/test_mail_dns.py +++ b/src/coulobre/hosting/test_mail_dns.py @@ -214,6 +214,14 @@ class MailDnsPanelTests(TestCase): public_key.public_bytes(Encoding.DER, PublicFormat.PKCS1) ).decode() ) + spki = ( + "v=DKIM1; k=rsa; p=" + + base64.b64encode( + public_key.public_bytes( + Encoding.DER, PublicFormat.SubjectPublicKeyInfo + ) + ).decode() + ) with patch( "coulobre.hosting.tasks.provision_signing_mailbox", return_value=dkim, @@ -223,8 +231,9 @@ class MailDnsPanelTests(TestCase): self.assertEqual(job.state, "configured") self.domain.refresh_from_db() zone.refresh_from_db() - self.assertEqual(self.domain.dkim_record, dkim) - self.assertEqual(dkim_key(dkim, expected=dkim)[0], "ok") + self.assertEqual(self.domain.dkim_record, spki) + self.assertEqual(dkim_key(dkim, expected=spki)[0], "ok") + self.assertEqual(dkim_key(spki, expected=dkim)[0], "ok") self.assertEqual(dkim_key(DKIM, expected=dkim)[0], "attention") self.assertEqual(zone.serial, 6) self.assertEqual( @@ -246,7 +255,7 @@ class MailDnsPanelTests(TestCase): { DOMAIN: f"v=spf1 ip4:{IP} ~all", "_dmarc.example.net": "v=DMARC1; p=none", - "coulobre._domainkey.example.net": dkim, + "coulobre._domainkey.example.net": spki, }, ) publication = DnsPublication.objects.get(zone=zone) @@ -255,6 +264,54 @@ class MailDnsPanelTests(TestCase): zone.refresh_from_db() self.assertEqual(zone.serial, 6) + def test_existing_dkim_is_reencoded_only_for_the_same_rsa_key(self): + prefix = "v=DKIM1; k=rsa; p=" + selector = f"coulobre._domainkey.{DOMAIN}" + signer = generate_private_key( + public_exponent=65537, key_size=2048 + ).public_key() + previous = ( + prefix + + base64.b64encode( + signer.public_bytes(Encoding.DER, PublicFormat.PKCS1) + ).decode() + ) + other = {"name": DOMAIN, "text": "site-verification=unchanged"} + zone = self.zone( + manage_mail_dns=True, + txt_records=[ + {"name": selector, "text": previous + "; s=email"}, + other, + ], + ) + sync_mail_dns(self.domain, public_key=previous) + zone.refresh_from_db() + self.domain.refresh_from_db() + self.assertNotEqual(self.domain.dkim_record, previous) + self.assertIn( + {"name": selector, "text": self.domain.dkim_record + "; s=email"}, + zone.txt_records, + ) + self.assertIn(other, zone.txt_records) + conflicting_signer = generate_private_key( + public_exponent=65537, key_size=2048 + ).public_key() + different = ( + prefix + + base64.b64encode( + conflicting_signer.public_bytes( + Encoding.DER, PublicFormat.SubjectPublicKeyInfo + ) + ).decode() + ) + records, serial = zone.txt_records, zone.serial + sync_mail_dns(self.domain, public_key=different) + zone.refresh_from_db() + self.domain.refresh_from_db() + self.assertEqual(zone.txt_records, records) + self.assertEqual(zone.serial, serial) + self.assertIn("already has a record", self.domain.dns_setup_problem) + def test_dns_sync_keeps_an_existing_mail_client_host_record(self): address = {"name": f"mail.{DOMAIN}", "address": "192.0.2.43"} zone = self.zone(manage_mail_dns=True, addresses=[address]) diff --git a/src/coulobre/mail_dns.py b/src/coulobre/mail_dns.py index f7ff9b4..452a748 100644 --- a/src/coulobre/mail_dns.py +++ b/src/coulobre/mail_dns.py @@ -430,9 +430,7 @@ def dkim_key(record, *, expected=""): "attention", "RSA key published. Prefer at least 2048 bits.", ) - if expected and key.public_bytes( - Encoding.DER, PublicFormat.PKCS1 - ) != decode_key(read_tags(expected).get("p", "")): + if expected and not same_rsa_dkim_key(record, expected): return ( "attention", "The public key differs from the mail node's key.", @@ -448,6 +446,46 @@ def decode_key(value): return base64.b64decode(data, validate=True) +def rsa_dkim_key(record): + tags = read_tags(record) + if tags.get("k", "rsa") != "rsa" or ( + "v" in tags and (next(iter(tags)) != "v" or tags["v"] != "DKIM1") + ): + return None + try: + key = load_der_public_key(decode_key(tags.get("p", ""))) + except (ValueError, UnsupportedAlgorithm): + return None + return key if isinstance(key, RSAPublicKey) else None + + +def same_rsa_dkim_key(first, second): + first_key = rsa_dkim_key(first) + second_key = rsa_dkim_key(second) + return ( + first_key is not None + and second_key is not None + and first_key.public_numbers() == second_key.public_numbers() + ) + + +def normalize_rsa_dkim_record(record): + """Encode the public key for DNS and preserve other tags.""" + key = rsa_dkim_key(record) + if key is None: + return record + data = key.public_bytes(Encoding.DER, PublicFormat.SubjectPublicKeyInfo) + encoded = base64.b64encode(data).decode("ascii") + if decode_key(read_tags(record)["p"]) == data: + return record + return re.sub( + r"(^|;)(\s*p\s*=)[^;]*", + lambda match: match[1] + match[2] + encoded, + record, + count=1, + ) + + def sts_policy(record): policy_id = read_tags(record).get("id", "") if not re.fullmatch(r"[A-Za-z0-9]{1,32}", policy_id): diff --git a/src/coulobre/mail_node_dkim.py b/src/coulobre/mail_node_dkim.py index 1f61bb4..205ca77 100644 --- a/src/coulobre/mail_node_dkim.py +++ b/src/coulobre/mail_node_dkim.py @@ -65,6 +65,6 @@ def provision_dkim_key(domain): "The DKIM key must be RSA with at least 2048 bits." ) public = key.public_key().public_bytes( - Encoding.DER, PublicFormat.PKCS1 + Encoding.DER, PublicFormat.SubjectPublicKeyInfo ) return "v=DKIM1; k=rsa; p=" + base64.b64encode(public).decode("ascii") From bb826877d219b658b472a46113002a5b09836a9f Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Tue, 1 Dec 2026 00:50:45 +0100 Subject: [PATCH 2/3] 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 @@ - - +
+ + +
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 %}
+ {% for record in records %} - + {% endfor %}
NameTypeValue
{{ record.name }}{{ record.type }}{{ record.value }}
{{ record.name }}{{ record.type }}{{ record.value }}
diff --git a/src/coulobre/hosting/templates/hosting/domain_workspace.html b/src/coulobre/hosting/templates/hosting/domain_workspace.html index dc4e410..a84b331 100644 --- a/src/coulobre/hosting/templates/hosting/domain_workspace.html +++ b/src/coulobre/hosting/templates/hosting/domain_workspace.html @@ -1,12 +1,14 @@ {% extends "hosting/workspace_page.html" %} {% block title %}{{ title }} · Coulobre Panel{% endblock %} {% block page_title %}{{ title }}{% endblock %} -{% block page_metadata %}

{{ account.primary_domain }}

{% endblock %} +{% block page_metadata %}

{{ domain }}

{% endblock %} {% block page_actions %} {% if page == 'domains' and can_add_site %}Add domain{% endif %} {% endblock %} {% block page_context %} - {% include "hosting/workspace_domain_selector.html" with field_id="domain" domains=domains domain=domain only %} + {% if domains %} + {% include "hosting/workspace_domain_selector.html" with field_id="domain" domains=domains domain=domain only %} + {% endif %} {% endblock %} {% block page_content %} {% if page == 'domains' %} @@ -17,9 +19,7 @@ {% if zone %}

DNS records

{% if zone.publication %}

Publication {{ zone.publication.state }}

{% endif %}
{% if may_edit_dns %}Edit zone{% endif %}
-
- {% for record in records %}{% endfor %} -
NameTypeValue
{{ record.name }}{{ record.type }}{{ record.value }}
+ {% include "hosting/dns_record_table.html" %}
{% else %}

This domain has no saved Coulobre DNS zone.

{% endif %} {% if not policy.dns_managed %} @@ -28,8 +28,8 @@
    {% for record in external_website_records %}
  • {{ record.name }} {{ record.record_type }} {{ record.address }}
  • {% empty %}
  • Ask your hosting provider for the website addresses.
  • {% endfor %}
{% if mail_domain and policy.mail_managed %}

Check MX, SPF, DKIM and DMARC records

{% endif %} - {% elif zone_defaults.nameservers %} -

Nameservers

{{ zone_defaults.nameservers|join:", " }}

+ {% elif zone %} +

Nameservers

{{ zone.spec.nameservers|join:", " }}

{% endif %} {% elif page == 'php' or page == 'files' %}
@@ -54,11 +54,22 @@ Email addressQuotaStateActions {% for mailbox in mailboxes %}{{ mailbox.spec.address }}{{ mailbox.quota_mib|default_if_none:"Unlimited" }}{% if mailbox.quota_mib %} MiB{% endif %}{{ mailbox.provisioning.state|default:"Requested"|capfirst }} {% if mailbox.provisioning.state == 'configured' %} - Credentials + {% if show_mailbox_credentials %} + Credentials + {% else %}Use the current mailbox password{% endif %} {% if roundcube_enabled %}
{% csrf_token %}
{% endif %} {% endif %} {% empty %}No mailboxes on this domain.{% endfor %}
+ {% 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 %} +
+
+

{{ site.domain }}

+ {% if snapshot %}

Last checked {{ snapshot.observed_at|utc|date:"j M Y, H:i:s" }} UTC

{% endif %} +
{% if snapshot %} -

Last checked {{ snapshot.observed_at }}

{% if 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 %} +

Recent access log

+ {% if access_entries %} +
+ + + {% for entry in access_entries %} + {% if entry.time %} + + {% else %}{% endif %} + {% endfor %} +
Time (UTC)MethodStatusPathSize
{{ entry.time|utc|date:"j M Y, H:i:s" }}{{ entry.method }}{{ entry.status }}{{ entry.path }}{{ entry.bytes|filesizeformat }}
{{ entry.raw }}
+
+ {% else %}

No entries.

{% endif %} +

Recent error log

+ {% 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 %} -
+
+

Mailbox storage

+ {% if mailboxes %} +
+ + + {% for mailbox in mailboxes %} + + {% endfor %} +
MailboxStorage usedLimit
{{ mailbox }}{% if mailbox.usage_bytes is not None %}{{ mailbox.usage_bytes|filesizeformat }}{% else %}Unavailable{% endif %}{{ mailbox.quota_mib|default_if_none:"Unlimited" }}{% if mailbox.quota_mib %} MiB{% endif %}
+
+ {% 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'