LeiWang_2000 asked for account groups, so a gateway key can be limited to
a group of accounts, or keep a group out, instead of picking accounts one
by one (on top of v0.1.3504's "Keep accounts out").
An account group is a named set of accounts of any providers, kept in
account-groups.json beside caller-keys.json (access.AccountGroup). A key's
account list names one as "@<id>" (use only its accounts) or "!@<id>"
(every account but its). identityOf expands the groups of the key and of
its issuer on every request, so every route that reads the Identity
(AllowsAccount, HeldModels, the routes' model checks) holds to the group
as it is now: a membership change reaches every key naming it at once, and
an account signed out since matches nothing. Fails closed: a group gone,
or a used group with no account, holds the key to nothing
(heldToNothing); unreadable groups refuse a key naming one and leave other
keys alone. A group a key names can't be removed (the error names the
keys); ReplaceAccount moves a rotated key in the groups too. Groups are
backed up and synced (WebDAV) with the gateway keys. A magpie older than
the mark reads "@<id>" as an account of no provider and fails closed.
CLI: `magpie gateway-key groups add|set|rename|remove`, and
`gateway-key accounts <id> @Work` / `!@Work`.
GUI: an "Account groups" editor (modal, rows of name + accounts picked in
the app menu) from the keys' header; the key's account menus list the
groups as options; the badge says "Work" / "Not Work". No layout change to
the rows.
Tests:
- internal/access TestAccountGroupHoldsAKey / ...AnIssuedKey: save rules,
name->id, used+kept-out refused, held/kept-out allows and models, a
change reaching both keys, rotate, remove refused, empty group, groups
gone, unreadable groups.
- internal/gateway TestGatewayKeyAccountGroupsKeyedRoutes: a held and a
kept-out key on 7 routes, flipped by a group change, and nothing asked
once the group is gone.
- internal/cli TestGatewayKeyAccountGroupsCLI.
- GUI gateway-key-account-groups.test.cjs: 5 languages x chromium/webkit
x 1100/440.
Broken by hand (each red, then restored):
- identityOf not expanding groups: access test red, gateway test 403.
- a gone/empty held group not becoming heldToNothing: access test red
("held to an empty group"), gateway test asked kA with the group gone.
Run on mote at this tree: gofmt -l clean; go vet darwin (touched
packages), GOOS=linux -tags nogui ./..., GOOS=windows ./... clean;
go test -count=1 access, cli, gui (size budgets included), backup,
davsync, gateway: all ok;
GUI: every gateway-*.test.cjs plus i18n-keys, i18n-en-no-cjk, key-page,
plugin-kernel, readable-focus, chromium + webkit: 571 of 574 at
--test-concurrency=6. The 3 reds: gateway-caller-keys and
gateway-key-models chromium en (the 560px read, red on main too, fixed in
the next commit), and gateway-key-groups zh once, 12 of 12 alone (#1974).
Rebased on c4a542466 (regexps made lazy across 157 files): gofmt, darwin
vet, go test of the root (lazyre guard), access, gui, backup, and the
cli and gateway account-group, key and parity tests again: all ok.
Not tried: a real account's requests through a group-held key (fixture
providers only), the Windows box, the real Linux app.
121 lines
4.9 KiB
Go
121 lines
4.9 KiB
Go
package gateway
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/access"
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// LeiWang_2000 on X: a gateway key held to an account group ("@<id>"),
|
|
// or keeping one out ("!@<id>"), asks only the accounts the group holds
|
|
// now, or every other, on every route that spends an account; a group
|
|
// gone holds the key to nothing.
|
|
func TestGatewayKeyAccountGroupsKeyedRoutes(t *testing.T) {
|
|
fresh(t)
|
|
up := newKeptOutUp(t)
|
|
for _, p := range []provider.Provider{
|
|
{ID: "relay", Name: "Relay", Chat: up.URL + "/v1", Anthropic: up.URL, Key: "kA", Keys: []provider.KeyAccount{{Key: "kB"}}, Models: []string{"m", "gpt-image-1"}},
|
|
{ID: "office", Name: "Office", Preset: provider.RemoteMagpiePreset, Chat: up.URL + "/v1", Key: "oA", Keys: []provider.KeyAccount{{Key: "oB"}}, Models: []string{"grok-imagine-video"}},
|
|
{ID: "jev", Name: "Jev", Decide: up.URL + "/decide/v1", Key: "jA", Keys: []provider.KeyAccount{{Key: "jB"}}, Models: []string{"jev-latest"}},
|
|
} {
|
|
if err := provider.Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := catalog.SaveLive("relay", up.URL+"/v1", []catalog.Model{{ID: "m"}, {ID: "gpt-image-1"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
group := func(ks ...string) []string {
|
|
var out []string
|
|
for _, k := range ks {
|
|
out = append(out, map[byte]string{'k': "relay", 'o': "office", 'j': "jev"}[k[0]]+"/"+provider.KeyID(k))
|
|
}
|
|
return out
|
|
}
|
|
g, err := access.SaveAccountGroup(access.AccountGroup{Name: "Work", Accounts: group("kB", "oB", "jB")})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
keys, secrets := newCaller(t, "Held", "Out")
|
|
if _, err := access.Update("accounts-key", access.Change{Key: keys[0].ID, Accounts: []string{"@Work"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := access.Update("accounts-key", access.Change{Key: keys[1].ID, Accounts: []string{"!@Work"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := New()
|
|
ask := func(secret, method, path, body string) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
r := httptest.NewRequest(method, path, strings.NewReader(body))
|
|
r.Header.Set("Authorization", "Bearer "+secret)
|
|
w := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(w, r)
|
|
return w
|
|
}
|
|
routes := []struct {
|
|
name, path, body string
|
|
local bool
|
|
}{
|
|
{"chat", "/v1/chat/completions", `{"model":"relay/m","messages":[{"role":"user","content":"hi"}]}`, false},
|
|
{"messages", "/v1/messages", `{"model":"relay/m","max_tokens":16,"messages":[{"role":"user","content":"hi"}]}`, false},
|
|
{"responses", "/v1/responses", `{"model":"relay/m","input":"hi"}`, false},
|
|
{"count_tokens", "/v1/messages/count_tokens", `{"model":"relay/m","messages":[{"role":"user","content":"hi"}]}`, true},
|
|
{"images", "/v1/images/generations", `{"model":"relay/gpt-image-1","prompt":"a magpie"}`, false},
|
|
{"videos", "/v1/videos", `{"model":"office/grok-imagine-video","prompt":"a magpie"}`, false},
|
|
{"systemone", "/v1/systemone", `{"model":"jev/jev-latest","state":{"message":"hi"},"questions":{"intent":{"type":"choice"}}}`, false},
|
|
}
|
|
// served by the one account of each provider want names
|
|
served := func(who, secret string, want map[string]string) {
|
|
t.Helper()
|
|
for _, c := range routes {
|
|
w := ask(secret, "POST", c.path, c.body)
|
|
if w.Code != 200 {
|
|
t.Fatalf("%s %s: %d %s", who, c.name, w.Code, w.Body.String())
|
|
}
|
|
k := want[map[string]string{"videos": "office", "systemone": "jev"}[c.name]]
|
|
if k == "" {
|
|
k = want["relay"]
|
|
}
|
|
if got := up.take(); !slices.Equal(got, []string{k}) {
|
|
t.Fatalf("%s %s: asked with %v, want %s alone", who, c.name, got, k)
|
|
}
|
|
}
|
|
}
|
|
// the default order asks kA, oA, jA first: the group's B accounts
|
|
// serve the key held to it, the A ones the key keeping it out
|
|
served("held", secrets[0], map[string]string{"relay": "kB", "office": "oB", "jev": "jB"})
|
|
served("out", secrets[1], map[string]string{"relay": "kA", "office": "oA", "jev": "jA"})
|
|
|
|
// the group changed: both keys follow it on the next request
|
|
g.Accounts = group("kA", "oA", "jA")
|
|
if _, err := access.SaveAccountGroup(g); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
served("held, changed", secrets[0], map[string]string{"relay": "kA", "office": "oA", "jev": "jA"})
|
|
served("out, changed", secrets[1], map[string]string{"relay": "kB", "office": "oB", "jev": "jB"})
|
|
|
|
// the group gone: neither key asks any account or lists a model
|
|
if err := access.RestoreGroups(nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i, secret := range secrets {
|
|
for _, c := range routes {
|
|
w := ask(secret, "POST", c.path, c.body)
|
|
if got := up.take(); len(got) != 0 {
|
|
t.Fatalf("key %d %s: asked %v with its group gone (%d %s)", i, c.name, got, w.Code, w.Body.String())
|
|
}
|
|
if !c.local && w.Code == 200 {
|
|
t.Fatalf("key %d %s: served with its group gone: %s", i, c.name, w.Body.String())
|
|
}
|
|
}
|
|
if w := ask(secret, "GET", "/v1/models", ""); strings.Contains(w.Body.String(), `"relay/m"`) {
|
|
t.Fatalf("key %d: /v1/models lists relay with its group gone", i)
|
|
}
|
|
}
|
|
}
|