forked from mtusi2026/mtusi2026lab5
37 lines
1.5 KiB
Python
37 lines
1.5 KiB
Python
"""Operator-only fault injection in a LOCAL TEST stack; never exposed as HTTP."""
|
|
import argparse
|
|
import json
|
|
import os
|
|
import uuid
|
|
import psycopg
|
|
from psycopg.rows import dict_row
|
|
from .attachments import BUCKET, s3_client
|
|
|
|
|
|
def main():
|
|
p = argparse.ArgumentParser()
|
|
p.add_argument("action", choices=["delete-primary", "delete-backup", "create-orphan"])
|
|
p.add_argument("--id", help="Attachment UUID for the delete actions")
|
|
args = p.parse_args()
|
|
if os.environ.get("LAB4_ALLOW_FAULT_INJECTION") != "yes":
|
|
raise SystemExit("Fault injection disabled; use LAB4_ALLOW_FAULT_INJECTION=yes in isolated test")
|
|
client = s3_client()
|
|
if args.action == "create-orphan":
|
|
key = f"attachments/{uuid.uuid4()}/original"
|
|
client.put_object(Bucket=BUCKET, Key=key, Body=b"synthetic orphan")
|
|
print(json.dumps({"fault":"orphan-created", "object_key":key}))
|
|
return
|
|
if not args.id:
|
|
raise SystemExit("--id is required")
|
|
ident = str(uuid.UUID(args.id))
|
|
with psycopg.connect(os.environ["DATABASE_URL"], row_factory=dict_row) as conn:
|
|
row = conn.execute("SELECT object_key,backup_key FROM attachments WHERE id=%s", (ident,)).fetchone()
|
|
if not row:
|
|
raise SystemExit("Attachment does not exist")
|
|
key = row["object_key"] if args.action == "delete-primary" else row["backup_key"]
|
|
client.delete_object(Bucket=BUCKET, Key=key)
|
|
print(json.dumps({"fault":args.action, "object_key":key}))
|
|
|
|
if __name__ == "__main__":
|
|
main()
|