Split the internet upgrades: API-only tunnel, OIDC, authenticator codes, expose-dashboard
install-newt now sets up the tunnel for the API only; the dashboard stays on the LAN. Dashboard sign-in is its own step: install-oidc (the OIDC flow, split out) or install-otp, which pairs authenticator apps with a QR code in the terminal (stdlib QR encoder, TOTP per RFC 6238) so a code also opens the dashboard, with replay and lockout rules. expose-dashboard walks through publishing the dashboard, only after install-newt, and warns while it's protected by the API key alone; with codes it offers to switch the key off for the dashboard. Each install-* has a remove-*. Also opencode-install.sh: a standalone script, fetched from the repo, that installs opencode if needed, saves the API key to a 0600 file and adds the provider to opencode.json for new and existing configs. /v1/models reports whether the model reasons, for that script. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
e6ff4ce909
commit
c8b70afdbf
20 changed files with 1715 additions and 239 deletions
|
|
@ -1,17 +1,20 @@
|
|||
"""install-newt / remove-newt: pinned download, secrets kept out of config and output,
|
||||
OIDC switched on with the tunnel and off again, all against fake services."""
|
||||
"""The internet upgrades, against fake services: install-newt (API only, pinned download,
|
||||
secrets kept out of config/output/argv), install-oidc, install-otp, expose-dashboard, and
|
||||
their remove-* undo commands."""
|
||||
import contextlib
|
||||
import hashlib
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
||||
import harness
|
||||
|
||||
ROOT = harness.setup()
|
||||
from llmhost import cli, newt, paths, service
|
||||
from llmhost import cli, newt, otp, paths, service
|
||||
from llmhost.web import oidc
|
||||
|
||||
harness.guard(paths, ROOT)
|
||||
|
|
@ -38,16 +41,22 @@ newt._fetch = lambda url: (fetches.append(url), FAKE_BIN)[1]
|
|||
oidc.OIDC._http = staticmethod(lambda m, url, data=None, headers=None: (200, json.dumps({
|
||||
"issuer": ISSUER, "authorization_endpoint": ISSUER + "/a", "token_endpoint": ISSUER + "/t"}).encode()))
|
||||
|
||||
sys.stdin = io.StringIO() # no terminal: missing values are errors, not prompts
|
||||
|
||||
class TTY(io.StringIO):
|
||||
def isatty(self):
|
||||
return True
|
||||
|
||||
|
||||
def run(argv, env=None):
|
||||
import os
|
||||
def run(argv, env=None, tty_input=None):
|
||||
"""Run the CLI. Without tty_input there's no terminal (missing values are errors, not
|
||||
prompts); with it, stdin/stdout are a terminal and input() reads those lines."""
|
||||
saved = {k: os.environ.get(k) for k in ("LLMHOST_NEWT_SECRET", "LLMHOST_OIDC_SECRET")}
|
||||
for k in saved:
|
||||
os.environ.pop(k, None)
|
||||
os.environ.update(env or {})
|
||||
out, code = io.StringIO(), 0
|
||||
out, code = (TTY() if tty_input is not None else io.StringIO()), 0
|
||||
old_in = sys.stdin
|
||||
sys.stdin = TTY("".join(l + "\n" for l in tty_input)) if tty_input is not None else io.StringIO()
|
||||
try:
|
||||
with contextlib.redirect_stdout(out):
|
||||
cli.main(argv)
|
||||
|
|
@ -56,6 +65,7 @@ def run(argv, env=None):
|
|||
if isinstance(e.code, str):
|
||||
out.write(e.code)
|
||||
finally:
|
||||
sys.stdin = old_in
|
||||
for k, v in saved.items():
|
||||
os.environ.pop(k, None)
|
||||
if v is not None:
|
||||
|
|
@ -67,9 +77,9 @@ cfg = harness.config()
|
|||
paths.save_config(cfg)
|
||||
paths.api_key(create=True)
|
||||
|
||||
ARGS = ["install-newt", "--endpoint", "https://pangolin.example.test", "--newt-id", "site123",
|
||||
"--public-url", "https://dash.example.test", "--issuer", ISSUER, "--allow-group", "llm-admins"]
|
||||
SECRETS = {"LLMHOST_NEWT_SECRET": NEWT_SECRET, "LLMHOST_OIDC_SECRET": OIDC_SECRET}
|
||||
NEWT_ARGS = ["install-newt", "--endpoint", "https://pangolin.example.test", "--newt-id", "site123"]
|
||||
OIDC_ARGS = ["install-oidc", "--public-url", "https://dash.example.test", "--issuer", ISSUER,
|
||||
"--client-id", "client-from-provider", "--allow-group", "llm-admins"]
|
||||
|
||||
print("\n1. a plain install doesn't include newt")
|
||||
check("setup's services are model and web only", service.NAMES == ("model", "web"), service.NAMES)
|
||||
|
|
@ -87,76 +97,121 @@ check("nothing installed after a bad checksum", not newt.binary().exists())
|
|||
newt.SHA256 = real
|
||||
check("good checksum installs", newt.ensure_binary(lambda *_: None) is True)
|
||||
check("binary is executable", newt.binary().stat().st_mode & 0o111)
|
||||
check("version recorded", newt.installed_version() == newt.VERSION)
|
||||
check("second call is a no-op", newt.ensure_binary(lambda *_: None) is False)
|
||||
check("download URL is the pinned GitHub release",
|
||||
fetches[-1].startswith(f"https://github.com/fosrl/newt/releases/download/{newt.VERSION}/newt_"), fetches)
|
||||
|
||||
print("\n3. install-newt needs everything, without a terminal")
|
||||
code, out = run(ARGS)
|
||||
check("missing secrets -> error", code != 0 and "LLMHOST_NEWT_SECRET" in out, out)
|
||||
code, out = run([a for a in ARGS if a not in ("--allow-group", "llm-admins")], SECRETS)
|
||||
check("no allow list -> error", code != 0 and "allow" in out, out)
|
||||
code, out = run([*ARGS[:6], "http://dash.example.test", *ARGS[7:]], SECRETS)
|
||||
check("an http:// public URL is refused", code != 0 and "https" in out, out)
|
||||
check("nothing configured by the failures", "newt" not in paths.load_config() and not installed)
|
||||
|
||||
print("\n4. install-newt")
|
||||
print("\n3. install-newt: the API only")
|
||||
code, out = run(NEWT_ARGS)
|
||||
check("missing secret without a terminal -> error", code != 0 and "LLMHOST_NEWT_SECRET" in out, out)
|
||||
calls.clear()
|
||||
code, out = run(ARGS, SECRETS)
|
||||
code, out = run(NEWT_ARGS, {"LLMHOST_NEWT_SECRET": NEWT_SECRET})
|
||||
c = paths.load_config()
|
||||
check("succeeds", code == 0, out)
|
||||
check("newt config saved", c.get("newt") == {"endpoint": "https://pangolin.example.test", "id": "site123"}, c.get("newt"))
|
||||
check("dashboard switched to OIDC", c["dashboard_auth"]["mode"] == "oidc" and c["dashboard_auth"]["issuer"] == ISSUER)
|
||||
check("allow list saved", c["dashboard_auth"]["allowed_groups"] == ["llm-admins"])
|
||||
check("dashboard sign-in untouched", "dashboard_auth" not in c and not paths.OIDC_SECRET_FILE.exists())
|
||||
check("newt service installed and started", ("install", "newt") in calls and ("restart", "newt") in calls, calls)
|
||||
check("dashboard restarted to pick up sign-in", ("restart", "web") in calls, calls)
|
||||
check("secrets saved 0600", all((f.stat().st_mode & 0o777) == 0o600
|
||||
for f in (paths.NEWT_SECRET_FILE, paths.OIDC_SECRET_FILE)))
|
||||
check("secrets read back", paths.NEWT_SECRET_FILE.read_text().strip() == NEWT_SECRET
|
||||
and paths.oidc_secret() == OIDC_SECRET)
|
||||
cfg_text = paths.CONFIG_FILE.read_text()
|
||||
check("no secret in config.json", NEWT_SECRET not in cfg_text and OIDC_SECRET not in cfg_text)
|
||||
check("no secret in the output", NEWT_SECRET not in out and OIDC_SECRET not in out)
|
||||
check("prints the redirect URI", "https://dash.example.test/auth/callback" in out, out)
|
||||
|
||||
print("\n5. a re-run keeps what it isn't given")
|
||||
check("dashboard not restarted", ("restart", "web") not in calls, calls)
|
||||
check("only the API resource is described", f":{c['api_port']}" in out and f":{c['web_port']}" not in out, out)
|
||||
check("points to expose-dashboard for the rest", "expose-dashboard" in out and "install-oidc" in out, out)
|
||||
check("secret saved 0600, not in config or output",
|
||||
(paths.NEWT_SECRET_FILE.stat().st_mode & 0o777) == 0o600
|
||||
and NEWT_SECRET not in paths.CONFIG_FILE.read_text() and NEWT_SECRET not in out)
|
||||
code, out = run(["install-newt", "--newt-id", "site456"])
|
||||
c = paths.load_config()
|
||||
check("re-run without secrets succeeds", code == 0, out)
|
||||
check("changed value updated", c["newt"]["id"] == "site456")
|
||||
check("other values kept", c["newt"]["endpoint"] == "https://pangolin.example.test"
|
||||
and c["dashboard_auth"]["allowed_groups"] == ["llm-admins"])
|
||||
check("secrets kept", paths.NEWT_SECRET_FILE.read_text().strip() == NEWT_SECRET and paths.oidc_secret() == OIDC_SECRET)
|
||||
check("re-run keeps what it isn't given", code == 0 and c["newt"] == {"endpoint": "https://pangolin.example.test", "id": "site456"}
|
||||
and paths.NEWT_SECRET_FILE.read_text().strip() == NEWT_SECRET, out)
|
||||
|
||||
print("\n6. newt starts with its credentials in the environment, not its arguments")
|
||||
print("\n4. newt starts with its credentials in the environment, not its arguments")
|
||||
seen = {}
|
||||
import os
|
||||
real_execve = os.execve
|
||||
os.execve = lambda exe, argv, env: seen.update(exe=exe, argv=argv, env=env)
|
||||
newt.run()
|
||||
os.execve = real_execve
|
||||
check("runs the installed binary", seen["exe"] == str(newt.binary()))
|
||||
check("no secret on the command line", seen["argv"] == [str(newt.binary())], seen["argv"])
|
||||
check("endpoint, id and secret in env",
|
||||
(seen["env"]["PANGOLIN_ENDPOINT"], seen["env"]["NEWT_ID"], seen["env"]["NEWT_SECRET"])
|
||||
== ("https://pangolin.example.test", "site456", NEWT_SECRET))
|
||||
|
||||
print("\n7. setup/update keep it")
|
||||
print("\n5. expose-dashboard won't publish behind the API key alone without asking")
|
||||
code, out = run(["expose-dashboard", "--public-url", "https://dash.example.test"])
|
||||
check("refused without a terminal or --force", code != 0 and "install-oidc" in out and "install-otp" in out, out)
|
||||
code, out = run(["expose-dashboard", "--public-url", "https://dash.example.test"], tty_input=["n"])
|
||||
check("refused when you say no", code != 0 and "Not published" in out, out)
|
||||
code, out = run(["expose-dashboard", "--public-url", "https://dash.example.test", "--force"])
|
||||
check("--force walks through it anyway", code == 0 and f":{paths.load_config()['web_port']}" in out
|
||||
and "dash.example.test" in out, out)
|
||||
|
||||
print("\n6. install-oidc")
|
||||
code, out = run(OIDC_ARGS)
|
||||
check("missing secret -> error", code != 0 and "LLMHOST_OIDC_SECRET" in out, out)
|
||||
code, out = run([a for a in OIDC_ARGS if a not in ("--allow-group", "llm-admins")], {"LLMHOST_OIDC_SECRET": OIDC_SECRET})
|
||||
check("no allow list -> error", code != 0 and "allow" in out, out)
|
||||
calls.clear()
|
||||
code, out = run(OIDC_ARGS, {"LLMHOST_OIDC_SECRET": OIDC_SECRET})
|
||||
c = paths.load_config()
|
||||
check("succeeds", code == 0, out)
|
||||
check("dashboard switched to OIDC", c["dashboard_auth"]["mode"] == "oidc" and c["dashboard_auth"]["client_id"] == "client-from-provider")
|
||||
check("newt untouched", c["newt"]["id"] == "site456")
|
||||
check("dashboard restarted", ("restart", "web") in calls, calls)
|
||||
check("prints the redirect URI", "https://dash.example.test/auth/callback" in out, out)
|
||||
check("secret 0600, not in config or output", (paths.OIDC_SECRET_FILE.stat().st_mode & 0o777) == 0o600
|
||||
and OIDC_SECRET not in paths.CONFIG_FILE.read_text() and OIDC_SECRET not in out)
|
||||
code, out = run(["expose-dashboard"])
|
||||
check("expose-dashboard with OIDC: no question, URL from OIDC", code == 0 and "dash.example.test" in out
|
||||
and "redirect URI" in out, out)
|
||||
|
||||
print("\n7. install-otp")
|
||||
code, out = run(["install-otp"])
|
||||
check("needs a terminal", code != 0 and "terminal" in out, out)
|
||||
FIXED = "JBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP"
|
||||
otp.new_secret = lambda: FIXED
|
||||
code, out = run(["install-otp"], tty_input=["000000", "000001", "000002"])
|
||||
check("wrong codes -> not paired", code != 0 and not paths.OTP_SECRET_FILE.exists(), out)
|
||||
calls.clear()
|
||||
code, out = run(["install-otp"], tty_input=[otp.code_at(FIXED, time.time())])
|
||||
check("right code -> paired", code == 0 and paths.OTP_SECRET_FILE.read_text().strip() == FIXED, out)
|
||||
check("secret 0600", (paths.OTP_SECRET_FILE.stat().st_mode & 0o777) == 0o600)
|
||||
check("showed a QR code and the manual key", "▀" in out and otp.grouped(FIXED) in out, out[:300])
|
||||
check("says OIDC takes precedence while it's on", "remove-oidc" in out, out)
|
||||
check("dashboard restarted", ("restart", "web") in calls)
|
||||
code, out = run(["install-otp"], tty_input=["n"])
|
||||
check("re-running asks before replacing", code != 0 and "kept" in out and paths.OTP_SECRET_FILE.read_text().strip() == FIXED, out)
|
||||
code, out = run(["install-otp", "--show"], tty_input=[])
|
||||
check("--show shows the existing pairing", code == 0 and otp.grouped(FIXED) in out, out[:200])
|
||||
|
||||
print("\n8. status and setup keep it all")
|
||||
code, out = run(["status"])
|
||||
check("status names the sign-in", "OIDC via " + ISSUER in out, out)
|
||||
carried = cli.carried_settings(paths.load_config(), set())
|
||||
check("newt and dashboard_auth carried", "newt" in carried and "dashboard_auth" in carried, carried.keys())
|
||||
|
||||
print("\n8. remove-newt")
|
||||
print("\n9. remove-oidc, then expose-dashboard with codes")
|
||||
code, out = run(["remove-oidc"])
|
||||
c = paths.load_config()
|
||||
check("OIDC off, secret deleted", code == 0 and "dashboard_auth" not in c and not paths.OIDC_SECRET_FILE.exists(), out)
|
||||
check("says codes work again", "authenticator code" in out, out)
|
||||
code, out = run(["status"])
|
||||
check("status: key or code", "API key or authenticator code" in out, out)
|
||||
code, out = run(["expose-dashboard", "--public-url", "https://dash.example.test"], tty_input=["y", ""])
|
||||
c = paths.load_config()
|
||||
check("offers to turn the key off for the dashboard", code == 0 and c.get("dashboard_key_login") is False, out)
|
||||
code, out = run(["status"])
|
||||
check("status: codes only", "sign-in authenticator code" in out, out)
|
||||
check("setup keeps the key switched off", cli.carried_settings(c, set()).get("dashboard_key_login") is False)
|
||||
|
||||
print("\n10. remove-otp and remove-newt")
|
||||
code, out = run(["remove-otp"])
|
||||
c = paths.load_config()
|
||||
check("unpaired, key back on", code == 0 and not paths.OTP_SECRET_FILE.exists() and "dashboard_key_login" not in c, out)
|
||||
calls.clear()
|
||||
code, out = run(["remove-newt"])
|
||||
c = paths.load_config()
|
||||
check("succeeds", code == 0, out)
|
||||
check("service removed", ("uninstall", "newt") in calls and "newt" not in installed, calls)
|
||||
check("config back to LAN", "newt" not in c and "dashboard_auth" not in c)
|
||||
check("secrets deleted", not paths.NEWT_SECRET_FILE.exists() and not paths.OIDC_SECRET_FILE.exists())
|
||||
check("binary deleted", not newt.binary().exists())
|
||||
check("dashboard restarted", ("restart", "web") in calls)
|
||||
check("newt removed", code == 0 and ("uninstall", "newt") in calls and "newt" not in c
|
||||
and not paths.NEWT_SECRET_FILE.exists() and not newt.binary().exists(), out)
|
||||
check("rest of the config untouched", c.get("api_port") == cfg.get("api_port") and c.get("models") == cfg.get("models"))
|
||||
code, out = run(["expose-dashboard"])
|
||||
check("expose-dashboard needs newt first", code != 0 and "install-newt" in out, out)
|
||||
code, out = run(["remove-newt"])
|
||||
check("running it again is harmless", code == 0 and "wasn't installed" in out, out)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue