#!/usr/bin/env python3 """What the worker credential can and cannot do. Prints statuses, not secrets.""" from __future__ import annotations import asyncio import uuid import asyncpg from botocore.exceptions import ClientError from app import bao, broker, config, db, s3, tlsutil def openbao_status(path: str) -> str: try: login = bao._request( "POST", "/v1/auth/approle/login", {"role_id": config.BAO_ROLE_ID, "secret_id": config.BAO_SECRET_ID}, ) token = (login.get("auth") or {}).get("client_token") or "" bao._request("GET", path, token=token) return "200" except RuntimeError as exc: text = str(exc) if "HTTP " in text: return text.split("HTTP ", 1)[1] return "error" async def main() -> None: bao.apply() pool = await db.connect() try: await pool.fetchval("SELECT count(*) FROM attachments") print("PASS worker db read attachments") try: await pool.fetchval("SELECT count(*) FROM users") except asyncpg.InsufficientPrivilegeError: print("PASS worker db cannot read users") else: raise SystemExit("FAIL worker read the users table") finally: await db.close() client = s3._client_or_create() key = "variants/track1-%s" % uuid.uuid4().hex client.put_object(Bucket=config.S3_BUCKET, Key=key, Body=b"ok") client.delete_object(Bucket=config.S3_BUCKET, Key=key) print("PASS worker s3 write variants") try: client.put_object(Bucket=config.S3_BUCKET, Key="secrets/track1", Body=b"no") except ClientError as exc: code = exc.response.get("Error", {}).get("Code", "") print("PASS worker s3 cannot write secrets", code) else: raise SystemExit("FAIL worker wrote secrets/") connection = await __import__("aio_pika").connect_robust( config.BROKER_URL, ssl_context=tlsutil.server_context(), ) channel = await connection.channel() try: await channel.declare_queue("stolen.queue", durable=False, auto_delete=True) except Exception as exc: print("PASS worker broker cannot declare stolen.queue", type(exc).__name__) else: raise SystemExit("FAIL worker declared stolen.queue") await connection.close() connection = await __import__("aio_pika").connect_robust( config.BROKER_URL, ssl_context=tlsutil.server_context(), ) channel = await connection.channel() await channel.declare_queue(broker.QUEUE, durable=True, passive=True) await connection.close() print("PASS worker broker can see attachment.jobs") own = openbao_status("/v1/secret/data/messenger/worker") foreign = openbao_status("/v1/secret/data/messenger/api") print("PASS worker openbao own path", own) print("PASS worker openbao api path", foreign) if own != "200" or foreign != "403": raise SystemExit("FAIL worker OpenBao policy own=%s api=%s" % (own, foreign)) if __name__ == "__main__": asyncio.run(main())