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) {