#!/usr/bin/env python3 import json import subprocess import uuid from pathlib import Path from client import Client, arguments, run def compose(*args): result = subprocess.run( ["docker", "compose", "--env-file", ".env", *args], cwd=Path(__file__).resolve().parents[1], capture_output=True, text=True, check=True, ) return result.stdout def reconcile(apply=False, grace="24"): command = ["exec", "-T", "api1", "python", "/app/reconcile.py", "--grace-hours", grace] if apply: command.append("--apply") return compose(*command) def main(): args = arguments("Track 2 reconciler fault-injection proof") c = Client(args.base_url, args.ca_file) alice, bob, eve, chat = c.bootstrap() attachment, content = c.attachment(alice, chat) orphan_key = f"reconcile-fault/orphan-{uuid.uuid4()}" compose("exec", "-T", "api1", "python", "-c", "import boto3,os;" f"c=boto3.client('s3',endpoint_url=os.environ['S3_ENDPOINT'],aws_access_key_id=os.environ['S3_ACCESS_KEY_ID'],aws_secret_access_key=os.environ['S3_SECRET_ACCESS_KEY'],region_name='us-east-1');" f"c.put_object(Bucket=os.environ['S3_BUCKET'],Key='{orphan_key}',Body=b'orphan')") compose( "exec", "-T", "api1", "python", "-c", "import boto3,os,psycopg;" f"con=psycopg.connect(os.environ['DATABASE_URL']);" f"cur=con.execute('SELECT object_key FROM attachments WHERE id=%s',('{attachment['id']}',));" "key=cur.fetchone()[0];" "c=boto3.client('s3',endpoint_url=os.environ['S3_ENDPOINT']," "aws_access_key_id=os.environ['S3_ACCESS_KEY_ID']," "aws_secret_access_key=os.environ['S3_SECRET_ACCESS_KEY'],region_name='us-east-1');" "c.delete_object(Bucket=os.environ['S3_BUCKET'],Key=key)", ) before = reconcile(False, "24") report = { "scenario": "missing metadata object and orphan object", "dry_run_before_apply": before.strip(), "apply_grace_hours": 0, } print(json.dumps(report, ensure_ascii=False, indent=2)) apply_fresh = reconcile(True, "0") print(apply_fresh.strip()) second = reconcile(True, "0") print(second.strip()) report["apply_output"] = apply_fresh.strip() report["repeat_output"] = second.strip() evidence = Path(__file__).resolve().parents[1] / "evidence" / "reconciler-track2.json" evidence.parent.mkdir(parents=True, exist_ok=True) evidence.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") if __name__ == "__main__": run(main)