diff --git a/docs/deployment.md b/docs/deployment.md index 8be31ba..a0e93c8 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -225,6 +225,7 @@ peer configuration above so Apache supplies the visitor address. Keep the Gunicorn socket private to the panel service and the front-end server. A direct TCP connection uses its network peer address for rate limiting. + ## Protect a host with nftables and Fail2ban Enable protection independently on a panel, web, mail, or DNS host. Use the @@ -636,6 +637,43 @@ serves no tenant content when unknown hosts should be rejected. [Apache default virtual host](https://httpd.apache.org/docs/current/en/vhosts/name-based.html#defaultvhost) ยท [Deleting a Certbot certificate](https://eff-certbot.readthedocs.io/en/stable/using.html#deleting-certificates) +## Host independent websites + +Set a hosting plan's **Website limit** above one to allow additional +websites under an account. The limit includes the primary website. Existing +plans start with one website. In **Independent websites**, choose **Add +website** to provision a domain or subdomain with its own document root, +operating-system user, PHP-FPM pool, and Apache virtual host. + +Leave the PHP series blank to select the newest installed stable series +allowed by the plan. Changing the series on one website keeps the other +websites on their configured versions. If the plan sets a website storage +limit, the web node applies that quota to the new site's user. + +Point the domain's DNS at the web server and select **Enable HTTPS** after +setting up the existing Certbot worker. Each website has its own Certbot +lineage. An alias under **Website aliases** shares the primary document +root and runtime. + +Use **Suspend website** to disable access to a single site. Account-wide +suspension applies to all its active websites. Resuming the account preserves +any independent website suspension. To retire a site, suspend it and +confirm its domain using **Retire website**. The web node archives its data +and disables its Apache and PHP configuration. Retiring a failed provisioning request also frees its place in the plan. +Its domain remains reserved until released with the account's names. + +Operators can download the website's version 2 manifest. Use the existing +[website SFTP setup](development.md#install-sftp-keys-on-a-web-node) on the web node +with that manifest to authorize a key for the site's own user. The web +node suspends new SFTP logins for a suspended independent website when +keys have been installed. + +New account archives include the primary website and activated independent +websites. Select the website when restoring a file or document root from +an archive containing several sites. The archive identifies each website +by domain and site ID. Upgrade the panel and its web and backup workers +together before enabling additional sites. + ## Enable HTTPS for hosted websites Once a site is active, obtain the website certificate with Certbot on the diff --git a/docs/development.md b/docs/development.md index 72c81c9..9812bb4 100644 --- a/docs/development.md +++ b/docs/development.md @@ -1215,12 +1215,14 @@ time. Use the [website backup command](#back-up-and-restore-hosted-website-files) on the web node and the [database backup command](#back-up-and-restore-a-managed-database) on each database node. Transfer the resulting files to a root-controlled -directory with mode `0600` on every archive. Name the website archive +directory with mode `0600` on every archive. Name the primary website archive `website.tar.gz` and each SQL dump `database-.sql.gz`, for example `database-cms.sql.gz`. When the manifest includes mail, use the [mailbox backup command](#back-up-and-restore-a-mailbox) on the mail node and place each archive under `mailboxes//.tar.gz` within the -component directory. Assemble the ZIP with the same account manifest. +component directory. For a version 3 account manifest, use +`websites/s.tar.gz` for each entry in `additional_websites`. +Assemble the ZIP with the same account manifest. ```sh sudo /opt/coulobre/bin/coulobre-account-backup pack \ @@ -1244,6 +1246,8 @@ sudo /opt/coulobre/bin/coulobre-account-backup unpack \ ``` Use the recovered `website.json` and `website.tar.gz` with `restore-site`. +For an independent website, use its `websites/s.json` and +`websites/s.tar.gz` pair with the same command. Use each `database-.json` and `database-.sql.gz` pair with `restore-database`. For mail, use the original manifest and archive recovered as `mailboxes//.json` and diff --git a/src/coulobre/hosting/independent_websites.py b/src/coulobre/hosting/independent_websites.py index 65e6e98..8df1cd9 100644 --- a/src/coulobre/hosting/independent_websites.py +++ b/src/coulobre/hosting/independent_websites.py @@ -117,13 +117,13 @@ def queue_website_operation(account_id, site_id, operation, version, actor): ) if operation == Operation.PHP or version: check_version(account, version) - if operation == Operation.RETIRE: - if account.backup_jobs.filter(finished_at__isnull=True).exists() or ( - WebsiteRestoreJob.objects.filter( - backup__account=account, finished_at__isnull=True - ).exists() - ): - raise ValidationError("Wait for the backup or restore to finish.") + if operation == Operation.RETIRE and ( + account.backup_jobs.filter(finished_at__isnull=True).exists() + or WebsiteRestoreJob.objects.filter( + backup__account=account, finished_at__isnull=True + ).exists() + ): + raise ValidationError("Wait for the backup or restore to finish.") IndependentWebsite.objects.requeue( IndependentWebsite.objects.filter(pk=website.pk), operation=operation, diff --git a/src/coulobre/hosting/test_independent_websites.py b/src/coulobre/hosting/test_independent_websites.py new file mode 100644 index 0000000..825d658 --- /dev/null +++ b/src/coulobre/hosting/test_independent_websites.py @@ -0,0 +1,293 @@ +from unittest.mock import patch +from uuid import uuid4 + +from django.contrib.auth import get_user_model +from django.test import SimpleTestCase, TestCase, override_settings +from django.urls import reverse +from django.utils import timezone + +from coulobre.account_backup import AccountBackup +from coulobre.account_restore_request import component_filename +from coulobre.php_pool_settings import PhpPoolSettings +from coulobre.web_node import WebSite +from coulobre.web_node_apply import SitePaths +from coulobre.web_node_tls import certbot_lineage + +from .account_backups import account_backup_manifest +from .models import ( + AccountBackupJob, + Customer, + HostingAccount, + HostingPlan, + IndependentWebsite, + WebsiteDomain, + WebsiteRestoreJob, +) +from .service_changes import apply_service_change, request_service_change +from .tasks import apply_website_restore, provision_independent_website + +Operation = IndependentWebsite.Operation + + +class WebsiteIdentityTests(SimpleTestCase): + def test_independent_site_keeps_account_id_and_separate_node_paths(self): + primary = WebSite(17, "example.test") + child = WebSite(17, "shop.example.test", 17) + self.assertEqual(primary.manifest()["version"], 1) + self.assertEqual(child.manifest()["version"], 2) + self.assertEqual(WebSite.from_manifest(child.manifest()), child) + self.assertEqual(primary.service_user, "coulobre-17") + self.assertEqual(child.service_user, "coulobre-s17") + self.assertEqual(SitePaths.for_site(child).account.name, "s17") + self.assertEqual(certbot_lineage(child).name, "coulobre-s17") + self.assertIn( + "/srv/coulobre/web/s17/public", + child.render("apache"), + ) + self.assertNotEqual( + SitePaths.for_site(child), SitePaths.for_site(primary) + ) + + def test_multisite_bundle_identifies_children_and_rejects_foreign_sites( + self, + ): + primary = WebSite(17, "example.test") + child = WebSite(17, "shop.example.test", 19) + spec = AccountBackup(primary, (), additional_websites=(child,)) + self.assertEqual(spec.manifest()["version"], 3) + self.assertEqual(AccountBackup.from_manifest(spec.manifest()), spec) + self.assertEqual( + component_filename(spec, child), "websites/s19.tar.gz" + ) + self.assertIsNone( + component_filename(spec, WebSite(17, child.domain, 20)) + ) + with self.assertRaisesMessage( + ValueError, "belong to the backup account" + ): + AccountBackup( + primary, + (), + additional_websites=(WebSite(42, child.domain, 19),), + ) + + +@override_settings( + LOCAL_WEB_ACTIVATION=True, + LOCAL_WEB_HTTPS=True, + LOCAL_ACCOUNT_BACKUPS=True, + LOCAL_WEBSITE_RESTORE=True, +) +class IndependentWebsiteTests(TestCase): + @classmethod + def setUpTestData(cls): + users = get_user_model().objects + cls.operator = users.create_superuser("operator") + cls.owner = users.create_user("owner") + cls.outsider = users.create_user("outsider") + Customer.objects.create(user=cls.outsider) + cls.plan = HostingPlan.objects.create( + name="Two websites", + website_limit=2, + php_versions="8.4,8.5", + website_storage_limit_mib=200, + ) + cls.account = HostingAccount.objects.create( + name="Example", + primary_domain="example.test", + customer=Customer.objects.create(user=cls.owner), + hosting_plan=cls.plan, + ) + cls.other = HostingAccount.objects.create( + name="Elsewhere", + primary_domain="foreign.test", + customer=cls.outsider.hosting_customer, + ) + + def setUp(self): + self.client.force_login(self.owner) + self.add = reverse( + "panel:independent_website_create", args=[self.account.pk] + ) + + def create_website(self): + response = self.client.post( + self.add, {"domain": "SHOP.Example.Test.", "php_version": "8.4"} + ) + self.assertRedirects(response, self.account.get_absolute_url()) + return IndependentWebsite.objects.get() + + def active_website(self): + website = self.create_website() + IndependentWebsite.objects.filter(pk=website.pk).update( + activated=True, php_version="8.4", finished_at=timezone.now() + ) + website.refresh_from_db() + return website + + def operation(self, website, action, data=None): + return self.client.post( + reverse( + "panel:independent_website_operation", + args=[self.account.pk, website.pk, action], + ), + data or {}, + ) + + def test_scoped_creation_and_quota_inherit_plan(self): + response = self.client.post(self.add, {"domain": "foreign.test"}) + self.assertContains(response, "This domain is unavailable.") + response = self.client.post( + self.add, {"domain": "shop.example.test", "php_version": "8.3"} + ) + self.assertContains(response, "outside the hosting plan") + website = self.create_website() + self.assertContains( + self.client.get(self.account.get_absolute_url()), + "shop.example.test", + ) + self.assertContains( + self.client.post(self.add, {"domain": "overflow.example.test"}), + "has reached its website limit", + ) + with ( + patch( + "coulobre.hosting.independent_websites.activate_local_site", + return_value=(PhpPoolSettings(), "8.4"), + ) as activate, + patch( + "coulobre.hosting.independent_websites.set_local_site_storage_limit" + ) as quota, + ): + provision_independent_website.run(website.pk) + activate.assert_called_once_with( + website.spec, self.plan.get_php_settings(), ("8.4",) + ) + quota.assert_called_once_with(website.spec, 200) + website.refresh_from_db() + self.assertEqual(website.storage_limit_mib, 200) + self.assertEqual(website.php_version, "8.4") + self.assertContains( + self.client.get(self.account.get_absolute_url()), + "Change PHP series", + ) + self.assertRedirects( + self.operation(website, Operation.PHP, {"php_version": "8.5"}), + self.account.get_absolute_url(), + ) + with patch( + "coulobre.hosting.independent_websites.update_local_php_version", + return_value="8.5", + ) as switch: + provision_independent_website.run(website.pk) + switch.assert_called_once_with(website.spec, "8.5") + website.refresh_from_db() + self.assertEqual(website.php_version, "8.5") + self.client.force_login(self.outsider) + self.assertEqual(self.client.get(self.add).status_code, 404) + self.assertEqual( + self.operation(website, Operation.SUSPEND).status_code, 404 + ) + + def test_tls_and_retirement_leave_primary_reservation_intact(self): + website = self.active_website() + self.assertRedirects( + self.operation(website, Operation.HTTPS), + self.account.get_absolute_url(), + ) + with patch( + "coulobre.hosting.independent_websites.enable_local_https" + ) as tls: + provision_independent_website.run(website.pk) + tls.assert_called_once_with(website.spec) + self.assertRedirects( + self.operation(website, Operation.SUSPEND), + self.account.get_absolute_url(), + ) + with patch( + "coulobre.hosting.independent_websites.apply_on_local_node" + ) as node: + provision_independent_website.run(website.pk) + request = node.call_args.args[0] + self.assertEqual(request.resource, website.spec) + self.assertTrue(request.suspended) + self.assertContains( + self.operation( + website, Operation.RETIRE, {"confirmation": "wrong"} + ), + "Enter the website domain exactly.", + ) + self.assertRedirects( + self.operation( + website, Operation.RETIRE, {"confirmation": str(website)} + ), + self.account.get_absolute_url(), + ) + with patch( + "coulobre.hosting.independent_websites.apply_on_local_node" + ) as node: + provision_independent_website.run(website.pk) + self.assertEqual(node.call_args.args[0].resource, website.spec) + website.refresh_from_db() + self.assertTrue(website.https_enabled) + self.assertIsNotNone(website.retired_at) + self.assertTrue( + WebsiteDomain.objects.filter( + account=self.account, is_primary=True + ).exists() + ) + self.assertContains( + self.client.post(self.add, {"domain": "shop.example.test"}), + "This domain is unavailable.", + ) + + def test_account_resume_preserves_an_independently_suspended_site(self): + website = self.active_website() + IndependentWebsite.objects.filter(pk=website.pk).update(suspended=True) + request_service_change(self.account.pk, "suspend", "operator") + self.account.service_changes.first().jobs.update( + finished_at=timezone.now(), applied_suspended=True + ) + request_service_change(self.account.pk, "resume", "operator") + job = self.account.service_changes.first().jobs.get() + with patch( + "coulobre.hosting.service_changes.apply_on_local_node" + ) as node: + apply_service_change(job) + self.assertEqual(node.call_args.args[0].resource, website.spec) + self.assertTrue(node.call_args.args[0].suspended) + + def test_restore_selects_exact_website_from_account_archive(self): + website = self.active_website() + backup = AccountBackupJob.objects.create( + account=self.account, + manifest=account_backup_manifest(self.account), + finished_at=timezone.now(), + ) + self.assertEqual( + backup.manifest["additional_websites"], [website.spec.manifest()] + ) + self.client.force_login(self.operator) + location = reverse( + "panel:website_restore", args=[self.account.pk, backup.pk] + ) + page = self.client.get(location) + self.assertContains(page, "shop.example.test") + data = { + "scope": "file", + "filename": "index.php", + "restore_id": uuid4(), + } + response = self.client.post(location, data | {"site_key": "s999"}) + self.assertIn("site_key", response.context["form"].errors) + self.assertRedirects( + self.client.post( + location, data | {"site_key": website.spec.node_key} + ), + self.account.get_absolute_url(), + ) + job = WebsiteRestoreJob.objects.get() + self.assertEqual(job.site_manifest, website.spec.manifest()) + with patch("coulobre.hosting.tasks.restore_local_website") as restore: + apply_website_restore(job) + self.assertEqual(restore.call_args.args[0].site, website.spec)