Compare commits
4 commits
main
...
fix/env-en
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5c30116dd1 | ||
|
|
09080d3f4b | ||
|
|
71e905df88 | ||
|
|
643615c26a |
10 changed files with 1234 additions and 1 deletions
28
CHANGELOG.md
28
CHANGELOG.md
|
|
@ -509,6 +509,34 @@ 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.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. 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. 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
|
||||
backend refuses, so gc's `renudge-stale-human-gates` order failed on every
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -84,6 +84,93 @@ 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)
|
||||
}
|
||||
|
||||
// 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
|
||||
// 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
|
||||
|
|
|
|||
|
|
@ -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...)
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
@ -361,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",
|
||||
|
|
|
|||
307
internal/storage/dolt/env_endpoint_fallback.go
Normal file
307
internal/storage/dolt/env_endpoint_fallback.go
Normal file
|
|
@ -0,0 +1,307 @@
|
|||
package dolt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"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"
|
||||
)
|
||||
|
||||
// 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
|
||||
|
||||
// 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
|
||||
// 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) {
|
||||
// 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, 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()
|
||||
}
|
||||
// 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)
|
||||
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)
|
||||
// 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 {
|
||||
// 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 && !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
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// 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;
|
||||
// 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, 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 == "" ||
|
||||
originalSocket != "" || cfg.Gateway || cfg.Database == doltserver.GlobalDatabaseName ||
|
||||
cfg.CreateIfMissing || doltserver.IsSharedServerModeForDir(cfg.BeadsDir) {
|
||||
return declaredEndpoint{}, false
|
||||
}
|
||||
alt, ok := projectDeclaredEndpoint(cfg.BeadsDir)
|
||||
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
|
||||
// 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 {
|
||||
return declaredEndpoint{host: hostOrLoopback(declaredHost()), port: port, source: doltserver.PortSourceConfigYaml}, true
|
||||
}
|
||||
}
|
||||
if meta != nil && meta.DoltServerPort > 0 {
|
||||
return declaredEndpoint{host: hostOrLoopback(declaredHost()), port: meta.DoltServerPort, source: doltserver.PortSourceMetadataJSON}, true
|
||||
}
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
705
internal/storage/dolt/env_endpoint_fallback_test.go
Normal file
705
internal/storage/dolt/env_endpoint_fallback_test.go
Normal file
|
|
@ -0,0 +1,705 @@
|
|||
package dolt
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/debug"
|
||||
"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
|
||||
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)
|
||||
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
|
||||
prevOnce := envEndpointFallbackNoteOnce
|
||||
envEndpointFallbackNote = &buf
|
||||
envEndpointFallbackNoteOnce = &sync.Once{}
|
||||
t.Cleanup(func() {
|
||||
envEndpointFallbackNote = prev
|
||||
envEndpointFallbackNoteOnce = prevOnce
|
||||
})
|
||||
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
|
||||
}{
|
||||
// 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:3308": 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 != 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())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
// 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")
|
||||
}
|
||||
}
|
||||
|
||||
// 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.
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
||||
// 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")
|
||||
}
|
||||
}
|
||||
|
|
@ -1793,7 +1793,7 @@ func New(ctx context.Context, cfg *Config) (*DoltStore, error) {
|
|||
cfg.Database, addr)
|
||||
}
|
||||
|
||||
return newServerMode(ctx, cfg)
|
||||
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) {
|
||||
|
|
@ -2451,6 +2459,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"+
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue