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) + } +}