#!/usr/bin/env python3 """Inject safe synthetic S3/DB inconsistencies and verify reconciliation.""" import json import os import subprocess import sys import uuid import boto3 from botocore.config import Config from botocore.exceptions import ClientError from botocore import UNSIGNED from client import Client, check def reconcile(*options): result = subprocess.run( [sys.executable, "scripts/reconcile.py", *options], capture_output=True, text=True, check=False, ) check(result.returncode == 0, "reconciler command failed: " + result.stderr.strip()) return json.loads(result.stdout) def upload(client, alice, chat): return client.attachment(alice, chat)[0] def main(): base_url = os.environ.get("RECONCILE_TEST_BASE_URL", "http://load_balancer:8080") client = Client(base_url) alice, _, _, chat = client.bootstrap() linked = upload(client, alice, chat) client.message(alice, chat, "", attachments=[linked["id"]]) missing_referenced = upload(client, alice, chat) client.message(alice, chat, "", attachments=[missing_referenced["id"]]) missing_unreferenced = upload(client, alice, chat) with open(os.environ["S3_ACCESS_KEY_ID_FILE"], encoding="utf-8") as file: access_key = file.read().strip() with open(os.environ["S3_SECRET_ACCESS_KEY_FILE"], encoding="utf-8") as file: secret_key = file.read().strip() s3 = boto3.client( "s3", endpoint_url=os.environ.get("S3_ENDPOINT", "http://s3:9000"), region_name=os.environ.get("S3_REGION", "us-east-1"), aws_access_key_id=access_key, aws_secret_access_key=secret_key, config=Config(signature_version="s3v4", connect_timeout=2, read_timeout=5), ) bucket = os.environ.get("S3_BUCKET", "messenger-attachments") for attachment in (missing_referenced, missing_unreferenced): s3.delete_object(Bucket=bucket, Key="attachments/" + attachment["id"]) fresh_orphan = "attachments/" + str(uuid.uuid4()) with open("tests/fixtures/sample.png", "rb") as file: fixture = file.read() s3.put_object( Bucket=bucket, Key=fresh_orphan, Body=fixture, ContentType="image/png", ) denied = boto3.client( "s3", endpoint_url=os.environ.get("S3_ENDPOINT", "http://s3:9000"), region_name=os.environ.get("S3_REGION", "us-east-1"), config=Config(signature_version=UNSIGNED, connect_timeout=2, read_timeout=5), ) try: denied.get_object(Bucket=bucket, Key="attachments/" + linked["id"]) raise AssertionError("private S3 bucket allowed an anonymous object read") except ClientError as error: check( error.response["ResponseMetadata"]["HTTPStatusCode"] in {403, 404}, "anonymous S3 request did not fail as private access", ) dry_run = reconcile("--grace-hours", "24") check(dry_run["mode"] == "dry-run", "default reconciliation must be dry-run") check(dry_run["orphan_fresh_preserved"] >= 1, "fresh orphan was not protected") check( dry_run["missing_referenced_or_fresh_preserved"] >= 2, "referenced/fresh missing metadata was not protected", ) applied_grace = reconcile("--apply", "--grace-hours", "24") check(applied_grace["objects_deleted"] == 0, "grace run deleted fresh object") check(applied_grace["metadata_deleted"] == 0, "grace run deleted fresh metadata") s3.head_object(Bucket=bucket, Key=fresh_orphan) s3.head_object(Bucket=bucket, Key="attachments/" + linked["id"]) repaired = reconcile("--apply", "--grace-hours", "0") check(repaired["objects_deleted"] >= 1, "stale orphan object was not removed") check(repaired["metadata_deleted"] >= 1, "stale unreferenced missing row was not removed") check( repaired["missing_referenced_or_fresh_preserved"] >= 1, "missing referenced attachment was not protected", ) repeated = reconcile("--apply", "--grace-hours", "0") check(repeated["objects_deleted"] == 0, "repeated run was not idempotent for objects") check(repeated["metadata_deleted"] == 0, "repeated run was not idempotent for metadata") check( client.call("GET", "/api/v1/attachments/" + linked["id"], actor=alice)["status"] == 200, "reconciler removed a used attachment", ) downloaded = client.call( "GET", "/api/v1/attachments/" + linked["id"] + "/content", actor=alice ) check(downloaded["bytes"] == fixture, "reconciler damaged a used attachment") client.call( "GET", "/api/v1/attachments/" + missing_referenced["id"], 200, actor=alice, schema="Attachment", ) client.call( "GET", "/api/v1/attachments/" + missing_unreferenced["id"], 404, actor=alice, ) print( json.dumps( { "private_bucket_anonymous_read": "denied", "dry_run_elapsed_seconds": dry_run["elapsed_seconds"], "objects_scanned": dry_run["objects_scanned"], "metadata_scanned": dry_run["metadata_scanned"], "dry_run_orphan_candidates": dry_run["orphan_old_candidates"], "fresh_orphan_preserved": applied_grace["orphan_fresh_preserved"], "apply_deleted_objects": repaired["objects_deleted"], "apply_deleted_unreferenced_metadata": repaired["metadata_deleted"], "referenced_missing_metadata_preserved": True, "repeat_deleted_objects": repeated["objects_deleted"], "repeat_deleted_metadata": repeated["metadata_deleted"], "used_attachment_preserved": True, }, sort_keys=True, ) ) if __name__ == "__main__": main()