diff --git a/deploy/panel.env.example b/deploy/panel.env.example index 611871a..febafb9 100644 --- a/deploy/panel.env.example +++ b/deploy/panel.env.example @@ -15,3 +15,4 @@ COULOBRE_BROKER_URL=amqp://coulobre:@127.0.0.1:5672/coulobre # COULOBRE_LOCAL_DNS_PUBLISHING=1 # COULOBRE_LOCAL_MAILBOX_PROVISIONING=1 # COULOBRE_LOCAL_ACCOUNT_BACKUPS=1 +# COULOBRE_LOCAL_ACCOUNT_BACKUP_MAIL=1 diff --git a/deploy/systemd/coulobre-account-backup@.service.d/mail.conf b/deploy/systemd/coulobre-account-backup@.service.d/mail.conf new file mode 100644 index 0000000..e6dfb05 --- /dev/null +++ b/deploy/systemd/coulobre-account-backup@.service.d/mail.conf @@ -0,0 +1,3 @@ +[Service] +Environment=COULOBRE_LOCAL_ACCOUNT_BACKUP_MAIL=1 +ReadWritePaths=-/srv/coulobre-mail diff --git a/docs/deployment.md b/docs/deployment.md index a84659c..8d346a8 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -396,6 +396,25 @@ Archives are stored under `/var/lib/coulobre/account-backups/` with root-only access. Retrieve archives and manage their retention on the server. Recovery uses the existing [account bundle commands](development.md#recover-backups-from-the-bundle). +#### Include mailboxes on the same server + +Complete the [Postfix and Dovecot setup](development.md#apply-a-mailbox-manifest) +on the same server. Install the service drop-in so the backup service can +create its temporary Maildir copies on the mail volume. + +```sh +sudo install -D -m 0644 deploy/systemd/coulobre-account-backup@.service.d/mail.conf \ + /etc/systemd/system/coulobre-account-backup@.service.d/mail.conf +sudo systemctl daemon-reload +``` + +Set `COULOBRE_LOCAL_ACCOUNT_BACKUP_MAIL=1` in `/etc/coulobre/panel.env` and +restart `coulobre-panel` and `coulobre-backup-worker`. Operators with +permission to view mailbox requests can select **Include mailboxes** when +creating an account backup. Provision all listed mailboxes before requesting +a backup with mail. For mailboxes on a separate server, use the +[bundle workflow for separate nodes](development.md#bundle-backups-from-separate-nodes). + ## Publish local DNS zones from the panel Enable the [DNS publishing socket](development.md#enable-local-dns-publishing) diff --git a/docs/development.md b/docs/development.md index 02aaec3..0c2120f 100644 --- a/docs/development.md +++ b/docs/development.md @@ -902,8 +902,10 @@ each listed database, and the requested mailbox archives. Back up DNS and panel settings separately. On a single-server installation, operators can start a backup of the website -and databases directly from the account page and view its status there. -[Enable account backups in the panel](deployment.md#start-local-account-backups-from-the-panel) +and databases directly from the account page and view its status there. If +mail is hosted on that server too, enable the **Include mailboxes** option. +[Enable account backups in the panel](deployment.md#start-local-account-backups-from-the-panel), +[include local mailboxes](deployment.md#include-mailboxes-on-the-same-server) If all resources in the manifest are on one server, run the account backup command there as root. To include mail, pass the manifest downloaded with diff --git a/src/coulobre/account_backup_request.py b/src/coulobre/account_backup_request.py index 9b3b664..df75c80 100644 --- a/src/coulobre/account_backup_request.py +++ b/src/coulobre/account_backup_request.py @@ -39,12 +39,7 @@ class AccountBackupRequest: backup_id = UUID(data["backup_id"]) if str(backup_id) != data["backup_id"]: raise ValueError("Expected a canonical backup ID.") - backup = AccountBackup.from_manifest(data["backup"]) - if backup.mailboxes: - raise ValueError( - "Use the account backup CLI to include mailboxes." - ) - return cls(backup_id, backup) + return cls(backup_id, AccountBackup.from_manifest(data["backup"])) def manifest(self): return { @@ -80,6 +75,11 @@ def create_requested_backup(request): if os.geteuid() != 0: raise ValueError("Run create-request as root on the backup node.") + if ( + request.backup.mailboxes is not None + and os.environ.get("COULOBRE_LOCAL_ACCOUNT_BACKUP_MAIL") != "1" + ): + raise ValueError("Enable mailbox backups on the local backup node.") require_root_directory(BACKUP_DIRECTORY) directory = os.open( BACKUP_DIRECTORY, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW diff --git a/src/coulobre/hosting/account_backups.py b/src/coulobre/hosting/account_backups.py index 8cf589d..d8d2731 100644 --- a/src/coulobre/hosting/account_backups.py +++ b/src/coulobre/hosting/account_backups.py @@ -22,7 +22,11 @@ def account_backup_manifest(account, *, include_mailboxes=False): @transaction.atomic -def queue_account_backup(account_id): +def queue_account_backup(account_id, *, include_mailboxes=False): account = lock_hosting_account(account_id, customer_id=None) if not account.backup_jobs.filter(finished_at__isnull=True).exists(): - account.backup_jobs.create(manifest=account_backup_manifest(account)) + account.backup_jobs.create( + manifest=account_backup_manifest( + account, include_mailboxes=include_mailboxes + ) + ) diff --git a/src/coulobre/hosting/models.py b/src/coulobre/hosting/models.py index 9b3a2fd..8f12bd8 100644 --- a/src/coulobre/hosting/models.py +++ b/src/coulobre/hosting/models.py @@ -392,6 +392,10 @@ class AccountBackupJob(ProvisioningJob): def archive(self): return str(backup_archive(self.account_id, self.backup_id)) + @property + def includes_mailboxes(self): + return "mailboxes" in self.manifest + class DnsPublication(ProvisioningJob): zone = models.OneToOneField( diff --git a/src/coulobre/hosting/tasks.py b/src/coulobre/hosting/tasks.py index 96077cc..485b3dd 100644 --- a/src/coulobre/hosting/tasks.py +++ b/src/coulobre/hosting/tasks.py @@ -295,6 +295,8 @@ def apply_account_backup(job): backup = AccountBackup.from_manifest(job.manifest) if backup.website.account_id != job.account_id: raise ValueError("The backup manifest belongs to a different account.") + if backup.mailboxes is not None and not settings.LOCAL_ACCOUNT_BACKUP_MAIL: + raise ValueError("Enable local mailbox backups before trying again.") create_local_account_backup(AccountBackupRequest(job.backup_id, backup)) diff --git a/src/coulobre/hosting/templates/hosting/account_backups.html b/src/coulobre/hosting/templates/hosting/account_backups.html index bb05241..108b306 100644 --- a/src/coulobre/hosting/templates/hosting/account_backups.html +++ b/src/coulobre/hosting/templates/hosting/account_backups.html @@ -5,22 +5,29 @@

