fix(federation): actionable error when peer credentials cannot be decrypted (#6995)

* 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>
This commit is contained in:
Julian Knutsen 2026-09-30 20:29:57 -07:00 • committed by GitHub
commit dba70de281
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 528 additions and 8 deletions

View file

@ -3,6 +3,7 @@
package main
import (
"errors"
"fmt"
"os"
"strings"
@ -303,6 +304,10 @@ func runFederationStatus(cmd *cobra.Command, args []string) error {
URL string
Reachable bool
ReachError string
// localCredentialErr marks a fetch that failed on this machine's
// credential key rather than on the network, so the line below does not
// blame the peer. Unexported: not part of the --json shape.
localCredentialErr bool
}
var peerStatuses []peerStatus
@ -321,6 +326,7 @@ func runFederationStatus(cmd *cobra.Command, args []string) error {
ps.Status = status
} else {
ps.ReachError = fetchErr.Error()
ps.localCredentialErr = errors.Is(fetchErr, storage.ErrCredentialKeyMismatch)
}
peerStatuses = append(peerStatuses, ps)
@ -343,9 +349,12 @@ func runFederationStatus(cmd *cobra.Command, args []string) error {
status := ps.Status
fmt.Printf(" %s %s\n", ui.RenderAccent(status.Peer), ui.RenderMuted(ps.URL))
if ps.Reachable {
switch {
case ps.Reachable:
fmt.Printf(" %s Reachable\n", ui.RenderPass("✓"))
} else {
case ps.localCredentialErr:
fmt.Printf(" %s Local credential error: %s\n", ui.RenderFail("✗"), ps.ReachError)
default:
fmt.Printf(" %s Unreachable: %s\n", ui.RenderFail("✗"), ps.ReachError)
}

View file

@ -3,10 +3,13 @@
package main
import (
"crypto/rand"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"testing"
@ -42,6 +45,24 @@ func bdFederationFail(t *testing.T, bd, dir string, args ...string) string {
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",
@ -197,6 +218,58 @@ func TestEmbeddedFederation(t *testing.T) {
}
})
// 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")

View file

@ -257,9 +257,27 @@ func (s *DoltStore) decryptPassword(encrypted []byte) (string, error) {
key := s.credentialKey
s.mu.RUnlock()
if key == nil {
// Deliberately not a key mismatch: the key was never initialized on
// this store, so the fix is init or file permissions, not re-adding
// the peer. Labeling it a mismatch would prescribe the wrong action.
return "", fmt.Errorf("credential encryption key not initialized")
}
return decryptWithKey(encrypted, key)
plaintext, err := decryptWithKey(encrypted, key)
if err != nil {
// federation_peers rows travel with the database, the key file does
// not, so a database opened on a second machine reaches here with a
// key that cannot read the stored password. Enrich at this single
// decrypt funnel, as embeddeddolt does, so every reader reports the
// credential problem rather than a bare cipher error. decryptWithKey
// stays unwrapped for the key-rotation path, which reads old keys on
// purpose and must not report a rotation miss as a mismatch. Name the
// resolved path rather than the bare filename: initCredentialKey looks
// this same basename up under both beadsDir and the legacy dbPath, so
// during the migration window two files carrying different keys can
// share the name and the bare one does not say which is meant.
return "", storage.CredentialKeyMismatchError(filepath.Join(s.beadsDir, credentialKeyFile), err)
}
return plaintext, nil
}
// AddFederationPeer adds or updates a federation peer with credentials.
@ -354,7 +372,9 @@ func (s *DoltStore) GetFederationPeer(ctx context.Context, name string) (*storag
}
peer.Password, err = s.decryptPassword(encryptedPwd)
if err != nil {
return nil, fmt.Errorf("failed to decrypt password: %w", err)
// decryptPassword already classifies the local-key case; name the
// peer and preserve the sentinel for errors.Is/As.
return nil, fmt.Errorf("failed to decrypt password for peer %s: %w", name, err)
}
}
@ -397,7 +417,9 @@ func (s *DoltStore) ListFederationPeers(ctx context.Context) ([]*storage.Federat
}
peer.Password, err = s.decryptPassword(encryptedPwd)
if err != nil {
return nil, fmt.Errorf("failed to decrypt password: %w", err)
// Same as GetFederationPeer: the funnel classified it, the
// caller only names which peer failed.
return nil, fmt.Errorf("failed to decrypt password for peer %s: %w", peer.Name, err)
}
}

View file

@ -2,16 +2,20 @@ package dolt
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"slices"
"strings"
"testing"
"time"
sqlmock "github.com/DATA-DOG/go-sqlmock"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/storage/doltutil"
"github.com/steveyegge/beads/internal/testutil"
@ -869,6 +873,138 @@ func TestFederationPeerCredentialLifecycleLazyKeyInit(t *testing.T) {
}
}
// A federation_peers row travels with the database; the credential key file
// does not. A database opened on a second machine therefore holds a peer
// password this machine's key cannot read, and both server-mode read paths
// must say so with the remediation instead of surfacing a bare
// "cipher: message authentication failed" (GH#5085 review). sqlmock stands in
// for the server, following draincall_regression_test.go, so the branch is
// pinned without a live Dolt.
func TestFederationPeerDecryptKeyMismatchNamesTheLocalKey(t *testing.T) {
writerKey := make([]byte, 32)
for i := range writerKey {
writerKey[i] = byte(i)
}
localKey := make([]byte, 32)
for i := range localKey {
localKey[i] = byte(i + 1)
}
encrypted, err := encryptWithKey("peerpass", writerKey)
if err != nil {
t.Fatalf("encryptWithKey() error = %v", err)
}
// beadsDir is set only so the enrichment can resolve the key path it names;
// credentialKey is preset, so initCredentialKey never runs and no test
// writes a key file. Leaving it empty would make the resolved-path fragment
// below vacuous, because filepath.Join("", credentialKeyFile) is the bare
// basename the fix replaced (GH#5214 review).
beadsDir := t.TempDir()
newStore := func(t *testing.T) (*DoltStore, sqlmock.Sqlmock) {
t.Helper()
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("sqlmock: %v", err)
}
t.Cleanup(func() { _ = db.Close() })
return &DoltStore{db: db, credentialKey: localKey, beadsDir: beadsDir}, mock
}
peerRows := func() *sqlmock.Rows {
now := time.Now()
return sqlmock.NewRows([]string{
"name", "remote_url", "username", "password_encrypted",
"sovereignty", "last_sync", "created_at", "updated_at",
}).AddRow("team", "https://peer.example/peerdb", "peeruser", encrypted, "T2", nil, now, now)
}
wantFragments := []string{
// Both paths name the peer, so the operator knows which one to re-add.
"failed to decrypt password for peer team",
// The shared enrichment, pinned verbatim. The machine-local key file is
// context in a parenthetical, not an asserted cause: an AES-GCM open also
// fails on a tampered blob and on a row still under the legacy key, and
// re-adding the peer is the fix in all three cases. The key file is named
// by its resolved path, because this basename is also looked up under the
// legacy dbPath and the bare name would not say which file is meant.
"stored peer credentials cannot be decrypted with this machine's credential key " +
"(the key file " + filepath.Join(beadsDir, credentialKeyFile) + " is machine-local and does not replicate with the database); " +
"re-run 'bd federation add-peer <name> <url> --user <user>' on this machine",
// The cipher error stays wrapped, so the raw cause is still readable.
"cipher: message authentication failed",
}
assertMismatch := func(t *testing.T, err error) {
t.Helper()
if err == nil {
t.Fatal("read succeeded, want decrypt failure")
}
if !errors.Is(err, storage.ErrCredentialKeyMismatch) {
t.Errorf("error = %v, want errors.Is storage.ErrCredentialKeyMismatch", err)
}
for _, want := range wantFragments {
if !strings.Contains(err.Error(), want) {
t.Errorf("error %q does not mention %q", err, want)
}
}
}
t.Run("GetFederationPeer", func(t *testing.T) {
store, mock := newStore(t)
mock.ExpectQuery(regexp.QuoteMeta("FROM federation_peers WHERE name = ?")).
WithArgs("team").
WillReturnRows(peerRows())
_, err := store.GetFederationPeer(t.Context(), "team")
assertMismatch(t, err)
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet sqlmock expectations: %v", err)
}
})
t.Run("ListFederationPeers", func(t *testing.T) {
store, mock := newStore(t)
mock.ExpectQuery(regexp.QuoteMeta("FROM federation_peers ORDER BY name")).
WillReturnRows(peerRows())
_, err := store.ListFederationPeers(t.Context())
assertMismatch(t, err)
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet sqlmock expectations: %v", err)
}
})
// withPeerCredentials is the seam that carries the sentinel into
// `bd federation status` on this backend, mirroring embeddeddolt's
// TestWithPeerAuth_KeyMismatchFailsClosed: it must fail closed rather than
// invoke the operation with no credentials, which would present the wrong
// identity to the peer and re-blame the network (GH#5214 review).
t.Run("withPeerCredentials", func(t *testing.T) {
store, mock := newStore(t)
mock.ExpectQuery(regexp.QuoteMeta("FROM federation_peers WHERE name = ?")).
WithArgs("team").
WillReturnRows(peerRows())
called := false
err := store.withPeerCredentials(t.Context(), "team", func(*remoteCredentials) error {
called = true
return nil
})
if called {
t.Error("withPeerCredentials ran the operation, want fail-closed")
}
assertMismatch(t, err)
// The seam's own wrap, so the sentinel survives the extra hop.
if want := "failed to get peer credentials:"; !strings.Contains(err.Error(), want) {
t.Errorf("error %q does not mention %q", err, want)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet sqlmock expectations: %v", err)
}
})
}
// openCloudAuthTestStore opens a DoltStore against the shared test Dolt server
// for cloud-auth routing tests. The returned store has serverMode=true and a
// fresh empty database; callers should AddRemote to seed the SQL surface.
@ -1143,3 +1279,93 @@ func TestEnvPrefixesForRemoteURL(t *testing.T) {
})
}
}
// An uninitialized credential key is not a key mismatch, and the distinction
// is reachable from the peer readers: initCredentialKey returns nil without
// setting a key when beadsDir is empty, so ensureCredentialKey succeeds and
// decryptPassword still finds no key. Wrapping at the callers labeled that
// error a mismatch and told the operator to re-add the peer, which does
// nothing when the key was never created; the fix there is init or file
// permissions. The wrap therefore lives at the decrypt funnel instead.
func TestFederationPeerUninitializedKeyIsNotAMismatch(t *testing.T) {
// beadsDir empty and credentialKey nil: ensureCredentialKey is a no-op
// that reports success, which is the edge the caller-side wrap mislabeled.
newStore := func(t *testing.T) (*DoltStore, sqlmock.Sqlmock) {
t.Helper()
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("sqlmock: %v", err)
}
t.Cleanup(func() { _ = db.Close() })
return &DoltStore{db: db}, mock
}
peerRows := func() *sqlmock.Rows {
now := time.Now()
return sqlmock.NewRows([]string{
"name", "remote_url", "username", "password_encrypted",
"sovereignty", "last_sync", "created_at", "updated_at",
}).AddRow("team", "https://peer.example/peerdb", "peeruser",
[]byte("stored ciphertext"), "T2", nil, now, now)
}
assertNotMismatch := func(t *testing.T, err error) {
t.Helper()
if err == nil {
t.Fatal("read succeeded, want a decrypt failure")
}
if errors.Is(err, storage.ErrCredentialKeyMismatch) {
t.Errorf("error = %v, want NOT errors.Is storage.ErrCredentialKeyMismatch", err)
}
if !strings.Contains(err.Error(), "credential encryption key not initialized") {
t.Errorf("error = %v, want it to name the uninitialized key", err)
}
if strings.Contains(err.Error(), "re-run 'bd federation add-peer") {
t.Errorf("error = %v, must not prescribe re-adding the peer for an uninitialized key", err)
}
}
t.Run("GetFederationPeer", func(t *testing.T) {
store, mock := newStore(t)
mock.ExpectQuery(regexp.QuoteMeta("FROM federation_peers WHERE name = ?")).
WithArgs("team").
WillReturnRows(peerRows())
_, err := store.GetFederationPeer(t.Context(), "team")
assertNotMismatch(t, err)
})
t.Run("ListFederationPeers", func(t *testing.T) {
store, mock := newStore(t)
mock.ExpectQuery(regexp.QuoteMeta("FROM federation_peers ORDER BY name")).
WillReturnRows(peerRows())
_, err := store.ListFederationPeers(t.Context())
assertNotMismatch(t, err)
})
}
// The key-rotation reader passes an old key to decryptWithKey on purpose, so
// that helper must stay unwrapped: a rotation miss is not a mismatch.
func TestDecryptWithKeyStaysUnwrappedForRotation(t *testing.T) {
key := make([]byte, 32)
for i := range key {
key[i] = byte(i)
}
other := make([]byte, 32)
for i := range other {
other[i] = byte(255 - i)
}
encrypted, err := encryptWithKey("secret", key)
if err != nil {
t.Fatalf("encryptWithKey() error = %v", err)
}
_, err = decryptWithKey(encrypted, other)
if err == nil {
t.Fatal("decryptWithKey with the wrong key succeeded, want an error")
}
if errors.Is(err, storage.ErrCredentialKeyMismatch) {
t.Errorf("error = %v, want the bare cipher error; classification belongs to decryptPassword", err)
}
}

