74 lines
3.9 KiB
Python
74 lines
3.9 KiB
Python
import json
|
|
import os
|
|
import sys
|
|
from datetime import datetime, timedelta, timezone
|
|
from pathlib import Path
|
|
|
|
from core.approval import issue_approval
|
|
from core.approval_store import ApprovalStore, ApprovalStoreError
|
|
from core.operator_approval import OperatorApprovalPolicy, OperatorGrant
|
|
from core.rehearsal import approval_context, create_rehearsal
|
|
|
|
root = Path(sys.argv[1])
|
|
action = sys.argv[2]
|
|
config = json.loads((root / "policy.json").read_text())
|
|
now = datetime(2026, 11, 28, 12, tzinfo=timezone.utc)
|
|
base = dict(site="operator-lab", release="2", revision="a"*40, provenance="b"*64,
|
|
compatibility="c"*64, database_identity="d"*64, backup_sha256="e"*64,
|
|
pending_migrations=["2026_11_28_000000_create_fixture"])
|
|
execution = dict(driver="postgresql", source_database="source", target_database="isolated_restore_fixture",
|
|
target_identity="f"*64, applied_migrations=base["pending_migrations"],
|
|
checks=dict.fromkeys(("isolation_verified", "backup_restored", "migration_succeeded",
|
|
"migration_history_verified", "application_verified",
|
|
"cleanup_verified"), True))
|
|
rehearsal = create_rehearsal(base, execution, completed_at=now)
|
|
rehearsal_path = root / "state" / "rehearsal.json"
|
|
if action not in ("issue", "wrong-principal"):
|
|
rehearsal = json.loads(rehearsal_path.read_text())
|
|
context = approval_context(rehearsal, base, now=now)
|
|
policy = OperatorApprovalPolicy([
|
|
OperatorGrant(config["uid"], "fixture-operator", op, base["site"], base["revision"],
|
|
now + timedelta(hours=1))
|
|
for op in config["operations"]
|
|
], sealing_key=(root / "operator.key").read_bytes())
|
|
store = ApprovalStore(root / "state", operator_policy=policy)
|
|
if action in ("issue", "wrong-principal"):
|
|
draft = issue_approval(context, approver=("other-principal" if action == "wrong-principal"
|
|
else "fixture-operator"), now=now)
|
|
try:
|
|
saved = store.save(draft, context, confirmation=draft["confirmation"], now=now)
|
|
except ApprovalStoreError as error:
|
|
assert action == "wrong-principal" and "not authorised" in str(error)
|
|
assert not store.approval_path(draft["approval_id"]).exists()
|
|
print("PASS: actual operator UID cannot issue for another principal")
|
|
else:
|
|
assert action == "issue" and saved["format_version"] == 2
|
|
rehearsal_path.write_text(json.dumps(rehearsal))
|
|
(root / "state" / "current").write_text(saved["approval_id"])
|
|
print("PASS: dedicated OS account issued sealed approval")
|
|
else:
|
|
identifier = (root / "state" / "current").read_text()
|
|
approval = store.load(identifier)
|
|
marker = store.consumed_path(identifier)
|
|
before = marker.read_bytes() if marker.exists() else None
|
|
try:
|
|
result = store.consume(identifier, base, rehearsal, confirmation=approval["confirmation"], now=now)
|
|
except ApprovalStoreError as error:
|
|
assert action in ("replay", "grant-revoked", "key-revoked")
|
|
assert (marker.read_bytes() if marker.exists() else None) == before
|
|
if action == "replay":
|
|
assert before and "already consumed" in str(error)
|
|
else:
|
|
assert before is None
|
|
try:
|
|
store.verify_execution(approval, context, confirmation=approval["confirmation"], now=now)
|
|
except ApprovalStoreError as cause:
|
|
expected = "not authorised" if action == "grant-revoked" else "invalid operator approval seal"
|
|
assert expected in str(cause)
|
|
else:
|
|
raise AssertionError("Preflight accepted revoked authority")
|
|
print("PASS:", action, "rejected without changing consumption state")
|
|
else:
|
|
assert action == "consume" and result["migration_executed"] is False
|
|
print("PASS: fresh process consumed an authorised approval without executing migrations")
|