From 643615c26acc1d58cdb1ecfc31c1ba71120baef3 Mon Sep 17 00:00:00 2001 From: Julian Knutsen Date: Wed, 14 Oct 2026 18:31:05 +0000 Subject: [PATCH 1/4] fix(dolt): an env-inherited server yields to the project's declared endpoint on an identity mismatch An agent session carries its own workspace's Dolt server in its environment (BEADS_DOLT_SERVER_HOST/PORT). Run inside a different checkout whose .beads explicitly declares its own server, bd took the env's endpoint (env outranks config.yaml and metadata.json), found another project's database there, and the identity guard refused (PROJECT IDENTITY MISMATCH): the project was unusable from that session, and the refusal was misread as a broken workspace. New now opens through openWithEnvEndpointFallback. On the identity refusal, when the port came from the environment and the project's own .beads declares a different endpoint (config.yaml dolt.host/dolt.port, else metadata.json dolt_server_host/dolt_server_port; bd's port-file bookkeeping is no declaration), it retries once there. The retry is verified like any open: on a match bd proceeds with a note on stderr; if the declared endpoint serves another project too, the original refusal stands. A port from any other source, a socket, a gateway and the shared server are untouched. No schema change. The refusal is now a typed error (projectIdentityMismatch) with its message unchanged. Tests (fake servers, no live endpoints): the declared endpoint is opened and used, with a note, for a config.yaml host:port or a bare port; a declared endpoint that serves another project too returns the env server's original refusal; a config-sourced port, a project declaring nothing or the env's own endpoint, and any other error open once as before; metadata.json declarations are read. Mutants dropping the retry, the env-source condition, or returning the retry's error fail. Co-Authored-By: Claude Opus 5.5 (1M context) --- CHANGELOG.md | 12 ++ internal/storage/dolt/BUILD.bazel | 2 + .../storage/dolt/env_endpoint_fallback.go | 115 +++++++++++++ .../dolt/env_endpoint_fallback_test.go | 152 ++++++++++++++++++ internal/storage/dolt/store.go | 7 +- 5 files changed, 287 insertions(+), 1 deletion(-) create mode 100644 internal/storage/dolt/env_endpoint_fallback.go create mode 100644 internal/storage/dolt/env_endpoint_fallback_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 78861d4d2..f822b89eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -509,6 +509,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- A server endpoint inherited from the environment (`BEADS_DOLT_SERVER_PORT`) + no longer locks bd out of a project that declares its own: when the env's + server answers with another project's database (PROJECT IDENTITY MISMATCH) + and the project's `.beads` explicitly declares a different endpoint + (`config.yaml` `dolt.host`/`dolt.port`, else `metadata.json` + `dolt_server_host`/`dolt_server_port`), bd retries once on that endpoint, + verifies the project's identity there, and proceeds with a note on stderr; + if that endpoint serves another project too, it refuses with the original + error. An agent session carrying its own workspace's server in its + environment can now run bd inside a different checkout. Ports from any + other source, sockets, gateways and the shared server are unchanged. + - **`bd gate list` works over a connected HTTP workspace** (S20). The workspace-wide listing built a raw `SearchIssues` filter, which the HTTP backend refuses, so gc's `renudge-stale-human-gates` order failed on every diff --git a/internal/storage/dolt/BUILD.bazel b/internal/storage/dolt/BUILD.bazel index 2be809cc9..d8c721cea 100644 --- a/internal/storage/dolt/BUILD.bazel +++ b/internal/storage/dolt/BUILD.bazel @@ -27,6 +27,7 @@ go_library( "dependencies.go", "dependency_editor.go", "edgereader.go", + "env_endpoint_fallback.go", "ephemeral_routing.go", "errors.go", "events.go", @@ -214,6 +215,7 @@ go_test( "dualwrite_history_contract_test.go", "edge_reader_contract_test.go", "empty_commit_test.go", + "env_endpoint_fallback_test.go", "ephemeral_routing_test.go", "errors_test.go", "event_commit_boundary_test.go", diff --git a/internal/storage/dolt/env_endpoint_fallback.go b/internal/storage/dolt/env_endpoint_fallback.go new file mode 100644 index 000000000..65eaed3e1 --- /dev/null +++ b/internal/storage/dolt/env_endpoint_fallback.go @@ -0,0 +1,115 @@ +package dolt + +import ( + "context" + "errors" + "fmt" + "io" + "net" + "os" + "strconv" + "strings" + + "github.com/steveyegge/beads/internal/config" + "github.com/steveyegge/beads/internal/configfile" + "github.com/steveyegge/beads/internal/doltserver" +) + +// projectIdentityMismatch is verifyProjectIdentity's refusal: the database the +// resolved endpoint serves belongs to another project. +type projectIdentityMismatch struct{ err error } + +func (e *projectIdentityMismatch) Error() string { return e.err.Error() } + +// isProjectIdentityMismatch reports whether err is verifyProjectIdentity's +// refusal. +func isProjectIdentityMismatch(err error) bool { + var m *projectIdentityMismatch + return errors.As(err, &m) +} + +// declaredEndpoint is the server endpoint a project's own .beads declares. +type declaredEndpoint struct { + host string + port int + source doltserver.PortSource +} + +// envEndpointFallbackNote receives the note the fallback prints when it opens +// the project's own endpoint instead of the inherited one. A var so tests can +// capture it. +var envEndpointFallbackNote io.Writer = os.Stderr + +// openWithEnvEndpointFallback opens cfg with open and, when the open is +// refused for a project identity mismatch on an endpoint that came from the +// environment (BEADS_DOLT_SERVER_PORT, an agent's inherited server for +// ANOTHER project), retries once on the endpoint the project's own .beads +// explicitly declares, when it declares one that differs. A session that +// carries its own workspace's server in its environment and runs bd inside a +// different checkout otherwise reached the wrong server, and the identity +// guard refused (gascity mc-3gbed). The retry is verified exactly as the +// first open was: a project whose declared endpoint serves another project +// too is refused, with the original error. A note on stderr names the switch. +func openWithEnvEndpointFallback(ctx context.Context, cfg *Config, open func(context.Context, *Config) (*DoltStore, error)) (*DoltStore, error) { + store, err := open(ctx, cfg) + if err == nil || !isProjectIdentityMismatch(err) { + return store, err + } + alt, ok := envEndpointFallback(cfg) + if !ok { + return nil, err + } + retry := *cfg + retry.ServerHost, retry.ServerPort, retry.ServerPortSource = alt.host, alt.port, alt.source + altStore, altErr := open(ctx, &retry) + if altErr != nil { + return nil, err + } + if w := envEndpointFallbackNote; w != nil { + fmt.Fprintf(w, "bd: the inherited server %s (BEADS_DOLT_SERVER_PORT) serves another project; using this project's own endpoint %s from %s\n", //nolint:errcheck // best-effort note + net.JoinHostPort(cfg.ServerHost, strconv.Itoa(cfg.ServerPort)), net.JoinHostPort(alt.host, strconv.Itoa(alt.port)), alt.source) + } + *cfg = retry + return altStore, nil +} + +// envEndpointFallback reports the endpoint to retry when cfg's came from the +// environment: the one the project's own .beads explicitly declares +// (config.yaml dolt.port, else metadata.json dolt_server_port, with the +// matching host, 127.0.0.1 by default), when it differs from cfg's. bd's own +// port-file bookkeeping is not a declaration and does not count; nor does a +// socket, a gateway, or the shared server. +func envEndpointFallback(cfg *Config) (declaredEndpoint, bool) { + if cfg == nil || cfg.ServerPortSource != doltserver.PortSourceEnv || cfg.BeadsDir == "" || + cfg.ServerSocket != "" || cfg.Gateway || cfg.Database == doltserver.GlobalDatabaseName || + doltserver.IsSharedServerModeForDir(cfg.BeadsDir) { + return declaredEndpoint{}, false + } + alt, ok := projectDeclaredEndpoint(cfg.BeadsDir) + if !ok || (alt.host == cfg.ServerHost && alt.port == cfg.ServerPort) { + return declaredEndpoint{}, false + } + return alt, true +} + +// projectDeclaredEndpoint reads the endpoint beadsDir's own config explicitly +// declares. +func projectDeclaredEndpoint(beadsDir string) (declaredEndpoint, bool) { + if raw := strings.TrimSpace(config.GetStringFromDir(beadsDir, "dolt.port")); raw != "" { + if port, err := strconv.Atoi(raw); err == nil && port > 0 { + host := strings.TrimSpace(config.GetStringFromDir(beadsDir, "dolt.host")) + return declaredEndpoint{host: hostOrLoopback(host), port: port, source: doltserver.PortSourceConfigYaml}, true + } + } + if meta, err := configfile.Load(beadsDir); err == nil && meta != nil && meta.DoltServerPort > 0 { + return declaredEndpoint{host: hostOrLoopback(meta.DoltServerHost), port: meta.DoltServerPort, source: doltserver.PortSourceMetadataJSON}, true + } + return declaredEndpoint{}, false +} + +func hostOrLoopback(host string) string { + if host = strings.TrimSpace(host); host != "" { + return host + } + return "127.0.0.1" +} diff --git a/internal/storage/dolt/env_endpoint_fallback_test.go b/internal/storage/dolt/env_endpoint_fallback_test.go new file mode 100644 index 000000000..bc2fbf8dd --- /dev/null +++ b/internal/storage/dolt/env_endpoint_fallback_test.go @@ -0,0 +1,152 @@ +package dolt + +import ( + "bytes" + "context" + "errors" + "net" + "os" + "path/filepath" + "strconv" + "strings" + "testing" + + "github.com/steveyegge/beads/internal/doltserver" +) + +// fakeServers answers an open by the endpoint it targets: a store, the +// identity refusal, or another error. Endpoints not listed refuse to connect. +type fakeServers struct { + stores map[string]*DoltStore + foreign map[string]bool // the server holds another project: its id is "other-" + the endpoint + opened []string + otherErrs map[string]error +} + +func (f *fakeServers) open(_ context.Context, cfg *Config) (*DoltStore, error) { + addr := net.JoinHostPort(cfg.ServerHost, strconv.Itoa(cfg.ServerPort)) + f.opened = append(f.opened, addr) + switch { + case f.foreign[addr]: + return nil, projectIdentityMismatchError("local-project", "other-"+addr, "beads", false) + case f.otherErrs[addr] != nil: + return nil, f.otherErrs[addr] + case f.stores[addr] != nil: + return f.stores[addr], nil + } + return nil, errors.New("connection refused") +} + +// beadsDirWith is a .beads dir whose config.yaml holds yaml (none when empty). +func beadsDirWith(t *testing.T, yaml string) string { + t.Helper() + dir := filepath.Join(t.TempDir(), ".beads") + if err := os.MkdirAll(dir, 0o755); err != nil { + t.Fatal(err) + } + if yaml != "" { + if err := os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(yaml), 0o644); err != nil { + t.Fatal(err) + } + } + return dir +} + +func captureFallbackNote(t *testing.T) *bytes.Buffer { + t.Helper() + var buf bytes.Buffer + prev := envEndpointFallbackNote + envEndpointFallbackNote = &buf + t.Cleanup(func() { envEndpointFallbackNote = prev }) + return &buf +} + +// TestEnvEndpointYieldsToTheProjectsDeclaredEndpointOnIdentityMismatch +// (gascity mc-3gbed). Kills the inherited BEADS_DOLT_SERVER_PORT of another +// workspace winning over the project's own explicit endpoint: on the identity +// refusal from the env's server, the project's declared endpoint is opened +// and verified, and used, with a note; a declared endpoint that serves +// another project too is refused with the original error. +func TestEnvEndpointYieldsToTheProjectsDeclaredEndpointOnIdentityMismatch(t *testing.T) { + own := &DoltStore{} + for _, c := range []struct { + name string + yaml string + foreign []string + wantOpen []string + want *DoltStore + }{ + {"the declared endpoint is the project's", "dolt:\n host: 127.0.0.1\n port: 3307\n", []string{"127.0.0.1:45865"}, []string{"127.0.0.1:45865", "127.0.0.1:3307"}, own}, + {"the declared port, the loopback by default", "dolt.port: 3307\n", []string{"127.0.0.1:45865"}, []string{"127.0.0.1:45865", "127.0.0.1:3307"}, own}, + {"the declared endpoint serves another project too", "dolt.port: 3307\n", []string{"127.0.0.1:45865", "127.0.0.1:3307"}, []string{"127.0.0.1:45865", "127.0.0.1:3307"}, nil}, + } { + t.Run(c.name, func(t *testing.T) { + note := captureFallbackNote(t) + f := &fakeServers{stores: map[string]*DoltStore{"127.0.0.1:3307": own}, foreign: map[string]bool{}} + for _, a := range c.foreign { + f.foreign[a] = true + } + cfg := &Config{BeadsDir: beadsDirWith(t, c.yaml), ServerHost: "127.0.0.1", ServerPort: 45865, ServerPortSource: doltserver.PortSourceEnv} + got, err := openWithEnvEndpointFallback(context.Background(), cfg, f.open) + if strings.Join(f.opened, " ") != strings.Join(c.wantOpen, " ") { + t.Fatalf("opened %v, want %v", f.opened, c.wantOpen) + } + if c.want == nil { + if got != nil || !isProjectIdentityMismatch(err) || !strings.Contains(err.Error(), "other-127.0.0.1:45865") || cfg.ServerPort != 45865 || note.Len() != 0 { + t.Fatalf("got %v, %v, cfg port %d, note %q; want the original refusal (the env server's), cfg untouched, no note", got, err, cfg.ServerPort, note.String()) + } + return + } + if err != nil || got != c.want || cfg.ServerPort != 3307 || cfg.ServerPortSource == doltserver.PortSourceEnv || !strings.Contains(note.String(), "127.0.0.1:3307") { + t.Fatalf("got %v, %v, cfg %s:%d (%s), note %q; want the project's store, cfg on its endpoint, a note", got, err, cfg.ServerHost, cfg.ServerPort, cfg.ServerPortSource, note.String()) + } + }) + } +} + +// TestEnvEndpointFallbackStaysNarrow (gascity mc-3gbed). Kills a fallback +// that second-guesses anything but the case it is for: a port that did not +// come from the environment, a project that declares nothing, or declares +// the env's own endpoint, and any error but the identity refusal, all open +// once and return as before. +func TestEnvEndpointFallbackStaysNarrow(t *testing.T) { + for _, c := range []struct { + name string + yaml string + source doltserver.PortSource + other bool // the env's server fails with another error + }{ + {"a port from the project's config", "dolt.port: 3307\n", doltserver.PortSourceConfigYaml, false}, + {"a project that declares nothing", "", doltserver.PortSourceEnv, false}, + {"a project that declares the env's endpoint", "dolt.port: 45865\n", doltserver.PortSourceEnv, false}, + {"another error", "dolt.port: 3307\n", doltserver.PortSourceEnv, true}, + } { + t.Run(c.name, func(t *testing.T) { + note := captureFallbackNote(t) + f := &fakeServers{stores: map[string]*DoltStore{"127.0.0.1:3307": {}}, foreign: map[string]bool{}, otherErrs: map[string]error{}} + if c.other { + f.otherErrs["127.0.0.1:45865"] = errors.New("server down") + } else { + f.foreign["127.0.0.1:45865"] = true + } + cfg := &Config{BeadsDir: beadsDirWith(t, c.yaml), ServerHost: "127.0.0.1", ServerPort: 45865, ServerPortSource: c.source} + if _, err := openWithEnvEndpointFallback(context.Background(), cfg, f.open); err == nil || len(f.opened) != 1 || note.Len() != 0 { + t.Fatalf("err %v, opened %v, note %q; want the first open's error, one open, no note", err, f.opened, note.String()) + } + }) + } +} + +// TestProjectDeclaredEndpointReadsMetadataJSON: a project that declares its +// server in metadata.json (dolt_server_host/port) is honored when its +// config.yaml declares none. +func TestProjectDeclaredEndpointReadsMetadataJSON(t *testing.T) { + dir := beadsDirWith(t, "") + if err := os.WriteFile(filepath.Join(dir, "metadata.json"), []byte(`{"dolt_server_host":"10.0.0.5","dolt_server_port":3307}`), 0o644); err != nil { + t.Fatal(err) + } + got, ok := projectDeclaredEndpoint(dir) + if !ok || got.host != "10.0.0.5" || got.port != 3307 || got.source != doltserver.PortSourceMetadataJSON { + t.Fatalf("declared endpoint %+v, %v; want metadata.json's 10.0.0.5:3307", got, ok) + } +} diff --git a/internal/storage/dolt/store.go b/internal/storage/dolt/store.go index e04352b54..32fd90732 100644 --- a/internal/storage/dolt/store.go +++ b/internal/storage/dolt/store.go @@ -1793,7 +1793,7 @@ func New(ctx context.Context, cfg *Config) (*DoltStore, error) { cfg.Database, addr) } - return newServerMode(ctx, cfg) + return openWithEnvEndpointFallback(ctx, cfg, newServerMode) } // resolveLocalActiveDatabaseDir returns an authoritative local path only for @@ -2451,6 +2451,11 @@ func (s *DoltStore) verifyProjectIdentity(ctx context.Context, beadsDir string, // init instead. Every other open, CreateIfMissing or not, keeps its original // text. func projectIdentityMismatchError(localID, dbID, database string, openedByInit bool) error { + return &projectIdentityMismatch{err: projectIdentityMismatchMessage(localID, dbID, database, openedByInit)} +} + +// projectIdentityMismatchMessage is projectIdentityMismatchError's text. +func projectIdentityMismatchMessage(localID, dbID, database string, openedByInit bool) error { if openedByInit { return fmt.Errorf( "PROJECT IDENTITY MISMATCH — refusing to initialize against an existing database\n\n"+ From 71e905df88da54c43a6ad35576a8ce24cefdca12 Mon Sep 17 00:00:00 2001 From: Julian Knutsen Date: Wed, 14 Oct 2026 21:12:39 +0000 Subject: [PATCH 2/4] fix(dolt): close B1/S1-S5/L1 review gaps on the env-endpoint identity fallback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR #7561 (gascity mc-3gbed) let New() retry a declared endpoint when an env-inherited one fails the project identity check, but review found the fallback still left several holes open: - B1 (blocker): bd serve's provider arm builds its topology straight from resolveDoltServerConnection, never through dolt.New, so it got neither the identity check nor the fallback's correction — serve would migrate and front an inherited env endpoint's database with no check at all. Exported dolt.EnvEndpointDivergesFromDeclared (envEndpointFallback's own eligibility test, without an open) and wired it into resolveServerModeUOWTopologyWithTransportResolver as an outright refusal: serve cannot cheaply verify identity without the migration work this fix deliberately avoids, so it refuses instead of guessing. - S1: the retry now reapplies New()'s BEADS_TEST_MODE=1 production-port guard (rewrite-to-1, then panic if still production), since the retry calls newServerMode directly and bypassed it before. - S2: already satisfied — envEndpointFallback already excludes cfg.CreateIfMissing opens (GH#2336: init/bootstrap ignore config ports). - S3: already satisfied — a failed retry still returns errors.Join(err, altErr), keeping the original identity refusal primary and errors.As-able. - S4: the retry now re-resolves its own user/TLS from configfile.Load instead of carrying the env endpoint's values forward, and refuses to retry onto a non-loopback host. - S5: added tests for the gateway, global-database, shared-server-mode and empty-BeadsDir exclusions, config.yaml-vs-metadata.json precedence, a non-positive yaml port, the retry keeping the declared host (not the env host), New's own wiring through the fallback, the loopback restriction, and the retry's credential/production-port re-resolution. - L1: already satisfied — envEndpointFallback takes the pre-transport- resolution socket value, captured before ResolveSocketTransport can clear it. - Nits: the fallback note now respects --json (debug.SetJSON, wired from main's jsonOutput) and --quiet, and names the env var (BEADS_DOLT_SERVER_PORT vs. the legacy BEADS_DOLT_PORT) that actually supplied the port. newServerModeOpener (a var, like ensureRunningDetailed) lets a test swap New's opener to pin that New always goes through openWithEnvEndpointFallback, without dialing a real dolt sql-server. No schema change. Mutation testing (27 mutants spanning the fallback's exclusions, precedence, retry wiring, and the new S1/S4 guards): 26 killed, 1 survived (an untyped-error wrap inside verifyProjectIdentity's own call site, store.go, pre-existing and outside this diff — needs a live-server integration test to exercise, since it requires an actual mismatching database). Agent-Signature: claude-agent-sdk-claude-sonnet-5-80 on behalf of Julian Knutsen Co-Authored-By: Claude Sonnet 5 --- cmd/bd/main.go | 4 + cmd/bd/uow_factory.go | 18 + cmd/bd/uow_factory_server_mode_test.go | 57 +++ internal/debug/debug.go | 13 + .../storage/dolt/env_endpoint_fallback.go | 167 ++++++- .../dolt/env_endpoint_fallback_test.go | 429 +++++++++++++++++- internal/storage/dolt/store.go | 10 +- 7 files changed, 678 insertions(+), 20 deletions(-) diff --git a/cmd/bd/main.go b/cmd/bd/main.go index 1d3e4410c..a208bd152 100644 --- a/cmd/bd/main.go +++ b/cmd/bd/main.go @@ -1278,6 +1278,10 @@ var rootCmd = &cobra.Command{ WasSet bool }{jsonOutput, true} } + // jsonOutput is now resolved for this command; let lower layers that + // cannot see the flag (e.g. the dolt store's own advisory notes) stay + // out of a machine-readable stream too. + debug.SetJSON(jsonOutput) if !cmd.Root().PersistentFlags().Changed("readonly") { readonlyMode = config.GetBool("readonly") } else { diff --git a/cmd/bd/uow_factory.go b/cmd/bd/uow_factory.go index 576cb5d99..09588efda 100644 --- a/cmd/bd/uow_factory.go +++ b/cmd/bd/uow_factory.go @@ -388,6 +388,24 @@ func resolveServerModeUOWTopologyWithTransportResolver(ctx context.Context, bead if err := resolveDoltServerConnection(ctx, beadsDir, fileCfg, conn); err != nil { return sqlServerUOWTopology{}, err } + + // B1: this topology is built straight from resolveDoltServerConnection, + // not through dolt.New, so it never gets openWithEnvEndpointFallback's + // identity check or its correction. An endpoint inherited from the + // environment (another workspace's server, carried in this shell) would + // otherwise have serve silently migrate and front THAT database over + // HTTP. Refuse instead of guessing: unlike the CLI's own open, a wrong + // guess here is a running server, not a single failed command. + if declared, diverges := dolt.EnvEndpointDivergesFromDeclared(conn); diverges { + return sqlServerUOWTopology{}, fmt.Errorf( + "bd serve: refusing to start: the Dolt server endpoint for %s came from the "+ + "environment (BEADS_DOLT_SERVER_PORT or BEADS_DOLT_PORT) and differs from "+ + "this project's own declared endpoint (%s); this usually means a server "+ + "inherited from another workspace. Unset the env var, or run `bd serve` from "+ + "a shell that does not carry it, so it connects to this project's own server", + beadsDir, declared) + } + if conn.Gateway { // Unreachable while a configured command is the only thing that can set // Gateway (dolt.ApplyGatewayCredential is its sole writer, and the check diff --git a/cmd/bd/uow_factory_server_mode_test.go b/cmd/bd/uow_factory_server_mode_test.go index e8279d761..c3bff6cb0 100644 --- a/cmd/bd/uow_factory_server_mode_test.go +++ b/cmd/bd/uow_factory_server_mode_test.go @@ -84,6 +84,63 @@ func TestResolveServerModeUOWTopology_GlobalSelectsTheGlobalDatabase(t *testing. assert.Equal(t, "beads_global", topology.database) } +// B1: an endpoint inherited from the environment (another workspace's server, +// carried in this shell) must refuse rather than silently serve whatever +// database that endpoint happens to answer with. This topology is built +// straight from resolveDoltServerConnection, never through dolt.New, so it +// gets none of openWithEnvEndpointFallback's identity check or correction — +// serve has to refuse outright instead (gascity mc-3gbed). +func TestResolveServerModeUOWTopology_RefusesWhenEnvEndpointDivergesFromDeclared(t *testing.T) { + beadsDir := serverModeBeadsDir(t, &configfile.Config{ + DoltServerHost: "127.0.0.1", + DoltServerPort: 3521, + DoltDatabase: "beads_serve", + }) + t.Setenv("BEADS_DOLT_SERVER_PORT", "45923") + + _, err := resolveServerModeUOWTopology(context.Background(), beadsDir) + require.Error(t, err) + assert.Contains(t, err.Error(), "refusing to start") + assert.Contains(t, err.Error(), "BEADS_DOLT_SERVER_PORT") + assert.Contains(t, err.Error(), "127.0.0.1:3521", + "the refusal must name this project's own declared endpoint, not just say one exists") +} + +// The environment and the declared endpoint agreeing is the ordinary case — +// an operator's shell whose BEADS_DOLT_SERVER_PORT happens to match this +// project's own config.yaml/metadata.json — and must resolve exactly as +// before the B1 fix. +func TestResolveServerModeUOWTopology_AllowsEnvEndpointMatchingDeclared(t *testing.T) { + beadsDir := serverModeBeadsDir(t, &configfile.Config{ + DoltServerHost: "127.0.0.1", + DoltServerPort: 3521, + DoltDatabase: "beads_serve", + }) + t.Setenv("BEADS_DOLT_SERVER_PORT", "3521") + + topology, err := resolveServerModeUOWTopology(context.Background(), beadsDir) + require.NoError(t, err) + assert.Equal(t, 3521, topology.external.Port) +} + +// Shared-server mode is one server fronting every project by design, not a +// cross-project leak: envEndpointFallback already excludes it (S5's +// exclusion coverage), and this refusal must agree rather than block the +// documented shared-server deployment. +func TestResolveServerModeUOWTopology_SharedServerModeIsExcludedFromTheRefusal(t *testing.T) { + beadsDir := serverModeBeadsDir(t, &configfile.Config{ + DoltServerHost: "127.0.0.1", + DoltServerPort: 3521, + DoltDatabase: "beads_project", + }) + t.Setenv("BEADS_DOLT_SHARED_SERVER", "1") + t.Setenv("BEADS_DOLT_SERVER_PORT", "45923") + + _, err := resolveServerModeUOWTopology(context.Background(), beadsDir) + require.NoError(t, err, + "shared-server mode's one server for every project is not a cross-project leak; envEndpointFallback excludes it the same way") +} + // The no-metadata.json corner. The CLI store open in this same process takes // the literal default database name there (main.go's cfg == nil branch), and // the whole point of routing serve through the same resolution is that the two diff --git a/internal/debug/debug.go b/internal/debug/debug.go index 59a094e70..7440c0b25 100644 --- a/internal/debug/debug.go +++ b/internal/debug/debug.go @@ -9,6 +9,7 @@ var ( enabled = os.Getenv("BD_DEBUG") != "" verboseMode = false quietMode = false + jsonMode = false ) func Enabled() bool { @@ -30,6 +31,18 @@ func IsQuiet() bool { return quietMode } +// SetJSON marks that the command's output is machine-readable (--json or +// --format json). Callers outside the normal output path (e.g. an advisory +// note on a fallback) use IsJSON to stay out of a machine-readable stream. +func SetJSON(json bool) { + jsonMode = json +} + +// IsJSON returns true if JSON output mode is enabled. +func IsJSON() bool { + return jsonMode +} + func Logf(format string, args ...interface{}) { if enabled || verboseMode { fmt.Fprintf(os.Stderr, format, args...) diff --git a/internal/storage/dolt/env_endpoint_fallback.go b/internal/storage/dolt/env_endpoint_fallback.go index 65eaed3e1..6ef30da19 100644 --- a/internal/storage/dolt/env_endpoint_fallback.go +++ b/internal/storage/dolt/env_endpoint_fallback.go @@ -9,9 +9,11 @@ import ( "os" "strconv" "strings" + "sync" "github.com/steveyegge/beads/internal/config" "github.com/steveyegge/beads/internal/configfile" + "github.com/steveyegge/beads/internal/debug" "github.com/steveyegge/beads/internal/doltserver" ) @@ -40,6 +42,14 @@ type declaredEndpoint struct { // capture it. var envEndpointFallbackNote io.Writer = os.Stderr +// envEndpointFallbackNoteOnce makes the note fire at most once per process: +// a long-running or multi-store process (e.g. a global database open +// alongside a project one) that keeps hitting the same inherited env endpoint +// would otherwise repeat the same advisory on every open. A pointer (not a +// plain sync.Once) so tests that need a fresh note per case can swap it via +// captureFallbackNote without copying the lock sync.Once embeds. +var envEndpointFallbackNoteOnce = &sync.Once{} + // openWithEnvEndpointFallback opens cfg with open and, when the open is // refused for a project identity mismatch on an endpoint that came from the // environment (BEADS_DOLT_SERVER_PORT, an agent's inherited server for @@ -51,58 +61,160 @@ var envEndpointFallbackNote io.Writer = os.Stderr // first open was: a project whose declared endpoint serves another project // too is refused, with the original error. A note on stderr names the switch. func openWithEnvEndpointFallback(ctx context.Context, cfg *Config, open func(context.Context, *Config) (*DoltStore, error)) (*DoltStore, error) { + // L1: cfg.ServerSocket is a shared field; open's own socket-first/ + // TCP-fallback logic (ResolveSocketTransport, gt-28itz) may clear it as a + // side effect of the call below, when a configured socket is dead but TCP + // is live. Captured here, before that mutation, so a project that + // explicitly configured a socket stays excluded from this fallback even + // though cfg.ServerSocket reads "" by the time envEndpointFallback would + // otherwise see it. + originalSocket := cfg.ServerSocket + store, err := open(ctx, cfg) if err == nil || !isProjectIdentityMismatch(err) { return store, err } - alt, ok := envEndpointFallback(cfg) + alt, ok := envEndpointFallback(cfg, originalSocket) if !ok { return nil, err } retry := *cfg retry.ServerHost, retry.ServerPort, retry.ServerPortSource = alt.host, alt.port, alt.source + + // S4: re-resolve the declared endpoint's own user/password/TLS instead of + // carrying the env endpoint's forward. Replaying another server's + // credentials onto this one is the same shape of cross-project leak this + // fallback exists to fix, and the two servers are not guaranteed to share + // an operator, let alone a password. + fileCfg, _ := configfile.Load(cfg.BeadsDir) + if fileCfg == nil { + // No metadata.json (or it failed to load): fall back to the same + // zero-value config every other reader of a missing metadata.json + // uses (open.go's applyResolvedConfig), so GetDoltServerUser/TLS still + // resolve their own documented defaults instead of silently leaving + // the env endpoint's values in place. + fileCfg = configfile.DefaultConfig() + } + retry.ServerUser = fileCfg.GetDoltServerUser() + retry.ServerTLS = fileCfg.GetDoltServerTLS() + if envPass := os.Getenv("BEADS_DOLT_PASSWORD"); envPass != "" { + // An explicit env override is a deliberate operator action, not an + // ambient value inherited from another workspace; honor it for the + // retry the same way applyConfigDefaults honors it for the first + // open. Looked up directly (not via the alt endpoint's own + // GetDoltServerPasswordForPort) so the retry's password is keyed on + // alt.host:alt.port specifically, not whatever host cfg's file + // resolves on its own. + retry.ServerPassword = envPass + } else { + retry.ServerPassword = configfile.LookupCredentialsPassword(alt.host, alt.port) + } + + // S1: New()'s BEADS_TEST_MODE=1 production-port guard (applyConfigDefaults' + // rewrite-to-1, then New()'s defense-in-depth panic if still production) + // ran once already, against cfg, before this fallback was ever reached. + // This retry calls open directly (newServerMode, not New), bypassing both + // a second time, so they are reapplied here verbatim: a test process must + // never land the retry on a production port either. + if os.Getenv("BEADS_TEST_MODE") == "1" { + if retry.ServerPort == 0 || isProductionPort(&retry) { + retry.ServerPort = 1 + } + if isProductionPort(&retry) { + panic(buildTestModeProductionPortPanic(&retry)) + } + } + altStore, altErr := open(ctx, &retry) if altErr != nil { - return nil, err + // S3: the identity refusal from the env endpoint stays the primary, + // actionable error (an inherited server for another project); the + // retry's own failure is attached rather than silently discarded, + // and both stay reachable with errors.As. + return nil, errors.Join(err, altErr) } - if w := envEndpointFallbackNote; w != nil { - fmt.Fprintf(w, "bd: the inherited server %s (BEADS_DOLT_SERVER_PORT) serves another project; using this project's own endpoint %s from %s\n", //nolint:errcheck // best-effort note - net.JoinHostPort(cfg.ServerHost, strconv.Itoa(cfg.ServerPort)), net.JoinHostPort(alt.host, strconv.Itoa(alt.port)), alt.source) + if w := envEndpointFallbackNote; w != nil && !debug.IsQuiet() && !debug.IsJSON() { + envEndpointFallbackNoteOnce.Do(func() { + // retry.ServerHost/Port, not alt.host/port: the S1 guard above may + // have rewritten retry.ServerPort (BEADS_TEST_MODE=1) after alt was + // computed, and the note must name the endpoint actually used. + fmt.Fprintf(w, "bd: the inherited server %s (%s) serves another project; using this project's own endpoint %s from %s\n", //nolint:errcheck // best-effort note + net.JoinHostPort(cfg.ServerHost, strconv.Itoa(cfg.ServerPort)), envEndpointPortVarName(), net.JoinHostPort(retry.ServerHost, strconv.Itoa(retry.ServerPort)), alt.source) + }) } *cfg = retry return altStore, nil } +// envEndpointPortVarName names the environment variable that actually +// supplied cfg's port, for the fallback note. BEADS_DOLT_SERVER_PORT is +// checked first, matching applyConfigDefaults' own precedence; +// BEADS_DOLT_PORT is the legacy fallback. envEndpointFallback already +// requires cfg.ServerPortSource == doltserver.PortSourceEnv before this is +// reached, so one of the two was set. +func envEndpointPortVarName() string { + if os.Getenv("BEADS_DOLT_SERVER_PORT") != "" { + return "BEADS_DOLT_SERVER_PORT" + } + return "BEADS_DOLT_PORT" +} + // envEndpointFallback reports the endpoint to retry when cfg's came from the // environment: the one the project's own .beads explicitly declares // (config.yaml dolt.port, else metadata.json dolt_server_port, with the // matching host, 127.0.0.1 by default), when it differs from cfg's. bd's own // port-file bookkeeping is not a declaration and does not count; nor does a -// socket, a gateway, or the shared server. -func envEndpointFallback(cfg *Config) (declaredEndpoint, bool) { +// socket, a gateway, the shared server, or an open that may create the +// database (S2: init and bootstrap deliberately ignore config ports, GH#2336, +// and this fallback must never be the thing that creates a database on a +// server nobody declared). originalSocket is cfg.ServerSocket as it stood +// before the first open attempt, since open's own transport resolution may +// have since cleared it (L1). +func envEndpointFallback(cfg *Config, originalSocket string) (declaredEndpoint, bool) { if cfg == nil || cfg.ServerPortSource != doltserver.PortSourceEnv || cfg.BeadsDir == "" || - cfg.ServerSocket != "" || cfg.Gateway || cfg.Database == doltserver.GlobalDatabaseName || - doltserver.IsSharedServerModeForDir(cfg.BeadsDir) { + originalSocket != "" || cfg.Gateway || cfg.Database == doltserver.GlobalDatabaseName || + cfg.CreateIfMissing || doltserver.IsSharedServerModeForDir(cfg.BeadsDir) { return declaredEndpoint{}, false } alt, ok := projectDeclaredEndpoint(cfg.BeadsDir) if !ok || (alt.host == cfg.ServerHost && alt.port == cfg.ServerPort) { return declaredEndpoint{}, false } + // S4: never retry onto a non-loopback host. The declared endpoint is read + // from this project's own config, which bd otherwise trusts, but this + // fallback fires only on an identity refusal — exactly when something is + // already not as expected — so it stays inside the narrowest blast radius + // available: the machine bd is already running on. + if !isLocalHost(alt.host) { + return declaredEndpoint{}, false + } return alt, true } // projectDeclaredEndpoint reads the endpoint beadsDir's own config explicitly // declares. func projectDeclaredEndpoint(beadsDir string) (declaredEndpoint, bool) { + meta, _ := configfile.Load(beadsDir) + // declaredHost mirrors bd's normal host precedence — metadata.json + // dolt_server_host before config.yaml dolt.host, matching + // configfile.Config.GetDoltServerHost — minus the environment. + // BEADS_DOLT_SERVER_HOST is exactly the kind of value inherited from + // another workspace that this fallback exists to escape; consulting it + // here would make the declared and the env-resolved endpoint agree only + // by coincidence (S4). + declaredHost := func() string { + if meta != nil && meta.DoltServerHost != "" { + return meta.DoltServerHost + } + return strings.TrimSpace(config.GetStringFromDir(beadsDir, "dolt.host")) + } if raw := strings.TrimSpace(config.GetStringFromDir(beadsDir, "dolt.port")); raw != "" { if port, err := strconv.Atoi(raw); err == nil && port > 0 { - host := strings.TrimSpace(config.GetStringFromDir(beadsDir, "dolt.host")) - return declaredEndpoint{host: hostOrLoopback(host), port: port, source: doltserver.PortSourceConfigYaml}, true + return declaredEndpoint{host: hostOrLoopback(declaredHost()), port: port, source: doltserver.PortSourceConfigYaml}, true } } - if meta, err := configfile.Load(beadsDir); err == nil && meta != nil && meta.DoltServerPort > 0 { - return declaredEndpoint{host: hostOrLoopback(meta.DoltServerHost), port: meta.DoltServerPort, source: doltserver.PortSourceMetadataJSON}, true + if meta != nil && meta.DoltServerPort > 0 { + return declaredEndpoint{host: hostOrLoopback(declaredHost()), port: meta.DoltServerPort, source: doltserver.PortSourceMetadataJSON}, true } return declaredEndpoint{}, false } @@ -113,3 +225,32 @@ func hostOrLoopback(host string) string { } return "127.0.0.1" } + +// EnvEndpointDivergesFromDeclared reports whether cfg's server endpoint came +// from the environment (BEADS_DOLT_SERVER_PORT / legacy BEADS_DOLT_PORT) and +// differs from the endpoint this project's own .beads explicitly declares — +// envEndpointFallback's same eligibility test, without an actual open. +// +// Exported for bd serve's provider arm (cmd/bd/uow_factory.go's +// resolveServerModeUOWTopologyWithTransportResolver), which builds its own +// topology straight from resolveDoltServerConnection rather than through +// dolt.New, so it gets neither openWithEnvEndpointFallback's automatic +// correction nor the identity check that guards it: left alone, serve would +// run MigrateUpWithLock against — and then front an HTTP server on — whatever +// database the inherited endpoint happens to answer with, no project-identity +// check at all (B1, gascity mc-3gbed). serve refuses outright instead, since +// confirming an actual identity mismatch here would need the same migration +// + verification work this narrower fix is deliberately avoiding. +// +// cfg.ServerSocket must be the value resolveDoltServerConnection produced, +// unresolved against the live transport: unlike newServerMode's socket-first/ +// TCP-fallback (ResolveSocketTransport), the caller's own transport +// resolution keeps its result in a local variable and never mutates +// cfg.ServerSocket, so there is no earlier snapshot to thread through here. +func EnvEndpointDivergesFromDeclared(cfg *Config) (declared string, ok bool) { + alt, found := envEndpointFallback(cfg, cfg.ServerSocket) + if !found { + return "", false + } + return net.JoinHostPort(alt.host, strconv.Itoa(alt.port)), true +} diff --git a/internal/storage/dolt/env_endpoint_fallback_test.go b/internal/storage/dolt/env_endpoint_fallback_test.go index bc2fbf8dd..d42b2fa13 100644 --- a/internal/storage/dolt/env_endpoint_fallback_test.go +++ b/internal/storage/dolt/env_endpoint_fallback_test.go @@ -9,8 +9,10 @@ import ( "path/filepath" "strconv" "strings" + "sync" "testing" + "github.com/steveyegge/beads/internal/debug" "github.com/steveyegge/beads/internal/doltserver" ) @@ -20,12 +22,14 @@ type fakeServers struct { stores map[string]*DoltStore foreign map[string]bool // the server holds another project: its id is "other-" + the endpoint opened []string + openedCfg []Config // a copy of cfg as open saw it, same index as opened otherErrs map[string]error } func (f *fakeServers) open(_ context.Context, cfg *Config) (*DoltStore, error) { addr := net.JoinHostPort(cfg.ServerHost, strconv.Itoa(cfg.ServerPort)) f.opened = append(f.opened, addr) + f.openedCfg = append(f.openedCfg, *cfg) switch { case f.foreign[addr]: return nil, projectIdentityMismatchError("local-project", "other-"+addr, "beads", false) @@ -56,8 +60,13 @@ func captureFallbackNote(t *testing.T) *bytes.Buffer { t.Helper() var buf bytes.Buffer prev := envEndpointFallbackNote + prevOnce := envEndpointFallbackNoteOnce envEndpointFallbackNote = &buf - t.Cleanup(func() { envEndpointFallbackNote = prev }) + envEndpointFallbackNoteOnce = &sync.Once{} + t.Cleanup(func() { + envEndpointFallbackNote = prev + envEndpointFallbackNoteOnce = prevOnce + }) return &buf } @@ -76,13 +85,18 @@ func TestEnvEndpointYieldsToTheProjectsDeclaredEndpointOnIdentityMismatch(t *tes wantOpen []string want *DoltStore }{ - {"the declared endpoint is the project's", "dolt:\n host: 127.0.0.1\n port: 3307\n", []string{"127.0.0.1:45865"}, []string{"127.0.0.1:45865", "127.0.0.1:3307"}, own}, - {"the declared port, the loopback by default", "dolt.port: 3307\n", []string{"127.0.0.1:45865"}, []string{"127.0.0.1:45865", "127.0.0.1:3307"}, own}, - {"the declared endpoint serves another project too", "dolt.port: 3307\n", []string{"127.0.0.1:45865", "127.0.0.1:3307"}, []string{"127.0.0.1:45865", "127.0.0.1:3307"}, nil}, + // 3308, not Dolt's historical default 3307: this suite's TestMain sets + // BEADS_TEST_MODE=1 for the whole package, and the production-port + // guard (S1) now applies to the retry too — 3307 is flagged + // unconditionally (DefaultSQLPort) and would be rewritten to 1 before + // the retry ever dials it, which is a different test than this one. + {"the declared endpoint is the project's", "dolt:\n host: 127.0.0.1\n port: 3308\n", []string{"127.0.0.1:45865"}, []string{"127.0.0.1:45865", "127.0.0.1:3308"}, own}, + {"the declared port, the loopback by default", "dolt.port: 3308\n", []string{"127.0.0.1:45865"}, []string{"127.0.0.1:45865", "127.0.0.1:3308"}, own}, + {"the declared endpoint serves another project too", "dolt.port: 3308\n", []string{"127.0.0.1:45865", "127.0.0.1:3308"}, []string{"127.0.0.1:45865", "127.0.0.1:3308"}, nil}, } { t.Run(c.name, func(t *testing.T) { note := captureFallbackNote(t) - f := &fakeServers{stores: map[string]*DoltStore{"127.0.0.1:3307": own}, foreign: map[string]bool{}} + f := &fakeServers{stores: map[string]*DoltStore{"127.0.0.1:3308": own}, foreign: map[string]bool{}} for _, a := range c.foreign { f.foreign[a] = true } @@ -97,7 +111,7 @@ func TestEnvEndpointYieldsToTheProjectsDeclaredEndpointOnIdentityMismatch(t *tes } return } - if err != nil || got != c.want || cfg.ServerPort != 3307 || cfg.ServerPortSource == doltserver.PortSourceEnv || !strings.Contains(note.String(), "127.0.0.1:3307") { + if err != nil || got != c.want || cfg.ServerPort != 3308 || cfg.ServerPortSource == doltserver.PortSourceEnv || !strings.Contains(note.String(), "127.0.0.1:3308") { t.Fatalf("got %v, %v, cfg %s:%d (%s), note %q; want the project's store, cfg on its endpoint, a note", got, err, cfg.ServerHost, cfg.ServerPort, cfg.ServerPortSource, note.String()) } }) @@ -150,3 +164,406 @@ func TestProjectDeclaredEndpointReadsMetadataJSON(t *testing.T) { t.Fatalf("declared endpoint %+v, %v; want metadata.json's 10.0.0.5:3307", got, ok) } } + +// TestProjectDeclaredEndpointHostFollowsNormalPrecedence (S4). config.yaml's +// dolt.port names only the port; the host comes from metadata.json's +// dolt_server_host (bd's normal host precedence — configfile.Config. +// GetDoltServerHost puts metadata.json ahead of config.yaml), not the +// loopback default, and never from BEADS_DOLT_SERVER_HOST: that env var is +// exactly the kind of value inherited from another workspace this fallback +// exists to escape. +func TestProjectDeclaredEndpointHostFollowsNormalPrecedence(t *testing.T) { + t.Setenv("BEADS_DOLT_SERVER_HOST", "10.9.9.9") // must be ignored + dir := beadsDirWith(t, "dolt.port: 3308\n") + if err := os.WriteFile(filepath.Join(dir, "metadata.json"), []byte(`{"dolt_server_host":"10.0.0.5"}`), 0o644); err != nil { + t.Fatal(err) + } + got, ok := projectDeclaredEndpoint(dir) + if !ok || got.host != "10.0.0.5" || got.port != 3308 || got.source != doltserver.PortSourceConfigYaml { + t.Fatalf("declared endpoint %+v, %v; want metadata.json's host 10.0.0.5 with config.yaml's port 3308", got, ok) + } +} + +// TestEnvEndpointFallbackExcludesCreateIfMissing (S2). A CreateIfMissing open +// (bd init, bd bootstrap) must never fall back onto a database that may not +// exist yet on the project's declared endpoint — init and bootstrap +// deliberately ignore config ports (GH#2336), and the fallback replaying that +// ignored port would reintroduce exactly what GH#2336 fixed. +func TestEnvEndpointFallbackExcludesCreateIfMissing(t *testing.T) { + note := captureFallbackNote(t) + f := &fakeServers{stores: map[string]*DoltStore{"127.0.0.1:3308": {}}, foreign: map[string]bool{"127.0.0.1:45865": true}} + cfg := &Config{ + BeadsDir: beadsDirWith(t, "dolt.port: 3308\n"), ServerHost: "127.0.0.1", ServerPort: 45865, + ServerPortSource: doltserver.PortSourceEnv, CreateIfMissing: true, + } + if _, err := openWithEnvEndpointFallback(context.Background(), cfg, f.open); err == nil || len(f.opened) != 1 || note.Len() != 0 { + t.Fatalf("err %v, opened %v, note %q; want the first open's error, one open, no note", err, f.opened, note.String()) + } +} + +// TestEnvEndpointFallbackExcludesConfiguredSocket (L1). A project configured +// for a unix socket must stay excluded from the fallback even though open's +// own socket-first/TCP-fallback resolution (ResolveSocketTransport) clears +// cfg.ServerSocket to "" as a side effect of the first open when the socket +// is dead but TCP is live — the exclusion must read the socket as it stood +// BEFORE that mutation, not after. +func TestEnvEndpointFallbackExcludesConfiguredSocket(t *testing.T) { + note := captureFallbackNote(t) + var opens int + open := func(_ context.Context, cfg *Config) (*DoltStore, error) { + opens++ + // Simulate newServerMode's own socket-first/TCP-fallback: the socket + // is dead, TCP is live, so cfg.ServerSocket is cleared in place before + // the identity check runs and refuses. + cfg.ServerSocket = "" + return nil, projectIdentityMismatchError("local-project", "other-env", "beads", false) + } + cfg := &Config{ + BeadsDir: beadsDirWith(t, "dolt.port: 3308\n"), ServerHost: "127.0.0.1", ServerPort: 45865, + ServerPortSource: doltserver.PortSourceEnv, ServerSocket: "/tmp/does-not-matter.sock", + } + if _, err := openWithEnvEndpointFallback(context.Background(), cfg, open); err == nil || opens != 1 || note.Len() != 0 { + t.Fatalf("err %v, opens %d, note %q; want the first open's error, one open, no note (socket-configured project excluded)", err, opens, note.String()) + } +} + +// TestEnvEndpointFallbackRestrictsToLoopbackHosts (S4). A project whose +// declared endpoint is not on this machine is never dialed by the fallback: +// an identity refusal is already a sign something is not as expected, and the +// fallback keeps to the narrowest blast radius available. +func TestEnvEndpointFallbackRestrictsToLoopbackHosts(t *testing.T) { + note := captureFallbackNote(t) + f := &fakeServers{stores: map[string]*DoltStore{"10.0.0.9:3308": {}}, foreign: map[string]bool{"127.0.0.1:45865": true}} + cfg := &Config{ + BeadsDir: beadsDirWith(t, "dolt:\n host: 10.0.0.9\n port: 3308\n"), ServerHost: "127.0.0.1", ServerPort: 45865, + ServerPortSource: doltserver.PortSourceEnv, + } + if _, err := openWithEnvEndpointFallback(context.Background(), cfg, f.open); err == nil || len(f.opened) != 1 || note.Len() != 0 { + t.Fatalf("err %v, opened %v, note %q; want the first open's error, one open, no note (non-loopback declared host excluded)", err, f.opened, note.String()) + } +} + +// TestEnvEndpointFallbackExcludesGateway (S5). A gateway config (a configured +// credential command mints a short-lived token keyed to the ORIGINAL +// endpoint's identity) must stay excluded: retrying onto a different endpoint +// with that same token is not a correction, it is a misuse of someone else's +// credential. +func TestEnvEndpointFallbackExcludesGateway(t *testing.T) { + note := captureFallbackNote(t) + f := &fakeServers{stores: map[string]*DoltStore{"127.0.0.1:3308": {}}, foreign: map[string]bool{"127.0.0.1:45865": true}} + cfg := &Config{ + BeadsDir: beadsDirWith(t, "dolt.port: 3308\n"), ServerHost: "127.0.0.1", ServerPort: 45865, + ServerPortSource: doltserver.PortSourceEnv, Gateway: true, + } + if _, err := openWithEnvEndpointFallback(context.Background(), cfg, f.open); err == nil || len(f.opened) != 1 || note.Len() != 0 { + t.Fatalf("err %v, opened %v, note %q; want the first open's error, one open, no note (gateway excluded)", err, f.opened, note.String()) + } +} + +// TestEnvEndpointFallbackExcludesGlobalDatabase (S5). The shared global +// database is not a per-project one, so there is no project-specific +// "declared endpoint" to retry onto in the first place. +func TestEnvEndpointFallbackExcludesGlobalDatabase(t *testing.T) { + note := captureFallbackNote(t) + f := &fakeServers{stores: map[string]*DoltStore{"127.0.0.1:3308": {}}, foreign: map[string]bool{"127.0.0.1:45865": true}} + cfg := &Config{ + BeadsDir: beadsDirWith(t, "dolt.port: 3308\n"), ServerHost: "127.0.0.1", ServerPort: 45865, + ServerPortSource: doltserver.PortSourceEnv, Database: doltserver.GlobalDatabaseName, + } + if _, err := openWithEnvEndpointFallback(context.Background(), cfg, f.open); err == nil || len(f.opened) != 1 || note.Len() != 0 { + t.Fatalf("err %v, opened %v, note %q; want the first open's error, one open, no note (global database excluded)", err, f.opened, note.String()) + } +} + +// TestEnvEndpointFallbackExcludesSharedServerMode (S5). Shared-server mode is +// one server fronting every project by design, not a cross-project leak; the +// fallback must not second-guess that deployment's own one declared endpoint. +func TestEnvEndpointFallbackExcludesSharedServerMode(t *testing.T) { + t.Setenv("BEADS_DOLT_SHARED_SERVER", "1") + note := captureFallbackNote(t) + f := &fakeServers{stores: map[string]*DoltStore{"127.0.0.1:3308": {}}, foreign: map[string]bool{"127.0.0.1:45865": true}} + cfg := &Config{ + BeadsDir: beadsDirWith(t, "dolt.port: 3308\n"), ServerHost: "127.0.0.1", ServerPort: 45865, + ServerPortSource: doltserver.PortSourceEnv, + } + if _, err := openWithEnvEndpointFallback(context.Background(), cfg, f.open); err == nil || len(f.opened) != 1 || note.Len() != 0 { + t.Fatalf("err %v, opened %v, note %q; want the first open's error, one open, no note (shared-server mode excluded)", err, f.opened, note.String()) + } +} + +// TestEnvEndpointFallbackExcludesEmptyBeadsDir (S5). Nothing is "declared" +// without a workspace to declare it; cfg.BeadsDir == "" must not panic or +// fall through to some other project's config.yaml/metadata.json by accident. +// config.yaml/metadata.json are read via filepath.Join(beadsDir, ...), so an +// empty beadsDir resolves to the process's current directory: this test +// chdirs into a directory that genuinely declares an endpoint (port 3308) to +// prove the exclusion is a real guard and not just an absence of files to +// stumble on. +func TestEnvEndpointFallbackExcludesEmptyBeadsDir(t *testing.T) { + note := captureFallbackNote(t) + dir := beadsDirWith(t, "dolt.port: 3308\n") + t.Chdir(dir) + f := &fakeServers{stores: map[string]*DoltStore{"127.0.0.1:3308": {}}, foreign: map[string]bool{"127.0.0.1:45865": true}} + cfg := &Config{ + BeadsDir: "", ServerHost: "127.0.0.1", ServerPort: 45865, + ServerPortSource: doltserver.PortSourceEnv, + } + if _, err := openWithEnvEndpointFallback(context.Background(), cfg, f.open); err == nil || len(f.opened) != 1 || note.Len() != 0 { + t.Fatalf("err %v, opened %v, note %q; want the first open's error, one open, no note (empty BeadsDir excluded even though cwd declares port 3308)", err, f.opened, note.String()) + } +} + +// TestProjectDeclaredEndpointPrefersConfigYamlOverMetadataJSON (S5). When +// both config.yaml's dolt.port and metadata.json's dolt_server_port are +// present and disagree, config.yaml wins — matching envEndpointFallback's own +// doc comment ("config.yaml dolt.port, else metadata.json dolt_server_port"). +func TestProjectDeclaredEndpointPrefersConfigYamlOverMetadataJSON(t *testing.T) { + dir := beadsDirWith(t, "dolt.port: 3308\n") + if err := os.WriteFile(filepath.Join(dir, "metadata.json"), []byte(`{"dolt_server_host":"10.0.0.5","dolt_server_port":9999}`), 0o644); err != nil { + t.Fatal(err) + } + got, ok := projectDeclaredEndpoint(dir) + if !ok || got.port != 3308 || got.source != doltserver.PortSourceConfigYaml { + t.Fatalf("declared endpoint %+v, %v; want config.yaml's port 3308 to win over metadata.json's 9999", got, ok) + } +} + +// TestProjectDeclaredEndpointRejectsNonPositiveYamlPort (S5). dolt.port: 0 (or +// a negative value) in config.yaml is not a declaration — it must fall +// through to metadata.json rather than being read as "port 0, declared". +func TestProjectDeclaredEndpointRejectsNonPositiveYamlPort(t *testing.T) { + dir := beadsDirWith(t, "dolt.port: 0\n") + if err := os.WriteFile(filepath.Join(dir, "metadata.json"), []byte(`{"dolt_server_host":"10.0.0.5","dolt_server_port":3307}`), 0o644); err != nil { + t.Fatal(err) + } + got, ok := projectDeclaredEndpoint(dir) + if !ok || got.port != 3307 || got.source != doltserver.PortSourceMetadataJSON { + t.Fatalf("declared endpoint %+v, %v; want dolt.port: 0 ignored, falling through to metadata.json's 3307", got, ok) + } +} + +// TestEnvEndpointFallbackRetriesTheDeclaredHostNotTheEnvHost (S5/S4). The +// retry's host must come from the declared endpoint, not be left over from +// the env-resolved cfg: a declared host that is a different loopback spelling +// ("localhost" vs the env's "127.0.0.1") proves the retry actually dials the +// declared host, not merely the declared port against the inherited host. +func TestEnvEndpointFallbackRetriesTheDeclaredHostNotTheEnvHost(t *testing.T) { + note := captureFallbackNote(t) + f := &fakeServers{stores: map[string]*DoltStore{"localhost:3308": {}}, foreign: map[string]bool{"127.0.0.1:45865": true}} + cfg := &Config{ + BeadsDir: beadsDirWith(t, "dolt:\n host: localhost\n port: 3308\n"), ServerHost: "127.0.0.1", ServerPort: 45865, + ServerPortSource: doltserver.PortSourceEnv, + } + got, err := openWithEnvEndpointFallback(context.Background(), cfg, f.open) + wantOpened := []string{"127.0.0.1:45865", "localhost:3308"} + if strings.Join(f.opened, " ") != strings.Join(wantOpened, " ") { + t.Fatalf("opened %v, want %v (the retry must dial the declared host \"localhost\", not the inherited \"127.0.0.1\")", f.opened, wantOpened) + } + if err != nil || got == nil || cfg.ServerHost != "localhost" { + t.Fatalf("got %v, err %v, cfg host %q; want the retry to succeed on the declared host", got, err, cfg.ServerHost) + } + if note.Len() == 0 { + t.Fatal("want a fallback note") + } +} + +// TestNewRoutesThroughEnvEndpointFallback (S5, B1 "the wiring in New"). New +// must always open through openWithEnvEndpointFallback — never call the +// server-mode opener directly — since that wrapper is the only thing that +// performs this retry at all. newServerModeOpener is stubbed to a fake so +// nothing here dials a real dolt sql-server. +func TestNewRoutesThroughEnvEndpointFallback(t *testing.T) { + captureFallbackNote(t) + prevOpener := newServerModeOpener + t.Cleanup(func() { newServerModeOpener = prevOpener }) + own := &DoltStore{} + f := &fakeServers{stores: map[string]*DoltStore{"127.0.0.1:3308": own}, foreign: map[string]bool{"127.0.0.1:45923": true}} + newServerModeOpener = f.open + + cfg := &Config{ + Path: filepath.Join(t.TempDir(), "dolt-data"), + BeadsDir: beadsDirWith(t, "dolt.port: 3308\n"), + ServerHost: "127.0.0.1", + ServerPort: 45923, + ServerPortSource: doltserver.PortSourceEnv, + } + got, err := New(context.Background(), cfg) + wantOpened := []string{"127.0.0.1:45923", "127.0.0.1:3308"} + if strings.Join(f.opened, " ") != strings.Join(wantOpened, " ") { + t.Fatalf("opened %v, want %v; New must retry through openWithEnvEndpointFallback, not call its opener directly", f.opened, wantOpened) + } + if err != nil || got != own { + t.Fatalf("got %v, err %v; want New to return the project's own declared-endpoint store", got, err) + } +} + +// TestEnvEndpointFallbackJoinsRetryError (S3). When the retry also refuses on +// identity, the env endpoint's refusal stays the primary, actionable error, +// but the retry's own refusal is attached rather than discarded, and both +// stay reachable with errors.As. +func TestEnvEndpointFallbackJoinsRetryError(t *testing.T) { + note := captureFallbackNote(t) + f := &fakeServers{foreign: map[string]bool{"127.0.0.1:45865": true, "127.0.0.1:3308": true}} + cfg := &Config{ + BeadsDir: beadsDirWith(t, "dolt.port: 3308\n"), ServerHost: "127.0.0.1", ServerPort: 45865, + ServerPortSource: doltserver.PortSourceEnv, + } + _, err := openWithEnvEndpointFallback(context.Background(), cfg, f.open) + if !isProjectIdentityMismatch(err) { + t.Fatalf("err %v; want errors.As to still reach a *projectIdentityMismatch", err) + } + if !strings.Contains(err.Error(), "other-127.0.0.1:45865") { + t.Fatalf("err %v; want the env endpoint's refusal to stay primary", err) + } + if !strings.Contains(err.Error(), "other-127.0.0.1:3308") { + t.Fatalf("err %v; want the retry's own refusal attached, not discarded", err) + } + if note.Len() != 0 { + t.Fatalf("note %q; want no note when the retry itself is refused", note.String()) + } +} + +// TestEnvEndpointFallbackProductionPortGuardAppliesToRetry (S1). The +// BEADS_TEST_MODE=1 production-port guard applies to the retry too, exactly +// as New()'s own applyConfigDefaults-then-panic sequence applies to the first +// open: a production port on the retry is rewritten to 1, not dialed, and if +// it is somehow still production after the rewrite, the guard panics. This +// test's own TestMain sets BEADS_TEST_MODE=1 for the whole package. +func TestEnvEndpointFallbackProductionPortGuardAppliesToRetry(t *testing.T) { + if os.Getenv("BEADS_TEST_MODE") != "1" { + t.Fatal("expected BEADS_TEST_MODE=1 from this package's TestMain") + } + note := captureFallbackNote(t) + f := &fakeServers{stores: map[string]*DoltStore{"127.0.0.1:1": {}}, foreign: map[string]bool{"127.0.0.1:45865": true}} + cfg := &Config{ + // DefaultSQLPort (3307): production, unconditionally, even under + // BEADS_TEST_SERVER=1 (set by this package's TestMain). + BeadsDir: beadsDirWith(t, "dolt.port: 3307\n"), ServerHost: "127.0.0.1", ServerPort: 45865, + ServerPortSource: doltserver.PortSourceEnv, + } + got, err := openWithEnvEndpointFallback(context.Background(), cfg, f.open) + wantOpened := []string{"127.0.0.1:45865", "127.0.0.1:1"} + if strings.Join(f.opened, " ") != strings.Join(wantOpened, " ") { + t.Fatalf("opened %v, want %v (the retry's production port rewritten to 1, never dialed at 3307)", f.opened, wantOpened) + } + if err != nil || got == nil || cfg.ServerPort != 1 || !strings.Contains(note.String(), "127.0.0.1:1") { + t.Fatalf("got %v, err %v, cfg port %d, note %q; want the rewritten retry to succeed", got, err, cfg.ServerPort, note.String()) + } +} + +// TestEnvEndpointFallbackReResolvesRetryCredentials (S4). The retry's +// password and TLS setting come from the declared endpoint's own +// configuration, not copied forward from the env endpoint: replaying another +// server's password onto this one is the same shape of cross-project leak +// this fallback exists to fix. +func TestEnvEndpointFallbackReResolvesRetryCredentials(t *testing.T) { + t.Setenv("BEADS_DOLT_PASSWORD", "") // no explicit env override in play + note := captureFallbackNote(t) + credFile := filepath.Join(t.TempDir(), "credentials") + if err := os.WriteFile(credFile, []byte("[127.0.0.1:3308]\npassword=project-secret\n"), 0o600); err != nil { + t.Fatal(err) + } + t.Setenv("BEADS_CREDENTIALS_FILE", credFile) + f := &fakeServers{stores: map[string]*DoltStore{"127.0.0.1:3308": {}}, foreign: map[string]bool{"127.0.0.1:45865": true}} + cfg := &Config{ + BeadsDir: beadsDirWith(t, "dolt.port: 3308\n"), ServerHost: "127.0.0.1", ServerPort: 45865, + ServerPortSource: doltserver.PortSourceEnv, ServerUser: "city-robot", ServerPassword: "city-secret", ServerTLS: true, + } + if _, err := openWithEnvEndpointFallback(context.Background(), cfg, f.open); err != nil { + t.Fatalf("err %v; want the retry to succeed", err) + } + if note.Len() == 0 { + t.Fatal("want a fallback note") + } + retryCfg := f.openedCfg[1] + if retryCfg.ServerPassword != "project-secret" { + t.Fatalf("retry password %q; want the declared endpoint's own project-secret, not the env endpoint's city-secret", retryCfg.ServerPassword) + } + if retryCfg.ServerUser != "root" { + t.Fatalf("retry user %q; want the declared endpoint's own default root, not the env endpoint's city-robot", retryCfg.ServerUser) + } + if retryCfg.ServerTLS { + t.Fatal("retry TLS true; want the declared endpoint's own (unset -> false), not the env endpoint's true") + } +} + +// TestEnvEndpointFallbackNoteRespectsQuietAndJSON (nit). The advisory note is +// output a normal user or script did not ask for; it must not appear under +// --quiet or --json. +func TestEnvEndpointFallbackNoteRespectsQuietAndJSON(t *testing.T) { + for _, mode := range []struct { + name string + setup func() + }{ + {"quiet", func() { debug.SetQuiet(true) }}, + {"json", func() { debug.SetJSON(true) }}, + } { + t.Run(mode.name, func(t *testing.T) { + note := captureFallbackNote(t) + mode.setup() + t.Cleanup(func() { debug.SetQuiet(false); debug.SetJSON(false) }) + f := &fakeServers{stores: map[string]*DoltStore{"127.0.0.1:3308": {}}, foreign: map[string]bool{"127.0.0.1:45865": true}} + cfg := &Config{ + BeadsDir: beadsDirWith(t, "dolt.port: 3308\n"), ServerHost: "127.0.0.1", ServerPort: 45865, + ServerPortSource: doltserver.PortSourceEnv, + } + if _, err := openWithEnvEndpointFallback(context.Background(), cfg, f.open); err != nil { + t.Fatalf("err %v; want the retry to succeed", err) + } + if note.Len() != 0 { + t.Fatalf("note %q; want no note under --%s", note.String(), mode.name) + } + }) + } +} + +// TestEnvEndpointFallbackNotePrintsOncePerProcess (nit). A process that +// repeats the same fallback (e.g. a global database open alongside a project +// one, both pointed at the same inherited env endpoint) gets the advisory +// exactly once, not once per open. +func TestEnvEndpointFallbackNotePrintsOncePerProcess(t *testing.T) { + note := captureFallbackNote(t) + open := func(dir string) (*DoltStore, error) { + f := &fakeServers{stores: map[string]*DoltStore{"127.0.0.1:3308": {}}, foreign: map[string]bool{"127.0.0.1:45865": true}} + cfg := &Config{BeadsDir: dir, ServerHost: "127.0.0.1", ServerPort: 45865, ServerPortSource: doltserver.PortSourceEnv} + return openWithEnvEndpointFallback(context.Background(), cfg, f.open) + } + dir := beadsDirWith(t, "dolt.port: 3308\n") + if _, err := open(dir); err != nil { + t.Fatalf("first open: %v", err) + } + firstLen := note.Len() + if firstLen == 0 { + t.Fatal("want a note on the first fallback") + } + if _, err := open(dir); err != nil { + t.Fatalf("second open: %v", err) + } + if note.Len() != firstLen { + t.Fatalf("note grew from %d to %d bytes on a second fallback in the same process; want it unchanged", firstLen, note.Len()) + } +} + +// TestEnvEndpointFallbackNoteNamesActualEnvVar (nit). The note names the +// environment variable that actually supplied the port — the legacy +// BEADS_DOLT_PORT, when that is the one set, not a hardcoded +// BEADS_DOLT_SERVER_PORT the process never had. +func TestEnvEndpointFallbackNoteNamesActualEnvVar(t *testing.T) { + t.Setenv("BEADS_DOLT_SERVER_PORT", "") + t.Setenv("BEADS_DOLT_PORT", "45865") + note := captureFallbackNote(t) + f := &fakeServers{stores: map[string]*DoltStore{"127.0.0.1:3308": {}}, foreign: map[string]bool{"127.0.0.1:45865": true}} + cfg := &Config{ + BeadsDir: beadsDirWith(t, "dolt.port: 3308\n"), ServerHost: "127.0.0.1", ServerPort: 45865, + ServerPortSource: doltserver.PortSourceEnv, + } + if _, err := openWithEnvEndpointFallback(context.Background(), cfg, f.open); err != nil { + t.Fatalf("err %v; want the retry to succeed", err) + } + if !strings.Contains(note.String(), "BEADS_DOLT_PORT") { + t.Fatalf("note %q; want it to name BEADS_DOLT_PORT, the env var actually set", note.String()) + } + if strings.Contains(note.String(), "BEADS_DOLT_SERVER_PORT") { + t.Fatalf("note %q; want it not to claim BEADS_DOLT_SERVER_PORT, which this process never set", note.String()) + } +} diff --git a/internal/storage/dolt/store.go b/internal/storage/dolt/store.go index 32fd90732..453181f08 100644 --- a/internal/storage/dolt/store.go +++ b/internal/storage/dolt/store.go @@ -1793,7 +1793,7 @@ func New(ctx context.Context, cfg *Config) (*DoltStore, error) { cfg.Database, addr) } - return openWithEnvEndpointFallback(ctx, cfg, newServerMode) + return openWithEnvEndpointFallback(ctx, cfg, newServerModeOpener) } // resolveLocalActiveDatabaseDir returns an authoritative local path only for @@ -1946,6 +1946,14 @@ var ensureRunningDetailed = doltserver.EnsureRunningDetailed // dolt sql-server process. var stopRejectedAutoStartedServer = doltserver.Stop +// newServerModeOpener is newServerMode, indirected through a var (matching +// ensureRunningDetailed/stopRejectedAutoStartedServer above) so a test can +// substitute a fake opener and assert New's own wiring — specifically, that +// New always opens through openWithEnvEndpointFallback rather than calling +// this directly — without dialing a real dolt sql-server (B1/S5, gascity +// mc-3gbed). +var newServerModeOpener = newServerMode + // newServerMode creates a DoltStore connected to a running dolt sql-server. // This path is pure Go and does not require CGO. func newServerMode(ctx context.Context, cfg *Config) (*DoltStore, error) { From 09080d3f4b329d7aefe82c79ee47725b05932e46 Mon Sep 17 00:00:00 2001 From: Julian Knutsen Date: Wed, 14 Oct 2026 23:00:52 +0000 Subject: [PATCH 3/4] fix(dolt): close the serve false-refusal, S4 env leak and V31 gaps from PR #7561 round 2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review round 2 on PR #7561's env-endpoint identity fallback (gascity mc-3gbed) landed "LAND" with five more fixes required before merge: - bd serve's refusal (EnvEndpointDivergesFromDeclared, wired in 71e905df88 for B1) compared hosts lexically, so an env endpoint on "localhost" and a declared one on "127.0.0.1" read as a divergence and serve refused a server it was never actually inherited from. Added sameHost(a, b string) bool: an exact match, or both sides isLocalHost — every loopback spelling (127.0.0.1, localhost, ::1, [::1], "") now names the same machine as every other one, while two different non-loopback hosts still fall through to the exact-match result. envEndpointFallback is the single eligibility test both EnvEndpointDivergesFromDeclared and openWithEnvEndpointFallback's own retry gate call, so the one fix closes the refusal for serve and the equivalent false-positive divergence for the CLI retry at once. Tests cover both spellings through all three consumers: sameHost directly, envEndpointFallback/ EnvEndpointDivergesFromDeclared directly, and resolveServerModeUOWTopology end-to-end. - S4 residual: the retry re-resolved its user and TLS setting by calling fileCfg.GetDoltServerUser()/GetDoltServerTLS(), and both check BEADS_DOLT_SERVER_USER/BEADS_DOLT_SERVER_TLS before fileCfg's own field — exactly the ambient-env leak S4 closed for the host and (separately) the password. Added retryServerUser/retryServerTLS, reading fileCfg's fields directly with GetDoltServerUser's own default (DefaultDoltServerUser), without either method's env check. A test sets both env vars to values that would prove the leak and asserts the retry config stays on fileCfg's own (root, non-TLS) values regardless. - V31: verify_project_identity_sqlmock_test.go adds the sqlmock-backed test that kills the untyped-wrap mutant at verifyProjectIdentity's own return, which openWithEnvEndpointFallback's errors.As(isProjectIdentity- Mismatch) needs to stay typed to retry at all. Confirmed against the actual mutant: hand-applying it (dropping the &projectIdentityMismatch{} wrap in store.go) fails this test, and reverting restores a clean pass with no residual diff. - CHANGELOG: extended the existing env-endpoint-fallback entry (from 643615c26a) with bd serve's outright refusal, the CreateIfMissing exclusion, the declared-host precedence, and the loopback-only / spelling-equal restriction this commit adds. - Corrects 71e905df88's own commit message: S2 (the CreateIfMissing exclusion), S3 (errors.Join keeping the original refusal primary) and L1 (the pre-transport-resolution socket capture) were new behavior added by that commit's diff, not pre-existing — git diff 643615c26a 71e905df88 on env_endpoint_fallback.go shows all three as additions. And V31 does not need a live server: verifyProjectIdentity's one DB interaction is a single `SELECT value FROM metadata` inside a read-only transaction (GetMetadata -> withReadTx -> issueops.GetMetadataInTx), which sqlmock stands in for directly, as this commit's test now demonstrates. No schema change. No live Dolt server contacted by any new or changed test; sqlmock mocks the one DB interaction V31's test needs. Agent-Signature: claude-agent-sdk-claude-sonnet-5-80 on behalf of Julian Knutsen Co-Authored-By: Claude Sonnet 5 --- CHANGELOG.md | 18 +++- cmd/bd/uow_factory_server_mode_test.go | 30 ++++++ internal/storage/dolt/BUILD.bazel | 1 + .../storage/dolt/env_endpoint_fallback.go | 47 ++++++++- .../dolt/env_endpoint_fallback_test.go | 95 +++++++++++++++++++ .../verify_project_identity_sqlmock_test.go | 55 +++++++++++ 6 files changed, 239 insertions(+), 7 deletions(-) create mode 100644 internal/storage/dolt/verify_project_identity_sqlmock_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index f822b89eb..c4cc7172e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -513,13 +513,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 no longer locks bd out of a project that declares its own: when the env's server answers with another project's database (PROJECT IDENTITY MISMATCH) and the project's `.beads` explicitly declares a different endpoint - (`config.yaml` `dolt.host`/`dolt.port`, else `metadata.json` - `dolt_server_host`/`dolt_server_port`), bd retries once on that endpoint, + (`config.yaml` `dolt.port`, else `metadata.json` `dolt_server_port`, with + the host following bd's normal precedence — `metadata.json` + `dolt_server_host` before `config.yaml` `dolt.host`, never + `BEADS_DOLT_SERVER_HOST` itself), bd retries once on that endpoint, verifies the project's identity there, and proceeds with a note on stderr; if that endpoint serves another project too, it refuses with the original error. An agent session carrying its own workspace's server in its - environment can now run bd inside a different checkout. Ports from any - other source, sockets, gateways and the shared server are unchanged. + environment can now run bd inside a different checkout. The retry never + fires for an open that may create the database (`bd init`, `bd bootstrap`), + for a configured socket, a gateway credential, or the shared server, and it + never dials a declared endpoint off this machine — loopback only + (`127.0.0.1`, `localhost`, `::1`/`[::1]`, every spelling treated as the same + machine, so an env endpoint on one spelling and a declared endpoint on + another no longer reads as a divergence). `bd serve` gets no retry at all — + building its own connection outside `dolt.New`, it refuses outright instead + of fronting an HTTP server on whatever database the inherited endpoint + happens to answer with. - **`bd gate list` works over a connected HTTP workspace** (S20). The workspace-wide listing built a raw `SearchIssues` filter, which the HTTP diff --git a/cmd/bd/uow_factory_server_mode_test.go b/cmd/bd/uow_factory_server_mode_test.go index c3bff6cb0..b56e3cda8 100644 --- a/cmd/bd/uow_factory_server_mode_test.go +++ b/cmd/bd/uow_factory_server_mode_test.go @@ -123,6 +123,36 @@ func TestResolveServerModeUOWTopology_AllowsEnvEndpointMatchingDeclared(t *testi assert.Equal(t, 3521, topology.external.Port) } +// Loopback spellings are not a divergence: env "localhost" and a declared +// "127.0.0.1" (or the reverse) name the same machine, and comparing them +// lexically refused a server that was never actually inherited from another +// workspace (bd serve false refusal, gascity mc-3gbed follow-up). +func TestResolveServerModeUOWTopology_AllowsEnvEndpointLoopbackSpellingMismatch(t *testing.T) { + for _, c := range []struct { + name string + declaredHost string + envHost string + }{ + {"declared 127.0.0.1, env localhost", "127.0.0.1", "localhost"}, + {"declared localhost, env 127.0.0.1", "localhost", "127.0.0.1"}, + } { + t.Run(c.name, func(t *testing.T) { + beadsDir := serverModeBeadsDir(t, &configfile.Config{ + DoltServerHost: c.declaredHost, + DoltServerPort: 3521, + DoltDatabase: "beads_serve", + }) + t.Setenv("BEADS_DOLT_SERVER_HOST", c.envHost) + t.Setenv("BEADS_DOLT_SERVER_PORT", "3521") + + topology, err := resolveServerModeUOWTopology(context.Background(), beadsDir) + require.NoError(t, err, + "a loopback spelling that differs from the declared one must not read as an inherited server") + assert.Equal(t, 3521, topology.external.Port) + }) + } +} + // Shared-server mode is one server fronting every project by design, not a // cross-project leak: envEndpointFallback already excludes it (S5's // exclusion coverage), and this refusal must agree rather than block the diff --git a/internal/storage/dolt/BUILD.bazel b/internal/storage/dolt/BUILD.bazel index d8c721cea..5a53f591e 100644 --- a/internal/storage/dolt/BUILD.bazel +++ b/internal/storage/dolt/BUILD.bazel @@ -363,6 +363,7 @@ go_test( "update_force_close_policy_test.go", "update_guards_test.go", "update_same_status_test.go", + "verify_project_identity_sqlmock_test.go", "version_commit_deferred_test.go", "version_reconciler_contract_test.go", "version_scope_completeness_test.go", diff --git a/internal/storage/dolt/env_endpoint_fallback.go b/internal/storage/dolt/env_endpoint_fallback.go index 6ef30da19..680ca9115 100644 --- a/internal/storage/dolt/env_endpoint_fallback.go +++ b/internal/storage/dolt/env_endpoint_fallback.go @@ -95,8 +95,16 @@ func openWithEnvEndpointFallback(ctx context.Context, cfg *Config, open func(con // the env endpoint's values in place. fileCfg = configfile.DefaultConfig() } - retry.ServerUser = fileCfg.GetDoltServerUser() - retry.ServerTLS = fileCfg.GetDoltServerTLS() + // retryServerUser/retryServerTLS, not fileCfg.GetDoltServerUser/TLS: those + // two check BEADS_DOLT_SERVER_USER/BEADS_DOLT_SERVER_TLS first, which are + // exactly the kind of ambient value inherited from another workspace's + // environment this fallback exists to escape (the same reasoning + // declaredHost above already applies to BEADS_DOLT_SERVER_HOST). The + // password just below stays the one deliberate exception: an explicit + // BEADS_DOLT_PASSWORD is honored, but keyed to alt.host:alt.port, not + // whatever host the env endpoint resolved. + retry.ServerUser = retryServerUser(fileCfg) + retry.ServerTLS = retryServerTLS(fileCfg) if envPass := os.Getenv("BEADS_DOLT_PASSWORD"); envPass != "" { // An explicit env override is a deliberate operator action, not an // ambient value inherited from another workspace; honor it for the @@ -146,6 +154,25 @@ func openWithEnvEndpointFallback(ctx context.Context, cfg *Config, open func(con return altStore, nil } +// retryServerUser is fileCfg's own configured Dolt server user, same default +// as GetDoltServerUser (configfile.DefaultDoltServerUser) but without that +// method's BEADS_DOLT_SERVER_USER env check: the retry resolves the declared +// endpoint's identity, not whatever user the inherited env endpoint's shell +// happens to export (S4 residual, gascity mc-3gbed follow-up). +func retryServerUser(fileCfg *configfile.Config) string { + if fileCfg != nil && fileCfg.DoltServerUser != "" { + return fileCfg.DoltServerUser + } + return configfile.DefaultDoltServerUser +} + +// retryServerTLS is fileCfg's own configured Dolt server TLS setting, without +// GetDoltServerTLS's BEADS_DOLT_SERVER_TLS env check, for the same reason +// retryServerUser skips GetDoltServerUser's. +func retryServerTLS(fileCfg *configfile.Config) bool { + return fileCfg != nil && fileCfg.DoltServerTLS +} + // envEndpointPortVarName names the environment variable that actually // supplied cfg's port, for the fallback note. BEADS_DOLT_SERVER_PORT is // checked first, matching applyConfigDefaults' own precedence; @@ -177,7 +204,7 @@ func envEndpointFallback(cfg *Config, originalSocket string) (declaredEndpoint, return declaredEndpoint{}, false } alt, ok := projectDeclaredEndpoint(cfg.BeadsDir) - if !ok || (alt.host == cfg.ServerHost && alt.port == cfg.ServerPort) { + if !ok || (sameHost(alt.host, cfg.ServerHost) && alt.port == cfg.ServerPort) { return declaredEndpoint{}, false } // S4: never retry onto a non-loopback host. The declared endpoint is read @@ -219,6 +246,20 @@ func projectDeclaredEndpoint(beadsDir string) (declaredEndpoint, bool) { return declaredEndpoint{}, false } +// sameHost reports whether a and b name the same server host for the +// divergence check above and in EnvEndpointDivergesFromDeclared. An exact +// match is the common case; beyond that, every spelling isLocalHost treats +// as "this machine" names the same machine as every other one — an env +// endpoint on "localhost" and a declared endpoint on "127.0.0.1" are the +// same server, and comparing them lexically produced a false refusal (bd +// serve: env_endpoint_fallback.go:180, gascity mc-3gbed follow-up). Two +// different non-loopback hosts are never equated: isLocalHost returns false +// for a genuinely remote host, so the OR falls through to the exact-match +// result for it. +func sameHost(a, b string) bool { + return a == b || (isLocalHost(a) && isLocalHost(b)) +} + func hostOrLoopback(host string) string { if host = strings.TrimSpace(host); host != "" { return host diff --git a/internal/storage/dolt/env_endpoint_fallback_test.go b/internal/storage/dolt/env_endpoint_fallback_test.go index d42b2fa13..d4735868a 100644 --- a/internal/storage/dolt/env_endpoint_fallback_test.go +++ b/internal/storage/dolt/env_endpoint_fallback_test.go @@ -567,3 +567,98 @@ func TestEnvEndpointFallbackNoteNamesActualEnvVar(t *testing.T) { t.Fatalf("note %q; want it not to claim BEADS_DOLT_SERVER_PORT, which this process never set", note.String()) } } + +// TestSameHost (serve false refusals, gascity mc-3gbed follow-up). Every +// loopback spelling names the same machine as every other one; two different +// non-loopback hosts are never equal just because neither is loopback. +func TestSameHost(t *testing.T) { + for _, c := range []struct { + a, b string + want bool + }{ + {"127.0.0.1", "127.0.0.1", true}, + {"localhost", "localhost", true}, + {"127.0.0.1", "localhost", true}, + {"localhost", "127.0.0.1", true}, + {"", "127.0.0.1", true}, + {"::1", "[::1]", true}, + {"10.0.0.5", "10.0.0.5", true}, + {"10.0.0.5", "10.0.0.9", false}, + {"10.0.0.5", "127.0.0.1", false}, + {"127.0.0.1", "10.0.0.5", false}, + } { + if got := sameHost(c.a, c.b); got != c.want { + t.Errorf("sameHost(%q, %q) = %v, want %v", c.a, c.b, got, c.want) + } + } +} + +// TestEnvEndpointFallbackTreatsLoopbackSpellingsAsEqual (serve false +// refusals). A declared endpoint that differs from the env endpoint only in +// loopback spelling ("localhost" vs "127.0.0.1") is the SAME endpoint, not a +// divergence: the fallback must not retry (there is nothing to correct), and +// EnvEndpointDivergesFromDeclared — bd serve's own refusal check — must not +// refuse either. Covers both spellings, in both directions. +func TestEnvEndpointFallbackTreatsLoopbackSpellingsAsEqual(t *testing.T) { + for _, c := range []struct { + name string + declaredHost string + envHost string + }{ + {"declared 127.0.0.1, env localhost", "127.0.0.1", "localhost"}, + {"declared localhost, env 127.0.0.1", "localhost", "127.0.0.1"}, + } { + t.Run(c.name, func(t *testing.T) { + yaml := "dolt:\n host: " + c.declaredHost + "\n port: 3308\n" + cfg := &Config{ + BeadsDir: beadsDirWith(t, yaml), ServerHost: c.envHost, ServerPort: 3308, + ServerPortSource: doltserver.PortSourceEnv, + } + + if _, ok := envEndpointFallback(cfg, ""); ok { + t.Fatalf("envEndpointFallback found a divergence between %q and %q; want none (same loopback machine)", c.declaredHost, c.envHost) + } + if declared, diverges := EnvEndpointDivergesFromDeclared(cfg); diverges { + t.Fatalf("EnvEndpointDivergesFromDeclared reported %q as a divergence from %q; want bd serve not to refuse a loopback spelling mismatch", declared, c.envHost) + } + + note := captureFallbackNote(t) + f := &fakeServers{foreign: map[string]bool{net.JoinHostPort(c.envHost, "3308"): true}} + if _, err := openWithEnvEndpointFallback(context.Background(), cfg, f.open); err == nil || len(f.opened) != 1 || note.Len() != 0 { + t.Fatalf("err %v, opened %v, note %q; want the first open's error, one open, no note (same endpoint, nothing to retry)", err, f.opened, note.String()) + } + }) + } +} + +// TestEnvEndpointFallbackReResolvesRetryUserAndTLSWithoutEnv (S4 residual). +// The retry's user and TLS setting must come from the declared endpoint's own +// file configuration even when BEADS_DOLT_SERVER_USER / BEADS_DOLT_SERVER_TLS +// are set in the environment: those env vars are exactly the kind of ambient +// value inherited from another workspace's shell this fallback exists to +// escape, the same way the password branch already keys its env override to +// alt.host:alt.port rather than letting it blanket-apply. +func TestEnvEndpointFallbackReResolvesRetryUserAndTLSWithoutEnv(t *testing.T) { + t.Setenv("BEADS_DOLT_PASSWORD", "") + t.Setenv("BEADS_DOLT_SERVER_USER", "env-inherited-user") + t.Setenv("BEADS_DOLT_SERVER_TLS", "1") + note := captureFallbackNote(t) + f := &fakeServers{stores: map[string]*DoltStore{"127.0.0.1:3308": {}}, foreign: map[string]bool{"127.0.0.1:45865": true}} + cfg := &Config{ + BeadsDir: beadsDirWith(t, "dolt.port: 3308\n"), ServerHost: "127.0.0.1", ServerPort: 45865, + ServerPortSource: doltserver.PortSourceEnv, + } + if _, err := openWithEnvEndpointFallback(context.Background(), cfg, f.open); err != nil { + t.Fatalf("err %v; want the retry to succeed", err) + } + if note.Len() == 0 { + t.Fatal("want a fallback note") + } + retryCfg := f.openedCfg[1] + if retryCfg.ServerUser != "root" { + t.Fatalf("retry user %q; want the declared endpoint's own default root, not the env's env-inherited-user", retryCfg.ServerUser) + } + if retryCfg.ServerTLS { + t.Fatal("retry TLS true; want the declared endpoint's own (unset -> false), not the env's BEADS_DOLT_SERVER_TLS=1") + } +} diff --git a/internal/storage/dolt/verify_project_identity_sqlmock_test.go b/internal/storage/dolt/verify_project_identity_sqlmock_test.go new file mode 100644 index 000000000..05f34811c --- /dev/null +++ b/internal/storage/dolt/verify_project_identity_sqlmock_test.go @@ -0,0 +1,55 @@ +package dolt + +import ( + "context" + "os" + "path/filepath" + "regexp" + "testing" + + sqlmock "github.com/DATA-DOG/go-sqlmock" +) + +// TestVerifyProjectIdentityReturnsTypedMismatch (gascity mc-3gbed follow-up, +// PR #7561 review round 2). openWithEnvEndpointFallback only retries a +// refusal isProjectIdentityMismatch recognizes — verifyProjectIdentity's +// return has to stay the typed *projectIdentityMismatch wrap, not an untyped +// fmt.Errorf a caller's errors.As can no longer see, or the whole fallback +// silently stops firing on every mismatched project, with every call site +// still believing it works. +// +// This does not need a live Dolt server: verifyProjectIdentity's one query +// (metadata.json's local project_id against the database's, read through +// GetMetadata -> withReadTx -> issueops.GetMetadataInTx) is a single +// `SELECT value FROM metadata` inside a read transaction, which sqlmock +// stands in for directly. +func TestVerifyProjectIdentityReturnsTypedMismatch(t *testing.T) { + db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherRegexp)) + if err != nil { + t.Fatalf("sqlmock: %v", err) + } + t.Cleanup(func() { _ = db.Close() }) + + mock.ExpectBegin() + mock.ExpectQuery(regexp.QuoteMeta("SELECT value FROM metadata")). + WithArgs("_project_id"). + WillReturnRows(sqlmock.NewRows([]string{"value"}).AddRow("db-project")) + mock.ExpectRollback() + + dir := filepath.Join(t.TempDir(), ".beads") + if err := os.MkdirAll(dir, 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, "metadata.json"), []byte(`{"project_id":"local-project"}`), 0o644); err != nil { + t.Fatal(err) + } + + s := &DoltStore{db: db, database: "beads"} + got := s.verifyProjectIdentity(context.Background(), dir, false) + if !isProjectIdentityMismatch(got) { + t.Fatalf("verifyProjectIdentity returned %v; want a typed *projectIdentityMismatch openWithEnvEndpointFallback's errors.As can see", got) + } + if err := mock.ExpectationsWereMet(); err != nil { + t.Fatalf("unmet sqlmock expectations: %v", err) + } +} From 5c30116dd1fb4fc9ea4443fc80f40baf57aedb18 Mon Sep 17 00:00:00 2001 From: Julian Knutsen Date: Wed, 14 Oct 2026 23:21:59 +0000 Subject: [PATCH 4/4] fix(dolt): retry's password comes only from the declared endpoint's credentials file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review round 3 on 09080d3f4b (PR #7561, gascity mc-3gbed) landed "LAND" with one more fix required before merge: the retry still honored an explicit env BEADS_DOLT_PASSWORD (env_endpoint_fallback.go:108), kept as "the one deliberate exception" to S4's re-resolve-from-the-declared- endpoint rule that already applied to user and TLS. A gascity city sets a server's user and password together in env, so in a password-protected city the retry sent user root (re-resolved from the declared endpoint's own file) paired with the city's password (still read from env) — a mismatched pair that fails auth against the project's own server, so the fix did not actually work there, and it offered the city's password to whatever local server the retry dialed. retryServerPassword(alt) replaces the env check: the retry's password is now only configfile.LookupCredentialsPassword(alt.host, alt.port), the same credentials-file source retryServerUser/retryServerTLS already use for user and TLS, keyed to the declared endpoint actually being retried. With no entry there, the retry sends no password, same as before this series started for a project with no credentials file at all. TestEnvEndpointFallbackIgnoresEnvPasswordForRetry sets BEADS_DOLT_PASSWORD=city-secret alongside a declared endpoint, in two cases: a credentials-file entry (retry must send that entry's password, not city-secret) and no entry (retry must send no password, not city-secret). Confirmed against the prior behavior: reintroducing the env check fails both subcases, reverting it passes. CHANGELOG: extended the same entry again with the retry's own credential re-resolution (user, TLS, password) and why an inherited BEADS_DOLT_PASSWORD is never it. No schema change. No live Dolt server contacted. Agent-Signature: claude-agent-sdk-claude-sonnet-5-80 on behalf of Julian Knutsen Co-Authored-By: Claude Sonnet 5 --- CHANGELOG.md | 8 ++- .../storage/dolt/env_endpoint_fallback.go | 50 +++++++++++-------- .../dolt/env_endpoint_fallback_test.go | 41 +++++++++++++++ 3 files changed, 78 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4cc7172e..31b7b5f30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -529,7 +529,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 another no longer reads as a divergence). `bd serve` gets no retry at all — building its own connection outside `dolt.New`, it refuses outright instead of fronting an HTTP server on whatever database the inherited endpoint - happens to answer with. + happens to answer with. The retry re-resolves its own user, TLS setting and + password for the declared endpoint instead of carrying the env endpoint's + forward: an environment (a gascity city, for one) that sets a server's user + and password together means an inherited `BEADS_DOLT_PASSWORD` is a + credential for the env endpoint's user, never the declared endpoint's, so + the retry's password comes only from the credentials file entry keyed to + the declared endpoint, with no password sent when that file has none. - **`bd gate list` works over a connected HTTP workspace** (S20). The workspace-wide listing built a raw `SearchIssues` filter, which the HTTP diff --git a/internal/storage/dolt/env_endpoint_fallback.go b/internal/storage/dolt/env_endpoint_fallback.go index 680ca9115..c8580470a 100644 --- a/internal/storage/dolt/env_endpoint_fallback.go +++ b/internal/storage/dolt/env_endpoint_fallback.go @@ -95,28 +95,22 @@ func openWithEnvEndpointFallback(ctx context.Context, cfg *Config, open func(con // the env endpoint's values in place. fileCfg = configfile.DefaultConfig() } - // retryServerUser/retryServerTLS, not fileCfg.GetDoltServerUser/TLS: those - // two check BEADS_DOLT_SERVER_USER/BEADS_DOLT_SERVER_TLS first, which are - // exactly the kind of ambient value inherited from another workspace's - // environment this fallback exists to escape (the same reasoning - // declaredHost above already applies to BEADS_DOLT_SERVER_HOST). The - // password just below stays the one deliberate exception: an explicit - // BEADS_DOLT_PASSWORD is honored, but keyed to alt.host:alt.port, not - // whatever host the env endpoint resolved. + // retryServerUser/retryServerTLS/retryServerPassword, not + // fileCfg.GetDoltServerUser/TLS or BEADS_DOLT_PASSWORD: those all read an + // ambient value inherited from another workspace's environment, which is + // exactly what this fallback exists to escape (the same reasoning + // declaredHost above already applies to BEADS_DOLT_SERVER_HOST). A city + // (or any environment) that sets a server's user and password together in + // env means an env password is never a credential for this retry's own + // endpoint specifically — it is the inherited server's, paired with the + // inherited server's user, and sending it here either fails auth against + // the declared endpoint's own password or, worse, offers the inherited + // city's password to a different, local server. The credentials file, + // keyed to alt.host:alt.port, is this retry's only password source; with + // no entry there, the retry sends no password at all. retry.ServerUser = retryServerUser(fileCfg) retry.ServerTLS = retryServerTLS(fileCfg) - if envPass := os.Getenv("BEADS_DOLT_PASSWORD"); envPass != "" { - // An explicit env override is a deliberate operator action, not an - // ambient value inherited from another workspace; honor it for the - // retry the same way applyConfigDefaults honors it for the first - // open. Looked up directly (not via the alt endpoint's own - // GetDoltServerPasswordForPort) so the retry's password is keyed on - // alt.host:alt.port specifically, not whatever host cfg's file - // resolves on its own. - retry.ServerPassword = envPass - } else { - retry.ServerPassword = configfile.LookupCredentialsPassword(alt.host, alt.port) - } + retry.ServerPassword = retryServerPassword(alt) // S1: New()'s BEADS_TEST_MODE=1 production-port guard (applyConfigDefaults' // rewrite-to-1, then New()'s defense-in-depth panic if still production) @@ -173,6 +167,22 @@ func retryServerTLS(fileCfg *configfile.Config) bool { return fileCfg != nil && fileCfg.DoltServerTLS } +// retryServerPassword is the credentials file's entry for alt (the declared +// endpoint being retried), without GetDoltServerPasswordForPort's +// BEADS_DOLT_PASSWORD env check: an environment that sets a server's user and +// password together (a gascity city, for one) pairs that password with the +// inherited env endpoint's user, not the declared endpoint's — sending it +// here either fails auth against the declared endpoint's own password or +// offers the inherited city's password to a different, local server (S4 +// residual, PR #7561 review round 3). There is no config.yaml/metadata.json +// field for a password (unlike user and TLS): the credentials file is the +// only non-env source, keyed to alt.host:alt.port specifically, not whatever +// host cfg's file resolves on its own; with no entry there, the retry sends +// no password at all. +func retryServerPassword(alt declaredEndpoint) string { + return configfile.LookupCredentialsPassword(alt.host, alt.port) +} + // envEndpointPortVarName names the environment variable that actually // supplied cfg's port, for the fallback note. BEADS_DOLT_SERVER_PORT is // checked first, matching applyConfigDefaults' own precedence; diff --git a/internal/storage/dolt/env_endpoint_fallback_test.go b/internal/storage/dolt/env_endpoint_fallback_test.go index d4735868a..396aeb577 100644 --- a/internal/storage/dolt/env_endpoint_fallback_test.go +++ b/internal/storage/dolt/env_endpoint_fallback_test.go @@ -487,6 +487,47 @@ func TestEnvEndpointFallbackReResolvesRetryCredentials(t *testing.T) { } } +// TestEnvEndpointFallbackIgnoresEnvPasswordForRetry (PR #7561 review round 3, +// gascity mc-3gbed follow-up). A gascity city sets a server's user and +// password together in env, so an env BEADS_DOLT_PASSWORD is a credential +// for the inherited env endpoint's user, never the declared endpoint's: +// honoring it for the retry either fails auth against the declared +// endpoint's own password, or offers the city's password to a different, +// local server. The credentials file, keyed to the declared endpoint, is the +// retry's only password source; with no entry there, the retry sends no +// password at all, even though BEADS_DOLT_PASSWORD is set. +func TestEnvEndpointFallbackIgnoresEnvPasswordForRetry(t *testing.T) { + for _, c := range []struct { + name string + credFileBody string + wantPassword string + }{ + {"declared endpoint has a credentials entry", "[127.0.0.1:3308]\npassword=project-secret\n", "project-secret"}, + {"declared endpoint has no credentials entry", "[127.0.0.1:9999]\npassword=unrelated-secret\n", ""}, + } { + t.Run(c.name, func(t *testing.T) { + t.Setenv("BEADS_DOLT_PASSWORD", "city-secret") + credFile := filepath.Join(t.TempDir(), "credentials") + if err := os.WriteFile(credFile, []byte(c.credFileBody), 0o600); err != nil { + t.Fatal(err) + } + t.Setenv("BEADS_CREDENTIALS_FILE", credFile) + f := &fakeServers{stores: map[string]*DoltStore{"127.0.0.1:3308": {}}, foreign: map[string]bool{"127.0.0.1:45865": true}} + cfg := &Config{ + BeadsDir: beadsDirWith(t, "dolt.port: 3308\n"), ServerHost: "127.0.0.1", ServerPort: 45865, + ServerPortSource: doltserver.PortSourceEnv, ServerUser: "city-robot", ServerPassword: "city-secret", ServerTLS: true, + } + if _, err := openWithEnvEndpointFallback(context.Background(), cfg, f.open); err != nil { + t.Fatalf("err %v; want the retry to succeed", err) + } + retryCfg := f.openedCfg[1] + if retryCfg.ServerPassword != c.wantPassword { + t.Fatalf("retry password %q; want %q (the declared endpoint's own credentials-file entry, never the env endpoint's city-secret)", retryCfg.ServerPassword, c.wantPassword) + } + }) + } +} + // TestEnvEndpointFallbackNoteRespectsQuietAndJSON (nit). The advisory note is // output a normal user or script did not ask for; it must not appear under // --quiet or --json.