diff --git a/docs/development.md b/docs/development.md index 5bd207a..8e75d0a 100644 --- a/docs/development.md +++ b/docs/development.md @@ -131,7 +131,27 @@ In `/admin/`, users with **Can view customer impersonation session** can review the administrator, customer, timestamps, and outcome for each session. The audit records are read-only in Django admin. -### Make changes during a support session +### Manage hosting on behalf of a customer + +Grant **Can manage hosting as a customer** under **Customer impersonation +sessions**, in addition to the two customer view permissions. In the operator +workspace, open **Show All Users** and select **Manage as customer**. You can +use the customer's hosting tools for their assigned accounts, with the same +feature availability and package limits as the customer. This includes website +management, DNS, email, databases, SFTP, and backups where enabled. + +The operator remains signed in. The support banner and **Leave impersonation** +stay available while managing hosting. Personal account settings, sign-in +credentials, MFA, existing service credential reveals, and SSO are not +delegated. Service password rotation, where enabled, requires the operator's +own password and the normal service credential permissions. + +Managed hosting submissions are audited with the operator and customer +session, route, HTTP outcome, and numeric resource identifiers when available. +The audit does not include form values or credentials. A queued submission's +HTTP outcome is not its eventual execution result. + +### Make limited changes during a support session Grant **Can start a support session with write access** in addition to the two customer view permissions. In the operator workspace, open the legacy diff --git a/docs/panel-design.md b/docs/panel-design.md index 2957ec0..f1d33d4 100644 --- a/docs/panel-design.md +++ b/docs/panel-design.md @@ -104,7 +104,8 @@ heading, help, fields, and actions should share a left edge. `` styled as a button. An action that submits the change uses a native ` + + {% endif %} {% if may_impersonate %}
{% csrf_token %} diff --git a/src/coulobre/hosting/templates/hosting/base.html b/src/coulobre/hosting/templates/hosting/base.html index 7e2822f..d0dc29f 100644 --- a/src/coulobre/hosting/templates/hosting/base.html +++ b/src/coulobre/hosting/templates/hosting/base.html @@ -83,7 +83,9 @@ {% if request.impersonation %}
Viewing as {{ request.impersonation.customer }} - {% if request.impersonation.write_enabled %} + {% if request.impersonation.managed_enabled %} +

Managing customer hosting · {{ request.impersonation.operator_username }}

+ {% elif request.impersonation.write_enabled %}

Support session with write access · {{ request.impersonation.operator_username }}

{% else %}

Read-only support session

diff --git a/src/coulobre/hosting/templates/hosting/customer_certificates.html b/src/coulobre/hosting/templates/hosting/customer_certificates.html index e64b28c..192715d 100644 --- a/src/coulobre/hosting/templates/hosting/customer_certificates.html +++ b/src/coulobre/hosting/templates/hosting/customer_certificates.html @@ -3,7 +3,7 @@ {% block page_title %}SSL Certificates{% endblock %} {% block page_metadata %}

{{ account.primary_domain }}

