Prepare Coulobre 0.11.1 with shared receiver and database hardening #150

Merged
parkel merged 3 commits from release/0.11.1 into main 2026-11-23 07:27:10 +01:00
5 changed files with 64 additions and 37 deletions
Showing only changes of commit 1bd09f8cd7 - Show all commits

No files matched your search

+2 -2
View File
@@ -74,13 +74,13 @@ def replace_configuration(path, content, *, mode=0o644):
@contextmanager
def locked_root_directory(path):
def locked_root_directory(path, *, shared=False):
import fcntl
require_root_directory(path)
fd = os.open(path, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
try:
fcntl.flock(fd, fcntl.LOCK_EX)
fcntl.flock(fd, fcntl.LOCK_SH if shared else fcntl.LOCK_EX)
yield fd
finally:
os.close(fd)
+18 -13
View File
@@ -1,7 +1,8 @@
import json
import sys
from contextlib import nullcontext
from .receiver_scope import authorize_request
from .receiver_scope import authorized_request
MAX_REQUEST_BYTES = 4096
@@ -16,6 +17,8 @@ def serve_apply_request(
include_result=False,
probe_role=None,
):
from .node_maintenance import rollout_target
report = {"version": manifest_version}
try:
body = sys.stdin.buffer.read(limit + 1)
@@ -30,7 +33,6 @@ def serve_apply_request(
and isinstance(payload, dict)
and payload.get("operation") == "probe"
):
from .node_maintenance import rollout_target
from .node_probe import (
PROBE,
STATUS_PROBE,
@@ -46,9 +48,9 @@ def serve_apply_request(
report = probe_identity(probe_role) | {
"version": payload["version"]
}
authorize_request(probe_role)
check_receiver(probe_role)
target = rollout_target(probe_role)
with authorized_request(probe_role):
check_receiver(probe_role)
target = rollout_target(probe_role)
if target and payload == PROBE:
raise ValueError("The receiver is in upgrade maintenance.")
if target:
@@ -64,16 +66,19 @@ def serve_apply_request(
)
return 0
else:
if probe_role is not None:
from .node_maintenance import rollout_target
if rollout_target(probe_role):
raise ValueError("The receiver is in upgrade maintenance.")
if probe_role is not None and rollout_target(probe_role):
raise ValueError("The receiver is in upgrade maintenance.")
spec = parse_manifest(payload)
report = spec.manifest() if identity is None else identity(spec)
if probe_role is not None:
authorize_request(probe_role, spec)
result = apply(spec)
guard = (
nullcontext()
if probe_role is None
else authorized_request(probe_role, spec)
)
with guard:
if probe_role is not None and rollout_target(probe_role):
raise ValueError("The receiver is in upgrade maintenance.")
result = apply(spec)
if include_result and result is not None:
report.update(result)
except (OSError, ValueError) as error:
+3 -2
View File
@@ -248,11 +248,12 @@ def check_ssh_receiver(role, account=None):
raise ValueError(f"Check the effective SSH policy for {account}.")
if account != role.receiver and any(
line.startswith("acceptenv ")
and set(line.split()[1:]) - {"LANG", "LC_*"}
and set(line.split()[1:]) - {"LANG", "LC_*", "COLORTERM", "NO_COLOR"}
for line in live.splitlines()
):
raise ValueError(
"Restrict accepted SSH environment variables to locale settings."
"Restrict accepted SSH environment variables to locale and "
"terminal color settings."
)
+24 -13
View File
@@ -6,9 +6,14 @@ import socket
import stat
import struct
import sys
from contextlib import contextmanager
from .domain_names import canonical_domain
from .node_files import read_root_file, require_root_directory
from .node_files import (
locked_root_directory,
read_root_file,
require_root_directory,
)
from .node_maintenance import NODE_KEYS, rollout_target
LOCAL_WORKERS = {
@@ -84,21 +89,27 @@ def manifest_domain(spec):
return manifest.get("domain")
def authorize_request(role, spec=None):
@contextmanager
def authorized_request(role, spec=None):
peer = socket_peer()
if peer in (None, "root", LOCAL_WORKERS[role]):
yield
return
if peer == f"coulobre-{role}-request":
if scope_files(role):
raise ValueError(
"Use a domain-scoped panel key for this receiver."
)
return
if not peer.startswith(f"coulobre-{role}-p-"):
raise ValueError("The peer is not authorized for this receiver.")
domains = scoped_domains(scope_path(peer))
if spec is not None and manifest_domain(spec) not in domains:
raise ValueError("The panel key is not assigned to this domain.")
with locked_root_directory(NODE_KEYS, shared=True):
if peer == f"coulobre-{role}-request":
if scope_files(role):
raise ValueError(
"Use a domain-scoped panel key for this receiver."
)
elif peer.startswith(f"coulobre-{role}-p-"):
domains = scoped_domains(scope_path(peer))
if spec is not None and manifest_domain(spec) not in domains:
raise ValueError(
"The panel key is not assigned to this domain."
)
else:
raise ValueError("The peer is not authorized for this receiver.")
yield
def check_mail_service_peer():
+17 -7
View File
@@ -20,7 +20,19 @@ from .node_retirement import RetirementRequest
from .service_access import ServiceAccessRequest
def check_request(role, spec=None):
with receiver_scope.authorized_request(role, spec):
pass
class ReceiverScopeTests(TestCase):
def setUp(self):
lock = patch.object(
receiver_scope, "locked_root_directory", return_value=nullcontext()
)
lock.start()
self.addCleanup(lock.stop)
def test_unix_peer_identity_comes_from_the_kernel(self):
left, right = socket.socketpair()
with (
@@ -58,12 +70,10 @@ class ReceiverScopeTests(TestCase):
),
):
for request in requests:
receiver_scope.authorize_request("mail", request)
check_request("mail", request)
for domain in ("beta.example.test", "sub.alpha.example.test"):
with self.assertRaisesRegex(ValueError, "not assigned"):
receiver_scope.authorize_request(
"mail", MailQueueRequest(domain)
)
check_request("mail", MailQueueRequest(domain))
def test_scope_is_enforced_before_dispatching_dns_operations(self):
zone = DnsZone(
@@ -112,16 +122,16 @@ class ReceiverScopeTests(TestCase):
),
):
with self.assertRaisesRegex(ValueError, "domain-scoped"):
receiver_scope.authorize_request("mail")
check_request("mail")
peer.return_value = "coulobre-dns-p-alpha"
with self.assertRaisesRegex(ValueError, "not authorized"):
receiver_scope.authorize_request("mail")
check_request("mail")
for name in receiver_scope.MAIL_SERVICE_PEERS:
peer.return_value = name
with self.assertRaisesRegex(ValueError, "unavailable"):
receiver_scope.check_mail_service_peer()
peer.return_value = "root"
receiver_scope.authorize_request("mail")
check_request("mail")
receiver_scope.check_mail_service_peer()
def test_scopes_reject_overlap_and_record_both_keys(