+ {% if rotating %}
+ {% if database %}
Password changed. Update the database password in each application that uses this login.
+ {% else %}
Password changed. Update the saved login in your mail clients.
{% endif %}
+ {% endif %}
{% for key, label, value in fields %}
@@ -24,16 +31,24 @@
{% else %}
diff --git a/src/coulobre/hosting/test_credential_rotation.py b/src/coulobre/hosting/test_credential_rotation.py
new file mode 100644
index 0000000..0b30204
--- /dev/null
+++ b/src/coulobre/hosting/test_credential_rotation.py
@@ -0,0 +1,207 @@
+from pathlib import Path
+from tempfile import TemporaryDirectory
+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 coulobre.credential_rotation import finish_rotation, stage_rotation
+from coulobre.database_node import DatabaseSpec
+from coulobre.service_credentials import (
+ CREDENTIAL_SOCKET,
+ MAIL_CREDENTIAL_SOCKET,
+ ServiceCredentialRequest,
+)
+
+from .models import (
+ Customer,
+ HostingAccount,
+ HostingMailbox,
+ HostingMailDomain,
+ ServiceCredentialAccess,
+)
+
+PASSWORD = "fixture-password-for-panel"
+REPLACEMENT = "fixture-service-credential"
+Outcome = ServiceCredentialAccess.Outcome
+
+
+@override_settings(
+ DEBUG=False,
+ LOCAL_DATABASE_CREDENTIALS=True,
+ LOCAL_DATABASE_CREDENTIAL_ROTATION=True,
+ REMOTE_MAILBOX_CREDENTIALS=True,
+ REMOTE_MAILBOX_CREDENTIAL_ROTATION=True,
+)
+class CredentialRotationTests(TestCase):
+ @classmethod
+ def setUpTestData(cls):
+ users = get_user_model().objects
+ cls.owner = users.create_user("customer", password=PASSWORD)
+ customer = Customer.objects.create(user=cls.owner)
+ cls.account = HostingAccount.objects.create(
+ name="Own", primary_domain="own.example", customer=customer
+ )
+ cls.database = cls.account.databases.create(suffix="app")
+ cls.second_database = cls.account.databases.create(suffix="shop")
+ other = HostingAccount.objects.create(
+ name="Other", primary_domain="other.example"
+ )
+ cls.other_database = other.databases.create(suffix="app")
+ domain = HostingMailDomain.objects.create(
+ account=cls.account, domain=cls.account.primary_domain
+ )
+ cls.mailbox = HostingMailbox.objects.create(
+ mail_domain=domain, local_part="support"
+ )
+
+ def setUp(self):
+ self.client.force_login(self.owner)
+
+ def url(self, kind, target):
+ return reverse(f"panel:{kind}_credential_rotation", args=[target.pk])
+
+ def submit(self, url, ticket, password=PASSWORD):
+ return self.client.post(
+ url, {"password": password, "ticket": ticket}, secure=True
+ )
+
+ def test_scope_confirmation_and_database_result(self):
+ database = self.database
+ url = self.url("database", database)
+ reply = {
+ "database": database.spec.name,
+ "user": database.spec.name,
+ "host": "localhost",
+ "socket": "/run/mysqld/mysqld.sock",
+ "password": REPLACEMENT,
+ }
+ with patch(
+ "coulobre.hosting.service_credentials.apply_on_local_node",
+ side_effect=[{"generation": None}, reply],
+ ) as node:
+ form = self.client.get(url, secure=True)
+ ticket = form.context["form"]["ticket"].value()
+ self.assertNotContains(form, REPLACEMENT)
+ self.assertContains(
+ self.submit(
+ self.url("database", self.second_database), ticket
+ ),
+ "Open a new password change form",
+ status_code=409,
+ )
+ self.assertEqual(
+ self.submit(
+ self.url("database", self.other_database), ticket
+ ).status_code,
+ 404,
+ )
+ self.assertContains(
+ self.submit(url, ticket, "incorrect"),
+ "Enter your current panel password.",
+ )
+ self.assertEqual(node.call_count, 1)
+ result = self.submit(url, ticket)
+ self.assertContains(result, REPLACEMENT)
+ self.assertNotContains(result, PASSWORD)
+ self.assertIn("no-store", result.headers["Cache-Control"])
+ request, socket, *_ = node.call_args.args
+ self.assertEqual(socket, CREDENTIAL_SOCKET)
+ self.assertEqual(request.operation, "rotate")
+ self.assertEqual(request.resource, database.spec)
+ attempts = ServiceCredentialAccess.objects.order_by("pk")
+ self.assertEqual(
+ list(attempts.values_list("outcome", flat=True)),
+ [Outcome.REJECTED, Outcome.ROTATED],
+ )
+ self.assertNotIn(REPLACEMENT, str(list(attempts.values())))
+
+ def test_remote_mail_retry_reuses_the_request_after_a_lost_response(self):
+ url = self.url("mailbox", self.mailbox)
+ reply = {"user": self.mailbox.spec.address, "password": REPLACEMENT}
+ with patch(
+ "coulobre.hosting.service_credentials.apply_on_local_node",
+ side_effect=[{"generation": None}, OSError, reply],
+ ) as node:
+ form = self.client.get(url, secure=True)
+ ticket = form.context["form"]["ticket"].value()
+ self.assertContains(
+ self.submit(url, ticket),
+ "could not be confirmed",
+ status_code=503,
+ )
+ reopened = self.client.get(url, secure=True)
+ self.assertContains(
+ self.submit(url, reopened.context["form"]["ticket"].value()),
+ REPLACEMENT,
+ )
+ self.assertEqual(node.call_count, 3)
+ self.assertEqual(
+ [call.args[1] for call in node.call_args_list],
+ [MAIL_CREDENTIAL_SOCKET] * 3,
+ )
+ self.assertEqual(
+ node.call_args_list[1].args[0], node.call_args_list[2].args[0]
+ )
+ attempts = ServiceCredentialAccess.objects.order_by("pk")
+ self.assertEqual(
+ list(attempts.values_list("outcome", flat=True)),
+ [Outcome.UNAVAILABLE, Outcome.ROTATED],
+ )
+ self.assertEqual(
+ attempts.first().rotation_id, attempts.last().rotation_id
+ )
+
+ def test_a_stale_form_cannot_change_a_password_again(self):
+ url = self.url("mailbox", self.mailbox)
+ reply = {"user": self.mailbox.spec.address, "password": REPLACEMENT}
+ with patch(
+ "coulobre.hosting.service_credentials.apply_on_local_node",
+ side_effect=[{"generation": None}, {"generation": None}, reply],
+ ) as node:
+ tickets = [
+ self.client.get(url, secure=True)
+ .context["form"]["ticket"]
+ .value()
+ for _ in range(2)
+ ]
+ self.assertContains(self.submit(url, tickets[0]), REPLACEMENT)
+ self.assertContains(
+ self.submit(url, tickets[1]),
+ "Open a new password change form",
+ status_code=409,
+ )
+ self.assertEqual(node.call_count, 3)
+ self.assertEqual(
+ ServiceCredentialAccess.objects.first().outcome, Outcome.BLOCKED
+ )
+
+
+class NodeRotationStateTests(SimpleTestCase):
+ def test_a_pending_operation_is_replayed_and_old_generations_are_rejected(
+ self,
+ ):
+ path = Path(self.enterContext(TemporaryDirectory())) / "test.json"
+ resource = DatabaseSpec(1, "test")
+ reservation = ServiceCredentialRequest(
+ resource, "rotate", str(uuid4())
+ )
+ second = ServiceCredentialRequest(resource, "rotate", str(uuid4()))
+ with patch(
+ "coulobre.credential_rotation.read_root_file",
+ side_effect=lambda path, **kwargs: path.read_text(),
+ ):
+ staged = stage_rotation(path, reservation, None)
+ self.assertEqual(stage_rotation(path, reservation, None), staged)
+ with self.assertRaisesMessage(
+ ValueError, "Retry the current credential change"
+ ):
+ stage_rotation(path, second, None)
+ finish_rotation(path)
+ self.assertIsNone(
+ stage_rotation(path, reservation, reservation.rotation_id)
+ )
+ with self.assertRaisesMessage(ValueError, "changed after"):
+ stage_rotation(path, second, reservation.rotation_id)
diff --git a/src/coulobre/hosting/urls.py b/src/coulobre/hosting/urls.py
index 563dcc3..5cbb379 100644
--- a/src/coulobre/hosting/urls.py
+++ b/src/coulobre/hosting/urls.py
@@ -163,6 +163,11 @@ urlpatterns = [
DatabaseCredentialView.as_view(),
name="database_credentials",
),
+ path(
+ "databases/
/credentials/rotate/",
+ DatabaseCredentialView.as_view(rotating=True),
+ name="database_credential_rotation",
+ ),
path(
"databases//manifest/",
views.DownloadDatabaseManifest.as_view(),
@@ -203,6 +208,11 @@ urlpatterns = [
MailboxCredentialView.as_view(),
name="mailbox_credentials",
),
+ path(
+ "mailboxes//credentials/rotate/",
+ MailboxCredentialView.as_view(rotating=True),
+ name="mailbox_credential_rotation",
+ ),
path(
"mailboxes//manifest/",
views.DownloadMailboxManifest.as_view(),
diff --git a/src/coulobre/settings.py b/src/coulobre/settings.py
index f947776..00fe385 100644
--- a/src/coulobre/settings.py
+++ b/src/coulobre/settings.py
@@ -138,6 +138,18 @@ LOCAL_DNS_PUBLISHING = os.environ.get("COULOBRE_LOCAL_DNS_PUBLISHING") == "1"
LOCAL_MAILBOX_PROVISIONING = (
os.environ.get("COULOBRE_LOCAL_MAILBOX_PROVISIONING") == "1"
)
+REMOTE_MAILBOX_CREDENTIALS = (
+ os.environ.get("COULOBRE_REMOTE_MAILBOX_CREDENTIALS") == "1"
+)
+LOCAL_DATABASE_CREDENTIAL_ROTATION = (
+ os.environ.get("COULOBRE_LOCAL_DATABASE_CREDENTIALS_ROTATION") == "1"
+)
+LOCAL_MAILBOX_CREDENTIAL_ROTATION = (
+ os.environ.get("COULOBRE_LOCAL_MAILBOX_CREDENTIALS_ROTATION") == "1"
+)
+REMOTE_MAILBOX_CREDENTIAL_ROTATION = (
+ os.environ.get("COULOBRE_REMOTE_MAILBOX_CREDENTIALS_ROTATION") == "1"
+)
LOCAL_DATABASE_CREDENTIALS = (
os.environ.get("COULOBRE_LOCAL_DATABASE_CREDENTIALS") == "1"
)