"""Trusted operator proves passwords are enforced behind the TLS proxy for every DB role.""" import json,os import psycopg from psycopg.conninfo import conninfo_to_dict,make_conninfo base=conninfo_to_dict(os.environ['DATABASE_URL']);credentials=json.loads(os.environ['ROLE_CREDENTIALS']);rows=[] for role in ['api','worker','publisher','observer','janitor']: info={**base,'user':'messenger_'+role,'password':credentials[role]['db_password']} with psycopg.connect(make_conninfo(**info),connect_timeout=3) as c:assert c.execute('SELECT current_user').fetchone()[0]=='messenger_'+role try:psycopg.connect(make_conninfo(**{**info,'password':'synthetic_wrong_password'}),connect_timeout=3);raise AssertionError('wrong DB password accepted') except psycopg.OperationalError as e:assert 'password authentication failed' in str(e) rows.append({'role':role,'correct_password':True,'wrong_password_denied':True}) try:psycopg.connect(make_conninfo(**{**base,'password':credentials['worker']['db_password']}),connect_timeout=3);raise AssertionError('worker escalated to admin') except psycopg.OperationalError as e:assert 'password authentication failed' in str(e) print(json.dumps({'status':'PASS','rows':rows,'worker_password_cannot_authenticate_admin':True,'tcp_auth':'scram-sha-256'},indent=2))