|
|
|
@@ -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)
|