Includes the website and its MariaDB databases.

{% if not account_backup_pending %} -
+ {% csrf_token %} + {% if account_backup_allow_mail %} + + {% endif %}
{% endif %} + {% if account_backup_pending_hidden %} +

A backup is already queued or running.

+ {% endif %} {% if account_backup_jobs %}
- + {% for job in account_backup_jobs %} +
RequestedStatusArchive
RequestedContentsStatusArchive
{{ job.requested_at|date:"j M Y, H:i" }} UTC{% if job.includes_mailboxes %}Website, databases, and mail{% else %}Website and databases{% endif %} {% if job.state == "configured" %} Ready @@ -40,7 +47,7 @@
- {% else %} + {% elif not account_backup_pending_hidden %}

No backups requested.

{% endif %} diff --git a/src/coulobre/hosting/test_panel_account_backups.py b/src/coulobre/hosting/test_panel_account_backups.py index 410d324..72b30ec 100644 --- a/src/coulobre/hosting/test_panel_account_backups.py +++ b/src/coulobre/hosting/test_panel_account_backups.py @@ -10,6 +10,7 @@ from uuid import uuid4 from zipfile import ZipFile from django.contrib.auth import get_user_model +from django.contrib.auth.models import Permission from django.test import SimpleTestCase, TestCase, override_settings from django.urls import reverse from django.utils import timezone @@ -21,7 +22,14 @@ from coulobre.database_node import DEFAULT_SOCKET from coulobre.mail_node import MailboxSpec from coulobre.web_node import WebSite -from .models import AccountBackupJob, HostingAccount, HostingDatabase, Reseller +from .models import ( + AccountBackupJob, + HostingAccount, + HostingDatabase, + HostingMailbox, + HostingMailDomain, + Reseller, +) from .tasks import ( ACCOUNT_BACKUP_LEASE, ACCOUNT_BACKUP_QUEUE, @@ -134,6 +142,116 @@ class PanelAccountBackupTests(TestCase): ) self.assertFalse(AccountBackupJob.objects.exists()) + @override_settings(LOCAL_ACCOUNT_BACKUP_MAIL=True) + def test_operator_can_include_mailboxes_and_see_the_completed_job(self): + domain = HostingMailDomain.objects.create( + account=self.account, domain="old.example" + ) + mailbox = HostingMailbox.objects.create( + mail_domain=domain, local_part="support" + ) + self.assertContains( + self.client.get(self.account.get_absolute_url()), + "Include mailboxes", + ) + self.assertRedirects( + self.client.post(self.request_url, {"include_mail": "1"}), + self.account.get_absolute_url(), + ) + HostingMailbox.objects.create(mail_domain=domain, local_part="later") + self.client.post(self.request_url) + job = AccountBackupJob.objects.get(account=self.account) + + with patch( + "coulobre.hosting.tasks.create_local_account_backup" + ) as node: + create_account_backup(job.pk) + + node.assert_called_once_with( + AccountBackupRequest( + job.backup_id, + AccountBackup( + WebSite(self.account.pk, self.account.primary_domain), + (self.database.spec,), + (mailbox.spec,), + ), + ) + ) + page = self.client.get(self.account.get_absolute_url()) + self.assertContains(page, "Website, databases, and mail") + self.assertContains(page, job.archive) + + @override_settings(LOCAL_ACCOUNT_BACKUP_MAIL=False) + def test_mail_opt_in_requires_local_mail_backup_to_be_enabled(self): + self.assertNotContains( + self.client.get(self.account.get_absolute_url()), + "Include mailboxes", + ) + + response = self.client.post(self.request_url, {"include_mail": "1"}) + self.assertEqual(response.status_code, 404) + self.assertFalse(AccountBackupJob.objects.exists()) + self.assertEqual(self.client.post(self.request_url).status_code, 302) + self.assertFalse(AccountBackupJob.objects.get().includes_mailboxes) + + @override_settings(LOCAL_ACCOUNT_BACKUP_MAIL=True) + def test_operator_without_mail_permission_cannot_request_or_see_mail_jobs( + self, + ): + self.client.post(self.request_url, {"include_mail": "1"}) + job = AccountBackupJob.objects.get(account=self.account) + limited = get_user_model().objects.create_user("limited-operator") + limited.user_permissions.set( + Permission.objects.filter( + content_type__app_label="hosting", + codename__in=( + "view_hostingaccount", + "view_webprovisioningplan", + "view_hostingdatabase", + "add_accountbackupjob", + ), + ) + ) + self.client.force_login(limited) + + pending = self.client.get(self.account.get_absolute_url()) + self.assertContains(pending, "A backup is already queued or running.") + self.assertNotContains(pending, "Create backup") + job.finished_at = timezone.now() + job.problem = "Could not back up a mailbox at private.example." + job.save(update_fields=["finished_at", "problem"]) + page = self.client.get(self.account.get_absolute_url()) + self.assertNotContains(page, "private.example") + self.assertNotContains(page, "Include mailboxes") + self.assertEqual( + self.client.post( + self.request_url, {"include_mail": "1"} + ).status_code, + 403, + ) + self.assertEqual(self.client.post(self.request_url).status_code, 302) + self.assertEqual(AccountBackupJob.objects.count(), 2) + + @override_settings(LOCAL_ACCOUNT_BACKUP_MAIL=True) + def test_worker_leaves_a_failure_when_mail_is_disabled_after_queueing( + self, + ): + self.client.post(self.request_url, {"include_mail": "1"}) + job = AccountBackupJob.objects.get(account=self.account) + + with ( + self.settings(LOCAL_ACCOUNT_BACKUP_MAIL=False), + patch( + "coulobre.hosting.tasks.create_local_account_backup" + ) as node, + ): + create_account_backup(job.pk) + + node.assert_not_called() + job.refresh_from_db() + self.assertEqual(job.state, "failed") + self.assertIn("Enable local mailbox backups", job.problem) + class AccountBackupRequestTests(SimpleTestCase): def setUp(self): @@ -197,7 +315,7 @@ class AccountBackupRequestTests(SimpleTestCase): self.assertEqual(report["state"], "failed") self.create.assert_not_called() - def test_panel_worker_rejects_requests_that_include_mailboxes(self): + def test_backup_node_requires_mail_backup_opt_in(self): backup = AccountBackup( self.backup.website, (), @@ -205,12 +323,33 @@ class AccountBackupRequestTests(SimpleTestCase): ) request = AccountBackupRequest(self.request.backup_id, backup) - code, report = self.send(request.manifest()) + with patch.dict( + "os.environ", {"COULOBRE_LOCAL_ACCOUNT_BACKUP_MAIL": "0"} + ): + code, report = self.send(request.manifest()) self.assertEqual(code, 1) - self.assertIn("Use the account backup CLI", report["problem"]) + self.assertIn("Enable mailbox backups", report["problem"]) + self.assertEqual(report["backup_id"], str(request.backup_id)) self.create.assert_not_called() + def test_backup_node_accepts_and_replays_mail_requests_when_enabled(self): + backup = AccountBackup( + self.backup.website, (), (MailboxSpec("example.net", "sales"),) + ) + request = AccountBackupRequest(self.request.backup_id, backup) + archive = self.directory / f"account-123-{request.backup_id}.zip" + + with patch.dict( + "os.environ", {"COULOBRE_LOCAL_ACCOUNT_BACKUP_MAIL": "1"} + ): + for _ in range(2): + code, report = self.send(request.manifest()) + self.assertEqual(code, 0) + self.assertEqual(report["state"], "configured") + + self.create.assert_called_once_with(backup, archive, DEFAULT_SOCKET) + def test_replay_rejects_an_archive_with_a_different_plan(self): archive = self.directory / f"account-123-{self.request.backup_id}.zip" self.write_zip( diff --git a/src/coulobre/hosting/views.py b/src/coulobre/hosting/views.py index 96753dd..9fb6ce8 100644 --- a/src/coulobre/hosting/views.py +++ b/src/coulobre/hosting/views.py @@ -250,10 +250,23 @@ class AccountDetail(TenantScopeMixin, PermissionRequiredMixin, DetailView): jobs = list( self.object.backup_jobs.order_by("-requested_at", "-pk")[:10] ) + can_view_mailboxes = user.has_perm(VIEW_MAILBOXES) context["show_account_backups"] = True - context["account_backup_jobs"] = jobs - context["account_backup_pending"] = any( - job.finished_at is None for job in jobs + visible_jobs = ( + jobs + if can_view_mailboxes + else [job for job in jobs if not job.includes_mailboxes] + ) + context["account_backup_jobs"] = visible_jobs + pending = next( + (job for job in jobs if job.finished_at is None), None + ) + context["account_backup_pending"] = pending is not None + context["account_backup_pending_hidden"] = ( + pending is not None and pending not in visible_jobs + ) + context["account_backup_allow_mail"] = ( + settings.LOCAL_ACCOUNT_BACKUP_MAIL and can_view_mailboxes ) context["manage_sftp"] = ( settings.LOCAL_WEB_SFTP @@ -339,8 +352,14 @@ class RequestAccountBackup(OperatorOnlyMixin, PermissionRequiredMixin, View): def post(self, request, pk): if not settings.LOCAL_ACCOUNT_BACKUPS: raise Http404 + include_mailboxes = request.POST.get("include_mail") == "1" + if include_mailboxes: + if not settings.LOCAL_ACCOUNT_BACKUP_MAIL: + raise Http404 + if not request.user.has_perm(VIEW_MAILBOXES): + return self.handle_no_permission() try: - queue_account_backup(pk) + queue_account_backup(pk, include_mailboxes=include_mailboxes) except HostingAccount.DoesNotExist: raise Http404 from None return redirect("panel:account", pk=pk) diff --git a/src/coulobre/settings.py b/src/coulobre/settings.py index 0c48d05..b677560 100644 --- a/src/coulobre/settings.py +++ b/src/coulobre/settings.py @@ -129,6 +129,9 @@ LOCAL_MAILBOX_PROVISIONING = ( os.environ.get("COULOBRE_LOCAL_MAILBOX_PROVISIONING") == "1" ) LOCAL_ACCOUNT_BACKUPS = os.environ.get("COULOBRE_LOCAL_ACCOUNT_BACKUPS") == "1" +LOCAL_ACCOUNT_BACKUP_MAIL = ( + os.environ.get("COULOBRE_LOCAL_ACCOUNT_BACKUP_MAIL") == "1" +) CELERY_BEAT_SCHEDULE = { "dispatch-web-provisioning-plans": { "task": "coulobre.hosting.tasks.dispatch_web_plans",