diff --git a/core/approval_store.py b/core/approval_store.py index 9777eac..a55befb 100644 --- a/core/approval_store.py +++ b/core/approval_store.py @@ -307,9 +307,14 @@ class ApprovalStore: return record def require_operator_policy(self): - require(isinstance(self.operator_policy, OperatorApprovalPolicy), + require(callable(self.operator_policy), "Trusted migration operator policy loader required") + try: + policy = self.operator_policy() + except Exception: + raise ApprovalStoreError("Unable to verify current migration operator authority") from None + require(isinstance(policy, OperatorApprovalPolicy), "Trusted migration operator policy required") - return self.operator_policy + return policy def verify_execution(self, approval, context, *, confirmation, now): verify_approval(approval, context, confirmation=confirmation, now=now) diff --git a/core/operator_approval.py b/core/operator_approval.py index c10aa7e..b8ff953 100644 --- a/core/operator_approval.py +++ b/core/operator_approval.py @@ -1,10 +1,14 @@ """Local operator identity and scoped migration approval policy.""" import hmac +import json +import os +import stat from dataclasses import dataclass from datetime import datetime from os import geteuid +from pathlib import Path from core.approval import ( ApprovalError, @@ -82,3 +86,46 @@ class OperatorApprovalPolicy: def verify_execution(self, approval, now): self.verify_seal(approval) self.authorise(approval, "execute", now) + + +class FileOperatorAuthority: + """Load a fresh, atomic authority snapshot from a trusted local provisioner.""" + + MAX_BYTES = 65536 + + def __init__(self, path, *, group): + self.path = Path(path) + self.group = group + require(self.path.is_absolute() and self.path.parent.resolve() == self.path.parent + and type(group) is int and group > 0, "Invalid operator authority binding") + + def __call__(self): + # The parent and its ancestors are trusted, and the final entry cannot + # be replaced by the reader or resolved through a symbolic link. + directory = os.open(self.path.parent, os.O_RDONLY | os.O_DIRECTORY + | os.O_NOFOLLOW | os.O_CLOEXEC) + try: + info = os.fstat(directory) + require(info.st_uid == 0 and info.st_gid == self.group + and stat.S_IMODE(info.st_mode) == 0o750, + "Unsafe operator authority directory") + descriptor = os.open(self.path.name, os.O_RDONLY | os.O_NOFOLLOW + | os.O_NONBLOCK | os.O_CLOEXEC, dir_fd=directory) + with os.fdopen(descriptor, "rb") as stream: + info = os.fstat(stream.fileno()) + require(stat.S_ISREG(info.st_mode) and info.st_uid == 0 + and info.st_gid == self.group and info.st_nlink == 1 + and stat.S_IMODE(info.st_mode) == 0o440, + "Unsafe operator authority file") + data = stream.read(self.MAX_BYTES + 1) + finally: + os.close(directory) + require(0 < len(data) <= self.MAX_BYTES, "Invalid operator authority size") + snapshot = json.loads(data) + require(isinstance(snapshot, dict) and set(snapshot) == {"grants", "sealing_key"} + and isinstance(snapshot["grants"], list), "Invalid operator authority snapshot") + grants = [ + OperatorGrant(**dict(grant, expires_at=datetime.fromisoformat(grant["expires_at"]))) + for grant in snapshot["grants"] + ] + return OperatorApprovalPolicy(grants, sealing_key=bytes.fromhex(snapshot["sealing_key"])) diff --git a/docs/MIGRATION-OPERATOR-AUTHORISATION.md b/docs/MIGRATION-OPERATOR-AUTHORISATION.md index 4189e01..89ac266 100644 --- a/docs/MIGRATION-OPERATOR-AUTHORISATION.md +++ b/docs/MIGRATION-OPERATOR-AUTHORISATION.md @@ -1,8 +1,8 @@ # Local migration operator authorisation -The supported development composition accepts an `OperatorApprovalPolicy` -through `SiteOperations(operator_policy=...)`. Configure the policy in trusted -runtime code with a one-to-one mapping from dedicated OS accounts to operator +The supported development composition accepts a policy loader through +`SiteOperations(operator_policy=...)`. The loader must return a fresh +`OperatorApprovalPolicy` on each call. Configure the policy in trusted runtime code with a one-to-one mapping from dedicated OS accounts to operator principals. The policy authenticates the process's effective UID. The operator's login or service launcher must authenticate access to that account. A shared service account cannot provide individual operator identity. @@ -24,7 +24,20 @@ for audit. Unsealed recovery evidence requires manual review. Recovery evidence verification requires access to the sealing key used for issuance. The runtime process, its code, policy, key, clock and state directory are trusted. -The policy is constructed from a configuration snapshot. Reload it before -further execution when grants are revoked or changed. The approved account + +Use `FileOperatorAuthority(path, group=...)` for a local authority source. Place +the root-owned mode 0440 JSON file in a root-owned mode 0750 directory whose +group matches the configured reader group, under administrator-controlled +ancestors. The JSON object contains `sealing_key` as hex and `grants` as a list +of objects with `uid`, `principal`, `operation`, `site`, `revision` and +`expires_at` (timezone-aware ISO 8601). Atomically replace the whole file when +removing grants or changing the key. The runtime reopens it for each authority +check, including consumption under the approval lock. Missing, unreadable, +unsafe or malformed state denies the operation. Key rotation invalidates +outstanding approvals and puts evidence sealed with the retired key into +manual review. Revocation is evaluated at the next authority read and cannot +recall an operation that already passed its final consumption check. + +The approved account must never be exposed as a shared application or arbitrary command identity. The inspection CLI remains disabled for production execution. diff --git a/tests/unit/test_approval_store.py b/tests/unit/test_approval_store.py index 096a25e..8e665f1 100644 --- a/tests/unit/test_approval_store.py +++ b/tests/unit/test_approval_store.py @@ -96,7 +96,7 @@ class ApprovalStoreTests(unittest.TestCase): for operation in ("issue", "execute") ] self.policy = OperatorApprovalPolicy(self.grants, sealing_key=self.key) - self.store = ApprovalStore(self.root, operator_policy=self.policy) + self.store = ApprovalStore(self.root, operator_policy=lambda: self.policy) def save(self): @@ -171,7 +171,7 @@ class ApprovalStoreTests(unittest.TestCase): self.save() self.consume() - self.store = ApprovalStore(self.root, operator_policy=self.policy) + self.store = ApprovalStore(self.root, operator_policy=lambda: self.policy) with self.assertRaisesRegex(ApprovalStoreError, "already consumed"): self.consume() @@ -483,13 +483,13 @@ class ApprovalStoreTests(unittest.TestCase): def test_missing_policy_denies_issuance(self): self.store = ApprovalStore(self.root) - with self.assertRaisesRegex(ApprovalStoreError, "policy required"): + with self.assertRaisesRegex(ApprovalStoreError, "policy loader required"): self.save() self.assertFalse(self.store.approval_path(self.identifier).exists()) def test_unrecognised_os_identity_denies_issuance(self): other_account = [replace(grant, uid=self.uid + 1) for grant in self.grants] - self.store.operator_policy = OperatorApprovalPolicy(other_account, sealing_key=self.key) + self.policy = OperatorApprovalPolicy(other_account, sealing_key=self.key) with self.assertRaisesRegex(ApprovalStoreError, "not authorised"): self.save() self.assertFalse(self.store.approval_path(self.identifier).exists()) @@ -502,7 +502,7 @@ class ApprovalStoreTests(unittest.TestCase): replace(execute, principal="other-operator"), replace(execute, expires_at=self.now)): with self.subTest(grant=grant): - self.store.operator_policy = OperatorApprovalPolicy([grant], sealing_key=self.key) + self.policy = OperatorApprovalPolicy([grant], sealing_key=self.key) with self.assertRaises(ApprovalStoreError): self.consume() self.assertFalse(self.store.consumed_path(self.identifier).exists()) @@ -522,5 +522,26 @@ class ApprovalStoreTests(unittest.TestCase): self.consume() self.assertFalse(self.store.consumed_path(self.identifier).exists()) + + def test_static_authority_is_rejected(self): + self.store.operator_policy = self.policy + with self.assertRaisesRegex(ApprovalStoreError, "policy loader required"): + self.save() + + def test_unavailable_authority_fails_closed_after_issuance(self): + self.save() + with patch.object(self.store, "operator_policy", side_effect=OSError("unavailable")): + with self.assertRaises(ApprovalStoreError): + self.consume() + self.assertFalse(self.store.consumed_path(self.identifier).exists()) + + def test_running_store_rejects_retired_sealing_key(self): + self.save() + self.policy = OperatorApprovalPolicy(self.grants, sealing_key=os.urandom(32)) + with self.assertRaises(ApprovalStoreError): + self.consume() + self.assertFalse(self.store.consumed_path(self.identifier).exists()) + + if __name__ == "__main__": unittest.main() diff --git a/tests/unit/test_migration.py b/tests/unit/test_migration.py index 7f58aab..0d49b0f 100644 --- a/tests/unit/test_migration.py +++ b/tests/unit/test_migration.py @@ -17,7 +17,7 @@ class MigrationTests(unittest.TestCase): self.fixture = test_approval_store.ApprovalStoreTests() self.addCleanup(self.fixture.doCleanups) self.fixture.setUp() - self.ops = SiteOperations(self.fixture.root, operator_policy=self.fixture.policy) + self.ops = SiteOperations(self.fixture.root, operator_policy=lambda: self.fixture.policy) self.fixture.store = self.ops.approvals self.fixture.save() @@ -367,7 +367,7 @@ class MigrationTests(unittest.TestCase): def test_missing_execution_authority_blocks_before_containment(self): from core.operator_approval import OperatorApprovalPolicy - self.fixture.store.operator_policy = OperatorApprovalPolicy( + self.fixture.policy = OperatorApprovalPolicy( self.fixture.grants[:1], sealing_key=self.fixture.key, ) with self.assertRaises(MigrationError):