* fix(embeddeddolt): use stored peer credentials for federation sync bd federation add-peer promises stored credentials are used automatically when syncing, but the embedded Fetch/PushTo/PullFrom paths read only the DOLT_REMOTE_USER environment variable, so first sync against any authenticated Dolt server presented the wrong identity. This is the peer-path remainder of the class fixed for default remotes in #2543. withPeerAuth mirrors server mode's withPeerCredentials: peer-store lookup first, environment fallback for unknown remotes and credential-free peers (federation sync iterates every dolt remote, so non-peer remotes must keep their behavior). Stored credentials override the process-wide DOLT_REMOTE_USER/DOLT_REMOTE_PASSWORD pair as a unit under a federationEnvMutex, mirroring package dolt: the in-process Dolt engine reads them from the process environment, so the override is serialized and both variables are restored (or re-unset) afterward, including on callback failure. A stored empty field unsets its variable rather than inheriting an ambient value. Fixes #5080 Agent-Signature: claude-code-claude-fable-5-unknown-reasoning on behalf of Michael Pursifull * docs(embeddeddolt): clarify federationEnvMutex is separate from package dolt's lock Review nit on #5085: the 'mirroring' phrasing read as if the two packages shared one mutex. State explicitly that they are separate locks and why per-package serialization suffices. * fix(federation): actionable error when peer credentials cannot be decrypted A database pulled to a second machine carries federation_peers rows its local .beads-credential-key cannot decrypt, and every peer operation died with a raw cipher error while bd federation status blamed the network (Unreachable:). The failure semantics were right (fail closed, matching server mode); the message said nothing about the machine-local key or the fix. Add storage.ErrCredentialKeyMismatch plus a shared wording helper CredentialKeyMismatchError naming the key file as context and the remediation (re-run bd federation add-peer on this machine). Both backends wrap their decrypt failures with it: embeddeddolt at its single decrypt funnel, dolt at its two read paths. GetFederationPeer wraps now name the peer. bd federation status classifies the sentinel with errors.Is and prints Local credential error instead of Unreachable; the --json shape is unchanged and now pinned by test. When the error fires is unchanged: same call sites, same fail-closed semantics, no fallback to ambient credentials. Tests pin the decrypt-failure branch in both backends (embeddeddolt against a real store with a rotated key, dolt via sqlmock) and the status classification end to end in the gated CLI harness. Refs #5080 * fix(embeddeddolt): classify short ciphertext as credential-key mismatch A stored peer password blob shorter than the GCM nonce returned a bare 'ciphertext too short' error, bypassing CredentialKeyMismatchError, so federation status read that local corruption as an unreachable peer. Route the branch through the same sentinel as a failed GCM open; the remediation (re-add the peer on this machine) is identical. Adds a short-ciphertext regression at the decrypt funnel. Refs: gastownhall/beads#5214 (review, gap 1) * fix(embeddeddolt): bind stored credentials to the remote URL; serialize env fallbacks Two fixes to the shared withPeerAuth seam from the consolidation review: - Stored peer credentials are now bound to the peer's canonical remote URL, not only to a matching remote name. AddRemoteIfNotExists keeps an existing same-name remote regardless of its URL, so a redirected remote could receive the stored password. withPeerAuth now verifies the live dolt_remotes URL against the peer row before any credential is installed, failing closed on divergence and on a missing remote. - The plain-remote and empty-credential fallback callbacks now run under federationEnvMutex too. They previously called remoteAuthUser() and the operation itself outside the lock, so a fallback operation could observe a concurrent stored-peer operation's temporarily installed DOLT_REMOTE_USER/DOLT_REMOTE_PASSWORD pair. Adds three regressions: diverged-URL fail-closed, missing-remote fail-closed, and env-fallback-does-not-observe-peer-credentials. Refs: gastownhall/beads#5085 (review) * style(dolt): use US spelling in the decrypt-funnel comments The repo's misspell linter rejects 'Labelling' and 'labelled'. Comment text only; no behavior change. Refs: gastownhall/beads#5214 (CI) * review: name the resolved credential-key path in the mismatch error Maintainer review fixup for PR #5214 (adopt-pr iteration 1). Addresses the one minor and the one nit from the review synthesis; both reviewers found 0 blockers and 0 majors and the quality scorecard is approve (898/850). Minor (Debuggability & Operability). All three CredentialKeyMismatchError call sites passed the bare const credentialKeyFile, so the message claimed "the key file .beads-credential-key is machine-local" without saying where it is. On the dolt backend that basename is genuinely ambiguous: initCredentialKey resolves it against s.beadsDir and falls back to the legacy s.dbPath as a transparent migration, so during the migration window two files with that name can exist carrying different keys. Pass filepath.Join(s.beadsDir, credentialKeyFile) instead, at dolt/credentials.go and both embeddeddolt/federation.go sites. No signature change: the parameter was already named keyFile and its doc comment already describes it as machine-local. beadsDir is provably non-empty at all three sites in production, verified in source rather than assumed: dolt only reaches the funnel with a non-nil key, and initCredentialKey leaves the key nil when beadsDir is empty; embeddeddolt's ensureCredentialKey errors out when it is empty. This lengthens a message the author pre-declared as a judgment call. The synthesis adjudicated that carve-out deliberately: it covers the message's wording and its single-line form, not which file the text identifies. Nit (Test Evidence Quality). dolt's withPeerCredentials is the seam that carries the sentinel into `bd federation status` on the default backend and had no fails-closed pin, unlike its embedded twin TestWithPeerAuth_KeyMismatchFailsClosed. Added one subtest on the existing sqlmock harness asserting the callback does not run and the sentinel survives the seam's own wrap. Both pins were proven non-vacuous by mutation, not assumed: - Reverting the three call sites to the bare const reds all three dolt subtests and all three embeddeddolt tests, with the original bare-basename text in the diff output. - Making withPeerCredentials swallow the decrypt error reds only the new subtest, on exactly the two properties the nit named (callback ran; no failure). The dolt test needed beadsDir set on its hand-built store: filepath.Join("", credentialKeyFile) is the bare basename, so with beadsDir left empty the resolved-path assertion would have passed vacuously. The embeddeddolt pin's package-level const became a function of the store for the same reason — its store has a real temp beadsDir, so the clause now pins the resolved path. Validation: go vet rc=0 on all three packages; internal/storage, dolt (3 tests incl. both negative/narrowing tests) and embeddeddolt (3 credential tests) green; cmd/bd TestEmbeddedFederation 12/12 green including the PR's own status_credential_key_mismatch e2e subtest. make bazel-sync-check rc=0 with zero tracked drift — no new files and zero import changes, so no BUILD srcs or deps can move. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> --------- Co-authored-by: Michael Pursifull <mike@arcaven.com> Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local> Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: Eddie the Engineer <adopt-pr@gascity.com>
337 lines
11 KiB
Go
337 lines
11 KiB
Go
//go:build cgo
|
|
|
|
package main
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/steveyegge/beads/internal/storage"
|
|
)
|
|
|
|
// bdFederation runs "bd federation" with extra args. Returns combined output.
|
|
func bdFederation(t *testing.T, bd, dir string, args ...string) string {
|
|
t.Helper()
|
|
fullArgs := append([]string{"federation"}, args...)
|
|
cmd := exec.Command(bd, fullArgs...)
|
|
cmd.Dir = dir
|
|
cmd.Env = bdEnv(dir)
|
|
stdout, stderr, err := runCommandBuffers(t, cmd)
|
|
if err != nil {
|
|
t.Fatalf("bd federation %s failed: %v\nstdout:\n%s\nstderr:\n%s", strings.Join(args, " "), err, stdout.String(), stderr.String())
|
|
}
|
|
return stdout.String()
|
|
}
|
|
|
|
// bdFederationFail runs "bd federation" expecting failure.
|
|
func bdFederationFail(t *testing.T, bd, dir string, args ...string) string {
|
|
t.Helper()
|
|
fullArgs := append([]string{"federation"}, args...)
|
|
cmd := exec.Command(bd, fullArgs...)
|
|
cmd.Dir = dir
|
|
cmd.Env = bdEnv(dir)
|
|
out, err := cmd.CombinedOutput()
|
|
if err == nil {
|
|
t.Fatalf("bd federation %s should have failed, got: %s", strings.Join(args, " "), out)
|
|
}
|
|
return string(out)
|
|
}
|
|
|
|
// assertExactJSONKeys pins a JSON object's key set: every wanted key present,
|
|
// nothing else added.
|
|
func assertExactJSONKeys(t *testing.T, what string, obj map[string]json.RawMessage, want ...string) {
|
|
t.Helper()
|
|
wanted := make(map[string]bool, len(want))
|
|
for _, key := range want {
|
|
wanted[key] = true
|
|
if _, ok := obj[key]; !ok {
|
|
t.Errorf("%s is missing key %q", what, key)
|
|
}
|
|
}
|
|
for key := range obj {
|
|
if !wanted[key] {
|
|
t.Errorf("%s gained key %q, want only %v", what, key, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFormatFederationPeerListJSONPreservesLegacyKeys(t *testing.T) {
|
|
formatted := formatFederationPeerListJSON([]storage.RemoteInfo{{
|
|
Name: "town-beta",
|
|
URL: "file:///tmp/town-beta",
|
|
}})
|
|
|
|
raw, err := json.Marshal(formatted)
|
|
if err != nil {
|
|
t.Fatalf("marshal federation peer JSON: %v", err)
|
|
}
|
|
body := string(raw)
|
|
if !strings.Contains(body, `"Name":"town-beta"`) || !strings.Contains(body, `"URL":"file:///tmp/town-beta"`) {
|
|
t.Fatalf("formatted JSON should preserve legacy Name/URL keys, got %s", body)
|
|
}
|
|
if strings.Contains(body, `"name"`) || strings.Contains(body, `"url"`) {
|
|
t.Fatalf("formatted JSON should not expose lowercase RemoteInfo storage tags, got %s", body)
|
|
}
|
|
}
|
|
|
|
func TestEmbeddedFederation(t *testing.T) {
|
|
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
|
|
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt federation tests")
|
|
}
|
|
t.Parallel()
|
|
|
|
bd := buildEmbeddedBD(t)
|
|
|
|
t.Run("list_peers_empty", func(t *testing.T) {
|
|
dir, _, _ := bdInit(t, bd, "--prefix", "fdlst0")
|
|
|
|
out := bdFederation(t, bd, dir, "list-peers")
|
|
if !strings.Contains(out, "No federation peers") {
|
|
t.Errorf("expected 'No federation peers', got: %s", out)
|
|
}
|
|
})
|
|
|
|
t.Run("list_peers_empty_json", func(t *testing.T) {
|
|
dir, _, _ := bdInit(t, bd, "--prefix", "fdlstj")
|
|
|
|
out := bdFederation(t, bd, dir, "list-peers", "--json")
|
|
// Should be valid JSON (empty array or null)
|
|
out = strings.TrimSpace(out)
|
|
if out != "null" && out != "[]" {
|
|
// Try parsing as JSON array
|
|
var result []interface{}
|
|
if err := json.Unmarshal([]byte(out), &result); err != nil {
|
|
t.Fatalf("expected valid JSON array, got: %s", out)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("add_peer_simple", func(t *testing.T) {
|
|
dir, _, _ := bdInit(t, bd, "--prefix", "fdadd")
|
|
|
|
out := bdFederation(t, bd, dir, "add-peer", "test-peer", "file:///tmp/fake-peer")
|
|
if !strings.Contains(out, "test-peer") {
|
|
t.Errorf("expected peer name in output, got: %s", out)
|
|
}
|
|
|
|
// Verify it appears in list
|
|
listOut := bdFederation(t, bd, dir, "list-peers")
|
|
if !strings.Contains(listOut, "test-peer") {
|
|
t.Errorf("expected 'test-peer' in list, got: %s", listOut)
|
|
}
|
|
})
|
|
|
|
t.Run("add_peer_json", func(t *testing.T) {
|
|
dir, _, _ := bdInit(t, bd, "--prefix", "fdaddj")
|
|
|
|
out := bdFederation(t, bd, dir, "add-peer", "json-peer", "file:///tmp/json-peer", "--json")
|
|
var result map[string]interface{}
|
|
if err := json.Unmarshal([]byte(out), &result); err != nil {
|
|
t.Fatalf("failed to parse JSON: %v\n%s", err, out)
|
|
}
|
|
if added, _ := result["added"].(string); added != "json-peer" {
|
|
t.Errorf("expected added='json-peer', got %q", added)
|
|
}
|
|
})
|
|
|
|
t.Run("add_peer_with_credentials", func(t *testing.T) {
|
|
dir, _, _ := bdInit(t, bd, "--prefix", "fdcred")
|
|
|
|
out := bdFederation(t, bd, dir, "add-peer", "auth-peer", "file:///tmp/auth-peer",
|
|
"--user", "admin", "--password", "secret", "--json")
|
|
var result map[string]interface{}
|
|
if err := json.Unmarshal([]byte(out), &result); err != nil {
|
|
t.Fatalf("failed to parse JSON: %v\n%s", err, out)
|
|
}
|
|
if hasAuth, _ := result["has_auth"].(bool); !hasAuth {
|
|
t.Error("expected has_auth=true")
|
|
}
|
|
})
|
|
|
|
t.Run("add_peer_with_sovereignty", func(t *testing.T) {
|
|
dir, _, _ := bdInit(t, bd, "--prefix", "fdsov")
|
|
|
|
out := bdFederation(t, bd, dir, "add-peer", "sov-peer", "file:///tmp/sov-peer",
|
|
"--sovereignty", "T2", "--json")
|
|
var result map[string]interface{}
|
|
if err := json.Unmarshal([]byte(out), &result); err != nil {
|
|
t.Fatalf("failed to parse JSON: %v\n%s", err, out)
|
|
}
|
|
if sov, _ := result["sovereignty"].(string); sov != "T2" {
|
|
t.Errorf("expected sovereignty='T2', got %q", sov)
|
|
}
|
|
})
|
|
|
|
t.Run("remove_peer", func(t *testing.T) {
|
|
dir, _, _ := bdInit(t, bd, "--prefix", "fdrm")
|
|
|
|
bdFederation(t, bd, dir, "add-peer", "removable", "file:///tmp/removable")
|
|
|
|
// Verify it exists
|
|
listOut := bdFederation(t, bd, dir, "list-peers")
|
|
if !strings.Contains(listOut, "removable") {
|
|
t.Fatalf("peer should exist before removal, got: %s", listOut)
|
|
}
|
|
|
|
// Remove it
|
|
out := bdFederation(t, bd, dir, "remove-peer", "removable")
|
|
if !strings.Contains(out, "removable") {
|
|
t.Errorf("expected peer name in removal output, got: %s", out)
|
|
}
|
|
|
|
// Verify it's gone
|
|
listOut = bdFederation(t, bd, dir, "list-peers")
|
|
if strings.Contains(listOut, "removable") {
|
|
t.Errorf("peer should be gone after removal, got: %s", listOut)
|
|
}
|
|
})
|
|
|
|
t.Run("remove_peer_json", func(t *testing.T) {
|
|
dir, _, _ := bdInit(t, bd, "--prefix", "fdrmj")
|
|
|
|
bdFederation(t, bd, dir, "add-peer", "rm-json", "file:///tmp/rm-json")
|
|
|
|
out := bdFederation(t, bd, dir, "remove-peer", "rm-json", "--json")
|
|
var result map[string]interface{}
|
|
if err := json.Unmarshal([]byte(out), &result); err != nil {
|
|
t.Fatalf("failed to parse JSON: %v\n%s", err, out)
|
|
}
|
|
if removed, _ := result["removed"].(string); removed != "rm-json" {
|
|
t.Errorf("expected removed='rm-json', got %q", removed)
|
|
}
|
|
})
|
|
|
|
t.Run("status_no_peers", func(t *testing.T) {
|
|
dir, _, _ := bdInit(t, bd, "--prefix", "fdst0")
|
|
|
|
out := bdFederation(t, bd, dir, "status")
|
|
if !strings.Contains(out, "No federation peers") {
|
|
t.Errorf("expected 'No federation peers', got: %s", out)
|
|
}
|
|
})
|
|
|
|
// Machine B: the peer row arrived with the database, the machine-local
|
|
// credential key did not. Status must point at the local key rather than
|
|
// report the peer as unreachable (GH#5085 review).
|
|
t.Run("status_credential_key_mismatch", func(t *testing.T) {
|
|
dir, beadsDir, _ := bdInit(t, bd, "--prefix", "fdkey")
|
|
|
|
bdFederation(t, bd, dir, "add-peer", "keyless", "file:///tmp/keyless",
|
|
"--user", "admin", "--password", "secret")
|
|
|
|
otherKey := make([]byte, 32)
|
|
if _, err := io.ReadFull(rand.Reader, otherKey); err != nil {
|
|
t.Fatalf("generate replacement key: %v", err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(beadsDir, ".beads-credential-key"), otherKey, 0600); err != nil {
|
|
t.Fatalf("write replacement key: %v", err)
|
|
}
|
|
|
|
out := bdFederation(t, bd, dir, "status")
|
|
if !strings.Contains(out, "Local credential error") {
|
|
t.Errorf("expected the local-credential label, got: %s", out)
|
|
}
|
|
if strings.Contains(out, "Unreachable:") {
|
|
t.Errorf("a local key failure should not be reported as unreachable, got: %s", out)
|
|
}
|
|
for _, want := range []string{".beads-credential-key", "machine-local", "bd federation add-peer"} {
|
|
if !strings.Contains(out, want) {
|
|
t.Errorf("status output does not mention %q, got: %s", want, out)
|
|
}
|
|
}
|
|
|
|
// The local-credential signal is a text-mode label carried by an
|
|
// unexported field, so --json keeps exactly the keys it had before that
|
|
// field existed. Asserted with the failing peer present, which is the
|
|
// only shape where the field is set.
|
|
jsonOut := bdFederation(t, bd, dir, "status", "--json")
|
|
var payload map[string]json.RawMessage
|
|
if err := json.Unmarshal([]byte(jsonOut), &payload); err != nil {
|
|
t.Fatalf("failed to parse status --json: %v\n%s", err, jsonOut)
|
|
}
|
|
// schema_version comes from the outputJSON envelope, not from this command.
|
|
assertExactJSONKeys(t, "status --json", payload, "peers", "pendingChanges", "schema_version")
|
|
|
|
var peers []map[string]json.RawMessage
|
|
if err := json.Unmarshal(payload["peers"], &peers); err != nil {
|
|
t.Fatalf("failed to parse status --json peers: %v\n%s", err, jsonOut)
|
|
}
|
|
if len(peers) != 1 {
|
|
t.Fatalf("status --json peers length = %d, want 1: %s", len(peers), jsonOut)
|
|
}
|
|
assertExactJSONKeys(t, "status --json peer", peers[0], "Status", "URL", "Reachable", "ReachError")
|
|
})
|
|
|
|
t.Run("status_json_no_peers", func(t *testing.T) {
|
|
dir, _, _ := bdInit(t, bd, "--prefix", "fdstj")
|
|
|
|
out := bdFederation(t, bd, dir, "status", "--json")
|
|
var result map[string]interface{}
|
|
if err := json.Unmarshal([]byte(out), &result); err != nil {
|
|
t.Fatalf("failed to parse JSON: %v\n%s", err, out)
|
|
}
|
|
if _, ok := result["peers"]; !ok {
|
|
t.Error("missing 'peers' in status JSON")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestEmbeddedFederationConcurrent(t *testing.T) {
|
|
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
|
|
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt federation tests")
|
|
}
|
|
t.Parallel()
|
|
|
|
bd := buildEmbeddedBD(t)
|
|
dir, _, _ := bdInit(t, bd, "--prefix", "fdconc")
|
|
|
|
const numWorkers = 10
|
|
|
|
type result struct {
|
|
worker int
|
|
out string
|
|
err error
|
|
}
|
|
|
|
results := make([]result, numWorkers)
|
|
var wg sync.WaitGroup
|
|
wg.Add(numWorkers)
|
|
|
|
for w := 0; w < numWorkers; w++ {
|
|
go func(worker int) {
|
|
defer wg.Done()
|
|
peerName := fmt.Sprintf("conc-peer-%d", worker)
|
|
peerURL := fmt.Sprintf("file:///tmp/conc-peer-%d", worker)
|
|
cmd := exec.Command(bd, "federation", "add-peer", peerName, peerURL)
|
|
cmd.Dir = dir
|
|
cmd.Env = bdEnv(dir)
|
|
out, err := cmd.CombinedOutput()
|
|
results[worker] = result{worker: worker, out: string(out), err: err}
|
|
}(w)
|
|
}
|
|
wg.Wait()
|
|
|
|
successes := 0
|
|
for _, r := range results {
|
|
if strings.Contains(r.out, "panic") {
|
|
t.Errorf("worker %d panicked:\n%s", r.worker, r.out)
|
|
}
|
|
if r.err == nil {
|
|
successes++
|
|
} else if !strings.Contains(r.out, "one writer at a time") {
|
|
t.Errorf("worker %d failed with unexpected error: %v\n%s", r.worker, r.err, r.out)
|
|
}
|
|
}
|
|
if successes < 1 {
|
|
t.Errorf("expected at least 1 successful add-peer, got %d", successes)
|
|
}
|
|
t.Logf("%d/%d federation workers succeeded", successes, numWorkers)
|
|
}
|