* 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>
146 lines
4.4 KiB
Go
146 lines
4.4 KiB
Go
//go:build cgo
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/steveyegge/beads/internal/configfile"
|
|
"github.com/steveyegge/beads/internal/storage/dolt"
|
|
"github.com/steveyegge/beads/internal/types"
|
|
)
|
|
|
|
func filteredEnvForContextBinding(keys ...string) []string {
|
|
strip := make(map[string]struct{}, len(keys))
|
|
for _, key := range keys {
|
|
strip[key+"="] = struct{}{}
|
|
}
|
|
|
|
env := os.Environ()
|
|
filtered := make([]string, 0, len(env))
|
|
for _, entry := range env {
|
|
if strings.HasPrefix(entry, "BEADS_") || strings.HasPrefix(entry, "BD_") {
|
|
continue
|
|
}
|
|
trim := false
|
|
for prefix := range strip {
|
|
if strings.HasPrefix(entry, prefix) {
|
|
trim = true
|
|
break
|
|
}
|
|
}
|
|
if !trim {
|
|
filtered = append(filtered, entry)
|
|
}
|
|
}
|
|
return append(filtered, "BD_DISABLE_METRICS=1", "BD_DISABLE_EVENT_FLUSH=1")
|
|
}
|
|
|
|
func TestListExplicitDBPathRebindsTargetContext(t *testing.T) {
|
|
if testDoltServerPort == 0 {
|
|
t.Skip("Dolt test server not available, skipping")
|
|
}
|
|
|
|
tmpDir := t.TempDir()
|
|
callerRepo := filepath.Join(tmpDir, "caller")
|
|
callerBeadsDir := filepath.Join(callerRepo, ".beads")
|
|
writeTestConfigYAML(t, callerBeadsDir, "dolt.auto-commit: invalid\nactor: caller-actor\n")
|
|
if err := os.WriteFile(filepath.Join(callerBeadsDir, ".env"), []byte("BEADS_DOLT_SERVER_PORT=1\n"), 0o600); err != nil {
|
|
t.Fatalf("write caller .env: %v", err)
|
|
}
|
|
|
|
targetRepo := filepath.Join(tmpDir, "target")
|
|
targetBeadsDir := filepath.Join(targetRepo, ".beads")
|
|
writeTestConfigYAML(t, targetBeadsDir, "dolt.auto-commit: off\nactor: target-actor\n")
|
|
database := uniqueTestDBName(t)
|
|
if err := (&configfile.Config{
|
|
Backend: configfile.BackendDolt,
|
|
DoltMode: configfile.DoltModeServer,
|
|
DoltServerHost: "127.0.0.1",
|
|
DoltServerPort: testDoltServerPort,
|
|
DoltDatabase: database,
|
|
}).Save(targetBeadsDir); err != nil {
|
|
t.Fatalf("save target metadata: %v", err)
|
|
}
|
|
|
|
ctx := context.Background()
|
|
testStore, err := dolt.New(ctx, &dolt.Config{
|
|
Path: filepath.Join(targetBeadsDir, "dolt"),
|
|
BeadsDir: targetBeadsDir,
|
|
ServerHost: "127.0.0.1",
|
|
ServerPort: testDoltServerPort,
|
|
Database: database,
|
|
CreateIfMissing: true,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create test store: %v", err)
|
|
}
|
|
defer func() {
|
|
_ = testStore.Close()
|
|
dropTestDatabase(database, testDoltServerPort)
|
|
}()
|
|
if err := testStore.SetConfig(ctx, "issue_prefix", "ctx"); err != nil {
|
|
t.Fatalf("set issue_prefix: %v", err)
|
|
}
|
|
now := time.Now()
|
|
nowIssue := &types.Issue{
|
|
ID: "ctx-1",
|
|
Title: "Context binding proof",
|
|
Description: "Proves explicit --db commands use the target workspace config",
|
|
Status: types.StatusOpen,
|
|
Priority: 1,
|
|
IssueType: types.TypeTask,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
if err := testStore.CreateIssue(ctx, nowIssue, "test-user"); err != nil {
|
|
t.Fatalf("create issue: %v", err)
|
|
}
|
|
|
|
binPath := filepath.Join(t.TempDir(), "bd-under-test")
|
|
packageDir, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("getwd: %v", err)
|
|
}
|
|
buildCmd := exec.Command("go", "build", "-o", binPath, ".")
|
|
buildCmd.Dir = packageDir
|
|
buildOut, err := buildCmd.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("go build failed: %v\n%s", err, buildOut)
|
|
}
|
|
|
|
listCmd := exec.Command(binPath, "list", "--db", filepath.Join(targetBeadsDir, "dolt"), "--json")
|
|
listCmd.Dir = callerRepo
|
|
listCmd.Env = append(filteredEnvForContextBinding("BEADS_DIR", "BEADS_DB", "BD_DB", "BEADS_DOLT_SERVER_PORT", "BEADS_DOLT_SERVER_DATABASE"),
|
|
"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=",
|
|
)
|
|
output, err := listCmd.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("bd list failed: %v\n%s", err, output)
|
|
}
|
|
if !strings.Contains(string(output), "Context binding proof") {
|
|
t.Fatalf("expected list output to include target issue\n%s", output)
|
|
}
|
|
|
|
if _, err := os.Stat(filepath.Join(callerBeadsDir, localVersionFile)); err == nil {
|
|
t.Fatalf("caller workspace unexpectedly created %s", filepath.Join(callerBeadsDir, localVersionFile))
|
|
} else if !os.IsNotExist(err) {
|
|
t.Fatalf("stat caller %s: %v", localVersionFile, err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(targetBeadsDir, localVersionFile)); err != nil {
|
|
t.Fatalf("target workspace should create %s: %v", localVersionFile, err)
|
|
}
|
|
}
|