A gateway key could only be held to accounts picked one by one, so an account signed in after the key was saved was never used. An entry "<provider>/!<account>" (access.ExcludeMark) now keeps that account out: the key uses every other account of the provider, one added later too, and never the one kept out. - access: AllowsAccount refuses a kept-out account first, and counts only the entries naming accounts to use for "the providers it names", so a provider with only kept-out entries serves every other account. HeldModels and AccountModels ignore kept-out entries (keeping one out holds the key to no model). An issued key keeps out what its issuer does (foldAccounts). CleanAccounts resolves the ref after the mark and refuses an account both used and kept out. ReplaceAccount moves kept-out entries too. AccountsShown renders "X; every account but Z" for the CLI and the gateway's refusals. - CLI: `magpie gateway-key accounts <id> relay/!Second` prints "every account but relay/Second"; list and models read it. - GUI: the key row's menu has "Keep accounts out", which opens the app's menu (openProtoMenu, no native select); a badge names what is kept out and opens the same menu; an account the key was held to and is now kept out leaves the held ones. Strings in zh, zh-TW, ja and de. - docs: reference.md and providers-accounts.md. Run (mote, at this tree): - gofmt -l internal/ clean; go vet darwin, GOOS=linux -tags nogui and GOOS=windows of access, cli and gateway: clean. - go test -count=1 ./internal/access ./internal/cli ./internal/gateway: ok. New: TestKeptOutAccounts, TestKeptOutFolded, TestKeptOutReplaced; TestGatewayKeyKeptOutAccountsKeyedRoutes (chat, messages, responses, count_tokens, images, videos, System One and /v1/models over a key provider, a remote magpie and a Decide provider: the kept-out key never asked, every account kept out asks nothing, a key added after the gateway key was saved is used); TestGatewayKeyKeptOutCodexAccounts (Codex sign-ins, one signed in later used); TestGatewayKeyKeptOutQuotaCards; TestGatewayKeyAccountsCLI extended. - GUI: gateway-key-kept-out.test.cjs, chromium and webkit, 1100 and 440 px, en zh zh-TW ja de: 20 of 20. Every gateway-*.test.cjs and i18n-*.test.cjs file in both engines: 496 of 516, the 20 red all gateway-card's count of the row menu's items (5, now 6), updated here; then gateway-card, api-key-theme, gateway-connect-keys, gui-de, gui-ja and gui-zh-tw: 39 of 39. - Again after rebasing onto4869d0c96(this commit's tree): gofmt clean, vet darwin/linux nogui/windows clean, go test access, cli, gateway and gui (TestCoreScriptBudget among them) ok; gateway-key-kept-out, gateway-card, gateway-key-accounts, i18n-keys, i18n-en-no-cjk, gui-zh-tw, gui-ja, gui-de in both engines: 61 of 61. - Rebased ontob922e5168(nanobot, which also adds i18n strings): go test gui and access ok; gateway-key-kept-out, i18n-keys, i18n-en-no-cjk, gui-zh-tw, gui-ja, gui-de in both engines: 37 of 37. Broken by hand: - AllowsAccount without its kept-out refusal: TestKeptOutAccounts, TestKeptOutFolded and all three TestGatewayKeyKeptOut* tests red. - gatewayKeptOutPick keeping a held account it was told to keep out: gateway-key-kept-out red (the server key's accounts-key body). Not tried: a real account kept out on a running magpie (the tests use fake upstreams and sign-ins); Windows (no OS-specific code).
149 lines
5.6 KiB
Go
149 lines
5.6 KiB
Go
package access
|
|
|
|
import (
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// A key may keep some accounts out instead of naming the ones it uses
|
|
// (LeiWang_2000 on X): "<provider>/!<account>" lets it use every other
|
|
// account of the provider, one signed in after the key was saved too,
|
|
// and never the one kept out.
|
|
func TestKeptOutAccounts(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Chat: "http://127.0.0.1:1/v1", Keys: []provider.KeyAccount{{Key: "sk-1", Name: "First"}, {Key: "sk-2"}}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
k1, k2, k3 := provider.KeyID("sk-1"), provider.KeyID("sk-2"), provider.KeyID("sk-3")
|
|
|
|
// kept by its id, the mark kept, a name resolving to it too
|
|
for _, c := range []struct {
|
|
name string
|
|
in []string
|
|
want []string
|
|
bad string
|
|
}{
|
|
{"kept out by its fingerprint", []string{"relay/!" + k2}, []string{"relay/!" + k2}, ""},
|
|
{"kept out by the name it is shown by", []string{"relay/!First"}, []string{"relay/!" + k1}, ""},
|
|
{"once, told apart in lower case", []string{"relay/!" + k2, " RELAY/!" + strings.ToUpper(k2) + " "}, []string{"relay/!" + k2}, ""},
|
|
{"used and kept out is refused", []string{"relay/" + k1, "relay/!First"}, nil, "either used or kept out"},
|
|
{"a mark with no account", []string{"relay/!"}, nil, "<provider>/<account>"},
|
|
{"one the provider has not", []string{"relay/!" + k3}, nil, "has no key"},
|
|
} {
|
|
out, err := CleanAccounts(c.in)
|
|
if c.bad != "" {
|
|
if err == nil || !strings.Contains(err.Error(), c.bad) {
|
|
t.Errorf("%s: said %v, want %q in it", c.name, err, c.bad)
|
|
}
|
|
continue
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("%s: %v", c.name, err)
|
|
}
|
|
if !slices.Equal(out, c.want) {
|
|
t.Errorf("%s: kept %v, want %v", c.name, out, c.want)
|
|
}
|
|
}
|
|
|
|
who := Identity{Accounts: []string{"relay/!" + strings.ToUpper(k2)}}
|
|
if !who.AllowsAccount("relay", k1) || who.AllowsAccount("relay", k2) {
|
|
t.Fatal("kept out: the other used, it never")
|
|
}
|
|
// a key signed in after the key was saved is one it may use
|
|
if !who.AllowsAccount("relay", k3) {
|
|
t.Fatal("an account added later was held out")
|
|
}
|
|
if !who.AllowsAccount("other", k2) {
|
|
t.Fatal("another provider's was held")
|
|
}
|
|
// a provider with no account left: the list names none it uses, so
|
|
// it serves as it always did
|
|
if !who.AllowsAccount("relay", "") {
|
|
t.Fatal("no account named to use, yet the provider was held")
|
|
}
|
|
// used and kept out of different providers
|
|
mixed := Identity{Accounts: []string{"relay/" + k1, "codex/!acct-2"}}
|
|
if !mixed.AllowsAccount("relay", k1) || mixed.AllowsAccount("relay", k3) {
|
|
t.Fatal("relay is held to k1")
|
|
}
|
|
if !mixed.AllowsAccount("codex", "acct-1") || !mixed.AllowsAccount("codex", "acct-9") || mixed.AllowsAccount("codex", "acct-2") {
|
|
t.Fatal("codex: every account but acct-2")
|
|
}
|
|
|
|
// kept out holds the key to no model
|
|
if ms := HeldModels(nil, []string{"relay/!" + k2}); ms != nil {
|
|
t.Fatalf("kept out only: models %v, want every one", ms)
|
|
}
|
|
if ms := HeldModels(nil, []string{"relay/" + k1, "codex/!acct-2"}); !slices.Equal(ms, []string{"relay/*"}) {
|
|
t.Fatalf("models %v", ms)
|
|
}
|
|
|
|
// shown
|
|
names := map[string]string{"relay/" + k1: "relay/First", "relay/" + k2: "relay/sk-…2"}
|
|
if got := AccountsShown(names, []string{"relay/!" + k2}); got != "every account but relay/sk-…2" {
|
|
t.Fatalf("shown %q", got)
|
|
}
|
|
if got := AccountsShown(names, []string{"relay/" + k1, "codex/!acct-2"}); got != "relay/First; every account but codex/acct-2" {
|
|
t.Fatalf("shown %q", got)
|
|
}
|
|
}
|
|
|
|
// An issued key folds in its issuer's kept-out accounts, and may not
|
|
// name one of them.
|
|
func TestKeptOutFolded(t *testing.T) {
|
|
for _, c := range []struct {
|
|
child, issuer, want []string
|
|
}{
|
|
{nil, []string{"codex/!a2"}, nil},
|
|
{[]string{"codex/a1"}, []string{"codex/!a2"}, []string{"codex/a1", "codex/!a2"}},
|
|
{[]string{"codex/a2", "codex/a1"}, []string{"codex/!a2"}, []string{"codex/a1", "codex/!a2"}},
|
|
{[]string{"codex/!a3"}, []string{"codex/!a2"}, []string{"codex/!a3", "codex/!a2"}},
|
|
{[]string{"codex/!a3"}, []string{"codex/a1"}, []string{"codex/a1", "codex/!a3"}},
|
|
} {
|
|
got := foldAccounts(c.child, c.issuer)
|
|
if c.want == nil {
|
|
c.want = c.issuer
|
|
}
|
|
if !slices.Equal(got, c.want) {
|
|
t.Errorf("foldAccounts(%v, %v) = %v, want %v", c.child, c.issuer, got, c.want)
|
|
}
|
|
who := Identity{Accounts: got}
|
|
if who.AllowsAccount("codex", "a2") {
|
|
if slices.Contains(c.issuer, "codex/!a2") {
|
|
t.Errorf("%v: the issuer's kept-out account was let in", got)
|
|
}
|
|
}
|
|
}
|
|
issuer := Identity{Accounts: []string{"codex/!a2"}}
|
|
if issuer.allowsEntry("codex/a2") || !issuer.allowsEntry("codex/a9") || !issuer.allowsEntry("codex/!a3") {
|
|
t.Fatal("allowsEntry")
|
|
}
|
|
}
|
|
|
|
// A key value replaced in place (#1869) moves the keys keeping it out too.
|
|
func TestKeptOutReplaced(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Chat: "http://127.0.0.1:1/v1", Keys: []provider.KeyAccount{{Key: "sk-1"}, {Key: "sk-2"}}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Update("add-key", Change{Name: "Desk"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
keys, _ := List()
|
|
id := keys[len(keys)-1].ID
|
|
if _, err := Update("accounts-key", Change{Key: id, Accounts: []string{"relay/!" + provider.KeyID("sk-2")}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
n, err := ReplaceAccount("relay", provider.KeyID("sk-2"), provider.KeyID("sk-2b"))
|
|
if err != nil || n != 1 {
|
|
t.Fatalf("moved %d, %v", n, err)
|
|
}
|
|
keys, _ = List()
|
|
if got := keys[len(keys)-1].Accounts; !slices.Equal(got, []string{"relay/!" + provider.KeyID("sk-2b")}) {
|
|
t.Fatalf("accounts %v", got)
|
|
}
|
|
}
|