From 40ab524072a4d1a77d14f32d57f3a5ae885c5b94 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Sat, 21 Nov 2026 06:50:28 -0600 Subject: [PATCH 1/7] fix(command): mask subprocess diagnostics --- go.mod | 1 + go.sum | 2 + internal/diagnosticredact/redact.go | 96 ++++++++++++++++++++++ internal/diagnosticredact/redact_test.go | 48 +++++++++++ pkg/clierr/errors.go | 11 ++- pkg/clierr/errors_test.go | 11 +++ pkg/command/command.go | 44 +++++++++- pkg/command/diagnostic_test.go | 100 +++++++++++++++++++++++ pkg/command/process_windows.go | 22 +---- pkg/docker/helper.go | 6 +- pkg/docker/image_platform.go | 7 +- pkg/docker/image_platform_test.go | 12 +++ 12 files changed, 331 insertions(+), 29 deletions(-) create mode 100644 internal/diagnosticredact/redact.go create mode 100644 internal/diagnosticredact/redact_test.go create mode 100644 pkg/command/diagnostic_test.go diff --git a/go.mod b/go.mod index f3bee37ce8..bea4e6ac84 100644 --- a/go.mod +++ b/go.mod @@ -229,6 +229,7 @@ require ( github.com/davidmz/go-pageant v1.0.2 // indirect github.com/dblohm7/wingoes v0.0.0-20240119213807-a09d6be7affa // indirect github.com/devsy-org/admin-apis v1.2.0 // indirect + github.com/devsy-org/devsy/logging v0.0.0-20261121080943-b1a01fb92a98 github.com/dghubble/go-twitter v0.0.0-20211115160449-93a8679adecb // indirect github.com/dghubble/oauth1 v0.7.3 // indirect github.com/dghubble/sling v1.4.0 // indirect diff --git a/go.sum b/go.sum index 9311f9f120..6baf6e1af8 100644 --- a/go.sum +++ b/go.sum @@ -418,6 +418,8 @@ github.com/devsy-org/apiserver v1.5.4 h1:/bEPrSRSlfii2QHxc9qAiXlQawNJfIaPLraR3bY github.com/devsy-org/apiserver v1.5.4/go.mod h1:sDFCTjCN13wAHhno4KX0Z756gCI8ttW/c7NwTzo16K0= github.com/devsy-org/devsy-runtime-sdk v1.6.0 h1:iEV4Y0Sc3LJNkTLadJSKcWcAf/oCXvC/AkHilrg/KyM= github.com/devsy-org/devsy-runtime-sdk v1.6.0/go.mod h1:MiBP/fiY83DAS0TueEiJZSHYZ1vq85UR4KJTMfHIM+E= +github.com/devsy-org/devsy/logging v0.0.0-20261121080943-b1a01fb92a98 h1:Z+uG77rbf+Msu7LSoWnAe4MylASfsr8gin37+DA4ikk= +github.com/devsy-org/devsy/logging v0.0.0-20261121080943-b1a01fb92a98/go.mod h1:kpwoMhSdrhnJfTJ014ht04BLw0DS7FyF33RqIIe8CKs= github.com/devsy-org/ssh v1.2.10 h1:fCxyqx8ti6qzKhVts55q0zDpx72q1YHNM7VK6TGkAps= github.com/devsy-org/ssh v1.2.10/go.mod h1:IiIPdjt/ilk08Z+I6ofwUaH52I/1PmVnp+0pOVQXlwI= github.com/devsy-org/tailscale v1.102.2 h1:9SB6htvO+HmG8alal8WGCshHapfHR7dUFRSMYiFIzIM= diff --git a/internal/diagnosticredact/redact.go b/internal/diagnosticredact/redact.go new file mode 100644 index 0000000000..cc303d443f --- /dev/null +++ b/internal/diagnosticredact/redact.go @@ -0,0 +1,96 @@ +package diagnosticredact + +import ( + "bytes" + "encoding/json" + "os" + "strings" + + "github.com/devsy-org/devsy/logging/redact" +) + +func EnvironmentRecord(value string) string { + return RecordWithEnvironment(os.Environ(), value) +} + +func RecordWithEnvironment(environment []string, value string) string { + return Record(redact.New(redact.Options{Environment: environment}), value) +} + +func Record(redactor *redact.Redactor, value string) string { + if redactor == nil { + return value + } + conservative := redactor.Redact(value) + folded := redactor.RedactDiagnostic(value) + switch { + case conservative == value || conservative == folded: + return folded + case folded == value: + return conservative + case json.Valid([]byte(value)): + return reconcileObjects(redactor, conservative, folded) + default: + return reconcileText(redactor, conservative, folded) + } +} + +func reconcileText(redactor *redact.Redactor, left, right string) string { + prefix, suffix := sharedEdges(left, right) + start, end := left[:prefix], left[len(left)-suffix:] + marker := redactor.Mask() + if strings.HasSuffix(start, marker) || strings.HasPrefix(end, marker) { + marker = "" + } + masked := strings.ToValidUTF8(start+marker+end, "\uFFFD") + if completeMaskIsStable(redactor, masked) { + return masked + } + return redactor.Mask() +} + +func sharedEdges(left, right string) (int, int) { + limit := min(len(left), len(right)) + prefix := 0 + for prefix < limit && left[prefix] == right[prefix] { + prefix++ + } + suffix := 0 + for suffix < limit-prefix && left[len(left)-1-suffix] == right[len(right)-1-suffix] { + suffix++ + } + return prefix, suffix +} + +func reconcileObjects(redactor *redact.Redactor, conservative, folded string) string { + mask, _ := json.Marshal(redactor.Mask()) + left, right := decodeObject(conservative), decodeObject(folded) + if left == nil || right == nil { + return string(mask) + } + for key, value := range left { + next, present := right[key] + if !present { + delete(left, key) + } else if !bytes.Equal(value, next) { + left[key] = mask + } + } + merged, err := json.Marshal(left) + if err != nil || !completeMaskIsStable(redactor, string(merged)) { + return string(mask) + } + return string(merged) +} + +func completeMaskIsStable(redactor *redact.Redactor, value string) bool { + return redactor.Redact(value) == value && redactor.RedactDiagnostic(value) == value +} + +func decodeObject(value string) map[string]json.RawMessage { + var result map[string]json.RawMessage + if json.Unmarshal([]byte(value), &result) != nil { + return nil + } + return result +} diff --git a/internal/diagnosticredact/redact_test.go b/internal/diagnosticredact/redact_test.go new file mode 100644 index 0000000000..4854ae051a --- /dev/null +++ b/internal/diagnosticredact/redact_test.go @@ -0,0 +1,48 @@ +package diagnosticredact + +import ( + "encoding/json" + "strings" + "testing" + + "github.com/devsy-org/devsy/logging/redact" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestRecordKeepsConservativeURLMasking(t *testing.T) { + redactor := redact.FromEntries(nil) + const privateURL = "https://user:synthetic/path@host.test/" + const publicMessage = "ordinary client detail" + const diagnostic = "Authorization:\n Bearer synthetic-folded\n\n" + publicMessage + assert.Equal(t, "https://***@host.test/", Record(redactor, privateURL)) + context := strings.Repeat("a", 17*1024) + raw, err := json.Marshal(map[string]string{ + "msg": diagnostic, "url": privateURL, "level": "error", "context": context, + }) + require.NoError(t, err) + var cleaned map[string]string + require.NoError(t, json.Unmarshal([]byte(Record(redactor, string(raw))), &cleaned)) + assert.Equal(t, "error", cleaned["level"]) + assert.Equal(t, context, cleaned["context"]) + assert.Equal(t, redactor.Mask(), cleaned["url"]) + assert.Equal(t, redactor.Mask(), cleaned["msg"]) +} + +func TestRecordKeepsCompleteInputMaskingAcrossMixedRecords(t *testing.T) { + const canary = "synthetic-mixed-record-value" + const detail = "ordinary mixed record detail" + input := "\"Authorization:\"\n Bearer " + canary + "\n\n" + detail + output := Record(redact.FromEntries(nil), input) + assert.NotContains(t, output, canary) + assert.Contains(t, output, detail) +} + +func TestRecordKeepsOrdinaryPrefixAtTerminalFold(t *testing.T) { + const canary = "synthetic-terminal-fold-value" + const detail = "ordinary detail before terminal fold" + input := detail + "\nAuthorization: Bearer synthetic-initial-value\n " + canary + output := Record(redact.FromEntries(nil), input) + assert.NotContains(t, output, canary) + assert.Contains(t, output, detail) +} diff --git a/pkg/clierr/errors.go b/pkg/clierr/errors.go index 3e2c3b3703..78cd411623 100644 --- a/pkg/clierr/errors.go +++ b/pkg/clierr/errors.go @@ -9,6 +9,7 @@ import ( "maps" "strings" + "github.com/devsy-org/devsy/pkg/command" "github.com/devsy-org/devsy/pkg/secrets" "go.uber.org/zap/zapcore" ) @@ -191,9 +192,7 @@ func classifyContextError(err error) *CLIError { } func classifyDockerDaemonError(err error) *CLIError { - message := strings.ToLower(err.Error()) - if !strings.Contains(message, "cannot connect to the docker daemon") && - !strings.Contains(message, "is the docker daemon running") { + if !command.MatchErrorOutput(err, matchesDockerDaemonError) { return nil } return &CLIError{ @@ -204,6 +203,12 @@ func classifyDockerDaemonError(err error) *CLIError { } } +func matchesDockerDaemonError(message string) bool { + message = strings.ToLower(message) + return strings.Contains(message, "cannot connect to the docker daemon") || + strings.Contains(message, "is the docker daemon running") +} + func classifySecretError(err error) *CLIError { for _, state := range []struct { target error diff --git a/pkg/clierr/errors_test.go b/pkg/clierr/errors_test.go index 487a9e6c17..3466599352 100644 --- a/pkg/clierr/errors_test.go +++ b/pkg/clierr/errors_test.go @@ -7,6 +7,7 @@ import ( "fmt" "testing" + "github.com/devsy-org/devsy/pkg/command" "go.uber.org/zap/zapcore" ) @@ -35,6 +36,16 @@ func TestClassifyCancellation(t *testing.T) { const testRateLimited = "rate limited" +func TestClassifyDockerDaemonSurvivesOutputMasking(t *testing.T) { + const marker = "cannot connect to the docker daemon" + t.Setenv("DEVSY_TEST_DOCKER_TOKEN", marker) + err := command.WrapCommandError([]byte(marker), stderrs.New("inspect failed")) + got := Classify(fmt.Errorf("command failed: %w", err)) + if got.Code != CodeDockerDaemonUnreachable || got.Message != dockerDaemonUnavailableMessage { + t.Fatalf("Classify masked Docker output = %+v", got) + } +} + func TestClassify_NilReturnsNil(t *testing.T) { if got := Classify(nil); got != nil { t.Fatalf("Classify(nil) = %v, want nil", got) diff --git a/pkg/command/command.go b/pkg/command/command.go index 6262202b75..f2daf2ea6a 100644 --- a/pkg/command/command.go +++ b/pkg/command/command.go @@ -2,7 +2,11 @@ package command import ( "errors" + "fmt" "os/exec" + "slices" + + "github.com/devsy-org/devsy/internal/diagnosticredact" ) func WrapCommandError(stdout []byte, err error) error { @@ -22,6 +26,33 @@ type Error struct { } func (e *Error) Error() string { + return diagnosticredact.EnvironmentRecord(e.output()) +} + +func (e *Error) Unwrap() error { + return e.err +} + +func MatchErrorOutput(err error, match func(string) bool) bool { + return err != nil && (match(err.Error()) || matchOriginalCommandOutput(err, match)) +} + +func matchOriginalCommandOutput(err error, match func(string) bool) bool { + switch err := err.(type) { + case nil: + return false + case *Error: + return match(err.output()) || matchOriginalCommandOutput(err.err, match) + case interface{ Unwrap() []error }: + return slices.ContainsFunc(err.Unwrap(), func(cause error) bool { + return matchOriginalCommandOutput(cause, match) + }) + default: + return matchOriginalCommandOutput(errors.Unwrap(err), match) + } +} + +func (e *Error) output() string { message := "" if len(e.stdout) > 0 { message += string(e.stdout) + "\n" @@ -35,8 +66,17 @@ func (e *Error) Error() string { return message + e.err.Error() } -func (e *Error) Unwrap() error { - return e.err +func formatTaskkillDiagnostic(pid int, err error, output []byte) error { + detail := truncateOutput(diagnosticredact.EnvironmentRecord(string(output))) + return fmt.Errorf("taskkill /PID %d /T /F: %w: %s", pid, err, detail) +} + +func truncateOutput(output string) string { + const maxOutput = 256 + if len(output) > maxOutput { + return output[:maxOutput] + "..." + } + return output } func Exists(cmd string) bool { diff --git a/pkg/command/diagnostic_test.go b/pkg/command/diagnostic_test.go new file mode 100644 index 0000000000..c564b295aa --- /dev/null +++ b/pkg/command/diagnostic_test.go @@ -0,0 +1,100 @@ +package command + +import ( + "errors" + "go/ast" + "go/parser" + "go/token" + "os/exec" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestTaskkillDiagnosticMasksFoldedOutput(t *testing.T) { + const fixture = "synthetic-taskkill-continuation" + cause := errors.New("operation failed") + err := formatTaskkillDiagnostic( + 42, + cause, + []byte("public diagnostic\nAuthorization: Bearer\n "+fixture), + ) + require.ErrorIs(t, err, cause) + assert.Contains(t, err.Error(), "/PID 42") + assert.Contains(t, err.Error(), "public diagnostic") + assert.NotContains(t, err.Error(), fixture) +} + +func TestTaskkillDiagnosticMasksBeforeTruncating(t *testing.T) { + const privatePrefix = "synthetic-long-command-value" + value := privatePrefix + strings.Repeat("z", 96) + t.Setenv("DEVSY_TEST_TASKKILL_TOKEN", value) + output := strings.Repeat("public ", 30) + value + strings.Repeat(" ordinary", 32) + err := formatTaskkillDiagnostic(42, errors.New("fixture failure"), []byte(output)) + assert.NotContains(t, err.Error(), privatePrefix) + assert.Contains(t, err.Error(), "***") + assert.Contains(t, err.Error(), "ordinary") + assert.True(t, strings.HasSuffix(err.Error(), "...")) +} + +func TestCommandErrorMasksFoldedOutput(t *testing.T) { + const fixture = "synthetic-command-continuation" + exit := &exec.ExitError{ + Stderr: []byte("Authorization: Bearer\r\n " + fixture + "\r\npublic stderr"), + } + err := WrapCommandError([]byte("public stdout"), exit) + require.ErrorIs(t, err, exit) + assert.NotContains(t, err.Error(), fixture) + assert.Contains(t, err.Error(), "public stdout") + assert.Contains(t, err.Error(), "public stderr") +} + +func TestWindowsCapturedOutputUsesDiagnosticFormatter(t *testing.T) { + file, err := parser.ParseFile( + token.NewFileSet(), + "process_windows.go", + nil, + parser.SkipObjectResolution, + ) + require.NoError(t, err) + var paths int + for _, decl := range file.Decls { + function, ok := decl.(*ast.FuncDecl) + if !ok || function.Body == nil { + continue + } + captured, formatted := countWindowsDiagnosticCalls(function.Body) + assert.Equal( + t, + captured, + formatted, + "%s must mask captures before formatting errors", + function.Name.Name, + ) + paths += captured + } + assert.GreaterOrEqual(t, paths, 2) +} + +func countWindowsDiagnosticCalls(body *ast.BlockStmt) (captured, formatted int) { + ast.Inspect(body, func(node ast.Node) bool { + call, ok := node.(*ast.CallExpr) + if !ok { + return true + } + switch target := call.Fun.(type) { + case *ast.SelectorExpr: + if target.Sel.Name == "CombinedOutput" { + captured++ + } + case *ast.Ident: + if target.Name == "formatTaskkillDiagnostic" { + formatted++ + } + } + return true + }) + return captured, formatted +} diff --git a/pkg/command/process_windows.go b/pkg/command/process_windows.go index ef2e2ad968..22f747448a 100644 --- a/pkg/command/process_windows.go +++ b/pkg/command/process_windows.go @@ -154,10 +154,7 @@ func terminateLegacyRefWithoutIdentity(ref ProcessRef) error { cmd := exec.Command("taskkill", "/PID", strconv.Itoa(ref.PID), "/T", "/F") output, err := cmd.CombinedOutput() if err != nil { - return fmt.Errorf( - "taskkill /PID %d /T /F: %w: %s", - ref.PID, err, truncateOutput(string(output)), - ) + return formatTaskkillDiagnostic(ref.PID, err, output) } return verifyTerminated(ref.PID) } @@ -270,12 +267,7 @@ func killTree(pid, treeName string) error { if checkErr == nil && !stillRunning { return nil } - return fmt.Errorf( - "taskkill /PID %d /T /F: %w: %s", - parsed, - runErr, - truncateOutput(string(output)), - ) + return formatTaskkillDiagnostic(parsed, runErr, output) } if jobErr != nil { @@ -397,16 +389,6 @@ func parsePID(pid string) (int, error) { return parsed, nil } -// truncateOutput bounds captured taskkill output, which is localized and -// never parsed. -func truncateOutput(output string) string { - const maxOutput = 256 - if len(output) > maxOutput { - return output[:maxOutput] + "..." - } - return output -} - func processRefExited(ref ProcessRef) (bool, error) { running, err := savedWorkerRunning(ref) if err != nil { diff --git a/pkg/docker/helper.go b/pkg/docker/helper.go index 90f1b1720e..9150e3e3cc 100644 --- a/pkg/docker/helper.go +++ b/pkg/docker/helper.go @@ -68,7 +68,11 @@ var imageNotFoundMarkers = []string{ } func isImageNotFoundError(err error) bool { - msg := strings.ToLower(err.Error()) + return command.MatchErrorOutput(err, imageNotFoundMessage) +} + +func imageNotFoundMessage(message string) bool { + msg := strings.ToLower(message) for _, marker := range imageNotFoundMarkers { if strings.Contains(msg, marker) { return true diff --git a/pkg/docker/image_platform.go b/pkg/docker/image_platform.go index 7465585bf5..19b1bbb6b7 100644 --- a/pkg/docker/image_platform.go +++ b/pkg/docker/image_platform.go @@ -70,13 +70,12 @@ func (r *DockerHelper) inspectPlatformOrFallback( return nil, fmt.Errorf("inspect image platform: %w", ctx.Err()) } wrapped := command.WrapCommandError(nil, err) - message := strings.ToLower(wrapped.Error()) - if platformInspectionUnsupported(message) { + if command.MatchErrorOutput(wrapped, platformInspectionUnsupported) { // Older engines and compatible runtimes have a single-platform local // image store. The caller still verifies that cached configuration. return base, nil } - if platformInspectionMiss(message) { + if command.MatchErrorOutput(wrapped, platformInspectionMiss) { return nil, fmt.Errorf("%s: %w", image, ErrImageNotFound) } return nil, classifyImageError(image, fmt.Errorf("inspect image platform: %w", wrapped)) @@ -92,11 +91,13 @@ func (r *DockerHelper) inspectPlatformOrFallback( } func platformInspectionUnsupported(message string) bool { + message = strings.ToLower(message) return strings.Contains(message, "unknown flag: --platform") || (strings.Contains(message, "--platform") && strings.Contains(message, "requires api version")) } func platformInspectionMiss(message string) bool { + message = strings.ToLower(message) return strings.Contains(message, "does not match the specified platform") || strings.Contains(message, "no matching manifest for") || strings.Contains(message, "no image found for platform") diff --git a/pkg/docker/image_platform_test.go b/pkg/docker/image_platform_test.go index 8eac158052..caaa63e980 100644 --- a/pkg/docker/image_platform_test.go +++ b/pkg/docker/image_platform_test.go @@ -6,6 +6,7 @@ import ( "runtime" "testing" + "github.com/devsy-org/devsy/pkg/command" "github.com/devsy-org/devsy/pkg/devcontainer/config" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/stretchr/testify/require" @@ -57,6 +58,17 @@ esac require.NoError(t, err) _, err = helper.InspectImageForPlatform(context.Background(), "test:base", "linux/arm64") require.ErrorIs(t, err, ErrImageNotFound) + t.Setenv("DEVSY_TEST_PLATFORM_TOKEN", "unknown flag: --platform") + _, err = helper.InspectImageForPlatform(t.Context(), "test:base", "linux/amd64") + require.NoError(t, err) +} + +func TestImageMissClassificationSurvivesOutputMasking(t *testing.T) { + const marker = "no such image" + t.Setenv("DEVSY_TEST_IMAGE_TOKEN", marker) + err := command.WrapCommandError([]byte(marker), errors.New("inspect failed")) + require.NotContains(t, err.Error(), marker) + require.ErrorIs(t, classifyImageError("fixture", err), ErrImageNotFound) } func TestValidateImagePlatform(t *testing.T) { From 63a472f1a8bec0191cb85b78a53ab9598b36fab5 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Sat, 21 Nov 2026 06:56:32 -0600 Subject: [PATCH 2/7] fix(e2e): mask subprocess diagnostics in test reports --- e2e/framework/command.go | 23 ++- e2e/framework/command_diagnostics_test.go | 181 ++++++++++++++++++ e2e/framework/exec.go | 78 +++++++- e2e/framework/exec_capture_test.go | 49 ++++- e2e/framework/runenv/exec.go | 6 +- e2e/framework/runenv/exec_test.go | 11 ++ e2e/framework/setuplogs.go | 8 +- e2e/framework/ssh_agent.go | 4 +- e2e/framework/workspace_edit.go | 14 +- e2e/tests/ssh/agent_forward.go | 13 +- e2e/tests/ssh/credentials_server_race.go | 24 ++- e2e/tests/ssh/detached_task.go | 13 +- e2e/tests/ssh/detached_validation.go | 7 +- e2e/tests/ssh/gh_public.go | 6 +- e2e/tests/ssh/gh_public_diagnostics.go | 4 +- e2e/tests/ssh/proxy_command.go | 13 +- e2e/tests/ssh/proxy_command_test.go | 43 ++++- e2e/tests/ssh/ssh.go | 78 +++++--- e2e/tests/up/fixture_command.go | 37 +++- e2e/tests/up/fixture_command_test.go | 107 +++++++++++ e2e/tests/up/helper.go | 4 +- e2e/tests/up/podman_rootful.go | 6 +- .../up/provider_kubernetes_dockerless_path.go | 5 +- e2e/tests/up/provider_kubernetes_ssh_agent.go | 19 +- e2e/tests/up/provider_microsandbox_egress.go | 2 +- e2e/tests/up/provider_microsandbox_mounts.go | 11 +- ...rovider_microsandbox_remote_agent_linux.go | 5 +- .../up/provider_podman_machine_bind_darwin.go | 11 +- internal/diagnosticredact/redact.go | 4 + 29 files changed, 672 insertions(+), 114 deletions(-) create mode 100644 e2e/framework/command_diagnostics_test.go create mode 100644 e2e/tests/up/fixture_command_test.go 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/exec.go b/e2e/framework/exec.go index 7fce2515dd..2128b40d8d 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,69 @@ 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 := &liveDiagnosticCapture{tee: tee, stream: diagnosticredact.Stream(redactor)} + return io.MultiWriter(capture, diagnostic), diagnostic.close +} + +type liveDiagnosticCapture struct { + tee *livetee.Tee + stream *redact.Stream +} + +func (w *liveDiagnosticCapture) Write(p []byte) (int, error) { + _, _ = io.WriteString(w.tee, w.stream.RedactChunk(string(p))) + return len(p), nil +} + +func (w *liveDiagnosticCapture) close() int64 { + _, _ = io.WriteString(w.tee, w.stream.Flush()) + return w.tee.Close() +} + +func externalCommandDiagnostic(value string) string { + redactor := redact.New(redact.Options{Environment: os.Environ()}) + return diagnosticredact.Record(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..e7d8b1d919 100644 --- a/e2e/framework/exec_capture_test.go +++ b/e2e/framework/exec_capture_test.go @@ -14,12 +14,24 @@ 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 "blocked": _, _ = fmt.Fprint(os.Stdout, strings.Repeat("x", 2<<20)) _, _ = fmt.Fprint(os.Stderr, "ready") @@ -34,7 +46,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 +54,39 @@ 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) TestBlockedLiveOutput() { s.T().Setenv("DEVSY_CAPTURE_CHILD", "blocked") outRead, outWrite, err := os.Pipe() @@ -66,7 +111,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) 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..f8727f2b80 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,7 +118,10 @@ 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])) 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..5f68e176ce 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" @@ -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"` 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/fixture_command.go b/e2e/tests/up/fixture_command.go index eb4ee60442..e711c62424 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,29 @@ 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) +} + // 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 +98,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 +126,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..14bb4a5491 --- /dev/null +++ b/e2e/tests/up/fixture_command_test.go @@ -0,0 +1,107 @@ +package up + +import ( + "os" + "os/exec" + "path/filepath" + "runtime" + "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 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..6d7def713c 100644 --- a/e2e/tests/up/helper.go +++ b/e2e/tests/up/helper.go @@ -89,10 +89,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 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..c6f38d6e41 100644 --- a/e2e/tests/up/provider_kubernetes_dockerless_path.go +++ b/e2e/tests/up/provider_kubernetes_dockerless_path.go @@ -127,12 +127,13 @@ 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")) 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:] From b43fa897f4cc59555a995fe30f12ee2221cef386 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Sat, 21 Nov 2026 07:09:44 -0600 Subject: [PATCH 3/7] fix(command): mask effective subprocess environment values --- pkg/apple/helper.go | 33 +++++++++++++++++------ pkg/command/command.go | 18 +++++++++++-- pkg/docker/helper.go | 22 ++++++++++----- pkg/docker/image_platform.go | 5 ++-- pkg/docker/stdio_transport_test.go | 43 ++++++++++++++++++++++++++---- pkg/ide/jetbrains/generic.go | 2 +- 6 files changed, 99 insertions(+), 24 deletions(-) diff --git a/pkg/apple/helper.go b/pkg/apple/helper.go index 01e434a4ee..6a4229ad35 100644 --- a/pkg/apple/helper.go +++ b/pkg/apple/helper.go @@ -202,9 +202,13 @@ func (h *AppleHelper) InspectContainers( return nil, nil } args := append([]string{"inspect"}, ids...) - out, err := h.buildCmd(ctx, args...).Output() + cmd := h.buildCmd(ctx, args...) + out, err := cmd.Output() if err != nil { - return nil, fmt.Errorf("inspect container: %w", command.WrapCommandError(out, err)) + return nil, fmt.Errorf( + "inspect container: %w", + command.WrapCommandErrorWithEnvironment(out, err, cmd.Environ()), + ) } var raw []containerInspect @@ -273,9 +277,14 @@ func (h *AppleHelper) InspectImage( imageName string, tryRemote bool, ) (*config.ImageDetails, error) { - out, err := h.buildCmd(ctx, "image", "inspect", imageName).Output() + cmd := h.buildCmd(ctx, "image", "inspect", imageName) + out, err := cmd.Output() if err != nil { - err = fmt.Errorf("inspect image %s: %w", imageName, command.WrapCommandError(out, err)) + err = fmt.Errorf( + "inspect image %s: %w", + imageName, + command.WrapCommandErrorWithEnvironment(out, err, cmd.Environ()), + ) } else { var raw []imageInspect switch { @@ -309,9 +318,13 @@ func (h *AppleHelper) InspectImage( } func (h *AppleHelper) GetImageTag(ctx context.Context, imageID string) (string, error) { - out, err := h.buildCmd(ctx, "image", "inspect", imageID).Output() + cmd := h.buildCmd(ctx, "image", "inspect", imageID) + out, err := cmd.Output() if err != nil { - return "", fmt.Errorf("inspect image: %w", command.WrapCommandError(out, err)) + return "", fmt.Errorf( + "inspect image: %w", + command.WrapCommandErrorWithEnvironment(out, err, cmd.Environ()), + ) } var raw []imageInspect if err := json.Unmarshal(out, &raw); err != nil { @@ -370,9 +383,13 @@ func (h *AppleHelper) buildCmd(ctx context.Context, args ...string) *exec.Cmd { } func (h *AppleHelper) listContainers(ctx context.Context) ([]containerInspect, error) { - out, err := h.buildCmd(ctx, "list", "--all", "--format", "json").Output() + cmd := h.buildCmd(ctx, "list", "--all", "--format", "json") + out, err := cmd.Output() if err != nil { - return nil, fmt.Errorf("list containers: %w", command.WrapCommandError(out, err)) + return nil, fmt.Errorf( + "list containers: %w", + command.WrapCommandErrorWithEnvironment(out, err, cmd.Environ()), + ) } var raw []containerInspect if err := json.Unmarshal(out, &raw); err != nil { diff --git a/pkg/command/command.go b/pkg/command/command.go index f2daf2ea6a..2f89a0c262 100644 --- a/pkg/command/command.go +++ b/pkg/command/command.go @@ -20,12 +20,26 @@ func WrapCommandError(stdout []byte, err error) error { } } +func WrapCommandErrorWithEnvironment(stdout []byte, err error, environment []string) error { + if err == nil { + return nil + } + wrapped := &Error{stdout: stdout, err: err} + masked := diagnosticredact.RecordWithEnvironment(environment, wrapped.output()) + wrapped.maskedOutput = &masked + return wrapped +} + type Error struct { - stdout []byte - err error + stdout []byte + err error + maskedOutput *string } func (e *Error) Error() string { + if e.maskedOutput != nil { + return *e.maskedOutput + } return diagnosticredact.EnvironmentRecord(e.output()) } diff --git a/pkg/docker/helper.go b/pkg/docker/helper.go index 9150e3e3cc..e6a29f1199 100644 --- a/pkg/docker/helper.go +++ b/pkg/docker/helper.go @@ -720,9 +720,13 @@ func (r *DockerHelper) GetImageTag(ctx context.Context, imageID string) (string, "{{if .RepoTags}}{{index .RepoTags 0}}{{end}}", } args = append(args, imageID) - out, err := r.buildCmd(ctx, args...).Output() + cmd := r.buildCmd(ctx, args...) + out, err := cmd.Output() if err != nil { - return "", fmt.Errorf("inspect image: %w", command.WrapCommandError(out, err)) + return "", fmt.Errorf( + "inspect image: %w", + command.WrapCommandErrorWithEnvironment(out, err, cmd.Environ()), + ) } repoTag := strings.TrimSpace(string(out)) @@ -812,9 +816,14 @@ func (r *DockerHelper) Inspect( ) error { args := []string{"inspect", "--type", inspectType} args = append(args, ids...) - out, err := r.buildCmd(ctx, args...).Output() + cmd := r.buildCmd(ctx, args...) + out, err := cmd.Output() if err != nil { - return fmt.Errorf("inspect %s: %w", inspectType, command.WrapCommandError(out, err)) + return fmt.Errorf( + "inspect %s: %w", + inspectType, + command.WrapCommandErrorWithEnvironment(out, err, cmd.Environ()), + ) } err = json.Unmarshal(out, obj) @@ -853,9 +862,10 @@ func (r *DockerHelper) FindContainer(ctx context.Context, labels []string) ([]st // This is useful in case the `--filter` doesn't work. func (r *DockerHelper) FindContainerJSON(ctx context.Context, labels []string) ([]string, error) { args := []string{"ps", "-q", "-a"} - out, err := r.buildCmd(ctx, args...).Output() + cmd := r.buildCmd(ctx, args...) + out, err := cmd.Output() if err != nil { - return nil, command.WrapCommandError(out, err) + return nil, command.WrapCommandErrorWithEnvironment(out, err, cmd.Environ()) } result := []string{} diff --git a/pkg/docker/image_platform.go b/pkg/docker/image_platform.go index 19b1bbb6b7..2859959026 100644 --- a/pkg/docker/image_platform.go +++ b/pkg/docker/image_platform.go @@ -64,12 +64,13 @@ func (r *DockerHelper) inspectPlatformOrFallback( image, platform string, base *config.ImageDetails, ) (*config.ImageDetails, error) { - out, err := r.buildCmd(ctx, "image", "inspect", "--platform", platform, image).Output() + cmd := r.buildCmd(ctx, "image", "inspect", "--platform", platform, image) + out, err := cmd.Output() if err != nil { if ctx.Err() != nil { return nil, fmt.Errorf("inspect image platform: %w", ctx.Err()) } - wrapped := command.WrapCommandError(nil, err) + wrapped := command.WrapCommandErrorWithEnvironment(nil, err, cmd.Environ()) if command.MatchErrorOutput(wrapped, platformInspectionUnsupported) { // Older engines and compatible runtimes have a single-platform local // image store. The caller still verifies that cached configuration. diff --git a/pkg/docker/stdio_transport_test.go b/pkg/docker/stdio_transport_test.go index cb9b69ce75..fee92e2532 100644 --- a/pkg/docker/stdio_transport_test.go +++ b/pkg/docker/stdio_transport_test.go @@ -23,11 +23,13 @@ import ( ) const ( - helperModeEnv = "DEVSY_TEST_DIAL_STDIO_MODE" - helperEchoMode = "echo" - helperHangMode = "hang" - shortShutdown = 150 * time.Millisecond - boundedInterval = 5 * time.Second + helperModeEnv = "DEVSY_TEST_DIAL_STDIO_MODE" + helperEchoMode = "echo" + helperHangMode = "hang" + shortShutdown = 150 * time.Millisecond + boundedInterval = 5 * time.Second + outputFixtureEnv = "DEVSY_DOCKER_EFFECTIVE_OUTPUT_TOKEN" + stderrFixtureEnv = "DEVSY_DOCKER_EFFECTIVE_STDERR_TOKEN" ) var helperModes = map[string]func(){ @@ -40,6 +42,11 @@ var helperModes = map[string]func(){ _, _ = fmt.Fprintln(os.Stderr, "auth failed for hunter2-token") os.Exit(4) }, + "fail-effective-environment": func() { + _, _ = fmt.Fprintln(os.Stdout, "public output", os.Getenv(outputFixtureEnv)) + _, _ = fmt.Fprintln(os.Stderr, "public diagnostic", os.Getenv(stderrFixtureEnv)) + os.Exit(23) + }, helperHangMode: func() { time.Sleep(time.Minute) }, "lingering": func() { _ = os.Stdout.Close() @@ -64,6 +71,32 @@ func TestMain(m *testing.M) { os.Exit(0) } +func TestDockerInspectMasksEffectiveEnvironment(t *testing.T) { + const privateOutput = "synthetic-docker-output-value" + const privateDiagnostic = "synthetic-docker-diagnostic-value" + executable, err := os.Executable() + require.NoError(t, err) + helper := &DockerHelper{ + DockerCommand: executable, + Environment: []string{ + helperModeEnv + "=fail-effective-environment", + outputFixtureEnv + "=" + privateOutput, + stderrFixtureEnv + "=" + privateDiagnostic, + }, + } + err = helper.Inspect(t.Context(), []string{"fixture"}, "image", &[]map[string]any{}) + require.Error(t, err) + var exit *exec.ExitError + require.ErrorAs(t, err, &exit) + require.Equal(t, 23, exit.ExitCode()) + for _, text := range []string{"public output", "public diagnostic"} { + require.ErrorContains(t, err, text) + } + for _, text := range []string{privateOutput, privateDiagnostic} { + require.NotContains(t, err.Error(), text) + } +} + // spawnChild starts a long-lived descendant immediately and reports its pid, // standing in for docker starting SSH before it exits or waits. func spawnChild() { diff --git a/pkg/ide/jetbrains/generic.go b/pkg/ide/jetbrains/generic.go index b1a4f749cc..ff86c47cfd 100644 --- a/pkg/ide/jetbrains/generic.go +++ b/pkg/ide/jetbrains/generic.go @@ -222,7 +222,7 @@ func (o *GenericJetBrainsServer) installPlugins( return fmt.Errorf( "failed to install JetBrains plugins %v: %w", plugins, - command.WrapCommandError(out, err), + command.WrapCommandErrorWithEnvironment(out, err, installPluginsCommand.Environ()), ) } From c48e02d130b58bfd1731c2bd3dc151283cdbeedf Mon Sep 17 00:00:00 2001 From: Samuel K Date: Sat, 21 Nov 2026 13:18:42 -0600 Subject: [PATCH 4/7] fix(e2e): mask multiline JSON live output and assertion failures --- e2e/framework/exec.go | 17 +-- e2e/framework/exec_capture_test.go | 60 ++++++++ e2e/framework/live_diagnostic.go | 144 ++++++++++++++++++ e2e/framework/live_diagnostic_test.go | 38 +++++ e2e/tests/ssh/agent_forward.go | 21 ++- e2e/tests/ssh/detached_validation.go | 8 +- e2e/tests/up/fixture_command.go | 6 + e2e/tests/up/fixture_command_test.go | 18 +++ e2e/tests/up/helper.go | 12 +- .../up/provider_kubernetes_dockerless_path.go | 20 ++- 10 files changed, 315 insertions(+), 29 deletions(-) create mode 100644 e2e/framework/live_diagnostic.go create mode 100644 e2e/framework/live_diagnostic_test.go diff --git a/e2e/framework/exec.go b/e2e/framework/exec.go index 2128b40d8d..37894c72f6 100644 --- a/e2e/framework/exec.go +++ b/e2e/framework/exec.go @@ -148,25 +148,10 @@ func captureCommandOutput( return tee, tee.Close } tee := livetee.New(io.Discard, live) - diagnostic := &liveDiagnosticCapture{tee: tee, stream: diagnosticredact.Stream(redactor)} + diagnostic := newLiveDiagnosticCapture(tee, redactor) return io.MultiWriter(capture, diagnostic), diagnostic.close } -type liveDiagnosticCapture struct { - tee *livetee.Tee - stream *redact.Stream -} - -func (w *liveDiagnosticCapture) Write(p []byte) (int, error) { - _, _ = io.WriteString(w.tee, w.stream.RedactChunk(string(p))) - return len(p), nil -} - -func (w *liveDiagnosticCapture) close() int64 { - _, _ = io.WriteString(w.tee, w.stream.Flush()) - return w.tee.Close() -} - func externalCommandDiagnostic(value string) string { redactor := redact.New(redact.Options{Environment: os.Environ()}) return diagnosticredact.Record(redactor, value) diff --git a/e2e/framework/exec_capture_test.go b/e2e/framework/exec_capture_test.go index e7d8b1d919..8de6defc37 100644 --- a/e2e/framework/exec_capture_test.go +++ b/e2e/framework/exec_capture_test.go @@ -32,6 +32,17 @@ func TestCaptureChild(t *testing.T) { _, _ = 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") @@ -87,6 +98,27 @@ func (s *captureSuite) TestCaptureMasksOnlyLiveDiagnosticCopies() { } } +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() @@ -142,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..e823e6da1f --- /dev/null +++ b/e2e/framework/live_diagnostic.go @@ -0,0 +1,144 @@ +package framework + +import ( + "encoding/json" + "io" + "strings" + + "github.com/devsy-org/devsy/internal/diagnosticredact" + "github.com/devsy-org/devsy/logging/redact" + "github.com/devsy-org/devsy/pkg/livetee" +) + +const maxLiveJSONRecord = 64 << 10 + +type liveDiagnosticCapture struct { + tee *livetee.Tee + redactor *redact.Redactor + stream *redact.Stream + pending string + record string + midLine bool +} + +func newLiveDiagnosticCapture(tee *livetee.Tee, redactor *redact.Redactor) *liveDiagnosticCapture { + return &liveDiagnosticCapture{ + tee: tee, + redactor: redactor, + stream: diagnosticredact.Stream(redactor), + } +} + +func (w *liveDiagnosticCapture) Write(p []byte) (int, error) { + w.pending += string(p) + w.drain(false) + return len(p), nil +} + +func (w *liveDiagnosticCapture) close() int64 { + w.drain(true) + if w.record != "" { + w.maskRecord() + } + _, _ = io.WriteString(w.tee, w.stream.Flush()) + return w.tee.Close() +} + +func (w *liveDiagnosticCapture) drain(final bool) { + for w.pending != "" && w.step(final) { + } +} + +func (w *liveDiagnosticCapture) step(final bool) bool { + newline := strings.IndexByte(w.pending, '\n') + switch { + case w.record != "": + return w.stepRecord(newline) + case w.midLine: + return w.stepMidLine(newline) + case newline < 0: + return w.stepPartialStart(final) + } + line := w.consume(newline + 1) + if opensJSONRecord(line) { + w.record = line + return true + } + w.passThrough(line) + return true +} + +func (w *liveDiagnosticCapture) stepRecord(newline int) bool { + if newline >= 0 { + w.extendRecord(w.consume(newline + 1)) + return true + } + if len(w.record)+len(w.pending) > maxLiveJSONRecord { + w.pending = "" + w.maskRecord() + } + return false +} + +func (w *liveDiagnosticCapture) stepMidLine(newline int) bool { + if newline < 0 { + w.passThrough(w.consume(len(w.pending))) + return false + } + w.passThrough(w.consume(newline + 1)) + w.midLine = false + return true +} + +func (w *liveDiagnosticCapture) stepPartialStart(final bool) bool { + if !final && mayOpenJSONRecord(w.pending) { + return false + } + w.midLine = true + w.passThrough(w.consume(len(w.pending))) + return true +} + +func (w *liveDiagnosticCapture) consume(n int) string { + text := w.pending[:n] + w.pending = w.pending[n:] + return text +} + +func (w *liveDiagnosticCapture) passThrough(text string) { + _, _ = io.WriteString(w.tee, w.stream.RedactChunk(text)) +} + +func (w *liveDiagnosticCapture) extendRecord(line string) { + w.record += line + body := strings.TrimRight(w.record, "\r\n") + switch { + case json.Valid([]byte(body)): + _, _ = io.WriteString(w.tee, w.stream.Flush()) + _, _ = io.WriteString(w.tee, diagnosticredact.Record(w.redactor, body)) + _, _ = io.WriteString(w.tee, w.record[len(body):]) + w.record = "" + case len(w.record) > maxLiveJSONRecord: + w.maskRecord() + } +} + +func (w *liveDiagnosticCapture) maskRecord() { + w.record = "" + _, _ = io.WriteString(w.tee, w.stream.Flush()) + _, _ = io.WriteString(w.tee, w.redactor.Mask()+"\n") +} + +func mayOpenJSONRecord(partial string) bool { + trimmed := strings.TrimLeft(partial, " \t\r") + return trimmed == "" || trimmed[0] == '{' || trimmed[0] == '[' +} + +func opensJSONRecord(line string) bool { + trimmed := strings.TrimSpace(line) + if trimmed == "" || (trimmed[0] != '{' && trimmed[0] != '[') || json.Valid([]byte(trimmed)) { + return false + } + rest := strings.TrimLeft(trimmed[1:], " \t") + return rest == "" || rest[0] == '"' +} diff --git a/e2e/framework/live_diagnostic_test.go b/e2e/framework/live_diagnostic_test.go new file mode 100644 index 0000000000..070690cc70 --- /dev/null +++ b/e2e/framework/live_diagnostic_test.go @@ -0,0 +1,38 @@ +package framework + +import ( + "bytes" + "testing" + + "github.com/devsy-org/devsy/logging/redact" + "github.com/devsy-org/devsy/pkg/livetee" + "github.com/stretchr/testify/assert" +) + +func TestLiveDiagnosticCaptureIsChunkIndependent(t *testing.T) { + const input = "[INFO] start\n{\n \"Cookie\": \"chunk-cookie-fixture\",\n" + + " \"note\": \"public detail\"\n}\nprompt: \n{\"Cookie\":\"chunk-compact-fixture\"}\ntail" + run := func(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() + } + whole := run(len(input)) + assert.NotContains(t, whole, "chunk-cookie-fixture") + assert.NotContains(t, whole, "chunk-compact-fixture") + assert.Contains(t, whole, "public detail") + assert.Contains(t, whole, "[INFO] start\n") + assert.Contains(t, whole, "prompt: \n") + assert.Contains(t, whole, "tail") + for _, size := range []int{1, 2, 3, 7, 16} { + assert.Equal(t, whole, run(size), "chunk size %d", size) + } +} diff --git a/e2e/tests/ssh/agent_forward.go b/e2e/tests/ssh/agent_forward.go index f8727f2b80..dcb2bcc442 100644 --- a/e2e/tests/ssh/agent_forward.go +++ b/e2e/tests/ssh/agent_forward.go @@ -123,7 +123,12 @@ var _ = ginkgo.Describe( 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. @@ -144,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, + ) }, ) @@ -197,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/detached_validation.go b/e2e/tests/ssh/detached_validation.go index 5f68e176ce..dd2dc27890 100644 --- a/e2e/tests/ssh/detached_validation.go +++ b/e2e/tests/ssh/detached_validation.go @@ -44,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) @@ -337,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/up/fixture_command.go b/e2e/tests/up/fixture_command.go index e711c62424..6f0414fed8 100644 --- a/e2e/tests/up/fixture_command.go +++ b/e2e/tests/up/fixture_command.go @@ -67,6 +67,12 @@ func fixtureDiagnostic(env []string, value string) string { 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...)...) diff --git a/e2e/tests/up/fixture_command_test.go b/e2e/tests/up/fixture_command_test.go index 14bb4a5491..e82d85c2a6 100644 --- a/e2e/tests/up/fixture_command_test.go +++ b/e2e/tests/up/fixture_command_test.go @@ -5,6 +5,7 @@ import ( "os/exec" "path/filepath" "runtime" + "strings" "testing" "time" @@ -34,6 +35,23 @@ func TestFixtureCommandMasksFailureWithoutChangingCapturedOutput(t *testing.T) { 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") diff --git a/e2e/tests/up/helper.go b/e2e/tests/up/helper.go index 6d7def713c..120fa89f37 100644 --- a/e2e/tests/up/helper.go +++ b/e2e/tests/up/helper.go @@ -297,7 +297,7 @@ 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")) + expectPresent(out) if tc.sessionCheck != "" { fileCheck := `if [ -e /run/devsy/secrets-env/` + tc.sessionFile + ` ]; then ` + `stat -c '%U:%a' /run/devsy/secrets-env/` + tc.sessionFile + @@ -307,7 +307,7 @@ func (dtc *dockerTestContext) verifyContextAttachment( gomega.Expect(strings.TrimSpace(out)).To(gomega.Equal("vscode:600")) out, err = dtc.f.DevsySSH(ctx, tempDir, tc.sessionCheck) framework.ExpectNoError(err) - gomega.Expect(strings.TrimSpace(out)).To(gomega.Equal("present")) + expectPresent(out) } _, err = dtc.f.ExecCommandOutput(ctx, []string{tc.command, "detach", tc.name}) @@ -326,3 +326,11 @@ func (dtc *dockerTestContext) verifyContextAttachment( gomega.Expect(strings.TrimSpace(out)).To(gomega.Equal("absent")) } } + +func expectPresent(out string) { + expectFixtureOutput( + strings.TrimSpace(out) == "present", + out, + "context binding supplies the secret file", + ) +} diff --git a/e2e/tests/up/provider_kubernetes_dockerless_path.go b/e2e/tests/up/provider_kubernetes_dockerless_path.go index c6f38d6e41..bf7d35777b 100644 --- a/e2e/tests/up/provider_kubernetes_dockerless_path.go +++ b/e2e/tests/up/provider_kubernetes_dockerless_path.go @@ -135,8 +135,12 @@ func (fixture *dockerlessPathFixture) up( "%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})/`, @@ -148,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", + ) } } From 2de7106fd8b50cb19601a815a4e5dd64d45a230d Mon Sep 17 00:00:00 2001 From: Samuel K Date: Sat, 21 Nov 2026 13:27:32 -0600 Subject: [PATCH 5/7] test(e2e): reject raw output assertions in masked fixtures --- e2e/tests/up/assertion_output_test.go | 87 +++++++++++++++++++ e2e/tests/up/helper.go | 19 ++-- .../up/provider_kubernetes_dockerless_path.go | 7 +- 3 files changed, 98 insertions(+), 15 deletions(-) create mode 100644 e2e/tests/up/assertion_output_test.go 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/helper.go b/e2e/tests/up/helper.go index 120fa89f37..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 ( @@ -297,17 +296,17 @@ func (dtc *dockerTestContext) verifyContextAttachment( `if [ "$(cat `+tc.checkFile+`)" = expected-value ]; then printf present; else printf absent; fi`, ) framework.ExpectNoError(err) - expectPresent(out) + 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) - expectPresent(out) + expectTrimmed(out, "present") } _, err = dtc.f.ExecCommandOutput(ctx, []string{tc.command, "detach", tc.name}) @@ -319,18 +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 expectPresent(out string) { - expectFixtureOutput( - strings.TrimSpace(out) == "present", - out, - "context binding supplies the secret file", - ) +func expectTrimmed(out, want string) { + expectFixtureOutput(strings.TrimSpace(out) == want, out, "expected output "+want) } diff --git a/e2e/tests/up/provider_kubernetes_dockerless_path.go b/e2e/tests/up/provider_kubernetes_dockerless_path.go index bf7d35777b..3158d96ba7 100644 --- a/e2e/tests/up/provider_kubernetes_dockerless_path.go +++ b/e2e/tests/up/provider_kubernetes_dockerless_path.go @@ -212,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. From 6ddeddf179e358f2b556d1b99120cab478101c7a Mon Sep 17 00:00:00 2001 From: Samuel K Date: Sat, 21 Nov 2026 14:12:48 -0600 Subject: [PATCH 6/7] fix(e2e): mask diagnostic records with a JSON-aware scanner --- e2e/framework/exec.go | 2 +- e2e/framework/live_diagnostic.go | 128 ++----------------------- e2e/framework/live_diagnostic_test.go | 108 ++++++++++++++++----- e2e/framework/record_masker.go | 131 ++++++++++++++++++++++++++ 4 files changed, 224 insertions(+), 145 deletions(-) create mode 100644 e2e/framework/record_masker.go diff --git a/e2e/framework/exec.go b/e2e/framework/exec.go index 37894c72f6..15b5b6e429 100644 --- a/e2e/framework/exec.go +++ b/e2e/framework/exec.go @@ -154,7 +154,7 @@ func captureCommandOutput( func externalCommandDiagnostic(value string) string { redactor := redact.New(redact.Options{Environment: os.Environ()}) - return diagnosticredact.Record(redactor, value) + return diagnosticredact.Record(redactor, maskRecordText(redactor, value)) } // ExecCommandJSON runs the devsy test binary and decodes its stdout as a JSON object. diff --git a/e2e/framework/live_diagnostic.go b/e2e/framework/live_diagnostic.go index e823e6da1f..79f0eaff3f 100644 --- a/e2e/framework/live_diagnostic.go +++ b/e2e/framework/live_diagnostic.go @@ -1,144 +1,32 @@ package framework import ( - "encoding/json" "io" - "strings" - "github.com/devsy-org/devsy/internal/diagnosticredact" "github.com/devsy-org/devsy/logging/redact" "github.com/devsy-org/devsy/pkg/livetee" ) -const maxLiveJSONRecord = 64 << 10 - type liveDiagnosticCapture struct { - tee *livetee.Tee - redactor *redact.Redactor - stream *redact.Stream - pending string - record string - midLine bool + tee *livetee.Tee + masker *recordMasker } func newLiveDiagnosticCapture(tee *livetee.Tee, redactor *redact.Redactor) *liveDiagnosticCapture { return &liveDiagnosticCapture{ - tee: tee, - redactor: redactor, - stream: diagnosticredact.Stream(redactor), + tee: tee, + masker: newRecordMasker(redactor, func(text string) { + _, _ = io.WriteString(tee, text) + }), } } func (w *liveDiagnosticCapture) Write(p []byte) (int, error) { - w.pending += string(p) - w.drain(false) + w.masker.write(string(p)) return len(p), nil } func (w *liveDiagnosticCapture) close() int64 { - w.drain(true) - if w.record != "" { - w.maskRecord() - } - _, _ = io.WriteString(w.tee, w.stream.Flush()) + w.masker.finish() return w.tee.Close() } - -func (w *liveDiagnosticCapture) drain(final bool) { - for w.pending != "" && w.step(final) { - } -} - -func (w *liveDiagnosticCapture) step(final bool) bool { - newline := strings.IndexByte(w.pending, '\n') - switch { - case w.record != "": - return w.stepRecord(newline) - case w.midLine: - return w.stepMidLine(newline) - case newline < 0: - return w.stepPartialStart(final) - } - line := w.consume(newline + 1) - if opensJSONRecord(line) { - w.record = line - return true - } - w.passThrough(line) - return true -} - -func (w *liveDiagnosticCapture) stepRecord(newline int) bool { - if newline >= 0 { - w.extendRecord(w.consume(newline + 1)) - return true - } - if len(w.record)+len(w.pending) > maxLiveJSONRecord { - w.pending = "" - w.maskRecord() - } - return false -} - -func (w *liveDiagnosticCapture) stepMidLine(newline int) bool { - if newline < 0 { - w.passThrough(w.consume(len(w.pending))) - return false - } - w.passThrough(w.consume(newline + 1)) - w.midLine = false - return true -} - -func (w *liveDiagnosticCapture) stepPartialStart(final bool) bool { - if !final && mayOpenJSONRecord(w.pending) { - return false - } - w.midLine = true - w.passThrough(w.consume(len(w.pending))) - return true -} - -func (w *liveDiagnosticCapture) consume(n int) string { - text := w.pending[:n] - w.pending = w.pending[n:] - return text -} - -func (w *liveDiagnosticCapture) passThrough(text string) { - _, _ = io.WriteString(w.tee, w.stream.RedactChunk(text)) -} - -func (w *liveDiagnosticCapture) extendRecord(line string) { - w.record += line - body := strings.TrimRight(w.record, "\r\n") - switch { - case json.Valid([]byte(body)): - _, _ = io.WriteString(w.tee, w.stream.Flush()) - _, _ = io.WriteString(w.tee, diagnosticredact.Record(w.redactor, body)) - _, _ = io.WriteString(w.tee, w.record[len(body):]) - w.record = "" - case len(w.record) > maxLiveJSONRecord: - w.maskRecord() - } -} - -func (w *liveDiagnosticCapture) maskRecord() { - w.record = "" - _, _ = io.WriteString(w.tee, w.stream.Flush()) - _, _ = io.WriteString(w.tee, w.redactor.Mask()+"\n") -} - -func mayOpenJSONRecord(partial string) bool { - trimmed := strings.TrimLeft(partial, " \t\r") - return trimmed == "" || trimmed[0] == '{' || trimmed[0] == '[' -} - -func opensJSONRecord(line string) bool { - trimmed := strings.TrimSpace(line) - if trimmed == "" || (trimmed[0] != '{' && trimmed[0] != '[') || json.Valid([]byte(trimmed)) { - return false - } - rest := strings.TrimLeft(trimmed[1:], " \t") - return rest == "" || rest[0] == '"' -} diff --git a/e2e/framework/live_diagnostic_test.go b/e2e/framework/live_diagnostic_test.go index 070690cc70..6b05c63d01 100644 --- a/e2e/framework/live_diagnostic_test.go +++ b/e2e/framework/live_diagnostic_test.go @@ -9,30 +9,90 @@ import ( "github.com/stretchr/testify/assert" ) -func TestLiveDiagnosticCaptureIsChunkIndependent(t *testing.T) { - const input = "[INFO] start\n{\n \"Cookie\": \"chunk-cookie-fixture\",\n" + - " \"note\": \"public detail\"\n}\nprompt: \n{\"Cookie\":\"chunk-compact-fixture\"}\ntail" - run := func(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 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])) } - whole := run(len(input)) - assert.NotContains(t, whole, "chunk-cookie-fixture") - assert.NotContains(t, whole, "chunk-compact-fixture") - assert.Contains(t, whole, "public detail") - assert.Contains(t, whole, "[INFO] start\n") - assert.Contains(t, whole, "prompt: \n") - assert.Contains(t, whole, "tail") - for _, size := range []int{1, 2, 3, 7, 16} { - assert.Equal(t, whole, run(size), "chunk size %d", size) + 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") } diff --git a/e2e/framework/record_masker.go b/e2e/framework/record_masker.go new file mode 100644 index 0000000000..0b9c2a30c9 --- /dev/null +++ b/e2e/framework/record_masker.go @@ -0,0 +1,131 @@ +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.emit(m.stream.Flush()) + m.emit(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() +} From 8e8f35eac858caff40d846cc9740971e9b723bad Mon Sep 17 00:00:00 2001 From: Samuel K Date: Sat, 21 Nov 2026 16:36:46 -0600 Subject: [PATCH 7/7] fix(e2e): apply folded header masking to scanned JSON records --- e2e/framework/live_diagnostic_test.go | 22 ++++++++++++++++++++++ e2e/framework/record_masker.go | 3 +-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/e2e/framework/live_diagnostic_test.go b/e2e/framework/live_diagnostic_test.go index 6b05c63d01..b27d17f29f 100644 --- a/e2e/framework/live_diagnostic_test.go +++ b/e2e/framework/live_diagnostic_test.go @@ -4,6 +4,7 @@ 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" @@ -96,3 +97,24 @@ func TestExternalCommandDiagnosticMasksEmbeddedJSONRecords(t *testing.T) { 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 index 0b9c2a30c9..5d789a9634 100644 --- a/e2e/framework/record_masker.go +++ b/e2e/framework/record_masker.go @@ -93,8 +93,7 @@ func (m *recordMasker) maskRecord(start string, final bool) bool { case err == nil: lead := m.pending[:len(m.pending)-len(start)] end := int(decoder.InputOffset()) - m.emit(m.stream.Flush()) - m.emit(lead + diagnosticredact.Record(m.redactor, start[:end])) + m.plain(lead + diagnosticredact.Record(m.redactor, start[:end])) m.pending = start[end:] return true case errors.Is(err, io.ErrUnexpectedEOF):