"""Run inside the actual worker: this script obtains only its own service secrets.""" import json import os from pathlib import Path import sys sys.path.insert(0,'/app/src') import vault from postgres import setting from pipeline import raw_connect,broker from objects import Objects from botocore.exceptions import ClientError report={'uid':os.getuid(),'own_kv':bool(vault.value('DATABASE_URL')),'root_or_unseal_files_present':any(Path(p).exists() for p in ['/operator','/run/secrets/postgres_password','/tls/ca.key'])} assert report['uid']!=0 and not report['root_or_unseal_files_present'] with raw_connect() as conn: report['job_rows_visible']=conn.execute('SELECT count(*) AS n FROM attachment_jobs').fetchone()['n'] denied=[] for table in ['users','sessions','messages']: try:conn.execute('SELECT count(*) FROM '+table) except Exception as error: assert type(error).__name__=='InsufficientPrivilege';denied.append(table) else:raise AssertionError('Worker read private business table') for sql in ['UPDATE attachments SET chat_id=chat_id','UPDATE attachment_jobs SET source_key=source_key','CREATE TABLE escaped (id INTEGER)','CREATE TEMP TABLE escaped_temp (id INTEGER)']: try:conn.execute(sql) except Exception as error:assert type(error).__name__=='InsufficientPrivilege' else:raise AssertionError('Worker modified protected schema or ownership/source columns') conn.execute('UPDATE attachment_jobs SET state=state WHERE 1=1 AND id=(SELECT id FROM attachment_jobs ORDER BY created_at LIMIT 1)') report['allowed_db_operations']=['SELECT job and attachment metadata','UPDATE allowed job state columns','pipeline INSERT job_events (covered by successful jobs)'] report['denied_db_tables']=denied report['protected_chat_source_columns_and_ddl_denied']=True client=broker() try:client.config_get('*') except Exception as error:assert type(error).__name__=='NoPermissionError';report['broker_config_denied']=True else:raise AssertionError('Worker can administer broker') print(json.dumps(report,indent=2))