install-newt: optional internet access via a Pangolin tunnel, with OIDC sign-in on the dashboard
A plain install is unchanged: LAN-only, dashboard behind the API key. `llmhost install-newt` is the upgrade path. It installs a pinned, sha256-checked newt as a third user service and switches the dashboard to an OIDC authorization-code + PKCE sign-in limited to allowed groups or users. The API still takes the key. Secrets live in 0600 files, never in config.json, a service file or argv. `llmhost remove-newt` undoes it; setup/update keep it; `logs --newt` shows the tunnel (and `logs -f` now follows the service it was asked for). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
d07a92e5b5
commit
e814a2359d
11 changed files with 1155 additions and 26 deletions
|
|
@ -1,19 +1,23 @@
|
|||
"""Dashboard: status, metrics and model switching. Stdlib only.
|
||||
|
||||
Serves index.html and a JSON API on cfg["web_port"]. /api/* routes need the API key.
|
||||
Serves index.html and a JSON API on cfg["web_port"]. /api/* routes need the API key, or,
|
||||
when config.json's "dashboard_auth" is set to OIDC, a signed-in session instead (oidc.py).
|
||||
"""
|
||||
import hmac
|
||||
import html
|
||||
import json
|
||||
import re
|
||||
import socket
|
||||
import threading
|
||||
import time
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
from collections import deque
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
from pathlib import Path
|
||||
|
||||
from .. import hfcache, hw, opencode, paths, plan, service
|
||||
from . import oidc as oidc_mod
|
||||
|
||||
HERE = Path(__file__).resolve().parent
|
||||
SAMPLE_SECONDS = 2
|
||||
|
|
@ -408,7 +412,7 @@ class Sampler:
|
|||
threading.Thread(target=fn, args=("model",), daemon=True).start()
|
||||
|
||||
|
||||
def make_handler(sampler, key):
|
||||
def make_handler(sampler, key, oidc=None):
|
||||
class Handler(BaseHTTPRequestHandler):
|
||||
server_version = "llm-host"
|
||||
|
||||
|
|
@ -425,6 +429,15 @@ def make_handler(sampler, key):
|
|||
self.wfile.write(body)
|
||||
|
||||
def authed(self):
|
||||
if oidc:
|
||||
# Signed in through the provider; the API key isn't accepted here.
|
||||
if oidc.session(self.headers.get("Cookie", "")) is None:
|
||||
self.send_json(401, {"error": "unauthorized", "login": "/auth/login"})
|
||||
return False
|
||||
if self.command == "POST" and not oidc.same_origin(self.headers):
|
||||
self.send_json(403, {"error": "cross-origin request refused"})
|
||||
return False
|
||||
return True
|
||||
h = self.headers.get("Authorization", "")
|
||||
tok = h[7:] if h.startswith("Bearer ") else ""
|
||||
if hmac.compare_digest(tok.encode(), key.encode()):
|
||||
|
|
@ -432,8 +445,67 @@ def make_handler(sampler, key):
|
|||
self.send_json(401, {"error": "unauthorized"})
|
||||
return False
|
||||
|
||||
def redirect(self, url, cookies=()):
|
||||
self.send_response(302)
|
||||
self.send_header("Location", url)
|
||||
self.send_header("Cache-Control", "no-store")
|
||||
for c in cookies:
|
||||
self.send_header("Set-Cookie", c)
|
||||
self.send_header("Content-Length", "0")
|
||||
self.end_headers()
|
||||
|
||||
def auth_page(self, code, msg):
|
||||
body = (f"<!doctype html><meta charset=utf-8><meta name=viewport content='width=device-width'>"
|
||||
f"<title>llm-host sign-in</title><body style='font:15px system-ui;margin:3em auto;"
|
||||
f"max-width:32em;padding:0 16px'><h2>Couldn't sign you in</h2><p>{html.escape(msg)}</p>"
|
||||
f"<p><a href='/auth/login'>Try again</a></p>").encode()
|
||||
self.send_response(code)
|
||||
self.send_header("Content-Type", "text/html; charset=utf-8")
|
||||
self.send_header("Cache-Control", "no-store")
|
||||
self.send_header("Set-Cookie", oidc.cookie(oidc_mod.STATE_COOKIE, "", 0, "/auth/"))
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
def on_public_host(self):
|
||||
"""A session cookie only exists on the public URL's host, so a visit by any
|
||||
other name (mybox.local:11340) is sent there instead of to a login loop."""
|
||||
host = (self.headers.get("Host") or "").lower()
|
||||
return host == urllib.parse.urlsplit(oidc.public_url).netloc.lower()
|
||||
|
||||
def auth_get(self, path, query):
|
||||
if path == "/auth/config":
|
||||
s = oidc.session(self.headers.get("Cookie", "")) if oidc else None
|
||||
return self.send_json(200, {"mode": "oidc" if oidc else "key",
|
||||
"user": s and s["user"], "name": s and s["name"]})
|
||||
if not oidc:
|
||||
return self.send_json(404, {"error": "not found"})
|
||||
if path == "/auth/login":
|
||||
if not self.on_public_host():
|
||||
return self.redirect(oidc.public_url + "/auth/login")
|
||||
try:
|
||||
url, state = oidc.start()
|
||||
except oidc_mod.AuthError as e:
|
||||
return self.auth_page(502, str(e))
|
||||
return self.redirect(url, [oidc.cookie(oidc_mod.STATE_COOKIE, state, oidc_mod.LOGIN_TTL, "/auth/")])
|
||||
if path == "/auth/callback":
|
||||
params = {k: v[0] for k, v in urllib.parse.parse_qs(query).items()}
|
||||
cookie_state = oidc_mod.parse_cookies(self.headers.get("Cookie", "")).get(oidc_mod.STATE_COOKIE, "")
|
||||
try:
|
||||
s = oidc.finish(params, cookie_state)
|
||||
except oidc_mod.AuthError as e:
|
||||
return self.auth_page(403, str(e))
|
||||
print(f"llm-host dashboard: {s['user']} signed in", flush=True)
|
||||
return self.redirect("/", [oidc.cookie(oidc_mod.SESSION_COOKIE, s["id"], oidc_mod.SESSION_TTL),
|
||||
oidc.cookie(oidc_mod.STATE_COOKIE, "", 0, "/auth/")])
|
||||
self.send_json(404, {"error": "not found"})
|
||||
|
||||
def do_GET(self):
|
||||
path = self.path.split("?", 1)[0]
|
||||
path, _, query = self.path.partition("?")
|
||||
if path.startswith("/auth/"):
|
||||
return self.auth_get(path, query)
|
||||
if oidc and path in ("/", "/index.html") and not self.on_public_host():
|
||||
return self.redirect(oidc.public_url + "/")
|
||||
if path in ("/", "/index.html"):
|
||||
body = (HERE / "index.html").read_bytes()
|
||||
self.send_response(200)
|
||||
|
|
@ -455,6 +527,17 @@ def make_handler(sampler, key):
|
|||
|
||||
def do_POST(self):
|
||||
path = self.path.split("?", 1)[0]
|
||||
if path == "/auth/logout" and oidc:
|
||||
if not oidc.same_origin(self.headers):
|
||||
return self.send_json(403, {"error": "cross-origin request refused"})
|
||||
oidc.end(self.headers.get("Cookie", ""))
|
||||
self.send_response(200)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.send_header("Set-Cookie", oidc.cookie(oidc_mod.SESSION_COOKIE, "", 0))
|
||||
self.send_header("Content-Length", "11")
|
||||
self.end_headers()
|
||||
self.wfile.write(b'{"ok": true}')
|
||||
return
|
||||
if not self.authed():
|
||||
return
|
||||
try:
|
||||
|
|
@ -494,6 +577,11 @@ def serve():
|
|||
key = paths.api_key()
|
||||
sampler = Sampler(cfg, key)
|
||||
threading.Thread(target=sampler.loop, daemon=True).start()
|
||||
srv = ThreadingHTTPServer(("0.0.0.0", cfg["web_port"]), make_handler(sampler, key))
|
||||
print(f"llm-host dashboard on :{cfg['web_port']}", flush=True)
|
||||
auth = None
|
||||
conf = cfg.get("dashboard_auth") or {}
|
||||
if conf.get("mode") == "oidc":
|
||||
auth = oidc_mod.OIDC(conf, paths.oidc_secret())
|
||||
srv = ThreadingHTTPServer(("0.0.0.0", cfg["web_port"]), make_handler(sampler, key, auth))
|
||||
how = f"sign-in via {auth.issuer}, at {auth.public_url}" if auth else "API key"
|
||||
print(f"llm-host dashboard on :{cfg['web_port']} ({how})", flush=True)
|
||||
srv.serve_forever()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue