#!/usr/bin/env python3
# Security posture check #3: map documented/undocumented API routes reachable directly on the backend and
# classify them by access level (unauth vs requires session). Records ONLY status code + size. No bodies.
import socket, urllib.request, urllib.error, ssl, os
OUT="/opt/data/workspace/i26007/sec_check3.out"
CTX=ssl._create_unverified_context()
def log(*a):
    s=" ".join(str(x) for x in a); print(s)
    with open(OUT,"a") as f: f.write(s+"\n")
open(OUT,"w").close()
HOST="console-backend.agentek.svc.cluster.local"; PORT=3000
try: socket.getaddrinfo(HOST,PORT,proto=socket.IPPROTO_TCP)
except Exception as e: log("RESOLVE_FAIL",e)
JWT=open("/opt/data/workspace/i26007/JWT").read().strip() if os.path.exists("/opt/data/workspace/i26007/JWT") else None
def req(url,headers,timeout=7):
    r=urllib.request.Request(url,headers=headers,method="GET")
    try:
        with urllib.request.urlopen(r,timeout=timeout,context=CTX) as resp:
            resp.read(32); return resp.status, str(resp.headers.get("Content-Length","?")), resp.headers.get("Content-Type","")[:22]
    except urllib.error.HTTPError as e:
        try: e.read(32)
        except: pass
        return e.code, str(e.headers.get("Content-Length","?")), e.headers.get("Content-Type","")[:22]
    except Exception as e:
        return "ERR", str(e)[:40], ""
base="http://%s:%d"%(HOST,PORT)
def H(extra=None):
    h={"User-Agent":"curl/8.5.0","Accept":"*/*","Connection":"close"}
    if extra: h.update(extra)
    return h
paths=[
 "/api/public","/api/public/","/api/public/health","/api/public/config","/api/public/status","/api/public/models",
 "/api/stream","/api/models-dev","/api/drag-source-monitor","/api/drop-target-monitor",
 "/api/knowledge-bases","/api/knowledge-bases/1","/api/knowledge-bases/1/sources","/api/files","/api/chats","/api/search",
 "/api/agents","/api/spaces","/api/models","/api/providers","/api/integrations","/api/tools","/api/mcp","/api/servers",
 "/api/services","/api/webhooks","/api/invites","/api/org","/api/team","/api/members","/api/roles","/api/permissions",
 "/api/settings","/api/feature-flags","/api/flags","/api/notifications","/api/events","/api/activity","/api/audit",
 "/api/logs","/api/traces","/api/tasks","/api/runs","/api/sessions","/api/tokens","/api/keys","/api/secrets",
 "/api/oauth","/api/auth","/api/user","/api/me","/api/profile","/api/account","/api/workspace","/api/projects",
 "/api/documents","/api/datasets","/api/embeddings","/api/vector","/api/llm","/api/chat","/api/completions",
 "/api/billing","/api/usage","/api/otel/v1/traces","/api/otel/v1/metrics","/api/ping","/api/version","/api/config",
 "/api/status","/api/health","/api/healthz","/api/auth/oauth/dex/start","/api/auth/refresh",
]
log("=== ROUTE MAP (no auth) — only non-404 are noteworthy ===")
for p in paths:
    sc,cl,ct=req(base+p,H())
    mark=" <<<" if sc not in (404,) else ""
    log("%-38s %-4s len=%-7s %s%s"%(p,sc,cl,ct,mark))
log("=== DRILL: same routes WITH our low-priv session (status only) ===")
if JWT:
    for p in paths:
        sc,cl,ct=req(base+p,H({"Authorization":"Bearer "+JWT}))
        if sc not in (404,401,403):
            log("%-38s jwt=%-4s len=%-7s %s"%(p,sc,cl,ct))
log("DONE")
