diff --git a/cmd/machine/ssh.go b/cmd/machine/ssh.go index acaf349621..9025eb90e4 100644 --- a/cmd/machine/ssh.go +++ b/cmd/machine/ssh.go @@ -158,10 +158,11 @@ func (cmd *SSHCmd) Run(ctx context.Context, args []string) error { return devagent.InjectAgent(ctx, &devagent.InjectOptions{ Exec: func(ctx context.Context, command string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error { return machineClient.Command(ctx, client.CommandOptions{ - Command: command, - Stdin: stdin, - Stdout: stdout, - Stderr: stderr, + Command: command, + Stdin: stdin, + Stdout: stdout, + Stderr: stderr, + RawStdout: true, }) }, IsLocal: machineClient.AgentLocal(), diff --git a/cmd/workspace/logs.go b/cmd/workspace/logs.go index 2aa2f1aca0..dc664277f3 100644 --- a/cmd/workspace/logs.go +++ b/cmd/workspace/logs.go @@ -127,10 +127,11 @@ func injectLogsAgent(ctx context.Context, params injectLogsAgentParams) error { return agent.InjectAgent(ctx, &agent.InjectOptions{ Exec: func(ctx context.Context, command string, stdinR io.Reader, stdoutW io.Writer, stderrW io.Writer) error { return params.client.Command(ctx, clientpkg.CommandOptions{ - Command: command, - Stdin: stdinR, - Stdout: stdoutW, - Stderr: stderrW, + Command: command, + Stdin: stdinR, + Stdout: stdoutW, + Stderr: stderrW, + RawStdout: true, }) }, IsLocal: params.client.AgentLocal(), diff --git a/cmd/workspace/up/agent.go b/cmd/workspace/up/agent.go index 671a07cd53..b3bd74b29f 100644 --- a/cmd/workspace/up/agent.go +++ b/cmd/workspace/up/agent.go @@ -255,10 +255,11 @@ func newAgentInjectFunc( return agent.InjectAgent(cancelCtx, &agent.InjectOptions{ Exec: func(ctx context.Context, command string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error { return client.Command(ctx, client2.CommandOptions{ - Command: command, - Stdin: stdin, - Stdout: stdout, - Stderr: stderr, + Command: command, + Stdin: stdin, + Stdout: stdout, + Stderr: stderr, + RawStdout: true, }) }, IsLocal: client.AgentLocal(), diff --git a/pkg/agent/delivery/factory.go b/pkg/agent/delivery/factory.go index 91a6d69519..0c4c48fc7c 100644 --- a/pkg/agent/delivery/factory.go +++ b/pkg/agent/delivery/factory.go @@ -141,6 +141,7 @@ func CommandFunc( Stdin: stdin, Stdout: stdout, Stderr: stderr, + RawStdout: true, }) } } diff --git a/pkg/agent/delivery/factory_test.go b/pkg/agent/delivery/factory_test.go index e72181df1c..82693085ad 100644 --- a/pkg/agent/delivery/factory_test.go +++ b/pkg/agent/delivery/factory_test.go @@ -14,6 +14,21 @@ import ( const testKubernetesInstallPath = "/home/vscode/.local/bin/devsy" +func TestCommandFuncPreservesProtocolStdout(t *testing.T) { + var got *driver.CommandParams + execFn := CommandFunc(func(_ context.Context, params *driver.CommandParams) error { + got = params + return nil + }, "workspace-id") + + err := execFn(context.Background(), "test-command", nil, io.Discard, io.Discard) + require.NoError(t, err) + require.NotNil(t, got) + assert.Equal(t, "workspace-id", got.WorkspaceID) + assert.Equal(t, "test-command", got.Command) + assert.True(t, got.RawStdout) +} + func TestNewAgentDelivery_LocalDocker(t *testing.T) { opts := FactoryOptions{ WorkspaceConfig: &provider.AgentWorkspaceInfo{ diff --git a/pkg/client/client.go b/pkg/client/client.go index e9c52681be..326d876243 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -169,10 +169,11 @@ type StatusOptions struct { } type CommandOptions struct { - Command string - Stdin io.Reader - Stdout io.Writer - Stderr io.Writer + Command string + Stdin io.Reader + Stdout io.Writer + Stderr io.Writer + RawStdout bool } type CommandTransportOptions struct { diff --git a/pkg/client/clientimplementation/machine_client.go b/pkg/client/clientimplementation/machine_client.go index 5d28f2be6a..58612e5f60 100644 --- a/pkg/client/clientimplementation/machine_client.go +++ b/pkg/client/clientimplementation/machine_client.go @@ -61,6 +61,7 @@ type execConfig struct { stderr io.Writer extraEnv map[string]string stdin io.Reader + rawStdout bool withProgress bool startMsg string doneMsg string @@ -87,6 +88,7 @@ func (e *machineExecutor) execute(ctx context.Context, cfg execConfig) error { Stderr: cfg.stderr, ExtraEnv: cfg.extraEnv, Stdin: cfg.stdin, + RawStdout: cfg.rawStdout, }) if err != nil { return err @@ -192,11 +194,12 @@ func (s *machineClient) Stop(ctx context.Context, options client.StopOptions) er func (s *machineClient) Command(ctx context.Context, commandOptions client.CommandOptions) error { return s.executor.execute(ctx, execConfig{ - name: "command", - command: s.config.Exec.Command, - stdout: commandOptions.Stdout, - stderr: commandOptions.Stderr, - stdin: commandOptions.Stdin, + name: "command", + command: s.config.Exec.Command, + stdout: commandOptions.Stdout, + stderr: commandOptions.Stderr, + stdin: commandOptions.Stdin, + rawStdout: commandOptions.RawStdout, extraEnv: map[string]string{ provider.CommandEnv: commandOptions.Command, }, @@ -212,6 +215,7 @@ func (s *machineClient) OpenCommandTransport( func(ctx context.Context, stdin io.Reader, stdout io.Writer) error { return s.Command(ctx, client.CommandOptions{ Command: opt.Command, Stdin: stdin, Stdout: stdout, Stderr: opt.Stderr, + RawStdout: true, }) }, transport.CallbackConnOptions{ diff --git a/pkg/client/clientimplementation/proxy_client.go b/pkg/client/clientimplementation/proxy_client.go index 4da49e5f5d..7eda800935 100644 --- a/pkg/client/clientimplementation/proxy_client.go +++ b/pkg/client/clientimplementation/proxy_client.go @@ -64,11 +64,12 @@ type proxyExecutor struct { } type execParams struct { - command types.StrArray - extraEnv map[string]string - stdin io.Reader - stdout io.Writer - stderr io.Writer + command types.StrArray + extraEnv map[string]string + stdin io.Reader + stdout io.Writer + stderr io.Writer + rawStdout bool } // execute runs a proxy command with common settings. @@ -83,6 +84,7 @@ func (e *proxyExecutor) execute(ctx context.Context, params execParams) error { Stdin: params.stdin, Stdout: params.stdout, Stderr: params.stderr, + RawStdout: params.rawStdout, }) } @@ -241,10 +243,11 @@ func (s *proxyClient) Create( func (s *proxyClient) Ssh(ctx context.Context, opt client.SshOptions) error { return s.executor.executeWithJSONLog(ctx, execParams{ - command: s.config.Exec.Proxy.Ssh, - extraEnv: EncodeOptions(opt, config.EnvFlagsSSH), - stdin: opt.Stdin, - stdout: opt.Stdout, + command: s.config.Exec.Proxy.Ssh, + extraEnv: EncodeOptions(opt, config.EnvFlagsSSH), + stdin: opt.Stdin, + stdout: opt.Stdout, + rawStdout: true, }) } @@ -256,10 +259,11 @@ func (s *proxyClient) OpenSSHTransport( ctx, func(ctx context.Context, stdin io.Reader, stdout io.Writer) error { return s.executor.executeWithJSONLog(ctx, execParams{ - command: s.config.Exec.Proxy.Ssh, - extraEnv: EncodeOptions(client.SshOptions{User: opt.User}, config.EnvFlagsSSH), - stdin: stdin, - stdout: stdout, + command: s.config.Exec.Proxy.Ssh, + extraEnv: EncodeOptions(client.SshOptions{User: opt.User}, config.EnvFlagsSSH), + stdin: stdin, + stdout: stdout, + rawStdout: true, }) }, transport.CallbackConnOptions{ @@ -291,10 +295,11 @@ func (s *proxyClient) Up(ctx context.Context, opt client.UpOptions) error { } return s.executor.executeWithJSONLog(ctx, execParams{ - command: s.config.Exec.Proxy.Up, - extraEnv: opts, - stdin: opt.Stdin, - stdout: opt.Stdout, + command: s.config.Exec.Proxy.Up, + extraEnv: opts, + stdin: opt.Stdin, + stdout: opt.Stdout, + rawStdout: true, }) } diff --git a/pkg/client/clientimplementation/workspace_client.go b/pkg/client/clientimplementation/workspace_client.go index 245fcad443..17a9d47045 100644 --- a/pkg/client/clientimplementation/workspace_client.go +++ b/pkg/client/clientimplementation/workspace_client.go @@ -280,11 +280,12 @@ func (s *workspaceClient) Command(ctx context.Context, opt client.CommandOptions } return RunCommand(ctx, RunCommandOptions{ - Command: s.providerConfig.Exec.Command, - Environ: environ, - Stdin: opt.Stdin, - Stdout: opt.Stdout, - Stderr: opt.Stderr, + Command: s.providerConfig.Exec.Command, + Environ: environ, + Stdin: opt.Stdin, + Stdout: opt.Stdout, + Stderr: opt.Stderr, + RawStdout: opt.RawStdout, }) } @@ -303,6 +304,7 @@ func (s *workspaceClient) OpenCommandTransport( Command: s.providerConfig.Exec.Command, Environ: environ, Stdin: stdin, Stdout: stdout, Stderr: opt.Stderr, + RawStdout: true, }) }, transport.CallbackConnOptions{ @@ -835,6 +837,7 @@ type WorkspaceCommandConfig struct { Stdin io.Reader Stdout io.Writer Stderr io.Writer + RawStdout bool Command types.StrArray Workspace *provider.Workspace Machine *provider.Machine @@ -855,23 +858,25 @@ func RunCommandWithBinaries(ctx context.Context, cfg WorkspaceCommandConfig) err } return RunCommand(ctx, RunCommandOptions{ - Command: cfg.Command, - Environ: environ, - Stdin: cfg.Stdin, - Stdout: cfg.Stdout, - Stderr: cfg.Stderr, + Command: cfg.Command, + Environ: environ, + Stdin: cfg.Stdin, + Stdout: cfg.Stdout, + Stderr: cfg.Stderr, + RawStdout: cfg.RawStdout, }) } type RunCommandOptions struct { - Command types.StrArray - Environ []string - Stdin io.Reader - Stdout io.Writer - Stderr io.Writer + Command types.StrArray + Environ []string + Stdin io.Reader + Stdout io.Writer + Stderr io.Writer + RawStdout bool } -func RunCommand(ctx context.Context, opts RunCommandOptions) error { +func RunCommand(ctx context.Context, opts RunCommandOptions) (retErr error) { if len(opts.Command) == 0 { return nil } @@ -881,11 +886,21 @@ func RunCommand(ctx context.Context, opts RunCommandOptions) error { } redactor := secrets.NewEnvironmentRedactor(opts.Environ) - stdout := &subprocess.StreamingRedactingWriter{Next: opts.Stdout, Redactor: redactor} + stdout := opts.Stdout + var redactingStdout *subprocess.StreamingRedactingWriter + if !opts.RawStdout { + redactingStdout = &subprocess.StreamingRedactingWriter{ + Next: opts.Stdout, + Redactor: redactor, + } + stdout = redactingStdout + } stderr := &subprocess.StreamingRedactingWriter{Next: opts.Stderr, Redactor: redactor} defer func() { - _ = stdout.Flush() - _ = stderr.Flush() + flushErr := flushCommandOutput(redactingStdout, stderr) + if retErr == nil { + retErr = flushErr + } }() if len(opts.Command) == 1 { @@ -920,6 +935,20 @@ func RunCommand(ctx context.Context, opts RunCommandOptions) error { return nil } +func flushCommandOutput(stdout, stderr *subprocess.StreamingRedactingWriter) error { + var stdoutErr error + if stdout != nil { + if err := stdout.Flush(); err != nil { + stdoutErr = fmt.Errorf("flush provider stdout: %w", err) + } + } + var stderrErr error + if err := stderr.Flush(); err != nil { + stderrErr = fmt.Errorf("flush provider stderr: %w", err) + } + return errors.Join(stdoutErr, stderrErr) +} + // providerAction extracts the provider action from the environment variables. func providerAction(environ []string) string { for _, kv := range environ { @@ -1202,10 +1231,11 @@ func runAgentInjection(ctx context.Context, cfg workspaceInjectionConfig) chan e errChan <- agent.InjectAgent(ctx, &agent.InjectOptions{ Exec: func(ctx context.Context, command string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error { return cfg.workspaceClient.Command(ctx, client.CommandOptions{ - Command: command, - Stdin: stdin, - Stdout: stdout, - Stderr: stderr, + Command: command, + Stdin: stdin, + Stdout: stdout, + Stderr: stderr, + RawStdout: true, }) }, IsLocal: cfg.workspaceClient.AgentLocal(), diff --git a/pkg/client/clientimplementation/workspace_client_test.go b/pkg/client/clientimplementation/workspace_client_test.go index 80bc6abd26..fd9f3ac205 100644 --- a/pkg/client/clientimplementation/workspace_client_test.go +++ b/pkg/client/clientimplementation/workspace_client_test.go @@ -4,13 +4,17 @@ import ( "bytes" "context" "errors" + "io" "os" "path/filepath" + "runtime" "strings" "testing" "time" "github.com/devsy-org/devsy/pkg/client" + "github.com/devsy-org/devsy/pkg/config" + "github.com/devsy-org/devsy/pkg/provider" "github.com/devsy-org/devsy/pkg/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -133,6 +137,166 @@ func TestRunCommand(t *testing.T) { }) } +func TestRunCommand_RawStdoutDoesNotHoldProtocolBytes(t *testing.T) { + requireProtocolByteRoundTrip( + t, + func(ctx context.Context, stdin io.Reader, stdout io.Writer) error { + return RunCommand(ctx, RunCommandOptions{ + Command: types.StrArray{"/bin/sh", "-c", "printf h; read ack"}, + Environ: []string{"DEVSY_RUN_COMMAND_SECRET=hidden"}, + Stdin: stdin, + Stdout: stdout, + RawStdout: true, + }) + }, + ) +} + +func TestRunCommand_RawStdoutStillRedactsStderr(t *testing.T) { + requirePOSIXShell(t) + const secret = "protocol-test-secret-846307" //nolint:gosec // test credential fixture + var stdout, stderr bytes.Buffer + err := RunCommand(context.Background(), RunCommandOptions{ + Command: types.StrArray{ + "/bin/sh", + "-c", + `printf '%s' "$DEVSY_RUN_COMMAND_SECRET"; printf '%s' "$DEVSY_RUN_COMMAND_SECRET" >&2`, + }, + Environ: []string{"DEVSY_RUN_COMMAND_SECRET=" + secret}, + Stdout: &stdout, + Stderr: &stderr, + RawStdout: true, + }) + require.NoError(t, err) + assert.Equal(t, secret, stdout.String()) + assert.NotContains(t, stderr.String(), secret) + assert.Contains(t, stderr.String(), "***") +} + +type failingWriter struct{ err error } + +func (w failingWriter) Write([]byte) (int, error) { return 0, w.err } + +func TestRunCommand_ReportsFlushFailureWithoutReplacingCommandError(t *testing.T) { + requirePOSIXShell(t) + const secret = "hidden-flush-secret-846307" //nolint:gosec // test credential fixture + flushErr := errors.New("flush failed") + tests := []struct { + name string + command string + stdout io.Writer + stderr io.Writer + commandErr bool + }{ + { + name: "stdout flush", + command: "printf h", + stdout: failingWriter{flushErr}, + }, + { + name: "stderr flush", + command: "printf h >&2", + stderr: failingWriter{flushErr}, + }, + { + name: "command failure takes precedence", + command: "printf h; exit 7", + stdout: failingWriter{flushErr}, + commandErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := RunCommand(context.Background(), RunCommandOptions{ + Command: types.StrArray{"/bin/sh", "-c", tt.command}, + Environ: []string{"DEVSY_RUN_COMMAND_SECRET=" + secret}, + Stdout: tt.stdout, + Stderr: tt.stderr, + }) + if tt.commandErr { + require.ErrorContains(t, err, "exit status 7") + assert.NotErrorIs(t, err, flushErr) + return + } + require.ErrorIs(t, err, flushErr) + }) + } +} + +func TestProxyUp_PreservesProtocolStdout(t *testing.T) { + const contextName = "default" + proxy := &proxyClient{ + devsyConfig: &config.Config{ + DefaultContext: contextName, + Contexts: map[string]*config.ContextConfig{ + contextName: {}, + }, + }, + config: &provider.ProviderConfig{ + Name: "test", + Exec: provider.ProviderCommands{ + Proxy: &provider.ProxyCommands{ + Up: types.StrArray{"/bin/sh", "-c", "printf h; read ack"}, + }, + }, + }, + workspace: &provider.Workspace{ID: "test", Context: contextName}, + } + proxy.executor = &proxyExecutor{client: proxy} + requireProtocolByteRoundTrip( + t, + func(ctx context.Context, stdin io.Reader, stdout io.Writer) error { + return proxy.Up(ctx, client.UpOptions{Stdin: stdin, Stdout: stdout}) + }, + ) +} + +func requireProtocolByteRoundTrip( + t *testing.T, + run func(context.Context, io.Reader, io.Writer) error, +) { + t.Helper() + requirePOSIXShell(t) + + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + stdinReader, stdinWriter := io.Pipe() + stdoutReader, stdoutWriter := io.Pipe() + defer func() { _ = stdinReader.Close() }() + defer func() { _ = stdinWriter.Close() }() + defer func() { _ = stdoutReader.Close() }() + defer func() { _ = stdoutWriter.Close() }() + stopClosingPipes := context.AfterFunc(ctx, func() { + _ = stdinReader.Close() + _ = stdinWriter.Close() + _ = stdoutReader.Close() + _ = stdoutWriter.Close() + }) + defer stopClosingPipes() + + done := make(chan error, 1) + go func() { + defer func() { _ = stdoutWriter.Close() }() + done <- run(ctx, stdinReader, stdoutWriter) + }() + + got := make([]byte, 1) + _, err := io.ReadFull(stdoutReader, got) + require.NoError(t, err) + require.Equal(t, []byte("h"), got) + _, err = io.WriteString(stdinWriter, "ack\n") + require.NoError(t, err) + require.NoError(t, stdinWriter.Close()) + require.NoError(t, <-done) +} + +func requirePOSIXShell(t *testing.T) { + t.Helper() + if runtime.GOOS == "windows" { + t.Skip("requires a POSIX shell") + } +} + func TestLogBusy(t *testing.T) { t.Run("within threshold keeps timestamp", func(t *testing.T) { start := time.Now() diff --git a/pkg/devcontainer/setup.go b/pkg/devcontainer/setup.go index b4ae71a3f9..2b14bb4641 100644 --- a/pkg/devcontainer/setup.go +++ b/pkg/devcontainer/setup.go @@ -260,6 +260,7 @@ func (r *runner) legacyInject(ctx context.Context, timeout time.Duration) error Stdin: stdin, Stdout: stdout, Stderr: stderr, + RawStdout: true, }) }, IsLocal: false, @@ -543,6 +544,7 @@ func (r *runner) executeSetup( Stdin: sshTunnelStdinReader, Stdout: sshTunnelStdoutWriter, Stderr: writer, + RawStdout: true, }) } diff --git a/pkg/driver/custom/custom.go b/pkg/driver/custom/custom.go index 41cb08e3db..eb6f530829 100644 --- a/pkg/driver/custom/custom.go +++ b/pkg/driver/custom/custom.go @@ -73,6 +73,7 @@ func (c *customDriver) CommandDevContainer( stdin: params.Stdin, stdout: params.Stdout, stderr: params.Stderr, + rawStdout: params.RawStdout, extraEnv: []string{ "DEVCONTAINER_USER=" + params.User, "DEVCONTAINER_COMMAND=" + params.Command, @@ -244,6 +245,7 @@ type runCommandOptions struct { stdin io.Reader stdout io.Writer stderr io.Writer + rawStdout bool extraEnv []string } @@ -268,11 +270,12 @@ func (c *customDriver) runCommand( } return clientimplementation.RunCommand(ctx, clientimplementation.RunCommandOptions{ - Command: runOpts.command, - Environ: environ, - Stdin: runOpts.stdin, - Stdout: runOpts.stdout, - Stderr: runOpts.stderr, + Command: runOpts.command, + Environ: environ, + Stdin: runOpts.stdin, + Stdout: runOpts.stdout, + Stderr: runOpts.stderr, + RawStdout: runOpts.rawStdout, }) } diff --git a/pkg/driver/types.go b/pkg/driver/types.go index e6218ae3cc..605c64f066 100644 --- a/pkg/driver/types.go +++ b/pkg/driver/types.go @@ -97,6 +97,9 @@ type CommandParams struct { Stdin io.Reader Stdout io.Writer Stderr io.Writer + // RawStdout preserves protocol bytes; text commands keep stdout redacted. + // Protocol consumers must not log the unredacted stream. + RawStdout bool } // Streams bundles the standard IO streams for an exec. diff --git a/pkg/secrets/redact.go b/pkg/secrets/redact.go index e85978603f..1223030cf5 100644 --- a/pkg/secrets/redact.go +++ b/pkg/secrets/redact.go @@ -104,9 +104,8 @@ func (r *Redactor) Redact(s string) string { return authorizationPattern.ReplaceAllString(s, "$1***") } -// StreamingRedactor preserves a short suffix between writes so secrets split -// across subprocess or logger chunks are still masked before they are -// forwarded. Call Flush when the stream ends to release the final suffix. +// StreamingRedactor masks secrets split across text writes, buffering a short +// suffix until the next write or Flush. type StreamingRedactor struct { mu sync.Mutex base *Redactor diff --git a/pkg/ssh/config.go b/pkg/ssh/config.go index 9c354bea13..597cbeae6d 100644 --- a/pkg/ssh/config.go +++ b/pkg/ssh/config.go @@ -278,40 +278,50 @@ func buildTunnelConfigLines(params addHostParams) []string { build() } -// findInsertPosition finds where to insert new SSH config entry. -func findInsertPosition(config string) (int, []string, error) { - lineNumber := 0 - found := false - lines := []string{} - commentLines := 0 +func sshConfigKeyword(line string) string { + trimmed := strings.TrimSpace(line) + if trimmed == "" || strings.HasPrefix(trimmed, "#") { + return "" + } - scanner := bufio.NewScanner(strings.NewReader(config)) - for scanner.Scan() { - line := scanner.Text() + if separator := strings.IndexAny(trimmed, " \t="); separator >= 0 { + return trimmed[:separator] + } - if strings.HasPrefix(strings.TrimSpace(line), "Host") && !found { - found = true - lineNumber = max(lineNumber-commentLines, 0) - } + return trimmed +} - if strings.HasPrefix(strings.TrimSpace(line), "#") { - commentLines++ - } else { - commentLines = 0 - } +func isSSHSectionStart(line string) bool { + keyword := sshConfigKeyword(line) - if !found { - lineNumber++ - } + return strings.EqualFold(keyword, "host") || strings.EqualFold(keyword, "match") +} - lines = append(lines, line) +// findInsertPosition finds where to insert new SSH config entry. +func findInsertPosition(config string) (int, []string, error) { + lines := make([]string, 0) + scanner := bufio.NewScanner(strings.NewReader(config)) + for scanner.Scan() { + lines = append(lines, scanner.Text()) } - if err := scanner.Err(); err != nil { return 0, nil, err } - return lineNumber, lines, nil + position := len(lines) + for i, line := range lines { + if !isSSHSectionStart(line) { + continue + } + + position = i + for position > 0 && strings.HasPrefix(strings.TrimSpace(lines[position-1]), "#") { + position-- + } + break + } + + return position, lines, nil } // mergeSSHConfig inserts new lines into existing config. diff --git a/pkg/ssh/config_test.go b/pkg/ssh/config_test.go index a6ddcb5a61..c4230dab59 100644 --- a/pkg/ssh/config_test.go +++ b/pkg/ssh/config_test.go @@ -1,8 +1,12 @@ package ssh import ( + "os" + "path/filepath" + "strings" "testing" + "github.com/devsy-org/devsy/pkg/config" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" ) @@ -224,7 +228,7 @@ Host existingtesthost HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa ProxyCommand "/path/to/exec" workspace ssh --stdio --context testcontext --user testuser testworkspace User testuser -# Devsy End testhost +# Devsy End existingtesthost Host existinghost User existinguser`, @@ -258,7 +262,7 @@ Host existingtesthost HostKeyAlgorithms rsa-sha2-256,rsa-sha2-512,ssh-rsa ProxyCommand "/path/to/exec" workspace ssh --stdio --context testcontext --user testuser testworkspace User testuser -# Devsy End testhost +# Devsy End existingtesthost Host existinghost User existinguser`, @@ -409,3 +413,253 @@ func TestNormalizeSSHExecPathForOS(t *testing.T) { }) } } + +//nolint:goconst // repeated values document distinct lexical syntax cases. +func TestSSHConfigKeyword(t *testing.T) { + tests := []struct { + name string + line string + expected string + }{ + {name: "empty", line: "", expected: ""}, + {name: "comment", line: "# comment", expected: ""}, + {name: "indented comment", line: " # indented comment", expected: ""}, + {name: "host", line: "Host myserver", expected: "Host"}, + {name: "lowercase host", line: "host myserver", expected: "host"}, + {name: "uppercase host", line: "HOST myserver", expected: "HOST"}, + {name: "tab separator", line: "HoSt\tmyserver", expected: "HoSt"}, + {name: "equals separator", line: "Host=myserver", expected: "Host"}, + {name: "equals with spaces", line: "Host = myserver", expected: "Host"}, + {name: "hostname", line: "HostName example.com", expected: "HostName"}, + { + name: "host key algorithms", + line: "HostKeyAlgorithms ssh-ed25519", + expected: "HostKeyAlgorithms", + }, + {name: "match", line: `Match exec "true"`, expected: "Match"}, + {name: "lowercase match", line: "match host foo", expected: "match"}, + {name: "port", line: "Port 22", expected: "Port"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.expected, sshConfigKeyword(tt.line)) + }) + } +} + +func TestIsSSHSectionStart(t *testing.T) { + positives := []string{ + "Host myserver", + "host myserver", + "HOST myserver", + "HoSt\tmyserver", + "Host=myserver", + "Host = myserver", + `Match exec "true"`, + "match host foo", + "MATCH all", + } + for _, line := range positives { + t.Run("positive/"+line, func(t *testing.T) { + assert.True(t, isSSHSectionStart(line)) + }) + } + + negatives := []string{ + "HostName example.com", + "hostname example.com", + "HostKeyAlgorithms ssh-ed25519", + "Port 22", + "# Host commented", + "# Match commented", + "", + } + for _, line := range negatives { + t.Run("negative/"+line, func(t *testing.T) { + assert.False(t, isSSHSectionStart(line)) + }) + } +} + +//nolint:funlen // keep the insertion-position regression matrix together. +func TestFindInsertPosition(t *testing.T) { + tests := []struct { + name string + config string + expected int + }{ + { + name: "HostName is not a section", + config: `HostName global.example.com +Host actual-host + User example`, + expected: 1, + }, + { + name: "lowercase host", + config: `host * + User alice + +host server + Port 22`, + expected: 0, + }, + {name: "mixed-case section", config: "HoSt server\n User alice", expected: 0}, + {name: "tab separator", config: "Host\tserver", expected: 0}, + {name: "equals separator", config: "Host=server", expected: 0}, + { + name: "Match", + config: `IdentityFile ~/.ssh/id_ed25519 + +Match exec "true" + User conditional`, + expected: 2, + }, + { + name: "preceding comments stay attached", + config: `# Production server +# Do not remove +Host prod + User deploy`, + expected: 0, + }, + { + name: "managed block stays contiguous", + config: `# Devsy Start existing.devsy +Host existing.devsy + User existing +# Devsy End existing.devsy`, + expected: 0, + }, + { + name: "preamble stays before generated host", + config: `Include ~/.ssh/common.conf +IdentityFile ~/.ssh/id_ed25519 + +Host existing + User alice`, + expected: 3, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + position, lines, err := findInsertPosition(tt.config) + assert.NoError(t, err) + assert.Equal(t, tt.expected, position) + assert.Equal(t, strings.Split(tt.config, "\n"), lines) + }) + } +} + +const ( + testSSHUser = "devsy" + testSSHContext = "context" +) + +func TestAddHostSectionSectionSyntaxVariants(t *testing.T) { + tests := []struct { + name string + config string + }{ + {name: "lowercase host", config: "host *\n User alice"}, + {name: "uppercase host", config: "HOST *\n User alice"}, + {name: "mixed-case host", config: "HoSt *\n User alice"}, + {name: "tab separator", config: "Host\t*\n User alice"}, + {name: "equals separator", config: "Host=*\n User alice"}, + {name: "match first", config: "Match exec \"true\"\n User alice"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result, err := addHostSection(tt.config, testExecPath, addHostParams{ + host: "newhost", + user: testSSHUser, + context: testSSHContext, + workspace: "workspace", + }) + assert.NoError(t, err) + assert.True(t, strings.HasPrefix(result, "# Devsy Start newhost\nHost newhost")) + assert.Contains(t, result, tt.config) + }) + } +} + +func TestAddHostSectionPreservesExistingManagedBlock(t *testing.T) { + const existingBlock = `# Devsy Start existing.devsy +Host existing.devsy + User existing +# Devsy End existing.devsy` + + result, err := addHostSection( + "host *\n User alice\n\n"+existingBlock, + testExecPath, + addHostParams{ + host: "newhost", + user: testSSHUser, + context: testSSHContext, + workspace: "workspace", + }, + ) + + assert.NoError(t, err) + assert.Contains(t, result, existingBlock) + newStart := strings.Index(result, MarkerStartPrefix+"newhost") + existingStart := strings.Index(result, MarkerStartPrefix+"existing.devsy") + assert.NotEqual(t, -1, newStart) + assert.NotEqual(t, -1, existingStart) + assert.Less(t, newStart, existingStart) +} + +func TestConfigureSSHConfig(t *testing.T) { + tmpDir := t.TempDir() + configPath := filepath.Join(tmpDir, "config") + const existingBlock = `# Devsy Start existing.devsy +Host existing.devsy + User existing +# Devsy End existing.devsy` + initial := `host * + IdentityFile ~/.ssh/id_ed25519 + IdentityFile ~/.ssh/id_ed25519 + +` + existingBlock + ` + +Match exec "true" + User conditional` + assert.NoError(t, os.WriteFile(configPath, []byte(initial), 0o600)) + + err := ConfigureSSHConfig(SSHConfigParams{ + SSHConfigPath: configPath, + Context: testSSHContext, + Workspace: "new-workspace", + User: testSSHUser, + }) + assert.NoError(t, err) + + // #nosec G304 -- configPath is created within the test temporary directory. + resultBytes, err := os.ReadFile( + configPath, + ) + assert.NoError(t, err) + result := string(resultBytes) + newHost := "new-workspace" + config.SSHHostSuffix + newBlockStart := strings.Index(result, MarkerStartPrefix+newHost) + existingBlockStart := strings.Index(result, MarkerStartPrefix+"existing.devsy") + wildcardStart := strings.Index(result, "host *") + matchStart := strings.Index(result, "Match exec \"true\"") + + assert.Contains(t, result, MarkerStartPrefix+newHost) + assert.Contains(t, result, MarkerEndPrefix+newHost) + assert.Contains(t, result, "Host "+newHost) + assert.Contains( + t, + result, + "host *\n IdentityFile ~/.ssh/id_ed25519\n IdentityFile ~/.ssh/id_ed25519", + ) + assert.Contains(t, result, "Match exec \"true\"\n User conditional") + assert.Equal(t, 1, strings.Count(result, existingBlock)) + assert.Less(t, newBlockStart, wildcardStart) + assert.Less(t, newBlockStart, existingBlockStart) + assert.Less(t, newBlockStart, matchStart) +} diff --git a/pkg/subprocess/capture.go b/pkg/subprocess/capture.go index e48f6d9112..8aad2940d6 100644 --- a/pkg/subprocess/capture.go +++ b/pkg/subprocess/capture.go @@ -54,10 +54,13 @@ func (w RedactingWriter) Write(p []byte) (int, error) { return w.Next.Write(p) } redacted := []byte(w.Redactor.Redact(string(p))) - _, err := w.Next.Write(redacted) + n, err := w.Next.Write(redacted) if err != nil { return 0, err } + if n != len(redacted) { + return 0, io.ErrShortWrite + } return len(p), nil } @@ -80,10 +83,13 @@ func (w *StreamingRedactingWriter) Write(p []byte) (int, error) { if len(redacted) == 0 { return len(p), nil } - _, err := w.Next.Write(redacted) + n, err := w.Next.Write(redacted) if err != nil { return 0, err } + if n != len(redacted) { + return 0, io.ErrShortWrite + } return len(p), nil } @@ -93,8 +99,18 @@ func (w *StreamingRedactingWriter) Flush() error { if w == nil || w.Next == nil || w.stream == nil { return nil } - _, err := w.Next.Write([]byte(w.stream.Flush())) - return err + pending := w.stream.Flush() + if pending == "" { + return nil + } + n, err := w.Next.Write([]byte(pending)) + if err != nil { + return err + } + if n != len(pending) { + return io.ErrShortWrite + } + return nil } // Result contains bounded diagnostic output and execution metadata. diff --git a/pkg/subprocess/capture_test.go b/pkg/subprocess/capture_test.go index c3810defda..c8b3e8c878 100644 --- a/pkg/subprocess/capture_test.go +++ b/pkg/subprocess/capture_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "errors" + "io" "runtime" "strings" "testing" @@ -84,6 +85,65 @@ func TestRedactingWriterRedactsSecretSplitAcrossWrites(t *testing.T) { } } +type shortWriter struct{} + +func (shortWriter) Write([]byte) (int, error) { return 0, nil } + +type countingWriter struct{ writes int } + +func (w *countingWriter) Write(p []byte) (int, error) { + w.writes++ + return len(p), nil +} + +func TestRedactingWritersReportShortWrites(t *testing.T) { + redactor := secrets.NewRedactor(nil) + writers := []struct { + name string + writer io.Writer + }{ + {"redacting", RedactingWriter{Next: shortWriter{}, Redactor: redactor}}, + {"streaming", &StreamingRedactingWriter{Next: shortWriter{}, Redactor: redactor}}, + } + for _, tt := range writers { + t.Run(tt.name, func(t *testing.T) { + _, err := tt.writer.Write([]byte("plain text")) + if !errors.Is(err, io.ErrShortWrite) { + t.Fatalf("Write error = %v, want ErrShortWrite", err) + } + }) + } +} + +func TestStreamingRedactingWriterFlushOnlyWritesPendingBytes(t *testing.T) { + var next countingWriter + w := &StreamingRedactingWriter{Next: &next, Redactor: secrets.NewRedactor(nil)} + _, err := w.Write([]byte("plain text")) + if err != nil { + t.Fatal(err) + } + if err := w.Flush(); err != nil { + t.Fatal(err) + } + if next.writes != 1 { + t.Fatalf("downstream writes = %d, want 1", next.writes) + } +} + +func TestStreamingRedactingWriterFlushReportsShortWrite(t *testing.T) { + w := &StreamingRedactingWriter{ + Next: shortWriter{}, + Redactor: secrets.NewRedactor([]string{"hidden"}), + } + _, err := w.Write([]byte("h")) + if err != nil { + t.Fatal(err) + } + if err := w.Flush(); !errors.Is(err, io.ErrShortWrite) { + t.Fatalf("Flush error = %v, want ErrShortWrite", err) + } +} + func TestBoundedBufferRedactsSecretSplitAcrossWrites(t *testing.T) { secret := captureTestSecret b := NewBuffer(secrets.NewRedactor([]string{"TOKEN=" + secret})) diff --git a/pkg/tunnel/container.go b/pkg/tunnel/container.go index 39a9be375f..24777a41b3 100644 --- a/pkg/tunnel/container.go +++ b/pkg/tunnel/container.go @@ -132,10 +132,11 @@ func (c *ContainerTunnel) runHostTunnel( return agent.InjectAgent(ctx, &agent.InjectOptions{ Exec: func(ctx context.Context, command string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error { return c.client.Command(ctx, client.CommandOptions{ - Command: command, - Stdin: stdin, - Stdout: stdout, - Stderr: stderr, + Command: command, + Stdin: stdin, + Stdout: stdout, + Stderr: stderr, + RawStdout: true, }) }, IsLocal: c.client.AgentLocal(),