63 lines
2.5 KiB
Python
63 lines
2.5 KiB
Python
import json
|
|
import os
|
|
import secrets
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
account, uid, gid, root = sys.argv[1:]
|
|
uid, gid, root = int(uid), int(gid), Path(root)
|
|
path = root / "authority.json"
|
|
key = secrets.token_hex(32)
|
|
|
|
def publish(actions, signing_key):
|
|
grants = [dict(uid=uid, principal="operator", operation=op, site="example-absdev-net",
|
|
revision="a"*40, expires_at="2026-12-01T10:00:00+00:00") for op in actions]
|
|
with tempfile.NamedTemporaryFile(mode="w", dir=root, delete=False) as stream:
|
|
json.dump({"grants": grants, "sealing_key": signing_key}, stream)
|
|
stream.flush()
|
|
os.fchown(stream.fileno(), 0, gid)
|
|
os.fchmod(stream.fileno(), 0o440)
|
|
os.fsync(stream.fileno())
|
|
os.replace(stream.name, path)
|
|
|
|
def report(check):
|
|
print(json.dumps({"check": check, "result": "PASS"}), flush=True)
|
|
|
|
publish(("issue", "execute"), key)
|
|
with subprocess.Popen(("runuser", "-u", account, "--", "env", "TMPDIR=" + str(root.parent / "operator-tmp"), "python3", "-B", "worker.py", str(path)),
|
|
stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True) as process:
|
|
def send(action, expected):
|
|
process.stdin.write(action + "\n")
|
|
process.stdin.flush()
|
|
assert process.stdout.readline().strip() == expected, action
|
|
|
|
send("issue", "issued")
|
|
send("consume", "consumed")
|
|
send("replay", "rejected")
|
|
report("authorised approval consumed once and replay rejected")
|
|
send("issue", "issued")
|
|
publish(("issue",), key)
|
|
send("reject", "rejected")
|
|
report("same running process rejected unconsumed approval after execute grant removal")
|
|
publish(("issue", "execute"), key)
|
|
rotated = secrets.token_hex(32)
|
|
publish(("issue", "execute"), rotated)
|
|
send("reject", "rejected")
|
|
report("same running process rejected an approval sealed by the retired key")
|
|
send("issue", "issued")
|
|
path.unlink()
|
|
send("reject", "rejected")
|
|
report("missing authority failed closed without a consumption marker")
|
|
publish(("issue", "execute"), rotated)
|
|
path.chmod(0o444)
|
|
send("reject", "rejected")
|
|
report("unsafe authority permissions failed closed without a consumption marker")
|
|
publish(("issue", "execute"), rotated)
|
|
send("consume", "consumed")
|
|
send("replay", "rejected")
|
|
report("replacement key restored authorised consumption in the original process")
|
|
process.stdin.close()
|
|
assert process.wait(timeout=10) == 0
|