|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Generate a mini-enclava attestation policy document.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import argparse |
| 7 | +import json |
| 8 | +from datetime import datetime, timezone |
| 9 | + |
| 10 | +SCHEMA_ID = "mini-enclava-attestation-policy/v1" |
| 11 | +DEFAULT_ATTESTATION_TYPE = "coco-sev-snp" |
| 12 | +DEFAULT_RUNTIME_CLASS = "kata-qemu-snp" |
| 13 | +DEFAULT_KBS_RESOURCE_PATH = "default/flowforge-storage/workload-secret-seed" |
| 14 | +DEFAULT_REPORT_POLICY = 196608 |
| 15 | + |
| 16 | + |
| 17 | +def utc_now() -> str: |
| 18 | + return datetime.now(timezone.utc).isoformat() |
| 19 | + |
| 20 | + |
| 21 | +def build_policy(args: argparse.Namespace) -> dict: |
| 22 | + return { |
| 23 | + "schema": SCHEMA_ID, |
| 24 | + "generated_at": utc_now(), |
| 25 | + "generator": { |
| 26 | + "name": "mini-enclava-policy-ci", |
| 27 | + "source_ref": args.source_ref, |
| 28 | + "image_tag": args.image_tag, |
| 29 | + }, |
| 30 | + "expected": { |
| 31 | + "attestation_type": args.attestation_type, |
| 32 | + "runtime_class": args.runtime_class, |
| 33 | + "workload_image": args.workload_image, |
| 34 | + "kbs_resource_path": args.kbs_resource_path, |
| 35 | + }, |
| 36 | + "report_constraints": { |
| 37 | + "vmpl": args.vmpl, |
| 38 | + "policy": args.report_policy, |
| 39 | + "measurement_hex": args.measurement_hex.lower() if args.measurement_hex else None, |
| 40 | + }, |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | +def parse_args() -> argparse.Namespace: |
| 45 | + parser = argparse.ArgumentParser(description="Generate mini-enclava attestation policy JSON") |
| 46 | + parser.add_argument("--output", required=True, help="Output policy path") |
| 47 | + parser.add_argument("--workload-image", required=True, help="Pinned image reference (must include @sha256)") |
| 48 | + parser.add_argument("--source-ref", default="", help="Git ref that produced this policy") |
| 49 | + parser.add_argument("--image-tag", default="", help="Human-friendly image tag") |
| 50 | + parser.add_argument("--attestation-type", default=DEFAULT_ATTESTATION_TYPE) |
| 51 | + parser.add_argument("--runtime-class", default=DEFAULT_RUNTIME_CLASS) |
| 52 | + parser.add_argument("--kbs-resource-path", default=DEFAULT_KBS_RESOURCE_PATH) |
| 53 | + parser.add_argument("--vmpl", type=int, default=0) |
| 54 | + parser.add_argument("--report-policy", type=int, default=DEFAULT_REPORT_POLICY) |
| 55 | + parser.add_argument("--measurement-hex", default=None) |
| 56 | + parser.add_argument("--allow-non-digest-image", action="store_true") |
| 57 | + return parser.parse_args() |
| 58 | + |
| 59 | + |
| 60 | +def main() -> int: |
| 61 | + args = parse_args() |
| 62 | + if "@sha256:" not in args.workload_image and not args.allow_non_digest_image: |
| 63 | + raise SystemExit("workload image must be digest-pinned (@sha256:...)") |
| 64 | + |
| 65 | + policy = build_policy(args) |
| 66 | + with open(args.output, "w", encoding="utf-8") as f: |
| 67 | + json.dump(policy, f, indent=2) |
| 68 | + f.write("\n") |
| 69 | + |
| 70 | + print(f"wrote policy: {args.output}") |
| 71 | + print(f"workload_image={args.workload_image}") |
| 72 | + return 0 |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == "__main__": |
| 76 | + raise SystemExit(main()) |
0 commit comments