forked from mtusi2026/mtusi2026lab7
75 lines
3.0 KiB
Python
75 lines
3.0 KiB
Python
"""Follow a synthetic request via Grafana and check logs/traces for sensitive values."""
|
|
import base64
|
|
import json
|
|
from pathlib import Path
|
|
import ssl
|
|
import sys
|
|
import time
|
|
from urllib.parse import urlencode
|
|
from urllib.request import Request, urlopen
|
|
from uuid import uuid4
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path[:0] = [str(ROOT), str(ROOT / "tests")]
|
|
from client import Client
|
|
from scripts.operator import settings
|
|
from scripts.pki import SECRETS
|
|
|
|
CONFIG = settings()
|
|
GRAFANA = "https://127.0.0.1:" + CONFIG.get("GRAFANA_PORT", "3007")
|
|
TLS = ssl.create_default_context(cafile=str(SECRETS / "trust" / "grafana.crt"))
|
|
|
|
|
|
def query(source, path, **parameters):
|
|
password = (SECRETS / "runtime/grafana-config/grafana_password").read_text().strip()
|
|
credentials = base64.b64encode(("admin:" + password).encode()).decode()
|
|
url = f"{GRAFANA}/api/datasources/proxy/uid/{source}{path}"
|
|
if parameters:
|
|
url += "?" + urlencode(parameters)
|
|
request = Request(url, headers={"Authorization": "Basic " + credentials})
|
|
with urlopen(request, context=TLS, timeout=5) as response:
|
|
return json.load(response)
|
|
|
|
|
|
def poll(action, nonempty, seconds=30):
|
|
end = time.monotonic() + seconds
|
|
while time.monotonic() < end:
|
|
value = action()
|
|
if nonempty(value):
|
|
return value
|
|
time.sleep(1)
|
|
raise AssertionError("telemetry did not arrive before deadline")
|
|
|
|
|
|
def main():
|
|
client = Client("https://localhost:" + CONFIG.get("HTTPS_PORT", "8443"), str(SECRETS / "trust/edge.crt"))
|
|
actor = client.user("audit")
|
|
probe = "sensitive-canary-" + uuid4().hex
|
|
start = time.time_ns() - 3_000_000_000
|
|
response = client.call("GET", "/api/v1/users/me?probe=" + probe, 400, actor=actor,
|
|
headers={"X-User-Id": probe})
|
|
request_id = response["headers"]["X-Request-Id"]
|
|
log = poll(
|
|
lambda: query("loki", "/loki/api/v1/query_range",
|
|
query='{service_name="messenger-api"} | request_id="' + request_id + '"',
|
|
start=str(start), limit=50),
|
|
lambda value: value.get("data", {}).get("result"),
|
|
)
|
|
traces = poll(
|
|
lambda: query("tempo", "/api/search", q='{span.request_id = "' + request_id + '"}', limit=3),
|
|
lambda value: value.get("traces"),
|
|
)
|
|
trace_id = traces["traces"][0]["traceID"]
|
|
trace = query("tempo", "/api/traces/" + trace_id)
|
|
metrics = poll(lambda: query("prometheus", "/api/v1/query", query='up{job="messenger"}'),
|
|
lambda value: value.get("data", {}).get("result"))
|
|
assert any(series["value"][1] == "1" for series in metrics["data"]["result"])
|
|
payload = json.dumps([log, trace], ensure_ascii=False)
|
|
for value in (actor["token"], actor["registration"]["password"], actor["registration"]["username"], probe):
|
|
assert value not in payload, "a synthetic sensitive value appeared in telemetry"
|
|
print("PASS Grafana queries over mTLS: request in Loki and Tempo, Prometheus scrape up; secret canaries absent")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|