Reload current rehearsal attestation key at approval checks
ABSDEV Framework CI / framework-ci (push) Successful in 20s

This commit is contained in:
Mark Gregory
2026-11-30 11:57:09 +00:00
parent 2a001b1269
commit 054c546c8f
4 changed files with 70 additions and 28 deletions
+25 -21
View File
@@ -88,6 +88,30 @@ class OperatorApprovalPolicy:
self.authorise(approval, "execute", now) self.authorise(approval, "execute", now)
def read_protected_authority(path, *, group, max_bytes):
# Ancestors are trusted; open the parent and file without following links.
directory = os.open(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 == group
and stat.S_IMODE(info.st_mode) == 0o750,
"Unsafe operator authority directory")
descriptor = os.open(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 == group and info.st_nlink == 1
and stat.S_IMODE(info.st_mode) == 0o440,
"Unsafe operator authority file")
data = stream.read(max_bytes + 1)
finally:
os.close(directory)
require(0 < len(data) <= max_bytes, "Invalid operator authority size")
return data
class FileOperatorAuthority: class FileOperatorAuthority:
"""Load a fresh, atomic authority snapshot from a trusted local provisioner.""" """Load a fresh, atomic authority snapshot from a trusted local provisioner."""
@@ -100,27 +124,7 @@ class FileOperatorAuthority:
and type(group) is int and group > 0, "Invalid operator authority binding") and type(group) is int and group > 0, "Invalid operator authority binding")
def __call__(self): def __call__(self):
# The parent and its ancestors are trusted, and the final entry cannot data = read_protected_authority(self.path, group=self.group, max_bytes=self.MAX_BYTES)
# 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) snapshot = json.loads(data)
require(isinstance(snapshot, dict) and set(snapshot) == {"grants", "sealing_key"} require(isinstance(snapshot, dict) and set(snapshot) == {"grants", "sealing_key"}
and isinstance(snapshot["grants"], list), "Invalid operator authority snapshot") and isinstance(snapshot["grants"], list), "Invalid operator authority snapshot")
+26 -5
View File
@@ -2,26 +2,34 @@
import copy import copy
import hmac import hmac
from pathlib import Path
from core.operator_approval import read_protected_authority
from core.rehearsal import ( from core.rehearsal import (
RECORD_FIELDS, approval_context, create_rehearsal, matches, require, RECORD_FIELDS, RehearsalError, approval_context, create_rehearsal, matches, require,
validate_base_context, validate_base_context,
) )
class TrustedRehearsal: class TrustedRehearsal:
def __init__(self, runner_id, runner, *, key, clock): def __init__(self, runner_id, runner, *, key_loader, clock):
require(matches(runner_id, r"[a-zA-Z0-9_.-]{1,128}"), "Invalid rehearsal runner identity") require(matches(runner_id, r"[a-zA-Z0-9_.-]{1,128}"), "Invalid rehearsal runner identity")
require(callable(runner) and callable(clock), "Trusted rehearsal runner and clock required") require(callable(runner) and callable(clock), "Trusted rehearsal runner and clock required")
require(isinstance(key, bytes) and len(key) >= 32, "Invalid rehearsal trust key") require(callable(key_loader), "Trusted rehearsal key loader required")
self.runner_id = runner_id self.runner_id = runner_id
self._runner = runner self._runner = runner
self._key = key self._key_loader = key_loader
self._clock = clock self._clock = clock
def _seal(self, fingerprint): def _seal(self, fingerprint):
payload = f"absdev:trusted-rehearsal:v1:{self.runner_id}:{fingerprint}".encode("ascii") payload = f"absdev:trusted-rehearsal:v1:{self.runner_id}:{fingerprint}".encode("ascii")
return hmac.digest(self._key, payload, "sha256").hex() try:
key = self._key_loader()
except Exception:
raise RehearsalError("Unable to verify current rehearsal authority") from None
require(isinstance(key, bytes) and len(key) >= 32, "Invalid rehearsal trust key")
return hmac.digest(key, payload, "sha256").hex()
def run(self, context): def run(self, context):
context = copy.deepcopy(context) context = copy.deepcopy(context)
@@ -42,3 +50,16 @@ class TrustedRehearsal:
require(hmac.compare_digest(evidence["runner_seal"], self._seal(record["fingerprint"])), require(hmac.compare_digest(evidence["runner_seal"], self._seal(record["fingerprint"])),
"Untrusted rehearsal runner attestation") "Untrusted rehearsal runner attestation")
return approved return approved
class FileRehearsalKey:
"""Read a protected current key snapshot for each attestation operation."""
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 rehearsal key binding")
def __call__(self):
return read_protected_authority(self.path, group=self.group, max_bytes=4096)
+9 -1
View File
@@ -1,6 +1,6 @@
# Trusted rehearsal evidence # Trusted rehearsal evidence
Configure `TrustedRehearsal(runner_id, runner, key=..., clock=...)` in trusted Configure `TrustedRehearsal(runner_id, runner, key_loader=..., clock=...)` in trusted
runtime code and supply it to `SiteOperations(trusted_rehearsal=...)`. runtime code and supply it to `SiteOperations(trusted_rehearsal=...)`.
Use a separately managed random key of at least 32 bytes and a stable runner Use a separately managed random key of at least 32 bytes and a stable runner
identifier. Only trusted runtime processes may have access to this key. identifier. Only trusted runtime processes may have access to this key.
@@ -23,3 +23,11 @@ authorise a migration.
The configured runner and protected key provisioning are deployment inputs. The configured runner and protected key provisioning are deployment inputs.
Production execution remains disabled. Production execution remains disabled.
The loader must re-read the current key on each call. Use
`FileRehearsalKey(path, group=...)` for a root-owned binary key file (0440)
in a root-owned directory (0750) with the configured reader group. Keep the
ancestors under trusted administrative control. Replace the file atomically.
The runtime rejects missing, unreadable, or unsafe key snapshots and newly
checks the key at both approval issuance and consumption. A rotation
invalidates outstanding attestations under the retired key at the next check.
+10 -1
View File
@@ -63,9 +63,10 @@ class ApprovalStoreTests(unittest.TestCase):
}, },
} }
self.rehearsal_key = os.urandom(32)
self.trusted_rehearsal = TrustedRehearsal( self.trusted_rehearsal = TrustedRehearsal(
"fixture-runner", lambda _context: self.execution, "fixture-runner", lambda _context: self.execution,
key=os.urandom(32), clock=lambda: self.now, key_loader=lambda: self.rehearsal_key, clock=lambda: self.now,
) )
self.rehearsal = self.trusted_rehearsal.run(self.base) self.rehearsal = self.trusted_rehearsal.run(self.base)
self.context = self.trusted_rehearsal.approval_context( self.context = self.trusted_rehearsal.approval_context(
@@ -572,5 +573,13 @@ class ApprovalStoreTests(unittest.TestCase):
self.assertFalse(self.store.approval_path(self.approval["approval_id"]).exists()) self.assertFalse(self.store.approval_path(self.approval["approval_id"]).exists())
def test_running_store_rejects_retired_rehearsal_key(self):
self.save()
self.rehearsal_key = os.urandom(32)
with self.assertRaises(ApprovalStoreError):
self.consume()
self.assertFalse(self.store.consumed_path(self.identifier).exists())
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()