#!/usr/bin/env python3 """exe-auth: lets anyone who can reach this VM use LiteLLM as themselves. It sits between exe.dev's HTTPS proxy and LiteLLM. exe.dev only lets through people the VM is shared with (or their VM tokens, or VMs attached by a peer integration), and tells us who they are in X-ExeDev-Email or X-Exedev-Source-Vm. For model requests that carry no LiteLLM key, exe-auth swaps in that person's own LiteLLM key, creating their LiteLLM user and key the first time, so LiteLLM's budgets, model lists, and logs apply per person. Requests with a LiteLLM key (sk-...), and everything else (the admin UI and API), pass through untouched. Standard library only. Settings come from the environment: LITELLM_MASTER_KEY LiteLLM's master key; also seeds the per-person keys LITELLM_URL where LiteLLM listens (http://litellm:4000) EXE_AUTH_PORT where exe-auth listens (8000) EXE_AUTH_BUDGET a new person's budget in USD (20) EXE_AUTH_BUDGET_PERIOD how often it resets (30d) """ import hashlib import hmac import http.client import json import os import sys import threading import urllib.parse from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer MASTER_KEY = os.environ["LITELLM_MASTER_KEY"] UPSTREAM = urllib.parse.urlsplit(os.environ.get("LITELLM_URL", "http://litellm:4000")) PORT = int(os.environ.get("EXE_AUTH_PORT", "8000")) BUDGET = float(os.environ.get("EXE_AUTH_BUDGET", "20")) BUDGET_PERIOD = os.environ.get("EXE_AUTH_BUDGET_PERIOD", "30d") # Model routes, with or without /v1, plus LiteLLM's provider-style prefixes. MODEL_ROUTES = ("/chat/completions", "/completions", "/embeddings", "/responses", "/messages", "/models", "/audio/", "/images/", "/moderations", "/rerank") CREDENTIAL_HEADERS = ("authorization", "x-api-key", "x-litellm-api-key", "api-key") HOP_BY_HOP = {"connection", "keep-alive", "proxy-connection", "te", "trailer", "transfer-encoding", "upgrade", "content-length", "host"} provisioned = set() lock = threading.Lock() def is_model_route(path): path = urllib.parse.urlsplit(path).path for prefix in ("/v1", "/anthropic/v1", "/anthropic"): if path.startswith(prefix + "/"): path = path[len(prefix):] break return path.startswith(MODEL_ROUTES) def presented_key(headers): for name in CREDENTIAL_HEADERS: value = headers.get(name, "") if value.lower().startswith("bearer "): value = value[7:] if value.strip().startswith("sk-"): return value.strip() return None def person_key(identity): digest = hmac.new(MASTER_KEY.encode(), identity.encode(), hashlib.sha256).hexdigest() return "sk-exe-" + digest[:40] def admin(method, path, body=None): conn = http.client.HTTPConnection(UPSTREAM.hostname, UPSTREAM.port or 80, timeout=30) data = json.dumps(body).encode() if body is not None else None conn.request(method, path, body=data, headers={ "Authorization": "Bearer " + MASTER_KEY, "Content-Type": "application/json"}) resp = conn.getresponse() text = resp.read().decode(errors="replace") conn.close() return resp.status, text def provision(identity, email, key): """Make sure identity has a LiteLLM user and its key, once per process.""" with lock: if identity in provisioned: return status, text = admin("GET", "/key/info?key=" + urllib.parse.quote(key)) exists = status == 200 and not (json.loads(text).get("info") or {}).get("deleted_at") if not exists: user = {"user_id": identity, "max_budget": BUDGET, "budget_duration": BUDGET_PERIOD, "auto_create_key": False} if email: user["user_email"] = email status, text = admin("POST", "/user/new", user) if status != 200 and "already exists" not in text: print(f"exe-auth: /user/new for {identity}: {status} {text[:200]}", file=sys.stderr) status, text = admin("POST", "/key/generate", { "key": key, "user_id": identity, "key_alias": "exe:" + identity}) if status != 200: raise RuntimeError(f"/key/generate for {identity}: {status} {text[:200]}") print(f"exe-auth: created a key for {identity}", file=sys.stderr) provisioned.add(identity) class Handler(BaseHTTPRequestHandler): protocol_version = "HTTP/1.1" def log_message(self, fmt, *args): pass def proxy(self): length = int(self.headers.get("Content-Length") or 0) body = self.rfile.read(length) if length else None headers = {k: v for k, v in self.headers.items() if k.lower() not in HOP_BY_HOP} headers["Host"] = self.headers.get("Host", "") email = self.headers.get("X-ExeDev-Email", "").strip() source_vm = self.headers.get("X-Exedev-Source-Vm", "").strip() identity = "vm:" + source_vm if source_vm else email injected = None if identity and is_model_route(self.path) and not presented_key(self.headers): injected = person_key(identity) headers = {k: v for k, v in headers.items() if k.lower() not in CREDENTIAL_HEADERS} headers["Authorization"] = "Bearer " + injected for attempt in range(2): if injected: try: provision(identity, "" if source_vm else email, injected) except Exception as e: # LiteLLM still starting, say print(f"exe-auth: {e}", file=sys.stderr) return self.reply(503, {"error": {"message": "exe-auth: could not set up your key; try again"}}) try: conn = http.client.HTTPConnection(UPSTREAM.hostname, UPSTREAM.port or 80, timeout=600) conn.request(self.command, self.path, body=body, headers=headers) resp = conn.getresponse() except OSError as e: return self.reply(502, {"error": {"message": f"exe-auth: LiteLLM is not answering: {e}"}}) if not (injected and resp.status == 401 and attempt == 0): break resp.read() # the key was deleted: make it again and retry conn.close() with lock: provisioned.discard(identity) self.send_response(resp.status, resp.reason) for k, v in resp.getheaders(): if k.lower() not in HOP_BY_HOP: self.send_header(k, v) if self.command == "HEAD" or resp.status in (204, 304): self.send_header("Content-Length", "0") self.end_headers() conn.close() return self.send_header("Transfer-Encoding", "chunked") self.end_headers() try: while chunk := resp.read1(65536): self.wfile.write(b"%x\r\n%s\r\n" % (len(chunk), chunk)) self.wfile.flush() self.wfile.write(b"0\r\n\r\n") except (BrokenPipeError, ConnectionResetError): self.close_connection = True finally: conn.close() def reply(self, status, obj): data = json.dumps(obj).encode() self.send_response(status) self.send_header("Content-Type", "application/json") self.send_header("Content-Length", str(len(data))) self.end_headers() self.wfile.write(data) do_GET = do_POST = do_PUT = do_PATCH = do_DELETE = do_HEAD = do_OPTIONS = proxy if __name__ == "__main__": print(f"exe-auth: listening on :{PORT}, LiteLLM at {UPSTREAM.geturl()}", file=sys.stderr) ThreadingHTTPServer(("0.0.0.0", PORT), Handler).serve_forever()