From c29874fa607d7a8b18938cb315e9b8c9ad69c8e5 Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Tue, 1 Dec 2026 15:07:57 +0100 Subject: [PATCH] Cover reciprocal peer access and initialize the SFTP path --- src/coulobre/hosting/test_peering.py | 54 +++++++++++++++++++++++++++- src/coulobre/local_feature_setup.py | 1 + src/coulobre/test_receiver_scope.py | 25 +++++++++++-- 3 files changed, 77 insertions(+), 3 deletions(-) diff --git a/src/coulobre/hosting/test_peering.py b/src/coulobre/hosting/test_peering.py index 4908861..d972ea8 100644 --- a/src/coulobre/hosting/test_peering.py +++ b/src/coulobre/hosting/test_peering.py @@ -10,7 +10,15 @@ from django_otp import DEVICE_ID_SESSION_KEY from django_otp.plugins.otp_totp.models import TOTPDevice from .models import OperatorOperation, PanelPeer, PeerActionGrant -from .peering import PROTOCOL, issue_grant, panel_public_key, signed, verify +from .peering import ( + PROTOCOL, + PeerAccessError, + issue_grant, + panel_public_key, + refresh_peer, + signed, + verify, +) from .tasks import run_operator_operation @@ -137,3 +145,47 @@ class PanelPeerTests(TestCase): apply.assert_not_called() operation.refresh_from_db() self.assertEqual(operation.state, "failed") + + def test_reverse_direction_and_unreachable_peer(self): + target_key = panel_public_key() + with override_settings(SECRET_KEY="source-panel-identity"): + operator = get_user_model().objects.create_superuser("source") + TOTPDevice.objects.create(user=operator, name="source device") + reciprocal = PanelPeer.objects.create( + origin="https://target.example.test", + public_key=target_key, + remote_username="target", + operator=operator, + ) + token = issue_grant(reciprocal, operator) + packet = signed( + { + "request": str(uuid4()), + "sender": target_key, + "recipient": self.source_key, + "protocol": PROTOCOL, + "timestamp": int(timezone.now().timestamp()), + "action": "package-check", + "actor": "target", + "value": token, + } + ) + with ( + override_settings(SECRET_KEY="source-panel-identity"), + patch( + "coulobre.hosting.peering.Path.is_socket", return_value=True + ), + ): + reply = self.call_target(packet) + self.assertEqual(reply.status_code, 200) + envelope = reply.json() + verify(self.source_key, envelope["message"], envelope["signature"]) + self.assertEqual(OperatorOperation.objects.get().actor, operator) + with patch( + "coulobre.hosting.peering.peer_call", + side_effect=PeerAccessError("The peer did not respond."), + ): + refresh_peer(reciprocal) + reciprocal.refresh_from_db() + self.assertEqual(reciprocal.problem, "The peer did not respond.") + self.assertEqual(reciprocal.status, {}) diff --git a/src/coulobre/local_feature_setup.py b/src/coulobre/local_feature_setup.py index 31fa460..09faffe 100644 --- a/src/coulobre/local_feature_setup.py +++ b/src/coulobre/local_feature_setup.py @@ -242,6 +242,7 @@ def check_folder_sftp_filesystem(): run_mount, ) + require_root_directory(WEB_ROOT.parent, create_mode=0o755) require_root_directory(WEB_ROOT, create_mode=0o755) require_root_directory(JAIL_ROOT, create_mode=0o755) with ( diff --git a/src/coulobre/test_receiver_scope.py b/src/coulobre/test_receiver_scope.py index c83c405..e5b4e28 100644 --- a/src/coulobre/test_receiver_scope.py +++ b/src/coulobre/test_receiver_scope.py @@ -18,7 +18,10 @@ from .mail_queue import MailQueueRequest from .node_requests import serve_apply_request from .node_retirement import RetirementRequest from .service_access import ServiceAccessRequest -from .service_credentials import ServiceCredentialRequest +from .service_credentials import ( + ServiceCredentialRequest, + apply_mailbox_password_request, +) def check_request(role, spec=None): @@ -99,12 +102,13 @@ class ReceiverScopeTests(TestCase): patch.object( receiver_scope, "scoped_domains", - side_effect=[{mailbox.domain}, {"beta.example.test"}] * 2, + side_effect=[{mailbox.domain}, {"beta.example.test"}] * 3, ), ): for operation in ( ServiceCredentialRequest(mailbox, "generation"), ServiceCredentialRequest(mailbox, "rotate", str(uuid4())), + ServiceCredentialRequest(mailbox, "webmail"), ): request = parse_mailbox_request(operation.manifest()) self.assertEqual(request, operation) @@ -114,6 +118,23 @@ class ReceiverScopeTests(TestCase): with self.assertRaisesRegex(ValueError, "not assigned"): check_request("mail", request) + def test_scoped_webmail_refuses_an_unscoped_legacy_relay(self): + mailbox = MailboxSpec("alpha.example.test", "sales") + request = ServiceCredentialRequest(mailbox, "webmail") + with ( + patch("coulobre.service_credentials.socket_peer") as peer, + patch( + "coulobre.service_credentials.read_mailbox_credentials" + ) as read, + ): + peer.return_value = "coulobre-mail-relay" + with self.assertRaisesRegex(ValueError, "domain-scoped"): + apply_mailbox_password_request(request) + read.assert_not_called() + peer.return_value = "coulobre-mail-p-alpha" + apply_mailbox_password_request(request) + read.assert_called_once_with(mailbox, active_only=True) + def test_scope_is_enforced_before_dispatching_dns_operations(self): zone = DnsZone( "beta.example.test",