From 2eac916170245f30d6de58a1283be2585b1b2919 Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 31 Oct 2026 13:44:29 +0800 Subject: [PATCH] gateway: short-lived gateway keys, issued by POST /v1/magpie/keys or magpie gateway-key issue MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit xazaj on X is moving an enterprise LLM gateway onto magpie. Their orchestrator gives each sandboxed agent a key of its own, and that key has to stop working by itself. A gateway key can now expire (Key.Expires) and can be issued under another key (Key.Issuer). It lives from 1m to 30d. It can be held to a set of models, a set of accounts, tokens or cost over its whole life (one "hours" limit as long as its life), and read-only. - POST /v1/magpie/keys {"ttl":"1h"|3600, name, models, accounts, tokens, cost, readOnly} answers 201 with the secret once, no-store. GET lists the caller's keys, masked. DELETE /v1/magpie/keys/{id} revokes one. - magpie gateway-key issue --ttl 1h [--name] [--models] [--accounts] [--tokens] [--cost] [--read-only] [--from ] [--json] prints the key alone. gateway-key issuer on|off lets a key issue keys. list shows until/expired/issued by. Who may issue: - a key the user let issue (Issues), from wherever that key is let in, so a container or CI can; - with no key, a request that profileRefused passes: loopback, no forwarding header, no Origin/Sec-Fetch/Mozilla marks, a loopback Host. Such a request can already use every model with no key and run the CLI, so it gains nothing. Never: a page, another computer without such a key, a key not let issue, an issued or expiring key, or a read-only key (readOnlyGuard). An issued key never outlives or exceeds its issuer. IssueKey clamps its expiry and refuses models and accounts the issuer can't use. On every authentication (access.Check, under Authenticate and On), identityOf folds the key into its issuer as the issuer is now: models, accounts, read-only, quotas and expiry. An issuer that is off, removed or expired refuses its keys. Spend counts against the issuer too: the new usage field callerIssuerId, budget.read/Append counting both keys, and Reserve reserving on both. /v1/magpie/limit answers expires and issuerLimits. An expired or refused key gets 401 with why ("… expired at …: ask for a new one") on every route, from loopback too. Otherwise a held sandbox whose key ran out would be let in as no key. Expired keys are pruned after an hour (every 10 minutes while serving, and on each issue). rowText packs CallerIssuerID: without it, usage.Visit returned the field empty and the issuer's limit counted none of its keys' calls. Docs: gateway-routing.md has "Short-lived gateway keys" and the /v1/magpie stats API table. site/public/docs remote has "Short-lived keys" and "Stats API" in en, zh and ja. GitHub OIDC is filed as #4531. Run at base 07a3312c5 (origin/main, fetched just before): - mote: go test -count=1 -timeout 45m on access, budget, gwbudget, usage, cli, gateway, davsync, agent: ok. - mote: internal/gui fails TestCoreScriptBudget (#4526), TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and ten AppKit window tests killed at their deadline (#4125, #2645). The first three fail the same at origin/main 07a3312c5 on mote. This change touches no GUI file. - mote: gofmt -l clean; go vet ./...; GOOS=linux go vet -tags nogui ./...; GOOS=windows go vet ./...; go build; node --test site/*.test.mjs (49 pass). - win-run: access and budget ok. usage fails TestDesktopSessionAccountMetadata the same at origin/main (#1356). cli's TestLibraryMCPHub failed once on TempDir cleanup (#4140) and passed alone. - Real binary on mote, sandbox HOME, port 47811: issued a 1m key with curl, read its limit, listed it, saw `gateway-key list` show "until", had a page's Origin refused 403, and after 62s /v1/models answered 401 "the gateway key "sandbox-1" expired at …". Broken by hand, each restored: - no expiry check in Check: TestIssuedKeyExpires and TestIssuedKeyExpiresOnEveryRoute go red. - no 401 for a refused key on loopback: TestIssuedKeyExpiresOnEveryRoute goes red (the expired key let in as no key). - budget not counting CallerIssuerID: TestIssuedKeysSpendTheirIssuersLimit goes red (5 calls reached the upstream). Not tried: a Responses WebSocket open across the expiry. The GUI shows no expiry or issuer yet (issued keys look like plain keys until pruned). A held issuer's /v1/magpie/usage shows its own rows, not its keys'. The refused-key 401 doesn't count toward tooManyAuthFails. Issued keys sync through davsync like other keys, with their expiry. --- docs/subsystems/gateway-routing.md | 83 ++++++ internal/access/access.go | 67 +++-- internal/access/issue.go | 374 +++++++++++++++++++++++++ internal/access/issue_test.go | 272 ++++++++++++++++++ internal/budget/budget.go | 43 ++- internal/cli/gateway_key_issue_test.go | 77 +++++ internal/cli/gateway_keys_cli.go | 140 ++++++++- internal/cli/main.go | 3 + internal/gateway/gateway.go | 5 + internal/gateway/issue_keys.go | 175 ++++++++++++ internal/gateway/issue_keys_test.go | 218 ++++++++++++++ internal/gateway/key_limit.go | 19 +- internal/gateway/lan.go | 20 +- internal/gateway/readonly_test.go | 1 + internal/gateway/search_api.go | 2 +- internal/gateway/search_native.go | 2 +- internal/gateway/usage_api.go | 2 +- internal/gwbudget/gwbudget.go | 2 +- internal/usage/reported.go | 2 +- internal/usage/request_page.go | 8 +- internal/usage/usage.go | 3 + site/public/docs/ja/remote.html | 44 +++ site/public/docs/remote.html | 44 +++ site/public/docs/zh/remote.html | 44 +++ 24 files changed, 1599 insertions(+), 51 deletions(-) create mode 100644 internal/access/issue.go create mode 100644 internal/access/issue_test.go create mode 100644 internal/cli/gateway_key_issue_test.go create mode 100644 internal/gateway/issue_keys.go create mode 100644 internal/gateway/issue_keys_test.go diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md index d927f6a5b..545b36156 100644 --- a/docs/subsystems/gateway-routing.md +++ b/docs/subsystems/gateway-routing.md @@ -615,6 +615,89 @@ when nothing else makes videos. Covered by `TestMiniMaxKeysMakeH3Videos`, `TestMiniMaxComesAfterOtherVideomakers` and `TestMiniMaxVideoMovesPastATokenPlanKey`. +## Short-lived gateway keys + +A gateway key may expire (`access.Key.Expires`), and one issued under another +key keeps that key's id (`Key.Issuer`). They are for an orchestrator that +gives each sandboxed agent a key of its own that stops working by itself +(xazaj on X, moving off an enterprise LLM gateway). `access.IssueKey` in +[`issue.go`](../../internal/access/issue.go) makes one, living 1 minute to +30 days (`MinTTL`, `MaxTTL`); `--tokens`/`--cost` cap it over its whole life, +as one `hours` limit as long as its life from the moment it is issued. + +Where it is issued: + +- `POST /v1/magpie/keys` with `{"ttl": "1h" | 3600, "name", "models", + "accounts", "tokens", "cost", "readOnly"}` answers 201 with the key's + `id`, `name`, `key` (the secret, told once, `Cache-Control: no-store`), + `expires` and what it is held to. `GET /v1/magpie/keys` lists the keys + the caller issued, masked; `DELETE /v1/magpie/keys/{id}` revokes one. + See [`issue_keys.go`](../../internal/gateway/issue_keys.go). +- `magpie gateway-key issue --ttl 1h [--name n] [--models a,b] [--accounts + a,b] [--tokens N] [--cost USD] [--read-only] [--from ] [--json]` + prints the secret alone; `gateway-key issuer on|off` lets a key + issue; `gateway-key list` shows `until