#!/usr/bin/env python3 """Checks that passwords, tokens, message text and filenames do not reach Loki/Tempo and that metric labels hold no IDs. Run inside the tools container: docker compose run --rm tools python3 scripts/telemetry-privacy.py """ import base64 import json import os import re import sys import time import urllib.parse import urllib.request import uuid BASE = os.environ.get('BASE_URL', 'http://lb:8080') GRAFANA = os.environ.get('GRAFANA_URL', 'http://grafana:3000') SAMPLE = open(os.path.join(os.path.dirname(__file__), '..', 'tests', 'fixtures', 'sample.png'), 'rb').read() UUID = re.compile(r'[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}') def call(method, path, body=None, headers=None, raw=None): data = raw if raw is not None else (json.dumps(body).encode() if body is not None else None) headers = dict(headers or {}) if body is not None: headers['Content-Type'] = 'application/json' with urllib.request.urlopen(urllib.request.Request(BASE + path, data=data, method=method, headers=headers), timeout=15) as r: return json.loads(r.read() or b'null') def grafana(path, **params): with urllib.request.urlopen(GRAFANA + path + '?' + urllib.parse.urlencode(params), timeout=30) as r: return r.read().decode() def main(): marker = uuid.uuid4().hex[:12] secrets = { 'password': 'Privacy-password-' + marker, 'message text': 'privacy message body ' + marker, 'filename': 'privacy-file-%s.png' % marker, } trace_ids = [] def traced(): trace_id = os.urandom(16).hex() trace_ids.append(trace_id) return {'traceparent': '00-%s-%s-01' % (trace_id, os.urandom(8).hex())} username = 'privacy_' + marker user = call('POST', '/api/v1/users', {'username': username, 'display_name': 'Privacy', 'password': secrets['password']}, traced()) basic = base64.b64encode(('%s:%s' % (username, secrets['password'])).encode()).decode() token = call('POST', '/api/v1/auth/sessions', headers={'Authorization': 'Basic ' + basic, **traced()})['token'] secrets['session token'] = token secrets['basic credentials'] = basic auth = {'Authorization': 'Bearer ' + token} chat = call('POST', '/api/v1/chats', {'title': 'Privacy', 'member_ids': [user['id']]}, {**auth, **traced()})['id'] call('POST', '/api/v1/chats/%s/messages' % chat, {'text': secrets['message text']}, {**auth, 'Idempotency-Key': marker, **traced()}) call('POST', '/api/v1/chats/%s/attachments?filename=%s' % (chat, secrets['filename']), headers={**auth, 'Content-Type': 'image/png', **traced()}, raw=SAMPLE) call('GET', '/api/v1/chats/%s/messages?limit=5' % chat, headers={**auth, **traced()}) print('sent %d traced requests with known password, token, message text and filename; waiting 25 s for export' % len(trace_ids)) time.sleep(25) traces = '' found_traces = 0 for trace_id in trace_ids: try: traces += grafana('/api/datasources/proxy/uid/tempo/api/traces/' + trace_id) found_traces += 1 except OSError: pass def loki_lines(query): body = grafana('/api/datasources/proxy/uid/loki/loki/api/v1/query_range', query=query, limit=100, since='5m') return sum(len(s['values']) for s in json.loads(body)['data']['result']) own = loki_lines('{service_name=~"messenger-.+"} |~ "%s"' % '|'.join(trace_ids)) print('Tempo: %d/%d traces fetched (%d bytes); Loki: %d log lines carry these trace_ids (control)' % ( found_traces, len(trace_ids), len(traces), own)) leaks = 0 for name, value in list(secrets.items()) + [('Authorization hdr', 'authorization')]: in_traces = value.lower() in traces.lower() in_logs = loki_lines('{service_name=~"messenger-.+"} |~ "(?i)%s"' % re.escape(value).replace('\\', '\\\\')) leaks += in_traces + in_logs print(' %-18s in traces: %-5s log lines in Loki (5 min): %d' % (name, in_traces, in_logs)) series = json.loads(grafana('/api/datasources/proxy/uid/prometheus/api/v1/series', **{'match[]': '{job=~"messenger-.+"}'}))['data'] names = {s['__name__'] for s in series} with_ids = [s for s in series if any(UUID.search(v) for k, v in s.items() if k != 'instance')] labels = sorted({k for s in series for k in s} - {'__name__'}) print('Prometheus: %d series, %d metric names from messenger services; label keys: %s' % (len(series), len(names), ', '.join(labels))) print(' series with a UUID in any label value: %d' % len(with_ids)) routes = sorted({s.get('http_route') for s in series if s.get('http_route')}) print(' http_route values: %s' % ', '.join(routes)) ok = found_traces == len(trace_ids) and own > 0 and leaks == 0 and not with_ids print('%s: no secrets/content in logs and traces, no IDs in metric labels' % ('PASS' if ok else 'FAIL')) sys.exit(0 if ok else 1) if __name__ == '__main__': main()