View file

@ -95,12 +95,23 @@ func (s *EmbeddedDoltStore) decryptPassword(encrypted []byte) (string, error) {
}
nonceSize := gcm.NonceSize()
if len(encrypted) < nonceSize {
return "", fmt.Errorf("ciphertext too short")
// A blob too short to carry its nonce is the same local-corruption
// class as the failed GCM open below: the stored credential cannot be
// decrypted on this machine, and re-adding the peer is the fix.
// Classify it through the same sentinel so federation status reports
// the credential problem rather than an unreachable peer.
return "", storage.CredentialKeyMismatchError(filepath.Join(s.beadsDir, credentialKeyFile), fmt.Errorf("ciphertext too short"))
}
nonce, ciphertext := encrypted[:nonceSize], encrypted[nonceSize:]
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return "", err
// The common shape: the peer row replicated with the database but the key
// file did not, so machine B pulls the row, ensureCredentialKey generates
// a fresh key, and every decrypt fails authentication. Enrich at the
// single decrypt funnel so every reader reports it: peer resolution for
// the remote verbs, GetFederationPeer, ListFederationPeers. The wording
// is shared with package dolt, which enriches its own two read paths.
return "", storage.CredentialKeyMismatchError(filepath.Join(s.beadsDir, credentialKeyFile), err)
}
return string(plaintext), nil
}
@ -141,7 +152,7 @@ func (s *EmbeddedDoltStore) GetFederationPeer(ctx context.Context, name string)
if len(row.EncryptedPwd) > 0 {
row.Peer.Password, err = s.decryptPassword(row.EncryptedPwd)
if err != nil {
return nil, fmt.Errorf("decrypt password: %w", err)
return nil, fmt.Errorf("decrypt password for peer %s: %w", name, err)
}
}
return &row.Peer, nil

