diff --git a/docs/deployment.md b/docs/deployment.md index 50d51ed..c793c7e 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -1115,10 +1115,12 @@ sudo systemctl restart coulobre-panel coulobre-worker coulobre-beat sudo systemctl enable --now coulobre-database-provisioner.service ``` -Open a hosting account and select **Provision database** for a database request. -Reload the account page to see the status. Use **Retry provisioning** after a -reported failure. Operators need permission to view hosting accounts and -database requests and the **Can add database provisioning** permission. +Select **Add database** on a hosting account to queue its provisioning. +Customers can add databases on their own accounts. Customer managers need +permission to manage resources for their assigned accounts. For a pending +request, an operator can select **Provision database**. Operators need +permission to view hosting accounts and databases and the **Can add database +provisioning** permission. Use **Retry provisioning** after a reported failure. The MariaDB node stores the application login in the root-only file `/var/lib/coulobre/database-node/.json`. Use the credentials @@ -1774,10 +1776,11 @@ sudo systemctl restart coulobre-panel coulobre-worker coulobre-beat sudo systemctl enable --now coulobre-dns-publisher.service ``` -Open a hosting account and select **Publish zone**. Reload the account page to -see progress and the last SOA serial confirmed by the local BIND9 node. -Operators need permission to view hosting accounts and DNS zones and the -**Can add DNS publication** permission. +Select **Save and publish zone** to queue a new zone from a hosting account. +Use **Publish zone** to publish a pending revision of an existing zone. +The account page shows progress and the last SOA serial confirmed by the +local BIND9 node. Operators need permission to view hosting accounts and DNS +zones and the **Can add DNS publication** permission. Each request captures the reviewed SOA serial and records. You can edit the zone while it publishes. Publish the newer revision after the current request diff --git a/src/coulobre/hosting/installation_views.py b/src/coulobre/hosting/installation_views.py index 8c437e1..24544c1 100644 --- a/src/coulobre/hosting/installation_views.py +++ b/src/coulobre/hosting/installation_views.py @@ -7,6 +7,7 @@ from django.views.decorators.cache import never_cache from django.views.generic import TemplateView from .access import OperatorOnlyMixin +from .dns_defaults import zone_defaults @method_decorator(never_cache, name="dispatch") @@ -22,6 +23,7 @@ class InstallationView( except PackageNotFoundError: installed_version = "Unavailable" + dns_defaults = zone_defaults() features = ( ("Local website provisioning", settings.LOCAL_WEB_ACTIVATION), ("Managed website certificates", settings.LOCAL_WEB_HTTPS), @@ -31,8 +33,8 @@ class InstallationView( ( "Default managed DNS zones for new accounts", settings.LOCAL_DNS_PUBLISHING - and settings.HOSTING_NAMESERVERS - and settings.HOSTING_PUBLIC_ADDRESSES, + and dns_defaults.nameservers + and dns_defaults.addresses, ), ( "Local mailbox provisioning", diff --git a/src/coulobre/hosting/resource_requests.py b/src/coulobre/hosting/resource_requests.py index e2a0349..5034572 100644 --- a/src/coulobre/hosting/resource_requests.py +++ b/src/coulobre/hosting/resource_requests.py @@ -27,7 +27,9 @@ def lock_hosting_account(account_id, customer_id): @transaction.atomic -def create_database_request(account_id, suffix, *, customer_id=None): +def create_database_request( + account_id, suffix, *, customer_id=None, auto_provision=False +): account = lock_hosting_account(account_id, customer_id) require_open_account(account) databases = account.databases @@ -45,7 +47,11 @@ def create_database_request(account_id, suffix, *, customer_id=None): raise ValidationError( "This hosting account has reached its database limit." ) - return databases.create(suffix=suffix) + database = databases.create(suffix=suffix) + if auto_provision: + require_active_services(account) + DatabaseProvisioning.objects.queue(database=database) + return database @transaction.atomic diff --git a/src/coulobre/hosting/templates/hosting/account_detail.html b/src/coulobre/hosting/templates/hosting/account_detail.html index 9212aaa..a306962 100644 --- a/src/coulobre/hosting/templates/hosting/account_detail.html +++ b/src/coulobre/hosting/templates/hosting/account_detail.html @@ -133,7 +133,7 @@
-

MariaDB database requests

+

MariaDB databases

{% if is_tenant or perms.hosting.view_hostingplan %} {% if account.hosting_plan and account.hosting_plan.database_limit is not None %}

{{ databases|length }} of {{ account.hosting_plan.database_limit }} databases requested

@@ -141,51 +141,50 @@ {% endif %}
{% if is_customer or reseller_resource_management or is_operator and perms.hosting.add_hostingdatabase %} - Request database + {% if automatic_database_provisioning %}Add{% else %}Request{% endif %} database {% endif %}
{% if databases %}
- {% if is_operator or manage_database_provisioning %}{% endif %} + {% for database in databases %} - + - {% if is_operator or manage_database_provisioning %} - - {% endif %} {% endfor %}
DatabaseDeployment
DatabaseStatus and actions
- {{ database }} - {% if show_database_credentials %} - - {% endif %} - {% if show_phpmyadmin %} -
- {% csrf_token %} - -
+
{{ database }} +
+ {% if manage_database_provisioning %} + {% url 'panel:database_provision' database.pk as provision_url %} + {% include "hosting/resource_provisioning.html" with provisioning=database.provisioning enabled=local_database_enabled action=provision_url resource=database provision_label="Provision database" %} + {% else %} + {% include "hosting/resource_provisioning.html" with provisioning=database.provisioning enabled=False %} + {% endif %} + {% if not database.provisioning %}Requested{% endif %} + {% if show_database_credentials %} + Connection details + {% endif %} + {% if show_phpmyadmin %} +
+ {% csrf_token %} + +
+ {% endif %} + {% if is_operator %} + Download manifest + {% endif %} +
+ {% if is_operator and manage_database_provisioning and database.provisioning.problem %} +

{{ database.provisioning.problem }}

{% endif %}
-
- {% if is_operator %} - Download manifest - {% endif %} - {% if manage_database_provisioning %} - {% url 'panel:database_provision' database.pk as provision_url %} - {% include "hosting/resource_provisioning.html" with provisioning=database.provisioning enabled=local_database_enabled action=provision_url resource=database provision_label="Provision database" %} - {% endif %} -
- {% if is_operator and manage_database_provisioning and database.provisioning.problem %} -

{{ database.provisioning.problem }}

- {% endif %} -
{% else %} -

No databases requested yet.

+

No databases yet.

{% endif %}
{% endif %} diff --git a/src/coulobre/hosting/templates/hosting/database_form.html b/src/coulobre/hosting/templates/hosting/database_form.html index 79a5bdb..2b8c592 100644 --- a/src/coulobre/hosting/templates/hosting/database_form.html +++ b/src/coulobre/hosting/templates/hosting/database_form.html @@ -1,15 +1,15 @@ {% extends "hosting/base.html" %} -{% block title %}Request MariaDB database · Coulobre Panel{% endblock %} +{% block title %}{% if auto_provision %}Add{% else %}Request{% endif %} MariaDB database · Coulobre Panel{% endblock %} {% block content %} ← {{ account.name }} -

Request MariaDB database

+

{% if auto_provision %}Add{% else %}Request{% endif %} MariaDB database

{% include "hosting/customer_action_fields.html" %} {{ form.as_div }}
{% if can_request_resource %} - + {% endif %} Cancel
diff --git a/src/coulobre/hosting/templates/hosting/dns_zone_form.html b/src/coulobre/hosting/templates/hosting/dns_zone_form.html index 1dfe634..47104fe 100644 --- a/src/coulobre/hosting/templates/hosting/dns_zone_form.html +++ b/src/coulobre/hosting/templates/hosting/dns_zone_form.html @@ -37,7 +37,7 @@ {% endif %}
- + Cancel
diff --git a/src/coulobre/hosting/templates/hosting/installation.html b/src/coulobre/hosting/templates/hosting/installation.html index 29e1477..866dab3 100644 --- a/src/coulobre/hosting/templates/hosting/installation.html +++ b/src/coulobre/hosting/templates/hosting/installation.html @@ -25,7 +25,14 @@ {% for name, enabled in features %} {{ name }} - {% if enabled %}Enabled{% else %}Disabled{% endif %} + +
+ {% if enabled %}Enabled{% else %}Disabled{% endif %} + {% if request.user.is_superuser and name == 'Default managed DNS zones for new accounts' %} + Configure DNS defaults + {% endif %} +
+ {% endfor %} diff --git a/src/coulobre/hosting/templates/hosting/mailbox_form.html b/src/coulobre/hosting/templates/hosting/mailbox_form.html index 93d0ae3..3205769 100644 --- a/src/coulobre/hosting/templates/hosting/mailbox_form.html +++ b/src/coulobre/hosting/templates/hosting/mailbox_form.html @@ -1,10 +1,10 @@ {% extends "hosting/base.html" %} -{% block title %}{% if automatic_mailbox_provisioning %}Add{% else %}Request{% endif %} mailbox · Coulobre Panel{% endblock %} +{% block title %}{% if auto_provision %}Add{% else %}Request{% endif %} mailbox · Coulobre Panel{% endblock %} {% block content %} ← {{ account.name }}
-

{% if automatic_mailbox_provisioning %}Add{% else %}Request{% endif %} mailbox

+

{% if auto_provision %}Add{% else %}Request{% endif %} mailbox

{{ account.primary_domain }}

@@ -14,7 +14,7 @@ {{ form.as_div }}
{% if can_request_resource %} - + {% endif %} Cancel
diff --git a/src/coulobre/hosting/test_database_provisioning.py b/src/coulobre/hosting/test_database_provisioning.py index cf7c82a..aaedcb3 100644 --- a/src/coulobre/hosting/test_database_provisioning.py +++ b/src/coulobre/hosting/test_database_provisioning.py @@ -87,6 +87,34 @@ class DatabaseProvisioningTests(TestCase): self.assertEqual(self.client.post(self.url).status_code, 403) self.assertFalse(DatabaseProvisioning.objects.exists()) + def test_customer_creation_queues_a_database_and_shows_its_status(self): + self.client.force_login(self.user) + url = reverse("panel:database_create", args=[self.account.pk]) + self.assertContains(self.client.get(url), "Add database") + self.assertRedirects( + self.client.post(url, {"suffix": "new"}), self.detail + ) + job = DatabaseProvisioning.objects.get(database__suffix="new") + self.assertEqual(job.state, "queued") + self.assertContains(self.client.get(self.detail), "Queued") + + def test_operator_without_provisioning_permission_creates_a_request(self): + requester = get_user_model().objects.create_user("requester") + requester.user_permissions.add( + *self.permissions.exclude(codename="add_databaseprovisioning"), + Permission.objects.get(codename="add_hostingdatabase"), + ) + self.client.force_login(requester) + url = reverse("panel:database_create", args=[self.account.pk]) + self.assertContains(self.client.get(url), "Request database") + self.assertRedirects( + self.client.post(url, {"suffix": "request"}), self.detail + ) + self.assertTrue( + self.account.databases.filter(suffix="request").exists() + ) + self.assertFalse(DatabaseProvisioning.objects.exists()) + @override_settings(LOCAL_DATABASE_PROVISIONING=False) def test_disabled_provisioning_preserves_existing_manifest_download(self): detail = self.client.get(self.detail) diff --git a/src/coulobre/hosting/test_dns_publication.py b/src/coulobre/hosting/test_dns_publication.py index 36f74ff..e390368 100644 --- a/src/coulobre/hosting/test_dns_publication.py +++ b/src/coulobre/hosting/test_dns_publication.py @@ -42,6 +42,31 @@ class DnsPublicationTests(TestCase): self.url = reverse("panel:dns_zone_publish", args=[self.zone.pk]) self.detail = self.account.get_absolute_url() + def test_creating_a_managed_zone_queues_its_first_snapshot(self): + account = HostingAccount.objects.create( + name="Example DNS", primary_domain="new.example" + ) + url = reverse("panel:dns_zone_create", args=[account.pk]) + self.assertContains(self.client.get(url), "Save and publish zone") + self.assertRedirects( + self.client.post( + url, + { + "nameservers": "ns.provider.example", + "responsible_name": "hostmaster.new.example", + "addresses": "new.example 192.0.2.20", + }, + ), + account.get_absolute_url(), + ) + zone = account.dns_zones.get() + self.assertEqual( + (zone.publication.state, zone.publication.serial), ("queued", 1) + ) + self.assertEqual( + DnsZone.from_manifest(zone.publication.manifest), zone.spec + ) + def test_queued_snapshot_survives_edits_and_next_revision_can_publish( self, ): diff --git a/src/coulobre/hosting/test_reseller_onboarding.py b/src/coulobre/hosting/test_reseller_onboarding.py index bc39ea4..c7167da 100644 --- a/src/coulobre/hosting/test_reseller_onboarding.py +++ b/src/coulobre/hosting/test_reseller_onboarding.py @@ -196,13 +196,11 @@ class ResellerOnboardingTests(TestCase): for model, resource in (("database", database), ("mailbox", mailbox)): url = reverse(f"panel:{model}_provision", args=[resource.pk]) manifest = reverse(f"panel:{model}_manifest", args=[resource.pk]) - if model == "database": - self.assertContains( - self.client.get(detail), "Provision database" - ) - else: - self.assertEqual(mailbox.provisioning.state, "queued") - self.assertContains(self.client.get(detail), "Queued") + self.assertEqual(resource.provisioning.state, "queued") + self.assertContains(self.client.get(detail), "Queued") + self.assertNotContains( + self.client.get(detail), f"Provision {model}" + ) self.assertEqual(self.client.get(manifest).status_code, 403) for _ in range(2): self.assertRedirects(self.client.post(url), detail) @@ -214,7 +212,7 @@ class ResellerOnboardingTests(TestCase): "action", flat=True ) ), - [Action.RESOURCE_REQUEST] * 2 + [Action.RESOURCE_PROVISION] * 2, + [Action.RESOURCE_REQUEST, Action.RESOURCE_PROVISION] * 2, ) DatabaseProvisioning.objects.update( finished_at=timezone.now(), problem="Failed" diff --git a/src/coulobre/hosting/test_service_health.py b/src/coulobre/hosting/test_service_health.py index 99e8d4c..4a6e300 100644 --- a/src/coulobre/hosting/test_service_health.py +++ b/src/coulobre/hosting/test_service_health.py @@ -20,7 +20,7 @@ from coulobre.service_health import ( main, ) -from .models import Customer, Reseller, ServiceHealth +from .models import Customer, DnsZoneDefaults, Reseller, ServiceHealth TARGETS = ( HealthTarget("panel-01", "local", ("mariadb", "apache")), @@ -196,6 +196,24 @@ class ServiceHealthPanelTests(TestCase): self.assertNotContains(response, "test-only-bridge-key") self.assertNotContains(response, "https://webmail.example.net") + @override_settings( + LOCAL_DNS_PUBLISHING=True, + HOSTING_NAMESERVERS=(), + HOSTING_PUBLIC_ADDRESSES=(), + ) + def test_installation_reports_dns_defaults_saved_in_the_panel(self): + DnsZoneDefaults.objects.create( + pk=1, nameservers=["ns.example.net"], addresses=["192.0.2.20"] + ) + self.client.force_login(self.admin) + response = self.client.get(reverse("panel:installation")) + self.assertTrue( + dict(response.context["features"])[ + "Default managed DNS zones for new accounts" + ] + ) + self.assertContains(response, "Configure DNS defaults") + def test_view_marks_missing_and_old_observations_distinctly(self): now = timezone.now() for node, service, result, observed_at in ( diff --git a/src/coulobre/hosting/views.py b/src/coulobre/hosting/views.py index a0af15a..6607f20 100644 --- a/src/coulobre/hosting/views.py +++ b/src/coulobre/hosting/views.py @@ -238,10 +238,8 @@ PROVISION_MAILBOX = ( CHANGE_MAILBOX_QUOTA = (*PROVISION_MAILBOX, "hosting.change_hostingmailbox") -def automatic_mailbox_provisioning(user): - return settings.LOCAL_MAILBOX_PROVISIONING and ( - is_tenant(user) or user.has_perms(PROVISION_MAILBOX) - ) +def automatic_resource_provisioning(user, enabled, permissions): + return enabled and (is_tenant(user) or user.has_perms(permissions)) def missing_account_setup(account, actor): @@ -830,11 +828,17 @@ class AccountDetail(TenantScopeMixin, PermissionRequiredMixin, DetailView): "show_mailbox_credentials" ] and roundcube_available(self.request) if tenant or user.has_perm(VIEW_DATABASES): - databases = self.object.databases.all() + context["automatic_database_provisioning"] = ( + automatic_resource_provisioning( + user, + settings.LOCAL_DATABASE_PROVISIONING, + PROVISION_DATABASE, + ) + ) + databases = self.object.databases.select_related("provisioning") if manage_resources or ( not tenant and user.has_perms(PROVISION_DATABASE) ): - databases = databases.select_related("provisioning") context["manage_database_provisioning"] = True context["local_database_enabled"] = ( settings.LOCAL_DATABASE_PROVISIONING @@ -898,7 +902,11 @@ class AccountDetail(TenantScopeMixin, PermissionRequiredMixin, DetailView): ) if tenant or user.has_perm(VIEW_MAILBOXES): context["automatic_mailbox_provisioning"] = ( - automatic_mailbox_provisioning(user) + automatic_resource_provisioning( + user, + settings.LOCAL_MAILBOX_PROVISIONING, + PROVISION_MAILBOX, + ) ) context["manage_mail_routing"] = mail_policy_enabled( self.request, "routing" @@ -1932,6 +1940,8 @@ def provisioning_conflict(error): class ResourceRequestCreate(PermissionRequiredMixin, CreateView): audit_operation = None + provisioning_setting = None + provisioning_permissions = () @cached_property def customer(self): @@ -1959,9 +1969,23 @@ class ResourceRequestCreate(PermissionRequiredMixin, CreateView): return super().get_context_data( account=self.account, can_request_resource=can_edit_customer_resource(self.request), + auto_provision=self.auto_provision, **kwargs, ) + @cached_property + def auto_provision(self): + return automatic_resource_provisioning( + self.request.user, + getattr(settings, self.provisioning_setting), + self.provisioning_permissions, + ) + + def create_request(self, account_id, **values): + return self.create_resource( + account_id, auto_provision=self.auto_provision, **values + ) + def get_form(self, form_class=None): form = super().get_form(form_class) if not can_edit_customer_resource(self.request): @@ -2008,7 +2032,9 @@ class DatabaseCreate(ResourceRequestCreate): template_name = "hosting/database_form.html" permission_required = CUSTOMER_WRITE_PERMISSIONS["panel:database_create"] audit_operation = CustomerImpersonationAction.Operation.DATABASE - create_request = staticmethod(create_database_request) + provisioning_setting = "LOCAL_DATABASE_PROVISIONING" + provisioning_permissions = PROVISION_DATABASE + create_resource = staticmethod(create_database_request) class DownloadDatabaseManifest( @@ -2129,6 +2155,17 @@ class DnsZoneCreate(OperatorOnlyMixin, DnsZoneEditor): "record_rows": "[]", } + @cached_property + def publish_on_create(self): + return settings.LOCAL_DNS_PUBLISHING and self.request.user.has_perms( + PUBLISH_DNS + ) + + def get_context_data(self, **kwargs): + return super().get_context_data( + publish_on_create=self.publish_on_create, **kwargs + ) + def form_valid(self, form): try: with transaction.atomic(): @@ -2136,12 +2173,14 @@ class DnsZoneCreate(OperatorOnlyMixin, DnsZoneEditor): self.account.pk, customer_id=None ) require_open_account(account) - HostingDnsZone.objects.create( + zone = HostingDnsZone.objects.create( account=account, domain=account.primary_domain, **form.cleaned_data, ) - except ValidationError as error: + if self.publish_on_create: + queue_dns_publication(zone.pk, str(zone.serial)) + except (ValidationError, ValueError) as error: form.add_error(None, error) return self.form_invalid(form) except IntegrityError: @@ -2332,20 +2371,9 @@ class MailboxCreate(ResourceRequestCreate): permission_required = CUSTOMER_WRITE_PERMISSIONS["panel:mailbox_create"] audit_operation = CustomerImpersonationAction.Operation.MAILBOX - def create_request(self, account_id, **values): - return create_mailbox_request( - account_id, - auto_provision=automatic_mailbox_provisioning(self.request.user), - **values, - ) - - def get_context_data(self, **kwargs): - return super().get_context_data( - automatic_mailbox_provisioning=automatic_mailbox_provisioning( - self.request.user - ), - **kwargs, - ) + provisioning_setting = "LOCAL_MAILBOX_PROVISIONING" + provisioning_permissions = PROVISION_MAILBOX + create_resource = staticmethod(create_mailbox_request) def get_form(self, form_class=None): form = super().get_form(form_class)