"""Verify normal API permissions of the assigned worker service identity.""" import argparse import json from uuid import UUID import psycopg from botocore.exceptions import ClientError from redis.exceptions import NoPermissionError from attachments import Objects from bao_config import configure from db import connection_string from pipeline import STREAM, connect_broker def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("attachment_id", type=UUID) args = parser.parse_args() configure("worker") checks = {} with psycopg.connect(connection_string(), autocommit=True) as db: row = db.execute("SELECT pipeline_version FROM image_jobs WHERE attachment_id=%s", (args.attachment_id,)).fetchone() if not row: raise AssertionError("Expected a synthetic attachment produced by this stack") checks["can_read_job"] = True try: db.execute("SELECT password_hash FROM users LIMIT 1") except psycopg.errors.InsufficientPrivilege: checks["cannot_read_passwords"] = True else: raise AssertionError("Worker can read password hashes") try: db.execute("DELETE FROM messages WHERE false") except psycopg.errors.InsufficientPrivilege: checks["cannot_delete_messages"] = True else: raise AssertionError("Worker can delete messages") objects = Objects() if not objects.get(objects.key(args.attachment_id, "staging")): raise AssertionError("Worker could not read its staging object") checks["can_read_staging"] = True try: objects.get(objects.result_key(args.attachment_id, row[0], "original")) except ClientError as exc: if exc.response["ResponseMetadata"]["HTTPStatusCode"] != 403: raise checks["cannot_read_published_original"] = True else: raise AssertionError("Worker has unwanted access to published files") broker = connect_broker() try: assert broker.ping() try: broker.xadd(STREAM, {"job": "{}"}) except NoPermissionError: checks["cannot_publish_to_stream"] = True else: raise AssertionError("Worker has producer permissions") finally: broker.close() print(json.dumps(checks, indent=2)) if __name__ == "__main__": main()