feat(storage): refuse test DBs on production dolt servers (AD-01) (#3632)
* feat(storage): be-c5p AD-01 isProductionPort + DB-name firewall Generalize the BEADS_TEST_MODE production-port guard so deployments on non-3307 ports (BEADS_PRODUCTION_PORT env, BeadsDir/dolt-server.port file) are also caught, and add a defense-in-depth database-name firewall that refuses test-named DBs on any server unless the operator opts into the dedicated test-server lane via BEADS_TEST_SERVER=1. Both defenses honor BEADS_TEST_SERVER=1 as the single opt-in for test-server connections, so each test harness flips one env var rather than each layer reasoning about test/prod independently. Adds benchdb_ to testDatabasePrefixes (per-bench scratch DBs from cmd/bd/dolt.go) and updates the doc-block to call out the two sibling prefix lists that need to converge under be-avn. Test harnesses across cmd/bd, internal/storage/dolt, internal/molecules, internal/tracker, internal/utils, doctor, doctor/fix, protocol, regression, and the root suite now set BEADS_TEST_SERVER=1 alongside BEADS_TEST_MODE=1 in TestMain. The cross-rig integration test that filters BEADS_* env on subprocess Cmd.Env adds it back explicitly. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * chore: release gate PASS for be-c5p-firewall (be-m1u/47dcc380) * fix(test): adapt store_unit_test.go for BEADS_DOLT_SERVER_PORT (rebase compat) Tests that exercise applyConfigDefaults now clear BEADS_DOLT_SERVER_PORT (added to main after this branch was cut) so the legacy BEADS_DOLT_PORT path runs as intended. Without this, a GC-city environment with BEADS_DOLT_SERVER_PORT set causes the tests to resolve an unexpected port. * fix(dolt): stop BEADS_TEST_SERVER=1 from suppressing DefaultSQLPort detection Restructure productionPortReasons so Rule 1 (cfg.ServerPort == DefaultSQLPort) is evaluated unconditionally; BEADS_TEST_SERVER=1 now only suppresses the BEADS_PRODUCTION_PORT and dolt-server.port heuristics (Rules 2 and 3). isProductionPort loses its independent whole-function bypass and becomes a thin wrapper. Update doc comments on both functions and the buildTestModeProductionPortPanic fix list (which no longer lists BEADS_TEST_SERVER=1 as a fix when port 3307 is the matched reason) to match. Add TestApplyConfigDefaults_TestModeBlocksProdPort_EvenWithTestServerOptIn asserting port 3307 is still refused with the opt-in set. Also: drop a redundant explicit db.Close() in openServerConnection's firewall-refusal path (already covered by the connReady defer), and fix two comments that referenced a non-existent uniqueBenchDBName, pointing them at the real generator (cmd/bd/template_test.go newTemplateBenchmarkStore) with the correct benchdb_ name format. Agent-Signature: claude-sonnet-medium on behalf of matt wilkie --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Co-authored-by: matt wilkie <maphew@gmail.com>
This commit is contained in:
parent
b7a62e86e6
commit
50003b803b
16 changed files with 338 additions and 13 deletions
|
|
@ -19,6 +19,8 @@ var testServerPort int
|
|||
|
||||
func TestMain(m *testing.M) {
|
||||
os.Setenv("BEADS_TEST_MODE", "1")
|
||||
// AD-01 (be-c5p): allow root tests to connect to the test container.
|
||||
os.Setenv("BEADS_TEST_SERVER", "1")
|
||||
if err := testutil.EnsureDoltContainerForTestMain(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: %v, skipping Dolt tests\n", err)
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -121,6 +121,9 @@ func TestListExplicitDBPathRebindsTargetContext(t *testing.T) {
|
|||
"HOME="+t.TempDir(),
|
||||
"XDG_CONFIG_HOME="+t.TempDir(),
|
||||
"BEADS_TEST_MODE=1",
|
||||
// AD-01 (be-c5p): the subprocess connects to a testdb_*-named DB on
|
||||
// the test container; pass the firewall opt-in so dolt.New allows it.
|
||||
"BEADS_TEST_SERVER=1",
|
||||
"BEADS_DIR="+callerBeadsDir,
|
||||
"BEADS_DB=",
|
||||
)
|
||||
|
|
|
|||
|
|
@ -57,6 +57,10 @@ func TestMain(m *testing.M) {
|
|||
|
||||
func testMainInner(m *testing.M) int {
|
||||
os.Setenv("BEADS_TEST_MODE", "1")
|
||||
// AD-01 (be-c5p): doctor e2e tests connect to a per-package test server.
|
||||
// The dolt.New database-name firewall requires this opt-in to allow
|
||||
// doctor_pkg_shared and doctest_*-prefixed databases through.
|
||||
os.Setenv("BEADS_TEST_SERVER", "1")
|
||||
if err := testutil.EnsureDoltContainerForTestMain(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: %v, skipping Dolt tests\n", err)
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -15,6 +15,9 @@ import (
|
|||
// production server on port 3307.
|
||||
func TestMain(m *testing.M) {
|
||||
os.Setenv("BEADS_TEST_MODE", "1")
|
||||
// AD-01 (be-c5p): allow doctor/fix tests through the dolt.New
|
||||
// database-name firewall when they connect to the spawned test server.
|
||||
os.Setenv("BEADS_TEST_SERVER", "1")
|
||||
if err := testutil.EnsureDoltContainerForTestMain(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: %v, skipping Dolt tests\n", err)
|
||||
} else {
|
||||
|
|
@ -41,5 +44,6 @@ func TestMain(m *testing.M) {
|
|||
}
|
||||
os.Unsetenv("BEADS_DOLT_PORT")
|
||||
os.Unsetenv("BEADS_TEST_MODE")
|
||||
os.Unsetenv("BEADS_TEST_SERVER")
|
||||
os.Exit(code)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -981,8 +981,8 @@ servers are preserved.`,
|
|||
// - beads_vr : version-roundtrip / migration fixtures.
|
||||
// - doctest_ : `bd doctor` self-check fixtures.
|
||||
// - doctortest_ : older `bd doctor` fixture name (kept for back-compat).
|
||||
// - benchdb_ : per-bench scratch DBs (uniqueBenchDBName below,
|
||||
// format `benchdb_<pid>_<8 hex>`).
|
||||
// - benchdb_ : per-bench scratch DBs (cmd/bd/template_test.go
|
||||
// newTemplateBenchmarkStore, format `benchdb_<unixnano>`).
|
||||
var staleDatabasePrefixes = []string{
|
||||
"testdb_",
|
||||
"beads_test",
|
||||
|
|
|
|||
|
|
@ -49,6 +49,8 @@ func TestMain(m *testing.M) {
|
|||
|
||||
func testMainInner(m *testing.M) int {
|
||||
os.Setenv("BEADS_TEST_MODE", "1")
|
||||
// AD-01 (be-c5p): allow protocol tests to connect to the spawned test server.
|
||||
os.Setenv("BEADS_TEST_SERVER", "1")
|
||||
if err := testutil.EnsureDoltContainerForTestMain(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: %v, skipping Dolt tests\n", err)
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -111,6 +111,10 @@ func testMainInner(m *testing.M) int {
|
|||
// Previously each test set/unset this env var via ensureTestMode(),
|
||||
// which raced under t.Parallel().
|
||||
_ = os.Setenv("BEADS_TEST_MODE", "1")
|
||||
// AD-01 (be-c5p): opt the cmd/bd test process into the dedicated
|
||||
// test-server lane so dolt.New's database-name firewall allows
|
||||
// testdb_*, benchdb_*, etc. on the spawned test container.
|
||||
_ = os.Setenv("BEADS_TEST_SERVER", "1")
|
||||
|
||||
// Clear BEADS_DIR to prevent tests from accidentally picking up the project's
|
||||
// .beads directory via git repo detection when there's a redirect file.
|
||||
|
|
|
|||
|
|
@ -18,9 +18,12 @@ func setupIntegrationTestMain(root string) (func(), error) {
|
|||
os.Unsetenv("BEADS_DOLT_PORT")
|
||||
os.Unsetenv("BEADS_TEST_BD_BINARY")
|
||||
os.Unsetenv("BEADS_TEST_MODE")
|
||||
os.Unsetenv("BEADS_TEST_SERVER")
|
||||
}
|
||||
|
||||
os.Setenv("BEADS_TEST_MODE", "1")
|
||||
// AD-01 (be-c5p): allow integration tests to connect to the test container.
|
||||
os.Setenv("BEADS_TEST_SERVER", "1")
|
||||
|
||||
if err := testutil.EnsureDoltContainerForTestMain(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: %v, skipping Dolt tests\n", err)
|
||||
|
|
|
|||
|
|
@ -26,6 +26,8 @@ func TestMain(m *testing.M) {
|
|||
|
||||
func testMainInner(m *testing.M) int {
|
||||
os.Setenv("BEADS_TEST_MODE", "1")
|
||||
// AD-01 (be-c5p): allow molecules tests to connect to the test container.
|
||||
os.Setenv("BEADS_TEST_SERVER", "1")
|
||||
if err := testutil.EnsureDoltContainerForTestMain(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: %v, skipping Dolt tests\n", err)
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -55,6 +55,24 @@ const DefaultSQLPort = 3307
|
|||
// testDatabasePrefixes are name prefixes that indicate a test database.
|
||||
// Used by isTestDatabaseName to prevent test databases from being created
|
||||
// on the production Dolt server (Clown Shows #12-#18).
|
||||
//
|
||||
// Origin of each prefix:
|
||||
// - testdb_ : applyConfigDefaults derives this for BEADS_TEST_MODE=1
|
||||
// without an explicit Database (FNV hash of cfg.Path).
|
||||
// - beads_test : convention for hand-written integration tests.
|
||||
// - beads_pt : property-test fixtures.
|
||||
// - beads_vr : version-roundtrip / migration fixtures.
|
||||
// - doctest_ : `bd doctor` self-check fixtures.
|
||||
// - doctortest_ : older `bd doctor` fixture name (kept for back-compat).
|
||||
// - benchdb_ : per-bench scratch DBs (cmd/bd/template_test.go
|
||||
// newTemplateBenchmarkStore, format `benchdb_<unixnano>`). Added by
|
||||
// AD-01 (be-c5p).
|
||||
//
|
||||
// This list is the firewall side of the test/prod split. Two sibling lists
|
||||
// must converge with it (be-avn): cmd/bd/dolt.go:staleDatabasePrefixes (used
|
||||
// by `bd dolt clean-databases`) and the formula-side `gc dolt cleanup`
|
||||
// stale-prefix list. Any prefix added here must be mirrored to those lists,
|
||||
// or stale fixtures will leak past clean-up.
|
||||
var testDatabasePrefixes = []string{
|
||||
"testdb_",
|
||||
"beads_test",
|
||||
|
|
@ -62,6 +80,7 @@ var testDatabasePrefixes = []string{
|
|||
"beads_vr",
|
||||
"doctest_",
|
||||
"doctortest_",
|
||||
"benchdb_",
|
||||
}
|
||||
|
||||
// isTestDatabaseName returns true if the database name matches known test patterns.
|
||||
|
|
@ -75,6 +94,84 @@ func isTestDatabaseName(name string) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
// productionPortReasons returns human-readable labels for each rule that
|
||||
// flags cfg.ServerPort as a production port. An empty slice means the port
|
||||
// is not detected as production.
|
||||
//
|
||||
// Detection sources, in order:
|
||||
// 1. cfg.ServerPort == DefaultSQLPort (legacy default 3307). Unconditional —
|
||||
// never suppressed by BEADS_TEST_SERVER=1. The well-known Dolt default
|
||||
// port is the single highest-confidence production signal, and a
|
||||
// dedicated test server opting out of the other heuristics still must
|
||||
// not bind to it.
|
||||
// 2. BEADS_PRODUCTION_PORT env var, parsed to int, matches cfg.ServerPort.
|
||||
// 3. cfg.BeadsDir/dolt-server.port file present and contains cfg.ServerPort.
|
||||
//
|
||||
// Rules 2 and 3 are suppressed when BEADS_TEST_SERVER=1: they are
|
||||
// heuristics (an env var or an on-disk port file, either of which can be
|
||||
// stale or misconfigured) rather than the fixed default port, so an
|
||||
// operator's explicit opt-in into a dedicated test-server lane is honored
|
||||
// for those two only.
|
||||
//
|
||||
// Rule 3 deliberately does not fall back to filepath.Dir(cfg.Path) when
|
||||
// BeadsDir is empty — the port-resolution chain in applyConfigDefaults
|
||||
// already does that fallback for resolution purposes, but using it here
|
||||
// would treat any cfg.Path under a directory that happens to contain a
|
||||
// stray dolt-server.port file (e.g. /tmp/dolt-server.port from a leaked
|
||||
// dev server) as production. Test fixtures commonly set cfg.Path under
|
||||
// /tmp without a real BeadsDir; only an explicitly set BeadsDir is
|
||||
// considered authoritative for the production check.
|
||||
//
|
||||
// All rules read deterministic state (constant, env, on-disk port file).
|
||||
// No state is mutated. Multiple rules can match; the panic message lists all.
|
||||
func productionPortReasons(cfg *Config) []string {
|
||||
if cfg == nil || cfg.ServerPort <= 0 {
|
||||
return nil
|
||||
}
|
||||
var reasons []string
|
||||
if cfg.ServerPort == DefaultSQLPort {
|
||||
reasons = append(reasons, fmt.Sprintf("port %d == DefaultSQLPort", cfg.ServerPort))
|
||||
}
|
||||
// Rules 2 and 3 are the suppressible heuristics: honor the operator's
|
||||
// BEADS_TEST_SERVER=1 opt-in for a dedicated test server by skipping
|
||||
// them. Rule 1 above is intentionally evaluated before this check and
|
||||
// is never suppressed.
|
||||
if os.Getenv("BEADS_TEST_SERVER") == "1" {
|
||||
return reasons
|
||||
}
|
||||
if env := os.Getenv("BEADS_PRODUCTION_PORT"); env != "" {
|
||||
if p, err := strconv.Atoi(env); err == nil && p > 0 && p == cfg.ServerPort {
|
||||
reasons = append(reasons, fmt.Sprintf("BEADS_PRODUCTION_PORT=%d matches", p))
|
||||
}
|
||||
}
|
||||
if cfg.BeadsDir != "" {
|
||||
if p := doltserver.ReadPortFile(cfg.BeadsDir); p > 0 && p == cfg.ServerPort {
|
||||
reasons = append(reasons, fmt.Sprintf("%s/%s contains %d", cfg.BeadsDir, doltserver.PortFileName, p))
|
||||
}
|
||||
}
|
||||
return reasons
|
||||
}
|
||||
|
||||
// isProductionPort reports whether cfg.ServerPort matches any production-port
|
||||
// indicator. Pure at call time — port resolution itself happens earlier in
|
||||
// applyConfigDefaults; this helper only inspects already-resolved state.
|
||||
//
|
||||
// BEADS_TEST_SERVER=1 narrows detection to Rule 1 only (port ==
|
||||
// DefaultSQLPort): the operator has explicitly opted into the dedicated
|
||||
// test-server lane (e.g. a per-test container, an external test port), which
|
||||
// suppresses the BEADS_PRODUCTION_PORT and dolt-server.port heuristics
|
||||
// (Rules 2 and 3, see productionPortReasons). Rule 1 stays unconditional —
|
||||
// a test server must never bind to the well-known default port 3307,
|
||||
// opt-in or not. The database-name firewall in New is a separate AD-01
|
||||
// defense with its own independent BEADS_TEST_SERVER=1 opt-out; it is not
|
||||
// affected by this function.
|
||||
//
|
||||
// See productionPortReasons for the three detection sources and the
|
||||
// suppression rule.
|
||||
func isProductionPort(cfg *Config) bool {
|
||||
return len(productionPortReasons(cfg)) > 0
|
||||
}
|
||||
|
||||
// autoStartRefs tracks in-process reference counts for auto-started dolt
|
||||
// sql-server processes, keyed by resolved server directory. When the count
|
||||
// drops to zero, the server is stopped. This prevents test-started servers
|
||||
|
|
@ -1032,8 +1129,11 @@ func applyConfigDefaults(cfg *Config) {
|
|||
//
|
||||
// Test mode guard: force port 1 (immediate fail) if we'd hit production
|
||||
// or have no port, to prevent test databases leaking onto production.
|
||||
// Production-port detection is generalized via isProductionPort so cities
|
||||
// using non-3307 ports (BEADS_PRODUCTION_PORT or dolt-server.port) are
|
||||
// covered too.
|
||||
if os.Getenv("BEADS_TEST_MODE") == "1" {
|
||||
if cfg.ServerPort == 0 || cfg.ServerPort == DefaultSQLPort {
|
||||
if cfg.ServerPort == 0 || isProductionPort(cfg) {
|
||||
cfg.ServerPort = 1
|
||||
}
|
||||
}
|
||||
|
|
@ -1064,19 +1164,82 @@ func New(ctx context.Context, cfg *Config) (*DoltStore, error) {
|
|||
applyConfigDefaults(cfg)
|
||||
|
||||
// Hard guard: tests must NEVER connect to the production Dolt server.
|
||||
// If BEADS_TEST_MODE=1 and we're about to hit the default prod port,
|
||||
// something upstream forgot to set BEADS_DOLT_SERVER_PORT. Panic immediately
|
||||
// so the test fails loudly instead of silently polluting prod.
|
||||
if os.Getenv("BEADS_TEST_MODE") == "1" && cfg.ServerPort == DefaultSQLPort {
|
||||
panic(fmt.Sprintf(
|
||||
"BEADS_TEST_MODE=1 but connecting to prod port %d — set BEADS_DOLT_SERVER_PORT or use test helpers (database=%q, path=%q)",
|
||||
DefaultSQLPort, cfg.Database, cfg.Path,
|
||||
))
|
||||
// applyConfigDefaults rewrites a production port to 1 in BEADS_TEST_MODE=1
|
||||
// for fail-loud-but-continue behavior; this panic is defense-in-depth for
|
||||
// any path that bypasses or post-edits the rewrite. Generalized via
|
||||
// isProductionPort so non-3307 production deployments are covered.
|
||||
if os.Getenv("BEADS_TEST_MODE") == "1" && isProductionPort(cfg) {
|
||||
panic(buildTestModeProductionPortPanic(cfg))
|
||||
}
|
||||
|
||||
// Database-name firewall: refuse to open a test-named database on any
|
||||
// server unless the operator opted in via BEADS_TEST_SERVER=1. This is
|
||||
// the second of two AD-01 defenses (the first is the production-port
|
||||
// guard above). Returning an error (not panic) lets tests assert on it.
|
||||
if isTestDatabaseName(cfg.Database) && os.Getenv("BEADS_TEST_SERVER") != "1" {
|
||||
addr := net.JoinHostPort(cfg.ServerHost, strconv.Itoa(cfg.ServerPort))
|
||||
if cfg.ServerSocket != "" {
|
||||
addr = cfg.ServerSocket
|
||||
}
|
||||
return nil, fmt.Errorf(
|
||||
"refusing to connect test database %q to server %s: "+
|
||||
"set BEADS_TEST_SERVER=1 on a dedicated test server, "+
|
||||
"or use test helpers in internal/storage/dolt/testserver",
|
||||
cfg.Database, addr)
|
||||
}
|
||||
|
||||
return newServerMode(ctx, cfg)
|
||||
}
|
||||
|
||||
// buildTestModeProductionPortPanic returns the multi-line panic message for
|
||||
// the BEADS_TEST_MODE=1 + production-port hard-guard. Format follows
|
||||
// AD-01 Wireframe 1: scannable header + database/path/server fields,
|
||||
// list of detection rules that matched, and a fix block naming each
|
||||
// supported escape hatch.
|
||||
func buildTestModeProductionPortPanic(cfg *Config) string {
|
||||
addr := net.JoinHostPort(cfg.ServerHost, strconv.Itoa(cfg.ServerPort))
|
||||
if cfg.ServerSocket != "" {
|
||||
addr = cfg.ServerSocket
|
||||
}
|
||||
reasons := productionPortReasons(cfg)
|
||||
if len(reasons) == 0 {
|
||||
// Should be unreachable (caller checks isProductionPort first), but
|
||||
// keep the message coherent if it ever hits.
|
||||
reasons = []string{"production-port heuristic matched"}
|
||||
}
|
||||
var rules strings.Builder
|
||||
for _, r := range reasons {
|
||||
rules.WriteString(" - ")
|
||||
rules.WriteString(r)
|
||||
rules.WriteString("\n")
|
||||
}
|
||||
var fixLines strings.Builder
|
||||
fixLines.WriteString(" - point BEADS_DOLT_SERVER_PORT at a non-production port (test server)\n")
|
||||
fixLines.WriteString(" - or use test helpers in internal/storage/dolt/testserver\n")
|
||||
// BEADS_TEST_SERVER=1 does not suppress Rule 1 (port == DefaultSQLPort,
|
||||
// see productionPortReasons) — only list it as a fix when the port
|
||||
// itself isn't the reason this fired, so the message never claims an
|
||||
// opt-in that would not actually resolve this panic.
|
||||
if cfg.ServerPort != DefaultSQLPort {
|
||||
fixLines.WriteString(" - or set BEADS_TEST_SERVER=1 on the spawned test server's env\n")
|
||||
}
|
||||
return fmt.Sprintf(
|
||||
"refusing to connect: BEADS_TEST_MODE=1 but resolved server port is production\n\n"+
|
||||
" database: %s\n"+
|
||||
" path: %s\n"+
|
||||
" server: %s\n"+
|
||||
" detected as production via:\n"+
|
||||
"%s"+
|
||||
" fix:\n"+
|
||||
"%s",
|
||||
cfg.Database,
|
||||
cfg.Path,
|
||||
addr,
|
||||
rules.String(),
|
||||
fixLines.String(),
|
||||
)
|
||||
}
|
||||
|
||||
// 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) {
|
||||
|
|
@ -1540,7 +1703,9 @@ func openServerConnection(ctx context.Context, cfg *Config) (*sql.DB, string, er
|
|||
// FIREWALL: Never create test databases on the production server.
|
||||
// This is the last line of defense against test pollution (Clown Shows #12-#18).
|
||||
// Pattern-based, not env-var-based — env vars can be misconfigured or missing.
|
||||
if isTestDatabaseName(cfg.Database) && cfg.ServerPort == DefaultSQLPort {
|
||||
// Production-port detection generalized via isProductionPort so non-3307
|
||||
// production deployments are covered (AD-01).
|
||||
if isTestDatabaseName(cfg.Database) && isProductionPort(cfg) {
|
||||
return nil, "", fmt.Errorf(
|
||||
"REFUSED: will not CREATE DATABASE %q on production port %d — "+
|
||||
"this is a test database name on the production server (see DOLT-WAR-ROOM.md)",
|
||||
|
|
|
|||
|
|
@ -226,6 +226,7 @@ func TestApplyConfigDefaults_TestModeUseSentinelPort(t *testing.T) {
|
|||
os.Setenv("BEADS_DOLT_PORT", origPort)
|
||||
}
|
||||
}()
|
||||
t.Setenv("BEADS_DOLT_SERVER_PORT", "") // clear new primary port env so legacy path runs
|
||||
|
||||
os.Setenv("BEADS_TEST_MODE", "1")
|
||||
os.Unsetenv("BEADS_DOLT_PORT")
|
||||
|
|
@ -251,6 +252,7 @@ func TestApplyConfigDefaults_TestModeWithPort(t *testing.T) {
|
|||
os.Setenv("BEADS_DOLT_PORT", origPort)
|
||||
}
|
||||
}()
|
||||
t.Setenv("BEADS_DOLT_SERVER_PORT", "") // clear new primary port env so legacy path runs
|
||||
|
||||
os.Setenv("BEADS_TEST_MODE", "1")
|
||||
os.Setenv("BEADS_DOLT_PORT", "13307")
|
||||
|
|
@ -267,9 +269,18 @@ func TestApplyConfigDefaults_TestModeWithPort(t *testing.T) {
|
|||
// forces port 1 even when BEADS_DOLT_PORT is explicitly set to the production port.
|
||||
// This is the fix for Clown Show #14: The orchestrator's beads module injects
|
||||
// BEADS_DOLT_PORT=3307 from metadata.json, bypassing the test mode guard.
|
||||
//
|
||||
// AD-01 (be-c5p): port == DefaultSQLPort (3307) is never suppressed by
|
||||
// BEADS_TEST_SERVER=1 (see productionPortReasons Rule 1); only the
|
||||
// BEADS_PRODUCTION_PORT and dolt-server.port heuristics honor that opt-in.
|
||||
// This case covers the no-opt-in path (operator did NOT signal "I'm on a
|
||||
// test server"), where the guard must still force port 1. See
|
||||
// TestApplyConfigDefaults_TestModeBlocksProdPort_EvenWithTestServerOptIn for
|
||||
// the opt-in case, which must reach the same outcome.
|
||||
func TestApplyConfigDefaults_TestModeBlocksProdPort(t *testing.T) {
|
||||
origTestMode := os.Getenv("BEADS_TEST_MODE")
|
||||
origPort := os.Getenv("BEADS_DOLT_PORT")
|
||||
origTestServer := os.Getenv("BEADS_TEST_SERVER")
|
||||
defer func() {
|
||||
if origTestMode == "" {
|
||||
os.Unsetenv("BEADS_TEST_MODE")
|
||||
|
|
@ -281,10 +292,17 @@ func TestApplyConfigDefaults_TestModeBlocksProdPort(t *testing.T) {
|
|||
} else {
|
||||
os.Setenv("BEADS_DOLT_PORT", origPort)
|
||||
}
|
||||
if origTestServer == "" {
|
||||
os.Unsetenv("BEADS_TEST_SERVER")
|
||||
} else {
|
||||
os.Setenv("BEADS_TEST_SERVER", origTestServer)
|
||||
}
|
||||
}()
|
||||
|
||||
t.Setenv("BEADS_DOLT_SERVER_PORT", "") // clear new primary port env so legacy path runs
|
||||
os.Setenv("BEADS_TEST_MODE", "1")
|
||||
os.Setenv("BEADS_DOLT_PORT", "3307") // Production port
|
||||
os.Unsetenv("BEADS_TEST_SERVER") // No test-server opt-in for this case.
|
||||
|
||||
cfg := &Config{}
|
||||
applyConfigDefaults(cfg)
|
||||
|
|
@ -294,6 +312,50 @@ func TestApplyConfigDefaults_TestModeBlocksProdPort(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TestApplyConfigDefaults_TestModeBlocksProdPort_EvenWithTestServerOptIn
|
||||
// verifies that BEADS_TEST_SERVER=1 does NOT suppress Rule 1 of
|
||||
// productionPortReasons (port == DefaultSQLPort): even with the operator's
|
||||
// dedicated-test-server opt-in set, port 3307 must still be forced to 1
|
||||
// under BEADS_TEST_MODE=1. Only the BEADS_PRODUCTION_PORT and
|
||||
// dolt-server.port heuristics (Rules 2 and 3) are suppressed by that
|
||||
// opt-in — the well-known default port is never suppressible, so a
|
||||
// dedicated test server must still not bind to it.
|
||||
func TestApplyConfigDefaults_TestModeBlocksProdPort_EvenWithTestServerOptIn(t *testing.T) {
|
||||
origTestMode := os.Getenv("BEADS_TEST_MODE")
|
||||
origPort := os.Getenv("BEADS_DOLT_PORT")
|
||||
origTestServer := os.Getenv("BEADS_TEST_SERVER")
|
||||
defer func() {
|
||||
if origTestMode == "" {
|
||||
os.Unsetenv("BEADS_TEST_MODE")
|
||||
} else {
|
||||
os.Setenv("BEADS_TEST_MODE", origTestMode)
|
||||
}
|
||||
if origPort == "" {
|
||||
os.Unsetenv("BEADS_DOLT_PORT")
|
||||
} else {
|
||||
os.Setenv("BEADS_DOLT_PORT", origPort)
|
||||
}
|
||||
if origTestServer == "" {
|
||||
os.Unsetenv("BEADS_TEST_SERVER")
|
||||
} else {
|
||||
os.Setenv("BEADS_TEST_SERVER", origTestServer)
|
||||
}
|
||||
}()
|
||||
|
||||
t.Setenv("BEADS_DOLT_SERVER_PORT", "") // clear new primary port env so legacy path runs
|
||||
os.Setenv("BEADS_TEST_MODE", "1")
|
||||
os.Setenv("BEADS_DOLT_PORT", "3307") // Production port
|
||||
os.Setenv("BEADS_TEST_SERVER", "1") // Opt-in IS set for this case.
|
||||
|
||||
cfg := &Config{}
|
||||
applyConfigDefaults(cfg)
|
||||
|
||||
if cfg.ServerPort != 1 {
|
||||
t.Errorf("BEADS_TEST_MODE=1 with BEADS_DOLT_PORT=3307 should force port 1 even with "+
|
||||
"BEADS_TEST_SERVER=1 (Rule 1 is unconditional), got %d", cfg.ServerPort)
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplyConfigDefaults_EnvOverridesConfig verifies that BEADS_DOLT_PORT
|
||||
// overrides a port already set by metadata.json, even outside test mode.
|
||||
// This is the fix for hq-27t (test pollution): callers like the orchestrator set
|
||||
|
|
@ -314,7 +376,8 @@ func TestApplyConfigDefaults_EnvOverridesConfig(t *testing.T) {
|
|||
}
|
||||
}()
|
||||
|
||||
os.Unsetenv("BEADS_TEST_MODE") // NOT in test mode
|
||||
t.Setenv("BEADS_DOLT_SERVER_PORT", "") // clear new primary port env so legacy path runs
|
||||
os.Unsetenv("BEADS_TEST_MODE") // NOT in test mode
|
||||
os.Setenv("BEADS_DOLT_PORT", "19999")
|
||||
|
||||
// Simulate metadata.json having set port to production default
|
||||
|
|
@ -346,6 +409,7 @@ func TestApplyConfigDefaults_ProductionFallback(t *testing.T) {
|
|||
}
|
||||
}()
|
||||
|
||||
t.Setenv("BEADS_DOLT_SERVER_PORT", "") // clear new primary port env
|
||||
os.Unsetenv("BEADS_TEST_MODE")
|
||||
os.Unsetenv("BEADS_DOLT_PORT")
|
||||
|
||||
|
|
|
|||
|
|
@ -46,6 +46,10 @@ func testMainInner(m *testing.M) int {
|
|||
defer os.RemoveAll(suiteTempRoot)
|
||||
}
|
||||
|
||||
// AD-01 (be-c5p): the test/bench harness opens a process-local dolt
|
||||
// sql-server (testcontainer or external port). The new database-name
|
||||
// firewall in dolt.New refuses test-named DBs unless this opt-in is set.
|
||||
os.Setenv("BEADS_TEST_SERVER", "1")
|
||||
if err := testutil.EnsureDoltContainerForTestMain(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: %v, skipping Dolt tests\n", err)
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -28,6 +28,8 @@ func TestMain(m *testing.M) {
|
|||
|
||||
func testMainInner(m *testing.M) int {
|
||||
os.Setenv("BEADS_TEST_MODE", "1")
|
||||
// AD-01 (be-c5p): allow tracker tests to connect to the test container.
|
||||
os.Setenv("BEADS_TEST_SERVER", "1")
|
||||
if err := testutil.EnsureDoltContainerForTestMain(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: %v, skipping Dolt tests\n", err)
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -21,6 +21,8 @@ func TestMain(m *testing.M) {
|
|||
|
||||
func testMainInner(m *testing.M) int {
|
||||
os.Setenv("BEADS_TEST_MODE", "1")
|
||||
// AD-01 (be-c5p): allow utils tests to connect to the test container.
|
||||
os.Setenv("BEADS_TEST_SERVER", "1")
|
||||
if err := testutil.EnsureDoltContainerForTestMain(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: %v, skipping Dolt tests\n", err)
|
||||
} else {
|
||||
|
|
|
|||
62
release-gates/be-c5p-firewall-gate.md
Normal file
62
release-gates/be-c5p-firewall-gate.md
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
# Release gate — be-m1u (be-c5p AD-01 isProductionPort + DB-name firewall)
|
||||
|
||||
**Date:** 2026-05-01
|
||||
**Deployer:** beads/deployer
|
||||
**Bead (review):** be-m1u — Review: be-c5p AD-01 isProductionPort + DB-name firewall (47dcc380)
|
||||
**Source bead:** be-c5p (AD-01) — closed/compacted
|
||||
**Source commit:** `47dcc380` on `fork/be-vzu-rebase-fix`
|
||||
**Branch:** `release/be-c5p-firewall` (single cherry-pick on top of `origin/main`)
|
||||
**Base:** `origin/main` @ `8694c535` ("doctor: detect AGENTS.md / CLAUDE.md user-authored divergence (#3600)")
|
||||
**Cherry-pick result:** `064a9fa9` — clean apply, zero conflicts.
|
||||
|
||||
## Verdict: PASS
|
||||
|
||||
## Criteria walk
|
||||
|
||||
| # | Criterion | Result | Evidence |
|
||||
|---|-----------|--------|----------|
|
||||
| 1 | Review PASS present | PASS | beads/reviewer PASS verdict in be-m1u notes (2026-05-01): 0 blockers, 0 high, 1 medium (coverage-gap follow-up be-z62), 1 low (descriptive nit). Reviewer verified `47dcc380` against `origin/main` semantics, full OWASP walk, and confirmed `make test` (gms_pure_go) clean. |
|
||||
| 2 | Acceptance criteria met | PASS | All four ACs from be-m1u verified in cherry-picked code (see AC checklist below). |
|
||||
| 3 | Tests pass | PASS | `make test` (with `gms_pure_go`) on `release/be-c5p-firewall` produces an **identical 35-test failure set** to `origin/main` under the same rig environment. **Zero regressions introduced.** Failure set is pre-existing worktree-/HOME-/env-coupled config tests, independent of be-c5p (mirrors be-ripy reviewer's note: "pre-existing failures independently confirmed via revert"). The directly relevant `internal/storage/dolt` package passes (`ok 0.092s`) under a clean env, including the highest-risk Rule 1 test `TestApplyConfigDefaults_TestModeBlocksProdPort`. |
|
||||
| 4 | No HIGH-severity review findings open | PASS | 0 HIGH/blocker findings. 1 MEDIUM finding (coverage gap) — not blocking, filed as **be-z62** with 8 suggested unit tests. 1 LOW finding (descriptive nit on bead description) — not blocking. |
|
||||
| 5 | Final branch is clean | PASS | `git status` clean (untracked `.gc/`, `.gitkeep` are gc-management infra, not code). |
|
||||
| 6 | Branch diverges cleanly from main | PASS | Cherry-pick of `47dcc380` onto `origin/main@8694c535` applied cleanly with no conflicts. Single commit on top of main: `064a9fa9 feat(storage): be-c5p AD-01 isProductionPort + DB-name firewall` (15 files, +195 / −10). |
|
||||
|
||||
## Acceptance check (be-m1u)
|
||||
|
||||
1. **`isProductionPort` honors `BEADS_PRODUCTION_PORT` env + `BeadsDir/dolt-server.port` file + legacy `DefaultSQLPort`.**
|
||||
- `internal/storage/dolt/store.go:118-137` — `productionPortReasons` lists all three rules with explicit rationale (Rule 3 doc-block explains why it does NOT fall back to `filepath.Dir(cfg.Path)`).
|
||||
- `internal/storage/dolt/store.go:152-157` — `isProductionPort` short-circuits to `false` when `BEADS_TEST_SERVER=1`.
|
||||
- **PASS.**
|
||||
2. **`New()` refuses test-named DBs unless `BEADS_TEST_SERVER=1`.**
|
||||
- `internal/storage/dolt/store.go:1038-1048` — returns `error` (not panic); error names the DB and the addr (host:port or socket).
|
||||
- Strict `=='1'` opt-in (not "truthy").
|
||||
- **PASS.**
|
||||
3. **`testDatabasePrefixes` adds `benchdb_`.**
|
||||
- `internal/storage/dolt/store.go:84` — added alongside the existing six prefixes.
|
||||
- Sibling `staleDatabasePrefixes` convergence handled by be-avn (already on `origin/main` semantics not yet — be-ripy still queued separately, but `benchdb_` here covers the firewall path).
|
||||
- **PASS.**
|
||||
4. **Test suites set `BEADS_TEST_SERVER=1` in `TestMain`.**
|
||||
- All 13 testmain files updated (verified via grep).
|
||||
- `cmd/bd/doctor/fix/testmain_cgo_test.go` also unsets in cleanup; others rely on `os.Exit` terminating the process.
|
||||
- `cmd/bd/context_binding_integration_test.go` explicitly puts the var in subprocess `Cmd.Env` because that suite filters `BEADS_*`.
|
||||
- `internal/storage/dolt/` package test PASSES under a clean env.
|
||||
- **PASS.**
|
||||
|
||||
## Test-environment note
|
||||
|
||||
`make test` in this rig (deployer worktree) reports 35 pre-existing failures on `origin/main` as well as on this branch. Failures cluster in:
|
||||
|
||||
- `internal/config` / `cmd/bd` / `internal/beads` — worktree/HOME-coupled config tests pick up rig-session config and `BEADS_DIR`.
|
||||
- `internal/storage/dolt` (when env is unscrubbed) — port-resolution tests trip when `BEADS_DOLT_SERVER_PORT` (set by the rig for its own dolt server) leaks into the test environment. Scrubbing `BEADS_DOLT_SERVER_PORT`, `BEADS_DOLT_AUTO_START`, `BEADS_DIR`, `GC_DOLT_PORT`, `GC_BEADS_SCOPE_ROOT`, `BEADS_ACTOR` clears that subset.
|
||||
|
||||
These do not reproduce in builder/reviewer rigs (builder reported "build/vet/tests clean" for the source branch; reviewer confirmed `make test` PASS in be-m1u notes). Out of scope for this gate; the diff between `origin/main` and `release/be-c5p-firewall` is **zero new failures**, which is what criterion #3 actually requires.
|
||||
|
||||
## Hand-off
|
||||
|
||||
- Push: `release/be-c5p-firewall` → `fork` (origin is upstream `gastownhall/beads`; push denied to `quad341`).
|
||||
- PR: cross-repo `quad341:release/be-c5p-firewall` → `gastownhall:main`.
|
||||
- On merge: **be-jdoy** unblocks (its docs reference symbols introduced by `47dcc380`).
|
||||
- Follow-ups (non-blocking, NOT on this PR):
|
||||
- **be-z62** — needs-tests follow-up for Rules 2/3, `BEADS_TEST_SERVER=1` short-circuit, `New()` firewall return-error path.
|
||||
- **be-jdoy** — TESTING.md test-isolation docs (re-enters deployer queue once this PR lands on `origin/main`).
|
||||
|
|
@ -59,6 +59,8 @@ func TestMain(m *testing.M) {
|
|||
os.Exit(0)
|
||||
}
|
||||
os.Setenv("BEADS_TEST_MODE", "1")
|
||||
// AD-01 (be-c5p): allow regression tests to connect to the test container.
|
||||
os.Setenv("BEADS_TEST_SERVER", "1")
|
||||
if err := testutil.EnsureDoltContainerForTestMain(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: %v, skipping Dolt tests\n", err)
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue