diff --git a/AGENTS.md b/AGENTS.md index a60735be0..1dfe98c9c 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -59,6 +59,8 @@ The agent binary runs inside the (Linux) workspace, so it is a Linux binary (`de ## Testing and Headless Verification +When testing local external providers with `--disable-daemon`, verify that startup and workspace logs pass configured options to the runtime subprocess and that the serialized agent payload excludes them. Include a host secret with the same name to check the local SSH session handoff. Provider options can contain host credentials. + ### Headless / Xvfb Requirements Desktop tests run inside an Electron browser environment. In headless or container environments, Electron commands require an `xvfb-run` prefix to emulate a display server: diff --git a/README.md b/README.md index 4394d75fb..d3eb4e623 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,8 @@ Devsy gives teams and their AI coding agents separate workspaces built from one standard definition. Workspaces run on Docker, Kubernetes, cloud providers, and SSH remote hosts. +Local external providers keep their configured runtime options when you use `devsy workspace up --disable-daemon`, including when a host secret has the same environment name. Provider options stay out of the workspace agent payload in this mode. Workspace logs use the configured runtime options as well. + diff --git a/cmd/workspace/logs.go b/cmd/workspace/logs.go index 546567235..7c735b7a4 100644 --- a/cmd/workspace/logs.go +++ b/cmd/workspace/logs.go @@ -14,6 +14,7 @@ import ( clientpkg "github.com/devsy-org/devsy/pkg/client" "github.com/devsy-org/devsy/pkg/config" "github.com/devsy-org/devsy/pkg/log" + "github.com/devsy-org/devsy/pkg/provider" "github.com/devsy-org/devsy/pkg/ssh" "github.com/devsy-org/devsy/pkg/tunnel" "github.com/devsy-org/devsy/pkg/workspace" @@ -133,6 +134,15 @@ func (cmd *LogsCmd) getWorkspaceClient( // injectLogsAgent injects the devsy agent binary over stdin/stdout and runs the // remote ssh-server that runLogsSession then connects to. func injectLogsAgent(ctx context.Context, params injectLogsAgentParams) error { + info, err := localLogsAgentInfo(params.client) + if err != nil { + return err + } + var environment []string + if info != nil { + environment = provider.ToEnvironment(info.Workspace, info.Machine, info.Options, nil) + } + streamer := log.NewJSONLogStreamer(log.StreamerOptions{ FallbackLevel: log.LevelDebug, DetectLevelPrefixes: true, @@ -149,17 +159,34 @@ func injectLogsAgent(ctx context.Context, params injectLogsAgentParams) error { RawStdout: true, }) }, - IsLocal: params.client.AgentLocal(), - RemoteAgentPath: params.client.AgentPath(), - DownloadURL: params.client.AgentURL(), - Command: params.sshServerCmd, - Stdin: params.stdin, - Stdout: params.stdout, - Stderr: streamer, - Timeout: params.timeout, + LocalEnvironment: environment, + IsLocal: params.client.AgentLocal(), + RemoteAgentPath: params.client.AgentPath(), + DownloadURL: params.client.AgentURL(), + Command: params.sshServerCmd, + Stdin: params.stdin, + Stdout: params.stdout, + Stderr: streamer, + Timeout: params.timeout, }) } +func localLogsAgentInfo( + workspaceClient clientpkg.WorkspaceClient, +) (*provider.AgentWorkspaceInfo, error) { + if !workspaceClient.AgentLocal() { + return nil, nil + } + _, info, err := workspaceClient.AgentInfo(provider.CLIOptions{}) + if err != nil { + return nil, fmt.Errorf("get agent info for logs: %w", err) + } + if info.Agent.Driver != provider.ExternalDriver { + return nil, nil + } + return info, nil +} + type injectLogsAgentParams struct { client clientpkg.WorkspaceClient sshServerCmd string @@ -169,6 +196,10 @@ type injectLogsAgentParams struct { } func runLogsSession(stdout, stdin *os.File, client clientpkg.WorkspaceClient) error { + info, err := localLogsAgentInfo(client) + if err != nil { + return err + } sshClient, err := ssh.StdioClientWithUser(stdout, stdin, "") if err != nil { return err @@ -180,6 +211,13 @@ func runLogsSession(stdout, stdin *os.File, client clientpkg.WorkspaceClient) er return err } defer func() { _ = session.Close() }() + if info != nil { + for name, value := range provider.ToOptions(info.Workspace, info.Machine, info.Options) { + if err := session.Setenv(name, value); err != nil { + return fmt.Errorf("set provider environment %q: %w", name, err) + } + } + } agentCommand := fmt.Sprintf( "%s internal agent workspace logs --context %s --id %s", diff --git a/cmd/workspace/logs_environment_test.go b/cmd/workspace/logs_environment_test.go new file mode 100644 index 000000000..66c16112c --- /dev/null +++ b/cmd/workspace/logs_environment_test.go @@ -0,0 +1,102 @@ +package workspace + +import ( + "errors" + "io" + "os" + "testing" + + "github.com/devsy-org/devsy/pkg/client" + "github.com/devsy-org/devsy/pkg/config" + "github.com/devsy-org/devsy/pkg/provider" + "github.com/stretchr/testify/suite" +) + +type logsEnvironmentSuite struct{ suite.Suite } + +func TestLogsEnvironment(t *testing.T) { suite.Run(t, new(logsEnvironmentSuite)) } + +func (s *logsEnvironmentSuite) TestLocalInjection() { + s.T().Setenv(config.EnvHome, s.T().TempDir()) + s.T().Setenv("MSB_PATH", "ambient-runtime") + s.T().Setenv("MSB_LIBKRUNFW_PATH", "") + s.T().Setenv("MSB_HOME", "") + for _, tc := range []struct{ name, driver, want string }{ + {name: "external", driver: provider.ExternalDriver, want: "configured runtime|configured firmware|workspace home"}, + {name: "docker", driver: provider.DockerDriver, want: "ambient-runtime||"}, + } { + s.Run(tc.name, func() { + c := newLogsEnvironmentClient(tc.driver) + reader, writer, err := os.Pipe() + s.Require().NoError(err) + defer func() { s.NoError(reader.Close()) }() + defer func() { _ = writer.Close() }() + err = injectLogsAgent(s.T().Context(), injectLogsAgentParams{ + client: c, stdout: writer, + sshServerCmd: `printf '%s|%s|%s' "$MSB_PATH" "$MSB_LIBKRUNFW_PATH" "$MSB_HOME"`, + }) + s.Require().NoError(err) + s.Require().NoError(writer.Close()) + output, err := io.ReadAll(reader) + s.Require().NoError(err) + s.Equal(tc.want, string(output)) + s.Equal("ambient-runtime", os.Getenv("MSB_PATH")) + }) + } +} + +func (s *logsEnvironmentSuite) TestResolutionError() { + c := newLogsEnvironmentClient(provider.ExternalDriver) + c.infoErr = errors.New("option resolution failed") + err := injectLogsAgent(s.T().Context(), injectLogsAgentParams{client: c}) + s.ErrorIs(err, c.infoErr) +} + +func (s *logsEnvironmentSuite) TestRemoteInjectionDoesNotResolveOptions() { + c := newLogsEnvironmentClient(provider.ExternalDriver) + c.local = false + c.infoErr = errors.New("remote options must not be requested") + info, err := localLogsAgentInfo(c) + s.Require().NoError(err) + s.Nil(info) + s.Zero(c.infoCalls) +} + +type logsEnvironmentClient struct { + client.WorkspaceClient + info *provider.AgentWorkspaceInfo + infoErr error + local bool + infoCalls int +} + +func (c *logsEnvironmentClient) AgentLocal() bool { return c.local } +func (*logsEnvironmentClient) AgentPath() string { return "" } +func (*logsEnvironmentClient) AgentURL() string { return "" } + +func (c *logsEnvironmentClient) AgentInfo( + provider.CLIOptions, +) (string, *provider.AgentWorkspaceInfo, error) { + c.infoCalls++ + return "", c.info, c.infoErr +} + +func newLogsEnvironmentClient(driver string) *logsEnvironmentClient { + return &logsEnvironmentClient{ + local: true, + info: &provider.AgentWorkspaceInfo{ + Agent: provider.ProviderAgentConfig{Driver: driver, Local: config.BoolTrue}, + Options: map[string]config.OptionValue{ + "MSB_PATH": {Value: "configured runtime"}, + "MSB_LIBKRUNFW_PATH": {Value: "configured firmware"}, + }, + Workspace: &provider.Workspace{ + ID: "logs-environment", Context: config.DefaultContext, + Provider: provider.WorkspaceProviderConfig{ + Name: "logs-external", + Options: map[string]config.OptionValue{"MSB_HOME": {Value: "workspace home"}}, + }, + }, + }, + } +} diff --git a/cmd/workspace/up/agent.go b/cmd/workspace/up/agent.go index 993ef0de8..1a046d0d0 100644 --- a/cmd/workspace/up/agent.go +++ b/cmd/workspace/up/agent.go @@ -272,6 +272,10 @@ func (cmd *UpCmd) devsyUpMachineSSH( if lifecycleOption != nil { options = append(options, lifecycleOption) } + var environment map[string]string + if wInfo.Agent.Driver == provider2.ExternalDriver && wInfo.Agent.Local == config.BoolTrue { + environment = provider2.ToOptions(wInfo.Workspace, wInfo.Machine, wInfo.Options) + } return sshtunnel.ExecuteCommand(ctx, sshtunnel.ExecuteCommandOptions{ Client: client, AddPrivateKeys: devsyConfig.ContextOption( @@ -280,6 +284,7 @@ func (cmd *UpCmd) devsyUpMachineSSH( AgentInject: newAgentInjectFunc(client, wInfo), SSHCommand: sshTunnelCmd, Command: agentCommand, + EnvVars: environment, TunnelServerFunc: func(ctx context.Context, stdin io.WriteCloser, stdout io.Reader) (*config2.Result, error) { return tunnelserver.RunUpServer(ctx, tunnelserver.UpSpec{ Reader: stdout, @@ -318,6 +323,15 @@ func newAgentInjectFunc( client client2.WorkspaceClient, wInfo *provider2.AgentWorkspaceInfo, ) sshtunnel.AgentInjectFunc { + var localEnvironment []string + if wInfo.Agent.Driver == provider2.ExternalDriver && wInfo.Agent.Local == config.BoolTrue { + localEnvironment = provider2.ToEnvironment( + wInfo.Workspace, + wInfo.Machine, + wInfo.Options, + nil, + ) + } return func( cancelCtx context.Context, sshCmd string, @@ -334,14 +348,15 @@ func newAgentInjectFunc( RawStdout: true, }) }, - IsLocal: client.AgentLocal(), - RemoteAgentPath: client.AgentPath(), - DownloadURL: client.AgentURL(), - Command: sshCmd, - Stdin: sshTunnelStdinReader, - Stdout: sshTunnelStdoutWriter, - Stderr: writer, - Timeout: wInfo.InjectTimeout, + LocalEnvironment: localEnvironment, + IsLocal: client.AgentLocal(), + RemoteAgentPath: client.AgentPath(), + DownloadURL: client.AgentURL(), + Command: sshCmd, + Stdin: sshTunnelStdinReader, + Stdout: sshTunnelStdoutWriter, + Stderr: writer, + Timeout: wInfo.InjectTimeout, }) } } diff --git a/cmd/workspace/up/external_environment_test.go b/cmd/workspace/up/external_environment_test.go new file mode 100644 index 000000000..8ad20fdd6 --- /dev/null +++ b/cmd/workspace/up/external_environment_test.go @@ -0,0 +1,99 @@ +package up + +import ( + "bytes" + "os" + "runtime" + "testing" + + "github.com/devsy-org/devsy/pkg/agent" + "github.com/devsy-org/devsy/pkg/client" + "github.com/devsy-org/devsy/pkg/client/clientimplementation" + "github.com/devsy-org/devsy/pkg/config" + "github.com/devsy-org/devsy/pkg/hash" + "github.com/devsy-org/devsy/pkg/provider" + "github.com/stretchr/testify/suite" +) + +type externalEnvironmentSuite struct{ suite.Suite } + +func TestExternalEnvironment(t *testing.T) { suite.Run(t, new(externalEnvironmentSuite)) } + +func (s *externalEnvironmentSuite) TestLocalInjection() { + s.T().Setenv(config.EnvHome, s.T().TempDir()) + s.T().Setenv("MSB_PATH", "ambient-runtime") + for _, disabled := range []bool{false, true} { + name := "daemon" + if disabled { + name = "disabled" + } + s.Run(name, func() { + client := s.newClient() + compressed, info, err := client.AgentInfo(provider.CLIOptions{DisableDaemon: disabled}) + s.Require().NoError(err) + payload, _, err := agent.DecodeWorkspaceInfo(compressed) + s.Require().NoError(err) + s.Equal(!disabled, len(payload.Options) > 0) + s.Equal(!disabled, len(payload.Workspace.Provider.Options) > 0) + var stdout bytes.Buffer + err = newAgentInjectFunc( + client, + info, + )( + s.T().Context(), + `printf '%s|%s|%s' "$MSB_PATH" "$MSB_LIBKRUNFW_PATH" "$MSB_HOME"`, + nil, + &stdout, + nil, + ) + s.Require().NoError(err) + s.Equal("configured runtime|configured firmware|workspace home", stdout.String()) + s.Equal("ambient-runtime", os.Getenv("MSB_PATH")) + }) + } +} + +const environmentProvider = "external-test" + +func (s *externalEnvironmentSuite) newClient() client.WorkspaceClient { + cfg := &config.Config{ + DefaultContext: config.DefaultContext, + Contexts: map[string]*config.ContextConfig{ + config.DefaultContext: { + Providers: map[string]*config.ProviderConfig{ + environmentProvider: { + Options: map[string]config.OptionValue{ + "MSB_PATH": {Value: "configured runtime"}, + "MSB_LIBKRUNFW_PATH": {Value: "configured firmware"}, + }, + }, + }, + }, + }, + } + workspace := &provider.Workspace{ + ID: "environment", + Context: config.DefaultContext, + Provider: provider.WorkspaceProviderConfig{ + Name: environmentProvider, + Options: map[string]config.OptionValue{"MSB_HOME": {Value: "workspace home"}}, + }, + } + providerConfig := &provider.ProviderConfig{Name: environmentProvider} + providerConfig.Agent.Driver = provider.ExternalDriver + providerConfig.Agent.Local = config.BoolTrue + providerConfig.Agent.External.Binary = "RUNTIME" + providerConfig.Agent.Binaries = map[string][]*provider.ProviderBinary{ + "RUNTIME": { + { + OS: runtime.GOOS, + Arch: runtime.GOARCH, + Path: "https://example.invalid/runtime", + Checksum: hash.String("runtime"), + }, + }, + } + client, err := clientimplementation.NewWorkspaceClient(cfg, providerConfig, workspace, nil) + s.Require().NoError(err) + return client +} diff --git a/pkg/agent/inject.go b/pkg/agent/inject.go index 011220ae2..dc8c5068d 100644 --- a/pkg/agent/inject.go +++ b/pkg/agent/inject.go @@ -39,6 +39,7 @@ var ( // InjectOptions defines the parameters for injecting the Devsy agent into a remote environment. type InjectOptions struct { + LocalEnvironment []string // Exec is the function used to execute commands on the remote machine. Required. // nolint:staticcheck Exec inject.ExecFunc @@ -271,7 +272,7 @@ func injectLocally(ctx context.Context, opts *InjectOptions) error { Stdin: opts.Stdin, Stdout: opts.Stdout, Stderr: opts.Stderr, - Environ: nil, + Environ: opts.LocalEnvironment, }) } diff --git a/pkg/client/clientimplementation/external_config_test.go b/pkg/client/clientimplementation/external_config_test.go index 0a96de60f..a6e9e2d62 100644 --- a/pkg/client/clientimplementation/external_config_test.go +++ b/pkg/client/clientimplementation/external_config_test.go @@ -1,11 +1,15 @@ package clientimplementation import ( + "runtime" "testing" + "github.com/devsy-org/api/pkg/devsy" + "github.com/devsy-org/devsy/pkg/agent" "github.com/devsy-org/devsy/pkg/config" "github.com/devsy-org/devsy/pkg/hash" "github.com/devsy-org/devsy/pkg/provider" + "github.com/devsy-org/devsy/pkg/types" "github.com/stretchr/testify/suite" ) @@ -61,3 +65,130 @@ func (s *externalHandoffSuite) TestResolution() { }) } } + +func (s *externalHandoffSuite) TestProviderOptionsStayOnHost() { + s.T().Setenv(config.EnvHome, s.T().TempDir()) + for _, tc := range providerOptionsCases() { + s.Run(tc.name, func() { + client := newProviderOptionsClient(tc.driver, tc.local) + compressed, host, err := client.AgentInfo(tc.cli) + s.Require().NoError(err) + payload, _, err := agent.DecodeWorkspaceInfo(compressed) + s.Require().NoError(err) + for _, info := range []struct { + value *provider.AgentWorkspaceInfo + options bool + }{{host, tc.hostOptions}, {payload, tc.payloadOptions}} { + if info.options { + s.Equal(handoffOption("global"), info.value.Options) + s.Equal(handoffOption("workspace"), info.value.Workspace.Provider.Options) + s.Equal(handoffOption("machine"), info.value.Machine.Provider.Options) + } else { + s.Empty(info.value.Options) + s.Empty(info.value.Workspace.Provider.Options) + s.Empty(info.value.Machine.Provider.Options) + } + } + s.Equal(handoffOption("global"), client.devsyConfig.ProviderOptions(handoffProvider)) + s.Equal(handoffOption("workspace"), client.providerWorkspace.Provider.Options) + s.Equal(handoffOption("machine"), client.providerMachine.Provider.Options) + }) + } +} + +type providerOptionsCase struct { + name string + driver string + local types.StrBool + cli provider.CLIOptions + hostOptions bool + payloadOptions bool +} + +func providerOptionsCases() []providerOptionsCase { + return []providerOptionsCase{ + { + name: "external local disabled", + driver: provider.ExternalDriver, + local: config.BoolTrue, + cli: provider.CLIOptions{DisableDaemon: true}, + hostOptions: true, + }, + { + name: "external remote disabled", driver: provider.ExternalDriver, + cli: provider.CLIOptions{DisableDaemon: true}, + }, + { + name: "docker disabled", driver: provider.DockerDriver, local: config.BoolTrue, + cli: provider.CLIOptions{DisableDaemon: true}, + }, + { + name: "custom disabled", driver: provider.CustomDriver, local: config.BoolTrue, + cli: provider.CLIOptions{DisableDaemon: true}, hostOptions: true, payloadOptions: true, + }, + { + name: "external daemon", driver: provider.ExternalDriver, local: config.BoolTrue, + hostOptions: true, payloadOptions: true, + }, + { + name: "external local platform", + driver: provider.ExternalDriver, + local: config.BoolTrue, + cli: provider.CLIOptions{ + Platform: devsy.PlatformOptions{Enabled: true}, + }, + hostOptions: true, + }, + } +} + +const handoffProvider = "handoff" + +func handoffOption(value string) map[string]config.OptionValue { + return map[string]config.OptionValue{"RUNTIME_OPTION": {Value: value}} +} + +func newProviderOptionsClient(driver string, local types.StrBool) *workspaceClient { + providerConfig := &provider.ProviderConfig{Name: handoffProvider} + providerConfig.Agent.Driver = driver + providerConfig.Agent.Local = local + providerConfig.Agent.External.Binary = "HANDOFF_RUNTIME" + providerConfig.Agent.Binaries = map[string][]*provider.ProviderBinary{ + "HANDOFF_RUNTIME": { + { + OS: runtime.GOOS, + Arch: runtime.GOARCH, + Path: "https://example.invalid/runtime", + Checksum: hash.String("runtime"), + }, + }, + } + return &workspaceClient{ + providerConfig: providerConfig, + devsyConfig: &config.Config{ + DefaultContext: config.DefaultContext, + Contexts: map[string]*config.ContextConfig{ + config.DefaultContext: { + Providers: map[string]*config.ProviderConfig{ + handoffProvider: {Options: handoffOption("global")}, + }, + }, + }, + }, + providerWorkspace: &provider.Workspace{ + ID: "workspace", + Context: config.DefaultContext, + Provider: provider.WorkspaceProviderConfig{ + Name: handoffProvider, + Options: handoffOption("workspace"), + }, + }, + providerMachine: &provider.Machine{ + ID: "machine", + Provider: provider.MachineProviderConfig{ + Name: handoffProvider, + Options: handoffOption("machine"), + }, + }, + } +} diff --git a/pkg/client/clientimplementation/workspace_client.go b/pkg/client/clientimplementation/workspace_client.go index 99431d4ca..643e6bae7 100644 --- a/pkg/client/clientimplementation/workspace_client.go +++ b/pkg/client/clientimplementation/workspace_client.go @@ -461,7 +461,13 @@ func (s *workspaceClient) compressedAgentInfo( } } - out, err := json.Marshal(agentInfo) + payload := agentPayload(agentInfo) + if agentInfo.Agent.Driver != provider.ExternalDriver || + agentInfo.Agent.Local != config.BoolTrue { + agentInfo = payload + } + + out, err := json.Marshal(payload) if err != nil { return "", nil, err } @@ -474,6 +480,15 @@ func (s *workspaceClient) compressedAgentInfo( return compressed, agentInfo, nil } +func agentPayload(info *provider.AgentWorkspaceInfo) *provider.AgentWorkspaceInfo { + payload := *info + if payload.Agent.Driver != provider.CustomDriver && + (payload.CLIOptions.Platform.Enabled || payload.CLIOptions.DisableDaemon) { + stripProviderOptions(&payload) + } + return &payload +} + func (s *workspaceClient) agentInfo(cliOptions provider.CLIOptions) *provider.AgentWorkspaceInfo { agentInfo := &provider.AgentWorkspaceInfo{ WorkspaceOrigin: s.workspaceOrigin(), @@ -495,11 +510,6 @@ func (s *workspaceClient) agentInfo(cliOptions provider.CLIOptions) *provider.Ag agentInfo.Agent.InjectDockerCredentials = config.BoolTrue } - if agentInfo.Agent.Driver != provider.CustomDriver && - (cliOptions.Platform.Enabled || cliOptions.DisableDaemon) { - stripProviderOptions(agentInfo) - } - return agentInfo } @@ -1620,14 +1630,24 @@ func BuildAgentClient(ctx context.Context, opts BuildAgentClientOptions) (*confi cancelCtx, cancel := context.WithCancel(ctx) defer cancel() + var localEnvironment []string + if wInfo.Agent.Driver == provider.ExternalDriver && wInfo.Agent.Local == config.BoolTrue { + localEnvironment = provider.ToEnvironment( + wInfo.Workspace, + wInfo.Machine, + wInfo.Options, + nil, + ) + } command := buildAgentCommand(opts.WorkspaceClient, opts.AgentCommand, workspaceInfo) errChan := runAgentInjection(cancelCtx, workspaceInjectionConfig{ - workspaceClient: opts.WorkspaceClient, - command: command, - stdin: pipes.stdinReader, - stdout: pipes.stdoutWriter, - timeout: wInfo.InjectTimeout, - cancel: cancel, + workspaceClient: opts.WorkspaceClient, + command: command, + stdin: pipes.stdinReader, + stdout: pipes.stdoutWriter, + timeout: wInfo.InjectTimeout, + localEnvironment: localEnvironment, + cancel: cancel, }) result, err := runTunnelServer(cancelCtx, opts, pipes.stdoutReader, pipes.stdinWriter) @@ -1690,12 +1710,13 @@ func (p *processIOPipes) close() { } type workspaceInjectionConfig struct { - workspaceClient client.WorkspaceClient - command string - stdin *os.File - stdout *os.File - timeout time.Duration - cancel context.CancelFunc + localEnvironment []string + workspaceClient client.WorkspaceClient + command string + stdin *os.File + stdout *os.File + timeout time.Duration + cancel context.CancelFunc } func runAgentInjection(ctx context.Context, cfg workspaceInjectionConfig) chan error { @@ -1721,14 +1742,15 @@ func runAgentInjection(ctx context.Context, cfg workspaceInjectionConfig) chan e RawStdout: true, }) }, - IsLocal: cfg.workspaceClient.AgentLocal(), - RemoteAgentPath: cfg.workspaceClient.AgentPath(), - DownloadURL: cfg.workspaceClient.AgentURL(), - Command: cfg.command, - Stdin: cfg.stdin, - Stdout: cfg.stdout, - Stderr: streamer, - Timeout: cfg.timeout, + LocalEnvironment: cfg.localEnvironment, + IsLocal: cfg.workspaceClient.AgentLocal(), + RemoteAgentPath: cfg.workspaceClient.AgentPath(), + DownloadURL: cfg.workspaceClient.AgentURL(), + Command: cfg.command, + Stdin: cfg.stdin, + Stdout: cfg.stdout, + Stderr: streamer, + Timeout: cfg.timeout, }) }() return errChan diff --git a/pkg/devcontainer/sshtunnel/environment_test.go b/pkg/devcontainer/sshtunnel/environment_test.go new file mode 100644 index 000000000..31f463437 --- /dev/null +++ b/pkg/devcontainer/sshtunnel/environment_test.go @@ -0,0 +1,93 @@ +package sshtunnel + +import ( + "crypto/ed25519" + "crypto/rand" + "net" + "os" + "testing" + "time" + + sshserver "github.com/devsy-org/ssh" + "github.com/stretchr/testify/suite" + "golang.org/x/crypto/ssh" +) + +type sessionEnvironmentSuite struct{ suite.Suite } + +func TestSessionEnvironment(t *testing.T) { suite.Run(t, new(sessionEnvironmentSuite)) } + +func (s *sessionEnvironmentSuite) TestCommandEnvironment() { + for _, tc := range []struct { + name string + environment map[string]string + closed bool + }{ + {name: "default"}, + {name: "configured", environment: map[string]string{"MSB_PATH": "configured runtime"}}, + { + name: "closed", environment: map[string]string{"MSB_PATH": "configured runtime"}, + closed: true, + }, + } { + s.Run(tc.name, func() { + client, observed := s.newClient() + session, err := client.NewSession() + s.Require().NoError(err) + defer func() { _ = session.Close() }() + if tc.closed { + s.Require().NoError(client.Close()) + } + stdin, input, err := os.Pipe() + s.Require().NoError(err) + s.Require().NoError(input.Close()) + defer func() { _ = stdin.Close() }() + output, stdout, err := os.Pipe() + s.Require().NoError(err) + defer func() { _ = output.Close() }() + defer func() { _ = stdout.Close() }() + err = runCommandInSSHTunnel(s.T().Context(), sshCommandParams{ + session: session, command: "probe", envVars: tc.environment, + }, stdin, stdout) + if tc.closed { + s.Require().ErrorContains(err, "set provider environment") + s.NotContains(err.Error(), "configured runtime") + return + } + s.Require().NoError(err) + if tc.environment == nil { + s.Empty(<-observed) + return + } + s.Equal([]string{"MSB_PATH=configured runtime"}, <-observed) + }) + } +} + +func (s *sessionEnvironmentSuite) newClient() (*ssh.Client, <-chan []string) { + _, key, err := ed25519.GenerateKey(rand.Reader) + s.Require().NoError(err) + signer, err := ssh.NewSignerFromKey(key) + s.Require().NoError(err) + observed := make(chan []string, 1) + server := &sshserver.Server{Handler: func(session sshserver.Session) { + observed <- session.Environ() + }} + server.AddHostKey(signer) + listener, err := net.Listen("tcp", "127.0.0.1:0") + s.Require().NoError(err) + done := make(chan error, 1) + go func() { done <- server.Serve(listener) }() + s.T().Cleanup(func() { + s.NoError(server.Close()) + s.ErrorIs(<-done, sshserver.ErrServerClosed) + }) + client, err := ssh.Dial("tcp", listener.Addr().String(), &ssh.ClientConfig{ + User: "test", + HostKeyCallback: ssh.FixedHostKey(signer.PublicKey()), + Timeout: 5 * time.Second, + }) + s.Require().NoError(err) + s.T().Cleanup(func() { _ = client.Close() }) + return client, observed +} diff --git a/pkg/devcontainer/sshtunnel/sshtunnel.go b/pkg/devcontainer/sshtunnel/sshtunnel.go index 571290334..1fe2a521e 100644 --- a/pkg/devcontainer/sshtunnel/sshtunnel.go +++ b/pkg/devcontainer/sshtunnel/sshtunnel.go @@ -31,6 +31,7 @@ type ExecuteCommandOptions struct { AgentInject AgentInjectFunc SSHCommand string Command string + EnvVars map[string]string TunnelServerFunc TunnelServerFunc } @@ -157,6 +158,7 @@ func runSSHTunnel(ctx context.Context, p sshTunnelParams) (*config2.Result, erro return runCommandInSSHTunnel(ctx, sshCommandParams{ session: sess, command: p.opts.Command, + envVars: p.opts.EnvVars, }, stdin, stdout) }, func(ctx context.Context, stdout, stdin *os.File) error { @@ -235,9 +237,15 @@ func setupSSHAgentForwarding(sshClient *ssh.Client, sess *ssh.Session) { type sshCommandParams struct { session *ssh.Session command string + envVars map[string]string } func runCommandInSSHTunnel(ctx context.Context, p sshCommandParams, stdin, stdout *os.File) error { + for name, value := range p.envVars { + if err := p.session.Setenv(name, value); err != nil { + return fmt.Errorf("set provider environment %q: %w", name, err) + } + } streamer := newSSHTunnelJSONLogStreamer() defer func() { _ = streamer.Close() }() diff --git a/pkg/driver/external/production_test.go b/pkg/driver/external/production_test.go index 0663d3087..170178315 100644 --- a/pkg/driver/external/production_test.go +++ b/pkg/driver/external/production_test.go @@ -33,7 +33,10 @@ func TestMain(m *testing.M) { os.Exit(m.Run()) } -const noImageBackend = "none" +const ( + noImageBackend = "none" + fixtureSetting = "FIXTURE_SETTING" +) type ProductionHostSuite struct{ suite.Suite } @@ -83,12 +86,36 @@ func (s *ProductionHostSuite) TestPreparedRuntimeThroughCLIHelper() { s.NotEmpty(host.Info().RuntimeName) s.Require().NoError(host.Preflight(ctx, driver.PreflightOptions{})) s.assertFactoryIntegration(ctx, workspace) + s.assertProviderEnvironment(ctx, workspace) workspace.Origin = filepath.Join(origin, "missing") _, err = external.New(ctx, workspace) s.ErrorIs(err, os.ErrNotExist) } +func (s *ProductionHostSuite) assertProviderEnvironment( + ctx context.Context, + workspace *provider.AgentWorkspaceInfo, +) { + workspace = provider.CloneAgentWorkspaceInfo(workspace) + workspace.Agent.External.Args = append(workspace.Agent.External.Args, "--mode", "environment") + workspace.CLIOptions.DisableDaemon = true + workspace.Options = map[string]config.OptionValue{ + "FIXTURE_TOKEN": {Value: "private-canary-value"}, + fixtureSetting: {Value: "global"}, + } + workspace.Workspace.Provider.Options = map[string]config.OptionValue{ + "FIXTURE_EMPTY": {}, + fixtureSetting: {Value: "workspace"}, + } + workspace.Machine = &provider.Machine{Provider: provider.MachineProviderConfig{ + Options: map[string]config.OptionValue{fixtureSetting: {Value: "machine"}}, + }} + host, err := external.New(ctx, workspace) + s.Require().NoError(err) + s.Equal("machine", host.Info().RuntimeVersion) +} + func (s *ProductionHostSuite) assertFactoryIntegration( ctx context.Context, workspace *provider.AgentWorkspaceInfo,
Website