#!/usr/bin/env python3 """End-to-end session expiry check for the local Compose stand.""" import pathlib import subprocess from client import Client, LAB, arguments, check, run def read_env(path): values = {} env_path = pathlib.Path(path) if not env_path.is_file(): return values for raw in env_path.read_text(encoding='utf-8-sig').splitlines(): line = raw.strip() if not line or line.startswith('#') or '=' not in line: continue name, value = line.split('=', 1) values[name.strip()] = value.strip() return values def configure(parser): parser.add_argument('--env-file', default='.env') def main(): args = arguments('Local Compose end-to-end session expiry check', configure) check(LAB >= 2, 'session expiry requires lab2 or later') client = Client(args.base_url, args.ca_file) client.call('GET', '/health/ready', schema='Health') actor = client.user('expiry') client.call('GET', '/api/v1/users/me', actor=actor, schema='User') env = read_env(args.env_file) database_user = env.get('DATABASE_USER', 'messenger') database_name = env.get('DATABASE_NAME', 'messenger') username = actor['registration']['username'] sql = """ UPDATE sessions SET expires_at = created_at + INTERVAL '1 microsecond' WHERE user_id = (SELECT id FROM users WHERE username = :'target_username') AND revoked_at IS NULL AND expires_at > CURRENT_TIMESTAMP RETURNING 1; """ result = subprocess.run([ 'docker', 'compose', '--env-file', args.env_file, 'exec', '-T', 'postgres', 'psql', '--username', database_user, '--dbname', database_name, '--set', 'ON_ERROR_STOP=1', '--set', 'target_username=' + username, '--tuples-only', '--no-align', ], cwd=pathlib.Path(__file__).resolve().parents[1], input=sql, capture_output=True, text=True, timeout=20) check(result.returncode == 0, 'operator expiry update failed') check('1' in result.stdout.splitlines(), 'operator expiry update did not select exactly one synthetic session') client.call('GET', '/api/v1/users/me', 401, actor=actor) client.call('DELETE', '/api/v1/auth/sessions/current', 401, actor=actor) print('PASS: an expired server-side session is rejected over verified HTTPS.') if __name__ == '__main__': run(main)