forked from mtusi2026/mtusi2026lab7
76 lines
3.0 KiB
Python
76 lines
3.0 KiB
Python
"""Verify that correlated traces and logs omit synthetic credentials and text."""
|
|
import base64
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
import time
|
|
from urllib.parse import urlencode
|
|
import uuid
|
|
|
|
from client import Client, arguments, check, run
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
FETCH = """
|
|
import json,ssl,sys,urllib.request,urllib.error
|
|
ctx=ssl.create_default_context(cafile='/ca/ca.crt')
|
|
ctx.load_cert_chain('/identity/tls.crt','/identity/tls.key')
|
|
for url in sys.argv[1:]:
|
|
try:
|
|
with urllib.request.urlopen(url,context=ctx,timeout=5) as reply:
|
|
print(json.dumps(json.load(reply)))
|
|
except urllib.error.HTTPError as error:
|
|
if error.code != 404:
|
|
raise
|
|
print('{}')
|
|
"""
|
|
|
|
|
|
def main():
|
|
args = arguments('Local log and trace privacy')
|
|
client = Client(args.base_url, args.ca_file)
|
|
alice, _, _, chat = client.bootstrap()
|
|
registration = alice['registration']
|
|
password = registration['password']
|
|
credential = base64.b64encode(f"{registration['username']}:{password}".encode()).decode()
|
|
login = client.call('POST', '/api/v1/auth/sessions', 201,
|
|
headers={'Authorization': 'Basic ' + credential}, schema='Session')
|
|
marker = 'telemetry-private-' + uuid.uuid4().hex
|
|
message = client.message(alice, chat, marker)
|
|
pki = ROOT / 'certs/private'
|
|
command = [
|
|
'docker', 'run', '--rm', '--network', 'messenger-lab7_default',
|
|
'-v', f'{pki / "ca.crt"}:/ca/ca.crt:ro',
|
|
'-v', f'{pki / "grafana-client"}:/identity:ro',
|
|
'python:3.11.9-slim', 'python', '-c', FETCH,
|
|
]
|
|
urls = []
|
|
for response in (login, message):
|
|
headers = response['headers']
|
|
query = urlencode({'query': '{service_name="messenger-api"} | request_id="'
|
|
+ headers['X-Request-Id'] + '"'})
|
|
urls.extend([
|
|
'https://loki:3101/loki/api/v1/query_range?' + query,
|
|
'https://tempo:3201/api/traces/' + headers['X-Trace-Id'],
|
|
])
|
|
deadline = time.monotonic() + 60
|
|
while True:
|
|
output = subprocess.check_output([*command, *urls], cwd=ROOT, text=True)
|
|
login_logs, login_trace, message_logs, message_trace = map(json.loads, output.splitlines())
|
|
if (login_logs.get('data', {}).get('result') and login_trace
|
|
and message_logs.get('data', {}).get('result') and message_trace):
|
|
break
|
|
check(time.monotonic() < deadline, 'Correlated login or message telemetry not found')
|
|
time.sleep(3)
|
|
for sensitive in (password, credential, login['json']['token'], alice['token'], marker):
|
|
check(sensitive not in output, 'Correlated telemetry contained a synthetic secret')
|
|
(ROOT / 'evidence/telemetry-privacy.json').write_text(json.dumps({
|
|
'correlated_log_and_trace_found': ['login', 'message'],
|
|
'synthetic_secrets_found': 0,
|
|
}, indent=2) + '\n', encoding='utf-8')
|
|
print('PASS: correlated login and message telemetry omit synthetic secrets')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
run(main)
|