81 lines
3.6 KiB
Python
81 lines
3.6 KiB
Python
import os
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path("tests/unit").resolve()))
|
|
from test_approval_store import ApprovalStoreTests
|
|
from core.approval_store import ApprovalStore, ApprovalStoreError
|
|
from core.operator_approval import FileOperatorAuthority
|
|
import core.trusted_rehearsal as rehearsal_module
|
|
from core.trusted_rehearsal import TrustedRehearsal
|
|
|
|
fixture = ApprovalStoreTests()
|
|
try:
|
|
fixture.setUp()
|
|
assert os.geteuid() > 0
|
|
fixture.base["site"] = json.loads(Path("site-manifest.json").read_text())["site"]["identifier"]
|
|
attesting = len(sys.argv) > 2 and sys.argv[2] == "--attest"
|
|
key_path = Path(sys.argv[1]) if attesting else Path(sys.argv[1]).parent / "rehearsal-key"
|
|
if hasattr(rehearsal_module, "FileRehearsalKey"):
|
|
info = key_path.stat()
|
|
key_options = {"key_loader": rehearsal_module.FileRehearsalKey(key_path, group=info.st_gid)}
|
|
else:
|
|
key_options = {"key": key_path.read_bytes()}
|
|
trusted = TrustedRehearsal(
|
|
"staging-rehearsal", lambda _context: fixture.execution,
|
|
clock=lambda: fixture.now, **key_options,
|
|
)
|
|
if attesting:
|
|
print(json.dumps(trusted.run(fixture.base)))
|
|
raise SystemExit(0)
|
|
fixture.rehearsal = json.loads((key_path.parent / "attestation.json").read_text())
|
|
fixture.context = trusted.approval_context(fixture.rehearsal, fixture.base, now=fixture.now)
|
|
fixture.store = ApprovalStore(
|
|
fixture.root, operator_policy=FileOperatorAuthority(sys.argv[1], group=os.getegid()),
|
|
trusted_rehearsal=trusted,
|
|
)
|
|
original_draft = fixture.approval
|
|
for action in sys.stdin:
|
|
action = action.strip()
|
|
if action == "reload-attestation":
|
|
fixture.rehearsal = json.loads((key_path.parent / "attestation.json").read_text())
|
|
fixture.context = trusted.approval_context(fixture.rehearsal, fixture.base, now=fixture.now)
|
|
print("loaded", flush=True)
|
|
elif action == "deny-issue":
|
|
from core.approval import issue_approval
|
|
draft = issue_approval(fixture.context, approver=original_draft["approver"], now=fixture.now)
|
|
try:
|
|
fixture.store.save(draft, fixture.context, rehearsal=fixture.rehearsal,
|
|
confirmation=draft["confirmation"], now=fixture.now)
|
|
except ApprovalStoreError:
|
|
assert not fixture.store.approval_path(draft["approval_id"]).exists()
|
|
print("rejected", flush=True)
|
|
else:
|
|
raise AssertionError("Retired key allowed issuance")
|
|
elif action == "issue":
|
|
from core.approval import issue_approval
|
|
fixture.approval = issue_approval(fixture.context, approver=original_draft["approver"],
|
|
now=fixture.now)
|
|
fixture.identifier = fixture.approval["approval_id"]
|
|
fixture.save()
|
|
print("issued", flush=True)
|
|
elif action == "consume":
|
|
result = fixture.consume()
|
|
assert result["migration_executed"] is False
|
|
print("consumed", flush=True)
|
|
else:
|
|
try:
|
|
fixture.consume()
|
|
except ApprovalStoreError as error:
|
|
marker = fixture.store.consumed_path(fixture.identifier)
|
|
if action == "replay":
|
|
assert "already consumed" in str(error) and marker.exists()
|
|
else:
|
|
assert not marker.exists()
|
|
print("rejected", flush=True)
|
|
else:
|
|
raise AssertionError("Expected rejection")
|
|
finally:
|
|
fixture.doCleanups()
|