diff --git a/docs/deployment.md b/docs/deployment.md index 4488a8e..a5c7238 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -496,6 +496,20 @@ connected. Follow their logs with `journalctl`. sudo journalctl -u coulobre-panel -u coulobre-worker -u coulobre-beat ``` +## Manage panel roles + +A verified superuser can assign a Customer or Customer manager role to an +active login from **Customers** or **Customer managers**. Choose **Assign +to existing login** and enter the username. Customer roles use their own +assigned hosting accounts. Customer managers use their own hosting account +assignments and hosting plan policy. New manager assignments use the default +customer manager profile when one is configured. + +Logins with multiple roles can choose **Panel role** in the account area. +The role applies to the session, including direct page requests and actions. +Returning to the administrator role requires a verified second factor. If the +active role assignment is revoked, the session asks the user to sign in again. + ## Select a panel theme The panel follows the browser's light or dark preference on the first visit. @@ -2657,6 +2671,25 @@ Keep the managed data directories on their service filesystems to allow an atomic move into the archive directory. Existing backup archives remain available to operators with backup access. +## Configure installed features in the panel + +Open **About Coulobre → Configure features** as a superuser with a verified +second factor. The page shows the installation prerequisites and distinguishes +local mail and DNS services from linked receivers. Use **Configure DNS +defaults** to edit the zone template for new hosting accounts. + +The switches control which installed capabilities the panel offers for new +actions. Existing managed sites, mailbox delivery, published DNS records, +schedules and queued provisioning continue running. Backup archives remain +accessible when new backup requests are paused. Pausing an application sign-in +also prevents redemption of pending tickets for that application. + +Configure service packages, linked receiver identities, bridge keys and +backup credentials on the host using their deployment procedures. Enabling +a paused feature checks its available service socket and, for linked mail or +DNS, the receiver's readiness and version. A failed prerequisite check leaves +the saved choices intact. Changes are recorded in the administrator audit log. + ## Monitor local and remote services The panel's **Service health** page shows the latest observation and timestamp diff --git a/src/coulobre/hosting/account_backup_forms.py b/src/coulobre/hosting/account_backup_forms.py index 5e8ad10..362a929 100644 --- a/src/coulobre/hosting/account_backup_forms.py +++ b/src/coulobre/hosting/account_backup_forms.py @@ -11,6 +11,7 @@ from coulobre.account_restore_request import ( ) from coulobre.database_node import DatabaseSpec +from .installation_features import panel_feature_allowed from .models import AccountBackupSchedule SCHEDULE_FIELDS = ( @@ -49,7 +50,11 @@ class AccountBackupScheduleForm(forms.Form): self.allow_mail = allow_mail if not allow_mail and not self.initial.get("include_mailboxes"): del self.fields["include_mailboxes"] - destinations = settings.OFFSITE_BACKUP_DESTINATIONS + destinations = ( + settings.OFFSITE_BACKUP_DESTINATIONS + if panel_feature_allowed("OFFSITE_BACKUP_DESTINATIONS") + else {} + ) current = self.initial.get("offsite_destination") if destinations or current: choices = [("", "Local archive only"), *destinations.items()] diff --git a/src/coulobre/hosting/account_setup.py b/src/coulobre/hosting/account_setup.py index f53f143..a01f327 100644 --- a/src/coulobre/hosting/account_setup.py +++ b/src/coulobre/hosting/account_setup.py @@ -11,6 +11,7 @@ from coulobre.web_node import WebSite from .dns_defaults import zone_defaults from .dns_forms import normalize_record_rows from .dns_publication import queue_zone_snapshot +from .installation_features import panel_feature_allowed from .mail_dns_setup import mail_dns_proposal from .models import ( HostingDnsZone, @@ -70,8 +71,8 @@ def setup_new_account(account, options): ) if managed_dns: existing_managed_mail = ( - settings.LOCAL_MAILBOX_PROVISIONING - and settings.MANAGE_MAIL_DNS + panel_feature_allowed("LOCAL_MAILBOX_PROVISIONING") + and panel_feature_allowed("MANAGE_MAIL_DNS") and account.mail_domains.filter(domain=domain).exists() ) create_default_dns_zone( diff --git a/src/coulobre/hosting/independent_website_views.py b/src/coulobre/hosting/independent_website_views.py index c77d68c..d5e68d6 100644 --- a/src/coulobre/hosting/independent_website_views.py +++ b/src/coulobre/hosting/independent_website_views.py @@ -1,5 +1,4 @@ from django import forms -from django.conf import settings from django.contrib.auth.mixins import PermissionRequiredMixin from django.core.exceptions import ValidationError from django.db import IntegrityError @@ -20,6 +19,7 @@ from .independent_websites import ( create_website, queue_website_operation, ) +from .installation_features import panel_feature_allowed from .models import HostingAccount, IndependentWebsite, WebsiteDomain @@ -79,7 +79,7 @@ class IndependentWebsiteEditor( template_name = "hosting/independent_website_form.html" def dispatch(self, request, *args, **kwargs): - if not settings.LOCAL_WEB_ACTIVATION: + if not panel_feature_allowed("LOCAL_WEB_ACTIVATION"): raise Http404 return super().dispatch(request, *args, **kwargs) @@ -130,7 +130,9 @@ class IndependentWebsiteOperation(IndependentWebsiteEditor): operation = Operation(self.kwargs["operation"]) except ValueError: raise Http404 from None - if operation == Operation.HTTPS and not settings.LOCAL_WEB_HTTPS: + if operation == Operation.HTTPS and not panel_feature_allowed( + "LOCAL_WEB_HTTPS" + ): raise Http404 return operation diff --git a/src/coulobre/hosting/independent_websites.py b/src/coulobre/hosting/independent_websites.py index 094b798..90d4691 100644 --- a/src/coulobre/hosting/independent_websites.py +++ b/src/coulobre/hosting/independent_websites.py @@ -9,6 +9,7 @@ from coulobre.php_runtime import validate_php_version from coulobre.service_access import ServiceAccessRequest from .access import is_tenant, manages_account +from .installation_features import panel_feature_allowed from .models import IndependentWebsite, WebsiteDomain, WebsiteRestoreJob from .node_client import ( activate_local_site, @@ -54,7 +55,7 @@ def website_actions(website): if website.suspended: return (Operation.RESUME, Operation.RETIRE) actions = (Operation.PHP, Operation.SUSPEND) - if settings.LOCAL_WEB_HTTPS and not website.https_enabled: + if panel_feature_allowed("LOCAL_WEB_HTTPS") and not website.https_enabled: actions += (Operation.HTTPS,) return actions diff --git a/src/coulobre/hosting/installation_features.py b/src/coulobre/hosting/installation_features.py new file mode 100644 index 0000000..bf499a8 --- /dev/null +++ b/src/coulobre/hosting/installation_features.py @@ -0,0 +1,193 @@ +from contextvars import ContextVar +from dataclasses import dataclass +from pathlib import Path + +from django.conf import settings +from django.utils.functional import cached_property + +from coulobre.linked_updates import UNIT_DIR + +from .dns_defaults import zone_defaults +from .models import PanelFeaturePolicy + + +@dataclass(frozen=True) +class Feature: + setting: str + label: str + prerequisite: str + socket: str = "" + role: str = "" + + @property + def configured(self): + if self.setting == "DEFAULT_DNS_ZONES": + defaults = zone_defaults() + return bool( + settings.LOCAL_DNS_PUBLISHING + and defaults.nameservers + and defaults.addresses + ) + if self.setting == "PHPMYADMIN_URL": + return bool( + settings.PHPMYADMIN_URL and settings.LOCAL_DATABASE_CREDENTIALS + ) + if self.setting == "ROUNDCUBE_URL": + return bool( + settings.ROUNDCUBE_URL + and ( + settings.LOCAL_MAILBOX_CREDENTIALS + or settings.REMOTE_MAILBOX_CREDENTIALS + ) + ) + return bool(getattr(settings, self.setting)) + + @property + def linked(self): + return ( + bool(self.role) + and ( + UNIT_DIR / f"coulobre-{self.role}-apply@.service.d/remote.conf" + ).is_file() + ) + + @property + def socket_ready(self): + if self.setting == "ROUNDCUBE_URL": + socket = ( + "coulobre-mail-credentials" + if settings.REMOTE_MAILBOX_CREDENTIALS + else "coulobre-service-credentials" + ) + return Path(f"/run/{socket}.sock").is_socket() + return not self.socket or Path(f"/run/{self.socket}.sock").is_socket() + + +FEATURES = ( + Feature( + "LOCAL_WEB_ACTIVATION", + "Website provisioning", + "Apache and PHP provisioning", + "coulobre-web-apply", + ), + Feature( + "LOCAL_WEB_HTTPS", + "Managed website certificates", + "Website certificate provisioning", + "coulobre-https-apply", + ), + Feature( + "WEB_FILE_MANAGER", + "Website file manager", + "Website file service", + "coulobre-website-files", + ), + Feature( + "WEBSITE_JOBS", + "Website scheduled tasks", + "Website task service", + "coulobre-website-jobs", + ), + Feature( + "LOCAL_DNS_PUBLISHING", + "DNS publishing", + "Local BIND9 or a linked DNS receiver", + "coulobre-dns-apply", + "dns", + ), + Feature( + "DEFAULT_DNS_ZONES", + "Default managed DNS zones for new accounts", + "DNS publishing, nameservers and hosting IP addresses", + ), + Feature( + "LOCAL_MAILBOX_PROVISIONING", + "Mailbox provisioning", + "Local mail services or a linked mail receiver", + "coulobre-mail-apply", + "mail", + ), + Feature( + "MANAGE_MAIL_DNS", + "Managed mail DNS records", + "Mail exchanger and sending IP addresses", + ), + Feature( + "LOCAL_DATABASE_PROVISIONING", + "Database provisioning", + "MariaDB provisioning", + "coulobre-database-apply", + ), + Feature( + "LOCAL_ACCOUNT_BACKUPS", + "Local account backups", + "Local backup service", + "coulobre-account-backup", + ), + Feature( + "OFFSITE_BACKUP_DESTINATIONS", + "Off-site backup destinations", + "A configured destination and the transfer service", + "coulobre-account-offsite", + ), + Feature( + "PHPMYADMIN_URL", + "phpMyAdmin sign-in", + "phpMyAdmin bridge and MariaDB credential service", + "coulobre-service-credentials", + ), + Feature( + "ROUNDCUBE_URL", + "Roundcube sign-in", + "Roundcube bridge and mailbox credential service", + ), +) + + +class FeaturePolicy: + @cached_property + def saved(self): + return ( + PanelFeaturePolicy.objects.filter(pk=1).first() + or PanelFeaturePolicy() + ) + + @cached_property + def disabled(self): + return frozenset(self.saved.disabled) + + +current_policy = ContextVar("current_feature_policy", default=None) + + +def panel_feature_allowed(setting): + if not getattr(settings, setting): + return False + return not panel_feature_paused(setting) + + +def panel_feature_paused(setting): + policy = current_policy.get() or FeaturePolicy() + return setting in policy.disabled + + +def default_dns_zones_allowed(defaults): + if not defaults.nameservers or not defaults.addresses: + return False + policy = current_policy.get() or FeaturePolicy() + return ( + panel_feature_allowed("LOCAL_DNS_PUBLISHING") + and "DEFAULT_DNS_ZONES" not in policy.disabled + ) + + +class FeaturePolicyMiddleware: + def __init__(self, get_response): + self.get_response = get_response + + def __call__(self, request): + token = current_policy.set(FeaturePolicy()) + try: + return self.get_response(request) + finally: + current_policy.reset(token) diff --git a/src/coulobre/hosting/installation_views.py b/src/coulobre/hosting/installation_views.py index 24544c1..9e73056 100644 --- a/src/coulobre/hosting/installation_views.py +++ b/src/coulobre/hosting/installation_views.py @@ -1,13 +1,23 @@ from importlib.metadata import PackageNotFoundError, version -from django.conf import settings -from django.contrib.auth.mixins import PermissionRequiredMixin +from django import forms +from django.contrib import messages +from django.contrib.admin.models import CHANGE, LogEntry +from django.contrib.auth.mixins import ( + PermissionRequiredMixin, + UserPassesTestMixin, +) +from django.db import transaction +from django.db.models import F +from django.urls import reverse_lazy from django.utils.decorators import method_decorator from django.views.decorators.cache import never_cache -from django.views.generic import TemplateView +from django.views.generic import FormView, TemplateView from .access import OperatorOnlyMixin -from .dns_defaults import zone_defaults +from .installation_features import FEATURES, FeaturePolicy +from .models import PanelFeaturePolicy +from .update_views import inspect_linked_nodes @method_decorator(never_cache, name="dispatch") @@ -22,41 +32,137 @@ class InstallationView( installed_version = version("coulobre-panel") except PackageNotFoundError: installed_version = "Unavailable" - - dns_defaults = zone_defaults() - features = ( - ("Local website provisioning", settings.LOCAL_WEB_ACTIVATION), - ("Managed website certificates", settings.LOCAL_WEB_HTTPS), - ("Website file manager", settings.WEB_FILE_MANAGER), - ("Website scheduled tasks", settings.WEBSITE_JOBS), - ("Local DNS publishing", settings.LOCAL_DNS_PUBLISHING), - ( - "Default managed DNS zones for new accounts", - settings.LOCAL_DNS_PUBLISHING - and dns_defaults.nameservers - and dns_defaults.addresses, - ), - ( - "Local mailbox provisioning", - settings.LOCAL_MAILBOX_PROVISIONING, - ), - ("Managed mail DNS records", settings.MANAGE_MAIL_DNS), - ( - "Local database provisioning", - settings.LOCAL_DATABASE_PROVISIONING, - ), - ("Local account backups", settings.LOCAL_ACCOUNT_BACKUPS), - ( - "Off-site backup destinations", - settings.OFFSITE_BACKUP_DESTINATIONS, - ), - ("phpMyAdmin sign-in", settings.PHPMYADMIN_URL), - ("Roundcube sign-in", settings.ROUNDCUBE_URL), - ) + policy = FeaturePolicy() + features = [] + for feature in FEATURES: + configured = feature.configured + enabled = ( + configured + and feature.setting not in policy.disabled + and ( + feature.setting != "DEFAULT_DNS_ZONES" + or "LOCAL_DNS_PUBLISHING" not in policy.disabled + ) + ) + features.append((feature, configured, enabled)) return super().get_context_data( - installed_version=installed_version, - features=[ - (name, bool(configured)) for name, configured in features - ], - **kwargs, + installed_version=installed_version, features=features, **kwargs + ) + + +class FeatureConfigurationForm(forms.Form): + revision = forms.IntegerField(min_value=0, widget=forms.HiddenInput) + + def __init__(self, *args, policy, **kwargs): + super().__init__(*args, **kwargs) + self.initial["revision"] = policy.saved.revision + self.configured_features = [ + feature for feature in FEATURES if feature.configured + ] + for feature in self.configured_features: + self.fields[feature.setting] = forms.BooleanField( + required=False, + label=feature.label, + help_text=feature.prerequisite, + initial=feature.setting not in policy.disabled, + ) + + def changed_features(self): + return [ + feature + for feature in self.configured_features + if feature.setting in self.changed_data + ] + + +def check_features(form, changed): + new_enabled = [ + feature for feature in changed if form.cleaned_data[feature.setting] + ] + nodes = {} + if any(feature.linked for feature in new_enabled): + topology = inspect_linked_nodes() + nodes = {node["role"]: node for node in topology.get("nodes", [])} + for feature in new_enabled: + if not feature.socket_ready: + form.add_error( + feature.setting, + "Start the configured service before enabling this feature.", + ) + elif feature.linked: + report = nodes.get(feature.role, {}) + if report.get("state") != "configured" or report.get( + "release" + ) != version("coulobre-panel"): + form.add_error( + feature.setting, + "The linked receiver must be ready and on the same " + "release.", + ) + if ( + feature.setting == "DEFAULT_DNS_ZONES" + and not form.cleaned_data.get("LOCAL_DNS_PUBLISHING") + ): + form.add_error(feature.setting, "Enable DNS publishing first.") + + +@method_decorator(never_cache, name="dispatch") +class FeatureConfigurationView(UserPassesTestMixin, FormView): + form_class = FeatureConfigurationForm + template_name = "hosting/feature_configuration.html" + success_url = reverse_lazy("panel:installation") + + def test_func(self): + return ( + self.request.user.is_superuser and self.request.user.is_verified() + ) + + def get_form_kwargs(self): + return {**super().get_form_kwargs(), "policy": FeaturePolicy()} + + def form_valid(self, form): + revision = form.cleaned_data["revision"] + policy = FeaturePolicy() + if revision != policy.saved.revision: + return self.stale_form(form) + changed = form.changed_features() + if not changed: + return super().form_valid(form) + check_features(form, changed) + if form.errors: + return self.form_invalid(form) + disabled = set(policy.disabled) + for feature in changed: + if form.cleaned_data[feature.setting]: + disabled.discard(feature.setting) + else: + disabled.add(feature.setting) + with transaction.atomic(): + saved, _ = PanelFeaturePolicy.objects.get_or_create(pk=1) + updated = PanelFeaturePolicy.objects.filter( + pk=1, revision=revision + ).update(disabled=sorted(disabled), revision=F("revision") + 1) + if not updated: + return self.stale_form(form) + LogEntry.objects.log_actions( + self.request.user.pk, + [saved], + CHANGE, + [ + { + "changed": { + "fields": [feature.label for feature in changed] + } + } + ], + ) + messages.success(self.request, "Feature configuration saved.") + return super().form_valid(form) + + def stale_form(self, form): + form.add_error( + None, "The configuration changed. Reopen this page and try again." + ) + return self.render_to_response( + self.get_context_data(form=form), status=409 ) diff --git a/src/coulobre/hosting/mail_dns_views.py b/src/coulobre/hosting/mail_dns_views.py index d9a715c..5cc74f9 100644 --- a/src/coulobre/hosting/mail_dns_views.py +++ b/src/coulobre/hosting/mail_dns_views.py @@ -22,6 +22,7 @@ from coulobre.mail_dns import ( ) from .access import OperatorOnlyMixin, TenantScopeMixin, is_tenant +from .installation_features import panel_feature_allowed from .mail_dns_setup import ( closest_zone, mail_dns_proposal, @@ -156,8 +157,8 @@ class MailDnsCheckView( report=report, mail_domain=self.object, can_refresh=( - settings.MANAGE_MAIL_DNS - and settings.LOCAL_MAILBOX_PROVISIONING + panel_feature_allowed("MANAGE_MAIL_DNS") + and panel_feature_allowed("LOCAL_MAILBOX_PROVISIONING") and not is_tenant(self.request.user) and self.request.user.has_perms(PROVISION_MAILBOX) and self.object.mailboxes.filter( @@ -261,7 +262,8 @@ class RequestMailDnsSetup(OperatorOnlyMixin, PermissionRequiredMixin, View): def post(self, request, pk): if not ( - settings.MANAGE_MAIL_DNS and settings.LOCAL_MAILBOX_PROVISIONING + panel_feature_allowed("MANAGE_MAIL_DNS") + and panel_feature_allowed("LOCAL_MAILBOX_PROVISIONING") ): raise Http404 domain = get_object_or_404(HostingMailDomain, pk=pk) diff --git a/src/coulobre/hosting/mail_queue_views.py b/src/coulobre/hosting/mail_queue_views.py index 778d61b..c857c67 100644 --- a/src/coulobre/hosting/mail_queue_views.py +++ b/src/coulobre/hosting/mail_queue_views.py @@ -1,6 +1,5 @@ from datetime import timedelta -from django.conf import settings from django.contrib.auth.mixins import PermissionRequiredMixin from django.db import transaction from django.http import Http404 @@ -11,6 +10,7 @@ from django.views.decorators.cache import never_cache from django.views.generic import TemplateView from .access import OperatorOnlyMixin +from .installation_features import panel_feature_allowed from .models import HostingMailDomain from .tasks import refresh_mail_queue @@ -24,7 +24,7 @@ class MailQueueView(OperatorOnlyMixin, PermissionRequiredMixin, TemplateView): template_name = "hosting/mail_queue.html" def get_domain(self, *, locked=False): - if not settings.LOCAL_MAILBOX_PROVISIONING: + if not panel_feature_allowed("LOCAL_MAILBOX_PROVISIONING"): raise Http404 domains = HostingMailDomain.objects.filter( account__retired_at__isnull=True diff --git a/src/coulobre/hosting/migrations/0059_panel_feature_policy.py b/src/coulobre/hosting/migrations/0059_panel_feature_policy.py new file mode 100644 index 0000000..feec519 --- /dev/null +++ b/src/coulobre/hosting/migrations/0059_panel_feature_policy.py @@ -0,0 +1,28 @@ +# Generated by Django 5.2.19 on 2026-11-23 19:32 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("hosting", "0058_scope_service_signon_grants"), + ] + + operations = [ + migrations.CreateModel( + name="PanelFeaturePolicy", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("disabled", models.JSONField(blank=True, default=list)), + ("revision", models.PositiveBigIntegerField(default=0)), + ], + ), + ] diff --git a/src/coulobre/hosting/models.py b/src/coulobre/hosting/models.py index e7ac10f..a2c0fc9 100644 --- a/src/coulobre/hosting/models.py +++ b/src/coulobre/hosting/models.py @@ -608,6 +608,11 @@ class DirectAdminImport(models.Model): fingerprint = models.CharField(max_length=64, unique=True) +class PanelFeaturePolicy(models.Model): + disabled = models.JSONField(default=list, blank=True) + revision = models.PositiveBigIntegerField(default=0) + + class DnsZoneDefaults(models.Model): nameservers = models.JSONField(default=list, blank=True) addresses = models.JSONField(default=list, blank=True) diff --git a/src/coulobre/hosting/phpmyadmin.py b/src/coulobre/hosting/phpmyadmin.py index 9d7e356..5ce089c 100644 --- a/src/coulobre/hosting/phpmyadmin.py +++ b/src/coulobre/hosting/phpmyadmin.py @@ -12,6 +12,7 @@ from django.views.decorators.http import require_POST from .access import customer_for_user from .credential_views import DatabaseCredentialView +from .installation_features import panel_feature_allowed from .models import HostingDatabase, PhpMyAdminGrant, ServiceCredentialAccess from .service_credentials import ( CredentialKind, @@ -30,7 +31,7 @@ from .signon import ( def phpmyadmin_available(request): return bool( - settings.PHPMYADMIN_URL + panel_feature_allowed("PHPMYADMIN_URL") and settings.PHPMYADMIN_BRIDGE_KEY and enabled_credential_access(request, CredentialKind.DATABASE) ) diff --git a/src/coulobre/hosting/roundcube.py b/src/coulobre/hosting/roundcube.py index cbc5a61..6006ec0 100644 --- a/src/coulobre/hosting/roundcube.py +++ b/src/coulobre/hosting/roundcube.py @@ -12,6 +12,7 @@ from django.views.decorators.http import require_POST from .access import customer_for_user from .credential_views import MailboxCredentialView +from .installation_features import panel_feature_allowed from .models import HostingMailbox, RoundcubeGrant, ServiceCredentialAccess from .service_credentials import ( CredentialKind, @@ -30,7 +31,7 @@ from .signon import ( def roundcube_available(request): return bool( - settings.ROUNDCUBE_URL + panel_feature_allowed("ROUNDCUBE_URL") and settings.ROUNDCUBE_BRIDGE_KEY and enabled_credential_access(request, CredentialKind.MAILBOX) ) diff --git a/src/coulobre/hosting/templates/hosting/account_backups.html b/src/coulobre/hosting/templates/hosting/account_backups.html index 1ac1d91..ccbc916 100644 --- a/src/coulobre/hosting/templates/hosting/account_backups.html +++ b/src/coulobre/hosting/templates/hosting/account_backups.html @@ -16,7 +16,7 @@
{% endif %} - {% if not account_backup_pending %} + {% if can_request_account_backup and not account_backup_pending %}