|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Verify opt-in request tracing and retrieval isolation using synthetic users.""" |
| 3 | +import argparse |
| 4 | +import json |
| 5 | +import urllib.error |
| 6 | +import urllib.request |
| 7 | +from integration import server, ROOT |
| 8 | + |
| 9 | + |
| 10 | +def main(): |
| 11 | + parser = argparse.ArgumentParser() |
| 12 | + parser.add_argument('--binary', required=True) |
| 13 | + args = parser.parse_args() |
| 14 | + with server(args.binary) as request: |
| 15 | + admin = request('POST', '/api/collections/_superusers/auth-with-password', |
| 16 | + {'identity': 'admin@example.com', 'password': 'SyntheticAdminPassword123!'})['token'] |
| 17 | + users = [] |
| 18 | + for index in range(2): |
| 19 | + email = f'trace{index}@example.com' |
| 20 | + user = request('POST', '/api/collections/users/records', { |
| 21 | + 'email': email, 'name': f'Trace user {index}', |
| 22 | + 'password': 'SyntheticUserPassword123!', 'passwordConfirm': 'SyntheticUserPassword123!'}, admin) |
| 23 | + token = request('POST', '/api/collections/users/auth-with-password', |
| 24 | + {'identity': email, 'password': 'SyntheticUserPassword123!'})['token'] |
| 25 | + users.append((user, token)) |
| 26 | + user, token = users[0] |
| 27 | + |
| 28 | + def call(method, path, body=None, auth=token, extra=None): |
| 29 | + headers = {'Content-Type': 'application/json'} |
| 30 | + if auth: |
| 31 | + headers['Authorization'] = auth |
| 32 | + headers.update(extra or {}) |
| 33 | + req = urllib.request.Request(request.base_url + path, method=method, headers=headers, |
| 34 | + data=None if body is None else json.dumps(body).encode()) |
| 35 | + try: |
| 36 | + response = urllib.request.urlopen(req, timeout=20) |
| 37 | + except urllib.error.HTTPError as error: |
| 38 | + response = error |
| 39 | + with response: |
| 40 | + raw = response.read() |
| 41 | + return response.code, response.headers, json.loads(raw) if raw else None |
| 42 | + |
| 43 | + sql = 'SELECT id FROM user_directory LIMIT 5' |
| 44 | + status, headers, baseline = call('POST', '/api/context/query', {'sql': sql}) |
| 45 | + assert status == 200 and 'X-Context-Request-Id' not in headers |
| 46 | + for capture in (False, True): |
| 47 | + extra = {'X-Context-Trace': '1', 'X-Context-Correlation-Id': 'synthetic-operation'} |
| 48 | + if capture: |
| 49 | + extra['X-Context-Capture-Sql'] = '1' |
| 50 | + status, headers, body = call('POST', '/api/context/query', {'sql': sql}, extra=extra) |
| 51 | + assert status == 200 and body == baseline |
| 52 | + path = '/api/context/traces/' + headers['X-Context-Request-Id'] |
| 53 | + status, retrieval_headers, trace = call('GET', path, extra=extra) |
| 54 | + assert status == 200 and retrieval_headers['Cache-Control'] == 'no-store' |
| 55 | + assert 'X-Context-Request-Id' not in retrieval_headers |
| 56 | + assert trace['service'] == ROOT.name and trace['user_id'] == user['id'] |
| 57 | + assert trace['correlation_id'] == 'synthetic-operation' and len(trace['spans']) >= 4 |
| 58 | + assert trace.get('sql', '') == (sql if capture else '') |
| 59 | + assert call('GET', path, auth=users[1][1])[0] == 404 |
| 60 | + assert call('GET', path, auth='')[0] in (401, 403) |
| 61 | + assert call('GET', path, auth=admin)[0] in (401, 403) |
| 62 | + assert token not in json.dumps(trace) |
| 63 | + # REST traces exclude response values and query strings. |
| 64 | + status, headers, body = call('GET', '/api/collections/user_directory/records?perPage=1', extra={'X-Context-Trace': '1'}) |
| 65 | + assert status == 200 |
| 66 | + trace = call('GET', '/api/context/traces/' + headers['X-Context-Request-Id'])[2] |
| 67 | + assert '?' not in trace['route'] and 'Trace user' not in json.dumps(trace) |
| 68 | + # Application revocation also revokes trace retrieval. |
| 69 | + path = '/api/context/traces/' + headers['X-Context-Request-Id'] |
| 70 | + request('PATCH', '/api/collections/users/records/' + user['id'], {'disabled': True}, admin) |
| 71 | + assert call('GET', path)[0] == 401 |
| 72 | + print('PASS opt-out, SQL opt-in, source ownership, REST redaction and revocation') |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == '__main__': |
| 76 | + main() |
0 commit comments