diff --git a/docs/deployment.md b/docs/deployment.md index 316a713..eb0b8c4 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -1158,6 +1158,52 @@ could activate new resources and restore requests wait until service access is active and confirmed again. Website files, mail, database contents, certificates, and storage limits are retained throughout. +## Retire a hosting account + +Release covers resources recorded as provisioned or restored through the +panel. If you applied a manifest directly on a node, first run the matching +panel provisioning action to record it. Suspend the account and finish its +pending service, backup, restore, and credential operations. Open **Retire account** to review the managed +website and aliases, certificate requests, SSH key fingerprints, databases, +mailboxes, DNS zones, and backup archives. An operator with **Can retire +hosting accounts** confirms the primary domain shown in the preview. +Retirement detaches the customer's and reseller's access to this hosting +account. It stops the account's backup schedule and automatic archive expiry. + +- **Keep names reserved** leaves the suspended resources in place and keeps + published DNS and incoming mail available. The names remain assigned to the + retired account. Operators can choose to release them later. +- **Release names and retain data** removes the managed Apache sites, Certbot + lineages, SFTP keys, mail delivery and lookup entries, and authoritative BIND + zones. The owned database stays locked under its original account identifier. + Website files and mail are moved to private archives on their service nodes. + +The release uses the provisioning workers already configured for the account, +including remote mail and DNS. Bring the panel and remote nodes to the same +package version. An alias is removed before its primary website. A failed +cleanup leaves the names reserved. Check the error and choose **Retry failed +cleanup** to continue the same operation. The panel releases the hostname, +alias, DNS, and mailbox reservations after every node has confirmed cleanup. +New databases use the new account's identifier. Resolver caches may continue +serving previous DNS answers until their TTLs expire. + +**Retired accounts** lists the original account identity, the policy, +node results, and the operator history. The completed node operations show +the archive reference, which is the operation UUID used in the paths below. + +| Retained material | Location on its node | +| --- | --- | +| Website files | `/srv/coulobre/web/.retired//` | +| Website configuration and SFTP keys | `/var/lib/coulobre/web-node/.retired//` | +| Mailbox data | `/srv/coulobre-mail/.retired//` | +| Mailbox credential registry | `/var/lib/coulobre/mail-node/.retired//` | +| Unpublished zone file | `/etc/bind/coulobre/.retired//zone` | +| Database contents | Original MariaDB schema, accessible to the node administrator | + +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. + ## Monitor local and remote services The panel's **Service health** page shows the latest observation and timestamp diff --git a/src/coulobre/hosting/test_account_retirement.py b/src/coulobre/hosting/test_account_retirement.py new file mode 100644 index 0000000..cca7fe6 --- /dev/null +++ b/src/coulobre/hosting/test_account_retirement.py @@ -0,0 +1,201 @@ +from unittest.mock import patch + +from django.contrib.auth import get_user_model +from django.test import TestCase, override_settings +from django.urls import reverse +from django.utils import timezone + +from .account_retirement import account_inventory, inventory_digest +from .models import ( + AccountRetirementJob, + Customer, + DnsPublication, + HostingAccount, + HostingDnsZone, + HostingMailbox, + HostingMailDomain, + MailboxProvisioning, +) +from .service_changes import request_service_change +from .tasks import ( + dispatch_account_retirements, + retire_account_resource, +) + + +@override_settings(LOCAL_DNS_PUBLISHING=True, LOCAL_MAILBOX_PROVISIONING=True) +class AccountRetirementTests(TestCase): + @classmethod + def setUpTestData(cls): + users = get_user_model().objects + cls.operator = users.create_superuser("operator") + cls.user = users.create_user("customer") + customer = Customer.objects.create(user=cls.user) + cls.account = HostingAccount.objects.create( + name="Example", primary_domain="example.net", customer=customer + ) + cls.other = HostingAccount.objects.create( + name="Other", primary_domain="other.net" + ) + cls.zone = HostingDnsZone.objects.create( + account=cls.account, + domain="example.net", + nameservers=["ns1.provider.net"], + responsible_name="hostmaster.example.net", + ) + DnsPublication.objects.create( + zone=cls.zone, + manifest=cls.zone.spec.manifest(), + serial=cls.zone.serial, + published_serial=cls.zone.serial, + finished_at=timezone.now(), + ) + domain = HostingMailDomain.objects.create( + account=cls.account, domain="example.net" + ) + cls.mailbox = HostingMailbox.objects.create( + mail_domain=domain, local_part="help", quota_mib=10 + ) + MailboxProvisioning.objects.create( + mailbox=cls.mailbox, finished_at=timezone.now() + ) + + def setUp(self): + self.url = reverse("panel:account_retirement", args=[self.account.pk]) + self.client.force_login(self.operator) + + def preview(self): + return self.client.get(self.url).context["form"]["digest"].value() + + def retire(self, policy, *, digest=None): + return self.client.post( + self.url, + { + "policy": policy, + "confirmation": "example.net", + "digest": digest or self.preview(), + }, + ) + + def suspend(self): + request_service_change(self.account.pk, "suspend", "operator") + self.account.refresh_from_db() + self.account.service_changes.first().jobs.update( + finished_at=timezone.now(), applied_suspended=True + ) + + def test_scope_and_confirmed_suspension_are_required(self): + preview = self.client.get(self.url) + self.assertContains(preview, "help@example.net") + self.assertContains(preview, "published serial") + self.assertContains( + self.retire("release"), "Suspend the account", status_code=409 + ) + self.client.force_login(self.user) + self.assertEqual(self.client.get(self.url).status_code, 403) + self.assertEqual(self.client.post(self.url).status_code, 403) + + def test_changed_inventory_requires_another_confirmation(self): + self.suspend() + digest = self.preview() + self.zone.serial += 1 + self.zone.save(update_fields=["serial"]) + self.assertContains( + self.retire("hold", digest=digest), + "on a current retirement preview", + status_code=409, + ) + self.assertFalse(hasattr(self.account, "retirement")) + + def test_reserves_names_until_all_cleanup_replies_are_confirmed(self): + self.suspend() + self.assertRedirects(self.retire("hold"), self.url) + self.assertContains(self.client.get(self.url), "Names reserved") + self.client.force_login(self.user) + account = self.account.get_absolute_url() + self.assertEqual(self.client.get(account).status_code, 404) + + self.client.force_login(self.operator) + self.assertRedirects(self.retire("release"), self.url) + retirement = self.account.retirement + mail = retirement.jobs.get(kind=AccountRetirementJob.Kind.MAILBOX) + dns = retirement.jobs.get(kind=AccountRetirementJob.Kind.DNS) + identity = mail.operation_id + with ( + patch.object(retire_account_resource, "apply_async") as publish, + patch( + "coulobre.hosting.account_retirement.apply_on_local_node", + side_effect=[None, OSError, None], + ) as node, + ): + dispatch_account_retirements.run() + publish.assert_any_call( + args=[dns.pk], queue="coulobre-dns-publishing", retry=False + ) + publish.assert_any_call( + args=[mail.pk], + queue="coulobre-mailbox-provisioning", + retry=False, + ) + retire_account_resource.run(dns.pk) + retire_account_resource.run(mail.pk) + dispatch_account_retirements.run() + retirement.refresh_from_db() + self.assertIsNone(retirement.released_at) + self.assertTrue( + HostingAccount.objects.filter( + primary_domain="example.net" + ).exists() + ) + self.assertContains( + self.client.get(self.url), "Retry failed cleanup" + ) + + self.assertRedirects( + self.client.post(self.url, {"action": "retry"}), self.url + ) + mail.refresh_from_db() + self.assertEqual(mail.operation_id, identity) + retire_account_resource.run(mail.pk) + dispatch_account_retirements.run() + self.assertEqual(node.call_count, 3) + self.assertEqual( + node.call_args_list[1].args[0], node.call_args_list[2].args[0] + ) + retirement.refresh_from_db() + self.assertIsNotNone(retirement.released_at) + self.assertEqual( + [event["action"] for event in retirement.history], + ["hold", "release", "retry"], + ) + self.assertContains(self.client.get(self.url), "Names released") + self.account.refresh_from_db() + self.assertIsNone(self.account.primary_domain) + self.assertFalse( + HostingDnsZone.objects.filter(pk=self.zone.pk).exists() + ) + self.assertFalse( + HostingMailbox.objects.filter(pk=self.mailbox.pk).exists() + ) + replacement = HostingAccount.objects.create( + name="New", primary_domain="example.net" + ) + self.assertNotEqual(replacement.pk, self.account.pk) + self.assertTrue( + HostingAccount.objects.filter(pk=self.other.pk).exists() + ) + + def test_empty_account_can_be_retired_and_released(self): + other_url = reverse("panel:account_retirement", args=[self.other.pk]) + self.assertRedirects( + self.client.post( + other_url, + { + "policy": "release", + "confirmation": "other.net", + "digest": inventory_digest(account_inventory(self.other)), + }, + ), + other_url, + ) + self.assertTrue(self.other.retirement.released_at)