diff --git a/cmd/bd/federation.go b/cmd/bd/federation.go index 0e4f30d36..06e7e207b 100644 --- a/cmd/bd/federation.go +++ b/cmd/bd/federation.go @@ -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) } diff --git a/cmd/bd/federation_embedded_test.go b/cmd/bd/federation_embedded_test.go index 34d6f69bc..8428f0467 100644 --- a/cmd/bd/federation_embedded_test.go +++ b/cmd/bd/federation_embedded_test.go @@ -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") diff --git a/internal/storage/dolt/credentials.go b/internal/storage/dolt/credentials.go index 64db95f08..2ab1ade50 100644 --- a/internal/storage/dolt/credentials.go +++ b/internal/storage/dolt/credentials.go @@ -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) } } diff --git a/internal/storage/dolt/credentials_test.go b/internal/storage/dolt/credentials_test.go index 62c12d4be..d0ae3069c 100644 --- a/internal/storage/dolt/credentials_test.go +++ b/internal/storage/dolt/credentials_test.go @@ -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 --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) + } +} diff --git a/internal/storage/embeddeddolt/federation.go b/internal/storage/embeddeddolt/federation.go index 577d17693..016538d5a 100644 --- a/internal/storage/embeddeddolt/federation.go +++ b/internal/storage/embeddeddolt/federation.go @@ -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 diff --git a/internal/storage/embeddeddolt/federation_credentials_test.go b/internal/storage/embeddeddolt/federation_credentials_test.go index 3269c6b69..147b893db 100644 --- a/internal/storage/embeddeddolt/federation_credentials_test.go +++ b/internal/storage/embeddeddolt/federation_credentials_test.go @@ -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 --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) diff --git a/internal/storage/storage.go b/internal/storage/storage.go index dc66d2cac..b90f62833 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -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 --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.