The reporter's Volcengine Agent Plan card vanished: providers.json had
lost accessKeyID/secretAccessKey, and volcPlanQuotas skipped a provider
without them before building any card, so nothing said why.
Writers that dropped the access key, fixed at the cause:
- Sync and restore (Mirror, Restore, davsync take) kept this machine's
access key and balance token only when the incoming provider had no
key at all. A magpie before v0.1.1142 (aa82288f), or one the access
key was never given on, sends the same key with no access key, and
that replaced it here and on the server. KeepSecrets now keeps them
when the incoming has the same key and none of its own; another key is
another account and still brings its own (TestBackupBalanceToken
unchanged). A keyless copy also keeps this machine's secret headers,
which withoutKeys strips and nothing put back. The secret-name regex
moves to provider.SecretHeader; backup.Secret delegates to it.
- The GUI save kept the access key ID only when the field was left out;
the editor always sends it, so an editor opened before the key was
given saved "" over it (the Secret was kept). Blank now keeps it; only
clearAccessKey drops it.
- AddCopy skipped the Secret when the copy's form sent the source's ID.
- read() and validProviders didn't strip a UTF-8 BOM (lastgood.JSON
does): a BOM'd providers.json fell back to its .bak, an older
generation.
Import's replace mode replaces providers on purpose and is unchanged.
The Usage page: a plan endpoint (Volcengine Coding/Agent Plan, Tencent
Token Plan Enterprise) whose providers have no access key is a card
with no windows, an error naming AccessKey ID and Secret (SecretId and
SecretKey for Tencent), needsAccessKey, and an "Open settings" button to
the provider's editor; one card per plan, none when another provider on
the plan has the key. Strings in en, zh, zh-TW, ja, de.
Run:
- go test ./internal/provider ./internal/davsync ./internal/backup
./internal/gui -count=1, twice, green (private GOCACHE, sandbox HOME).
- Broke each by hand and saw red: KeepSecrets same-key keep
(TestSyncKeepsAccessKey, TestTakeKeepsAccessKey), header keep, BOM
strip (TestProvidersFileWithBOM), GUI blank-ID keep
(TestProviderSaveKeepsVolcengineSecret), AddCopy
(TestAddCopyKeepsAccessKeySecret), the Volcengine missing card
(TestPlanCardWithoutAccessKey).
- GUI: usage-access-key-card 20/20 (chromium + webkit, 5 languages, 900
and 420px; red with the button removed and with the ja string
changed); i18n-keys, volc-access-key, tencent-access-key,
tencent-cloud, quota-balance-error, usage-card-refresh,
usage-remote-cards green.
- gofmt -l clean; go vet darwin, linux -tags nogui, windows clean for
provider, davsync, backup, gui, cli.
Not verified: which writer dropped the reporter's key is inferred from
their 10-10 to 10-11 snapshots, not reproduced on their machine (most
likely sync from another computer). Not tried against real Volcengine or
Tencent accounts. The tray panel shows the error text with no button.
226 lines
8.6 KiB
Go
226 lines
8.6 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/agentenv"
|
|
)
|
|
|
|
func keepSecretsHome(t *testing.T) {
|
|
t.Helper()
|
|
isolate(t)
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
t.Setenv("PATH", h)
|
|
for _, v := range agentenv.Vars {
|
|
t.Setenv(v, "")
|
|
}
|
|
}
|
|
|
|
// #1821: a Volcengine provider's AccessKey ID and Secret went missing from
|
|
// providers.json. Sync and a restore bring the provider whole from another
|
|
// computer; one that knows no access key (a magpie before v0.1.1142, or
|
|
// one it was never given on) sends the same key and nothing else, and
|
|
// that wiped it here. Kept now while the key is the one here, as a copy
|
|
// without keys keeps its secret headers too; another key is another
|
|
// account, whose access key it isn't.
|
|
func TestSyncKeepsAccessKey(t *testing.T) {
|
|
for _, op := range []struct {
|
|
name string
|
|
put func([]Provider) error
|
|
}{
|
|
{"restore", func(ps []Provider) error { _, _, err := Restore(ps); return err }},
|
|
{"mirror", func(ps []Provider) error { return Mirror(ps, nil) }},
|
|
} {
|
|
t.Run(op.name, func(t *testing.T) {
|
|
for _, c := range []struct {
|
|
name string
|
|
incoming Provider
|
|
wantAK, wantTok string
|
|
wantKey, wantHdr string
|
|
}{
|
|
{"same key, no access key", Provider{Key: "ark-here"}, "AK-here", "tok-here", "ark-here", ""},
|
|
{"same key, its own access key", Provider{Key: "ark-here", AccessKeyID: "AK-new", SecretAccessKey: "SK-new"}, "AK-new", "tok-here", "ark-here", ""},
|
|
{"without keys", Provider{}, "AK-here", "tok-here", "ark-here", "hdr-here"},
|
|
{"another key", Provider{Key: "ark-other"}, "", "", "ark-other", ""},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
keepSecretsHome(t)
|
|
here := Provider{ID: "volcengine", Name: "Volcengine Ark", Chat: "https://ark.cn-beijing.volces.com/api/coding/v3",
|
|
Key: "ark-here", BalanceToken: "tok-here", AccessKeyID: "AK-here", SecretAccessKey: "SK-here",
|
|
Headers: map[string]string{"X-Api-Token": "hdr-here", "X-Plain": "plain-here"}}
|
|
if err := Save(here); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p := c.incoming
|
|
p.ID, p.Name, p.Chat = "volcengine", "Ark synced", here.Chat
|
|
p.Headers = map[string]string{"X-Plain": "plain-new"}
|
|
list := []Provider{p}
|
|
if err := op.put(list); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(list[0].Headers) != 1 {
|
|
t.Fatalf("the caller's headers were written into: %v", list[0].Headers)
|
|
}
|
|
ps, _ := Stored()
|
|
if len(ps) != 1 {
|
|
t.Fatalf("providers: %+v", ps)
|
|
}
|
|
got := ps[0]
|
|
wantSK := strings.Replace(c.wantAK, "AK", "SK", 1)
|
|
if got.AccessKeyID != c.wantAK || got.SecretAccessKey != wantSK {
|
|
t.Errorf("access key %q %q, want %q %q", got.AccessKeyID, got.SecretAccessKey, c.wantAK, wantSK)
|
|
}
|
|
if got.BalanceToken != c.wantTok || got.Key != c.wantKey || got.Name != "Ark synced" {
|
|
t.Errorf("got %+v, want token %q key %q", got, c.wantTok, c.wantKey)
|
|
}
|
|
if got.Headers["X-Api-Token"] != c.wantHdr || got.Headers["X-Plain"] != "plain-new" {
|
|
t.Errorf("headers %v, want the secret one %q and the incoming plain one", got.Headers, c.wantHdr)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A providers.json an editor saved with a byte-order mark is read as it
|
|
// is, not taken for a broken file whose .bak (an older generation without
|
|
// the access key given since) stands in for it (#1821).
|
|
func TestProvidersFileWithBOM(t *testing.T) {
|
|
keepSecretsHome(t)
|
|
if err := Save(Provider{ID: "volcengine", Name: "Volcengine Ark", Chat: "https://ark.cn-beijing.volces.com/api/coding/v3", Key: "ark-k"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// a second save leaves the first as the .bak
|
|
if err := Save(Provider{ID: "volcengine", Name: "Volcengine Ark", Chat: "https://ark.cn-beijing.volces.com/api/coding/v3", Key: "ark-k", AccessKeyID: "AK1", SecretAccessKey: "SK1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, err := os.ReadFile(Path())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(Path(), append([]byte("\xef\xbb\xbf"), b...), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ps, err := Stored()
|
|
if err != nil || len(ps) != 1 || ps[0].AccessKeyID != "AK1" || ps[0].SecretAccessKey != "SK1" {
|
|
t.Fatalf("read %+v %v, want the file's access key", ps, err)
|
|
}
|
|
// and a write after it keeps it
|
|
if err := Save(Provider{ID: "other", Name: "Other", Chat: "https://other.example/v1", Key: "sk-o"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p, err := Find("volcengine"); err != nil || p.AccessKeyID != "AK1" || p.SecretAccessKey != "SK1" {
|
|
t.Fatalf("after a save: %+v %v", p, err)
|
|
}
|
|
}
|
|
|
|
// A copy made in the editor sends the access key's ID as the form shows
|
|
// it, never the saved Secret: the copy keeps both, as with a blank ID
|
|
// (#1821). An ID of its own is its own, Secret and all.
|
|
func TestAddCopyKeepsAccessKeySecret(t *testing.T) {
|
|
keepSecretsHome(t)
|
|
src := Provider{ID: "volcengine", Name: "Volcengine Ark", Chat: "https://ark.cn-beijing.volces.com/api/coding/v3", Key: "ark-k", AccessKeyID: "AK1", SecretAccessKey: "SK1"}
|
|
if err := Save(src); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
id, err := AddCopy(Provider{Name: "Ark copy", Chat: src.Chat, AccessKeyID: "AK1"}, "volcengine")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p, _ := Find(id); p.AccessKeyID != "AK1" || p.SecretAccessKey != "SK1" {
|
|
t.Fatalf("copy: %q %q", p.AccessKeyID, p.SecretAccessKey)
|
|
}
|
|
id, err = AddCopy(Provider{Name: "Ark copy 2", Chat: src.Chat, AccessKeyID: "AK2", SecretAccessKey: "SK2"}, "volcengine")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p, _ := Find(id); p.AccessKeyID != "AK2" || p.SecretAccessKey != "SK2" {
|
|
t.Fatalf("copy with its own: %q %q", p.AccessKeyID, p.SecretAccessKey)
|
|
}
|
|
}
|
|
|
|
// #1821: a Volcengine or Tencent plan whose provider has no access key
|
|
// was no card at all, so a key gone from providers.json looked like a
|
|
// card gone for no reason. It is a card that says what to give, once a
|
|
// plan, and none when another provider on the plan has the key.
|
|
func TestPlanCardWithoutAccessKey(t *testing.T) {
|
|
keepSecretsHome(t)
|
|
var keyedIn atomic.Bool
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if !keyedIn.Load() {
|
|
t.Errorf("asked %s %s with no access key", r.Header.Get("X-Host"), r.URL)
|
|
}
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
}))
|
|
defer srv.Close()
|
|
old := http.DefaultClient.Transport
|
|
http.DefaultClient.Transport = rewrite{srv}
|
|
t.Cleanup(func() { http.DefaultClient.Transport = old })
|
|
forgetPlanQuotas()
|
|
t.Cleanup(forgetPlanQuotas)
|
|
for _, p := range []Provider{
|
|
{ID: "volcengine", Name: "Volcengine Ark", Chat: "https://ark.cn-beijing.volces.com/api/coding/v3", Key: "ark-k"},
|
|
// the same plan again: one card
|
|
{ID: "volc-copy", Name: "Ark copy", Anthropic: "https://ark.cn-beijing.volces.com/api/coding", Key: "ark-k", AccessKeyID: "AK-half"},
|
|
// pay-as-you-go reads nothing with an access key
|
|
{ID: "volc-payg", Name: "Ark API", Chat: "https://ark.cn-beijing.volces.com/api/v3", Key: "ark-k"},
|
|
{ID: "volc-off", Name: "Ark off", Chat: "https://ark.cn-beijing.volces.com/api/plan/v3", Key: "ark-k", Off: true},
|
|
{ID: "tencent-cloud", Name: "Tencent Cloud", Chat: "https://tokenhub.tencentmaas.com/plan/v3", Key: "sk-tp-1"},
|
|
} {
|
|
if err := Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
got := map[string]SubscriptionQuota{}
|
|
for _, q := range PlanQuotas(context.Background()) {
|
|
if q.NeedsAccessKey {
|
|
if _, dup := got[q.Provider]; dup {
|
|
t.Errorf("two cards for %s", q.Provider)
|
|
}
|
|
got[q.Provider] = q
|
|
}
|
|
}
|
|
volc, okV := got["volcengine"]
|
|
if !okV {
|
|
volc, okV = got["volc-copy"]
|
|
}
|
|
tc, okT := got["tencent-cloud"]
|
|
if len(got) != 2 || !okV || !okT {
|
|
t.Fatalf("cards: %+v", got)
|
|
}
|
|
if !strings.Contains(volc.Error, "AccessKey ID and Secret") || volc.Plan == "" || volc.Windows == nil {
|
|
t.Errorf("volcengine: %+v", volc)
|
|
}
|
|
if !strings.Contains(tc.Error, "SecretId and SecretKey") || tc.Plan != tcPlanName {
|
|
t.Errorf("tencent: %+v", tc)
|
|
}
|
|
|
|
// another provider on the plan with the key: its card, not this one
|
|
keyedIn.Store(true)
|
|
if err := Save(Provider{ID: "volc-keyed", Name: "Ark keyed", Chat: "https://ark.cn-beijing.volces.com/api/coding/v3", Key: "ark-k2", AccessKeyID: "AK1", SecretAccessKey: "SK1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
forgetPlanQuotas()
|
|
var cards []string
|
|
for _, q := range volcPlanQuotas(context.Background()) {
|
|
cards = append(cards, q.Provider)
|
|
if q.NeedsAccessKey {
|
|
t.Errorf("a keyless card beside the keyed one: %+v", q)
|
|
}
|
|
}
|
|
if len(cards) != 1 || cards[0] != "volc-keyed" {
|
|
t.Errorf("cards: %v", cards)
|
|
}
|
|
}
|