View file

@ -3,8 +3,10 @@
package embeddeddolt
import (
"crypto/rand"
"database/sql"
"errors"
"io"
"os"
"path/filepath"
"strings"
@ -192,6 +194,156 @@ func TestWithPeerAuth_RestoresAbsentEnv(t *testing.T) {
}
}
// rotateCredentialKey simulates machine B: the peer row arrived with the
// database, the machine-local key file did not, so this machine holds a key
// that cannot decrypt the stored password.
func rotateCredentialKey(t *testing.T, store *EmbeddedDoltStore) {
t.Helper()
key := make([]byte, 32)
if _, err := io.ReadFull(rand.Reader, key); err != nil {
t.Fatalf("generate replacement key: %v", err)
}
if err := os.WriteFile(filepath.Join(store.beadsDir, credentialKeyFile), key, 0600); err != nil {
t.Fatalf("write replacement key: %v", err)
}
store.credentialKey = nil
}
// wantKeyMismatchClause is the enrichment storage.CredentialKeyMismatchError
// emits, pinned verbatim. The machine-local key file reads as context inside a
// parenthetical rather than as the asserted cause, because an AES-GCM open also
// fails on a tampered blob and on a row still under the legacy key; re-adding
// the peer is the fix in all three cases (GH#5085 review).
//
// It takes the store because the clause names the key file by its RESOLVED
// path, not by basename, so this pin fails if a caller regresses to the bare
// const (GH#5214 review).
func wantKeyMismatchClause(store *EmbeddedDoltStore) string {
return "stored peer credentials cannot be decrypted with this machine's credential key " +
"(the key file " + filepath.Join(store.beadsDir, credentialKeyFile) + " is machine-local and does not replicate with the database); " +
"re-run 'bd federation add-peer <name> <url> --user <user>' on this machine"
}
// Pins the decrypt-failure branch (GH#5085 review): a peer row whose password
// was encrypted with a different machine's key must fail with the local context
// and the fix, not a bare cipher error, on every read path.
func TestDecryptPassword_KeyMismatchNamesTheLocalKey(t *testing.T) {
ctx := t.Context()
store := newPeerAuthTestStore(t)
if err := store.AddFederationPeer(ctx, &storage.FederationPeer{
Name: "team", RemoteURL: "https://peer.example/peerdb",
Username: "peeruser", Password: "peerpass",
}); err != nil {
t.Fatalf("AddFederationPeer: %v", err)
}
rotateCredentialKey(t, store)
wantFragments := []string{
// Both read paths name the peer, so the operator knows which one to re-add.
"for peer team",
wantKeyMismatchClause(store),
// The cipher error stays wrapped so the raw cause is still readable.
"cipher: message authentication failed",
}
t.Run("GetFederationPeer", func(t *testing.T) {
_, err := store.GetFederationPeer(ctx, "team")
if err == nil {
t.Fatal("GetFederationPeer succeeded, want decrypt failure")
}
if !errors.Is(err, storage.ErrCredentialKeyMismatch) {
t.Errorf("error = %v, want errors.Is storage.ErrCredentialKeyMismatch", err)
}
for _, want := range wantFragments {
if !strings.Contains(err.Error(), want) {
t.Errorf("error %q does not mention %q", err, want)
}
}
})
t.Run("ListFederationPeers", func(t *testing.T) {
_, err := store.ListFederationPeers(ctx)
if err == nil {
t.Fatal("ListFederationPeers succeeded, want decrypt failure")
}
if !errors.Is(err, storage.ErrCredentialKeyMismatch) {
t.Errorf("error = %v, want errors.Is storage.ErrCredentialKeyMismatch", err)
}
for _, want := range wantFragments {
if !strings.Contains(err.Error(), want) {
t.Errorf("error %q does not mention %q", err, want)
}
}
})
}
// Pins the short-ciphertext branch (GH#5214 review): a stored blob shorter
// than the GCM nonce is local corruption of the credential row, so it must
// classify through the same sentinel as a failed authentication rather than
// surface as a bare cipher error that federation status reads as an
// unreachable peer.
func TestDecryptPassword_ShortCiphertextClassifiesAsKeyMismatch(t *testing.T) {
store := newPeerAuthTestStore(t)
_, err := store.decryptPassword([]byte("short"))
if err == nil {
t.Fatal("decryptPassword succeeded, want short-ciphertext failure")
}
if !errors.Is(err, storage.ErrCredentialKeyMismatch) {
t.Errorf("error = %v, want errors.Is storage.ErrCredentialKeyMismatch", err)
}
for _, want := range []string{wantKeyMismatchClause(store), "ciphertext too short"} {
if !strings.Contains(err.Error(), want) {
t.Errorf("error %q does not mention %q", err, want)
}
}
}
// A remote verb keeps failing closed on an undecryptable password: the
// operation must not run under ambient credentials, which could present the
// wrong identity to the peer.
func TestWithPeerAuth_KeyMismatchFailsClosed(t *testing.T) {
ctx := t.Context()
store := newPeerAuthTestStore(t)
t.Setenv("DOLT_REMOTE_USER", "envuser")
t.Setenv("DOLT_REMOTE_PASSWORD", "envpass")
if err := store.AddFederationPeer(ctx, &storage.FederationPeer{
Name: "team", RemoteURL: "https://peer.example/peerdb",
Username: "peeruser", Password: "peerpass",
}); err != nil {
t.Fatalf("AddFederationPeer: %v", err)
}
rotateCredentialKey(t, store)
called := false
err := store.withPeerAuth(ctx, "team", func(string) error {
called = true
return nil
})
if err == nil {
t.Fatal("withPeerAuth succeeded, want decrypt failure")
}
if called {
t.Error("withPeerAuth ran the operation, want fail-closed")
}
if !errors.Is(err, storage.ErrCredentialKeyMismatch) {
t.Errorf("error = %v, want errors.Is storage.ErrCredentialKeyMismatch", err)
}
// The message a remote verb prints, wrap included.
for _, want := range []string{
"resolve peer credentials:",
"decrypt password for peer team:",
wantKeyMismatchClause(store),
} {
if !strings.Contains(err.Error(), want) {
t.Errorf("error %q does not mention %q", err, want)
}
}
}
func TestWithPeerAuth_ConcurrentPeersDoNotMixCredentials(t *testing.T) {
ctx := t.Context()
store := newPeerAuthTestStore(t)

View file

@ -10,6 +10,7 @@ import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/steveyegge/beads/internal/types"
@ -85,6 +86,32 @@ const (
NotClaimableStatusFragment = ": status "
)
// ErrCredentialKeyMismatch is returned when a stored federation peer's password
// cannot be decrypted with the local credential key. federation_peers rows
// travel with the database, but the key file is machine-local and gitignored,
// so a database pulled from another machine carries peer passwords this
// machine's key cannot read. Callers errors.Is it to tell that local key
// problem apart from an unreachable peer.
var ErrCredentialKeyMismatch = errors.New("stored peer credentials cannot be decrypted with this machine's credential key")
// CredentialKeyMismatchError wraps a federation password decrypt failure with
// the ErrCredentialKeyMismatch sentinel, the machine-local nature of keyFile,
// and the command that re-stores the password on this machine. Both backends
// (dolt, embeddeddolt) call it with their own credentialKeyFile so the operator
// reads one wording whichever store answered.
//
// The machine-local key is context, not an asserted cause: an AES-GCM open also
// fails on a tampered or truncated blob and on a row still encrypted under the
// pre-migration legacy key. Re-adding the peer is the fix in every one of those
// cases, so the remediation holds even where the parenthetical does not.
//
// cause stays wrapped, so the raw cipher error remains readable and both the
// sentinel and cause are reachable through errors.Is / errors.As.
func CredentialKeyMismatchError(keyFile string, cause error) error {
return fmt.Errorf("%w (the key file %s is machine-local and does not replicate with the database); re-run 'bd federation add-peer <name> <url> --user <user>' on this machine: %w",
ErrCredentialKeyMismatch, keyFile, cause)
}
// CommentPageCursor is the resume position for a keyset page of an issue's
// comments: the (created_at, id) of the last comment already returned. The zero
// value starts a walk from the beginning of the thread.