"""Operator tools. Not part of the public HTTP API.""" import argparse import asyncio import hashlib import json import sys import urllib.error import urllib.request import uuid from app.config import load_settings from app.db import close, connect, migrate, pool from app.reconcile import reconcile from app import s3client async def _prepare(settings): await connect(settings) await migrate(settings) async def _inject(settings, chat_id: str, owner_id: str) -> dict: if settings.app_env != "lab": raise SystemExit("inject is allowed only when APP_ENV=lab") chat_uuid = uuid.UUID(chat_id) owner_uuid = uuid.UUID(owner_id) member = await pool().fetchval( "SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2", chat_uuid, owner_uuid, ) if member is None: raise SystemExit("owner is not a member of the chat") def staged(note: str) -> tuple[str, str, bytes]: attachment_id = str(uuid.uuid4()) key = f"o/{attachment_id}" payload = f"fault-{note}-{attachment_id}".encode("utf-8") return attachment_id, key, payload fresh_id, fresh_key, fresh_body = staged("fresh-orphan") old_id, old_key, old_body = staged("old-orphan") stale_id, stale_key, stale_body = staged("stale-missing") used_id, used_key, used_body = staged("in-use-missing") for key, body in ( (fresh_key, fresh_body), (old_key, old_body), (stale_key, stale_body), (used_key, used_body), ): s3client.put_bytes(settings, key, body) async with pool().acquire() as conn: async with conn.transaction(): for attachment_id, key, body, note in ( (fresh_id, fresh_key, fresh_body, "fresh-orphan"), (old_id, old_key, old_body, "old-orphan"), ): await conn.execute( """ INSERT INTO object_ledger ( object_key, attachment_id, chat_id, state, note, size_bytes, created_at, updated_at ) VALUES ($1, $2, $3, 'staging', $4, $5, clock_timestamp(), clock_timestamp()) """, key, uuid.UUID(attachment_id), chat_uuid, note, len(body), ) await conn.execute( """ UPDATE object_ledger SET created_at = clock_timestamp() - interval '2 hours', updated_at = clock_timestamp() - interval '2 hours' WHERE object_key = $1 """, old_key, ) for attachment_id, key, body, filename in ( (stale_id, stale_key, stale_body, "fault-stale-missing"), (used_id, used_key, used_body, "fault-in-use-missing"), ): digest = hashlib.sha256(body).hexdigest() await conn.execute( """ INSERT INTO attachments ( id, chat_id, owner_id, filename, content_type, size_bytes, sha256, status, object_key, error_code, created_at, updated_at ) VALUES ( $1, $2, $3, $4, 'image/png', $5, $6, 'ready', $7, NULL, clock_timestamp() - interval '2 hours', clock_timestamp() - interval '2 hours' ) """, uuid.UUID(attachment_id), chat_uuid, owner_uuid, filename, len(body), digest, key, ) await conn.execute( """ INSERT INTO object_ledger ( object_key, attachment_id, chat_id, state, note, size_bytes, created_at, updated_at ) VALUES ( $1, $2, $3, 'committed', $4, $5, clock_timestamp() - interval '2 hours', clock_timestamp() - interval '2 hours' ) """, key, uuid.UUID(attachment_id), chat_uuid, filename, len(body), ) message_id = uuid.uuid4() await conn.execute( """ INSERT INTO messages (id, chat_id, sender_id, text, created_at) VALUES ($1, $2, $3, $4, clock_timestamp() - interval '2 hours') """, message_id, chat_uuid, owner_uuid, "fault-in-use", ) await conn.execute( """ INSERT INTO message_attachments (message_id, attachment_id, position) VALUES ($1, $2, 0) """, message_id, uuid.UUID(used_id), ) s3client.delete_object(settings, stale_key) s3client.delete_object(settings, used_key) return { "fresh_orphan": {"attachment_id": fresh_id, "object_key": fresh_key, "note": "fresh-orphan"}, "old_orphan": {"attachment_id": old_id, "object_key": old_key, "note": "old-orphan"}, "stale_missing": {"attachment_id": stale_id, "object_key": stale_key, "note": "fault-stale-missing"}, "in_use_missing": {"attachment_id": used_id, "object_key": used_key, "note": "fault-in-use-missing"}, } async def _probe(settings, attachment_id: str | None, object_key: str | None) -> dict: result = {} if attachment_id: row = await pool().fetchval("SELECT 1 FROM attachments WHERE id = $1::uuid", uuid.UUID(attachment_id)) result["metadata"] = row is not None if object_key: result["object"] = s3client.object_exists(settings, object_key) return result def _anonymous_status(url: str) -> int: request = urllib.request.Request(url, method="GET") try: with urllib.request.urlopen(request, timeout=5) as response: return response.status except urllib.error.HTTPError as exc: return exc.code async def _check_private(settings) -> dict: endpoint = settings.s3_endpoint.rstrip("/") object_status = _anonymous_status(f"{endpoint}/{settings.s3_bucket}/o/private-probe-missing") list_status = _anonymous_status(f"{endpoint}/{settings.s3_bucket}?list-type=2") return { "anonymous_object_status": object_status, "anonymous_list_status": list_status, "private": object_status == 403 and list_status == 403, } async def _run(args) -> dict: settings = load_settings() await _prepare(settings) try: if args.cmd == "reconcile": if args.apply and args.dry_run: raise SystemExit("choose either --apply or --dry-run") return await reconcile(settings, apply=bool(args.apply)) if args.cmd == "inject": return await _inject(settings, args.chat_id, args.owner_id) if args.cmd == "probe": return await _probe(settings, args.attachment_id, args.object_key) if args.cmd == "check-private": return await _check_private(settings) raise SystemExit("unknown command") finally: await close() def main() -> None: parser = argparse.ArgumentParser(description="Lab operator tools") sub = parser.add_subparsers(dest="cmd", required=True) reconcile_parser = sub.add_parser("reconcile") reconcile_parser.add_argument("--apply", action="store_true") reconcile_parser.add_argument("--dry-run", action="store_true") inject_parser = sub.add_parser("inject") inject_parser.add_argument("--chat-id", required=True) inject_parser.add_argument("--owner-id", required=True) probe_parser = sub.add_parser("probe") probe_parser.add_argument("--attachment-id") probe_parser.add_argument("--object-key") sub.add_parser("check-private") args = parser.parse_args() if args.cmd == "reconcile" and not args.apply and not args.dry_run: args.dry_run = True report = asyncio.run(_run(args)) json.dump(report, sys.stdout, ensure_ascii=False) sys.stdout.write("\n") if __name__ == "__main__": main()