Files
magpie/internal/provider/backup.go
T
yetone 8792ee063d provider: a Volcengine or Tencent access key is kept through sync, restore, a stale editor and a copy, and a plan card without one says what to give (#1821, Edward4226)
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.
2026-10-12 23:45:06 +08:00

111 lines
2.9 KiB
Go

package provider
// A backup carries providers.json's entries as they are stored, and puts
// them back on another machine (see internal/backup).
import "slices"
// Stored is the providers as providers.json keeps them: keys included,
// signed-in accounts only as the model picks the user made for them. A
// file that can't be read is an error, never none: a backup or a sync
// carrying none would take every provider away where it is put back.
func Stored() ([]Provider, error) {
f, err := read()
return f.Providers, err
}
// Restore puts providers from a backup in. Each replaces the one here with
// its id; one that came without keys keeps the keys already here, and
// one without an access key or balance token keeps those (KeepSecrets). It
// returns how many were new and how many replaced one here.
func Restore(ps []Provider) (added, replaced int, err error) {
f, err := read()
if err != nil {
return 0, 0, err
}
for _, p := range ps {
p.IconURL = ""
if p.ID == "" || p.ID != Slug(p.ID) || p.ID == "magpie" {
continue
}
i := -1
for j := range f.Providers {
if f.Providers[j].ID == p.ID {
i = j
break
}
}
if i < 0 {
f.Providers = append(f.Providers, p)
added++
continue
}
f.Providers[i] = KeepSecrets(p, f.Providers[i], p.Key == "" && len(p.Keys) == 0)
replaced++
}
if added+replaced == 0 {
return 0, 0, nil
}
return added, replaced, store(f)
}
// StoredGroups is the groups as saved: the user's own, and the found ones
// the user removed. As with Stored, a file that can't be read is an error.
func StoredGroups() ([]Group, error) {
f, err := read()
return f.Groups, err
}
// RestoreGroups puts groups from a backup in, each replacing the one here
// with its id.
func RestoreGroups(gs []Group) error {
if len(gs) == 0 {
return nil
}
f, err := read()
if err != nil {
return err
}
for _, g := range gs {
if g.ID == "" || g.ID != GroupSlug(g.ID) {
continue
}
g.Auto = false
if i := slices.IndexFunc(f.Groups, func(x Group) bool { return x.ID == g.ID }); i >= 0 {
f.Groups[i] = g
} else {
f.Groups = append(f.Groups, g)
}
}
return store(f)
}
// Mirror makes the providers and groups exactly these, as sync brings them
// from another computer: one not among them goes, one that came without
// keys keeps the keys it has here, and an access key or balance token it
// came without is kept too (KeepSecrets).
func Mirror(ps []Provider, gs []Group) error {
f, err := read()
if err != nil {
return err
}
here := map[string]Provider{}
for _, p := range f.Providers {
here[p.ID] = p
}
out := []Provider{}
for _, p := range ps {
p.IconURL = ""
if p.ID == "" || p.ID != Slug(p.ID) || p.ID == "magpie" {
continue
}
if h, ok := here[p.ID]; ok {
p = KeepSecrets(p, h, p.Key == "" && len(p.Keys) == 0)
}
out = append(out, p)
}
f.Providers = out
f.Groups = slices.DeleteFunc(slices.Clone(gs), func(g Group) bool { return g.ID == "" || g.ID != GroupSlug(g.ID) })
return store(f)
}