Files
magpie/internal/provider/keep_secrets.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

54 lines
1.9 KiB
Go

package provider
import (
"maps"
"regexp"
)
var secretHeader = regexp.MustCompile(`(?i)auth|key|token|secret|cookie|session|password`)
// SecretHeader reports whether a header or an environment variable by that
// name looks like it holds a key: what a backup without keys leaves empty.
func SecretHeader(name string) bool { return secretHeader.MatchString(name) }
// KeepSecrets is in, as sync or a restore brings it over here, with the
// secrets that didn't come along kept from here (#1821). keyless is that
// in came from a copy without keys: its keys and its secret headers are
// here's then. An AccessKey ID and Secret, or a balance token, that in
// hasn't at all are kept when it came without keys or with the key here
// has, the same account: a magpie from before access keys were synced
// (v0.1.1142), or one the key was never given on, sends a provider with
// its key and nothing else, and taking that whole wiped the Volcengine
// and Tencent plan cards' access keys on this computer. A key of its own
// is another account's, whose access key this one's isn't.
func KeepSecrets(in, here Provider, keyless bool) Provider {
if keyless {
in.Key, in.KeyName, in.Keys, in.KeyProtocol, in.KeyWeight = here.Key, here.KeyName, here.Keys, here.KeyProtocol, here.KeyWeight
// the headers a copy without keys left out, in a map of its own:
// in's may be shared with the list it came in
cloned := false
for k, v := range here.Headers {
if _, ok := in.Headers[k]; ok || !SecretHeader(k) {
continue
}
if !cloned {
in.Headers, cloned = maps.Clone(in.Headers), true
if in.Headers == nil {
in.Headers = map[string]string{}
}
}
in.Headers[k] = v
}
}
if !keyless && (in.Key == "" || in.Key != here.Key) {
return in
}
if in.BalanceToken == "" {
in.BalanceToken = here.BalanceToken
}
if in.AccessKeyID == "" && in.SecretAccessKey == "" {
in.AccessKeyID, in.SecretAccessKey = here.AccessKeyID, here.SecretAccessKey
}
return in
}