diff --git a/internal/storage/embeddeddolt/federation.go b/internal/storage/embeddeddolt/federation.go index 016538d5a..86c9aa016 100644 --- a/internal/storage/embeddeddolt/federation.go +++ b/internal/storage/embeddeddolt/federation.go @@ -197,6 +197,8 @@ func (s *EmbeddedDoltStore) withPeerAuth(ctx context.Context, peer string, fn fu federationEnvMutex.Lock() defer federationEnvMutex.Unlock() + warnStoredPeerSuppressesAmbientPassword(peer, p) + restoreUser := overrideEnv("DOLT_REMOTE_USER", p.Username) restorePassword := overrideEnv("DOLT_REMOTE_PASSWORD", p.Password) defer func() { @@ -207,6 +209,44 @@ func (s *EmbeddedDoltStore) withPeerAuth(ctx context.Context, peer string, fn fu return fn(p.Username) } +// federationWarnWriter receives the peer-credential diagnostics. Defaults to +// os.Stderr, matching the unconditional warnings this package writes from +// store.go; overridable in tests. +var federationWarnWriter io.Writer = os.Stderr + +// warnStoredPeerSuppressesAmbientPassword emits one line when a peer stores a +// username but no password while DOLT_REMOTE_PASSWORD is set. Suppressing the +// ambient password is the intended security default, but a setup that used to +// authenticate off the environment then fails with no hint about why (GH#5085 +// review). Call with federationEnvMutex held and before overrideEnv rewrites +// the pair, so the value read is the ambient one and not another peer's +// installed password. +func warnStoredPeerSuppressesAmbientPassword(peer string, p *storage.FederationPeer) { + // Defensive: withPeerAuth returns before this guard when both fields are empty. + if p.Username == "" || p.Password != "" { + return + } + // An ambient empty value is treated as no ambient password: unsetting it + // changes nothing the user could be puzzled by. + if os.Getenv("DOLT_REMOTE_PASSWORD") == "" { + return + } + // add-peer upserts the whole row, so a re-run without --sovereignty + // clears a stored tier (GH#5213): carry the stored tier into the + // suggested command. The password is left to add-peer's no-echo prompt + // rather than suggested as an argument that lands in shell history. + sovereignty := "" + if p.Sovereignty != "" { + sovereignty = " --sovereignty " + p.Sovereignty + } + fmt.Fprintf(federationWarnWriter, + "Warning: peer %[1]q stores a username with an empty password, "+ + "which overrides the ambient DOLT_REMOTE_PASSWORD for this operation; "+ + "store a password by re-running 'bd federation add-peer %[1]s "+ + "--user %[2]s%[3]s' and entering it at the prompt.\n", + peer, p.Username, sovereignty) +} + // verifyPeerRemoteURL fails closed when the live remote named peer does not // carry the URL stored on the federation peer row. AddRemoteIfNotExists // preserves an existing same-name remote regardless of its URL, so the name diff --git a/internal/storage/embeddeddolt/federation_credentials_test.go b/internal/storage/embeddeddolt/federation_credentials_test.go index 147b893db..27d68cc02 100644 --- a/internal/storage/embeddeddolt/federation_credentials_test.go +++ b/internal/storage/embeddeddolt/federation_credentials_test.go @@ -3,6 +3,7 @@ package embeddeddolt import ( + "bytes" "crypto/rand" "database/sql" "errors" @@ -20,6 +21,14 @@ import ( func newPeerAuthTestStore(t *testing.T) *EmbeddedDoltStore { t.Helper() ctx := t.Context() + + // Several cases here deliberately build the state that warns about a + // suppressed ambient password; keep that off the test log unless a test + // installs its own writer to assert on it. + prevWarn := federationWarnWriter + federationWarnWriter = io.Discard + t.Cleanup(func() { federationWarnWriter = prevWarn }) + beadsDir := filepath.Join(t.TempDir(), ".beads") store, err := Open(ctx, beadsDir, "fedauth", "main") if err != nil { @@ -145,6 +154,146 @@ func TestWithPeerAuth(t *testing.T) { } } +func TestWithPeerAuth_WarnsWhenStoredPeerSuppressesAmbientPassword(t *testing.T) { + // Pinned verbatim, not matched by substring: the peer name, the username, + // the stored tier, and the Warning: prefix are the parts an operator + // reads, so a wording change has to be made here deliberately. + const wantOpenPwdWarning = `Warning: peer "open-pwd" stores a username with an empty password, ` + + `which overrides the ambient DOLT_REMOTE_PASSWORD for this operation; ` + + `store a password by re-running 'bd federation add-peer open-pwd ` + + `--user peeruser' and entering it at the prompt.` + "\n" + const wantTieredWarning = `Warning: peer "tiered" stores a username with an empty password, ` + + `which overrides the ambient DOLT_REMOTE_PASSWORD for this operation; ` + + `store a password by re-running 'bd federation add-peer tiered ` + + `--user peeruser --sovereignty T2' and entering it at the prompt.` + "\n" + + cases := []struct { + name string + peer *storage.FederationPeer + remote string + ambient string + ambientSet bool + // directPeer, when set, calls the warning helper itself instead of + // going through withPeerAuth, pinning the helper's own boundary. + directPeer *storage.FederationPeer + wantLine string + }{ + { + name: "username with empty stored password suppresses ambient password", + peer: &storage.FederationPeer{ + Name: "open-pwd", + RemoteURL: "https://peer.example/peerdb", + Username: "peeruser", + }, + remote: "open-pwd", ambient: "envpass", ambientSet: true, + wantLine: wantOpenPwdWarning, + }, + { + // add-peer upserts sovereignty along with the credentials, so the + // suggested re-run must carry the stored tier or it clears it. + name: "suggested re-add keeps the stored sovereignty tier", + peer: &storage.FederationPeer{ + Name: "tiered", + RemoteURL: "https://peer.example/peerdb", + Username: "peeruser", + Sovereignty: "T2", + }, + remote: "tiered", ambient: "envpass", ambientSet: true, + wantLine: wantTieredWarning, + }, + { + name: "stored password suppresses nothing the operator can act on", + peer: &storage.FederationPeer{ + Name: "team", + RemoteURL: "https://peer.example/peerdb", + Username: "peeruser", + Password: "peerpass", + }, + remote: "team", ambient: "envpass", ambientSet: true, + }, + { + name: "no ambient password to suppress", + peer: &storage.FederationPeer{ + Name: "open-pwd", + RemoteURL: "https://peer.example/peerdb", + Username: "peeruser", + }, + remote: "open-pwd", + }, + { + name: "empty ambient password reads as no ambient password", + peer: &storage.FederationPeer{ + Name: "open-pwd", + RemoteURL: "https://peer.example/peerdb", + Username: "peeruser", + }, + remote: "open-pwd", ambient: "", ambientSet: true, + }, + { + name: "peer without a username keeps the password puzzle out of scope", + peer: &storage.FederationPeer{ + Name: "open-usr", + RemoteURL: "https://peer.example/peerdb", + Password: "peerpass", + }, + remote: "open-usr", ambient: "envpass", ambientSet: true, + }, + { + name: "unknown remote keeps the environment fallback", + remote: "not-a-peer", ambient: "envpass", ambientSet: true, + }, + { + // withPeerAuth returns before the helper when a peer stores + // neither field, so a direct call is the only way to reach the + // helper's defensive empty-username clause. + name: "helper called directly with an all-empty peer stays silent", + directPeer: &storage.FederationPeer{}, + remote: "empty-peer", ambient: "envpass", ambientSet: true, + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + ctx := t.Context() + + t.Setenv("DOLT_REMOTE_USER", "envuser") + t.Setenv("DOLT_REMOTE_PASSWORD", tc.ambient) + if !tc.ambientSet { + _ = os.Unsetenv("DOLT_REMOTE_PASSWORD") + } + + var warnings bytes.Buffer + captureWarnings := func() { + // Installed after any store: newPeerAuthTestStore points the + // writer at io.Discard for the rest of the suite. + prev := federationWarnWriter + federationWarnWriter = &warnings + t.Cleanup(func() { federationWarnWriter = prev }) + } + + if tc.directPeer != nil { + captureWarnings() + warnStoredPeerSuppressesAmbientPassword(tc.remote, tc.directPeer) + } else { + store := newPeerAuthTestStore(t) + if tc.peer != nil { + if err := store.AddFederationPeer(ctx, tc.peer); err != nil { + t.Fatalf("AddFederationPeer: %v", err) + } + } + captureWarnings() + if err := store.withPeerAuth(ctx, tc.remote, func(string) error { return nil }); err != nil { + t.Fatalf("withPeerAuth: %v", err) + } + } + + if got := warnings.String(); got != tc.wantLine { + t.Errorf("warning = %q, want %q", got, tc.wantLine) + } + }) + } +} + func TestWithPeerAuth_RestoresEnvWhenCallbackFails(t *testing.T) { ctx := t.Context() store := newPeerAuthTestStore(t)