From 3b4157f514561cd0ad1105340874c442af562740 Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Sat, 21 Nov 2026 23:05:37 +0100 Subject: [PATCH] Document and verify writable customer support sessions --- README.md | 5 +- docs/development.md | 19 + src/coulobre/hosting/test_support_writes.py | 372 ++++++++++++++++++++ 3 files changed, 394 insertions(+), 2 deletions(-) create mode 100644 src/coulobre/hosting/test_support_writes.py diff --git a/README.md b/README.md index 9258a98..b8a0198 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,9 @@ Coulobre Panel is an open-source web hosting control panel for hosting providers and resellers. The project is in early development. The web panel gives operators a workspace to manage hosting accounts, request -Apache configuration previews, and follow their progress. Administrators with -permission can view the panel as a customer in a read-only support session. +Apache configuration previews, and follow their progress. Administrators +with the appropriate permissions can view the panel as a customer or +perform audited support actions on their behalf. See [Local development](docs/development.md) to run it. See [Single-server deployment](docs/deployment.md) to install it with Apache, MariaDB, and RabbitMQ. diff --git a/docs/development.md b/docs/development.md index c5c9df9..c25f727 100644 --- a/docs/development.md +++ b/docs/development.md @@ -119,6 +119,25 @@ sessions**. 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 + +Grant **Can start a support session with write access** in addition to the +two customer view permissions. Select **Support with write access** next +to the customer. The banner identifies the active customer, administrator, +and mode. + +In a writable session, the administrator can request a database or mailbox +and manage SSH keys on the customer's hosting accounts. Each action also +requires the administrator's normal permission for that operation. The +customer's account scope and plan limits apply. Forms belong to the active +support session. Reopen the form after changing sessions or modes. + +Grant **Can view customer impersonation action** to review these changes in +`/admin/`. Each entry records the session, administrator, customer, account +ID, operation, resource, and time. SSH key changes record fingerprints of +the keys added or removed. Queued changes still report their execution +status in the hosting account. + ## Request a MariaDB database Open a hosting account and select **Request database**. Choose a short suffix diff --git a/src/coulobre/hosting/test_support_writes.py b/src/coulobre/hosting/test_support_writes.py new file mode 100644 index 0000000..5339f82 --- /dev/null +++ b/src/coulobre/hosting/test_support_writes.py @@ -0,0 +1,372 @@ +from unittest.mock import patch + +from django.conf import settings +from django.contrib.auth import get_user_model +from django.contrib.auth.models import Permission +from django.core.files.uploadedfile import SimpleUploadedFile +from django.db import IntegrityError +from django.test import Client, TestCase, override_settings +from django.urls import reverse +from django.utils import timezone + +from coulobre.sftp_keys import public_key_fingerprint + +from .impersonation import SESSION_KEY +from .models import ( + Customer, + CustomerImpersonation, + CustomerImpersonationAction, + HostingAccount, + HostingDatabase, + HostingMailbox, + HostingPlan, + Reseller, + SiteSFTP, + WebActivation, + WebProvisioningPlan, +) +from .test_sftp_keys import public_key +from .views import DatabaseCreate + +Operation = CustomerImpersonationAction.Operation + + +class SupportWriteTests(TestCase): + @classmethod + def setUpTestData(cls): + users = get_user_model().objects + cls.admin = users.create_superuser("operator") + cls.staff = users.create_user("support", is_staff=True) + cls.customer = Customer.objects.create(user=users.create_user("alice")) + cls.account = HostingAccount.objects.create( + name="Alice site", + primary_domain="alice.example", + customer=cls.customer, + hosting_plan=HostingPlan.objects.create( + name="Small", database_limit=1, mailbox_limit=1 + ), + ) + cls.other = HostingAccount.objects.create( + name="Other site", + primary_domain="other.example", + customer=Customer.objects.create(user=users.create_user("other")), + ) + + def setUp(self): + self.client.force_login(self.admin) + self.start_url = reverse( + "panel:impersonation_write_start", args=[self.customer.pk] + ) + self.stop_url = reverse("panel:impersonation_stop") + self.database_url = reverse( + "panel:database_create", args=[self.account.pk] + ) + self.mailbox_url = reverse( + "panel:mailbox_create", args=[self.account.pk] + ) + + def grant(self, *codenames): + self.staff.user_permissions.add( + *Permission.objects.filter( + content_type__app_label="hosting", codename__in=codenames + ) + ) + + def start(self): + self.assertRedirects( + self.client.post(self.start_url), reverse("panel:accounts") + ) + session = CustomerImpersonation.objects.get( + pk=self.client.session[SESSION_KEY] + ) + self.assertTrue(session.write_enabled) + return session + + def test_write_mode_requires_a_separate_permission_and_staff(self): + self.grant("view_customer", "impersonate_customer") + self.client.force_login(self.staff) + url = reverse("panel:customers") + self.assertNotContains( + self.client.get(url), "Support with write access" + ) + self.assertEqual(self.client.post(self.start_url).status_code, 403) + self.grant("write_as_customer") + self.assertContains(self.client.get(url), "Support with write access") + Reseller.objects.create(user=self.staff) + self.assertEqual(self.client.post(self.start_url).status_code, 403) + self.assertFalse(CustomerImpersonation.objects.exists()) + + def test_explicit_session_records_database_and_mailbox_requests(self): + session = self.start() + page = self.client.get(self.database_url) + self.assertContains(page, "Support session with write access") + self.assertContains( + page, f'name="support_session" value="{session.pk}"' + ) + self.assertFalse(page.context["form"].fields["suffix"].disabled) + for url, data in ( + (self.database_url, {"suffix": "blog"}), + (self.mailbox_url, {"local_part": "help"}), + ): + self.assertRedirects( + self.client.post(url, data | {"support_session": session.pk}), + self.account.get_absolute_url(), + ) + actions = CustomerImpersonationAction.objects.filter( + session=session, hosting_account_id=self.account.pk + ) + self.assertEqual( + dict(actions.values_list("operation", "resource")), + { + Operation.DATABASE: HostingDatabase.objects.get().spec.name, + Operation.MAILBOX: "help@alice.example", + }, + ) + self.assertEqual(session.operator_username, "operator") + self.assertEqual(session.customer_username, "alice") + response = self.client.post( + self.database_url, + {"suffix": "extra", "support_session": session.pk}, + ) + self.assertContains(response, "reached its database limit") + self.assertEqual(actions.count(), 2) + + def test_limits_writes_to_the_customer_and_existing_operator_permissions( + self, + ): + self.grant( + "view_customer", + "impersonate_customer", + "write_as_customer", + "view_hostingaccount", + "view_hostingdatabase", + "add_hostingdatabase", + ) + self.client.force_login(self.staff) + session = self.start() + data = {"suffix": "blog", "support_session": session.pk} + self.assertEqual( + self.client.post( + reverse("panel:database_create", args=[self.other.pk]), data + ).status_code, + 404, + ) + self.assertTrue( + self.client.get(self.mailbox_url) + .context["form"] + .fields["local_part"] + .disabled + ) + self.assertEqual( + self.client.post( + self.mailbox_url, + {"local_part": "help", "support_session": session.pk}, + ).status_code, + 403, + ) + self.assertRedirects( + self.client.post(self.database_url, data), + self.account.get_absolute_url(), + ) + self.assertEqual(CustomerImpersonationAction.objects.count(), 1) + + def test_credentials_and_administration_stay_restricted(self): + session = self.start() + self.client.post( + self.database_url, + {"suffix": "blog", "support_session": session.pk}, + ) + database = HostingDatabase.objects.get() + for route, args in ( + ("panel:database_credential_rotation", [database.pk]), + ("panel:account_backup_create", [self.account.pk]), + ("admin:index", []), + ): + with self.subTest(route=route): + self.assertEqual( + self.client.post( + reverse(route, args=args), + {"support_session": session.pk}, + ).status_code, + 403, + ) + self.assertEqual(CustomerImpersonationAction.objects.count(), 1) + + def test_uses_csrf_and_rejects_forms_from_another_mode(self): + self.client = Client(enforce_csrf_checks=True) + self.client.force_login(self.admin) + self.client.get(reverse("panel:customers")) + key = settings.CSRF_COOKIE_NAME + self.assertEqual(self.client.post(self.start_url).status_code, 403) + self.assertEqual( + self.client.post( + self.start_url, + {}, + HTTP_X_CSRFTOKEN=self.client.cookies[key].value, + ).status_code, + 302, + ) + marker = self.client.session[SESSION_KEY] + data = {"suffix": "blog", "support_session": marker} + self.assertEqual( + self.client.post(self.database_url, data).status_code, 403 + ) + csrf = self.client.cookies[key].value + self.assertEqual( + self.client.post( + self.database_url, {"suffix": "blog"}, HTTP_X_CSRFTOKEN=csrf + ).status_code, + 403, + ) + self.assertEqual( + self.client.post( + self.stop_url, {}, HTTP_X_CSRFTOKEN=csrf + ).status_code, + 302, + ) + self.assertEqual( + self.client.post( + self.database_url, + data, + HTTP_X_CSRFTOKEN=self.client.cookies[key].value, + ).status_code, + 403, + ) + self.assertFalse(HostingDatabase.objects.exists()) + + def test_rechecks_session_at_write_boundary_and_rolls_back_on_audit_error( + self, + ): + session = self.start() + data = {"suffix": "blog", "support_session": session.pk} + with ( + patch.object( + CustomerImpersonationAction.objects, + "create", + side_effect=IntegrityError, + ), + self.assertRaises(IntegrityError), + ): + self.client.post(self.database_url, data) + self.assertFalse(HostingDatabase.objects.exists()) + + get_form = DatabaseCreate.get_form + + def end_before_write(view, *args, **kwargs): + form = get_form(view, *args, **kwargs) + CustomerImpersonation.objects.filter(pk=session.pk).update( + ended_at=timezone.now(), + end_reason=CustomerImpersonation.EndReason.STOPPED, + ) + return form + + with patch.object(DatabaseCreate, "get_form", end_before_write): + self.assertEqual( + self.client.post(self.database_url, data).status_code, 403 + ) + self.assertFalse(HostingDatabase.objects.exists()) + self.assertFalse(CustomerImpersonationAction.objects.exists()) + + def test_revoking_write_permission_ends_the_active_session(self): + self.grant( + "view_customer", "impersonate_customer", "write_as_customer" + ) + self.client.force_login(self.staff) + session = self.start() + self.staff.user_permissions.remove( + Permission.objects.get(codename="write_as_customer") + ) + self.assertContains( + self.client.post( + self.mailbox_url, + {"local_part": "help", "support_session": session.pk}, + ), + "Customer view ended", + status_code=409, + ) + session.refresh_from_db() + self.assertEqual(session.end_reason, session.EndReason.ACCESS_REVOKED) + self.assertFalse(HostingMailbox.objects.exists()) + + def test_action_audit_is_read_only_and_requires_view_permission(self): + session = self.start() + self.client.post( + self.database_url, + {"suffix": "blog", "support_session": session.pk}, + ) + event = session.actions.get() + self.client.force_login(self.staff) + audit = reverse("admin:hosting_customerimpersonationaction_changelist") + self.assertEqual(self.client.get(audit).status_code, 403) + self.grant("view_customerimpersonationaction") + response = self.client.get(audit) + for label in ( + "operator", + "alice", + "Requested database", + event.resource, + ): + self.assertContains(response, label) + detail = reverse( + "admin:hosting_customerimpersonationaction_change", args=[event.pk] + ) + self.assertNotContains(self.client.get(detail), 'name="_save"') + self.assertEqual( + self.client.post(detail, {"resource": "changed"}).status_code, 403 + ) + + @override_settings(LOCAL_WEB_SFTP=True) + def test_audits_queued_key_changes_by_fingerprint(self): + plan = WebProvisioningPlan.for_account(self.account) + plan.save() + WebActivation.objects.create(plan=plan, finished_at=timezone.now()) + session = self.start() + url = reverse("panel:sftp_keys", args=[self.account.pk]) + self.assertContains(self.client.get(url), "Upload SSH keys") + key = public_key() + for operation, data, expected in ( + ( + "add", + {"public_keys": SimpleUploadedFile("site.pub", key.encode())}, + Operation.ADD_KEYS, + ), + ( + "remove", + {"fingerprint": public_key_fingerprint(key)}, + Operation.REMOVE_KEY, + ), + ): + self.assertRedirects( + self.client.post( + url, + data + | {"operation": operation, "support_session": session.pk}, + ), + url, + ) + change = session.actions.latest("pk") + field = "added" if operation == "add" else "removed" + self.assertEqual(change.operation, expected) + self.assertEqual( + change.details[field], [public_key_fingerprint(key)] + ) + self.assertNotIn(key, str(change.details)) + SiteSFTP.objects.filter(account=self.account).update( + finished_at=timezone.now() + ) + self.assertEqual( + SiteSFTP.objects.get().requested_customer_id, self.customer.pk + ) + self.assertEqual(session.actions.count(), 2) + self.assertRedirects( + self.client.post( + url, + { + "operation": "remove", + "fingerprint": public_key_fingerprint(key), + "support_session": session.pk, + }, + ), + url, + ) + self.assertEqual(session.actions.count(), 2)