fix(backup): exclude balance tokens from keyless exports (#418)
This commit is contained in:
@@ -125,6 +125,7 @@ func Secret(name string) bool { return secretHeader.MatchString(name) }
|
||||
|
||||
func withoutKeys(p provider.Provider) provider.Provider {
|
||||
p.Key, p.KeyName, p.Keys, p.KeyProtocol = "", "", nil, ""
|
||||
p.BalanceToken = ""
|
||||
if len(p.Headers) > 0 {
|
||||
h := map[string]string{}
|
||||
for k, v := range p.Headers {
|
||||
|
||||
@@ -19,6 +19,7 @@ func home(t *testing.T) {
|
||||
t.Helper()
|
||||
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)
|
||||
@@ -36,9 +37,10 @@ func setUp(t *testing.T) {
|
||||
}
|
||||
for _, p := range []provider.Provider{
|
||||
{ID: "acme", Name: "Acme", Chat: "https://acme.example.com/v1", Key: "sk-acme", Icon: icon,
|
||||
Keys: []provider.KeyAccount{{Name: "second", Key: "sk-acme-2"}},
|
||||
Headers: map[string]string{"X-Team": "a", "X-Api-Key": "hdr-secret"}},
|
||||
{ID: "beta", Name: "Beta", Chat: "https://beta.example.com/v1", Key: "sk-beta"},
|
||||
BalanceToken: "balance-acme",
|
||||
Keys: []provider.KeyAccount{{Name: "second", Key: "sk-acme-2"}},
|
||||
Headers: map[string]string{"X-Team": "a", "X-Api-Key": "hdr-secret"}},
|
||||
{ID: "beta", Name: "Beta", Chat: "https://beta.example.com/v1", Key: "sk-beta", BalanceToken: "balance-beta"},
|
||||
} {
|
||||
if err := provider.Save(p); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -63,7 +65,7 @@ func TestRoundTrip(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, secret := range []string{"sk-acme", "hdr-secret", "acme.example.com", "work"} {
|
||||
for _, secret := range []string{"sk-acme", "balance-acme", "hdr-secret", "acme.example.com", "work"} {
|
||||
if strings.Contains(string(data), secret) {
|
||||
t.Fatalf("%q readable in the file", secret)
|
||||
}
|
||||
@@ -85,7 +87,7 @@ func TestRoundTrip(t *testing.T) {
|
||||
t.Fatalf("result: %+v", r)
|
||||
}
|
||||
p, err := provider.Find("acme")
|
||||
if err != nil || p.Key != "sk-acme" || len(p.Keys) != 1 || p.Headers["X-Api-Key"] != "hdr-secret" {
|
||||
if err != nil || p.Key != "sk-acme" || len(p.Keys) != 1 || p.BalanceToken != "balance-acme" || p.Headers["X-Api-Key"] != "hdr-secret" {
|
||||
t.Fatalf("acme: %+v %v", p, err)
|
||||
}
|
||||
name, _ := strings.CutPrefix(p.Icon, "file:")
|
||||
@@ -110,17 +112,23 @@ func TestNoKeys(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, p := range b.Providers {
|
||||
if p.Key != "" || len(p.Keys) != 0 {
|
||||
if p.Key != "" || len(p.Keys) != 0 || p.BalanceToken != "" {
|
||||
t.Fatalf("key in a keyless backup: %+v", p)
|
||||
}
|
||||
if _, ok := p.Headers["X-Api-Key"]; ok {
|
||||
t.Fatalf("auth header in a keyless backup: %+v", p.Headers)
|
||||
}
|
||||
}
|
||||
data, _ := Seal(b, "pw")
|
||||
if p, _ := provider.Find("acme"); p.Key != "sk-acme" || p.BalanceToken != "balance-acme" || p.Headers["X-Api-Key"] != "hdr-secret" {
|
||||
t.Fatalf("export changed the stored credentials: %+v", p)
|
||||
}
|
||||
data, err := Seal(b, "pw")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
home(t)
|
||||
if err := provider.Save(provider.Provider{ID: "acme", Name: "Acme old", Chat: "https://old.example.com/v1", Key: "sk-here"}); err != nil {
|
||||
if err := provider.Save(provider.Provider{ID: "acme", Name: "Acme old", Chat: "https://old.example.com/v1", Key: "sk-here", BalanceToken: "balance-here"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := Open(data, "pw")
|
||||
@@ -134,9 +142,12 @@ func TestNoKeys(t *testing.T) {
|
||||
if r.Added != 1 || r.Replaced != 1 || !slices.Equal(r.NeedKey, []string{"Beta"}) || r.Settings || r.Profiles != 0 {
|
||||
t.Fatalf("result: %+v", r)
|
||||
}
|
||||
if p, _ := provider.Find("acme"); p.Key != "sk-here" || p.Chat != "https://acme.example.com/v1" || p.Headers["X-Team"] != "a" {
|
||||
if p, _ := provider.Find("acme"); p.Key != "sk-here" || p.BalanceToken != "balance-here" || p.Chat != "https://acme.example.com/v1" || p.Headers["X-Team"] != "a" {
|
||||
t.Fatalf("acme: %+v", p)
|
||||
}
|
||||
if p, _ := provider.Find("beta"); p.BalanceToken != "" {
|
||||
t.Fatalf("balance token reached a new machine: %+v", p)
|
||||
}
|
||||
}
|
||||
|
||||
// The library goes too: its sets, servers and skills' files; without keys
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
package davsync
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/backup"
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// A computer sending no keys leaves the server's balance token in place,
|
||||
// just as it leaves its API keys. Sending keys replaces the token too.
|
||||
func TestTakeBalanceToken(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
serverKeys, incomingKeys bool
|
||||
token, want string
|
||||
}{
|
||||
{"keyless update", true, false, "", "balance-server"},
|
||||
{"explicit token", true, false, "balance-new", "balance-new"},
|
||||
{"with keys", true, true, "balance-new", "balance-new"},
|
||||
{"with keys without token", true, true, "", ""},
|
||||
{"keyless server", false, false, "", ""},
|
||||
{"adding keys", false, true, "balance-new", "balance-new"},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
server := provider.Provider{ID: "relay", Name: "Old", Chat: "https://old.example.com/v1"}
|
||||
if c.serverKeys {
|
||||
server.Key, server.BalanceToken = "sk-server", "balance-server"
|
||||
}
|
||||
incoming := provider.Provider{ID: "relay", Name: "New", Chat: "https://new.example.com/v1", BalanceToken: c.token}
|
||||
if c.incomingKeys {
|
||||
incoming.Key = "sk-new"
|
||||
}
|
||||
to := backup.Bundle{Keys: c.serverKeys, Providers: []provider.Provider{server}}
|
||||
from := backup.Bundle{Keys: c.incomingKeys, Providers: []provider.Provider{incoming}}
|
||||
take(&to, from, "providers")
|
||||
if len(to.Providers) != 1 {
|
||||
t.Fatalf("providers: %+v", to.Providers)
|
||||
}
|
||||
got := to.Providers[0]
|
||||
if got.BalanceToken != c.want || got.Name != incoming.Name || got.Chat != incoming.Chat {
|
||||
t.Fatalf("merged: %+v, want balance token %q and the incoming name and URL", got, c.want)
|
||||
}
|
||||
wantKey := incoming.Key
|
||||
if c.serverKeys && !c.incomingKeys {
|
||||
wantKey = server.Key
|
||||
}
|
||||
if got.Key != wantKey || to.Keys != (c.serverKeys || c.incomingKeys) {
|
||||
t.Fatalf("keys after merge: %+v", to)
|
||||
}
|
||||
if !reflect.DeepEqual(from.Providers, []provider.Provider{incoming}) {
|
||||
t.Fatalf("merge changed the incoming bundle: %+v", from.Providers)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Exercise the encrypted remote and two separate computers. Keyless
|
||||
// uploads keep each computer's token, and any token already on the server.
|
||||
func TestSyncBalanceToken(t *testing.T) {
|
||||
for _, serverKeys := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("serverKeys=%v", serverKeys), func(t *testing.T) {
|
||||
f, srv := newFakeS3(t)
|
||||
cfg := f.config(srv)
|
||||
cfg.Keys, cfg.Agents = serverKeys, false
|
||||
a, b := newComputer(t), newComputer(t)
|
||||
use := func(c computer) {
|
||||
c.use(t)
|
||||
t.Setenv("USERPROFILE", string(c))
|
||||
}
|
||||
now := func() {
|
||||
t.Helper()
|
||||
if err := Now(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
remote := func() backup.Bundle {
|
||||
t.Helper()
|
||||
got, err := backup.Open(f.objects["team x+y/magpie/magpie.magpie-backup"], cfg.Passphrase)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return got
|
||||
}
|
||||
use(a)
|
||||
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Chat: "https://relay.example.com/v1",
|
||||
Key: "sk-a", BalanceToken: "balance-a"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := Configure(cfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
now()
|
||||
wantKey, wantToken := "", ""
|
||||
if serverKeys {
|
||||
wantKey, wantToken = "sk-a", "balance-a"
|
||||
}
|
||||
if got := remote(); got.Keys != serverKeys || len(got.Providers) != 1 || got.Providers[0].Key != wantKey || got.Providers[0].BalanceToken != wantToken {
|
||||
t.Fatalf("first upload: %+v", got)
|
||||
}
|
||||
|
||||
use(b)
|
||||
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay b", Chat: "https://relay.example.com/v1",
|
||||
Key: "sk-b", BalanceToken: "balance-b"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
keyless := cfg
|
||||
keyless.Keys = false
|
||||
if err := Configure(keyless); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
now()
|
||||
localKey, localToken := "sk-b", "balance-b"
|
||||
if serverKeys {
|
||||
localKey, localToken = "sk-a", "balance-a"
|
||||
}
|
||||
ps := provider.Stored()
|
||||
if len(ps) != 1 || ps[0].Key != localKey || ps[0].BalanceToken != localToken {
|
||||
t.Fatalf("b after download: %+v", ps)
|
||||
}
|
||||
p := ps[0]
|
||||
p.Name, p.BalanceToken = "Renamed", "balance-b-new"
|
||||
if err := provider.Save(p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
now()
|
||||
if got := remote(); got.Keys != serverKeys || len(got.Providers) != 1 || got.Providers[0].Name != "Renamed" || got.Providers[0].Key != wantKey || got.Providers[0].BalanceToken != wantToken {
|
||||
t.Fatalf("after b's keyless upload: %+v", got)
|
||||
}
|
||||
if ps := provider.Stored(); len(ps) != 1 || ps[0].BalanceToken != "balance-b-new" {
|
||||
t.Fatalf("upload changed b's token: %+v", ps)
|
||||
}
|
||||
use(a)
|
||||
now()
|
||||
if ps := provider.Stored(); len(ps) != 1 || ps[0].Name != "Renamed" || ps[0].Key != "sk-a" || ps[0].BalanceToken != "balance-a" {
|
||||
t.Fatalf("a after download: %+v", ps)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -450,6 +450,9 @@ func take(to *backup.Bundle, from backup.Bundle, part string) {
|
||||
for i, p := range ps {
|
||||
if k, ok := keys[p.ID]; ok && p.Key == "" && len(p.Keys) == 0 {
|
||||
ps[i].Key, ps[i].KeyName, ps[i].Keys, ps[i].KeyProtocol = k.Key, k.KeyName, k.Keys, k.KeyProtocol
|
||||
if p.BalanceToken == "" {
|
||||
ps[i].BalanceToken = k.BalanceToken
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,9 @@ func Restore(ps []Provider) (added, replaced int, err error) {
|
||||
}
|
||||
if p.Key == "" && len(p.Keys) == 0 {
|
||||
p.Key, p.KeyName, p.Keys, p.KeyProtocol = f.Providers[i].Key, f.Providers[i].KeyName, f.Providers[i].Keys, f.Providers[i].KeyProtocol
|
||||
if p.BalanceToken == "" {
|
||||
p.BalanceToken = f.Providers[i].BalanceToken
|
||||
}
|
||||
}
|
||||
f.Providers[i] = p
|
||||
replaced++
|
||||
@@ -94,6 +97,9 @@ func Mirror(ps []Provider, gs []Group) error {
|
||||
}
|
||||
if h, ok := here[p.ID]; ok && p.Key == "" && len(p.Keys) == 0 {
|
||||
p.Key, p.KeyName, p.Keys, p.KeyProtocol = h.Key, h.KeyName, h.Keys, h.KeyProtocol
|
||||
if p.BalanceToken == "" {
|
||||
p.BalanceToken = h.BalanceToken
|
||||
}
|
||||
}
|
||||
out = append(out, p)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Restoring and syncing a keyless provider keep this machine's balance
|
||||
// token. A token supplied explicitly, or keys supplied with none, replaces it.
|
||||
func TestBackupBalanceToken(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
|
||||
want string
|
||||
}{
|
||||
{"without keys", Provider{}, "balance-here"},
|
||||
{"explicit token without keys", Provider{BalanceToken: "balance-new"}, "balance-new"},
|
||||
{"primary key with token", Provider{Key: "sk-new", BalanceToken: "balance-new"}, "balance-new"},
|
||||
{"primary key without token", Provider{Key: "sk-new"}, ""},
|
||||
{"spare key with token", Provider{Keys: []KeyAccount{{Key: "sk-new"}}, BalanceToken: "balance-new"}, "balance-new"},
|
||||
{"spare key without token", Provider{Keys: []KeyAccount{{Key: "sk-new"}}}, ""},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
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)
|
||||
here := Provider{ID: "relay", Name: "Relay old", Chat: "https://old.example.com/v1",
|
||||
Key: "sk-here", KeyName: "main", KeyProtocol: Chat,
|
||||
Keys: []KeyAccount{{Name: "spare", Key: "sk-spare", Protocol: Anthropic}}, BalanceToken: "balance-here"}
|
||||
if err := Save(here); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p := c.incoming
|
||||
p.ID, p.Name, p.Chat = "relay", "Relay new", "https://new.example.com/v1"
|
||||
if err := op.put([]Provider{p}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ps := Stored()
|
||||
if len(ps) != 1 {
|
||||
t.Fatalf("providers: %+v", ps)
|
||||
}
|
||||
got := ps[0]
|
||||
if got.BalanceToken != c.want || got.Name != p.Name || got.Chat != p.Chat {
|
||||
t.Fatalf("restored: %+v, want balance token %q and the incoming name and URL", got, c.want)
|
||||
}
|
||||
wantKeys := p
|
||||
if p.Key == "" && len(p.Keys) == 0 {
|
||||
wantKeys = here
|
||||
}
|
||||
if got.Key != wantKeys.Key || got.KeyName != wantKeys.KeyName || got.KeyProtocol != wantKeys.KeyProtocol || !slices.Equal(got.Keys, wantKeys.Keys) {
|
||||
t.Fatalf("keys: %+v, want %+v", got, wantKeys)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user