#!/usr/bin/env python3
# Security posture check: verify access control on backend routes.
# Only HTTP status code and response size are recorded. No response bodies / personal data.
import socket, urllib.request, urllib.error, ssl, time, os, json
OUT="/opt/data/workspace/i26007/sec_check.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:
    ips=sorted(set(x[4][0] for x in socket.getaddrinfo(HOST,PORT,proto=socket.IPPROTO_TCP)))
except Exception as e:
    ips=[]; log("RESOLVE_FAIL",e)
log("RESOLVE",HOST,"->",ips)
JWT=None
for p in ["/opt/data/workspace/i26007/JWT"]:
    if os.path.exists(p): JWT=open(p).read().strip()
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(64)
            return resp.status, resp.headers.get("Content-Length","?"), resp.headers.get("Content-Type","")[:28]
    except urllib.error.HTTPError as e:
        try: e.read(64)
        except: pass
        return e.code, e.headers.get("Content-Length","?"), e.headers.get("Content-Type","")[:28]
    except Exception as e:
        return "ERR", str(e)[:60], ""
paths=[
 "/","/health","/healthz","/health/readiness","/livez","/readyz","/metrics","/version",
 "/api","/api/health","/api/version","/api/status",
 "/internal","/internal/health","/internal/metrics","/internal/config","/internal/debug",
 "/api/internal","/api/internal/health","/api/internal/config","/api/internal/users","/api/internal/stats",
 "/api/admin","/api/admin/users","/api/admin/config","/api/admin/stats","/api/admin/spaces","/api/admin/tenants",
 "/api/users","/api/spaces","/api/agents","/api/agents/604","/api/mcp-tokens","/api/mcp/services",
 "/api/oauth","/api/oauth/providers","/api/secrets","/api/secrets/effective","/api/config","/api/settings",
 "/api/system","/api/system/info","/api/tenants","/api/accounts","/api/operator","/api/staff",
 "/.well-known/openid-configuration","/.well-known/jwks.json",
 "/swagger","/swagger.json","/openapi.json","/api-docs","/docs","/v3/api-docs",
 "/graphql","/debug","/actuator","/actuator/health","/actuator/env","/actuator/info",
 "/api/auth/config","/api/auth/me","/api/auth/providers","/api/v1","/robots.txt","/favicon.ico",
]
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
log("=== AUTH=none ===")
rows=[]
for p in paths:
    sc,cl,ct=req(base+p,H())
    rows.append((p,sc,cl,ct)); log("%-32s %-4s len=%-8s %s"%(p,sc,cl,ct))
if JWT:
    log("=== AUTH=low-priv-token (own session) ===")
    for p in ["/api/agents/604","/api/spaces","/api/users","/api/mcp/services","/api/auth/me","/api/admin","/api/admin/users","/api/internal/users","/api/secrets/effective"]:
        sc,cl,ct=req(base+p,H({"Authorization":"Bearer "+JWT}))
        log("%-32s %-4s len=%-8s %s"%(p,sc,cl,ct))
log("DONE")
