* davsync: scope provider and library credentials during partial sync * davsync: a sync writes which part carries its credentials, and no blank clears one yetone's review of the first cut found two losses the per-part markers caused, and both are fixed by making the markers say exactly which part they belong to: 1. A keyed computer's library-only upload cleared a keyless computer's private MCP token. The blanks in the uploader's library are redactions it downloaded and never held a value for, not clears it made; take now keeps the server's value for an empty secret whatever the policy is, so only a server taken out here is gone. The test that had the loss as its expected now expects the token kept. 2. Mixed versions dropped the remote's provider keys. A keyed upload didn't write the whole-bundle Keys bit, and an older keyless magpie, which reads no per-part marker, uploaded its own providers over the server's with nothing to keep. take sets Keys for the providers' again. The per-part markers are three-state now: a whole collect and a magpie before them write none, and the whole-bundle bit says it then; a sync writes one for every part it merges — true when the upload carried that part's credentials, false when it didn't — and that bit, not Keys, is what a reader goes by. That is what keeps 1 and 2 apart: the providers' whole-bundle bit is set for older readers, while the settings' and the library's own false is what tells a new reader they came without keys and their local values stay. take marks its merged bundle as a sync one (BundleVersion), so a sync's readers never fall back to Keys for a part it wrote. The two regression tests of the first cut that encoded the losses as expected are updated: library/clear keeps the server's secret, and the keyed providers upload writes the whole-bundle bit while leaving the other parts' contents alone. A remote that main already flipped to Keys=true over redacted settings or library isn't repaired by this: the fix keeps new syncs from flipping it, and a keyed computer syncing those parts repairs them by writing them whole. Validation on Windows: go test ./internal/... under a temp HOME passes whole; the davsync, backup, access and settings packages pass with -race; stashing the change fails TestTakeScopedPartCredentials (8 cases), the scoped-sync tests that cover both losses, and the balance-token and restore-scope tests that read the markers. go vet ./internal/... and the darwin, GOOS=linux and GOOS=windows builds pass. * davsync: preserve local secrets when applying partial uploads
131 lines
4.9 KiB
Go
131 lines
4.9 KiB
Go
package backup
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/appdir"
|
|
"github.com/yetone/magpie/internal/library"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
func TestRestoreLibraryKeysScope(t *testing.T) {
|
|
for _, policy := range []string{"keyless", "full", "settings", "providers", "library"} {
|
|
for _, selected := range []bool{false, true} {
|
|
name := policy + "/excluded"
|
|
if selected {
|
|
name = policy + "/selected"
|
|
}
|
|
t.Run(name, func(t *testing.T) {
|
|
home(t)
|
|
appdir.UseExecutable("")
|
|
cur := settings.Load()
|
|
cur.GitHubToken = "fixture-local-github"
|
|
if err := settings.Save(cur); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := library.SaveServer("", library.Server{Name: "github", Transport: "stdio", Command: "fixture-mcp",
|
|
Env: map[string]string{"GITHUB_TOKEN": "fixture-local-mcp"}, Agents: []string{}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := library.SaveServer("", library.Server{Name: "headers", Transport: "http", URL: "https://mcp.example.com",
|
|
Headers: map[string]string{"Authorization": "fixture-local-header"}, Agents: []string{}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b := Bundle{Version: BundleVersion, Keys: policy == "full", Library: &library.Bundle{MCP: []*library.Server{{Name: "github", Transport: "stdio", Command: "changed-mcp",
|
|
Env: map[string]string{"GITHUB_TOKEN": ""}, Agents: []string{}}, {Name: "headers", Transport: "http", URL: "https://mcp.example.com",
|
|
Headers: map[string]string{"Authorization": ""}, Agents: []string{}}}}}
|
|
if policy != "keyless" && policy != "full" {
|
|
// Read a serialized scoped bundle, as an imported backup would.
|
|
if err := json.Unmarshal([]byte(`{"`+policy+`Keys":true}`), &b); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if _, err := Restore(b, Parts{Library: selected}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := library.Collect()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wantToken, wantHeader, wantCommand := "fixture-local-mcp", "fixture-local-header", "fixture-mcp"
|
|
if selected {
|
|
wantCommand = "changed-mcp"
|
|
}
|
|
if got.MCP[0].Command != wantCommand || got.MCP[0].Env["GITHUB_TOKEN"] != wantToken || got.MCP[1].Headers["Authorization"] != wantHeader {
|
|
t.Errorf("library restoration ignored its selected credential scope: command=%q, token=%q, header=%q", got.MCP[0].Command, got.MCP[0].Env["GITHUB_TOKEN"], got.MCP[1].Headers["Authorization"])
|
|
}
|
|
if settings.Load().GitHubToken != cur.GitHubToken {
|
|
t.Error("restoring the library changed an unselected settings credential")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRestoreLibraryCredentialUpdates(t *testing.T) {
|
|
for _, scoped := range []bool{false, true} {
|
|
for _, action := range []string{"replace", "remove-keys", "remove-server"} {
|
|
policy := "full"
|
|
if scoped {
|
|
policy = "library"
|
|
}
|
|
t.Run(policy+"/"+action, func(t *testing.T) {
|
|
home(t)
|
|
appdir.UseExecutable("")
|
|
server := library.Server{Name: "github", Transport: "stdio", Command: "fixture-mcp", Agents: []string{},
|
|
Env: map[string]string{"GITHUB_TOKEN": "fixture-local", "MODE": "local"}}
|
|
if _, err := library.SaveServer("", server); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
headers := library.Server{Name: "headers", Transport: "http", URL: "https://mcp.example.com", Agents: []string{},
|
|
Headers: map[string]string{"Authorization": "fixture-local-header", "X-Mode": "local"}}
|
|
if _, err := library.SaveServer("", headers); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
server.Env = map[string]string{"GITHUB_TOKEN": "fixture-new", "MODE": ""}
|
|
headers.Headers = map[string]string{"Authorization": "fixture-new-header", "X-Mode": ""}
|
|
b := Bundle{Version: BundleVersion, Keys: !scoped, Library: &library.Bundle{MCP: []*library.Server{&server, &headers}}}
|
|
if scoped {
|
|
b.LibraryKeys = Flag(true)
|
|
}
|
|
switch action {
|
|
case "remove-keys":
|
|
delete(server.Env, "GITHUB_TOKEN")
|
|
delete(headers.Headers, "Authorization")
|
|
case "remove-server":
|
|
b.Library.MCP = nil
|
|
}
|
|
if _, err := Restore(b, Parts{Library: true}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := library.Collect()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if action == "remove-server" {
|
|
if len(got.MCP) != 0 {
|
|
t.Fatal("restoring credentials resurrected a removed server")
|
|
}
|
|
return
|
|
}
|
|
if len(got.MCP) != 2 || got.MCP[0].Env["MODE"] != "" || got.MCP[1].Headers["X-Mode"] != "" {
|
|
t.Fatal("a non-secret blank was replaced with a local value")
|
|
}
|
|
if action == "replace" {
|
|
if got.MCP[0].Env["GITHUB_TOKEN"] != "fixture-new" || got.MCP[1].Headers["Authorization"] != "fixture-new-header" {
|
|
t.Fatal("a nonempty incoming credential did not replace the local value")
|
|
}
|
|
} else {
|
|
if _, ok := got.MCP[0].Env["GITHUB_TOKEN"]; ok {
|
|
t.Error("a removed environment key came back")
|
|
}
|
|
if _, ok := got.MCP[1].Headers["Authorization"]; ok {
|
|
t.Error("a removed header came back")
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|