diff --git a/e2e/framework/command.go b/e2e/framework/command.go index 1045af890a..f9235a9e36 100644 --- a/e2e/framework/command.go +++ b/e2e/framework/command.go @@ -234,20 +234,21 @@ func (f *Framework) DevsySSHOnce( workspace string, command string, ) (string, error) { - out, stderr, err := f.ExecCommandCapture(ctx, []string{ + out, stderr, err := f.ExecCommandDiagnosticCapture(ctx, []string{ cmdWorkspace, cmdSSH, workspace, flagCommand, command, flagDebug, }) if err != nil { + diagnostic := externalCommandDiagnostic(stderr) if strings.Contains(stderr, "AGENT_INJECTION_RUNTIME_UNAVAILABLE") || strings.Contains(stderr, "PODMAN_ROOTFUL_RUNTIME_POISONED") { return "", fmt.Errorf( "SSH_RUNTIME_UNAVAILABLE: %w (stderr: %s)", errors.Join(docker.ErrRuntimeUnavailable, err), - stderr, + diagnostic, ) } if stderr != "" { - return "", fmt.Errorf("devsy ssh failed: %w (stderr: %s)", err, stderr) + return "", fmt.Errorf("devsy ssh failed: %w (stderr: %s)", err, diagnostic) } return "", fmt.Errorf("devsy ssh failed: %w", err) } @@ -406,10 +407,10 @@ func providerNameFromAddArgs(args []string) string { func (f *Framework) DevsyProviderDelete(ctx context.Context, args ...string) error { baseArgs := []string{cmdProvider, cmdDelete} baseArgs = append(baseArgs, args...) - _, stderr, err := f.ExecCommandCapture(ctx, baseArgs) + _, stderr, err := f.ExecCommandDiagnosticCapture(ctx, baseArgs) if err != nil { if strings.Contains(stderr, "because workspace") { - return fmt.Errorf("%w: %s", errProviderInUse, stderr) + return fmt.Errorf("%w: %s", errProviderInUse, externalCommandDiagnostic(stderr)) } return err } @@ -603,10 +604,14 @@ func ImportGpgKey(privateKeyPath string) error { // #nosec G204 -- test imports a controlled key path out, err := exec.Command("gpg", "--batch", "--import", privateKeyPath).CombinedOutput() if err != nil { - return fmt.Errorf("import gpg private key: %s: %w", out, err) + return fmt.Errorf( + "import gpg private key: %s: %w", + externalCommandDiagnostic(string(out)), + err, + ) } if out, err := exec.Command("gpg-connect-agent", "/bye").CombinedOutput(); err != nil { - return fmt.Errorf("start gpg-agent: %s: %w", out, err) + return fmt.Errorf("start gpg-agent: %s: %w", externalCommandDiagnostic(string(out)), err) } return nil } @@ -615,7 +620,7 @@ func (f *Framework) DevsySSHGpgSecretKeyForwarded( ctx context.Context, workspace, expectedFingerprint string, ) error { - stdout, _, err := f.ExecCommandCapture(ctx, []string{ + stdout, _, err := f.ExecCommandDiagnosticCapture(ctx, []string{ cmdWorkspace, cmdSSH, names.Flag(names.AgentForwarding), @@ -631,7 +636,7 @@ func (f *Framework) DevsySSHGpgSecretKeyForwarded( return fmt.Errorf( "gpg agent forwarding failed: secret key %s not reachable in container, got: %q", expectedFingerprint, - stdout, + externalCommandDiagnostic(stdout), ) } return nil diff --git a/e2e/framework/command_diagnostics_test.go b/e2e/framework/command_diagnostics_test.go new file mode 100644 index 0000000000..b96499f66b --- /dev/null +++ b/e2e/framework/command_diagnostics_test.go @@ -0,0 +1,181 @@ +package framework + +import ( + "os" + "os/exec" + "path/filepath" + "runtime" + "testing" + + "github.com/devsy-org/devsy/pkg/docker" + "github.com/stretchr/testify/require" +) + +const ( + diagnosticFixtureExecutable = "devsy" + diagnosticFixtureValue = "external-tool-fixture" + diagnosticFixtureText = "public command detail" + diagnosticFixture = "Authorization:\n Bearer " + diagnosticFixtureValue + "\n" + diagnosticFixtureText + fixtureCommandFailure = "printf '%s' \"$DEVSY_DIAGNOSTIC_FIXTURE\"\nexit 7\n" + fixtureCommandSuccess = "exit 0\n" +) + +func TestGPGCommandFailuresMaskFoldedDiagnostics(t *testing.T) { + for _, tc := range []struct { + name string + importCommand string + agentCommand string + detail string + }{ + {"import", fixtureCommandFailure, fixtureCommandSuccess, "import gpg private key"}, + {"connect agent", fixtureCommandSuccess, fixtureCommandFailure, "start gpg-agent"}, + } { + t.Run(tc.name, func(t *testing.T) { + bin := externalCommandFixture(t) + writeDiagnosticFixture(t, bin, "gpg", tc.importCommand) + writeDiagnosticFixture(t, bin, "gpg-connect-agent", tc.agentCommand) + assertMaskedExternalFailure( + t, + ImportGpgKey(filepath.Join(bin, "fixture.key")), + tc.detail, + ) + }) + } +} + +func TestSSHAgentCommandFailuresMaskFoldedDiagnostics(t *testing.T) { + bin := externalCommandFixture(t) + writeDiagnosticFixture(t, bin, "ssh-keygen", fixtureCommandFailure) + writeDiagnosticFixture(t, bin, "ssh-add", fixtureCommandFailure) + key := filepath.Join(bin, "fixture.key") + for _, tc := range []struct { + name string + run func() error + }{ + {"ssh-keygen", func() error { return generateAgentKey(key) }}, + {"ssh-add", func() error { return addKeyToAgent(filepath.Join(bin, "socket"), key) }}, + } { + t.Run(tc.name, func(t *testing.T) { + assertMaskedExternalFailure(t, tc.run(), tc.name) + }) + } +} + +func TestForwardedGPGKeyMismatchMasksFoldedDiagnostics(t *testing.T) { + bin := externalCommandFixture(t) + writeDiagnosticFixture( + t, + bin, + diagnosticFixtureExecutable, + "printf '%s' \"$DEVSY_DIAGNOSTIC_FIXTURE\"\n", + ) + f := &Framework{DevsyBinDir: bin, DevsyBinName: diagnosticFixtureExecutable} + err := f.DevsySSHGpgSecretKeyForwarded(t.Context(), "fixture-workspace", "expected-fingerprint") + require.ErrorContains(t, err, "expected-fingerprint not reachable") + require.ErrorContains(t, err, diagnosticFixtureText) + require.NotContains(t, err.Error(), diagnosticFixtureValue) + require.Contains(t, err.Error(), "***") +} + +func TestFrameworkCommandFailuresMaskDiagnosticCopies(t *testing.T) { + bin := externalCommandFixture(t) + writeDiagnosticFixture(t, bin, diagnosticFixtureExecutable, "printf '%s' 'functional stdout'\n"+ + "printf '%s\\n' \"$DEVSY_DIAGNOSTIC_DETAIL\" \"$DEVSY_DIAGNOSTIC_FIXTURE\" >&2\nexit 7\n") + f := &Framework{DevsyBinDir: bin, DevsyBinName: diagnosticFixtureExecutable} + for _, tc := range []struct { + name string + detail string + cause error + run func(*testing.T, *Framework) error + }{ + {"ssh", "ordinary SSH failure", nil, fixtureSSHOnce}, + {"runtime unavailable", "AGENT_INJECTION_RUNTIME_UNAVAILABLE", docker.ErrRuntimeUnavailable, fixtureSSHOnce}, + {"provider in use", "because workspace exists", errProviderInUse, fixtureProviderDelete}, + {"JSON result", "JSON command failure", nil, fixtureCommandJSON}, + {"setup logs", "setup logs failure", nil, fixtureSetupLogs}, + {"workspace edit", "workspace edit failure", nil, fixtureWorkspaceEdit}, + } { + t.Run(tc.name, func(t *testing.T) { + t.Setenv("DEVSY_DIAGNOSTIC_DETAIL", tc.detail) + err := tc.run(t, f) + require.ErrorContains(t, err, tc.detail) + require.ErrorContains(t, err, diagnosticFixtureText) + require.NotContains(t, err.Error(), diagnosticFixtureValue) + if tc.cause != nil { + require.ErrorIs(t, err, tc.cause) + } + }) + } +} + +func fixtureSSHOnce(t *testing.T, f *Framework) error { + _, err := f.DevsySSHOnce(t.Context(), "fixture-workspace", "true") + return err +} + +func fixtureProviderDelete(t *testing.T, f *Framework) error { + return f.DevsyProviderDelete(t.Context(), "fixture-provider") +} + +func fixtureCommandJSON(t *testing.T, f *Framework) error { + _, err := f.ExecCommandJSON(t.Context(), []string{"fixture"}) + return err +} + +func fixtureSetupLogs(t *testing.T, f *Framework) error { + t.Helper() + stdout, stderr, err := f.DevsySetupLogs(t.Context()) + require.Equal(t, "functional stdout", stdout) + require.Contains(t, stderr, diagnosticFixture) + return err +} + +func fixtureWorkspaceEdit(t *testing.T, f *Framework) error { + _, err := f.DevsyWorkspaceEditJSON(t.Context(), "fixture-workspace") + return err +} + +func TestWorkspaceEditDecodeErrorMasksDiagnosticCopy(t *testing.T) { + bin := externalCommandFixture(t) + writeDiagnosticFixture( + t, + bin, + diagnosticFixtureExecutable, + "printf '%s' \"$DEVSY_DIAGNOSTIC_FIXTURE\"\n", + ) + f := &Framework{DevsyBinDir: bin, DevsyBinName: diagnosticFixtureExecutable} + _, err := f.DevsyWorkspaceEditResult(t.Context(), "fixture-workspace") + require.ErrorContains(t, err, "decode workspace edit result") + require.ErrorContains(t, err, diagnosticFixtureText) + require.NotContains(t, err.Error(), diagnosticFixtureValue) +} + +func externalCommandFixture(t *testing.T) string { + t.Helper() + if runtime.GOOS == "windows" { + t.Skip("requires POSIX executable fixtures") + } + bin := t.TempDir() + t.Setenv("PATH", bin) + t.Setenv("DEVSY_DIAGNOSTIC_FIXTURE", diagnosticFixture) + return bin +} + +func writeDiagnosticFixture(t *testing.T, bin, name, script string) { + t.Helper() + root, err := os.OpenRoot(bin) + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, root.Close()) }) + require.NoError(t, root.WriteFile(name, []byte("#!/bin/sh\n"+script), 0o700)) +} + +func assertMaskedExternalFailure(t *testing.T, err error, detail string) { + t.Helper() + require.ErrorContains(t, err, detail) + var exit *exec.ExitError + require.ErrorAs(t, err, &exit) + require.Equal(t, 7, exit.ExitCode()) + require.ErrorContains(t, err, diagnosticFixtureText) + require.NotContains(t, err.Error(), diagnosticFixtureValue) + require.Contains(t, err.Error(), "***") +} diff --git a/e2e/framework/dockerwsl/setup_test.go b/e2e/framework/dockerwsl/setup_test.go index 8e1ab10230..d092586496 100644 --- a/e2e/framework/dockerwsl/setup_test.go +++ b/e2e/framework/dockerwsl/setup_test.go @@ -19,6 +19,7 @@ const ( wantEndpoint = "ssh://devsyci@" + testAlias envLineSSHHost = envSSHDockerHost + "=" + wantEndpoint + "\n" envLineDaemon = envWSLDaemonID + "=" + testDaemonID + "\n" + envLineSSHHostLog = envSSHDockerHost + "=ssh://[REDACTED]@" + testAlias + "\n" ) type DockerWSLSetupSuite struct { @@ -185,7 +186,8 @@ func (s *DockerWSLSetupSuite) TestLocalModeNeedsNoGitHubFiles() { s.Require(). NoError(s.rig.controller(func(o *dockerWSLOptions) { o.RunID = testRunID }).setup(context.Background())) - s.Contains(s.rig.out.String(), "local env: "+envLineSSHHost) + s.Contains(s.rig.out.String(), "local env: "+envLineSSHHostLog) + s.NotContains(s.rig.out.String(), "ssh://devsyci@") s.Contains(s.rig.out.String(), "local path: "+fixtureFile("docker-cli", "docker")) s.Empty(s.rig.files.Text(testGHEnv)) } diff --git a/e2e/framework/exec.go b/e2e/framework/exec.go index 7fce2515dd..15b5b6e429 100644 --- a/e2e/framework/exec.go +++ b/e2e/framework/exec.go @@ -5,12 +5,15 @@ import ( "context" "encoding/json" "fmt" + "io" "os" "os/exec" "path/filepath" "strings" "time" + "github.com/devsy-org/devsy/internal/diagnosticredact" + "github.com/devsy-org/devsy/logging/redact" "github.com/devsy-org/devsy/pkg/docker" "github.com/devsy-org/devsy/pkg/livetee" ) @@ -96,6 +99,21 @@ func (f *Framework) ExecCommandContaining( // ExecCommandCapture executes the command string with the devsy test binary, and returns // stdout, stderr, and any error that occurred. func (f *Framework) ExecCommandCapture(ctx context.Context, args []string) (string, string, error) { + return f.execCommandCapture(ctx, args, false) +} + +func (f *Framework) ExecCommandDiagnosticCapture( + ctx context.Context, + args []string, +) (string, string, error) { + return f.execCommandCapture(ctx, args, true) +} + +func (f *Framework) execCommandCapture( + ctx context.Context, + args []string, + diagnostic bool, +) (string, string, error) { var execOut bytes.Buffer var execErr bytes.Buffer @@ -103,25 +121,54 @@ func (f *Framework) ExecCommandCapture(ctx context.Context, args []string) (stri cmd := exec.CommandContext(ctx, filepath.Join(f.DevsyBinDir, f.DevsyBinName), args...) docker.PrepareForGroupCancellation(cmd) cmd.WaitDelay = execWaitDelay - outTee := livetee.New(&execOut, os.Stdout) - cmd.Stdout = outTee - errTee := livetee.New(&execErr, os.Stderr) - cmd.Stderr = errTee + var redactor *redact.Redactor + if diagnostic { + redactor = redact.New(redact.Options{Environment: cmd.Environ()}) + } + stdout, closeStdout := captureCommandOutput(&execOut, os.Stdout, redactor) + stderr, closeStderr := captureCommandOutput(&execErr, os.Stderr, redactor) + cmd.Stdout = stdout + cmd.Stderr = stderr err := cmd.Run() - f.liveStdoutDropped.Add(outTee.Close()) - f.liveStderrDropped.Add(errTee.Close()) + f.liveStdoutDropped.Add(closeStdout()) + f.liveStderrDropped.Add(closeStderr()) if err != nil && ctx.Err() != nil { err = ctx.Err() } return execOut.String(), execErr.String(), err } +func captureCommandOutput( + capture, live io.Writer, + redactor *redact.Redactor, +) (io.Writer, func() int64) { + if redactor == nil { + tee := livetee.New(capture, live) + return tee, tee.Close + } + tee := livetee.New(io.Discard, live) + diagnostic := newLiveDiagnosticCapture(tee, redactor) + return io.MultiWriter(capture, diagnostic), diagnostic.close +} + +func externalCommandDiagnostic(value string) string { + redactor := redact.New(redact.Options{Environment: os.Environ()}) + return diagnosticredact.Record(redactor, maskRecordText(redactor, value)) +} + // ExecCommandJSON runs the devsy test binary and decodes its stdout as a JSON object. func (f *Framework) ExecCommandJSON(ctx context.Context, args []string) (map[string]any, error) { - stdout, stderr, err := f.ExecCommandCapture(ctx, args) + stdout, stderr, err := f.ExecCommandDiagnosticCapture(ctx, args) if err != nil { - return nil, fmt.Errorf("devsy %s: %w (stderr: %s)", strings.Join(args, " "), err, stderr) + return nil, fmt.Errorf( + "devsy %s: %w (stderr: %s)", + externalCommandDiagnostic( + strings.Join(args, " "), + ), + err, + externalCommandDiagnostic(stderr), + ) } var result map[string]any diff --git a/e2e/framework/exec_capture_test.go b/e2e/framework/exec_capture_test.go index 330d988f14..8de6defc37 100644 --- a/e2e/framework/exec_capture_test.go +++ b/e2e/framework/exec_capture_test.go @@ -14,12 +14,35 @@ import ( "github.com/stretchr/testify/suite" ) +const ( + captureChildArg = "-test.run=^TestCaptureChild$" + capturePrefix = "Authorization:\n Bearer " + captureSuffix = "\npublic capture detail\n" +) + func TestCaptureChild(t *testing.T) { switch os.Getenv("DEVSY_CAPTURE_CHILD") { case "exit": _, _ = fmt.Fprint(os.Stdout, "stdout") _, _ = fmt.Fprint(os.Stderr, "stderr") os.Exit(3) + case "folded": + _, _ = fmt.Fprint(os.Stdout, capturePrefix) + _, _ = fmt.Fprint(os.Stdout, "framework-stdout-fixture"+captureSuffix) + _, _ = fmt.Fprint(os.Stderr, capturePrefix) + _, _ = fmt.Fprint(os.Stderr, "framework-stderr-fixture"+captureSuffix) + os.Exit(0) + case "pretty-json": + const record = "[INFO] start\n{\n \"Cookie\": \"framework-cookie-fixture\",\n" + + " \"note\": \"public json detail\"\n}\npublic json tail\n" + _, _ = fmt.Fprint(os.Stdout, record) + _, _ = fmt.Fprint(os.Stderr, record) + os.Exit(0) + case "open-json": + const record = "{\n \"Cookie\": \"framework-open-fixture\"\n" + _, _ = fmt.Fprint(os.Stdout, record) + _, _ = fmt.Fprint(os.Stderr, record) + os.Exit(0) case "blocked": _, _ = fmt.Fprint(os.Stdout, strings.Repeat("x", 2<<20)) _, _ = fmt.Fprint(os.Stderr, "ready") @@ -34,7 +57,7 @@ func TestCaptureSuite(t *testing.T) { suite.Run(t, new(captureSuite)) } func (s *captureSuite) TestExit() { s.T().Setenv("DEVSY_CAPTURE_CHILD", "exit") out, stderr, err := s.framework(). - ExecCommandCapture(context.Background(), []string{"-test.run=^TestCaptureChild$"}) + ExecCommandCapture(context.Background(), []string{captureChildArg}) var exit *exec.ExitError s.Require().ErrorAs(err, &exit) s.Equal(3, exit.ExitCode()) @@ -42,6 +65,60 @@ func (s *captureSuite) TestExit() { s.Equal("stderr", stderr) } +func (s *captureSuite) TestCaptureMasksOnlyLiveDiagnosticCopies() { + s.T().Setenv("DEVSY_CAPTURE_CHILD", "folded") + outRead, outWrite, err := os.Pipe() + s.Require().NoError(err) + defer func() { _ = outRead.Close() }() + defer func() { _ = outWrite.Close() }() + errRead, errWrite, err := os.Pipe() + s.Require().NoError(err) + defer func() { _ = errRead.Close() }() + defer func() { _ = errWrite.Close() }() + oldOut, oldErr := os.Stdout, os.Stderr + os.Stdout, os.Stderr = outWrite, errWrite + defer func() { os.Stdout, os.Stderr = oldOut, oldErr }() + + out, stderr, err := s.framework(). + ExecCommandDiagnosticCapture(s.T().Context(), []string{captureChildArg}) + s.Require().NoError(err) + s.Equal(capturePrefix+"framework-stdout-fixture"+captureSuffix, out) + s.Equal(capturePrefix+"framework-stderr-fixture"+captureSuffix, stderr) + s.Require().NoError(outWrite.Close()) + s.Require().NoError(errWrite.Close()) + liveOut, err := io.ReadAll(outRead) + s.Require().NoError(err) + liveErr, err := io.ReadAll(errRead) + s.Require().NoError(err) + for _, value := range [][]byte{liveOut, liveErr} { + s.Contains(string(value), "public capture detail") + s.NotContains(string(value), "framework-stdout-fixture") + s.NotContains(string(value), "framework-stderr-fixture") + s.Contains(string(value), "***") + } +} + +func (s *captureSuite) TestLiveDiagnosticsMaskMultilineJSONCredentials() { + got := s.liveDiagnostics("pretty-json") + s.Contains(got.out, "framework-cookie-fixture") + s.Contains(got.stderr, "framework-cookie-fixture") + for _, live := range []string{got.liveOut, got.liveErr} { + s.NotContains(live, "framework-cookie-fixture") + s.Contains(live, "[INFO] start\n") + s.Contains(live, "public json detail") + s.Contains(live, "public json tail\n") + } +} + +func (s *captureSuite) TestLiveDiagnosticsMaskUnterminatedJSONRecord() { + got := s.liveDiagnostics("open-json") + s.Contains(got.out, "framework-open-fixture") + for _, live := range []string{got.liveOut, got.liveErr} { + s.NotContains(live, "framework-open-fixture") + s.Contains(live, "***") + } +} + func (s *captureSuite) TestBlockedLiveOutput() { s.T().Setenv("DEVSY_CAPTURE_CHILD", "blocked") outRead, outWrite, err := os.Pipe() @@ -66,7 +143,7 @@ func (s *captureSuite) TestBlockedLiveOutput() { } done := make(chan result, 1) go func() { - out, stderr, err := f.ExecCommandCapture(ctx, []string{"-test.run=^TestCaptureChild$"}) + out, stderr, err := f.ExecCommandCapture(ctx, []string{captureChildArg}) done <- result{out, stderr, err} }() ready := make(chan error, 1) @@ -97,6 +174,34 @@ func (s *captureSuite) TestBlockedLiveOutput() { } } +type liveCapture struct{ out, stderr, liveOut, liveErr string } + +func (s *captureSuite) liveDiagnostics(mode string) liveCapture { + s.T().Setenv("DEVSY_CAPTURE_CHILD", mode) + outRead, outWrite, err := os.Pipe() + s.Require().NoError(err) + defer func() { _ = outRead.Close() }() + defer func() { _ = outWrite.Close() }() + errRead, errWrite, err := os.Pipe() + s.Require().NoError(err) + defer func() { _ = errRead.Close() }() + defer func() { _ = errWrite.Close() }() + oldOut, oldErr := os.Stdout, os.Stderr + os.Stdout, os.Stderr = outWrite, errWrite + defer func() { os.Stdout, os.Stderr = oldOut, oldErr }() + + out, stderr, err := s.framework(). + ExecCommandDiagnosticCapture(s.T().Context(), []string{captureChildArg}) + s.Require().NoError(err) + s.Require().NoError(outWrite.Close()) + s.Require().NoError(errWrite.Close()) + liveOut, err := io.ReadAll(outRead) + s.Require().NoError(err) + liveErr, err := io.ReadAll(errRead) + s.Require().NoError(err) + return liveCapture{out, stderr, string(liveOut), string(liveErr)} +} + func (s *captureSuite) framework() *Framework { executable, err := os.Executable() s.Require().NoError(err) diff --git a/e2e/framework/live_diagnostic.go b/e2e/framework/live_diagnostic.go new file mode 100644 index 0000000000..79f0eaff3f --- /dev/null +++ b/e2e/framework/live_diagnostic.go @@ -0,0 +1,32 @@ +package framework + +import ( + "io" + + "github.com/devsy-org/devsy/logging/redact" + "github.com/devsy-org/devsy/pkg/livetee" +) + +type liveDiagnosticCapture struct { + tee *livetee.Tee + masker *recordMasker +} + +func newLiveDiagnosticCapture(tee *livetee.Tee, redactor *redact.Redactor) *liveDiagnosticCapture { + return &liveDiagnosticCapture{ + tee: tee, + masker: newRecordMasker(redactor, func(text string) { + _, _ = io.WriteString(tee, text) + }), + } +} + +func (w *liveDiagnosticCapture) Write(p []byte) (int, error) { + w.masker.write(string(p)) + return len(p), nil +} + +func (w *liveDiagnosticCapture) close() int64 { + w.masker.finish() + return w.tee.Close() +} diff --git a/e2e/framework/live_diagnostic_test.go b/e2e/framework/live_diagnostic_test.go new file mode 100644 index 0000000000..b27d17f29f --- /dev/null +++ b/e2e/framework/live_diagnostic_test.go @@ -0,0 +1,120 @@ +package framework + +import ( + "bytes" + "testing" + + "github.com/devsy-org/devsy/internal/diagnosticredact" + "github.com/devsy-org/devsy/logging/redact" + "github.com/devsy-org/devsy/pkg/livetee" + "github.com/stretchr/testify/assert" +) + +func liveOutput(input string, size int) string { + var live bytes.Buffer + capture := newLiveDiagnosticCapture( + livetee.New(&bytes.Buffer{}, &live), + redact.New(redact.Options{}), + ) + for start := 0; start < len(input); start += size { + end := min(start+size, len(input)) + _, _ = capture.Write([]byte(input[start:end])) + } + capture.close() + return live.String() +} + +func TestLiveDiagnosticCaptureMasksRecordsIndependentlyOfChunking(t *testing.T) { + cases := map[string]struct { + input string + canary string + public []string + }{ + "pretty object": { + input: "[INFO] start\n{\n \"Cookie\": \"chunk-cookie-fixture\",\n" + + " \"note\": \"public detail\"\n}\nprompt: \n" + + "{\"Cookie\":\"chunk-compact-fixture\"}\ntail", + canary: "chunk-cookie-fixture", + public: []string{"[INFO] start\n", "public detail", "prompt: \n", "tail"}, + }, + "compact second record": { + input: "{\"Cookie\":\"chunk-compact-fixture\"}\n", + canary: "chunk-compact-fixture", + }, + "array opening with an element": { + input: "[{\n \"Cookie\": \"chunk-array-fixture\"\n}]\npublic array tail\n", + canary: "chunk-array-fixture", + public: []string{"public array tail\n"}, + }, + "final fragment without newline": { + input: "{\n \"note\": \"public final detail\",\n \"Cookie\": \"chunk-final-fixture\"\n}", + canary: "chunk-final-fixture", + public: []string{"public final detail"}, + }, + "carriage return separated": { + input: "progress\r{\"Cookie\":\"chunk-cr-fixture\"}\rpublic cr tail\r", + canary: "chunk-cr-fixture", + public: []string{"progress\r", "public cr tail\r"}, + }, + "indented record": { + input: " {\n \"Cookie\": \"chunk-indent-fixture\"\n }\npublic indent tail\n", + canary: "chunk-indent-fixture", + public: []string{"public indent tail\n"}, + }, + "bracketed log prefix": { + input: "[INFO] ready\n[WARN] public warning\n", + public: []string{"[INFO] ready\n", "[WARN] public warning\n"}, + }, + } + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + whole := liveOutput(tc.input, len(tc.input)) + if tc.canary != "" { + assert.NotContains(t, whole, tc.canary) + } + for _, public := range tc.public { + assert.Contains(t, whole, public) + } + for _, size := range []int{1, 2, 3, 7, 16} { + assert.Equal(t, whole, liveOutput(tc.input, size), "chunk size %d", size) + } + }) + } +} + +func TestLiveDiagnosticCaptureMasksUnterminatedRecordAtClose(t *testing.T) { + out := liveOutput("{\n \"Cookie\": \"chunk-open-fixture\"\n", 5) + assert.NotContains(t, out, "chunk-open-fixture") + assert.Contains(t, out, "***") +} + +func TestExternalCommandDiagnosticMasksEmbeddedJSONRecords(t *testing.T) { + const input = "public log line\n{\n \"Cookie\": \"external-cookie-fixture\",\n" + + " \"note\": \"public json detail\"\n}\npublic tail" + out := externalCommandDiagnostic(input) + assert.NotContains(t, out, "external-cookie-fixture") + assert.Contains(t, out, "public log line") + assert.Contains(t, out, "public json detail") + assert.Contains(t, out, "public tail") +} + +func TestRecordMaskerMatchesTheDiagnosticStreamForFoldedHeaders(t *testing.T) { + cases := map[string]string{ + "indented compact JSON continuation": "Authorization:\n {\"note\":\"fold-json-fixture\"}\npublic tail\n", + "indented pretty JSON continuation": "Authorization: Bearer\n {\n" + + " \"note\": \"fold-pretty-fixture\"\n }\npublic tail\n", + "unindented JSON after header": "Authorization:\n{\"note\":\"public json\"}\npublic tail\n", + "plain continuation": "Authorization:\n fold-plain-fixture\npublic tail\n", + } + for name, input := range cases { + t.Run(name, func(t *testing.T) { + redactor := redact.New(redact.Options{}) + stream := diagnosticredact.Stream(redactor) + want := stream.RedactChunk(input) + stream.Flush() + got := maskRecordText(redactor, input) + assert.Equal(t, want, got) + assert.NotContains(t, got, "fold-") + assert.Contains(t, got, "public tail") + }) + } +} diff --git a/e2e/framework/record_masker.go b/e2e/framework/record_masker.go new file mode 100644 index 0000000000..5d789a9634 --- /dev/null +++ b/e2e/framework/record_masker.go @@ -0,0 +1,130 @@ +package framework + +import ( + "encoding/json" + "errors" + "io" + "strings" + + "github.com/devsy-org/devsy/internal/diagnosticredact" + "github.com/devsy-org/devsy/logging/redact" +) + +const maxJSONRecordBytes = 64 << 10 + +type recordMasker struct { + redactor *redact.Redactor + stream *redact.Stream + emit func(string) + pending string + midLine bool +} + +func newRecordMasker(redactor *redact.Redactor, emit func(string)) *recordMasker { + return &recordMasker{ + redactor: redactor, + stream: diagnosticredact.Stream(redactor), + emit: emit, + } +} + +func (m *recordMasker) write(text string) { + m.pending += text + m.drain(false) +} + +func (m *recordMasker) finish() { + m.drain(true) + m.emit(m.stream.Flush()) +} + +func (m *recordMasker) drain(final bool) { + for m.pending != "" && m.step(final) { + } +} + +func (m *recordMasker) step(final bool) bool { + if m.midLine { + return m.copyLine() + } + start := strings.TrimLeft(m.pending, " \t") + if start == "" { + if final { + return m.copyLine() + } + return false + } + if start[0] != '{' && start[0] != '[' { + return m.copyLine() + } + return m.maskRecord(start, final) +} + +func (m *recordMasker) copyLine() bool { + end := strings.IndexAny(m.pending, "\r\n") + if end < 0 { + m.plain(m.pending) + m.pending = "" + m.midLine = true + return false + } + end++ + if strings.HasPrefix(m.pending[end-1:], "\r\n") { + end++ + } + m.plain(m.pending[:end]) + m.pending = m.pending[end:] + m.midLine = false + return true +} + +func (m *recordMasker) plain(text string) { + m.emit(m.stream.RedactChunk(text)) +} + +func (m *recordMasker) maskRecord(start string, final bool) bool { + if !final && !strings.ContainsAny(m.pending, "\r\n") { + return m.holdOrOverflow() + } + var record json.RawMessage + decoder := json.NewDecoder(strings.NewReader(start)) + err := decoder.Decode(&record) + switch { + case err == nil: + lead := m.pending[:len(m.pending)-len(start)] + end := int(decoder.InputOffset()) + m.plain(lead + diagnosticredact.Record(m.redactor, start[:end])) + m.pending = start[end:] + return true + case errors.Is(err, io.ErrUnexpectedEOF): + if final { + m.maskPending() + return true + } + return m.holdOrOverflow() + default: + return m.copyLine() + } +} + +func (m *recordMasker) holdOrOverflow() bool { + if len(m.pending) > maxJSONRecordBytes { + m.maskPending() + return true + } + return false +} + +func (m *recordMasker) maskPending() { + m.pending = "" + m.emit(m.stream.Flush()) + m.emit(m.redactor.Mask() + "\n") +} + +func maskRecordText(redactor *redact.Redactor, value string) string { + var out strings.Builder + masker := newRecordMasker(redactor, func(text string) { out.WriteString(text) }) + masker.write(value) + masker.finish() + return out.String() +} diff --git a/e2e/framework/runenv/exec.go b/e2e/framework/runenv/exec.go index 8d7f4c2104..573524a456 100644 --- a/e2e/framework/runenv/exec.go +++ b/e2e/framework/runenv/exec.go @@ -12,6 +12,8 @@ import ( "strings" "time" + "github.com/devsy-org/devsy/internal/diagnosticredact" + "github.com/devsy-org/devsy/logging/redact" "github.com/devsy-org/devsy/pkg/docker" "github.com/devsy-org/devsy/pkg/livetee" ) @@ -150,6 +152,8 @@ func LookupEnv(env []string, name string, foldCase bool) (string, bool) { return val, found } +var diagnosticRedactor = redact.New(redact.Options{Mask: redacted}) + var redactions = []struct { re *regexp.Regexp with string @@ -196,5 +200,5 @@ func Redact(s string, secrets ...string) string { for _, r := range redactions { s = r.re.ReplaceAllString(s, r.with) } - return s + return diagnosticredact.Record(diagnosticRedactor, s) } diff --git a/e2e/framework/runenv/exec_test.go b/e2e/framework/runenv/exec_test.go index c1503cae42..28e9c6c822 100644 --- a/e2e/framework/runenv/exec_test.go +++ b/e2e/framework/runenv/exec_test.go @@ -197,6 +197,17 @@ func (s *ExecSuite) TestRedact() { s.Equal("short x stays", Redact("short x stays", "x"), "tiny secrets ignored") } +func (s *ExecSuite) TestRedactMasksFoldedHeaderAndKeepsExtraFilters() { + input := "Authorization:\n Bearer primary-fixture\n continuation-fixture\n" + + "public detail\n" + `{"auth":"registry-fixture"}` + got := Redact(input) + s.NotContains(got, "primary-fixture") + s.NotContains(got, "continuation-fixture") + s.NotContains(got, "registry-fixture") + s.Contains(got, "public detail") + s.Contains(got, `{"auth":"`+redacted+`"}`) +} + type blockedWriter struct { entered chan struct{} release chan struct{} diff --git a/e2e/framework/setuplogs.go b/e2e/framework/setuplogs.go index dba8d5e0cf..02a138a6de 100644 --- a/e2e/framework/setuplogs.go +++ b/e2e/framework/setuplogs.go @@ -15,9 +15,13 @@ import ( // DevsySetupLogs runs `workspace logs --source setup` and returns stdout and stderr. func (f *Framework) DevsySetupLogs(ctx context.Context, args ...string) (string, string, error) { full := append([]string{cmdWorkspace, "logs", "--source", "setup"}, args...) - stdout, stderr, err := f.ExecCommandCapture(ctx, full) + stdout, stderr, err := f.ExecCommandDiagnosticCapture(ctx, full) if err != nil { - return stdout, stderr, fmt.Errorf("devsy setup logs failed: %w (stderr: %s)", err, stderr) + return stdout, stderr, fmt.Errorf( + "devsy setup logs failed: %w (stderr: %s)", + err, + externalCommandDiagnostic(stderr), + ) } return stdout, stderr, nil diff --git a/e2e/framework/ssh_agent.go b/e2e/framework/ssh_agent.go index 6e03bf7631..834223ff3f 100644 --- a/e2e/framework/ssh_agent.go +++ b/e2e/framework/ssh_agent.go @@ -86,7 +86,7 @@ func generateAgentKey(keyPath string) error { out, err := exec.Command("ssh-keygen", "-t", "ed25519", "-f", keyPath, "-N", "", "-q"). CombinedOutput() if err != nil { - return fmt.Errorf("ssh-keygen: %w: %s", err, out) + return fmt.Errorf("ssh-keygen: %w: %s", err, externalCommandDiagnostic(string(out))) } return nil } @@ -128,7 +128,7 @@ func addKeyToAgent(sockPath, keyPath string) error { cmd.Env = append(os.Environ(), "SSH_AUTH_SOCK="+sockPath) out, err := cmd.CombinedOutput() if err != nil { - return fmt.Errorf("ssh-add: %w: %s", err, out) + return fmt.Errorf("ssh-add: %w: %s", err, externalCommandDiagnostic(string(out))) } return nil } diff --git a/e2e/framework/workspace_edit.go b/e2e/framework/workspace_edit.go index 82f790aa76..e7feb6212b 100644 --- a/e2e/framework/workspace_edit.go +++ b/e2e/framework/workspace_edit.go @@ -17,9 +17,13 @@ func (f *Framework) DevsyWorkspaceEditJSON( ) (string, error) { baseArgs := []string{cmdWorkspace, "edit", id, flagResultFormat, formatJSON} baseArgs = append(baseArgs, args...) - stdout, stderr, err := f.ExecCommandCapture(ctx, baseArgs) + stdout, stderr, err := f.ExecCommandDiagnosticCapture(ctx, baseArgs) if err != nil { - return "", fmt.Errorf("devsy workspace edit failed: %w (stderr: %s)", err, stderr) + return "", fmt.Errorf( + "devsy workspace edit failed: %w (stderr: %s)", + err, + externalCommandDiagnostic(stderr), + ) } return stdout, nil @@ -39,7 +43,11 @@ func (f *Framework) DevsyWorkspaceEditResult( result := &workspace.PresentationResult{} if err := json.Unmarshal([]byte(stdout), result); err != nil { - return nil, fmt.Errorf("decode workspace edit result %q: %w", stdout, err) + return nil, fmt.Errorf( + "decode workspace edit result %q: %w", + externalCommandDiagnostic(stdout), + err, + ) } return result, nil diff --git a/e2e/tests/ssh/agent_forward.go b/e2e/tests/ssh/agent_forward.go index 0a323f89e1..dcb2bcc442 100644 --- a/e2e/tests/ssh/agent_forward.go +++ b/e2e/tests/ssh/agent_forward.go @@ -11,6 +11,8 @@ import ( "time" "github.com/devsy-org/devsy/e2e/framework" + "github.com/devsy-org/devsy/internal/diagnosticredact" + "github.com/devsy-org/devsy/logging/redact" "github.com/onsi/ginkgo/v2" "github.com/onsi/gomega" ) @@ -75,6 +77,7 @@ var _ = ginkgo.Describe( ginkgo.DeferCleanup(closeCM) env := map[string]string{envSSHAuthSock: authSock} + redactor := redact.New(redact.Options{Environment: os.Environ()}) // Session A: capture $SSH_AUTH_SOCK inside the container and // verify the forwarded key is reachable. @@ -82,7 +85,10 @@ var _ = ginkgo.Describe( controlPath, host, env, "sh", "-c", "printf %s \"$SSH_AUTH_SOCK\"", ) - ginkgo.GinkgoWriter.Printf("session A stderr: %s\n", errA) + ginkgo.GinkgoWriter.Printf( + "session A stderr: %s\n", + diagnosticredact.Record(redactor, errA), + ) framework.ExpectNoError(err) s1 := strings.TrimSpace(outA) gomega.Expect(s1).NotTo(gomega.BeEmpty(), "session A must see SSH_AUTH_SOCK") @@ -112,9 +118,17 @@ var _ = ginkgo.Describe( controlPath, host, env, "sh", "-c", "SSH_AUTH_SOCK="+s1+" ssh-add -L", ) - ginkgo.GinkgoWriter.Printf("session B explicit-S1 stderr: %s\n", errB) + ginkgo.GinkgoWriter.Printf( + "session B explicit-S1 stderr: %s\n", + diagnosticredact.Record(redactor, errB), + ) framework.ExpectNoError(err, "session B: socket from session A must still work") - gomega.Expect(outAddB).To(gomega.ContainSubstring(strings.Fields(pubKey)[1])) + expectSSHSubstring( + outAddB, + strings.Fields(pubKey)[1], + "session B lists the forwarded key", + true, + ) // Session B's reported SSH_AUTH_SOCK should equal S1 too - the // regression is that this used to differ session-to-session. @@ -135,7 +149,12 @@ var _ = ginkgo.Describe( "sh", "-c", "SSH_AUTH_SOCK="+s1+" ssh-add -L", ) framework.ExpectNoError(err, "session C: S1 must still work") - gomega.Expect(outAddC).To(gomega.ContainSubstring(strings.Fields(pubKey)[1])) + expectSSHSubstring( + outAddC, + strings.Fields(pubKey)[1], + "session C lists the forwarded key", + true, + ) }, ) @@ -188,7 +207,12 @@ var _ = ginkgo.Describe( err, "connection 2 must remain functional after connection 1 closes", ) - gomega.Expect(outAdd).To(gomega.ContainSubstring(strings.Fields(pubKey)[1])) + expectSSHSubstring( + outAdd, + strings.Fields(pubKey)[1], + "session lists the forwarded key", + true, + ) }, ) diff --git a/e2e/tests/ssh/credentials_server_race.go b/e2e/tests/ssh/credentials_server_race.go index 8cdd1a54e9..c7809c7b9c 100644 --- a/e2e/tests/ssh/credentials_server_race.go +++ b/e2e/tests/ssh/credentials_server_race.go @@ -8,7 +8,6 @@ import ( "github.com/devsy-org/devsy/e2e/framework" "github.com/onsi/ginkgo/v2" - "github.com/onsi/gomega" ) const ( @@ -64,7 +63,7 @@ var _ = ginkgo.Describe( time.Now().Add(30*time.Second), ) defer cancelSSH() - _, stderr, sshErr := f.ExecCommandCapture(sshCtx, []string{ + _, stderr, sshErr := f.ExecCommandDiagnosticCapture(sshCtx, []string{ cmdWorkspace, cmdSSH, tempDir, flagCommand, "sleep 2", }) stderrs[i] = stderr @@ -74,9 +73,24 @@ var _ = ginkgo.Describe( wg.Wait() for i := range sessions { - framework.ExpectNoError(runErrs[i], "ssh session %d stderr: %s", i, stderrs[i]) - gomega.Expect(stderrs[i]).NotTo(gomega.ContainSubstring("not available")) - gomega.Expect(stderrs[i]).NotTo(gomega.ContainSubstring("credentials server")) + framework.ExpectNoError( + runErrs[i], + "ssh session %d stderr: %s", + i, + sshDiagnostic(stderrs[i]), + ) + expectSSHSubstring( + stderrs[i], + "not available", + "SSH session should be available", + false, + ) + expectSSHSubstring( + stderrs[i], + "credentials server", + "SSH session should have no server warning", + false, + ) } }, ) diff --git a/e2e/tests/ssh/detached_task.go b/e2e/tests/ssh/detached_task.go index 31ca9086bd..54992a28e4 100644 --- a/e2e/tests/ssh/detached_task.go +++ b/e2e/tests/ssh/detached_task.go @@ -62,7 +62,7 @@ func startDetachedTunnelUp( args = append(args, workspace) before := detachedTaskIDs() - out, err := f.ExecCommandOutput(ctx, args) + out, _, err := f.ExecCommandDiagnosticCapture(ctx, args) if err != nil { reportDetachedLaunchFailure(before, workspace) return "", err @@ -73,10 +73,17 @@ func startDetachedTunnelUp( } line, _, _ := strings.Cut(strings.TrimSpace(out), "\n") if err := json.Unmarshal([]byte(line), &env); err != nil { - return "", fmt.Errorf("parse task envelope %q: %w", out, err) + return "", fmt.Errorf( + "parse task envelope %q: %w", + sshDiagnostic(out), + err, + ) } if env.Kind != "task" || env.ID == "" { - return "", fmt.Errorf("unexpected task envelope %q", line) + return "", fmt.Errorf( + "unexpected task envelope %q", + sshDiagnostic(line), + ) } return env.ID, nil } diff --git a/e2e/tests/ssh/detached_validation.go b/e2e/tests/ssh/detached_validation.go index c5f93603b4..dd2dc27890 100644 --- a/e2e/tests/ssh/detached_validation.go +++ b/e2e/tests/ssh/detached_validation.go @@ -12,9 +12,10 @@ import ( "time" "github.com/devsy-org/devsy/e2e/framework" + "github.com/devsy-org/devsy/internal/diagnosticredact" + "github.com/devsy-org/devsy/logging/redact" "github.com/devsy-org/devsy/pkg/command" "github.com/devsy-org/devsy/pkg/config" - "github.com/devsy-org/devsy/pkg/secrets" "github.com/devsy-org/devsy/pkg/task" "github.com/devsy-org/devsy/pkg/workspace" "github.com/onsi/ginkgo/v2" @@ -43,8 +44,8 @@ var _ = ginkgo.Describe("detached workspace input validation", ginkgo.Label("tas id, ) gomega.Expect(err).To(gomega.HaveOccurred()) - gomega.Expect(out).To(gomega.ContainSubstring("workspace name")) - gomega.Expect(out).NotTo(gomega.ContainSubstring(`"kind":"task"`)) + expectSSHSubstring(out, "workspace name", "validation names the workspace", true) + expectSSHSubstring(out, `"kind":"task"`, "validation emits no task envelope", false) } out, err := cli.command(ctx, cmdWorkspace, "task", "list") framework.ExpectNoError(err) @@ -84,8 +85,10 @@ var _ = ginkgo.Describe("detached workspace input validation", ginkgo.Label("tas if err != nil { reportDetachedLaunchFailure(before, source) } + redactor := redact.New(redact.Options{Environment: detachedValidationEnvironment(home)}) gomega.Expect(err).NotTo(gomega.HaveOccurred(), "CLI output: %s", - secrets.NewEnvironmentRedactor(detachedValidationEnvironment(home)).Redact(out)) + diagnosticredact.Record(redactor, out), + ) var envelope struct { Kind string `json:"kind"` ID string `json:"id"` @@ -334,8 +337,8 @@ func (cli detachedValidationCLI) expectFailureEnvelope(ctx context.Context, id, } framework.ExpectNoError(json.Unmarshal([]byte(strings.TrimSpace(out)), &envelope)) gomega.Expect(envelope.Kind).To(gomega.Equal("error")) - gomega.Expect(envelope.Message).To(gomega.ContainSubstring(diagnostic)) - gomega.Expect(out).NotTo(gomega.ContainSubstring("missing-provider-1471")) + expectSSHSubstring(envelope.Message, diagnostic, "error envelope carries the diagnostic", true) + expectSSHSubstring(out, "missing-provider-1471", "output omits the provider name", false) } func detachedValidationEnvironment(home string) []string { diff --git a/e2e/tests/ssh/gh_public.go b/e2e/tests/ssh/gh_public.go index 2027d6262e..a7fe064fef 100644 --- a/e2e/tests/ssh/gh_public.go +++ b/e2e/tests/ssh/gh_public.go @@ -53,7 +53,7 @@ func setPublicForwarding( if enabled { value = "true" } - _, _, err := f.ExecCommandCapture(ctx, []string{ + _, _, err := f.ExecCommandDiagnosticCapture(ctx, []string{ "context", "set", "default", "-o", "SSH_INJECT_GITHUB_CREDENTIALS=" + value, "-o", "SSH_AGENT_FORWARDING=false", "-o", "SSH_INJECT_GIT_CREDENTIALS=false", "-o", "SSH_INJECT_DOCKER_CREDENTIALS=false", @@ -95,7 +95,7 @@ exit 1`, p.registerDiagnostics(t, workspace, &deleted) // No services start during this check, leaving the first enabled SSH call // responsible for both preparing the relay and running the first gh command. - _, _, err = p.f.ExecCommandCapture(ctx, []string{ + _, _, err = p.f.ExecCommandDiagnosticCapture(ctx, []string{ cmdWorkspace, cmdSSH, workspace, flagNoServices, flagCommand, "test ! -e /usr/local/bin/gh.devsy-original", }) @@ -139,7 +139,7 @@ func (p *publicGHFixture) testMissingCLI( workspace string, ) { t.Helper() - _, _, err := p.f.ExecCommandCapture(ctx, []string{ + _, _, err := p.f.ExecCommandDiagnosticCapture(ctx, []string{ cmdWorkspace, cmdSSH, workspace, flagNoServices, flagCommand, "sudo -n rm -f /usr/local/bin/gh /usr/local/bin/gh.devsy-original /usr/bin/gh", diff --git a/e2e/tests/ssh/gh_public_diagnostics.go b/e2e/tests/ssh/gh_public_diagnostics.go index 682243f9ea..47f9093254 100644 --- a/e2e/tests/ssh/gh_public_diagnostics.go +++ b/e2e/tests/ssh/gh_public_diagnostics.go @@ -43,7 +43,7 @@ func (p *publicGHFixture) registerDiagnostics( } ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() - out, _, err := p.f.ExecCommandCapture(ctx, []string{ + out, _, err := p.f.ExecCommandDiagnosticCapture(ctx, []string{ cmdWorkspace, cmdSSH, workspace, @@ -51,6 +51,6 @@ func (p *publicGHFixture) registerDiagnostics( flagCommand, "head -c 4096 /tmp/fixture-api.log; pgrep -a ghfixture; stat -c '%a %u %g %n' /fixture/api-ready /fixture/cert.pem", }) - t.Logf("public API fixture diagnostics: %s (error: %v)", out, err) + t.Logf("public API fixture diagnostics: %s (error: %v)", sshDiagnostic(out), err) }) } diff --git a/e2e/tests/ssh/proxy_command.go b/e2e/tests/ssh/proxy_command.go index edcf43ebae..cc5691a06d 100644 --- a/e2e/tests/ssh/proxy_command.go +++ b/e2e/tests/ssh/proxy_command.go @@ -12,7 +12,8 @@ import ( "time" "github.com/devsy-org/devsy/e2e/framework" - "github.com/devsy-org/devsy/pkg/secrets" + "github.com/devsy-org/devsy/internal/diagnosticredact" + "github.com/devsy-org/devsy/logging/redact" "github.com/onsi/ginkgo/v2" "github.com/onsi/gomega" ) @@ -130,8 +131,8 @@ var _ = ginkgo.Describe( ginkgo.ReportEntryVisibilityFailureOrVerbose, ) framework.ExpectNoError(err, "OpenSSH should launch ProxyCommand; %s", diagnostic) - gomega.Expect(strings.TrimSpace(stdout.String())).To( - gomega.Equal("proxy-command-ok"), + gomega.Expect(strings.TrimSpace(stdout.String()) == "proxy-command-ok").To( + gomega.BeTrue(), "OpenSSH should return the expected stdout; %s", diagnostic, ) }, ) @@ -140,7 +141,7 @@ var _ = ginkgo.Describe( func proxyCommandOutputDiagnostic(traceRoot *os.Root, stdout, stderr string) string { trace, err := traceRoot.ReadFile(proxyCommandTraceFile) - redactor := secrets.NewEnvironmentRedactor(os.Environ()) + redactor := redact.New(redact.Options{Environment: os.Environ()}) return fmt.Sprintf( "stdout=%s stderr=%s traceReadError=%s trace=%s", proxyCommandExcerpt(redactor, stdout), @@ -158,8 +159,8 @@ const ( proxyCommandExcerptLimit = 8192 ) -func proxyCommandExcerpt(redactor *secrets.Redactor, text string) string { - quoted := strconv.QuoteToASCII(redactor.Redact(text)) +func proxyCommandExcerpt(redactor *redact.Redactor, text string) string { + quoted := strconv.QuoteToASCII(diagnosticredact.Record(redactor, text)) if len(quoted) <= proxyCommandExcerptLimit { return quoted } diff --git a/e2e/tests/ssh/proxy_command_test.go b/e2e/tests/ssh/proxy_command_test.go index 3fd2702729..037d4b3184 100644 --- a/e2e/tests/ssh/proxy_command_test.go +++ b/e2e/tests/ssh/proxy_command_test.go @@ -1,16 +1,40 @@ package ssh import ( + "os" "strings" "testing" - "github.com/devsy-org/devsy/pkg/secrets" + "github.com/devsy-org/devsy/logging/redact" + "github.com/onsi/gomega" "github.com/stretchr/testify/require" ) +func TestSSHOutputAssertionDoesNotRenderRawCapture(t *testing.T) { + const fixture = "synthetic-ssh-assertion-value" + const detail = "public signing detail" + output := "Authorization:\n Bearer " + fixture + "\n" + detail + previous := gomega.Default + gomega.Default = gomega.NewWithT(t) + t.Cleanup(func() { gomega.Default = previous }) + for _, present := range []bool{true, false} { + fragment := "missing expected detail" + if !present { + fragment = detail + } + failures := gomega.InterceptGomegaFailures(func() { + expectSSHSubstring(output, fragment, "SSH command assertion", present) + }) + require.Len(t, failures, 1) + require.Contains(t, failures[0], "SSH command assertion") + require.Contains(t, failures[0], detail) + require.NotContains(t, failures[0], fixture) + } +} + func TestProxyCommandExcerptProtectsFailureReport(t *testing.T) { secret := strings.Repeat("test-value-", 8) - redactor := secrets.NewEnvironmentRedactor([]string{"DEVSY_TOKEN=" + secret}) + redactor := redact.New(redact.Options{Environment: []string{"DEVSY_TOKEN=" + secret}}) input := strings.Repeat( "padding", 3000, @@ -25,3 +49,18 @@ func TestProxyCommandExcerptProtectsFailureReport(t *testing.T) { require.Contains(t, got, "example.com/end") require.Contains(t, got, "***") } + +func TestProxyCommandOutputDiagnosticMasksFoldedCredentials(t *testing.T) { + const input = "Authorization:\n Bearer proxy-folded-fixture\npublic proxy detail" + root, err := os.OpenRoot(t.TempDir()) + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, root.Close()) }) + require.NoError(t, root.WriteFile(proxyCommandTraceFile, []byte(input), 0o600)) + + got := proxyCommandOutputDiagnostic(root, "public stdout", input) + require.Contains(t, got, "public stdout") + require.Equal(t, 2, strings.Count(got, "public proxy detail")) + require.NotContains(t, got, "proxy-folded-fixture") + require.Contains(t, got, "***") + require.NotContains(t, got, "\n") +} diff --git a/e2e/tests/ssh/ssh.go b/e2e/tests/ssh/ssh.go index 41f19cfee8..a1e097910b 100644 --- a/e2e/tests/ssh/ssh.go +++ b/e2e/tests/ssh/ssh.go @@ -15,6 +15,8 @@ import ( "time" "github.com/devsy-org/devsy/e2e/framework" + "github.com/devsy-org/devsy/internal/diagnosticredact" + "github.com/devsy-org/devsy/logging/redact" "github.com/devsy-org/devsy/pkg/flags/names" "github.com/onsi/ginkgo/v2" "github.com/onsi/gomega" @@ -237,16 +239,15 @@ var _ = ginkgo.Describe("devsy ssh test suite", ginkgo.Label("ssh"), ginkgo.Orde tempDir, } - stdout, stderr, err := f.ExecCommandCapture(ctx, + stdout, stderr, err := f.ExecCommandDiagnosticCapture(ctx, append(sshBase, flagCommand, commitCmd), ) - ginkgo.GinkgoWriter.Printf("commit stdout: %s\n", stdout) - ginkgo.GinkgoWriter.Printf("commit stderr: %s\n", stderr) + reportSSHCommandOutput("commit", stdout, stderr) framework.ExpectNoError(err) - gomega.Expect(stdout).To( - gomega.ContainSubstring("signed test commit"), + expectSSHSubstring(stdout, "signed test commit", "git commit should succeed with the signed test commit message", + true, ) // Verify the commit is signed with a valid SSH signature. @@ -263,37 +264,35 @@ var _ = ginkgo.Describe("devsy ssh test suite", ginkgo.Label("ssh"), ginkgo.Orde gitVerifyCommitCmd, }, " && ") - stdout, stderr, err = f.ExecCommandCapture(ctx, + stdout, stderr, err = f.ExecCommandDiagnosticCapture(ctx, append(sshBase, flagCommand, verifyCmd), ) - ginkgo.GinkgoWriter.Printf("verify stdout: %s\n", stdout) - ginkgo.GinkgoWriter.Printf("verify stderr: %s\n", stderr) + reportSSHCommandOutput("verify", stdout, stderr) framework.ExpectNoError(err) // git verify-commit writes signature details to stderr combined := stdout + stderr - gomega.Expect(combined).To( - gomega.ContainSubstring("Good"), + expectSSHSubstring(combined, "Good", "git verify-commit should report a good SSH signature", + true, ) // And confirm the signature log shows the correct principal logCmd := "cd /tmp/test-sign-repo && git log --show-signature -1 2>&1" - stdout, stderr, err = f.ExecCommandCapture(ctx, + stdout, stderr, err = f.ExecCommandDiagnosticCapture(ctx, append(sshBase, flagCommand, logCmd), ) - ginkgo.GinkgoWriter.Printf("log stdout: %s\n", stdout) - ginkgo.GinkgoWriter.Printf("log stderr: %s\n", stderr) + reportSSHCommandOutput("log", stdout, stderr) framework.ExpectNoError(err) combined = stdout + stderr - gomega.Expect(combined).To( - gomega.ContainSubstring("Good"), + expectSSHSubstring(combined, "Good", "git log --show-signature should report a good signature", + true, ) - gomega.Expect(combined).To( - gomega.ContainSubstring("test@example.com"), + expectSSHSubstring(combined, "test@example.com", "signature should be associated with the test email principal", + true, ) }, ) @@ -374,7 +373,7 @@ var _ = ginkgo.Describe("devsy ssh test suite", ginkgo.Label("ssh"), ginkgo.Orde "git commit -m 'signed test commit' 2>&1", }, " && ") - stdout, stderr, err := f.ExecCommandCapture(ctx, []string{ + stdout, stderr, err := f.ExecCommandDiagnosticCapture(ctx, []string{ cmdWorkspace, cmdSSH, "--agent-forwarding", @@ -382,8 +381,7 @@ var _ = ginkgo.Describe("devsy ssh test suite", ginkgo.Label("ssh"), ginkgo.Orde tempDir, flagCommand, commitCmd, }) - ginkgo.GinkgoWriter.Printf("error commit stdout: %s\n", stdout) - ginkgo.GinkgoWriter.Printf("error commit stderr: %s\n", stderr) + reportSSHCommandOutput("error commit", stdout, stderr) // The commit should fail combined := stdout + stderr @@ -392,9 +390,11 @@ var _ = ginkgo.Describe("devsy ssh test suite", ginkgo.Label("ssh"), ginkgo.Orde } // The error must NOT contain JSON decode artifacts - gomega.Expect(combined).NotTo( - gomega.ContainSubstring("invalid character"), + expectSSHSubstring( + combined, + "invalid character", "error should not contain JSON parse errors - error messages must be human-readable", + false, ) }, ) @@ -426,7 +426,7 @@ var _ = ginkgo.Describe("devsy ssh test suite", ginkgo.Label("ssh"), ginkgo.Orde framework.ExpectNoError(err) signCmd := buildTildeSigningScript(privKeyBase64, pubKey) - stdout, stderr, err := f.ExecCommandCapture(ctx, []string{ + stdout, stderr, err := f.ExecCommandDiagnosticCapture(ctx, []string{ cmdWorkspace, cmdSSH, names.Flag(names.StartServices), @@ -434,23 +434,22 @@ var _ = ginkgo.Describe("devsy ssh test suite", ginkgo.Label("ssh"), ginkgo.Orde names.Flag(names.Command), signCmd, }) - ginkgo.GinkgoWriter.Printf("tilde signing stdout: %s\n", stdout) - ginkgo.GinkgoWriter.Printf("tilde signing stderr: %s\n", stderr) + reportSSHCommandOutput("tilde signing", stdout, stderr) framework.ExpectNoError(err) const goodSig = `Good "git" signature` combined := stdout + stderr - gomega.Expect(combined).To( - gomega.ContainSubstring("signed via default tilde relay"), + expectSSHSubstring(combined, "signed via default tilde relay", "commit signed via host-forwarded tilde key should succeed", + true, ) - gomega.Expect(combined).To( - gomega.ContainSubstring("signed via workspace tilde override"), + expectSSHSubstring(combined, "signed via workspace tilde override", "commit signed via workspace repo-local tilde private key should succeed", + true, ) - gomega.Expect(combined).To( - gomega.ContainSubstring(goodSig), + expectSSHSubstring(combined, goodSig, "git verify-commit should report a valid SSH signature", + true, ) gomega.Expect(strings.Count(combined, goodSig)).To( gomega.Equal(2), @@ -664,3 +663,20 @@ func buildTildeSigningScript(privKeyBase64, pubKey string) string { "test \"$(git rev-list --count HEAD)\" = 2", }, " && ") } + +func reportSSHCommandOutput(label, stdout, stderr string) { + redactor := redact.New(redact.Options{Environment: os.Environ()}) + ginkgo.GinkgoWriter.Printf("%s stdout: %s\n", label, diagnosticredact.Record(redactor, stdout)) + ginkgo.GinkgoWriter.Printf("%s stderr: %s\n", label, diagnosticredact.Record(redactor, stderr)) +} + +func sshDiagnostic(value string) string { + redactor := redact.New(redact.Options{Environment: os.Environ()}) + return diagnosticredact.Record(redactor, value) +} + +func expectSSHSubstring(output, fragment, description string, present bool) { + gomega.Expect(strings.Contains(output, fragment)).To( + gomega.Equal(present), "%s\nOutput: %s", description, sshDiagnostic(output), + ) +} diff --git a/e2e/tests/up/assertion_output_test.go b/e2e/tests/up/assertion_output_test.go new file mode 100644 index 0000000000..08668709d7 --- /dev/null +++ b/e2e/tests/up/assertion_output_test.go @@ -0,0 +1,87 @@ +package up + +import ( + "go/ast" + "go/parser" + "go/token" + "os" + "testing" + + "github.com/onsi/gomega" + "github.com/stretchr/testify/require" +) + +var maskedAssertionFiles = []string{ + "fixture_command.go", + "helper.go", + "provider_kubernetes_dockerless_path.go", +} + +var rawOutputMatchers = map[string]bool{ + "ContainSubstring": true, + "MatchRegexp": true, + "Equal": true, + "HavePrefix": true, + "HaveSuffix": true, +} + +var outputNames = map[string]bool{ + "output": true, "out": true, "stdout": true, "stderr": true, +} + +func TestGomegaPrintsTheActualValueOfAFailedSubstringMatch(t *testing.T) { + previousGomega := gomega.Default + gomega.Default = gomega.NewWithT(t) + t.Cleanup(func() { gomega.Default = previousGomega }) + failures := gomega.InterceptGomegaFailures(func() { + gomega.Expect("raw-output-marker").To(gomega.ContainSubstring("absent")) + }) + require.Len(t, failures, 1) + require.Contains(t, failures[0], "raw-output-marker") +} + +func TestUpSpecsDoNotAssertDirectlyOnRawCommandOutput(t *testing.T) { + var offenders []string + for _, name := range maskedAssertionFiles { + source, err := os.ReadFile(name) //nolint:gosec // fixed in-package source names + require.NoError(t, err) + parsed, err := parser.ParseFile(token.NewFileSet(), name, source, 0) + require.NoError(t, err) + ast.Inspect(parsed, func(node ast.Node) bool { + if call, ok := node.(*ast.CallExpr); ok && assertsRawOutput(call) { + offenders = append(offenders, name) + } + return true + }) + } + require.Empty(t, offenders, "assert on a boolean and report fixtureDiagnostic output") +} + +func assertsRawOutput(call *ast.CallExpr) bool { + selector, ok := call.Fun.(*ast.SelectorExpr) + if !ok || (selector.Sel.Name != "To" && selector.Sel.Name != "NotTo") || len(call.Args) == 0 { + return false + } + expect, ok := selector.X.(*ast.CallExpr) + return ok && len(expect.Args) == 1 && isRawOutputMatcher(call.Args[0]) && + mentionsOutput(expect.Args[0]) +} + +func mentionsOutput(expr ast.Expr) bool { + switch value := expr.(type) { + case *ast.Ident: + return outputNames[value.Name] + case *ast.CallExpr: + return len(value.Args) == 1 && mentionsOutput(value.Args[0]) + } + return false +} + +func isRawOutputMatcher(expr ast.Expr) bool { + call, ok := expr.(*ast.CallExpr) + if !ok { + return false + } + selector, ok := call.Fun.(*ast.SelectorExpr) + return ok && rawOutputMatchers[selector.Sel.Name] +} diff --git a/e2e/tests/up/fixture_command.go b/e2e/tests/up/fixture_command.go index eb4ee60442..6f0414fed8 100644 --- a/e2e/tests/up/fixture_command.go +++ b/e2e/tests/up/fixture_command.go @@ -3,10 +3,14 @@ package up import ( "context" "encoding/json" + "fmt" "os" "os/exec" + "strings" "time" + "github.com/devsy-org/devsy/internal/diagnosticredact" + "github.com/devsy-org/devsy/logging/redact" "github.com/devsy-org/devsy/pkg/docker" "github.com/onsi/ginkgo/v2" "github.com/onsi/gomega" @@ -40,11 +44,35 @@ func fixtureCommand( argv ...string, ) string { out, err := runFixtureCommand(ctx, timeout, env, argv...) - gomega.Expect(err). - NotTo(gomega.HaveOccurred(), "fixture command %v: %s", argv, out) + if err != nil { + gomega.Expect(err).NotTo(gomega.HaveOccurred(), "fixture command %s: %s", + fixtureDiagnostic(env, fmt.Sprint(argv)), fixtureDiagnostic(env, out)) + } return out } +func remoteAgentRemovalError(output string, err error) error { + if err != nil && !strings.Contains(output, "No such container") { + return fmt.Errorf( + "remove remote agent container: %w: %s", + err, + fixtureDiagnostic(nil, output), + ) + } + return nil +} + +func fixtureDiagnostic(env []string, value string) string { + redactor := redact.New(redact.Options{Environment: append(os.Environ(), env...)}) + return diagnosticredact.Record(redactor, value) +} + +// expectFixtureOutput fails the spec when ok is false and prints only the redacted output. +func expectFixtureOutput(ok bool, output, description string) { + gomega.Expect(ok). + To(gomega.BeTrue(), "%s\nOutput: %s", description, fixtureDiagnostic(nil, output)) +} + // kubectlFixture runs kubectl with args, bounded by timeout, and fails the current spec on error. func kubectlFixture(ctx context.Context, timeout time.Duration, args ...string) string { return fixtureCommand(ctx, timeout, nil, append([]string{"kubectl"}, args...)...) @@ -76,10 +104,10 @@ func dumpFixtureDiagnostics(ctx context.Context, namespace, selector string) { append([]string{"kubectl"}, args...)..., ) ginkgo.GinkgoWriter.Printf( - "Kubernetes failure diagnostics: %s\n%s\nerror: %v\n", + "Kubernetes failure diagnostics: %s\n%s\nerror: %s\n", args, - output, - err, + fixtureDiagnostic(nil, output), + fixtureDiagnostic(nil, fmt.Sprint(err)), ) } } @@ -104,7 +132,10 @@ func fixtureWorkspacePod(ctx context.Context, namespace, selector string) corev1 "-o", "json", ) - g.Expect(err).NotTo(gomega.HaveOccurred(), "workspace pod discovery: %s", out) + if err != nil { + g.Expect(err).NotTo(gomega.HaveOccurred(), "workspace pod discovery: %s", + fixtureDiagnostic(nil, out)) + } var pods corev1.PodList g.Expect(json.Unmarshal([]byte(out), &pods)).To(gomega.Succeed()) g.Expect(pods.Items).To(gomega.HaveLen(1), "workspace-specific selector %s", selector) diff --git a/e2e/tests/up/fixture_command_test.go b/e2e/tests/up/fixture_command_test.go new file mode 100644 index 0000000000..e82d85c2a6 --- /dev/null +++ b/e2e/tests/up/fixture_command_test.go @@ -0,0 +1,125 @@ +package up + +import ( + "os" + "os/exec" + "path/filepath" + "runtime" + "strings" + "testing" + "time" + + "github.com/onsi/gomega" + "github.com/stretchr/testify/require" +) + +func TestFixtureCommandMasksFailureWithoutChangingCapturedOutput(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("requires a POSIX shell") + } + const fixture = "Authorization:\n Bearer fixture-command-placeholder\npublic command detail\n" + path := filepath.Join(t.TempDir(), "fixture-output") + require.NoError(t, os.WriteFile(path, []byte(fixture), 0o600)) + previousGomega := gomega.Default + gomega.Default = gomega.NewWithT(t) + t.Cleanup(func() { gomega.Default = previousGomega }) + var captured string + failures := gomega.InterceptGomegaFailures(func() { + captured = fixtureCommand(t.Context(), time.Second, nil, "/bin/sh", "-c", + "cat \"$1\"; exit 7", "fixture", path) + }) + require.Equal(t, fixture, captured) + require.Len(t, failures, 1) + require.Contains(t, failures[0], "public command detail") + require.NotContains(t, failures[0], "fixture-command-placeholder") + require.Contains(t, failures[0], "***") +} + +func TestExpectFixtureOutputFailureReportsRedactedOutput(t *testing.T) { + const output = "Authorization:\n Bearer assertion-output-fixture\npublic assertion detail\n" + previousGomega := gomega.Default + gomega.Default = gomega.NewWithT(t) + t.Cleanup(func() { gomega.Default = previousGomega }) + failures := gomega.InterceptGomegaFailures(func() { + expectFixtureOutput(strings.Contains(output, "absent marker"), output, "marker present") + }) + require.Len(t, failures, 1) + require.Contains(t, failures[0], "marker present") + require.Contains(t, failures[0], "public assertion detail") + require.NotContains(t, failures[0], "assertion-output-fixture") + require.Empty(t, gomega.InterceptGomegaFailures(func() { + expectFixtureOutput(strings.Contains(output, "public assertion"), output, "marker present") + })) +} + +func TestMicrosandboxEgressIPMasksFoldedCommandError(t *testing.T) { + writeFixtureTool(t, "ip", + "printf 'Authorization:\\n Bearer egress-tool-fixture\\npublic egress detail'\nexit 7\n") + err := microsandboxEgressIP(t.Context(), "link", "show") + require.ErrorContains(t, err, "ip [link show]") + require.ErrorContains(t, err, "public egress detail") + assertFixtureToolFailure(t, err, "egress-tool-fixture") +} + +func TestRemoteAgentRemovalMasksErrorAndRetainsMissingContainerHandling(t *testing.T) { + t.Run("reported failure", func(t *testing.T) { + writeFixtureTool( + t, + "docker", + "printf 'Authorization:\\n Bearer cleanup-tool-fixture\\npublic cleanup detail'\nexit 7\n", + ) + out, execErr := runFixtureCommand( + t.Context(), + time.Second, + nil, + "docker", + "rm", + "-f", + "remote-agent-fixture", + ) + err := remoteAgentRemovalError(out, execErr) + require.ErrorContains(t, err, "remove remote agent container") + require.ErrorContains(t, err, "public cleanup detail") + assertFixtureToolFailure(t, err, "cleanup-tool-fixture") + }) + t.Run("missing container", func(t *testing.T) { + writeFixtureTool( + t, + "docker", + "printf 'Authorization:\\n Bearer No such container'\nexit 7\n", + ) + out, execErr := runFixtureCommand( + t.Context(), + time.Second, + nil, + "docker", + "rm", + "-f", + "remote-agent-fixture", + ) + require.Error(t, execErr) + require.NoError(t, remoteAgentRemovalError(out, execErr)) + }) +} + +func writeFixtureTool(t *testing.T, name, script string) { + t.Helper() + if runtime.GOOS == "windows" { + t.Skip("requires a POSIX shell") + } + bin := t.TempDir() + t.Setenv("PATH", bin) + root, err := os.OpenRoot(bin) + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, root.Close()) }) + require.NoError(t, root.WriteFile(name, []byte("#!/bin/sh\n"+script), 0o700)) +} + +func assertFixtureToolFailure(t *testing.T, err error, sensitive string) { + t.Helper() + var exit *exec.ExitError + require.ErrorAs(t, err, &exit) + require.Equal(t, 7, exit.ExitCode()) + require.NotContains(t, err.Error(), sensitive) + require.Contains(t, err.Error(), "***") +} diff --git a/e2e/tests/up/helper.go b/e2e/tests/up/helper.go index 869e8ac2b5..d4a9c529b1 100644 --- a/e2e/tests/up/helper.go +++ b/e2e/tests/up/helper.go @@ -17,7 +17,6 @@ import ( provider2 "github.com/devsy-org/devsy/pkg/provider" "github.com/devsy-org/devsy/pkg/scanner" "github.com/onsi/ginkgo/v2" - "github.com/onsi/gomega" ) const ( @@ -89,10 +88,10 @@ func probeSSH( out, err := f.DevsySSHOnce(probeCtx, workspace, command) if err != nil { ginkgo.GinkgoWriter.Printf( - "ssh readiness probe failed for %s (%s): %v\n", + "ssh readiness probe failed for %s (%s): %s\n", workspace, command, - err, + fixtureDiagnostic(nil, err.Error()), ) } return out, err @@ -297,17 +296,17 @@ func (dtc *dockerTestContext) verifyContextAttachment( `if [ "$(cat `+tc.checkFile+`)" = expected-value ]; then printf present; else printf absent; fi`, ) framework.ExpectNoError(err) - gomega.Expect(strings.TrimSpace(out)).To(gomega.Equal("present")) + expectTrimmed(out, "present") if tc.sessionCheck != "" { fileCheck := `if [ -e /run/devsy/secrets-env/` + tc.sessionFile + ` ]; then ` + `stat -c '%U:%a' /run/devsy/secrets-env/` + tc.sessionFile + `; else printf missing; fi` out, err = dtc.f.DevsySSH(ctx, tempDir, fileCheck) framework.ExpectNoError(err) - gomega.Expect(strings.TrimSpace(out)).To(gomega.Equal("vscode:600")) + expectTrimmed(out, "vscode:600") out, err = dtc.f.DevsySSH(ctx, tempDir, tc.sessionCheck) framework.ExpectNoError(err) - gomega.Expect(strings.TrimSpace(out)).To(gomega.Equal("present")) + expectTrimmed(out, "present") } _, err = dtc.f.ExecCommandOutput(ctx, []string{tc.command, "detach", tc.name}) @@ -319,10 +318,14 @@ func (dtc *dockerTestContext) verifyContextAttachment( `if [ -s `+tc.checkFile+` ]; then printf present; else printf absent; fi`, ) framework.ExpectNoError(err) - gomega.Expect(strings.TrimSpace(out)).To(gomega.Equal("absent")) + expectTrimmed(out, "absent") if tc.sessionCheck != "" { out, err = dtc.f.DevsySSH(ctx, tempDir, tc.sessionCheck) framework.ExpectNoError(err) - gomega.Expect(strings.TrimSpace(out)).To(gomega.Equal("absent")) + expectTrimmed(out, "absent") } } + +func expectTrimmed(out, want string) { + expectFixtureOutput(strings.TrimSpace(out) == want, out, "expected output "+want) +} diff --git a/e2e/tests/up/podman_rootful.go b/e2e/tests/up/podman_rootful.go index 9ea7c26505..f6615f72a9 100644 --- a/e2e/tests/up/podman_rootful.go +++ b/e2e/tests/up/podman_rootful.go @@ -12,8 +12,8 @@ import ( "time" "github.com/devsy-org/devsy/e2e/framework" + "github.com/devsy-org/devsy/logging/redact" "github.com/devsy-org/devsy/pkg/docker" - "github.com/devsy-org/devsy/pkg/secrets" "github.com/onsi/ginkgo/v2" ) @@ -512,8 +512,8 @@ func recoverPodmanCleanup( // Redact before truncation so a clipped URL cannot expose partial credentials. func redactPodmanDiagnostics(text string) string { - return secrets.Combine(secrets.NewRedactor(podmanEndpointSecrets(os.Getenv("DOCKER_HOST"))), - secrets.NewEnvironmentRedactor(os.Environ())).Redact(text) + return redact.Combine(redact.FromEntries(podmanEndpointSecrets(os.Getenv("DOCKER_HOST"))), + redact.New(redact.Options{Environment: os.Environ()})).RedactDiagnostic(text) } func podmanEndpointSecrets(value string) []string { diff --git a/e2e/tests/up/provider_kubernetes_dockerless_path.go b/e2e/tests/up/provider_kubernetes_dockerless_path.go index 5c7a610b79..3158d96ba7 100644 --- a/e2e/tests/up/provider_kubernetes_dockerless_path.go +++ b/e2e/tests/up/provider_kubernetes_dockerless_path.go @@ -127,15 +127,20 @@ func (fixture *dockerlessPathFixture) up( names.Flag(names.IDE), kubernetesIgnoreIDE, } - stdout, stderr, upErr := fixture.framework.ExecCommandCapture( + stdout, stderr, upErr := fixture.framework.ExecCommandDiagnosticCapture( commandCtx, append(baseArgs, args...), ) gomega.Expect(upErr).NotTo(gomega.HaveOccurred(), - "%s first CLI attempt: stdout=%s stderr=%s", phase, stdout, stderr) + "%s first CLI attempt: stdout=%s stderr=%s", phase, + fixtureDiagnostic(nil, stdout), fixtureDiagnostic(nil, stderr)) output := stdout + stderr - gomega.Expect(output).NotTo(gomega.ContainSubstring("failed to configure docker credentials")) - gomega.Expect(output).NotTo(gomega.ContainSubstring("docker credentials disabled")) + expectFixtureOutput( + !strings.Contains(output, "failed to configure docker credentials"), output, + phase+" configures docker credentials") + expectFixtureOutput( + !strings.Contains(output, "docker credentials disabled"), output, + phase+" keeps docker credentials enabled") helperPattern := regexp.MustCompile( `Wrote docker credentials helper to ` + `(/\.dockerless/\.docker/docker-credentials-[a-zA-Z0-9]{12})/`, @@ -147,8 +152,16 @@ func (fixture *dockerlessPathFixture) up( if mustBuild { gomega.Expect(helpers). NotTo(gomega.BeEmpty(), "%s must configure Dockerless credentials", phase) - gomega.Expect(output).To(gomega.ContainSubstring("starting dockerless build"), phase) - gomega.Expect(output).To(gomega.ContainSubstring("dockerless build completed"), phase) + expectFixtureOutput( + strings.Contains(output, "starting dockerless build"), output, phase+" starts a build") + expectFixtureOutput( + strings.Contains( + output, + "dockerless build completed", + ), + output, + phase+" completes the build", + ) } } @@ -199,9 +212,10 @@ func (fixture *dockerlessPathFixture) verifyEnvironment( // SSH must find the executable solely through the image/remote environment. out, err := fixture.framework.DevsySSHOnce(commandCtx, fixture.id, dockerlessPathCommand) gomega.Expect(err).NotTo(gomega.HaveOccurred(), "%s bare-name SSH command", phase) - gomega.Expect(strings.TrimSpace(out)). - To(gomega.Equal(expectedOutput), "%s SSH environment", phase) - gomega.Expect(out).NotTo(gomega.ContainSubstring("/.dockerless")) + expectFixtureOutput( + strings.TrimSpace(out) == expectedOutput, out, phase+" SSH environment") + expectFixtureOutput( + !strings.Contains(out, "/.dockerless"), out, phase+" SSH output omits the build path") // Raw kubectl exec reads files without treating the pod's original builder // environment as the post-setup agent/SSH environment. diff --git a/e2e/tests/up/provider_kubernetes_ssh_agent.go b/e2e/tests/up/provider_kubernetes_ssh_agent.go index fb9101aaa9..16190379cc 100644 --- a/e2e/tests/up/provider_kubernetes_ssh_agent.go +++ b/e2e/tests/up/provider_kubernetes_ssh_agent.go @@ -104,12 +104,13 @@ var _ = ginkgo.Describe( "workspace", "up", "--debug", "--ide", "none", "--pull-from-inside-container=true", } - stdout, stderr, err := f.ExecCommandCapture( + stdout, stderr, err := f.ExecCommandDiagnosticCapture( commandCtx, append(baseArgs, args...), ) gomega.Expect(err). - NotTo(gomega.HaveOccurred(), "%s first CLI attempt: stdout=%s stderr=%s", phase, stdout, stderr) + NotTo(gomega.HaveOccurred(), "%s first CLI attempt: stdout=%s stderr=%s", phase, + fixtureDiagnostic(nil, stdout), fixtureDiagnostic(nil, stderr)) for _, failure := range []string{ "chown ssh agent sock file", "fatal: Unable to read current working directory", @@ -214,10 +215,14 @@ func privateSSHGitFixture( "refs/heads/main", ) if err != nil { - return fmt.Errorf("authenticated fixture readiness: %w: %s", err, out) + return fmt.Errorf( + "authenticated fixture readiness: %w: %s", + err, + fixtureDiagnostic(env, out), + ) } if !strings.Contains(out, sha) { - return fmt.Errorf("fixture HEAD mismatch: %s", out) + return fmt.Errorf("fixture HEAD mismatch: %s", fixtureDiagnostic(env, out)) } return nil }).WithTimeout(time.Minute).WithPolling(time.Second).Should(gomega.Succeed()) @@ -238,7 +243,8 @@ func expectUnauthenticatedDenied(ctx context.Context, source, sshOptions string) source, ) gomega.Expect(err). - To(gomega.HaveOccurred(), "private repository must deny unauthenticated ls-remote: %s", out) + To(gomega.HaveOccurred(), "private repository must deny unauthenticated ls-remote: %s", + fixtureDiagnostic(nil, out)) gomega.Expect(out). To(gomega.ContainSubstring("Permission denied"), "unauthenticated failure must be authentication rejection") } @@ -306,7 +312,8 @@ func startKindSSHGitServer(ctx context.Context, buildDir, id string) string { defer cancel() if ginkgo.CurrentSpecReport().Failed() { out, err := runFixtureCommand(cleanupCtx, 2*time.Minute, nil, "docker", "logs", id) - ginkgo.GinkgoWriter.Printf("private SSH Git service logs:\n%s\nerror: %v\n", out, err) + ginkgo.GinkgoWriter.Printf("private SSH Git service logs:\n%s\nerror: %s\n", + fixtureDiagnostic(nil, out), fixtureDiagnostic(nil, fmt.Sprint(err))) } fixtureCommand(cleanupCtx, 2*time.Minute, nil, "docker", "rm", "-f", id) }) diff --git a/e2e/tests/up/provider_microsandbox_egress.go b/e2e/tests/up/provider_microsandbox_egress.go index fd6ed95eb6..862536f35a 100644 --- a/e2e/tests/up/provider_microsandbox_egress.go +++ b/e2e/tests/up/provider_microsandbox_egress.go @@ -311,7 +311,7 @@ func microsandboxEgressIP(ctx context.Context, args ...string) error { // #nosec G204 -- fixed ip executable and generated test-owned interface/address arguments. out, err := exec.CommandContext(commandCtx, "ip", args...).CombinedOutput() if err != nil { - return fmt.Errorf("ip %v: %w: %s", args, err, out) + return fmt.Errorf("ip %v: %w: %s", args, err, fixtureDiagnostic(nil, string(out))) } return nil } diff --git a/e2e/tests/up/provider_microsandbox_mounts.go b/e2e/tests/up/provider_microsandbox_mounts.go index eb90fbac2e..9d3f0cc58e 100644 --- a/e2e/tests/up/provider_microsandbox_mounts.go +++ b/e2e/tests/up/provider_microsandbox_mounts.go @@ -334,7 +334,7 @@ func microsandboxSSHAsRootOnce( f *framework.Framework, workspace, command string, ) (string, error) { - stdout, stderr, err := f.ExecCommandCapture(ctx, []string{ + stdout, stderr, err := f.ExecCommandDiagnosticCapture(ctx, []string{ cmdWorkspace, cmdSSH, workspace, @@ -345,7 +345,11 @@ func microsandboxSSHAsRootOnce( names.Flag(names.Debug), }) if err != nil { - return "", fmt.Errorf("devsy ssh as root failed: %w (stderr: %s)", err, stderr) + return "", fmt.Errorf( + "devsy ssh as root failed: %w (stderr: %s)", + err, + fixtureDiagnostic(nil, stderr), + ) } return stdout, nil } @@ -353,5 +357,6 @@ func microsandboxSSHAsRootOnce( func microsandboxMountCommand(ctx context.Context, args ...string) { // #nosec G204 -- fixed runtime executable and test-owned volume arguments. out, err := exec.CommandContext(ctx, "msb", args...).CombinedOutput() - gomega.Expect(err).NotTo(gomega.HaveOccurred(), fmt.Sprintf("msb %v: %s", args, out)) + gomega.Expect(err). + NotTo(gomega.HaveOccurred(), fmt.Sprintf("msb %v: %s", args, fixtureDiagnostic(nil, string(out)))) } diff --git a/e2e/tests/up/provider_microsandbox_remote_agent_linux.go b/e2e/tests/up/provider_microsandbox_remote_agent_linux.go index 30b711dce2..04e66f61b3 100644 --- a/e2e/tests/up/provider_microsandbox_remote_agent_linux.go +++ b/e2e/tests/up/provider_microsandbox_remote_agent_linux.go @@ -463,10 +463,7 @@ func removeRemoteAgentContainer(ctx context.Context, name string) error { // #nosec G204 -- docker is fixed; name is a nonce generated within this test. cmd := exec.CommandContext(ctx, "docker", "rm", "-f", name) output, err := cmd.CombinedOutput() - if err != nil && !strings.Contains(string(output), "No such container") { - return fmt.Errorf("remove remote agent container: %w: %s", err, output) - } - return nil + return remoteAgentRemovalError(string(output), err) } func assertRemoteAgentBinary(ctx context.Context, containerName, path, expectedHash string) string { diff --git a/e2e/tests/up/provider_podman_machine_bind_darwin.go b/e2e/tests/up/provider_podman_machine_bind_darwin.go index d245d3c3d7..5681b59884 100644 --- a/e2e/tests/up/provider_podman_machine_bind_darwin.go +++ b/e2e/tests/up/provider_podman_machine_bind_darwin.go @@ -75,7 +75,10 @@ var _ = ginkgo.Describe("Podman Machine engine-side bind sources", run := func(args ...string) string { // #nosec G204 -- executable is resolved Podman; arguments are test commands and quoted owned fixture paths. output, cmdErr := exec.CommandContext(ctx, podman, args...).CombinedOutput() - framework.ExpectNoError(cmdErr, "podman %v: %s", args, output) + if cmdErr != nil { + framework.ExpectNoError(cmdErr, "podman %v: %s", args, + redactPodmanDiagnostics(string(output))) + } return strings.TrimSpace(string(output)) } for _, key := range []string{ @@ -165,8 +168,8 @@ var _ = ginkgo.Describe("Podman Machine engine-side bind sources", gomega.Expect(guestUID).NotTo(gomega.Equal("0")) ginkgo.GinkgoWriter.Printf( "Guest UID: %s; runtime directory: %s\n", - guestUID, - guest(`printf %s "$XDG_RUNTIME_DIR"`), + redactPodmanDiagnostics(guestUID), + redactPodmanDiagnostics(guest(`printf %s "$XDG_RUNTIME_DIR"`)), ) gomega.Expect(info.Host.Hostname).To(gomega.Equal(guest("hostname"))) gomega.Expect(endpoint.Path). @@ -174,7 +177,7 @@ var _ = ginkgo.Describe("Podman Machine engine-side bind sources", gomega.Expect(guest(`id -un`)).To(gomega.Equal(endpoint.User.Username())) ginkgo.GinkgoWriter.Printf( "Podman version: %s; selected connection: %s; machine: %s\n", - run("version", podmanFormatFlag, podmanJSONFormat), + redactPodmanDiagnostics(run("version", podmanFormatFlag, podmanJSONFormat)), connection.Name, machine, ) diff --git a/internal/diagnosticredact/redact.go b/internal/diagnosticredact/redact.go index cc303d443f..bf3e1399ca 100644 --- a/internal/diagnosticredact/redact.go +++ b/internal/diagnosticredact/redact.go @@ -35,6 +35,10 @@ func Record(redactor *redact.Redactor, value string) string { } } +func Stream(redactor *redact.Redactor) *redact.Stream { + return redact.NewDiagnosticStream(redactor) +} + func reconcileText(redactor *redact.Redactor, left, right string) string { prefix, suffix := sharedEdges(left, right) start, end := left[:prefix], left[len(left)-suffix:]