From 4e804885ddfbb690d1f4e2a464ce404592b3a277 Mon Sep 17 00:00:00 2001 From: ecuthiell Date: Wed, 30 Sep 2026 19:30:25 -0600 Subject: [PATCH] test(credentials): make remaining credential-command fixtures native-Windows portable (#5748) (#5749) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * test(credentials): make command fixtures Windows-portable (#5748) Replace POSIX-only credential test commands with a shared shell-portable protocol, exercise their intended failure and non-invocation semantics on native Windows, and add the focused required CI selector. Agent-Signature: Codex-unknown-model-unknown-reasoning on behalf of Ewen Cuthiell * test(ci): require each Windows credential fixture to pass Include the standalone credentialcmd protocol test in the existing Windows step. One package/test map drives both selection and exact terminal checks, so missing, skipped, failed, or duplicate tests cannot report success. Resolve native Go once and reuse it for the test invocation. Bead: saa-cgh.66 Agent-Signature: unknown-model/unknown-reasoning * test(ci): select the first Go application on PATH Hosted Windows images can expose more than one Go application. Preserve setup-go's PATH precedence, select one absolute application and reuse it. Bead: saa-cgh.66 Agent-Signature: unknown-model/unknown-reasoning * chore(bazel): sync BUILD.bazel srcs for the new credentialcmd test helper Base landed the Bazel PR gates (#6881/#6887) after this PR was authored, so the PR's new internal/testutil/credentialcmd package and internal/creds testmain_test.go were never enumerated in any BUILD.bazel. Two already enumerated test srcs (internal/creds/command_test.go and internal/storage/dolt/gateway_credential_test.go) import the new package, so `bazel build //internal/creds:all` fails GoCompilePkg without this. Generated by `make bazel-sync` (gazelle); a second sync is a no-op. The internal/storage/dolt version_staging_completeness_test.go srcs row is pre-existing base drift that gazelle corrects in the same pass, not PR content. * test(credentialcmd): pin the Windows gate's assertions and stop claiming foreign helpers Maintainer review fixes for PR #5749, adoption iteration 1. The synthesis is `approve` with no required changes; all four findings are minor or nit. A maintainer bazel commit already exists on this branch, so the push was owed regardless and none of these cost anything extra. No production code changes. F1 — the new required Windows gate pinned its own wiring but almost none of its assertions. The guard checked three package paths and one test name, so it stayed green if someone dropped six of the nine test names from $expected, deleted the `Action -in 'fail', 'skip'` throw, or deleted the exactly-one-PASS loop -- leaving a required gate whose only real guarantee was "go test exited 0", which a fully skipped suite also satisfies. The pinned slice now carries all nine test names plus both load-bearing guard tokens, keeping the file's existing substring idiom. F2 — the property the fixture exists for was unassertable end to end. Emit is written to produce byte-exact stdout across sh and the Windows .cmd trampoline, but parseCredential runs bytes.TrimSpace before every comparison, so every existing assertion is blind to a trailing newline: a trampoline changed to `echo %OUT%` would keep the whole suite green. TestCommandSourceRealShell now wraps the existing credRunner package seam to capture the helper's stdout as the shell produced it, and asserts it byte-exact. The real shell stays in the loop, so this remains end-to-end rather than becoming a second in-process test. F3 — Dispatch claimed any process where BEADS_TEST_CREDENTIAL_COMMAND_HELPER=1, on the env alone and before inspecting argv, exiting 97 when the sentinel was absent. command() arms that variable in the *parent* process and BEADS_-prefixed variables reach every re-exec'd child through the integration allowlist, so the first composition with a package that re-execs its own test binary -- which internal/storage/dolt already does -- would have died with a wrong-subsystem "missing protocol sentinel". Both gates are now required, and an env-armed process without the sentinel falls through to its own TestMain. The second half of that finding's prescription is deliberately NOT applied. Adding BEADS_TEST_CREDENTIAL_COMMAND_HELPER to helperSubprocessSentinels reds TestHelperSubprocessSentinelsAreComplete: that guard has a reverse-direction control, and its regex looks for a literal os.Getenv("BEADS_..._HELPER") in internal/storage/dolt's own test sources. This sentinel is read in internal/testutil/credentialcmd, so the entry registers as a stale one. Measured, not assumed -- the failure is "helperSubprocessSentinels lists BEADS_TEST_CREDENTIAL_COMMAND_HELPER, but no test source reads it". With the Dispatch gate fixed the entry also buys nothing: a dolt helper child already carries its own sentinel, so isHelperSubprocess is true for it either way. F4 — the exported constructors documented themselves as "returns a unique shell command" while replacing PATH for the rest of the calling test, setting env vars via t.Setenv (which bans t.Parallel), and silently rejecting an empty payload. The shared contract is now stated once above the three of them, and Emit fails fast with an explanation instead of letting the shell drop the empty payload word and the helper exit 97. No caller passes an empty payload today. Each fix was checked by reverting it and confirming its own pin reddens: the dropped test name reds the guard by name; the trailing-newline wrong implementation reds the new raw-stdout assertion while the sibling end-to-end test stays green, which is the blindness the finding described; and reverting the Dispatch gate reproduces `(97, true)`. `make bazel-sync` is a no-op -- every edit landed in a file already enumerated in srcs. --------- Co-authored-by: Gas City Adopt-PR Co-authored-by: Claude Opus 5.5 --- .github/workflows/pr.yml | 38 ++++ internal/creds/BUILD.bazel | 2 + internal/creds/command_test.go | 49 ++++- internal/creds/testmain_test.go | 21 ++ internal/storage/dolt/BUILD.bazel | 1 + .../storage/dolt/gateway_credential_test.go | 31 +-- internal/storage/dolt/testmain_test.go | 11 +- internal/testutil/credentialcmd/BUILD.bazel | 18 ++ .../testutil/credentialcmd/credentialcmd.go | 187 ++++++++++++++++++ .../credentialcmd/credentialcmd_other.go | 105 ++++++++++ .../credentialcmd/credentialcmd_test.go | 81 ++++++++ .../credentialcmd/credentialcmd_windows.go | 77 ++++++++ scripts/ci_workflow_test.go | 50 +++++ 13 files changed, 657 insertions(+), 14 deletions(-) create mode 100644 internal/creds/testmain_test.go create mode 100644 internal/testutil/credentialcmd/BUILD.bazel create mode 100644 internal/testutil/credentialcmd/credentialcmd.go create mode 100644 internal/testutil/credentialcmd/credentialcmd_other.go create mode 100644 internal/testutil/credentialcmd/credentialcmd_test.go create mode 100644 internal/testutil/credentialcmd/credentialcmd_windows.go diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index a67d4d2f4..40d861500 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -615,6 +615,44 @@ jobs: shell: bash run: go test '-tags=gms_pure_go' -count=1 -run '^TestGeneratedHookTimeoutProcessBoundary$' ./cmd/bd + - name: Exercise credential command fixtures on Windows + if: matrix.os == 'windows-latest' + shell: pwsh + env: + CGO_ENABLED: "0" + run: | + $go = (Get-Command go -CommandType Application -ErrorAction Stop | Select-Object -First 1).Source + $hostInfo = @(& $go env GOHOSTOS GOOS) + if ($LASTEXITCODE -ne 0 -or ($hostInfo -join ',') -ne 'windows,windows') { + throw 'Credential fixtures require native Windows Go' + } + $expected = @{ + 'github.com/steveyegge/beads/internal/testutil/credentialcmd' = @('TestProtocol') + 'github.com/steveyegge/beads/internal/creds' = @( + 'TestCommandSourceRealShell', 'TestCredentialCommandFixtureProtocol') + 'github.com/steveyegge/beads/internal/storage/dolt' = @( + 'TestApplyGatewayCredentialCommand', 'TestApplyGatewayCredentialJSONEnvelope', + 'TestApplyGatewayCredentialFailsClosed', 'TestApplyGatewayCredentialPresetWins', + 'TestApplyGatewayCredentialRejectsBadCharToken', 'TestApplyResolvedConfigGatewayCredential') + } + $selector = '^(' + (($expected.Values | ForEach-Object { $_ }) -join '|') + ')$' + $raw = & $go test -json -tags gms_pure_go -count=1 -run $selector @($expected.Keys) + $testStatus = $LASTEXITCODE + $events = @($raw | ForEach-Object { $_ | ConvertFrom-Json -ErrorAction Stop }) + $events | Where-Object Output | ForEach-Object { [Console]::Write($_.Output) } + if ($testStatus -ne 0) { exit $testStatus } + if (@($events | Where-Object { $_.Action -in 'fail', 'skip' }).Count -ne 0) { + throw 'Credential fixtures must pass without skips' + } + foreach ($package in $expected.Keys) { + foreach ($test in $expected[$package]) { + $passes = @($events | Where-Object { + $_.Package -eq $package -and $_.Test -eq $test -and $_.Action -eq 'pass' + }) + if ($passes.Count -ne 1) { throw "Expected one PASS for $package/$test" } + } + } + - name: Check benchmark environment scrubbing if: matrix.os == 'windows-latest' shell: bash diff --git a/internal/creds/BUILD.bazel b/internal/creds/BUILD.bazel index f0fb4c241..8e19e51f6 100644 --- a/internal/creds/BUILD.bazel +++ b/internal/creds/BUILD.bazel @@ -15,6 +15,8 @@ go_test( srcs = [ "command_test.go", "creds_test.go", + "testmain_test.go", ], embed = [":creds"], + deps = ["//internal/testutil/credentialcmd"], ) diff --git a/internal/creds/command_test.go b/internal/creds/command_test.go index 24f5167ca..dbd210051 100644 --- a/internal/creds/command_test.go +++ b/internal/creds/command_test.go @@ -3,8 +3,13 @@ package creds import ( "context" "fmt" + "os" + "path/filepath" + "strings" "testing" "time" + + "github.com/steveyegge/beads/internal/testutil/credentialcmd" ) func TestParseCredential(t *testing.T) { @@ -108,14 +113,37 @@ func TestResolveCredentialTokenPropagatesHelperError(t *testing.T) { } // A real shell command flows end-to-end through CommandSource (no stub), proving the -// sh -c runner and the bare-token path work together. +// platform shell runner and the bare-token path work together. func TestCommandSourceRealShell(t *testing.T) { resetCache(t) - src := CommandSource{Command: "printf s3cr3t", Kind: KindSecret, Label: "TEST_CMD"} + + // Capture the helper's stdout as the shell actually produced it. The whole + // reason Emit exists rather than an echo-style emitter is byte-exact stdout + // across sh and the Windows .cmd trampoline — but parseCredential runs + // bytes.TrimSpace before every comparison (command.go), so every assertion + // on cred.Value is blind to a trailing "\r\n". Without the capture below, + // a trampoline changed to `echo %OUT%` would keep this test green while + // silently breaking the property the fixture was built for. Wrapping the + // existing package seam keeps the real shell in the loop instead of + // replacing it, so this stays an end-to-end assertion. + var rawStdout []byte + origRunner := credRunner + t.Cleanup(func() { credRunner = origRunner }) + credRunner = func(ctx context.Context, command string) ([]byte, error) { + out, err := origRunner(ctx, command) + rawStdout = append([]byte(nil), out...) + return out, err + } + + src := CommandSource{Command: credentialcmd.Emit(t, "s3cr3t"), Kind: KindSecret, Label: "TEST_CMD"} cred, ok, err := src.Resolve(context.Background()) if err != nil || !ok { t.Fatalf("resolve: ok=%v err=%v", ok, err) } + if string(rawStdout) != "s3cr3t" { + t.Fatalf("helper raw stdout = %q, want exactly %q: the fixture must emit the payload with no "+ + "trailing newline on either shell, and parseCredential's TrimSpace hides any that appears", rawStdout, "s3cr3t") + } if cred.Value != "s3cr3t" { t.Fatalf("value = %q, want s3cr3t", cred.Value) } @@ -126,3 +154,20 @@ func TestCommandSourceRealShell(t *testing.T) { t.Fatalf("source = %q, want TEST_CMD", cred.Source) } } + +func TestCredentialCommandFixtureProtocol(t *testing.T) { + resetCache(t) + marker := filepath.Join(t.TempDir(), "marker path with spaces") + src := CommandSource{Command: credentialcmd.Marker(t, marker), Kind: KindSecret, Label: "TEST_MARKER"} + _, ok, err := src.Resolve(context.Background()) + if !ok || err == nil || !strings.Contains(err.Error(), "credential command produced no output") { + t.Fatalf("marker resolve: ok=%v err=%v, want configured empty-output error", ok, err) + } + got, err := os.ReadFile(marker) + if err != nil { + t.Fatalf("read credential command marker: %v", err) + } + if string(got) != "invoked" { + t.Fatalf("credential command marker = %q, want invoked", got) + } +} diff --git a/internal/creds/testmain_test.go b/internal/creds/testmain_test.go new file mode 100644 index 000000000..61cfbd808 --- /dev/null +++ b/internal/creds/testmain_test.go @@ -0,0 +1,21 @@ +package creds + +import ( + "fmt" + "os" + "testing" + + "github.com/steveyegge/beads/internal/testutil/credentialcmd" +) + +func TestMain(m *testing.M) { + if code, ok := credentialcmd.Dispatch(); ok { + os.Exit(code) + } + code := m.Run() + if err := credentialcmd.Cleanup(); err != nil { + fmt.Fprintf(os.Stderr, "credential command fixture cleanup: %v\n", err) + code = 1 + } + os.Exit(code) +} diff --git a/internal/storage/dolt/BUILD.bazel b/internal/storage/dolt/BUILD.bazel index 2ab9ef9d1..3b3395918 100644 --- a/internal/storage/dolt/BUILD.bazel +++ b/internal/storage/dolt/BUILD.bazel @@ -394,6 +394,7 @@ go_test( "//internal/storage/storagecontract", "//internal/storage/versioncontrolops", "//internal/testutil", + "//internal/testutil/credentialcmd", "//internal/types", "//internal/utils", "//internal/workapi", diff --git a/internal/storage/dolt/gateway_credential_test.go b/internal/storage/dolt/gateway_credential_test.go index beb9eccee..873f7722b 100644 --- a/internal/storage/dolt/gateway_credential_test.go +++ b/internal/storage/dolt/gateway_credential_test.go @@ -2,16 +2,18 @@ package dolt import ( "context" + "path/filepath" "strings" "testing" "github.com/steveyegge/beads/internal/configfile" + "github.com/steveyegge/beads/internal/testutil/credentialcmd" ) // A configured credential command resolves the token into the username slot and marks // the connection as targeting a gateway server (with auto-start disabled). func TestApplyGatewayCredentialCommand(t *testing.T) { - t.Setenv("BEADS_DOLT_CREDENTIAL_COMMAND", "printf tok-abc") + t.Setenv("BEADS_DOLT_CREDENTIAL_COMMAND", credentialcmd.Emit(t, "tok-abc")) cfg := &Config{} applied, err := ApplyGatewayCredential(context.Background(), &configfile.Config{}, cfg) if err != nil || !applied { @@ -27,7 +29,7 @@ func TestApplyGatewayCredentialCommand(t *testing.T) { // An ExecCredential/OAuth-style JSON envelope resolves the token. func TestApplyGatewayCredentialJSONEnvelope(t *testing.T) { - t.Setenv("BEADS_DOLT_CREDENTIAL_COMMAND", `printf '{"access_token":"tok-1","expires_in":300}'`) + t.Setenv("BEADS_DOLT_CREDENTIAL_COMMAND", credentialcmd.Emit(t, `{"access_token":"tok-1","expires_in":300}`)) cfg := &Config{} if _, err := ApplyGatewayCredential(context.Background(), &configfile.Config{}, cfg); err != nil { t.Fatal(err) @@ -39,7 +41,7 @@ func TestApplyGatewayCredentialJSONEnvelope(t *testing.T) { // Fail-closed: a failing helper aborts and never leaves a fallback user. func TestApplyGatewayCredentialFailsClosed(t *testing.T) { - t.Setenv("BEADS_DOLT_CREDENTIAL_COMMAND", "false") + t.Setenv("BEADS_DOLT_CREDENTIAL_COMMAND", credentialcmd.Exit23(t)) cfg := &Config{} applied, err := ApplyGatewayCredential(context.Background(), &configfile.Config{}, cfg) if err == nil { @@ -48,15 +50,18 @@ func TestApplyGatewayCredentialFailsClosed(t *testing.T) { if !strings.Contains(err.Error(), "BEADS_DOLT_CREDENTIAL_COMMAND") { t.Fatalf("error should name the command var, got %v", err) } + if !strings.Contains(err.Error(), "exit status 23") { + t.Fatalf("error should preserve helper exit status 23, got %v", err) + } if applied || cfg.ServerUser != "" || cfg.Gateway { t.Fatalf("on failure the config must be untouched: %+v", cfg) } } -// A caller/flag-preset ServerUser wins and the helper is never run (a failing command -// doubles as an exec detector — no error means it never executed). +// A caller/flag-preset ServerUser wins and the helper is never run. func TestApplyGatewayCredentialPresetWins(t *testing.T) { - t.Setenv("BEADS_DOLT_CREDENTIAL_COMMAND", "false") + marker := filepath.Join(t.TempDir(), "credential-invoked") + t.Setenv("BEADS_DOLT_CREDENTIAL_COMMAND", credentialcmd.Marker(t, marker)) cfg := &Config{ServerUser: "preset"} applied, err := ApplyGatewayCredential(context.Background(), &configfile.Config{}, cfg) if err != nil { @@ -65,17 +70,21 @@ func TestApplyGatewayCredentialPresetWins(t *testing.T) { if applied || cfg.ServerUser != "preset" || cfg.Gateway { t.Fatalf("preset user must be preserved untouched: %+v", cfg) } + credentialcmd.AssertMarkerAbsent(t, marker) } // A token containing a DSN-breaking character (: @ /) is refused, not mis-placed as // username/password. func TestApplyGatewayCredentialRejectsBadCharToken(t *testing.T) { - t.Setenv("BEADS_DOLT_CREDENTIAL_COMMAND", "printf tok@host") + t.Setenv("BEADS_DOLT_CREDENTIAL_COMMAND", credentialcmd.Emit(t, "tok@host")) cfg := &Config{} applied, err := ApplyGatewayCredential(context.Background(), &configfile.Config{}, cfg) if err == nil { t.Fatal("expected an error for a token with a DSN-breaking character") } + if !strings.Contains(err.Error(), "contains a character") { + t.Fatalf("error should report DSN character rejection, got %v", err) + } if applied || cfg.ServerUser != "" || cfg.Gateway { t.Fatalf("config must be untouched on rejection: %+v", cfg) } @@ -102,7 +111,7 @@ func TestApplyResolvedConfigGatewayCredential(t *testing.T) { return &configfile.Config{DoltMode: configfile.DoltModeServer} } t.Run("server mode: command sets username + gateway", func(t *testing.T) { - t.Setenv("BEADS_DOLT_CREDENTIAL_COMMAND", "printf tok-xyz") + t.Setenv("BEADS_DOLT_CREDENTIAL_COMMAND", credentialcmd.Emit(t, "tok-xyz")) cfg := &Config{} if err := applyResolvedConfig(context.Background(), t.TempDir(), serverCfg(), cfg); err != nil { t.Fatal(err) @@ -126,9 +135,8 @@ func TestApplyResolvedConfigGatewayCredential(t *testing.T) { // resolves to embedded mode on this box/CI. t.Setenv("BEADS_DOLT_SERVER_MODE", "") t.Setenv("BEADS_DOLT_SHARED_SERVER", "") - // A failing command doubles as an exec detector: if the gate let it run, the - // open would error. It must not, because an embedded store presents no username. - t.Setenv("BEADS_DOLT_CREDENTIAL_COMMAND", "false") + marker := filepath.Join(t.TempDir(), "credential-invoked") + t.Setenv("BEADS_DOLT_CREDENTIAL_COMMAND", credentialcmd.Marker(t, marker)) cfg := &Config{} if err := applyResolvedConfig(context.Background(), t.TempDir(), &configfile.Config{}, cfg); err != nil { t.Fatalf("embedded open must not run the credential command: %v", err) @@ -136,5 +144,6 @@ func TestApplyResolvedConfigGatewayCredential(t *testing.T) { if cfg.Gateway { t.Fatal("embedded open must not be marked a gateway") } + credentialcmd.AssertMarkerAbsent(t, marker) }) } diff --git a/internal/storage/dolt/testmain_test.go b/internal/storage/dolt/testmain_test.go index dc261aa5e..12ce7eb7b 100644 --- a/internal/storage/dolt/testmain_test.go +++ b/internal/storage/dolt/testmain_test.go @@ -10,6 +10,7 @@ import ( "github.com/steveyegge/beads/internal/doltserver" "github.com/steveyegge/beads/internal/testutil" + "github.com/steveyegge/beads/internal/testutil/credentialcmd" ) // testServerPort is the port of the shared test Dolt server (0 = not running). @@ -44,7 +45,15 @@ func isHelperSubprocess() bool { } func TestMain(m *testing.M) { - os.Exit(testMainInner(m)) + if code, ok := credentialcmd.Dispatch(); ok { + os.Exit(code) + } + code := testMainInner(m) + if err := credentialcmd.Cleanup(); err != nil { + fmt.Fprintf(os.Stderr, "credential command fixture cleanup: %v\n", err) + code = 1 + } + os.Exit(code) } func testMainInner(m *testing.M) int { diff --git a/internal/testutil/credentialcmd/BUILD.bazel b/internal/testutil/credentialcmd/BUILD.bazel new file mode 100644 index 000000000..3ea66fd39 --- /dev/null +++ b/internal/testutil/credentialcmd/BUILD.bazel @@ -0,0 +1,18 @@ +load("@rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "credentialcmd", + srcs = [ + "credentialcmd.go", + "credentialcmd_other.go", + "credentialcmd_windows.go", + ], + importpath = "github.com/steveyegge/beads/internal/testutil/credentialcmd", + visibility = ["//:__subpackages__"], +) + +go_test( + name = "credentialcmd_test", + srcs = ["credentialcmd_test.go"], + embed = [":credentialcmd"], +) diff --git a/internal/testutil/credentialcmd/credentialcmd.go b/internal/testutil/credentialcmd/credentialcmd.go new file mode 100644 index 000000000..8795ac3b7 --- /dev/null +++ b/internal/testutil/credentialcmd/credentialcmd.go @@ -0,0 +1,187 @@ +// Package credentialcmd provides shell-portable credential-command fixtures +// for tests that need to exercise the production shell boundary. +package credentialcmd + +import ( + "encoding/base64" + "fmt" + "io" + "os" + "strings" + "sync" + "sync/atomic" + "testing" +) + +const ( + helperProcessEnv = "BEADS_TEST_CREDENTIAL_COMMAND_HELPER" + helperSentinel = "beads-credential-command-helper" + helperTestRun = "-test.run=NoTestsMatchCredentialCommandHelper" + malformedExit = 97 +) + +var ( + commandSequence atomic.Uint64 + fixtureOnce sync.Once + fixture preparedFixture +) + +type preparedFixture struct { + root string + shellName string + shellDir string + shellPath string + helperPath string + targetPath string + targetEnv string + err error +} + +// Dispatch handles an opted-in credential helper invocation. TestMain callers +// must call it before ordinary package setup so the helper process does not +// allocate suite resources. +// +// Both gates must hold before this claims the process: the env var AND the argv +// sentinel. The env gate alone is not sufficient, because command() arms +// helperProcessEnv in the *parent* test process and BEADS_-prefixed variables +// reach every re-exec'd child through integration.FilterEnv's allowlist. A +// package that re-execs its own test binary for an unrelated reason — +// internal/storage/dolt does exactly that, and maintains helperSubprocessSentinels +// for the hazard — would otherwise have its child claimed here and killed with a +// wrong-subsystem exit 97 the moment the two compose. An env-armed process +// without the sentinel is simply not ours, so it falls through to its own +// TestMain rather than being refused. +func Dispatch() (int, bool) { + if os.Getenv(helperProcessEnv) != "1" { + return 0, false + } + + for i, arg := range os.Args { + if arg == helperSentinel { + return runProtocol(os.Args[i+1:], os.Stdout, os.Stderr), true + } + } + return 0, false +} + +// Constructor contract — Emit, Exit23 and Marker all share it, and it is more +// than "returns a string": +// +// - They replace PATH for the remainder of the calling test with the isolated +// shell directory, and set two to three environment variables. Production +// code invoked later in the same test resolves binaries under that narrowed +// PATH, so any lookup it makes is affected. +// - They use t.Setenv, which makes the calling test permanently incompatible +// with t.Parallel. +// - Each returns a *distinct* command string, which is load-bearing: the +// production credential cache is keyed by command text, so identical +// fixtures would otherwise hit a 60s cross-test cache. +// +// Cleanup removes the package process's suite-scoped transport artifacts after +// all tests have completed. +func Cleanup() error { + if fixture.root == "" { + return nil + } + return os.RemoveAll(fixture.root) +} + +// Emit returns a unique shell command that writes value to stdout without a +// trailing newline. See the constructor contract above for the PATH/env side +// effects every constructor here shares. +// +// value must not be empty. The payload is appended as one word, and both sh and +// cmd word-split an empty argument away, so the helper would receive only +// "emit" and exit 97 "malformed protocol" — a confusing way to reach a case the +// production parser rejects anyway ("credential command produced no output"). +// Use Marker for a command that runs but yields nothing usable. +func Emit(t *testing.T, value string) string { + t.Helper() + if value == "" { + t.Fatalf("credentialcmd.Emit: empty value is unsupported; the shell drops the empty payload word " + + "and the helper exits 97. Use Marker for a command that runs and produces no usable output.") + } + return command(t, "emit", []byte(value)) +} + +// Exit23 returns a unique shell command that exits with status 23. See the +// constructor contract above for the PATH/env side effects every constructor +// here shares. +func Exit23(t *testing.T) string { + t.Helper() + return command(t, "exit23", nil) +} + +// Marker returns a unique shell command that writes an invocation marker. See +// the constructor contract above for the PATH/env side effects every +// constructor here shares. +func Marker(t *testing.T, path string) string { + t.Helper() + return command(t, "marker", []byte(path)) +} + +// AssertMarkerAbsent fails when a Marker command ran unexpectedly. +func AssertMarkerAbsent(t *testing.T, path string) { + t.Helper() + if _, err := os.Stat(path); err == nil { + t.Fatalf("credential command ran unexpectedly and wrote %q", path) + } else if !os.IsNotExist(err) { + t.Fatalf("inspect credential command marker %q: %v", path, err) + } +} + +func command(t *testing.T, operation string, payload []byte) string { + t.Helper() + + fixtureOnce.Do(prepareFixture) + if fixture.err != nil { + t.Fatalf("prepare credential command fixture: %v", fixture.err) + } + if !strings.Contains(fixture.helperPath, " ") { + t.Fatalf("credential helper path does not exercise quoting: %q", fixture.helperPath) + } + + executableEnv := fmt.Sprintf("BEADS_TEST_CREDENTIAL_COMMAND_EXE_%d", commandSequence.Add(1)) + executable := configurePlatformCommand(t, executableEnv) + t.Setenv(helperProcessEnv, "1") + + parts := []string{executable, helperTestRun, "--", helperSentinel, operation} + if payload != nil { + parts = append(parts, base64.RawURLEncoding.EncodeToString(payload)) + } + return strings.Join(parts, " ") +} + +func runProtocol(args []string, stdout, stderr io.Writer) int { + malformed := func() int { + fmt.Fprintln(stderr, "credential command helper: malformed protocol") + return malformedExit + } + + switch { + case len(args) == 2 && args[0] == "emit": + payload, err := base64.RawURLEncoding.DecodeString(args[1]) + if err != nil { + return malformed() + } + if _, err := stdout.Write(payload); err != nil { + fmt.Fprintf(stderr, "credential command helper: write stdout: %v\n", err) + return malformedExit + } + return 0 + case len(args) == 1 && args[0] == "exit23": + return 23 + case len(args) == 2 && args[0] == "marker": + payload, err := base64.RawURLEncoding.DecodeString(args[1]) + if err != nil || len(payload) == 0 { + return malformed() + } + if err := os.WriteFile(string(payload), []byte("invoked"), 0o600); err != nil { + fmt.Fprintf(stderr, "credential command helper: write marker: %v\n", err) + return malformedExit + } + return 0 + default: + return malformed() + } +} diff --git a/internal/testutil/credentialcmd/credentialcmd_other.go b/internal/testutil/credentialcmd/credentialcmd_other.go new file mode 100644 index 000000000..83187746c --- /dev/null +++ b/internal/testutil/credentialcmd/credentialcmd_other.go @@ -0,0 +1,105 @@ +//go:build !windows + +package credentialcmd + +import ( + "fmt" + "io" + "os" + "os/exec" + "path/filepath" + "testing" +) + +func prepareFixture() { + fixture.shellName = "sh" + shellSource, err := exec.LookPath(fixture.shellName) + if err != nil { + fixture.err = fmt.Errorf("resolve production credential shell %q: %w", fixture.shellName, err) + return + } + fixture.root, err = os.MkdirTemp("", "beads-credential-command-*") + if err != nil { + fixture.err = fmt.Errorf("create fixture root: %w", err) + return + } + fixture.shellDir = filepath.Join(fixture.root, "credential shell only") + if err := os.MkdirAll(fixture.shellDir, 0o755); err != nil { + fixture.err = fmt.Errorf("create isolated credential shell directory: %w", err) + return + } + fixture.shellPath = filepath.Join(fixture.shellDir, fixture.shellName) + if err := linkOrCopyExecutable(shellSource, fixture.shellPath); err != nil { + fixture.err = fmt.Errorf("install credential shell: %w", err) + return + } + + helperDir := filepath.Join(fixture.root, "credential helper with spaces") + if err := os.MkdirAll(helperDir, 0o755); err != nil { + fixture.err = fmt.Errorf("create credential helper directory: %w", err) + return + } + fixture.helperPath = filepath.Join(helperDir, "credential-helper") + currentExecutable, err := os.Executable() + if err != nil { + fixture.err = fmt.Errorf("resolve current test executable: %w", err) + return + } + if err := linkOrCopyExecutable(currentExecutable, fixture.helperPath); err != nil { + fixture.err = fmt.Errorf("install current test executable: %w", err) + } +} + +func configurePlatformCommand(t *testing.T, executableEnv string) string { + t.Helper() + + t.Setenv("PATH", fixture.shellDir) + resolvedShell, err := exec.LookPath(fixture.shellName) + if err != nil { + t.Fatalf("resolve credential shell from isolated PATH: %v", err) + } + resolvedInfo, err := os.Stat(resolvedShell) + if err != nil { + t.Fatalf("stat resolved credential shell: %v", err) + } + wantInfo, err := os.Stat(fixture.shellPath) + if err != nil { + t.Fatalf("stat installed credential shell: %v", err) + } + if !os.SameFile(resolvedInfo, wantInfo) { + t.Fatalf("credential shell resolved outside isolated PATH: got %q, want %q", resolvedShell, fixture.shellPath) + } + t.Setenv(executableEnv, fixture.helperPath) + return `"$` + executableEnv + `"` +} + +func linkOrCopyExecutable(source, destination string) error { + resolvedSource, err := filepath.EvalSymlinks(source) + if err != nil { + return fmt.Errorf("resolve %q: %w", source, err) + } + if err := os.Symlink(resolvedSource, destination); err == nil { + return nil + } + + input, err := os.Open(resolvedSource) //nolint:gosec // G304: source is the resolved shell or current executable in this test-only fixture. + if err != nil { + return fmt.Errorf("open %q: %w", resolvedSource, err) + } + defer input.Close() + output, err := os.OpenFile(destination, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o755) //nolint:gosec // G302: the copied shell or test helper must be executable. + if err != nil { + return fmt.Errorf("create %q: %w", destination, err) + } + if _, err := io.Copy(output, input); err != nil { + _ = output.Close() + return fmt.Errorf("copy to %q: %w", destination, err) + } + if err := output.Close(); err != nil { + return fmt.Errorf("close %q: %w", destination, err) + } + if err := os.Chmod(destination, 0o755); err != nil { //nolint:gosec // G302: the copied shell or test helper must be executable. + return fmt.Errorf("make %q runnable: %w", destination, err) + } + return nil +} diff --git a/internal/testutil/credentialcmd/credentialcmd_test.go b/internal/testutil/credentialcmd/credentialcmd_test.go new file mode 100644 index 000000000..7fa656920 --- /dev/null +++ b/internal/testutil/credentialcmd/credentialcmd_test.go @@ -0,0 +1,81 @@ +package credentialcmd + +import ( + "bytes" + "encoding/base64" + "os" + "path/filepath" + "strings" + "testing" +) + +func TestProtocol(t *testing.T) { + t.Run("emit preserves exact bytes", func(t *testing.T) { + payload := []byte("token without newline") + var stdout, stderr bytes.Buffer + if code := runProtocol([]string{"emit", base64.RawURLEncoding.EncodeToString(payload)}, &stdout, &stderr); code != 0 { + t.Fatalf("emit exit = %d, stderr=%q", code, stderr.String()) + } + if !bytes.Equal(stdout.Bytes(), payload) { + t.Fatalf("emit stdout = %q, want exact %q", stdout.Bytes(), payload) + } + }) + + t.Run("exit23 is distinctive", func(t *testing.T) { + if code := runProtocol([]string{"exit23"}, &bytes.Buffer{}, &bytes.Buffer{}); code != 23 { + t.Fatalf("exit23 exit = %d, want 23", code) + } + }) + + t.Run("marker decodes a spaced path", func(t *testing.T) { + marker := filepath.Join(t.TempDir(), "marker path with spaces", "invoked") + if err := os.MkdirAll(filepath.Dir(marker), 0o755); err != nil { + t.Fatal(err) + } + encoded := base64.RawURLEncoding.EncodeToString([]byte(marker)) + var stderr bytes.Buffer + if code := runProtocol([]string{"marker", encoded}, &bytes.Buffer{}, &stderr); code != 0 { + t.Fatalf("marker exit = %d, stderr=%q", code, stderr.String()) + } + got, err := os.ReadFile(marker) + if err != nil { + t.Fatal(err) + } + if string(got) != "invoked" { + t.Fatalf("marker content = %q, want invoked", got) + } + }) + + t.Run("malformed protocol exits 97", func(t *testing.T) { + var stderr bytes.Buffer + if code := runProtocol([]string{"emit", "not+rawurl"}, &bytes.Buffer{}, &stderr); code != malformedExit { + t.Fatalf("malformed exit = %d, want %d", code, malformedExit) + } + if !strings.Contains(stderr.String(), "malformed protocol") { + t.Fatalf("malformed stderr = %q", stderr.String()) + } + }) +} + +// TestDispatchIgnoresAnEnvArmedProcessWithoutTheSentinel pins the half of the +// gate that keeps a foreign helper alive. command() arms helperProcessEnv in +// the parent test process, and BEADS_-prefixed variables reach every re-exec'd +// child through the integration env allowlist — so a package that re-execs its +// own test binary for an unrelated reason (internal/storage/dolt does) can find +// itself env-armed without ever carrying our argv sentinel. Claiming it on the +// env alone killed it with a wrong-subsystem exit 97. +func TestDispatchIgnoresAnEnvArmedProcessWithoutTheSentinel(t *testing.T) { + t.Setenv(helperProcessEnv, "1") + + for _, arg := range os.Args { + if arg == helperSentinel { + t.Skipf("this process really is a credential helper (argv carries %q)", helperSentinel) + } + } + + code, claimed := Dispatch() + if claimed || code != 0 { + t.Fatalf("Dispatch() = (%d, %v), want (0, false): the process is env-armed but its argv carries no %q sentinel, "+ + "so it belongs to whoever re-exec'd it and must fall through to its own TestMain", code, claimed, helperSentinel) + } +} diff --git a/internal/testutil/credentialcmd/credentialcmd_windows.go b/internal/testutil/credentialcmd/credentialcmd_windows.go new file mode 100644 index 000000000..b8f18db2e --- /dev/null +++ b/internal/testutil/credentialcmd/credentialcmd_windows.go @@ -0,0 +1,77 @@ +package credentialcmd + +import ( + "fmt" + "os" + "os/exec" + "path/filepath" + "testing" +) + +func prepareFixture() { + fixture.shellName = "cmd.exe" + fixture.shellPath, fixture.err = exec.LookPath(fixture.shellName) + if fixture.err != nil { + fixture.err = fmt.Errorf("resolve production credential shell %q: %w", fixture.shellName, fixture.err) + return + } + fixture.shellDir = filepath.Dir(fixture.shellPath) + fixture.root, fixture.err = os.MkdirTemp("", "beads-credential-command-*") + if fixture.err != nil { + fixture.err = fmt.Errorf("create fixture root: %w", fixture.err) + return + } + helperDir := filepath.Join(fixture.root, "credential helper with spaces") + if err := os.MkdirAll(helperDir, 0o755); err != nil { + fixture.err = fmt.Errorf("create credential helper directory: %w", err) + return + } + fixture.helperPath = filepath.Join(helperDir, "credential-helper.cmd") + fixture.targetPath, fixture.err = os.Executable() + if fixture.err != nil { + fixture.err = fmt.Errorf("resolve current test executable: %w", fixture.err) + return + } + fixture.targetEnv = fmt.Sprintf("BEADS_TEST_CREDENTIAL_COMMAND_TARGET_%d", os.Getpid()) + trampoline := "@echo off\r\n\"%" + fixture.targetEnv + "%\" %*\r\nexit /b %errorlevel%\r\n" + if err := os.WriteFile(fixture.helperPath, []byte(trampoline), 0o600); err != nil { + fixture.err = fmt.Errorf("write credential helper trampoline: %w", err) + } +} + +func configurePlatformCommand(t *testing.T, executableEnv string) string { + t.Helper() + + // Restrict lookup to the directory containing the mandatory production + // shell. Unlike the Unix fixture, Windows cannot isolate cmd.exe in a copied + // directory without creating a suspicious system-binary copy chain, so + // explicitly prove the ambient fixture utilities remain unavailable. + t.Setenv("PATH", fixture.shellDir) + resolvedShell, err := exec.LookPath(fixture.shellName) + if err != nil { + t.Fatalf("resolve credential shell from restricted PATH: %v", err) + } + resolvedInfo, err := os.Stat(resolvedShell) + if err != nil { + t.Fatalf("stat resolved credential shell: %v", err) + } + wantInfo, err := os.Stat(fixture.shellPath) + if err != nil { + t.Fatalf("stat production credential shell: %v", err) + } + if !os.SameFile(resolvedInfo, wantInfo) { + t.Fatalf("credential shell resolved outside restricted PATH: got %q, want %q", resolvedShell, fixture.shellPath) + } + for _, forbidden := range []string{"printf", "false"} { + if path, err := exec.LookPath(forbidden); err == nil { + t.Fatalf("ambient fixture utility %q remains available at %q", forbidden, path) + } + } + + t.Setenv(fixture.targetEnv, fixture.targetPath) + // Let cmd.exe expand quotes stored in the variable value. Literal quotes in + // exec.Command's single /C argument are otherwise escaped by Go's Windows + // argv encoder before cmd.exe parses them. + t.Setenv(executableEnv, `"`+fixture.helperPath+`"`) + return `%` + executableEnv + `%` +} diff --git a/scripts/ci_workflow_test.go b/scripts/ci_workflow_test.go index fddbbf2b7..faf680c69 100644 --- a/scripts/ci_workflow_test.go +++ b/scripts/ci_workflow_test.go @@ -317,6 +317,56 @@ func TestPRWorkflowExercisesWindowsEnvironmentHelpers(t *testing.T) { } } +func TestPRPreflightPlatformsExercisesCredentialCommandFixturesOnWindows(t *testing.T) { + workflow := readCIWorkflow(t, "pr.yml") + job := workflow.job(t, "pr-preflight-platforms") + step := job.step(t, "Exercise credential command fixtures on Windows") + + if step.If != "matrix.os == 'windows-latest'" { + t.Errorf("credential command fixture selector = %q, want native Windows only", step.If) + } + if step.Shell != "pwsh" { + t.Errorf("credential command fixture shell = %q, want PowerShell", step.Shell) + } + if step.ContinueOnError != nil && step.ContinueOnError != false { + t.Error("credential command fixture step may not continue on error") + } + if got := step.Env["CGO_ENABLED"]; got != "0" { + t.Errorf("credential command fixture CGO_ENABLED = %q, want 0", got) + } + // Pin the step's assertions, not just its wiring. Pinning only the packages + // and one test name left a required gate whose sole surviving guarantee was + // "go test exited 0" — which a fully skipped suite also satisfies. Dropping + // six of the nine names from $expected, deleting the fail/skip throw, or + // deleting the exactly-one-PASS loop all kept this test green. Every name + // below is a test the Windows job must actually observe passing, so a + // selector that narrows silently now fails here; widen the two together. + for _, required := range []string{ + "internal/testutil/credentialcmd", "TestProtocol", + "internal/creds", + "TestCommandSourceRealShell", "TestCredentialCommandFixtureProtocol", + "internal/storage/dolt", + "TestApplyGatewayCredentialCommand", "TestApplyGatewayCredentialJSONEnvelope", + "TestApplyGatewayCredentialFailsClosed", "TestApplyGatewayCredentialPresetWins", + "TestApplyGatewayCredentialRejectsBadCharToken", "TestApplyResolvedConfigGatewayCredential", + // The two load-bearing guards inside the step body. + "'fail', 'skip'", "Expected one PASS", + } { + if !strings.Contains(step.Run, required) { + t.Errorf("credential command fixture step omits %q", required) + } + } + + gate := workflow.job(t, "ci-gate") + gateEnv := gate.step(t, "Evaluate CI gate").Env + if !contains(gate.Needs, "pr-preflight-platforms") || + gateEnv["PR_PREFLIGHT_PLATFORMS"] != "${{ needs.pr-preflight-platforms.result }}" || + !contains(strings.Fields(gateEnv["CI_GATE_REQUIRED"]), "PR_PREFLIGHT_PLATFORMS") { + t.Errorf("credential command fixture job is not required by ci-gate: needs=%v result=%q required=%q", + gate.Needs, gateEnv["PR_PREFLIGHT_PLATFORMS"], gateEnv["CI_GATE_REQUIRED"]) + } +} + func TestPRCIGateRequiresWindowsGlobalPrimeOverride(t *testing.T) { workflow := readCIWorkflow(t, "pr.yml") job := workflow.job(t, "test-windows-liveness")