{% endblock %} {% block page_actions %} - {% if not request.impersonation %}HTTPS by hostname{% endif %} + {% if not request.impersonation or request.impersonation.managed_enabled %}HTTPS by hostname{% endif %} {% endblock %} {% block page_tabs %} {% if sites %} diff --git a/src/coulobre/hosting/templates/hosting/mail_dns_check.html b/src/coulobre/hosting/templates/hosting/mail_dns_check.html index 9977cfc..ae0005e 100644 --- a/src/coulobre/hosting/templates/hosting/mail_dns_check.html +++ b/src/coulobre/hosting/templates/hosting/mail_dns_check.html @@ -13,7 +13,7 @@ ← {{ account.name }} {% endblock %} {% block page_content %} - {% if not request.impersonation %} + {% if not request.impersonation or request.impersonation.managed_enabled %}
{% csrf_token %} diff --git a/src/coulobre/hosting/test_impersonation.py b/src/coulobre/hosting/test_impersonation.py index 9bbce88..452bb09 100644 --- a/src/coulobre/hosting/test_impersonation.py +++ b/src/coulobre/hosting/test_impersonation.py @@ -1,10 +1,11 @@ from datetime import timedelta +from unittest.mock import patch from django.conf import settings from django.contrib.auth import SESSION_KEY as AUTH_SESSION_KEY from django.contrib.auth import get_user_model from django.contrib.auth.models import Permission -from django.test import Client, TestCase +from django.test import Client, TestCase, override_settings from django.urls import reverse from django.utils import timezone from django_otp.plugins.otp_totp.models import TOTPDevice @@ -15,8 +16,12 @@ from .models import ( CustomerImpersonation, HostingAccount, HostingDatabase, + HostingDnsZone, HostingMailbox, + HostingMailDomain, + MailboxProvisioning, Reseller, + ServiceCredentialAccess, ) @@ -67,6 +72,193 @@ class CustomerImpersonationTests(TestCase): pk=self.client.session[SESSION_KEY] ) + def start_managed(self): + url = reverse( + "panel:impersonation_managed_start", args=[self.customer.pk] + ) + response = self.client.post(url) + self.assertRedirects( + response, self.accounts, fetch_redirect_response=False + ) + self.assertIn("no-store", response.headers["Cache-Control"]) + return CustomerImpersonation.objects.get( + pk=self.client.session[SESSION_KEY] + ) + + def test_managed_session_can_make_customer_changes_and_audits_them(self): + self.assertContains( + self.client.get(self.all_users), "Manage as customer" + ) + support = self.start_managed() + self.assertTrue(support.managed_enabled) + page = self.client.get(self.accounts) + self.assertContains(page, "Managing customer hosting") + self.assertNotContains(page, "Add hosting account") + self.assertNotContains(page, self.other_account.name) + self.assertContains(page, "Leave impersonation") + url = reverse("panel:database_create", args=[self.account.pk]) + other = reverse("panel:database_create", args=[self.other_account.pk]) + self.assertNotEqual( + self.client.post(other, {"suffix": "denied"}).status_code, 302 + ) + form = self.client.get(url).context["form"] + self.assertFalse(form.fields["suffix"].disabled) + response = self.client.post(url, {"suffix": "application"}) + self.assertEqual(response.status_code, 302) + self.assertTrue( + self.account.databases.filter(suffix="application").exists() + ) + self.assertFalse(self.other_account.databases.exists()) + action = support.actions.get(details__status=302) + self.assertEqual(action.resource, "database_create") + self.assertEqual( + action.details, + { + "ids": {"pk": self.account.pk}, + "status": 302, + }, + ) + self.assertRedirects(self.client.post(self.stop_url), self.all_users) + self.assertEqual( + self.client.session[AUTH_SESSION_KEY], str(self.operator.pk) + ) + + def test_managed_dns_edit_respects_customer_delegation(self): + zone = HostingDnsZone.objects.create( + account=self.account, + domain=self.account.primary_domain, + nameservers=["ns.provider.example"], + responsible_name="hostmaster.alice.example", + ) + self.start_managed() + show = reverse("panel:dns_zone_edit", args=[zone.pk]) + self.assertEqual(self.client.get(show).status_code, 403) + zone.delegate_dns = True + zone.save(update_fields=["delegate_dns"]) + self.assertEqual(self.client.get(show).status_code, 200) + self.assertRedirects( + self.client.post( + show, + { + "serial": zone.serial, + "nameservers": "ns.unapproved.example", + "addresses": "alice.example 192.0.2.1", + }, + ), + self.account.get_absolute_url(), + ) + zone.refresh_from_db() + self.assertEqual(zone.nameservers, ["ns.provider.example"]) + self.assertEqual(len(zone.addresses), 1) + + def test_managed_session_denies_account_identity_and_operator_changes( + self, + ): + self.start_managed() + for name, args in ( + ("panel:account_security", []), + ("panel:account_password", []), + ("panel:account_authenticator", []), + ("panel:all_users", []), + ("panel:account_edit", [self.account.pk]), + ("panel:impersonation_start", [self.customer.pk]), + ("panel:switch_panel_role", []), + ): + with self.subTest(route=name): + url = reverse(name, args=args) + self.assertEqual(self.client.get(url).status_code, 403) + self.assertEqual(self.client.post(url).status_code, 403) + + def test_managed_permission_is_separate_and_revocation_ends_session(self): + self.grant_support_permissions( + self.support, + "view_customer", + "impersonate_customer", + "write_as_customer", + ) + self.client.force_login(self.support) + start = reverse( + "panel:impersonation_managed_start", args=[self.customer.pk] + ) + self.assertNotContains(self.client.get(self.all_users), start) + self.assertEqual(self.client.post(start).status_code, 403) + self.grant_support_permissions(self.support, "manage_as_customer") + managed = self.start_managed() + self.support.user_permissions.remove( + Permission.objects.get(codename="manage_as_customer") + ) + self.assertEqual( + self.client.post( + reverse("panel:database_create", args=[self.account.pk]), + {"suffix": "denied"}, + ).status_code, + 409, + ) + managed.refresh_from_db() + self.assertEqual(managed.end_reason, managed.EndReason.ACCESS_REVOKED) + self.assertFalse(self.account.databases.exists()) + + @override_settings( + LOCAL_MAILBOX_PROVISIONING=True, + MAILBOX_PASSWORD_CHANGES=True, + REMOTE_MAILBOX_CREDENTIALS=True, + REMOTE_MAILBOX_CREDENTIAL_ROTATION=True, + ) + def test_managed_mailbox_rotation_requires_operator_password(self): + self.operator.set_password("operator-password") + self.operator.save(update_fields=["password"]) + self.alice.set_password("customer-password") + self.alice.save(update_fields=["password"]) + self.client.force_login(self.operator) + domain = HostingMailDomain.objects.create( + account=self.account, domain=self.account.primary_domain + ) + mailbox = HostingMailbox.objects.create( + mail_domain=domain, local_part="support" + ) + MailboxProvisioning.objects.create( + mailbox=mailbox, finished_at=timezone.now() + ) + self.start_managed() + self.assertEqual( + self.client.get( + reverse("panel:mailbox_credentials", args=[mailbox.pk]) + ).status_code, + 403, + ) + url = reverse("panel:mailbox_credential_rotation", args=[mailbox.pk]) + with patch( + "coulobre.hosting.service_credentials.apply_on_local_node", + side_effect=[ + {"generation": None}, + { + "user": str(mailbox), + "password": "new-mailbox-password", + }, + ], + ) as node: + page = self.client.get(url, secure=True) + self.assertEqual(page.status_code, 200) + ticket = page.context["form"]["ticket"].value() + result = self.client.post( + url, + {"ticket": ticket, "password": "customer-password"}, + secure=True, + ) + self.assertContains(result, "Enter your current panel password.") + self.assertEqual(node.call_count, 1) + result = self.client.post( + url, + {"ticket": ticket, "password": "operator-password"}, + secure=True, + ) + self.assertContains(result, "new-mailbox-password") + self.assertIn("no-store", result.headers["Cache-Control"]) + self.assertEqual( + ServiceCredentialAccess.objects.get(outcome="rotated").actor, + self.operator, + ) + def test_operator_starts_from_all_users_and_leaves_in_sidebar(self): response = self.client.get(self.all_users) self.assertContains(response, f'action="{self.start_url}"', count=1) @@ -128,7 +320,13 @@ class CustomerImpersonationTests(TestCase): self.client.get(self.accounts, {"customer": self.customer.pk}), self.account.name, ) - for route in (self.start_url, self.write_start_url): + for route in ( + self.start_url, + self.write_start_url, + reverse( + "panel:impersonation_managed_start", args=[self.customer.pk] + ), + ): self.assertEqual(self.client.post(route).status_code, 403) def test_operator_cannot_impersonate_in_another_workspace(self): diff --git a/src/coulobre/hosting/urls.py b/src/coulobre/hosting/urls.py index 8597d97..1e68d00 100644 --- a/src/coulobre/hosting/urls.py +++ b/src/coulobre/hosting/urls.py @@ -685,6 +685,11 @@ urlpatterns = [ views.CustomerImpersonationStart.as_view(write_enabled=True), name="impersonation_write_start", ), + path( + "customers//manage-as/", + views.CustomerImpersonationStart.as_view(managed_enabled=True), + name="impersonation_managed_start", + ), path( "customer-view/end/", views.CustomerImpersonationStop.as_view(), diff --git a/src/coulobre/hosting/views.py b/src/coulobre/hosting/views.py index 9e53b84..3813cb0 100644 --- a/src/coulobre/hosting/views.py +++ b/src/coulobre/hosting/views.py @@ -89,8 +89,10 @@ from .impersonation import ( EndReason, can_edit_customer_resource, can_impersonate_customer, + can_start_managed_support, can_start_writable_support, end_impersonation, + restricted_preview, start_impersonation, support_write_session, ) @@ -699,7 +701,7 @@ class AccountDetail(TenantScopeMixin, PermissionRequiredMixin, DetailView): ) context["manage_wordpress"] = wordpress_available() and ( - getattr(self.request, "impersonation", None) is None + not restricted_preview(self.request) and can_manage_wordpress(user, self.object) ) context["manage_website_jobs"] = can_access_website_jobs( @@ -715,7 +717,7 @@ class AccountDetail(TenantScopeMixin, PermissionRequiredMixin, DetailView): website_provisioning and not self.object.services_suspended and can_manage_hosting(user, (CHANGE_ACCOUNTS,)) - and getattr(self.request, "impersonation", None) is None + and not restricted_preview(self.request) ) context["independent_websites"] = [ (website, website_actions(website) if can_manage else ()) @@ -893,6 +895,14 @@ class AccountDetail(TenantScopeMixin, PermissionRequiredMixin, DetailView): "apache_config" ).order_by("-requested_at", "-pk") context["apache_history"] = list(history[:10]) + context["show_mailbox_rotation"] = show_rotation_action( + self.request, CredentialKind.MAILBOX + ) + context["show_database_rotation"] = getattr( + self.request, "impersonation", None + ) is not None and show_rotation_action( + self.request, CredentialKind.DATABASE + ) if getattr(self.request, "impersonation", None) is None: context["show_database_credentials"] = show_credential_action( self.request, CredentialKind.DATABASE @@ -900,9 +910,6 @@ class AccountDetail(TenantScopeMixin, PermissionRequiredMixin, DetailView): context["show_mailbox_credentials"] = show_credential_action( self.request, CredentialKind.MAILBOX ) - context["show_mailbox_rotation"] = show_rotation_action( - self.request, CredentialKind.MAILBOX - ) context["show_phpmyadmin"] = context[ "show_database_credentials" ] and phpmyadmin_available(self.request) @@ -926,10 +933,7 @@ class AccountDetail(TenantScopeMixin, PermissionRequiredMixin, DetailView): context["databases"] = databases if tenant or user.has_perm(VIEW_DNS_ZONES): zone_list = self.object.dns_zones.all() - if ( - customer - and getattr(self.request, "impersonation", None) is None - ): + if customer and not restricted_preview(self.request): zone_list = zone_list.select_related("publication").defer( "publication__manifest" ) @@ -1060,9 +1064,7 @@ class AccountDetail(TenantScopeMixin, PermissionRequiredMixin, DetailView): context["change_mailbox_quota"] = ( not tenant and user.has_perms(CHANGE_MAILBOX_QUOTA) ) - elif customer is not None and not getattr( - self.request, "impersonation", None - ): + elif customer is not None and not restricted_preview(self.request): context["customer_mailbox_quota"] = ( panel_feature_allowed("LOCAL_MAILBOX_PROVISIONING") and customer_mailbox_quota_limit(self.object) is not None @@ -2096,8 +2098,11 @@ class CustomerAccess(OperatorOnlyMixin, PermissionRequiredMixin, View): class CustomerImpersonationStart(PermissionRequiredMixin, View): write_enabled = False + managed_enabled = False def has_permission(self): + if self.managed_enabled: + return can_start_managed_support(self.request.user) if self.write_enabled: return can_start_writable_support(self.request.user) return can_impersonate_customer(self.request.user) @@ -2108,7 +2113,10 @@ class CustomerImpersonationStart(PermissionRequiredMixin, View): if not customer.may_be_impersonated: raise Http404 start_impersonation( - request, customer, write_enabled=self.write_enabled + request, + customer, + write_enabled=self.write_enabled, + managed_enabled=self.managed_enabled, ) return redirect("panel:accounts") @@ -2438,7 +2446,7 @@ class DnsZoneEdit(DnsZoneEditor): if reseller_for_user(self.request.user) is not None: return False if is_tenant(self.request.user): - return getattr(self.request, "impersonation", None) is None and ( + return not restricted_preview(self.request) and ( self.zone.delegate_dns and manages_account(self.request.user, self.account) ) @@ -2588,7 +2596,7 @@ class RequestDnsPublication(PermissionRequiredMixin, View): if reseller_for_user(self.request.user) is not None: return False if is_tenant(self.request.user): - return getattr(self.request, "impersonation", None) is None + return not restricted_preview(self.request) return super().has_permission() def post(self, request, pk): @@ -2628,9 +2636,7 @@ class MailboxCreate(ResourceRequestCreate): domain = self.request.GET.get("domain") if domain is None: return None - if self.customer is None or getattr( - self.request, "impersonation", None - ): + if self.customer is None or restricted_preview(self.request): raise Http404 from .domain_policies import managed_mail_domains from .domain_services import allowed_domains @@ -2702,7 +2708,7 @@ class MailboxQuotaEdit(PermissionRequiredMixin, UpdateView): return customer_for_user(self.request.user) def has_permission(self): - if getattr(self.request, "impersonation", None): + if restricted_preview(self.request): return False if self.customer is not None: return panel_feature_allowed("LOCAL_MAILBOX_PROVISIONING") diff --git a/src/coulobre/hosting/website_file_views.py b/src/coulobre/hosting/website_file_views.py index 4990237..d9ba13e 100644 --- a/src/coulobre/hosting/website_file_views.py +++ b/src/coulobre/hosting/website_file_views.py @@ -24,6 +24,7 @@ from .access import ( can_manage_hosting, owns_customer_operations, ) +from .impersonation import restricted_preview from .independent_websites import active_websites from .installation_features import panel_feature_allowed from .models import HostingAccount @@ -39,7 +40,7 @@ def can_manage_website_files(request): user = request.user return ( panel_feature_allowed("WEB_FILE_MANAGER") - and getattr(request, "impersonation", None) is None + and not restricted_preview(request) and can_manage_hosting(user, MANAGE_FILES) ) diff --git a/src/coulobre/hosting/website_https_views.py b/src/coulobre/hosting/website_https_views.py index 946944c..12bf866 100644 --- a/src/coulobre/hosting/website_https_views.py +++ b/src/coulobre/hosting/website_https_views.py @@ -16,6 +16,7 @@ from coulobre.domain_names import canonical_domain from .access import customer_for_user, is_tenant from .certificate_views import account_certificate_sites, queue_certificate from .dns_forms import record_rows +from .impersonation import restricted_preview from .independent_websites import account_websites from .installation_features import panel_feature_allowed from .models import HostingAccount, WebsiteAlias @@ -48,7 +49,7 @@ NOT_MAPPED = HostStatus("", "Not mapped to a website") def may_manage_https(request, account): - if getattr(request, "impersonation", None): + if restricted_preview(request): return False customer = customer_for_user(request.user) if customer is not None: diff --git a/src/coulobre/hosting/website_job_views.py b/src/coulobre/hosting/website_job_views.py index 9f2269c..04e816e 100644 --- a/src/coulobre/hosting/website_job_views.py +++ b/src/coulobre/hosting/website_job_views.py @@ -20,6 +20,7 @@ from .access import ( can_manage_hosting, owns_customer_operations, ) +from .impersonation import restricted_preview from .independent_websites import check_actor from .installation_features import panel_feature_allowed from .models import HostingAccount @@ -35,7 +36,7 @@ MANAGE_JOBS = ("hosting.view_hostingaccount", "hosting.manage_website_jobs") def can_access_website_jobs(request): - impersonating = getattr(request, "impersonation", None) is not None + impersonating = restricted_preview(request) return not impersonating and can_manage_hosting(request.user, MANAGE_JOBS) diff --git a/src/coulobre/hosting/wordpress_views.py b/src/coulobre/hosting/wordpress_views.py index 881aa5d..67b22a3 100644 --- a/src/coulobre/hosting/wordpress_views.py +++ b/src/coulobre/hosting/wordpress_views.py @@ -14,6 +14,7 @@ from coulobre.wordpress_import import inspect_export from coulobre.wordpress_updates import WordPressUpdate from .access import TenantScopeMixin, customer_for_user, is_tenant +from .impersonation import restricted_preview from .installation_features import panel_feature_allowed from .models import HostingAccount from .wordpress_import_workflow import ( @@ -136,7 +137,7 @@ class AccountWordPressView( def has_permission(self): return ( - not getattr(self.request, "impersonation", None) + not restricted_preview(self.request) and super().has_permission() and can_manage_wordpress(self.request.user, self.get_object()) ) diff --git a/src/coulobre/hosting/workspaces.py b/src/coulobre/hosting/workspaces.py index 3757003..0e4c389 100644 --- a/src/coulobre/hosting/workspaces.py +++ b/src/coulobre/hosting/workspaces.py @@ -147,6 +147,12 @@ def customer_pages(request): return pages +def customer_account_key(request): + if getattr(request, "impersonation", None): + return "support_hosting_account" + return CUSTOMER_ACCOUNT + + @require_safe @never_cache def customer_workspace(request, page): @@ -165,10 +171,10 @@ def customer_workspace(request, page): ): raise Http404 account = get_object_or_404(accounts, pk=int(selected)) - request.session[CUSTOMER_ACCOUNT] = account.pk + request.session[customer_account_key(request)] = account.pk else: account = accounts.filter( - pk=request.session.get(CUSTOMER_ACCOUNT) + pk=request.session.get(customer_account_key(request)) ).first() account = account or accounts.first() if account is None: @@ -336,7 +342,9 @@ def groups_for(request, *, customer, manager): user = request.user if not user.is_authenticated: return () - if getattr(request, "impersonation", None): + from .impersonation import restricted_preview + + if restricted_preview(request): return (("Account Manager", [link("accounts", "My hosting")]),) if customer: pages = customer_pages(request) @@ -378,7 +386,11 @@ def groups_for(request, *, customer, manager): "Advanced Features", [ *hosted("backups", "tasks"), - link("account_security", "Security"), + *( + [] + if getattr(request, "impersonation", None) + else [link("account_security", "Security")] + ), ], ), ]