diff --git a/cmd/internal/agentcontainer/lifecycle_terminal.go b/cmd/internal/agentcontainer/lifecycle_terminal.go new file mode 100644 index 0000000000..c3ef595af0 --- /dev/null +++ b/cmd/internal/agentcontainer/lifecycle_terminal.go @@ -0,0 +1,50 @@ +//go:build !windows + +package agentcontainer + +import ( + "context" + "os" + "os/signal" + "syscall" + + "github.com/devsy-org/devsy/pkg/agent/tunnel" + "github.com/devsy-org/devsy/pkg/devcontainer/setup" + "github.com/devsy-org/devsy/pkg/provider" +) + +func (cmd *SetupContainerCmd) preAttach( + ctx context.Context, + state *containerState, + cfg *setup.ContainerSetupConfig, + open func(context.Context, tunnel.TunnelClient, provider.LifecycleTerminalRequest) (tunnel.TerminalSession, error), +) (setup.DeferredHooks, error) { + if cmd.protocolSet { + session, err := open( + ctx, + state.tunnelClient, + *state.workspaceInfo.CLIOptions.LifecycleTerminal, + ) + if err != nil { + return setup.DeferredHooks{}, err + } + defer func() { _ = session.Close(ctx) }() + if err := setup.ValidateInteractivePostStart( + state.setupInfo.MergedConfig.PostStartCommands, + ); err != nil { + return setup.DeferredHooks{}, err + } + cfg.Terminal = session + } + return setup.SetupContainerPreAttach(ctx, cfg) +} + +func setupSignalContext( + ctx context.Context, + interactive bool, +) (context.Context, context.CancelFunc) { + if !interactive { + return ctx, func() {} + } + return signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP) +} diff --git a/cmd/internal/agentcontainer/lifecycle_terminal_test.go b/cmd/internal/agentcontainer/lifecycle_terminal_test.go new file mode 100644 index 0000000000..25cfc03950 --- /dev/null +++ b/cmd/internal/agentcontainer/lifecycle_terminal_test.go @@ -0,0 +1,148 @@ +//go:build !windows + +package agentcontainer + +import ( + "context" + "encoding/json" + "errors" + "os" + "os/signal" + "syscall" + "testing" + "time" + + "github.com/devsy-org/devsy/cmd/flags" + "github.com/devsy-org/devsy/pkg/agent/tunnel" + "github.com/devsy-org/devsy/pkg/compress" + "github.com/devsy-org/devsy/pkg/devcontainer/config" + "github.com/devsy-org/devsy/pkg/devcontainer/setup" + "github.com/devsy-org/devsy/pkg/provider" + "github.com/devsy-org/devsy/pkg/types" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" +) + +type AgentContainerLifecycleTerminalSuite struct { + suite.Suite +} + +func TestAgentContainerLifecycleTerminalSuite(t *testing.T) { + suite.Run(t, new(AgentContainerLifecycleTerminalSuite)) +} + +func (s *AgentContainerLifecycleTerminalSuite) TestLifecycleTerminalGuard() { + t := s.T() + require.NotNil( + t, + NewSetupContainerCmd(&flags.GlobalFlags{}).Flags().Lookup("lifecycle-terminal-protocol"), + ) + for _, tc := range []struct { + set, present bool + version int + requestVersion uint32 + valid bool + }{ + {false, false, 0, 0, true}, + {true, true, 1, 1, true}, + {true, false, 1, 0, false}, + {false, true, 0, 1, false}, + {true, true, 0, 1, false}, + {true, true, 2, 1, false}, + {true, true, 1, 0, false}, + {true, true, 1, 2, false}, + } { + cmd := &SetupContainerCmd{protocolSet: tc.set, LifecycleTerminalProtocol: tc.version} + var request *provider.LifecycleTerminalRequest + if tc.present { + request = &provider.LifecycleTerminalRequest{ProtocolVersion: tc.requestVersion} + } + require.Equal(t, tc.valid, cmd.validateTerminal(request) == nil) + } +} + +type testTerminalSession struct{ closed bool } + +func (s *testTerminalSession) Begin( + context.Context, + tunnel.TerminalInvocation, +) (tunnel.TerminalAttachment, error) { + return nil, errors.New("unexpected begin") +} +func (s *testTerminalSession) Close(context.Context) error { s.closed = true; return nil } + +func (s *AgentContainerLifecycleTerminalSuite) TestPreAttachClosesSessionOnInvalidHooks() { + t := s.T() + session := &testTerminalSession{} + info := &config.Result{MergedConfig: &config.MergedDevContainerConfig{}} + info.MergedConfig.PostStartCommands = []types.LifecycleHook{{"a": {"true"}, "b": {"true"}}} + state := &containerState{ + setupInfo: info, + workspaceInfo: &provider.ContainerWorkspaceInfo{ + CLIOptions: provider.CLIOptions{ + LifecycleTerminal: &provider.LifecycleTerminalRequest{}, + }, + }, + } + cmd := &SetupContainerCmd{protocolSet: true} + _, err := cmd.preAttach( + context.Background(), + state, + &setup.ContainerSetupConfig{SetupInfo: info}, + func(context.Context, tunnel.TunnelClient, provider.LifecycleTerminalRequest) (tunnel.TerminalSession, error) { + return session, nil + }, + ) + require.ErrorContains(t, err, "postStartCommand") + require.True(t, session.closed) +} + +func (s *AgentContainerLifecycleTerminalSuite) TestSetupSignalContext() { + t := s.T() + signals := make(chan os.Signal, 1) + signal.Notify(signals, os.Interrupt) + defer signal.Stop(signals) + ctx, stop := setupSignalContext(context.Background(), false) + defer stop() + require.NoError(t, syscall.Kill(os.Getpid(), syscall.SIGINT)) + select { + case <-signals: + case <-time.After(5 * time.Second): + t.Fatal("signal not received") + } + require.NoError(t, ctx.Err()) + interactive, cancel := setupSignalContext(context.Background(), true) + defer cancel() + require.NoError(t, syscall.Kill(os.Getpid(), syscall.SIGINT)) + select { + case <-interactive.Done(): + case <-time.After(5 * time.Second): + t.Fatal("context not canceled") + } +} + +func (s *AgentContainerLifecycleTerminalSuite) TestRunRejectsRequestVersionBeforeInitialization() { + t := s.T() + info := provider.ContainerWorkspaceInfo{ + CLIOptions: provider.CLIOptions{ + LifecycleTerminal: &provider.LifecycleTerminalRequest{ProtocolVersion: 2}, + }, + } + data, err := json.Marshal(info) + require.NoError(t, err) + encoded, err := compress.Compress(string(data)) + require.NoError(t, err) + command := &SetupContainerCmd{ + ContainerWorkspaceInfo: encoded, + protocolSet: true, + LifecycleTerminalProtocol: 1, + } + setup, err := compress.Compress(`{}`) + require.NoError(t, err) + command.SetupInfo = setup + require.ErrorContains( + t, + command.Run(context.Background()), + "lifecycle terminal protocol and request mismatch", + ) +} diff --git a/cmd/internal/agentcontainer/setup.go b/cmd/internal/agentcontainer/setup.go index fc2272ab17..fc62acbb92 100644 --- a/cmd/internal/agentcontainer/setup.go +++ b/cmd/internal/agentcontainer/setup.go @@ -38,17 +38,19 @@ import ( type SetupContainerCmd struct { *flags.GlobalFlags - ChownWorkspace bool - StreamMounts bool - InjectGitCredentials bool - Prebuild bool - ContainerWorkspaceInfo string - SetupInfo string - AccessKey string - PlatformHost string - WorkspaceHost string - DotfilesRepo string - DotfilesScript string + LifecycleTerminalProtocol int + protocolSet bool + ChownWorkspace bool + StreamMounts bool + InjectGitCredentials bool + Prebuild bool + ContainerWorkspaceInfo string + SetupInfo string + AccessKey string + PlatformHost string + WorkspaceHost string + DotfilesRepo string + DotfilesScript string } // NewSetupContainerCmd creates a new command. @@ -61,6 +63,7 @@ func NewSetupContainerCmd(globalFlags *flags.GlobalFlags) *cobra.Command { Short: "Sets up a container", Args: cobra.NoArgs, RunE: func(cobraCmd *cobra.Command, args []string) error { + cmd.protocolSet = cobraCmd.Flags().Changed("lifecycle-terminal-protocol") return cmd.Run(cobraCmd.Context()) }, } @@ -82,16 +85,22 @@ type containerState struct { // Run runs the command logic. func (cmd *SetupContainerCmd) Run(ctx context.Context) error { - tunnelClient, err := cmd.initializeTunnelClient(ctx) + workspaceInfo, setupInfo, err := cmd.parseWorkspaceAndSetupInfo() if err != nil { return err } - workspaceInfo, setupInfo, err := cmd.parseWorkspaceAndSetupInfo() + if err := cmd.validateTerminal(workspaceInfo.CLIOptions.LifecycleTerminal); err != nil { + return err + } + tunnelClient, err := cmd.initializeTunnelClient(ctx) if err != nil { return err } + ctx, stop := setupSignalContext(ctx, cmd.protocolSet) + defer stop() + state := &containerState{ workspaceInfo: workspaceInfo, setupInfo: setupInfo, @@ -128,6 +137,16 @@ func (cmd *SetupContainerCmd) Run(ctx context.Context) error { return err } +func (cmd *SetupContainerCmd) validateTerminal(request *provider2.LifecycleTerminalRequest) error { + if cmd.protocolSet != (request != nil) || + (cmd.protocolSet && (cmd.LifecycleTerminalProtocol != int(tunnel.LifecycleTerminalProtocolVersion) || + request.ProtocolVersion != tunnel.LifecycleTerminalProtocolVersion || + int(request.ProtocolVersion) != cmd.LifecycleTerminalProtocol)) { + return fmt.Errorf("lifecycle terminal protocol and request mismatch") + } + return nil +} + func (cmd *SetupContainerCmd) reportSetup( ctx context.Context, state *containerState, @@ -176,6 +195,8 @@ func (cmd *SetupContainerCmd) runSetup(ctx context.Context, state *containerStat } func (cmd *SetupContainerCmd) registerFlags(setupContainerCmd *cobra.Command) { + setupContainerCmd.Flags(). + IntVar(&cmd.LifecycleTerminalProtocol, "lifecycle-terminal-protocol", 0, "Lifecycle terminal protocol version") cmd.registerBehaviorFlags(setupContainerCmd) cmd.registerWorkspaceInfoFlags(setupContainerCmd) cmd.registerDotfilesFlags(setupContainerCmd) @@ -345,7 +366,7 @@ func (cmd *SetupContainerCmd) finalizeSetup(ctx context.Context, state *containe }, } - deferred, err := setup.SetupContainerPreAttach(ctx, cfg) + deferred, err := cmd.preAttach(ctx, state, cfg, tunnelserver.OpenLifecycleTerminal) if err != nil { return err } diff --git a/cmd/internal/agentworkspace/lifecycle_terminal_test.go b/cmd/internal/agentworkspace/lifecycle_terminal_test.go new file mode 100644 index 0000000000..ddf274914d --- /dev/null +++ b/cmd/internal/agentworkspace/lifecycle_terminal_test.go @@ -0,0 +1,67 @@ +package agentworkspace + +import ( + "context" + "encoding/json" + "testing" + + "github.com/devsy-org/devsy/cmd/flags" + "github.com/devsy-org/devsy/pkg/compress" + "github.com/devsy-org/devsy/pkg/provider" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" +) + +type AgentWorkspaceLifecycleTerminalSuite struct { + suite.Suite +} + +func TestAgentWorkspaceLifecycleTerminalSuite(t *testing.T) { + suite.Run(t, new(AgentWorkspaceLifecycleTerminalSuite)) +} + +func (s *AgentWorkspaceLifecycleTerminalSuite) TestLifecycleTerminalGuard() { + t := s.T() + require.NotNil(t, NewUpCmd(&flags.GlobalFlags{}).Flags().Lookup("lifecycle-terminal-protocol")) + for _, tc := range []struct { + set, present bool + version int + requestVersion uint32 + valid bool + }{ + {false, false, 0, 0, true}, + {true, true, 1, 1, true}, + {true, false, 1, 0, false}, + {false, true, 0, 1, false}, + {true, true, 0, 1, false}, + {true, true, 2, 1, false}, + {true, true, 1, 0, false}, + {true, true, 1, 2, false}, + } { + cmd := &UpCmd{protocolSet: tc.set, LifecycleTerminalProtocol: tc.version} + var request *provider.LifecycleTerminalRequest + if tc.present { + request = &provider.LifecycleTerminalRequest{ProtocolVersion: tc.requestVersion} + } + require.Equal(t, tc.valid, cmd.validateTerminal(request) == nil) + } +} + +func (s *AgentWorkspaceLifecycleTerminalSuite) TestRunRejectsRequestVersionBeforeInitialization() { + t := s.T() + info := provider.AgentWorkspaceInfo{ + CLIOptions: provider.CLIOptions{ + LifecycleTerminal: &provider.LifecycleTerminalRequest{ProtocolVersion: 2}, + }, + } + data, err := json.Marshal(info) + require.NoError(t, err) + encoded, err := compress.Compress(string(data)) + require.NoError(t, err) + command := &UpCmd{WorkspaceInfo: encoded, protocolSet: true, LifecycleTerminalProtocol: 1} + require.ErrorContains( + t, + command.Run(context.Background()), + "lifecycle terminal protocol and request mismatch", + ) +} diff --git a/cmd/internal/agentworkspace/up.go b/cmd/internal/agentworkspace/up.go index 6566a2cb76..001c10f3ae 100644 --- a/cmd/internal/agentworkspace/up.go +++ b/cmd/internal/agentworkspace/up.go @@ -46,7 +46,9 @@ const ( type UpCmd struct { *flags.GlobalFlags - WorkspaceInfo string + WorkspaceInfo string + LifecycleTerminalProtocol int + protocolSet bool } // NewUpCmd creates a new command. @@ -59,6 +61,7 @@ func NewUpCmd(flags *flags.GlobalFlags) *cobra.Command { Short: "Starts a new devcontainer", Args: cobra.NoArgs, RunE: func(cobraCmd *cobra.Command, _ []string) error { + cmd.protocolSet = cobraCmd.Flags().Changed("lifecycle-terminal-protocol") return cmd.Run(cobraCmd.Context()) }, } @@ -66,11 +69,20 @@ func NewUpCmd(flags *flags.GlobalFlags) *cobra.Command { upCmd, cliflags.String(&cmd.WorkspaceInfo, names.WorkspaceInfo, "", "The workspace info"), ) + upCmd.Flags(). + IntVar(&cmd.LifecycleTerminalProtocol, "lifecycle-terminal-protocol", 0, "Lifecycle terminal protocol version") _ = upCmd.MarkFlagRequired(names.WorkspaceInfo) return upCmd } func (cmd *UpCmd) Run(ctx context.Context) error { + decoded, _, err := agent.DecodeWorkspaceInfo(cmd.WorkspaceInfo) + if err != nil { + return err + } + if err := cmd.validateTerminal(decoded.CLIOptions.LifecycleTerminal); err != nil { + return err + } workspaceInfo, err := cmd.loadWorkspaceInfo(ctx) if err != nil { return err @@ -104,6 +116,16 @@ func (cmd *UpCmd) Run(ctx context.Context) error { return nil } +func (cmd *UpCmd) validateTerminal(request *provider.LifecycleTerminalRequest) error { + if cmd.protocolSet != (request != nil) || + (cmd.protocolSet && (cmd.LifecycleTerminalProtocol != int(tunnel.LifecycleTerminalProtocolVersion) || + request.ProtocolVersion != tunnel.LifecycleTerminalProtocolVersion || + int(request.ProtocolVersion) != cmd.LifecycleTerminalProtocol)) { + return fmt.Errorf("lifecycle terminal protocol and request mismatch") + } + return nil +} + func (cmd *UpCmd) loadWorkspaceInfo(ctx context.Context) (*provider.AgentWorkspaceInfo, error) { shouldExit, workspaceInfo, err := agent.WriteWorkspaceInfoAndDeleteOld( cmd.WorkspaceInfo, @@ -230,8 +252,9 @@ func (cmd *UpCmd) devsyUp( reporter := tunnelserver.NewTunnelStatusReporter(ctx, tunnelClient) result, err := runner.Up(ctx, devcontainer.UpOptions{ - CLIOptions: cliOptions, - RegistryCache: workspaceInfo.RegistryCache, + CLIOptions: cliOptions, + LifecycleTerminalClient: tunnelClient, + RegistryCache: workspaceInfo.RegistryCache, }, workspaceInfo.InjectTimeout, reporter) run.Finish(err, false) if !hop.Flush(ctx) { diff --git a/cmd/workspace/up/agent.go b/cmd/workspace/up/agent.go index 8d54a288db..993ef0de83 100644 --- a/cmd/workspace/up/agent.go +++ b/cmd/workspace/up/agent.go @@ -50,17 +50,48 @@ func (cmd *UpCmd) devsyUp( return result, nil } +func (cmd *UpCmd) validateInteractiveRoute(client client2.BaseWorkspaceClient) error { + if !cmd.InteractivePostStart { + return nil + } + if cmd.Platform.Enabled { + return fmt.Errorf( + "--interactive-post-start is unsupported with platform-managed workspaces", + ) + } + switch client.(type) { + case client2.WorkspaceClient: + return nil + case client2.ProxyClient: + return fmt.Errorf("--interactive-post-start is unsupported with proxy workspaces") + case client2.DaemonClient: + return fmt.Errorf("--interactive-post-start is unsupported with daemon workspaces") + } + return nil +} + func (cmd *UpCmd) dispatchClient( ctx context.Context, devsyConfig *config.Config, client client2.BaseWorkspaceClient, ) (*config2.Result, error) { + if cmd.InteractivePostStart && cmd.Platform.Enabled { + return nil, fmt.Errorf( + "--interactive-post-start is unsupported with platform-managed workspaces", + ) + } switch client := client.(type) { case client2.WorkspaceClient: return cmd.devsyUpMachine(ctx, devsyConfig, client) case client2.ProxyClient: + if cmd.InteractivePostStart { + return nil, fmt.Errorf("--interactive-post-start is unsupported with proxy workspaces") + } return cmd.devsyUpProxy(ctx, client) case client2.DaemonClient: + if cmd.InteractivePostStart { + return nil, fmt.Errorf("--interactive-post-start is unsupported with daemon workspaces") + } return cmd.devsyUpDaemon(ctx, client) default: return nil, fmt.Errorf("unsupported client type: %T", client) @@ -204,6 +235,18 @@ func (cmd *UpCmd) devsyUpMachineSSH( devsyConfig *config.Config, client client2.WorkspaceClient, ) (*config2.Result, error) { + var owner *interactivePostStart + var lifecycleOption tunnelserver.Option + if request := cmd.LifecycleTerminal; request != nil { + gate := terminalStatusGate(cmd.reporter()) + owner = newInteractivePostStart(interactivePostStartOptions{ + stdin: os.Stdin, stdout: os.Stdout, rows: cmd.TerminalRows, + columns: cmd.TerminalColumns, term: os.Getenv("TERM"), + status: gate, output: cmd.stdout(), + }) + defer owner.Close() + lifecycleOption = tunnelserver.WithLifecycleTerminalOwner(owner, *request) + } workspaceInfo, wInfo, err := client.AgentInfo(withoutSecretValues(cmd.CLIOptions)) if err != nil { return nil, fmt.Errorf("get agent info: %w", err) @@ -214,15 +257,21 @@ func (cmd *UpCmd) devsyUpMachineSSH( sshTunnelCmd += " --debug" //nolint:goconst } - agentCommand := fmt.Sprintf( - "%s internal agent workspace up --workspace-info %s", - shellescape.Quote(client.AgentPath()), - shellescape.Quote(workspaceInfo), + agentCommand := workspaceAgentCommand( + client.AgentPath(), + workspaceInfo, + log.DebugEnabled(), + cmd.LifecycleTerminal != nil, ) - if log.DebugEnabled() { - agentCommand += " --debug" - } + options := append([]tunnelserver.Option{ + tunnelserver.WithSecrets(cmd.SecretsEnv, cmd.SecretsMount), + tunnelserver.WithGitToken(cmd.GitToken), + tunnelserver.WithStatusReporter(cmd.reporter()), + }, cmd.setupHop.Options()...) + if lifecycleOption != nil { + options = append(options, lifecycleOption) + } return sshtunnel.ExecuteCommand(ctx, sshtunnel.ExecuteCommandOptions{ Client: client, AddPrivateKeys: devsyConfig.ContextOption( @@ -238,16 +287,27 @@ func (cmd *UpCmd) devsyUpMachineSSH( AllowGitCredentials: client.AgentInjectGitCredentials(cmd.CLIOptions), AllowDockerCredentials: client.AgentInjectDockerCredentials(cmd.CLIOptions), Workspace: client.WorkspaceConfig(), - Options: append([]tunnelserver.Option{ - tunnelserver.WithSecrets(cmd.SecretsEnv, cmd.SecretsMount), - tunnelserver.WithGitToken(cmd.GitToken), - tunnelserver.WithStatusReporter(cmd.reporter()), - }, cmd.setupHop.Options()...), + Options: options, }) }, }) } +func workspaceAgentCommand(agentPath, workspaceInfo string, debug, lifecycleTerminal bool) string { + command := fmt.Sprintf( + "%s internal agent workspace up --workspace-info %s", + shellescape.Quote(agentPath), + shellescape.Quote(workspaceInfo), + ) + if debug { + command += " --debug" + } + if lifecycleTerminal { + command += " " + shellescape.Quote("--lifecycle-terminal-protocol=1") + } + return command +} + func withoutSecretValues(options provider2.CLIOptions) provider2.CLIOptions { options.SecretsEnv = nil options.SecretsMount = nil diff --git a/cmd/workspace/up/interactive_post_start.go b/cmd/workspace/up/interactive_post_start.go new file mode 100644 index 0000000000..1e46cdab2d --- /dev/null +++ b/cmd/workspace/up/interactive_post_start.go @@ -0,0 +1,575 @@ +package up + +import ( + "context" + "fmt" + "io" + "os" + "sync" + "time" + + "github.com/devsy-org/devsy/pkg/agent/tunnel" + "github.com/devsy-org/devsy/pkg/log" + "github.com/devsy-org/devsy/pkg/ssh" + "github.com/devsy-org/devsy/pkg/terminal" + "golang.org/x/term" +) + +type interactivePostStartOptions struct { + stdin, stdout *os.File + rows, columns int + term string + status *interactiveStatusGate + output io.Writer + exitAckTimeout time.Duration + inputFactory func(*os.File) (terminal.Input, error) +} + +type interactivePostStart struct { + opts interactivePostStartOptions + mu sync.Mutex + active bool + cancel context.CancelFunc + done chan struct{} +} + +func newInteractivePostStart(opts interactivePostStartOptions) *interactivePostStart { + if opts.status != nil { + opts.status.renderMu.Lock() + opts.status.mu.Lock() + opts.status.enabled = true + opts.status.mu.Unlock() + opts.status.renderMu.Unlock() + } + return &interactivePostStart{opts: opts} +} + +func (s *interactivePostStart) Close() { + ctx, stop := context.WithTimeout(context.Background(), 5*time.Second) + defer stop() + s.mu.Lock() + cancel, done := s.cancel, s.done + s.mu.Unlock() + if cancel != nil { + cancel() + } + if done != nil { + select { + case <-done: + case <-ctx.Done(): + return + } + } + if s.opts.status != nil { + s.opts.status.SetActive(false) + } + if s.opts.status != nil && s.opts.status.journal != nil { + _ = s.opts.status.journal.end(ctx) + } +} + +type terminalReceived struct { + frame *tunnel.LifecycleTerminalRequest + err error +} +type terminalRead struct { + data []byte + err error +} + +type interactiveSession struct { + owner *interactivePostStart + ctx context.Context + stream tunnel.Tunnel_LifecycleTerminalServer + rows, cols uint32 + input terminal.Input + restore, restoreLog func() + journalActive bool + inputCancel context.CancelFunc + done chan struct{} + reads <-chan terminalRead + invocation string + sequence uint64 + output chan []byte + outputDone chan error +} + +func (s *interactivePostStart) Serve( + parent context.Context, + stream tunnel.Tunnel_LifecycleTerminalServer, +) error { + s.mu.Lock() + if s.active { + s.mu.Unlock() + return fmt.Errorf("terminal session already active") + } + ctx, cancel := context.WithCancel(parent) + s.active = true + s.cancel = cancel + s.done = make(chan struct{}) + doneSession := s.done + s.mu.Unlock() + defer func() { + cancel() + s.mu.Lock() + s.active = false + s.cancel = nil + s.done = nil + close(doneSession) + s.mu.Unlock() + }() + session := &interactiveSession{owner: s, ctx: ctx, stream: stream} + return session.serve() +} + +func (s *interactivePostStart) size() (uint32, uint32, error) { + if !validTerminalDimension(s.opts.rows) || !validTerminalDimension(s.opts.columns) || + s.opts.stdout == nil { + return 0, 0, fmt.Errorf("invalid terminal dimensions") + } + c, r := terminalSize(s.opts.stdout) + if s.opts.rows != 0 { + r = s.opts.rows + } + if s.opts.columns != 0 { + c = s.opts.columns + } + return checkedTerminalSize(r, c) +} + +func checkedTerminalSize(r, c int) (uint32, uint32, error) { + if r < 0 || r > 65535 || c < 0 || c > 65535 { + return 0, 0, fmt.Errorf("invalid terminal dimensions") + } + return uint32(r), uint32(c), nil +} + +func terminalSize(stdout *os.File) (int, int) { + c, r, err := term.GetSize(int(stdout.Fd())) + if err != nil || c < 1 || r < 1 { + return 80, 24 + } + return c, r +} + +func validTerminalDimension(value int) bool { + return value >= 0 && value <= 65535 +} + +func (t *interactiveSession) serve() error { + if err := t.ready(); err != nil { + return err + } + received := make(chan terminalReceived, 1) + go t.receive(received) + resize := ssh.WatchWindowSize(t.ctx) + defer t.stopOutput() + defer t.finish() + for { + complete, err := t.step(resize, received) + if err != nil || complete { + return err + } + } +} + +func (t *interactiveSession) finish() { + t.cleanup() + t.finishJournal() + if t.owner.opts.status != nil { + t.owner.opts.status.SetActive(false) + } +} + +func (t *interactiveSession) step( + resize <-chan os.Signal, + received <-chan terminalReceived, +) (bool, error) { + select { + case <-t.ctx.Done(): + t.sendCancel() + return true, t.ctx.Err() + case <-resize: + return false, t.handleResize() + case v := <-t.reads: + return false, t.handleRead(v) + case v := <-received: + return t.handleFrame(v) + } +} + +func (t *interactiveSession) ready() error { + var err error + t.rows, t.cols, err = t.owner.size() + if err != nil { + return err + } + name := t.owner.opts.term + if name == "" { + name = "xterm-256color" + } + if !validInteractiveTerm(name) { + return fmt.Errorf("invalid TERM") + } + return t.stream.Send( + &tunnel.LifecycleTerminalResponse{Frame: &tunnel.LifecycleTerminalResponse_Ready{ + Ready: &tunnel.LifecycleTerminalReady{ + ProtocolVersion: tunnel.LifecycleTerminalProtocolVersion, + Term: name, Rows: t.rows, Columns: t.cols, Input: true, + }, + }}, + ) +} + +func (t *interactiveSession) receive(received chan<- terminalReceived) { + for { + f, e := t.stream.Recv() + select { + case received <- terminalReceived{f, e}: + case <-t.ctx.Done(): + return + } + if e != nil { + return + } + } +} + +func (t *interactiveSession) stopOutput() { + if t.output != nil { + close(t.output) + t.output = nil + } +} + +func (t *interactiveSession) cleanup() { + if t.restore != nil { + t.restore() + t.restore = nil + } + if t.restoreLog != nil { + t.restoreLog() + t.restoreLog = nil + } + if t.inputCancel != nil { + t.inputCancel() + t.inputCancel = nil + } + if t.input != nil { + t.input.Cancel() + if t.done != nil { + <-t.done + } + _ = t.input.Close() + t.input = nil + } + t.reads = nil +} + +func (t *interactiveSession) finishJournal() { + if !t.journalActive { + return + } + t.journalActive = false + ctx, stop := context.WithTimeout(context.Background(), 5*time.Second) + defer stop() + _ = t.owner.opts.status.journal.end(ctx) +} + +func (t *interactiveSession) sendCancel() { + if t.invocation == "" { + return + } + done := make(chan struct{}) + id := t.invocation + go func() { + _ = t.stream.Send( + &tunnel.LifecycleTerminalResponse{Frame: &tunnel.LifecycleTerminalResponse_Cancel{ + Cancel: &tunnel.LifecycleTerminalCancel{InvocationId: id}, + }}, + ) + close(done) + }() + timer := time.NewTimer(time.Second) + defer timer.Stop() + select { + case <-done: + case <-timer.C: + } +} + +func (t *interactiveSession) handleResize() error { + if t.invocation == "" { + return nil + } + r, c, err := t.owner.size() + if err != nil { + return err + } + if r == t.rows && c == t.cols { + return nil + } + t.rows, t.cols = r, c + return t.stream.Send( + &tunnel.LifecycleTerminalResponse{Frame: &tunnel.LifecycleTerminalResponse_Resize{ + Resize: &tunnel.LifecycleTerminalResize{ + InvocationId: t.invocation, + Rows: r, + Columns: c, + }, + }}, + ) +} + +func (t *interactiveSession) handleRead(v terminalRead) error { + if len(v.data) > 0 { + if err := t.stream.Send( + &tunnel.LifecycleTerminalResponse{Frame: &tunnel.LifecycleTerminalResponse_Input{ + Input: &tunnel.LifecycleTerminalInput{InvocationId: t.invocation, Data: v.data}, + }}, + ); err != nil { + return err + } + } + if v.err != nil { + t.sendCancel() + return v.err + } + return nil +} + +func (t *interactiveSession) handleFrame(v terminalReceived) (bool, error) { + if v.err != nil { + t.sendCancel() + return false, v.err + } + if v.frame == nil { + return false, io.ErrUnexpectedEOF + } + return t.dispatchFrame(v.frame) +} + +func (t *interactiveSession) dispatchFrame(frame *tunnel.LifecycleTerminalRequest) (bool, error) { + switch f := frame.Frame.(type) { + case *tunnel.LifecycleTerminalRequest_Begin: + return false, t.begin(f.Begin) + case *tunnel.LifecycleTerminalRequest_Output: + return t.writeOutput(f.Output) + case *tunnel.LifecycleTerminalRequest_Exit: + return false, t.exit(f.Exit) + case *tunnel.LifecycleTerminalRequest_Complete: + return t.complete(f.Complete) + default: + return false, fmt.Errorf("unexpected terminal frame") + } +} + +func (t *interactiveSession) complete(frame *tunnel.LifecycleTerminalComplete) (bool, error) { + if t.invocation != "" || frame == nil { + return false, fmt.Errorf("invalid complete") + } + return true, nil +} + +func (t *interactiveSession) begin(begin *tunnel.LifecycleTerminalBegin) error { + if t.invocation != "" || begin == nil || begin.InvocationId == "" || + begin.Phase != "postStartCommand" { + return fmt.Errorf("invalid begin") + } + if err := t.prepareInput(); err != nil { + return err + } + t.invocation = begin.InvocationId + t.sequence = 0 + t.output = make(chan []byte, 6) + t.outputDone = make(chan error, 1) + writer := t.owner.opts.output + if writer == nil { + writer = t.owner.opts.stdout + } + go terminalOutput(writer, t.output, t.outputDone) + if err := t.stream.Send( + &tunnel.LifecycleTerminalResponse{Frame: &tunnel.LifecycleTerminalResponse_BeginAck{ + BeginAck: &tunnel.LifecycleTerminalBeginAck{ + InvocationId: t.invocation, + Rows: t.rows, + Columns: t.cols, + }, + }}, + ); err != nil { + return err + } + t.startInput() + return nil +} + +func (t *interactiveSession) prepareInput() error { + var err error + t.rows, t.cols, err = t.owner.size() + if err != nil { + return err + } + factory := t.owner.opts.inputFactory + if factory == nil { + factory = terminal.NewInput + } + t.input, err = factory(t.owner.opts.stdin) + if err != nil { + return err + } + t.activateStatus() + if !log.DebugEnabled() { + t.restoreLog = log.SuppressConsoleOutput() + } + t.restore, err = terminal.EnterRaw(t.owner.opts.stdin, t.owner.opts.stdout) + if err != nil { + t.cleanup() + return err + } + return nil +} + +func (t *interactiveSession) activateStatus() { + if t.owner.opts.status != nil { + if t.owner.opts.status.journal != nil { + t.owner.opts.status.journal.begin() + t.journalActive = true + } + t.owner.opts.status.SetActive(true) + } +} + +func (t *interactiveSession) startInput() { + readCtx, cancel := terminalInputContext(t.ctx) + t.inputCancel = cancel + ch := make(chan terminalRead, 1) + t.reads = ch + t.done = make(chan struct{}) + done := t.done + go func(in terminal.Input) { + defer close(done) + for { + b := make([]byte, tunnel.LifecycleTerminalMaxFrame) + n, e := in.Read(b) + select { + case ch <- terminalRead{b[:n], e}: + case <-readCtx.Done(): + return + } + if e != nil { + return + } + } + }(t.input) +} + +func (t *interactiveSession) writeOutput(output *tunnel.LifecycleTerminalOutput) (bool, error) { + if !t.validOutput(output) { + return false, fmt.Errorf("invalid output") + } + t.sequence++ + data := append([]byte(nil), output.Data...) + select { + case t.output <- data: + return false, nil + case e := <-t.outputDone: + return true, e + case <-t.ctx.Done(): + return true, t.ctx.Err() + } +} + +func (t *interactiveSession) validOutput(output *tunnel.LifecycleTerminalOutput) bool { + return output != nil && t.invocation != "" && output.InvocationId == t.invocation && + len(output.Data) <= tunnel.LifecycleTerminalMaxFrame && output.Sequence == t.sequence+1 +} + +func (t *interactiveSession) exit(exit *tunnel.LifecycleTerminalExit) error { + if exit == nil || t.invocation == "" || exit.InvocationId != t.invocation { + return fmt.Errorf("invalid exit") + } + t.stopOutput() + if err := t.drainOutput(); err != nil { + return err + } + t.cleanup() + t.finishJournal() + if t.owner.opts.status != nil { + t.owner.opts.status.SetActive(false) + } + if err := t.stream.Send( + &tunnel.LifecycleTerminalResponse{Frame: &tunnel.LifecycleTerminalResponse_ExitAck{ + ExitAck: &tunnel.LifecycleTerminalExitAck{InvocationId: t.invocation}, + }}, + ); err != nil { + return err + } + t.invocation = "" + return nil +} + +func (t *interactiveSession) drainOutput() error { + timeout := t.owner.opts.exitAckTimeout + if timeout <= 0 { + timeout = tunnel.LifecycleTerminalExitAckTimeout + } + timer := time.NewTimer(timeout) + select { + case err := <-t.outputDone: + timer.Stop() + return err + case <-timer.C: + return fmt.Errorf("terminal output drain timed out") + case <-t.ctx.Done(): + timer.Stop() + return t.ctx.Err() + } +} + +func terminalInputContext(ctx context.Context) (context.Context, context.CancelFunc) { + return context.WithCancel(ctx) +} + +func terminalOutput(out io.Writer, chunks <-chan []byte, done chan<- error) { + for data := range chunks { + for len(data) > 0 { + n, err := out.Write(data) + if err != nil { + done <- err + return + } + if n == 0 { + done <- io.ErrShortWrite + return + } + data = data[n:] + } + } + done <- nil +} + +func validInteractiveTerm(value string) bool { + if len(value) == 0 || len(value) > 128 { + return false + } + for _, c := range value { + if !validInteractiveTermChar(c) { + return false + } + } + return true +} + +func validInteractiveTermChar(c rune) bool { + if asciiRange(c, 'a', 'z') || asciiRange(c, 'A', 'Z') || asciiRange(c, '0', '9') { + return true + } + switch c { + case '-', '_', '.', '+': + return true + default: + return false + } +} + +func asciiRange(c, first, last rune) bool { return c >= first && c <= last } diff --git a/cmd/workspace/up/interactive_post_start_cli_test.go b/cmd/workspace/up/interactive_post_start_cli_test.go new file mode 100644 index 0000000000..27ad3b0e55 --- /dev/null +++ b/cmd/workspace/up/interactive_post_start_cli_test.go @@ -0,0 +1,166 @@ +package up + +import ( + "context" + "os" + "testing" + + "al.essio.dev/pkg/shellescape" + "github.com/devsy-org/devsy/cmd/flags" + client2 "github.com/devsy-org/devsy/pkg/client" + "github.com/devsy-org/devsy/pkg/config" + "github.com/devsy-org/devsy/pkg/flags/names" + "github.com/devsy-org/devsy/pkg/provider" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" +) + +type InteractivePostStartCLISuite struct { + suite.Suite +} + +func TestInteractivePostStartCLISuite(t *testing.T) { + suite.Run(t, new(InteractivePostStartCLISuite)) +} + +func (s *InteractivePostStartCLISuite) TestValidateInteractivePostStartRejectsConflicts() { + tests := []struct { + name string + set func(*UpCmd) + }{ + {name: "detach", set: func(cmd *UpCmd) { cmd.Detach = true }}, + {name: "prebuild", set: func(cmd *UpCmd) { cmd.Prebuild = true }}, + {name: "recovery", set: func(cmd *UpCmd) { cmd.Recovery = true }}, + {name: "skip post start", set: func(cmd *UpCmd) { cmd.SkipPostStart = true }}, + {name: "skip nonblocking", set: func(cmd *UpCmd) { cmd.SkipNonBlockingCommands = true }}, + } + for _, test := range tests { + s.Run(test.name, func() { + t := s.T() + cmd := &UpCmd{InteractivePostStart: true} + test.set(cmd) + require.ErrorContains(t, cmd.validateInteractivePostStart(), "cannot be combined") + }) + } +} + +func (s *InteractivePostStartCLISuite) TestInteractivePostStartFlagDefaultsFalse() { + t := s.T() + root := NewUpCmd(&flags.GlobalFlags{}) + flag := root.Flags().Lookup(names.InteractivePostStart) + require.NotNil(t, flag) + require.Equal(t, "false", flag.DefValue) + value, err := root.Flags().GetBool(names.InteractivePostStart) + require.NoError(t, err) + require.False(t, value) +} + +func (s *InteractivePostStartCLISuite) TestWorkspaceAgentCommandLifecycleGuard() { + t := s.T() + base := workspaceAgentCommand("/agent", "workspace", false, false) + require.NotContains(t, base, "lifecycle-terminal-protocol") + interactive := workspaceAgentCommand("/agent", "workspace", false, true) + require.Contains(t, interactive, shellescape.Quote("--lifecycle-terminal-protocol=1")) + require.Equal(t, base+" "+shellescape.Quote("--lifecycle-terminal-protocol=1"), interactive) +} + +func (s *InteractivePostStartCLISuite) TestDefaultInvocationHasNoLifecycleTerminalRequest() { + t := s.T() + cmd := &UpCmd{} + require.False(t, cmd.InteractivePostStart) + require.Nil(t, cmd.LifecycleTerminal) +} + +func (s *InteractivePostStartCLISuite) TestDispatchRejectsInteractiveUnsupportedRoutes() { + t := s.T() + cmd := &UpCmd{InteractivePostStart: true} + cmd.Platform.Enabled = true + _, err := cmd.dispatchClient(context.Background(), &config.Config{}, nil) + require.ErrorContains(t, err, "platform-managed") + + cmd.Platform.Enabled = false + for _, client := range []client2.BaseWorkspaceClient{ + interactiveProxyClient{}, interactiveDaemonClient{}, + } { + _, err := cmd.dispatchClient(context.Background(), &config.Config{}, client) + require.ErrorContains(t, err, "unsupported") + } +} + +type ( + interactiveProxyClient struct{ client2.ProxyClient } + interactiveDaemonClient struct{ client2.DaemonClient } +) + +func (s *InteractivePostStartCLISuite) TestInteractivePostStartTerminalDimensions() { + t := s.T() + for _, value := range []int{-1, 65536} { + cmd := &UpCmd{CLIOptions: provider.CLIOptions{TerminalRows: value}} + require.ErrorContains(t, cmd.validateInteractivePostStart(), "between 0 and 65535") + cmd = &UpCmd{CLIOptions: provider.CLIOptions{TerminalColumns: value}} + require.ErrorContains(t, cmd.validateInteractivePostStart(), "between 0 and 65535") + } + require.NoError(t, (&UpCmd{}).validateInteractivePostStart()) +} + +func (s *InteractivePostStartCLISuite) TestInteractivePostStartRejectsJSONAndUI() { + t := s.T() + t.Setenv("DEVSY_UI", "") + cmd := &UpCmd{InteractivePostStart: true, GlobalFlags: &flags.GlobalFlags{ResultFormat: "json"}} + require.ErrorContains(t, cmd.validateInteractivePostStart(), "requires plain output") + t.Setenv("DEVSY_UI", "true") + cmd = &UpCmd{InteractivePostStart: true, GlobalFlags: &flags.GlobalFlags{ResultFormat: "plain"}} + require.ErrorContains(t, cmd.validateInteractivePostStart(), "DEVSY_UI") +} + +func (s *InteractivePostStartCLISuite) TestInteractivePostStartCIEnvironmentIsNotTerminalCheck() { + t := s.T() + t.Setenv("CI", "true") + t.Setenv("DEVSY_UI", "") + reader, writer, err := os.Pipe() + require.NoError(t, err) + oldStdin, oldStdout := os.Stdin, os.Stdout + os.Stdin, os.Stdout = reader, writer + t.Cleanup(func() { + os.Stdin, os.Stdout = oldStdin, oldStdout + _ = reader.Close() + _ = writer.Close() + }) + cmd := &UpCmd{ + InteractivePostStart: true, + GlobalFlags: &flags.GlobalFlags{ResultFormat: "plain"}, + } + err = cmd.validateInteractivePostStart() + require.ErrorContains(t, err, "attached stdin and stdout") + require.NotContains(t, err.Error(), "CI") +} + +func (s *InteractivePostStartCLISuite) TestCreateLifecycleTerminalRequest() { + t := s.T() + cmd := &UpCmd{} + client := &lifecycleTerminalRequestClient{} + require.NoError(t, cmd.createLifecycleTerminalRequest(client)) + require.NotNil(t, cmd.LifecycleTerminal) + require.Equal(t, uint32(1), cmd.LifecycleTerminal.ProtocolVersion) + require.NotEmpty(t, cmd.LifecycleTerminal.Nonce) + require.Equal(t, "context", cmd.LifecycleTerminal.Context) + require.Equal(t, "workspace-uid", cmd.LifecycleTerminal.WorkspaceUID) +} + +type lifecycleTerminalRequestClient struct { + client2.BaseWorkspaceClient +} + +func (*lifecycleTerminalRequestClient) Context() string { return "context" } + +func (*lifecycleTerminalRequestClient) WorkspaceConfig() *provider.Workspace { + return &provider.Workspace{UID: "workspace-uid"} +} + +func (s *InteractivePostStartCLISuite) TestMergedOptionsRejectInteractiveConflict() { + t := s.T() + t.Setenv("DEVSY_FLAGS_UP", `{"skipPostStart":true}`) + cmd := &UpCmd{InteractivePostStart: true} + err := cmd.prepareClientEnvironment(nil, nil, context.Background()) + require.ErrorContains(t, err, "cannot be combined") +} diff --git a/cmd/workspace/up/interactive_post_start_test.go b/cmd/workspace/up/interactive_post_start_test.go new file mode 100644 index 0000000000..1db7ab922b --- /dev/null +++ b/cmd/workspace/up/interactive_post_start_test.go @@ -0,0 +1,830 @@ +package up + +import ( + "bytes" + "context" + "errors" + "fmt" + "io" + "os" + "reflect" + "sync" + "testing" + "time" + + "github.com/devsy-org/devsy/cmd/flags" + "github.com/devsy-org/devsy/pkg/agent/tunnel" + client2 "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/pty/ptytest" + "github.com/devsy-org/devsy/pkg/status" + "github.com/devsy-org/devsy/pkg/terminal" + "github.com/devsy-org/devsy/pkg/workspace" + "github.com/stretchr/testify/suite" + "go.uber.org/goleak" + "golang.org/x/term" + "google.golang.org/grpc" +) + +const ( + testInvocationOne = "one" + testInvocationTwo = "two" +) + +type interactiveStream struct { + grpc.ServerStream + ctx context.Context + in chan *tunnel.LifecycleTerminalRequest + out chan *tunnel.LifecycleTerminalResponse + panicBegin bool +} + +func (s *interactiveStream) Context() context.Context { return s.ctx } +func (s *interactiveStream) Send(f *tunnel.LifecycleTerminalResponse) error { + if f.GetBeginAck() != nil && s.panicBegin { + panic("injected send panic") + } + select { + case s.out <- f: + return nil + case <-s.ctx.Done(): + return s.ctx.Err() + } +} + +func (s *interactiveStream) Recv() (*tunnel.LifecycleTerminalRequest, error) { + select { + case f, ok := <-s.in: + if !ok { + return nil, io.EOF + } + return f, nil + case <-s.ctx.Done(): + return nil, s.ctx.Err() + } +} + +type InteractivePostStartSuite struct{ suite.Suite } + +func TestInteractivePostStartSuite(t *testing.T) { suite.Run(t, new(InteractivePostStartSuite)) } + +type interactiveFixture struct { + device *ptytest.PTY + file *os.File + stream *interactiveStream + owner *interactivePostStart + done chan error + cancel context.CancelFunc +} + +func beginFrame() *tunnel.LifecycleTerminalRequest { + return &tunnel.LifecycleTerminalRequest{ + Frame: &tunnel.LifecycleTerminalRequest_Begin{ + Begin: &tunnel.LifecycleTerminalBegin{ + InvocationId: testInvocationOne, + Phase: "postStartCommand", + }, + }, + } +} + +func exitFrame() *tunnel.LifecycleTerminalRequest { + return &tunnel.LifecycleTerminalRequest{ + Frame: &tunnel.LifecycleTerminalRequest_Exit{ + Exit: &tunnel.LifecycleTerminalExit{InvocationId: testInvocationOne, ExitCode: 7}, + }, + } +} + +func (s *InteractivePostStartSuite) TestReadyBeforeRawAndCurrentBeginSize() { + f := s.fixture() + before, err := term.GetState(int(f.file.Fd())) + s.Require().NoError(err) + s.Require().NotNil(s.response(f).GetReady()) + after, err := term.GetState(int(f.file.Fd())) + s.Require().NoError(err) + s.True(reflect.DeepEqual(before, after)) + s.Require().NoError(f.device.Resize(43, 91)) + f.stream.in <- beginFrame() + ack := s.response(f).GetBeginAck() + s.Require().NotNil(ack) + s.Equal(uint32(43), ack.Rows) + s.Equal(uint32(91), ack.Columns) +} + +func (s *InteractivePostStartSuite) TestOutputWithoutNewlineAndInput() { + f := s.fixture() + s.begin(f) + f.stream.in <- &tunnel.LifecycleTerminalRequest{Frame: &tunnel.LifecycleTerminalRequest_Output{ + Output: &tunnel.LifecycleTerminalOutput{InvocationId: testInvocationOne, Sequence: 1, Data: []byte("prompt: ")}, + }} + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + f.device.ExpectMatchContext(ctx, "prompt: ") + f.device.Write('x') + s.Equal([]byte("x"), s.response(f).GetInput().Data) +} + +func (s *InteractivePostStartSuite) TestExitRestoresAndReleasesInput() { + f := s.fixture() + before, err := term.GetState(int(f.file.Fd())) + s.Require().NoError(err) + s.begin(f) + f.stream.in <- exitFrame() + s.Require().NotNil(s.response(f).GetExitAck()) + after, err := term.GetState(int(f.file.Fd())) + s.Require().NoError(err) + s.True(reflect.DeepEqual(before, after)) + f.device.WriteLine("later") + data := make([]byte, 16) + n, err := f.file.Read(data) + s.Require().NoError(err) + s.Equal("later\n", string(data[:n])) +} + +func (s *InteractivePostStartSuite) TestInvalidDimensions() { + owner := newInteractivePostStart(interactivePostStartOptions{stdout: os.Stdout, rows: -1}) + _, _, err := owner.size() + s.Error(err) + owner.opts.rows = 0 + owner.opts.columns = 65536 + _, _, err = owner.size() + s.Error(err) +} + +func (s *InteractivePostStartSuite) TestCancelRestores() { + f := s.fixture() + before, err := term.GetState(int(f.file.Fd())) + s.Require().NoError(err) + s.begin(f) + f.owner.Close() + s.Require().NotNil(s.response(f).GetCancel()) + select { + case err = <-f.done: + s.True(errors.Is(err, context.Canceled)) + f.done <- err + case <-time.After(3 * time.Second): + s.FailNow("cancel timeout") + } + after, err := term.GetState(int(f.file.Fd())) + s.Require().NoError(err) + s.True(reflect.DeepEqual(before, after)) +} + +type testReporter struct { + events chan string + block <-chan struct{} +} + +type stalledReporter struct { + started chan struct{} + release chan struct{} + events chan string + once sync.Once +} + +func (r *stalledReporter) Report(e status.Event) { + r.once.Do(func() { + close(r.started) + <-r.release + }) + r.events <- e.Step +} + +func (s *InteractivePostStartSuite) TestSequentialBeginDuringStalledStatusRender() { + ignore := goleak.IgnoreCurrent() + s.T().Cleanup(func() { goleak.VerifyNone(s.T(), ignore) }) + r := &stalledReporter{ + started: make(chan struct{}), release: make(chan struct{}), events: make(chan string, 3), + } + var release sync.Once + unblock := func() { release.Do(func() { close(r.release) }) } + s.T().Cleanup(unblock) + gate := newInteractiveStatusGate(r, io.Discard, false) + f := s.fixture(func(o *interactivePostStartOptions) { o.status = gate }) + s.begin(f) + gate.Report(status.Event{Step: testInvocationOne}) + gate.Report(status.Event{Step: testInvocationTwo}) + f.stream.in <- exitFrame() + s.waitStatusSignal(r.started) + s.Require().NotNil(s.timedResponse(f, 6*time.Second).GetExitAck()) + next := beginFrame() + next.GetBegin().InvocationId = testInvocationTwo + started := time.Now() + f.stream.in <- next + response := s.timedResponse(f, interactiveRenderWait+time.Second) + s.Require().NotNil(response.GetBeginAck()) + s.Equal(testInvocationTwo, response.GetBeginAck().InvocationId) + s.Less(time.Since(started), interactiveRenderWait+time.Second) + gate.Report(status.Event{Step: "third"}) + unblock() + select { + case event := <-r.events: + s.Equal(testInvocationOne, event) + case <-time.After(time.Second): + s.FailNow("stalled render did not finish") + } + gate.mu.Lock() + idle := gate.idle + gate.mu.Unlock() + s.waitStatusSignal(idle) + s.Empty(r.events) + exit := exitFrame() + exit.GetExit().InvocationId = testInvocationTwo + f.stream.in <- exit + s.Require().NotNil(s.response(f).GetExitAck()) + s.Equal(testInvocationTwo, <-r.events) + s.Equal("third", <-r.events) +} + +func (r *testReporter) Report(e status.Event) { + if r.block != nil { + <-r.block + } + r.events <- e.Step +} + +func (s *InteractivePostStartSuite) TestStatusHeldAndFlushedWithoutBlocking() { + events := make(chan string, 300) + block := make(chan struct{}) + r := &testReporter{events: events, block: block} + var out bytes.Buffer + gate := newInteractiveStatusGate(r, &out, false) + gate.enabled = true + gate.SetActive(true) + for _, v := range []string{"first", "second"} { + gate.Report(status.Event{Step: v}) + } + select { + case <-events: + s.FailNow("rendered during ownership") + default: + } + gate.Report(status.Event{Step: "third"}) + done := make(chan struct{}) + go func() { gate.SetActive(false); close(done) }() + select { + case <-done: + s.FailNow("drain returned before rendering") + case <-time.After(50 * time.Millisecond): + } + close(block) + select { + case <-done: + case <-time.After(time.Second): + s.FailNow("drain timed out") + } + s.Equal("first", <-events) + s.Equal("second", <-events) + s.Equal("third", <-events) + gate.Report(status.Event{Step: "ready"}) + s.Equal("ready", <-events) +} + +type pausedWriter struct { + release chan struct{} + started chan struct{} +} + +func (w *pausedWriter) Write(p []byte) (int, error) { + select { + case w.started <- struct{}{}: + default: + } + <-w.release + return len(p), nil +} + +func (s *InteractivePostStartSuite) TestPausedOutputLongerThanFiveSeconds() { + w := &pausedWriter{release: make(chan struct{}), started: make(chan struct{}, 1)} + f := s.fixture(func(o *interactivePostStartOptions) { o.output = w }) + s.begin(f) + f.stream.in <- &tunnel.LifecycleTerminalRequest{Frame: &tunnel.LifecycleTerminalRequest_Output{ + Output: &tunnel.LifecycleTerminalOutput{InvocationId: testInvocationOne, Sequence: 1, Data: []byte("prompt")}, + }} + <-w.started + f.stream.in <- exitFrame() + select { + case <-f.stream.out: + s.FailNow("early acknowledgement") + case <-time.After(5100 * time.Millisecond): + } + close(w.release) + s.Require().NotNil(s.response(f).GetExitAck()) +} + +func (s *InteractivePostStartSuite) TestStreamErrorRestores() { + f := s.fixture() + before, err := term.GetState(int(f.file.Fd())) + s.Require().NoError(err) + s.begin(f) + close(f.stream.in) + s.Require().NotNil(s.response(f).GetCancel()) + select { + case err = <-f.done: + s.Error(err) + f.done <- err + case <-time.After(3 * time.Second): + s.FailNow("stream error timeout") + } + after, err := term.GetState(int(f.file.Fd())) + s.Require().NoError(err) + s.True(reflect.DeepEqual(before, after)) +} + +func (s *InteractivePostStartSuite) TestNoStatusDoesNotSuppressTerminal() { + f := s.fixture(func(o *interactivePostStartOptions) { + o.status = newInteractiveStatusGate(status.Nop(), io.Discard, true) + }) + s.begin(f) + f.stream.in <- &tunnel.LifecycleTerminalRequest{Frame: &tunnel.LifecycleTerminalRequest_Output{ + Output: &tunnel.LifecycleTerminalOutput{InvocationId: testInvocationOne, Sequence: 1, Data: []byte("visible")}, + }} + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + f.device.ExpectMatchContext(ctx, "visible") +} + +type eofInput struct{} + +func (eofInput) Read([]byte) (int, error) { return 0, io.EOF } +func (eofInput) Close() error { return nil } +func (eofInput) Cancel() bool { return true } +func (s *InteractivePostStartSuite) TestLocalEOFSendsCancel() { + f := s.fixture(func(o *interactivePostStartOptions) { + o.inputFactory = func(*os.File) (terminal.Input, error) { return eofInput{}, nil } + }) + before, err := term.GetState(int(f.file.Fd())) + s.Require().NoError(err) + s.begin(f) + s.Require().NotNil(s.response(f).GetCancel()) + select { + case err = <-f.done: + s.ErrorIs(err, io.EOF) + f.done <- err + case <-time.After(time.Second): + s.FailNow("EOF timeout") + } + after, err := term.GetState(int(f.file.Fd())) + s.Require().NoError(err) + s.True(reflect.DeepEqual(before, after)) +} + +func (s *InteractivePostStartSuite) TestOutputTimeoutRestores() { + w := &pausedWriter{release: make(chan struct{}), started: make(chan struct{}, 1)} + f := s.fixture( + func(o *interactivePostStartOptions) { o.output = w; o.exitAckTimeout = 50 * time.Millisecond }, + ) + before, err := term.GetState(int(f.file.Fd())) + s.Require().NoError(err) + s.begin(f) + f.stream.in <- &tunnel.LifecycleTerminalRequest{Frame: &tunnel.LifecycleTerminalRequest_Output{ + Output: &tunnel.LifecycleTerminalOutput{InvocationId: testInvocationOne, Sequence: 1, Data: []byte("x")}, + }} + <-w.started + f.stream.in <- exitFrame() + select { + case err = <-f.done: + s.ErrorContains(err, "timed out") + f.done <- err + case <-time.After(time.Second): + s.FailNow("timeout not enforced") + } + after, err := term.GetState(int(f.file.Fd())) + s.Require().NoError(err) + s.True(reflect.DeepEqual(before, after)) + close(w.release) +} + +func (s *InteractivePostStartSuite) TestJournalContinuesWhilePresenterHeld() { + events := make(chan string, 4) + j := &testReporter{events: events} + held := make(chan string, 4) + gate := newInteractiveStatusGate(&testReporter{events: held}, io.Discard, false) + reporter := &journalStatusReporter{presenter: gate, journal: j, gate: gate} + reporter.begin() + gate.enabled = true + gate.SetActive(true) + reporter.Report(status.Event{Step: testInvocationOne}) + reporter.Report(status.Event{Step: testInvocationTwo}) + s.Equal(testInvocationOne, <-events) + s.Equal(testInvocationTwo, <-events) + select { + case <-held: + s.FailNow("rendered early") + default: + } + gate.SetActive(false) + s.Equal(testInvocationOne, <-held) + s.Equal(testInvocationTwo, <-held) +} + +func (s *InteractivePostStartSuite) TestPanicRestores() { + f := s.fixture() + before, err := term.GetState(int(f.file.Fd())) + s.Require().NoError(err) + s.Require().NotNil(s.response(f).GetReady()) + f.stream.panicBegin = true + f.stream.in <- beginFrame() + select { + case err = <-f.done: + s.ErrorContains(err, "panic") + f.done <- err + case <-time.After(time.Second): + s.FailNow("panic timeout") + } + after, err := term.GetState(int(f.file.Fd())) + s.Require().NoError(err) + s.True(reflect.DeepEqual(before, after)) +} + +func (s *InteractivePostStartSuite) TestInvalidOutputRestores() { + f := s.fixture() + before, err := term.GetState(int(f.file.Fd())) + s.Require().NoError(err) + s.begin(f) + f.stream.in <- &tunnel.LifecycleTerminalRequest{Frame: &tunnel.LifecycleTerminalRequest_Output{ + Output: &tunnel.LifecycleTerminalOutput{InvocationId: testInvocationOne, Sequence: 2, Data: []byte("bad")}, + }} + select { + case err = <-f.done: + s.ErrorContains(err, "invalid output") + f.done <- err + case <-time.After(time.Second): + s.FailNow("error timeout") + } + after, err := term.GetState(int(f.file.Fd())) + s.Require().NoError(err) + s.True(reflect.DeepEqual(before, after)) +} + +type eventWriter struct{ lines chan string } + +func (w eventWriter) Write(p []byte) (int, error) { w.lines <- string(p); return len(p), nil } +func (s *InteractivePostStartSuite) TestStatusOverflowReportsOmissions() { + events := make(chan string, 256) + lines := make(chan string, 1) + gate := newInteractiveStatusGate( + &testReporter{events: events}, + eventWriter{lines: lines}, + false, + ) + gate.enabled = true + gate.SetActive(true) + for i := range 300 { + gate.Report(status.Event{Step: fmt.Sprint(i)}) + } + gate.SetActive(false) + for i := range 256 { + select { + case event := <-events: + s.Equal(fmt.Sprint(i), event) + case <-time.After(time.Second): + s.FailNow("event timeout") + } + } + select { + case line := <-lines: + s.Equal("44 progress updates omitted\n", line) + case <-time.After(time.Second): + s.FailNow("omission timeout") + } +} + +func (s *InteractivePostStartSuite) TestBlockedJournalDoesNotBlockHandlers() { + events := make(chan string, 300) + block := make(chan struct{}) + gate := newInteractiveStatusGate(status.Nop(), io.Discard, true) + gate.enabled = true + gate.SetActive(true) + reporter := &journalStatusReporter{ + presenter: gate, + journal: &testReporter{events: events, block: block}, + gate: gate, + } + reporter.begin() + done := make(chan struct{}) + go func() { + for i := range 300 { + reporter.Report(status.Event{Step: fmt.Sprint(i)}) + } + close(done) + }() + select { + case <-done: + case <-time.After(time.Second): + s.FailNow("status handler blocked") + } + close(block) + for i := range 300 { + select { + case event := <-events: + s.Equal(fmt.Sprint(i), event) + case <-time.After(time.Second): + s.FailNow("journal timeout") + } + } + gate.SetActive(false) +} + +func (s *InteractivePostStartSuite) TestStatusFlushesBeforeExitAck() { + events := make(chan string, 2) + block := make(chan struct{}) + gate := newInteractiveStatusGate(&testReporter{events: events, block: block}, io.Discard, false) + f := s.fixture(func(o *interactivePostStartOptions) { o.status = gate }) + s.begin(f) + gate.Report(status.Event{Step: testInvocationOne}) + gate.Report(status.Event{Step: testInvocationTwo}) + select { + case <-events: + s.FailNow("rendered while terminal owned") + default: + } + f.stream.in <- exitFrame() + select { + case <-f.stream.out: + close(block) + s.FailNow("acknowledgement before status flush") + case <-time.After(50 * time.Millisecond): + } + close(block) + s.Require().NotNil(s.response(f).GetExitAck()) + s.Equal(testInvocationOne, <-events) + s.Equal(testInvocationTwo, <-events) +} + +func (s *InteractivePostStartSuite) TestNoninteractiveJournalIsSynchronousAndOrdered() { + events := make(chan string, 2) + block := make(chan struct{}) + reporter := &journalStatusReporter{ + presenter: status.Nop(), + journal: &testReporter{events: events, block: block}, + } + finished := make(chan struct{}) + go func() { reporter.Report(status.Event{Step: "first"}); close(finished) }() + select { + case <-finished: + s.FailNow("journal returned before append") + case <-time.After(50 * time.Millisecond): + } + close(block) + select { + case <-finished: + case <-time.After(time.Second): + s.FailNow("journal did not finish") + } + reporter.Report(status.Event{Step: "second"}) + s.Equal("first", <-events) + s.Equal("second", <-events) +} + +func (s *InteractivePostStartSuite) TestCloseWaitsForJournalDrain() { + events := make(chan string, 2) + block := make(chan struct{}) + gate := newInteractiveStatusGate(status.Nop(), io.Discard, true) + reporter := &journalStatusReporter{ + presenter: gate, + journal: &testReporter{events: events, block: block}, + gate: gate, + } + gate.journal = reporter + f := s.fixture(func(o *interactivePostStartOptions) { o.status = gate }) + s.begin(f) + reporter.Report(status.Event{Step: testInvocationOne}) + reporter.Report(status.Event{Step: testInvocationTwo}) + closed := make(chan struct{}) + go func() { f.owner.Close(); close(closed) }() + select { + case <-closed: + s.FailNow("close returned before drain") + case <-time.After(50 * time.Millisecond): + } + close(block) + select { + case <-closed: + case <-time.After(time.Second): + s.FailNow("close did not drain") + } + s.Equal(testInvocationOne, <-events) + s.Equal(testInvocationTwo, <-events) +} + +func (s *InteractivePostStartSuite) TestSequentialBeginAfterJournalDrain() { + events := make(chan string, 1) + block := make(chan struct{}) + gate := newInteractiveStatusGate(status.Nop(), io.Discard, true) + reporter := &journalStatusReporter{ + presenter: gate, + journal: &testReporter{events: events, block: block}, + gate: gate, + } + gate.journal = reporter + f := s.fixture(func(o *interactivePostStartOptions) { o.status = gate }) + s.begin(f) + reporter.Report(status.Event{Step: testInvocationOne}) + f.stream.in <- exitFrame() + select { + case <-f.stream.out: + close(block) + s.FailNow("acknowledgement before journal drain") + case <-time.After(2 * time.Second): + } + close(block) + s.Require().NotNil(s.response(f).GetExitAck()) + s.Equal(testInvocationOne, <-events) + next := beginFrame() + next.GetBegin().InvocationId = testInvocationTwo + started := time.Now() + f.stream.in <- next + select { + case response := <-f.stream.out: + s.Require().NotNil(response.GetBeginAck()) + s.Equal(testInvocationTwo, response.GetBeginAck().InvocationId) + s.Less(time.Since(started), 5*time.Second) + case <-time.After(5 * time.Second): + s.FailNow("second begin acknowledgement timed out") + } +} + +func (s *InteractivePostStartSuite) TestJournalDrainTimeout() { + events := make(chan string, 1) + block := make(chan struct{}) + reporter := &journalStatusReporter{ + presenter: status.Nop(), + journal: &testReporter{events: events, block: block}, + } + reporter.begin() + reporter.Report(status.Event{Step: testInvocationOne}) + ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) + defer cancel() + s.ErrorIs(reporter.end(ctx), context.DeadlineExceeded) + close(block) + s.Equal(testInvocationOne, <-events) +} + +func (s *InteractivePostStartSuite) TestDefaultLoggerSilentDuringOwnership() { + old := os.Stderr + r, w, err := os.Pipe() + s.Require().NoError(err) + os.Stderr = w + log.Init(log.Config{}) + defer func() { os.Stderr = old; log.Init(log.Config{}); _ = r.Close(); _ = w.Close() }() + f := s.fixture() + s.begin(f) + log.Error("must be hidden during ownership") + f.stream.in <- exitFrame() + s.Require().NotNil(s.response(f).GetExitAck()) + log.Error("visible after ownership") + s.Require().NoError(w.Close()) + data, err := io.ReadAll(r) + s.Require().NoError(err) + s.NotContains(string(data), "must be hidden during ownership") + s.Contains(string(data), "visible after ownership") +} + +func (s *InteractivePostStartSuite) TestUnsupportedRoutesBeforePreparation() { + for _, tc := range []struct { + name string + client client2.BaseWorkspaceClient + platform bool + }{ + {name: "platform", platform: true}, + {name: "proxy", client: interactiveProxyClient{}}, + {name: "daemon", client: interactiveDaemonClient{}}, + } { + s.Run(tc.name, func() { + calls := 0 + cmd := &UpCmd{ + GlobalFlags: &flags.GlobalFlags{}, + InteractivePostStart: true, + SecretsFile: "/missing/secrets", + resolveWorkspace: func( + context.Context, *config.Config, workspace.ResolveParams, + ) (client2.BaseWorkspaceClient, error) { + calls++ + return tc.client, nil + }, + } + cmd.Platform.Enabled = tc.platform + _, err := cmd.prepareClient(context.Background(), testConfig(), nil) + s.Require().ErrorContains(err, "unsupported") + if tc.platform { + s.Zero(calls) + } else { + s.Equal(1, calls) + } + }) + } +} + +func (s *InteractivePostStartSuite) TestCloseDrainsStatus() { + events := make(chan string, 2) + block := make(chan struct{}) + gate := newInteractiveStatusGate(&testReporter{events: events, block: block}, io.Discard, false) + owner := newInteractivePostStart(interactivePostStartOptions{status: gate}) + gate.SetActive(true) + gate.Report(status.Event{Step: "held"}) + done := make(chan struct{}) + go func() { owner.Close(); close(done) }() + select { + case <-done: + s.FailNow("close returned before drain") + case <-time.After(50 * time.Millisecond): + } + close(block) + select { + case <-done: + case <-time.After(time.Second): + s.FailNow("close timed out") + } + s.Equal("held", <-events) + gate.Report(status.Event{Step: "ready"}) + s.Equal("ready", <-events) +} + +func (s *InteractivePostStartSuite) fixture( + opts ...func(*interactivePostStartOptions), +) *interactiveFixture { + device := ptytest.New(s.T()) + file, err := os.OpenFile(device.Name(), os.O_RDWR, 0) + s.Require().NoError(err) + ctx, cancel := context.WithCancel(context.Background()) + stream := &interactiveStream{ + ctx: ctx, + in: make(chan *tunnel.LifecycleTerminalRequest, 16), + out: make(chan *tunnel.LifecycleTerminalResponse, 16), + } + options := interactivePostStartOptions{stdin: file, stdout: file} + for _, apply := range opts { + apply(&options) + } + f := &interactiveFixture{ + device: device, + file: file, + stream: stream, + owner: newInteractivePostStart(options), + done: make(chan error, 1), + cancel: cancel, + } + go func() { + defer func() { + if p := recover(); p != nil { + f.done <- fmt.Errorf("panic: %v", p) + } + }() + f.done <- f.owner.Serve(ctx, stream) + }() + s.T().Cleanup(func() { + cancel() + select { + case <-f.done: + case <-time.After(3 * time.Second): + s.T().Error("owner did not stop") + } + _ = file.Close() + }) + return f +} + +func (s *InteractivePostStartSuite) response( + f *interactiveFixture, +) *tunnel.LifecycleTerminalResponse { + s.T().Helper() + select { + case v := <-f.stream.out: + return v + case <-time.After(3 * time.Second): + s.FailNow("response timeout") + return nil + } +} + +func (s *InteractivePostStartSuite) begin(f *interactiveFixture) { + s.Require().NotNil(s.response(f).GetReady()) + f.stream.in <- beginFrame() + s.Require().NotNil(s.response(f).GetBeginAck()) +} + +func (s *InteractivePostStartSuite) waitStatusSignal(ch <-chan struct{}) { + s.T().Helper() + select { + case <-ch: + case <-time.After(time.Second): + s.FailNow("status signal timed out") + } +} + +func (s *InteractivePostStartSuite) timedResponse( + f *interactiveFixture, + timeout time.Duration, +) *tunnel.LifecycleTerminalResponse { + s.T().Helper() + select { + case response := <-f.stream.out: + return response + case <-time.After(timeout): + s.FailNow("response timed out") + return nil + } +} diff --git a/cmd/workspace/up/journal.go b/cmd/workspace/up/journal.go index 4beacc5dc4..11a666f6d6 100644 --- a/cmd/workspace/up/journal.go +++ b/cmd/workspace/up/journal.go @@ -1,6 +1,9 @@ package up import ( + "context" + "sync" + "github.com/devsy-org/devsy/pkg/log" "github.com/devsy-org/devsy/pkg/status" "github.com/devsy-org/devsy/pkg/workspacejournal" @@ -12,8 +15,113 @@ func withJournal(reporter status.Reporter, workspaceID string) status.Reporter { log.Debugf("workspace operation journal unavailable: %v", err) return reporter } - return status.Tee( - reporter, - status.ForPipeline(journal.Reporter(workspaceID), status.PipelineWorkspaceUp), - ) + r := &journalStatusReporter{ + presenter: reporter, + journal: status.ForPipeline(journal.Reporter(workspaceID), status.PipelineWorkspaceUp), + gate: terminalStatusGate(reporter), + } + if r.gate != nil { + r.gate.mu.Lock() + r.gate.journal = r + r.gate.mu.Unlock() + } + return r +} + +func terminalStatusGate(reporter status.Reporter) *interactiveStatusGate { + switch r := reporter.(type) { + case *interactiveStatusGate: + return r + case *journalStatusReporter: + return r.gate + default: + return nil + } +} + +type journalStatusReporter struct { + presenter status.Reporter + journal status.Reporter + gate *interactiveStatusGate + mu sync.Mutex + events []status.Event + active bool + running bool + idle chan struct{} +} + +func (r *journalStatusReporter) Report(event status.Event) { + r.presenter.Report(event) + for { + r.mu.Lock() + if r.active { + r.events = append(r.events, event) + if !r.running { + r.running = true + r.idle = make(chan struct{}) + go r.run() + } + r.mu.Unlock() + return + } + if r.running { + idle := r.idle + r.mu.Unlock() + <-idle + continue + } + r.running = true + r.idle = make(chan struct{}) + r.mu.Unlock() + r.journal.Report(event) + r.mu.Lock() + if len(r.events) > 0 { + go r.run() + } else { + r.running = false + close(r.idle) + } + r.mu.Unlock() + return + } +} + +func (r *journalStatusReporter) begin() { + r.mu.Lock() + r.active = true + r.mu.Unlock() +} + +func (r *journalStatusReporter) end(ctx context.Context) error { + r.mu.Lock() + r.active = false + idle := r.idle + running := r.running + r.mu.Unlock() + if !running { + return nil + } + select { + case <-idle: + return nil + case <-ctx.Done(): + return ctx.Err() + } +} + +func (r *journalStatusReporter) run() { + for { + r.mu.Lock() + if len(r.events) == 0 { + r.running = false + close(r.idle) + r.mu.Unlock() + return + } + event := r.events[0] + r.events[0] = status.Event{} + r.events = r.events[1:] + r.mu.Unlock() + r.journal.Report(event) + } } diff --git a/cmd/workspace/up/status.go b/cmd/workspace/up/status.go index 8d17c43993..a00b610daa 100644 --- a/cmd/workspace/up/status.go +++ b/cmd/workspace/up/status.go @@ -1,7 +1,10 @@ package up import ( + "fmt" "io" + "sync" + "time" config2 "github.com/devsy-org/devsy/pkg/devcontainer/config" "github.com/devsy-org/devsy/pkg/status" @@ -14,7 +17,7 @@ func newStatusReporter( verbose bool, noStatus bool, ) (status.Reporter, error) { - return status.NewCLIReporter(status.CLIOptions{ + reporter, err := status.NewCLIReporter(status.CLIOptions{ Format: resultFormat, Out: out, Prefix: "up", @@ -26,6 +29,10 @@ func newStatusReporter( return config2.WriteStatusJSON(out, e) }, }) + if err != nil { + return nil, err + } + return newInteractiveStatusGate(reporter, out, noStatus), nil } var phaseLabels = map[status.Phase]string{ @@ -44,3 +51,127 @@ var phaseLabels = map[status.Phase]string{ status.PhaseLaunchingIDE: "launching IDE", status.PhaseReady: "ready", } + +type interactiveStatusGate struct { + mu sync.Mutex + renderMu sync.Mutex + active bool + enabled bool + events []status.Event + omitted int + reporter status.Reporter + out io.Writer + quiet bool + running bool + idle chan struct{} + journal *journalStatusReporter +} + +const interactiveRenderWait = 500 * time.Millisecond + +func newInteractiveStatusGate( + reporter status.Reporter, + out io.Writer, + quiet bool, +) *interactiveStatusGate { + return &interactiveStatusGate{reporter: reporter, out: out, quiet: quiet} +} + +func (g *interactiveStatusGate) Report(event status.Event) { + g.mu.Lock() + if !g.enabled { + g.mu.Unlock() + g.renderMu.Lock() + g.mu.Lock() + if !g.enabled { + g.mu.Unlock() + g.reporter.Report(event) + g.renderMu.Unlock() + return + } + g.renderMu.Unlock() + } + if len(g.events) < 256 && g.omitted == 0 { + g.events = append(g.events, event) + } else { + g.omitted++ + } + g.startLocked() + g.mu.Unlock() +} + +func (g *interactiveStatusGate) SetActive(active bool) { + g.mu.Lock() + g.active = active + if active { + g.enabled = true + } else { + g.startLocked() + if !g.running { + g.enabled = false + } + } + idle := g.idle + running := g.running + g.mu.Unlock() + if active { + g.waitForRender() + } else if running { + select { + case <-idle: + case <-time.After(5 * time.Second): + } + } +} + +func (g *interactiveStatusGate) startLocked() { + if !g.active && !g.running && (len(g.events) > 0 || g.omitted > 0) { + g.running = true + g.idle = make(chan struct{}) + go g.run() + } +} + +func (g *interactiveStatusGate) waitForRender() { + done := make(chan struct{}) + go func() { + g.renderMu.Lock() + defer g.renderMu.Unlock() + close(done) + }() + select { + case <-done: + case <-time.After(interactiveRenderWait): + } +} + +func (g *interactiveStatusGate) run() { + for { + g.renderMu.Lock() + g.mu.Lock() + if g.active || (len(g.events) == 0 && g.omitted == 0) { + g.running = false + if !g.active { + g.enabled = false + } + close(g.idle) + g.mu.Unlock() + g.renderMu.Unlock() + return + } + if len(g.events) > 0 { + event := g.events[0] + g.events = g.events[1:] + g.mu.Unlock() + g.reporter.Report(event) + } else { + count := g.omitted + g.omitted = 0 + g.mu.Unlock() + if !g.quiet { + _, _ = fmt.Fprintf(g.out, "%d progress updates omitted\n", count) + } + } + g.renderMu.Unlock() + } +} diff --git a/cmd/workspace/up/up.go b/cmd/workspace/up/up.go index 322a1c8a20..11d6ba10bf 100644 --- a/cmd/workspace/up/up.go +++ b/cmd/workspace/up/up.go @@ -2,6 +2,8 @@ package up import ( "context" + "crypto/rand" + "encoding/hex" "errors" "fmt" "io" @@ -11,6 +13,7 @@ import ( "syscall" "github.com/devsy-org/devsy/cmd/flags" + "github.com/devsy-org/devsy/pkg/agent/tunnel" "github.com/devsy-org/devsy/pkg/agent/tunnelserver" client2 "github.com/devsy-org/devsy/pkg/client" "github.com/devsy-org/devsy/pkg/clierr" @@ -26,6 +29,7 @@ import ( "github.com/devsy-org/devsy/pkg/status" "github.com/devsy-org/devsy/pkg/task" "github.com/devsy-org/devsy/pkg/telemetry" + "github.com/devsy-org/devsy/pkg/terminal" "github.com/devsy-org/devsy/pkg/util" "github.com/devsy-org/devsy/pkg/workspace" "github.com/spf13/cobra" @@ -67,7 +71,8 @@ type UpCmd struct { // Read via Changed() so unset is distinguishable from explicit false. pullFromInsideContainerFlag bool // See cmd.runDetached. - Detach bool + Detach bool + InteractivePostStart bool // Set only on the detached child re-exec. taskID string workerTask *task.Task @@ -125,6 +130,11 @@ func RunHeadless( client client2.BaseWorkspaceClient, opts HeadlessOptions, ) (*config2.Result, error) { + if opts.CLIOptions.LifecycleTerminal != nil { + return nil, fmt.Errorf( + "interactive lifecycle terminals are not supported by headless execution", + ) + } gCopy := *opts.GlobalFlags cmd := &UpCmd{ GlobalFlags: &gCopy, @@ -290,6 +300,10 @@ func (cmd *UpCmd) runWorkspace( devsyConfig *config.Config, client client2.BaseWorkspaceClient, ) error { + if err := cmd.validateInteractiveRoute(client); err != nil { + return err + } + cmd.LifecycleTerminal = nil cmd.prepareWorkspace(client) emitJSON, out, err := cmd.configureRun(client.Workspace()) @@ -297,6 +311,11 @@ func (cmd *UpCmd) runWorkspace( return err } + if cmd.InteractivePostStart { + if err := cmd.createLifecycleTerminalRequest(client); err != nil { + return err + } + } t, err := cmd.setUpTask(client, emitJSON, out) if err != nil { return err @@ -323,6 +342,24 @@ func (cmd *UpCmd) runWorkspace( return succeedTask(t, wctx.result) } +func (cmd *UpCmd) createLifecycleTerminalRequest(client client2.BaseWorkspaceClient) error { + workspace := client.WorkspaceConfig() + if workspace == nil || workspace.UID == "" { + return fmt.Errorf("--interactive-post-start requires a resolved workspace UID") + } + nonce := make([]byte, 32) + if _, err := rand.Read(nonce); err != nil { + return fmt.Errorf("create lifecycle terminal nonce: %w", err) + } + cmd.LifecycleTerminal = &provider2.LifecycleTerminalRequest{ + ProtocolVersion: tunnel.LifecycleTerminalProtocolVersion, + Nonce: hex.EncodeToString(nonce), + Context: client.Context(), + WorkspaceUID: workspace.UID, + } + return nil +} + func (cmd *UpCmd) configureRun(workspaceID string) (bool, io.Writer, error) { mode, err := output.ResolveMode(cmd.ResultFormat) if err != nil { @@ -807,6 +844,7 @@ func watchForForceShutdown(ctx context.Context, done <-chan struct{}) { signal.Notify(signals, shutdownSignals...) select { case <-signals: + terminal.RestoreAll() os.Exit(1) // second signal - force shutdown case <-done: signal.Stop(signals) diff --git a/cmd/workspace/up/up_client.go b/cmd/workspace/up/up_client.go index fc5296436d..d019dee78b 100644 --- a/cmd/workspace/up/up_client.go +++ b/cmd/workspace/up/up_client.go @@ -66,6 +66,9 @@ func (cmd *UpCmd) prepareClient( devsyConfig *config.Config, args []string, ) (client2.BaseWorkspaceClient, error) { + if err := cmd.validateInteractiveRoute(nil); err != nil { + return nil, err + } if err := cmd.prepareClientEnvironment(devsyConfig, args, ctx); err != nil { return nil, err } @@ -101,6 +104,9 @@ func (cmd *UpCmd) prepareClient( return nil, err } + if err := cmd.validateInteractiveRoute(client); err != nil { + return nil, err + } if err := cmd.postResolveWorkspace(ctx, devsyConfig, client, existed); err != nil { return nil, err } @@ -287,6 +293,14 @@ func (cmd *UpCmd) prepareClientEnvironment( if err := mergeDevsyUpOptions(&cmd.CLIOptions); err != nil { return err } + if cmd.InteractivePostStart { + if err := cmd.validateInteractiveFlagConflicts(); err != nil { + return err + } + } + if err := cmd.validateInteractiveRoute(nil); err != nil { + return err + } if err := workspace2.ValidateID(cmd.ID); err != nil { return err } diff --git a/cmd/workspace/up/up_flags.go b/cmd/workspace/up/up_flags.go index 60a22e8aa1..0cc32cee3c 100644 --- a/cmd/workspace/up/up_flags.go +++ b/cmd/workspace/up/up_flags.go @@ -95,6 +95,12 @@ func (cmd *UpCmd) registerBuildFlags(upCmd *cobra.Command) { func (cmd *UpCmd) registerLifecycleFlags(upCmd *cobra.Command) { flags.Add(upCmd, + flags.Bool( + &cmd.InteractivePostStart, + names.InteractivePostStart, + false, + "Attach the terminal to postStartCommand; requires an attached terminal and runs post-start in the foreground", + ), flags.Int(&cmd.TerminalColumns, names.TerminalColumns, 0, "Terminal column count for lifecycle scripts"), flags.Int(&cmd.TerminalRows, names.TerminalRows, 0, diff --git a/cmd/workspace/up/up_validate.go b/cmd/workspace/up/up_validate.go index e851d39177..441ac9c518 100644 --- a/cmd/workspace/up/up_validate.go +++ b/cmd/workspace/up/up_validate.go @@ -2,7 +2,9 @@ package up import ( "fmt" + "os" "path/filepath" + "slices" "strconv" "strings" @@ -11,6 +13,8 @@ import ( config2 "github.com/devsy-org/devsy/pkg/devcontainer/config" "github.com/devsy-org/devsy/pkg/flags/names" "github.com/devsy-org/devsy/pkg/ide/opener" + "github.com/devsy-org/devsy/pkg/output" + "github.com/devsy-org/devsy/pkg/terminal" ) const ( @@ -23,6 +27,9 @@ const ( ) func (cmd *UpCmd) validate() error { + if err := cmd.validateInteractivePostStart(); err != nil { + return err + } cmd.applySkipLaunchIDEDefault() if err := devcontainer.ResolveSourceSpec(&cmd.CLIOptions); err != nil { @@ -50,6 +57,65 @@ func (cmd *UpCmd) validate() error { return validateRemoteUserUID(cmd.UpdateRemoteUserUIDDefault) } +func (cmd *UpCmd) validateInteractivePostStart() error { + if err := cmd.validateTerminalDimensions(); err != nil { + return err + } + if !cmd.InteractivePostStart { + return nil + } + if err := cmd.validateInteractiveRoute(nil); err != nil { + return err + } + if err := cmd.validateInteractiveFlagConflicts(); err != nil { + return err + } + return cmd.validateInteractiveTerminal() +} + +func (cmd *UpCmd) validateTerminalDimensions() error { + if cmd.TerminalRows < 0 || cmd.TerminalRows > 65535 || cmd.TerminalColumns < 0 || + cmd.TerminalColumns > 65535 { + return fmt.Errorf("--terminal-rows and --terminal-columns must be between 0 and 65535") + } + return nil +} + +func (cmd *UpCmd) validateInteractiveFlagConflicts() error { + if cmd.Detach || cmd.Prebuild || cmd.Recovery || cmd.SkipPostStart || + cmd.SkipNonBlockingCommands { + return fmt.Errorf( + "--interactive-post-start cannot be combined with --detach, --prebuild, " + + "--recovery, --skip-post-start, or --skip-non-blocking-commands", + ) + } + return nil +} + +func (cmd *UpCmd) validateInteractiveTerminal() error { + if slices.Contains(os.Environ(), "DEVSY_UI=true") { + return fmt.Errorf( + "--interactive-post-start requires an attached terminal and is unavailable in DEVSY_UI mode", + ) + } + mode, err := output.ResolveMode(cmd.ResultFormat) + if err != nil { + return err + } + if mode == output.ModeJSON { + return fmt.Errorf( + "--interactive-post-start requires plain output; JSON result output cannot share the terminal", + ) + } + if err := terminal.Attached(os.Stdin, os.Stdout); err != nil { + return fmt.Errorf( + "--interactive-post-start requires attached stdin and stdout terminals: %w", + err, + ) + } + return nil +} + // installIDE gates the container-side IDE server download on IDE name alone, // not IDELaunch, so skipping launch without also defaulting IDE to none // still downloads an unwanted binary. diff --git a/e2e/tests/up/interactive_post_start.go b/e2e/tests/up/interactive_post_start.go new file mode 100644 index 0000000000..580df3c952 --- /dev/null +++ b/e2e/tests/up/interactive_post_start.go @@ -0,0 +1,180 @@ +package up + +import ( + "bytes" + "context" + "fmt" + "io" + "os" + "os/exec" + "path/filepath" + "strings" + "sync" + + "github.com/creack/pty" + "github.com/devsy-org/devsy/e2e/framework" + "github.com/devsy-org/devsy/pkg/workspace" + "github.com/onsi/ginkgo/v2" + "github.com/onsi/gomega" +) + +const interactivePostStartFlag = "--interactive-post-start" + +type terminalOutputBuffer struct { + mu sync.Mutex + buf bytes.Buffer +} + +func (b *terminalOutputBuffer) Write(p []byte) (int, error) { + b.mu.Lock() + defer b.mu.Unlock() + return b.buf.Write(p) +} + +func (b *terminalOutputBuffer) String() string { + b.mu.Lock() + defer b.mu.Unlock() + return b.buf.String() +} + +type interactiveUpOptions struct { + prompt bool + args []string +} + +func interactiveUp( + ctx context.Context, + f *framework.Framework, + dir string, + opts interactiveUpOptions, +) string { + argv := append( + []string{cmdWorkspace, "up", dir, flagIDEOption, kubernetesIgnoreIDE}, + opts.args...) + path := filepath.Clean(filepath.Join(f.DevsyBinDir, f.DevsyBinName)) + cmd := exec.CommandContext(ctx, "/dev/null") + cmd.Path = path + cmd.Args = append([]string{path}, argv...) + terminal, err := pty.Start(cmd) + framework.ExpectNoError(err) + defer func() { _ = terminal.Close() }() + defer func() { _ = cmd.Process.Kill() }() + var output terminalOutputBuffer + done := make(chan struct{}) + go func() { + _, _ = io.Copy(&output, terminal) + close(done) + }() + waited := make(chan error, 1) + go func() { waited <- cmd.Wait() }() + if opts.prompt { + gomega.Eventually(func() bool { + select { + case err := <-waited: + <-done + ginkgo.Fail( + fmt.Sprintf("up exited before prompt: %v; output: %s", err, output.String()), + ) + default: + } + return strings.Contains(output.String(), "Name:") + }).WithContext(ctx).WithTimeout(framework.TimeoutModerate()).Should(gomega.BeTrue(), "up output: %s", output.String()) + _, err = terminal.Write([]byte("Devsy\n")) + framework.ExpectNoError(err) + } + err = <-waited + <-done + framework.ExpectNoError(err, "up output: %s", output.String()) + return output.String() +} + +var _ = ginkgo.Describe("interactive post start", ginkgo.Label("up-provider-docker"), func() { + var f *framework.Framework + var initialDir string + ginkgo.BeforeEach(func() { + var err error + initialDir, err = os.Getwd() + framework.ExpectNoError(err) + f, err = framework.SetupDockerProvider(filepath.Join(initialDir, "bin"), dockerValue) + framework.ExpectNoError(err) + }) + + ginkgo.It("accepts input and prompts once per start", func(ctx context.Context) { + dir, err := setupWorkspace("tests/up/testdata/interactive-post-start", initialDir, f) + framework.ExpectNoError(err) + out := interactiveUp( + ctx, + f, + dir, + interactiveUpOptions{prompt: true, args: []string{interactivePostStartFlag}}, + ) + gomega.Expect(strings.Count(out, "Name:")).To(gomega.Equal(1)) + result, err := f.DevsySSH(ctx, dir, "cat /tmp/interactive-post-start-result") + framework.ExpectNoError(err) + gomega.Expect(strings.TrimSpace(result)).To(gomega.Equal("Hello, Devsy")) + out = interactiveUp(ctx, f, dir, interactiveUpOptions{}) + gomega.Expect(out).NotTo(gomega.ContainSubstring("Name:")) + framework.ExpectNoError(f.DevsyWorkspaceStop(ctx, dir)) + out = interactiveUp( + ctx, + f, + dir, + interactiveUpOptions{prompt: true, args: []string{interactivePostStartFlag}}, + ) + gomega.Expect(strings.Count(out, "Name:")).To(gomega.Equal(1)) + }, ginkgo.SpecTimeout(framework.TimeoutLong())) + + ginkgo.It("provides terminal stdin and stdout", func(ctx context.Context) { + dir, err := setupWorkspace("tests/up/testdata/interactive-post-start", initialDir, f) + framework.ExpectNoError(err) + config := `{"image":"mcr.microsoft.com/devcontainers/base:debian",` + + `"postStartCommand":["bash","-c",` + + `"if test -t 0 && test -t 1; then echo yes > /tmp/interactive-tty; else echo no > /tmp/interactive-tty; fi"]}` + framework.ExpectNoError( + os.WriteFile( + filepath.Join(dir, ".devcontainer", "devcontainer.json"), + []byte(config), + 0o600, + ), + ) + interactiveUp(ctx, f, dir, interactiveUpOptions{args: []string{interactivePostStartFlag}}) + result, err := f.DevsySSH(ctx, dir, "cat /tmp/interactive-tty") + framework.ExpectNoError(err) + gomega.Expect(strings.TrimSpace(result)).To(gomega.Equal("yes")) + }, ginkgo.SpecTimeout(framework.TimeoutLong())) + + ginkgo.It( + "rejects detach and missing terminals before creating a workspace", + func(ctx context.Context) { + dir := framework.CopyTestdata("tests/up/testdata/interactive-post-start", initialDir) + for _, tc := range []struct { + args []string + message string + }{ + {[]string{interactivePostStartFlag, "--detach"}, "--interactive-post-start cannot be combined with --detach"}, + {[]string{interactivePostStartFlag}, "--interactive-post-start requires attached stdin and stdout terminals"}, + } { + args := append( + []string{ + cmdWorkspace, + "up", + dir, + flagIDEOption, + kubernetesIgnoreIDE, + "--result-format", + "plain", + }, + tc.args...) + stdout, stderr, err := f.ExecCommandCapture(ctx, args) + gomega.Expect(err).To(gomega.HaveOccurred()) + gomega.Expect(stdout + stderr).To(gomega.ContainSubstring(tc.message)) + workspaces, err := f.DevsyListParsed(ctx) + framework.ExpectNoError(err) + for _, w := range workspaces { + gomega.Expect(w.ID).NotTo(gomega.Equal(workspace.ToID(dir))) + } + } + }, + ginkgo.SpecTimeout(framework.TimeoutShort()), + ) +}) diff --git a/e2e/tests/up/testdata/interactive-post-start/.devcontainer/devcontainer.json b/e2e/tests/up/testdata/interactive-post-start/.devcontainer/devcontainer.json new file mode 100644 index 0000000000..d9797a70f4 --- /dev/null +++ b/e2e/tests/up/testdata/interactive-post-start/.devcontainer/devcontainer.json @@ -0,0 +1,8 @@ +{ + "image": "mcr.microsoft.com/devcontainers/base:debian", + "postStartCommand": [ + "bash", + "-c", + "read -r -p 'Name: ' name && echo \"Hello, $name\" > /tmp/interactive-post-start-result" + ] +} diff --git a/go.mod b/go.mod index 7774914004..9ee012296b 100644 --- a/go.mod +++ b/go.mod @@ -396,7 +396,7 @@ require ( github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect github.com/morikuni/aec v1.1.0 // indirect github.com/mr-tron/base58 v1.2.0 // indirect - github.com/muesli/cancelreader v0.2.2 // indirect + github.com/muesli/cancelreader v0.2.2 github.com/muesli/mango v0.2.0 // indirect github.com/muesli/mango-cobra v1.3.0 // indirect github.com/muesli/mango-pflag v0.1.0 // indirect diff --git a/hack/ci/suites.yaml b/hack/ci/suites.yaml index c6e4a2cdc1..5f62bbfa2f 100644 --- a/hack/ci/suites.yaml +++ b/hack/ci/suites.yaml @@ -74,7 +74,7 @@ unit: - name: windows-pty description: native Windows pseudoconsole and PTY test helpers os: [windows] - packages: [./pkg/pty, ./pkg/pty/ptytest] + packages: [./pkg/pty, ./pkg/pty/ptytest, ./pkg/terminal] require_tests: [TestStartWindowsSuite, TestPtytestSuite] args: [-race, -short, -count=1, -timeout=5m] diff --git a/pkg/agent/persist_test.go b/pkg/agent/persist_test.go index a8904fe727..6e6fdde9b1 100644 --- a/pkg/agent/persist_test.go +++ b/pkg/agent/persist_test.go @@ -41,9 +41,17 @@ func TestPersistAgentWorkspaceInfo_RequiresOrigin(t *testing.T) { func TestPersistAgentWorkspaceInfo_DoesNotPersistCLIOptions(t *testing.T) { dir := t.TempDir() info := &provider2.AgentWorkspaceInfo{ - Origin: dir, - Workspace: &provider2.Workspace{ID: "ws-test"}, - CLIOptions: provider2.CLIOptions{DaemonInterval: "3s"}, + Origin: dir, + Workspace: &provider2.Workspace{ID: "ws-test"}, + CLIOptions: provider2.CLIOptions{ + DaemonInterval: "3s", + LifecycleTerminal: &provider2.LifecycleTerminalRequest{ + ProtocolVersion: 1, + Nonce: "ephemeral-nonce", + Context: "ctx", + WorkspaceUID: "uid", + }, + }, } require.NoError(t, PersistAgentWorkspaceInfo(info)) @@ -52,5 +60,15 @@ func TestPersistAgentWorkspaceInfo_DoesNotPersistCLIOptions(t *testing.T) { require.NoError(t, err) assert.Equal(t, "ws-test", got.Workspace.ID) assert.Empty(t, got.CLIOptions.DaemonInterval, "CLIOptions must not be persisted") + assert.Nil( + t, + got.CLIOptions.LifecycleTerminal, + "lifecycle terminal requests must not be persisted", + ) + assert.NotNil( + t, + info.CLIOptions.LifecycleTerminal, + "cloning must not mutate the caller's options", + ) assert.Equal(t, "3s", info.CLIOptions.DaemonInterval, "caller's struct must not be mutated") } diff --git a/pkg/agent/tunnel/lifecycle_terminal.go b/pkg/agent/tunnel/lifecycle_terminal.go new file mode 100644 index 0000000000..0f593a71c8 --- /dev/null +++ b/pkg/agent/tunnel/lifecycle_terminal.go @@ -0,0 +1,44 @@ +package tunnel + +import ( + "context" + "io" + "time" +) + +const ( + LifecycleTerminalProtocolVersion uint32 = 1 + LifecycleTerminalMaxFrame = 32 << 10 + LifecycleTerminalHandshakeTimeout = 5 * time.Second + LifecycleTerminalExitAckTimeout = 30 * time.Second +) + +type TerminalWindow struct { + Term string + Rows, Columns uint16 +} + +type TerminalInvocation struct { + ID, Phase, Key string + Index int +} + +type TerminalExit struct { + ExitCode int + Signal, StartError string +} + +type TerminalSession interface { + Begin(ctx context.Context, inv TerminalInvocation) (TerminalAttachment, error) + Close(ctx context.Context) error +} + +type TerminalAttachment interface { + Window() TerminalWindow + Output() io.Writer + Abort() + Input() io.ReadCloser + Resizes() <-chan TerminalWindow + Canceled() <-chan struct{} + Exit(ctx context.Context, exit TerminalExit) error +} diff --git a/pkg/agent/tunnel/tunnel.pb.go b/pkg/agent/tunnel/tunnel.pb.go index bc2aa164f9..99062df2af 100644 --- a/pkg/agent/tunnel/tunnel.pb.go +++ b/pkg/agent/tunnel/tunnel.pb.go @@ -939,6 +939,918 @@ func (*Empty) Descriptor() ([]byte, []int) { return file_tunnel_proto_rawDescGZIP(), []int{14} } +type LifecycleTerminalRequest struct { + state protoimpl.MessageState `protogen:"open.v1"` + // Types that are valid to be assigned to Frame: + // + // *LifecycleTerminalRequest_Open + // *LifecycleTerminalRequest_Begin + // *LifecycleTerminalRequest_Output + // *LifecycleTerminalRequest_Exit + // *LifecycleTerminalRequest_Complete + Frame isLifecycleTerminalRequest_Frame `protobuf_oneof:"frame"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *LifecycleTerminalRequest) Reset() { + *x = LifecycleTerminalRequest{} + mi := &file_tunnel_proto_msgTypes[15] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *LifecycleTerminalRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*LifecycleTerminalRequest) ProtoMessage() {} + +func (x *LifecycleTerminalRequest) ProtoReflect() protoreflect.Message { + mi := &file_tunnel_proto_msgTypes[15] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use LifecycleTerminalRequest.ProtoReflect.Descriptor instead. +func (*LifecycleTerminalRequest) Descriptor() ([]byte, []int) { + return file_tunnel_proto_rawDescGZIP(), []int{15} +} + +func (x *LifecycleTerminalRequest) GetFrame() isLifecycleTerminalRequest_Frame { + if x != nil { + return x.Frame + } + return nil +} + +func (x *LifecycleTerminalRequest) GetOpen() *LifecycleTerminalOpen { + if x != nil { + if x, ok := x.Frame.(*LifecycleTerminalRequest_Open); ok { + return x.Open + } + } + return nil +} + +func (x *LifecycleTerminalRequest) GetBegin() *LifecycleTerminalBegin { + if x != nil { + if x, ok := x.Frame.(*LifecycleTerminalRequest_Begin); ok { + return x.Begin + } + } + return nil +} + +func (x *LifecycleTerminalRequest) GetOutput() *LifecycleTerminalOutput { + if x != nil { + if x, ok := x.Frame.(*LifecycleTerminalRequest_Output); ok { + return x.Output + } + } + return nil +} + +func (x *LifecycleTerminalRequest) GetExit() *LifecycleTerminalExit { + if x != nil { + if x, ok := x.Frame.(*LifecycleTerminalRequest_Exit); ok { + return x.Exit + } + } + return nil +} + +func (x *LifecycleTerminalRequest) GetComplete() *LifecycleTerminalComplete { + if x != nil { + if x, ok := x.Frame.(*LifecycleTerminalRequest_Complete); ok { + return x.Complete + } + } + return nil +} + +type isLifecycleTerminalRequest_Frame interface { + isLifecycleTerminalRequest_Frame() +} + +type LifecycleTerminalRequest_Open struct { + Open *LifecycleTerminalOpen `protobuf:"bytes,1,opt,name=open,proto3,oneof"` +} + +type LifecycleTerminalRequest_Begin struct { + Begin *LifecycleTerminalBegin `protobuf:"bytes,2,opt,name=begin,proto3,oneof"` +} + +type LifecycleTerminalRequest_Output struct { + Output *LifecycleTerminalOutput `protobuf:"bytes,3,opt,name=output,proto3,oneof"` +} + +type LifecycleTerminalRequest_Exit struct { + Exit *LifecycleTerminalExit `protobuf:"bytes,4,opt,name=exit,proto3,oneof"` +} + +type LifecycleTerminalRequest_Complete struct { + Complete *LifecycleTerminalComplete `protobuf:"bytes,5,opt,name=complete,proto3,oneof"` +} + +func (*LifecycleTerminalRequest_Open) isLifecycleTerminalRequest_Frame() {} + +func (*LifecycleTerminalRequest_Begin) isLifecycleTerminalRequest_Frame() {} + +func (*LifecycleTerminalRequest_Output) isLifecycleTerminalRequest_Frame() {} + +func (*LifecycleTerminalRequest_Exit) isLifecycleTerminalRequest_Frame() {} + +func (*LifecycleTerminalRequest_Complete) isLifecycleTerminalRequest_Frame() {} + +type LifecycleTerminalResponse struct { + state protoimpl.MessageState `protogen:"open.v1"` + // Types that are valid to be assigned to Frame: + // + // *LifecycleTerminalResponse_Ready + // *LifecycleTerminalResponse_BeginAck + // *LifecycleTerminalResponse_Input + // *LifecycleTerminalResponse_Resize + // *LifecycleTerminalResponse_Cancel + // *LifecycleTerminalResponse_ExitAck + Frame isLifecycleTerminalResponse_Frame `protobuf_oneof:"frame"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *LifecycleTerminalResponse) Reset() { + *x = LifecycleTerminalResponse{} + mi := &file_tunnel_proto_msgTypes[16] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *LifecycleTerminalResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*LifecycleTerminalResponse) ProtoMessage() {} + +func (x *LifecycleTerminalResponse) ProtoReflect() protoreflect.Message { + mi := &file_tunnel_proto_msgTypes[16] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use LifecycleTerminalResponse.ProtoReflect.Descriptor instead. +func (*LifecycleTerminalResponse) Descriptor() ([]byte, []int) { + return file_tunnel_proto_rawDescGZIP(), []int{16} +} + +func (x *LifecycleTerminalResponse) GetFrame() isLifecycleTerminalResponse_Frame { + if x != nil { + return x.Frame + } + return nil +} + +func (x *LifecycleTerminalResponse) GetReady() *LifecycleTerminalReady { + if x != nil { + if x, ok := x.Frame.(*LifecycleTerminalResponse_Ready); ok { + return x.Ready + } + } + return nil +} + +func (x *LifecycleTerminalResponse) GetBeginAck() *LifecycleTerminalBeginAck { + if x != nil { + if x, ok := x.Frame.(*LifecycleTerminalResponse_BeginAck); ok { + return x.BeginAck + } + } + return nil +} + +func (x *LifecycleTerminalResponse) GetInput() *LifecycleTerminalInput { + if x != nil { + if x, ok := x.Frame.(*LifecycleTerminalResponse_Input); ok { + return x.Input + } + } + return nil +} + +func (x *LifecycleTerminalResponse) GetResize() *LifecycleTerminalResize { + if x != nil { + if x, ok := x.Frame.(*LifecycleTerminalResponse_Resize); ok { + return x.Resize + } + } + return nil +} + +func (x *LifecycleTerminalResponse) GetCancel() *LifecycleTerminalCancel { + if x != nil { + if x, ok := x.Frame.(*LifecycleTerminalResponse_Cancel); ok { + return x.Cancel + } + } + return nil +} + +func (x *LifecycleTerminalResponse) GetExitAck() *LifecycleTerminalExitAck { + if x != nil { + if x, ok := x.Frame.(*LifecycleTerminalResponse_ExitAck); ok { + return x.ExitAck + } + } + return nil +} + +type isLifecycleTerminalResponse_Frame interface { + isLifecycleTerminalResponse_Frame() +} + +type LifecycleTerminalResponse_Ready struct { + Ready *LifecycleTerminalReady `protobuf:"bytes,1,opt,name=ready,proto3,oneof"` +} + +type LifecycleTerminalResponse_BeginAck struct { + BeginAck *LifecycleTerminalBeginAck `protobuf:"bytes,2,opt,name=begin_ack,json=beginAck,proto3,oneof"` +} + +type LifecycleTerminalResponse_Input struct { + Input *LifecycleTerminalInput `protobuf:"bytes,3,opt,name=input,proto3,oneof"` +} + +type LifecycleTerminalResponse_Resize struct { + Resize *LifecycleTerminalResize `protobuf:"bytes,4,opt,name=resize,proto3,oneof"` +} + +type LifecycleTerminalResponse_Cancel struct { + Cancel *LifecycleTerminalCancel `protobuf:"bytes,5,opt,name=cancel,proto3,oneof"` +} + +type LifecycleTerminalResponse_ExitAck struct { + ExitAck *LifecycleTerminalExitAck `protobuf:"bytes,6,opt,name=exit_ack,json=exitAck,proto3,oneof"` +} + +func (*LifecycleTerminalResponse_Ready) isLifecycleTerminalResponse_Frame() {} + +func (*LifecycleTerminalResponse_BeginAck) isLifecycleTerminalResponse_Frame() {} + +func (*LifecycleTerminalResponse_Input) isLifecycleTerminalResponse_Frame() {} + +func (*LifecycleTerminalResponse_Resize) isLifecycleTerminalResponse_Frame() {} + +func (*LifecycleTerminalResponse_Cancel) isLifecycleTerminalResponse_Frame() {} + +func (*LifecycleTerminalResponse_ExitAck) isLifecycleTerminalResponse_Frame() {} + +type LifecycleTerminalOpen struct { + state protoimpl.MessageState `protogen:"open.v1"` + ProtocolVersion uint32 `protobuf:"varint,1,opt,name=protocol_version,json=protocolVersion,proto3" json:"protocol_version,omitempty"` + WorkspaceUid string `protobuf:"bytes,2,opt,name=workspace_uid,json=workspaceUid,proto3" json:"workspace_uid,omitempty"` + Context string `protobuf:"bytes,3,opt,name=context,proto3" json:"context,omitempty"` + Nonce string `protobuf:"bytes,4,opt,name=nonce,proto3" json:"nonce,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *LifecycleTerminalOpen) Reset() { + *x = LifecycleTerminalOpen{} + mi := &file_tunnel_proto_msgTypes[17] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *LifecycleTerminalOpen) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*LifecycleTerminalOpen) ProtoMessage() {} + +func (x *LifecycleTerminalOpen) ProtoReflect() protoreflect.Message { + mi := &file_tunnel_proto_msgTypes[17] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use LifecycleTerminalOpen.ProtoReflect.Descriptor instead. +func (*LifecycleTerminalOpen) Descriptor() ([]byte, []int) { + return file_tunnel_proto_rawDescGZIP(), []int{17} +} + +func (x *LifecycleTerminalOpen) GetProtocolVersion() uint32 { + if x != nil { + return x.ProtocolVersion + } + return 0 +} + +func (x *LifecycleTerminalOpen) GetWorkspaceUid() string { + if x != nil { + return x.WorkspaceUid + } + return "" +} + +func (x *LifecycleTerminalOpen) GetContext() string { + if x != nil { + return x.Context + } + return "" +} + +func (x *LifecycleTerminalOpen) GetNonce() string { + if x != nil { + return x.Nonce + } + return "" +} + +type LifecycleTerminalReady struct { + state protoimpl.MessageState `protogen:"open.v1"` + ProtocolVersion uint32 `protobuf:"varint,1,opt,name=protocol_version,json=protocolVersion,proto3" json:"protocol_version,omitempty"` + Term string `protobuf:"bytes,2,opt,name=term,proto3" json:"term,omitempty"` + Rows uint32 `protobuf:"varint,3,opt,name=rows,proto3" json:"rows,omitempty"` + Columns uint32 `protobuf:"varint,4,opt,name=columns,proto3" json:"columns,omitempty"` + Input bool `protobuf:"varint,5,opt,name=input,proto3" json:"input,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *LifecycleTerminalReady) Reset() { + *x = LifecycleTerminalReady{} + mi := &file_tunnel_proto_msgTypes[18] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *LifecycleTerminalReady) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*LifecycleTerminalReady) ProtoMessage() {} + +func (x *LifecycleTerminalReady) ProtoReflect() protoreflect.Message { + mi := &file_tunnel_proto_msgTypes[18] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use LifecycleTerminalReady.ProtoReflect.Descriptor instead. +func (*LifecycleTerminalReady) Descriptor() ([]byte, []int) { + return file_tunnel_proto_rawDescGZIP(), []int{18} +} + +func (x *LifecycleTerminalReady) GetProtocolVersion() uint32 { + if x != nil { + return x.ProtocolVersion + } + return 0 +} + +func (x *LifecycleTerminalReady) GetTerm() string { + if x != nil { + return x.Term + } + return "" +} + +func (x *LifecycleTerminalReady) GetRows() uint32 { + if x != nil { + return x.Rows + } + return 0 +} + +func (x *LifecycleTerminalReady) GetColumns() uint32 { + if x != nil { + return x.Columns + } + return 0 +} + +func (x *LifecycleTerminalReady) GetInput() bool { + if x != nil { + return x.Input + } + return false +} + +type LifecycleTerminalBegin struct { + state protoimpl.MessageState `protogen:"open.v1"` + InvocationId string `protobuf:"bytes,1,opt,name=invocation_id,json=invocationId,proto3" json:"invocation_id,omitempty"` + Phase string `protobuf:"bytes,2,opt,name=phase,proto3" json:"phase,omitempty"` + Index uint32 `protobuf:"varint,3,opt,name=index,proto3" json:"index,omitempty"` + Key string `protobuf:"bytes,4,opt,name=key,proto3" json:"key,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *LifecycleTerminalBegin) Reset() { + *x = LifecycleTerminalBegin{} + mi := &file_tunnel_proto_msgTypes[19] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *LifecycleTerminalBegin) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*LifecycleTerminalBegin) ProtoMessage() {} + +func (x *LifecycleTerminalBegin) ProtoReflect() protoreflect.Message { + mi := &file_tunnel_proto_msgTypes[19] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use LifecycleTerminalBegin.ProtoReflect.Descriptor instead. +func (*LifecycleTerminalBegin) Descriptor() ([]byte, []int) { + return file_tunnel_proto_rawDescGZIP(), []int{19} +} + +func (x *LifecycleTerminalBegin) GetInvocationId() string { + if x != nil { + return x.InvocationId + } + return "" +} + +func (x *LifecycleTerminalBegin) GetPhase() string { + if x != nil { + return x.Phase + } + return "" +} + +func (x *LifecycleTerminalBegin) GetIndex() uint32 { + if x != nil { + return x.Index + } + return 0 +} + +func (x *LifecycleTerminalBegin) GetKey() string { + if x != nil { + return x.Key + } + return "" +} + +type LifecycleTerminalBeginAck struct { + state protoimpl.MessageState `protogen:"open.v1"` + InvocationId string `protobuf:"bytes,1,opt,name=invocation_id,json=invocationId,proto3" json:"invocation_id,omitempty"` + Rows uint32 `protobuf:"varint,2,opt,name=rows,proto3" json:"rows,omitempty"` + Columns uint32 `protobuf:"varint,3,opt,name=columns,proto3" json:"columns,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *LifecycleTerminalBeginAck) Reset() { + *x = LifecycleTerminalBeginAck{} + mi := &file_tunnel_proto_msgTypes[20] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *LifecycleTerminalBeginAck) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*LifecycleTerminalBeginAck) ProtoMessage() {} + +func (x *LifecycleTerminalBeginAck) ProtoReflect() protoreflect.Message { + mi := &file_tunnel_proto_msgTypes[20] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use LifecycleTerminalBeginAck.ProtoReflect.Descriptor instead. +func (*LifecycleTerminalBeginAck) Descriptor() ([]byte, []int) { + return file_tunnel_proto_rawDescGZIP(), []int{20} +} + +func (x *LifecycleTerminalBeginAck) GetInvocationId() string { + if x != nil { + return x.InvocationId + } + return "" +} + +func (x *LifecycleTerminalBeginAck) GetRows() uint32 { + if x != nil { + return x.Rows + } + return 0 +} + +func (x *LifecycleTerminalBeginAck) GetColumns() uint32 { + if x != nil { + return x.Columns + } + return 0 +} + +type LifecycleTerminalOutput struct { + state protoimpl.MessageState `protogen:"open.v1"` + InvocationId string `protobuf:"bytes,1,opt,name=invocation_id,json=invocationId,proto3" json:"invocation_id,omitempty"` + Sequence uint64 `protobuf:"varint,2,opt,name=sequence,proto3" json:"sequence,omitempty"` + Data []byte `protobuf:"bytes,3,opt,name=data,proto3" json:"data,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *LifecycleTerminalOutput) Reset() { + *x = LifecycleTerminalOutput{} + mi := &file_tunnel_proto_msgTypes[21] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *LifecycleTerminalOutput) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*LifecycleTerminalOutput) ProtoMessage() {} + +func (x *LifecycleTerminalOutput) ProtoReflect() protoreflect.Message { + mi := &file_tunnel_proto_msgTypes[21] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use LifecycleTerminalOutput.ProtoReflect.Descriptor instead. +func (*LifecycleTerminalOutput) Descriptor() ([]byte, []int) { + return file_tunnel_proto_rawDescGZIP(), []int{21} +} + +func (x *LifecycleTerminalOutput) GetInvocationId() string { + if x != nil { + return x.InvocationId + } + return "" +} + +func (x *LifecycleTerminalOutput) GetSequence() uint64 { + if x != nil { + return x.Sequence + } + return 0 +} + +func (x *LifecycleTerminalOutput) GetData() []byte { + if x != nil { + return x.Data + } + return nil +} + +type LifecycleTerminalInput struct { + state protoimpl.MessageState `protogen:"open.v1"` + InvocationId string `protobuf:"bytes,1,opt,name=invocation_id,json=invocationId,proto3" json:"invocation_id,omitempty"` + Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *LifecycleTerminalInput) Reset() { + *x = LifecycleTerminalInput{} + mi := &file_tunnel_proto_msgTypes[22] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *LifecycleTerminalInput) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*LifecycleTerminalInput) ProtoMessage() {} + +func (x *LifecycleTerminalInput) ProtoReflect() protoreflect.Message { + mi := &file_tunnel_proto_msgTypes[22] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use LifecycleTerminalInput.ProtoReflect.Descriptor instead. +func (*LifecycleTerminalInput) Descriptor() ([]byte, []int) { + return file_tunnel_proto_rawDescGZIP(), []int{22} +} + +func (x *LifecycleTerminalInput) GetInvocationId() string { + if x != nil { + return x.InvocationId + } + return "" +} + +func (x *LifecycleTerminalInput) GetData() []byte { + if x != nil { + return x.Data + } + return nil +} + +type LifecycleTerminalResize struct { + state protoimpl.MessageState `protogen:"open.v1"` + InvocationId string `protobuf:"bytes,1,opt,name=invocation_id,json=invocationId,proto3" json:"invocation_id,omitempty"` + Rows uint32 `protobuf:"varint,2,opt,name=rows,proto3" json:"rows,omitempty"` + Columns uint32 `protobuf:"varint,3,opt,name=columns,proto3" json:"columns,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *LifecycleTerminalResize) Reset() { + *x = LifecycleTerminalResize{} + mi := &file_tunnel_proto_msgTypes[23] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *LifecycleTerminalResize) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*LifecycleTerminalResize) ProtoMessage() {} + +func (x *LifecycleTerminalResize) ProtoReflect() protoreflect.Message { + mi := &file_tunnel_proto_msgTypes[23] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use LifecycleTerminalResize.ProtoReflect.Descriptor instead. +func (*LifecycleTerminalResize) Descriptor() ([]byte, []int) { + return file_tunnel_proto_rawDescGZIP(), []int{23} +} + +func (x *LifecycleTerminalResize) GetInvocationId() string { + if x != nil { + return x.InvocationId + } + return "" +} + +func (x *LifecycleTerminalResize) GetRows() uint32 { + if x != nil { + return x.Rows + } + return 0 +} + +func (x *LifecycleTerminalResize) GetColumns() uint32 { + if x != nil { + return x.Columns + } + return 0 +} + +type LifecycleTerminalCancel struct { + state protoimpl.MessageState `protogen:"open.v1"` + InvocationId string `protobuf:"bytes,1,opt,name=invocation_id,json=invocationId,proto3" json:"invocation_id,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *LifecycleTerminalCancel) Reset() { + *x = LifecycleTerminalCancel{} + mi := &file_tunnel_proto_msgTypes[24] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *LifecycleTerminalCancel) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*LifecycleTerminalCancel) ProtoMessage() {} + +func (x *LifecycleTerminalCancel) ProtoReflect() protoreflect.Message { + mi := &file_tunnel_proto_msgTypes[24] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use LifecycleTerminalCancel.ProtoReflect.Descriptor instead. +func (*LifecycleTerminalCancel) Descriptor() ([]byte, []int) { + return file_tunnel_proto_rawDescGZIP(), []int{24} +} + +func (x *LifecycleTerminalCancel) GetInvocationId() string { + if x != nil { + return x.InvocationId + } + return "" +} + +type LifecycleTerminalExit struct { + state protoimpl.MessageState `protogen:"open.v1"` + InvocationId string `protobuf:"bytes,1,opt,name=invocation_id,json=invocationId,proto3" json:"invocation_id,omitempty"` + ExitCode int32 `protobuf:"varint,2,opt,name=exit_code,json=exitCode,proto3" json:"exit_code,omitempty"` + Signal string `protobuf:"bytes,3,opt,name=signal,proto3" json:"signal,omitempty"` + StartError string `protobuf:"bytes,4,opt,name=start_error,json=startError,proto3" json:"start_error,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *LifecycleTerminalExit) Reset() { + *x = LifecycleTerminalExit{} + mi := &file_tunnel_proto_msgTypes[25] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *LifecycleTerminalExit) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*LifecycleTerminalExit) ProtoMessage() {} + +func (x *LifecycleTerminalExit) ProtoReflect() protoreflect.Message { + mi := &file_tunnel_proto_msgTypes[25] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use LifecycleTerminalExit.ProtoReflect.Descriptor instead. +func (*LifecycleTerminalExit) Descriptor() ([]byte, []int) { + return file_tunnel_proto_rawDescGZIP(), []int{25} +} + +func (x *LifecycleTerminalExit) GetInvocationId() string { + if x != nil { + return x.InvocationId + } + return "" +} + +func (x *LifecycleTerminalExit) GetExitCode() int32 { + if x != nil { + return x.ExitCode + } + return 0 +} + +func (x *LifecycleTerminalExit) GetSignal() string { + if x != nil { + return x.Signal + } + return "" +} + +func (x *LifecycleTerminalExit) GetStartError() string { + if x != nil { + return x.StartError + } + return "" +} + +type LifecycleTerminalExitAck struct { + state protoimpl.MessageState `protogen:"open.v1"` + InvocationId string `protobuf:"bytes,1,opt,name=invocation_id,json=invocationId,proto3" json:"invocation_id,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *LifecycleTerminalExitAck) Reset() { + *x = LifecycleTerminalExitAck{} + mi := &file_tunnel_proto_msgTypes[26] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *LifecycleTerminalExitAck) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*LifecycleTerminalExitAck) ProtoMessage() {} + +func (x *LifecycleTerminalExitAck) ProtoReflect() protoreflect.Message { + mi := &file_tunnel_proto_msgTypes[26] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use LifecycleTerminalExitAck.ProtoReflect.Descriptor instead. +func (*LifecycleTerminalExitAck) Descriptor() ([]byte, []int) { + return file_tunnel_proto_rawDescGZIP(), []int{26} +} + +func (x *LifecycleTerminalExitAck) GetInvocationId() string { + if x != nil { + return x.InvocationId + } + return "" +} + +type LifecycleTerminalComplete struct { + state protoimpl.MessageState `protogen:"open.v1"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *LifecycleTerminalComplete) Reset() { + *x = LifecycleTerminalComplete{} + mi := &file_tunnel_proto_msgTypes[27] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *LifecycleTerminalComplete) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*LifecycleTerminalComplete) ProtoMessage() {} + +func (x *LifecycleTerminalComplete) ProtoReflect() protoreflect.Message { + mi := &file_tunnel_proto_msgTypes[27] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use LifecycleTerminalComplete.ProtoReflect.Descriptor instead. +func (*LifecycleTerminalComplete) Descriptor() ([]byte, []int) { + return file_tunnel_proto_rawDescGZIP(), []int{27} +} + var File_tunnel_proto protoreflect.FileDescriptor const file_tunnel_proto_rawDesc = "" + @@ -997,7 +1909,64 @@ const file_tunnel_proto_rawDesc = "" + "\x12gap_first_sequence\x18\x03 \x01(\x04R\x10gapFirstSequence\x12*\n" + "\x11gap_last_sequence\x18\x04 \x01(\x04R\x0fgapLastSequence\x12\x16\n" + "\x06detail\x18\x05 \x01(\tR\x06detail\"\a\n" + - "\x05Empty*A\n" + + "\x05Empty\"\xc1\x02\n" + + "\x18LifecycleTerminalRequest\x123\n" + + "\x04open\x18\x01 \x01(\v2\x1d.tunnel.LifecycleTerminalOpenH\x00R\x04open\x126\n" + + "\x05begin\x18\x02 \x01(\v2\x1e.tunnel.LifecycleTerminalBeginH\x00R\x05begin\x129\n" + + "\x06output\x18\x03 \x01(\v2\x1f.tunnel.LifecycleTerminalOutputH\x00R\x06output\x123\n" + + "\x04exit\x18\x04 \x01(\v2\x1d.tunnel.LifecycleTerminalExitH\x00R\x04exit\x12?\n" + + "\bcomplete\x18\x05 \x01(\v2!.tunnel.LifecycleTerminalCompleteH\x00R\bcompleteB\a\n" + + "\x05frame\"\x8b\x03\n" + + "\x19LifecycleTerminalResponse\x126\n" + + "\x05ready\x18\x01 \x01(\v2\x1e.tunnel.LifecycleTerminalReadyH\x00R\x05ready\x12@\n" + + "\tbegin_ack\x18\x02 \x01(\v2!.tunnel.LifecycleTerminalBeginAckH\x00R\bbeginAck\x126\n" + + "\x05input\x18\x03 \x01(\v2\x1e.tunnel.LifecycleTerminalInputH\x00R\x05input\x129\n" + + "\x06resize\x18\x04 \x01(\v2\x1f.tunnel.LifecycleTerminalResizeH\x00R\x06resize\x129\n" + + "\x06cancel\x18\x05 \x01(\v2\x1f.tunnel.LifecycleTerminalCancelH\x00R\x06cancel\x12=\n" + + "\bexit_ack\x18\x06 \x01(\v2 .tunnel.LifecycleTerminalExitAckH\x00R\aexitAckB\a\n" + + "\x05frame\"\x97\x01\n" + + "\x15LifecycleTerminalOpen\x12)\n" + + "\x10protocol_version\x18\x01 \x01(\rR\x0fprotocolVersion\x12#\n" + + "\rworkspace_uid\x18\x02 \x01(\tR\fworkspaceUid\x12\x18\n" + + "\acontext\x18\x03 \x01(\tR\acontext\x12\x14\n" + + "\x05nonce\x18\x04 \x01(\tR\x05nonce\"\x9b\x01\n" + + "\x16LifecycleTerminalReady\x12)\n" + + "\x10protocol_version\x18\x01 \x01(\rR\x0fprotocolVersion\x12\x12\n" + + "\x04term\x18\x02 \x01(\tR\x04term\x12\x12\n" + + "\x04rows\x18\x03 \x01(\rR\x04rows\x12\x18\n" + + "\acolumns\x18\x04 \x01(\rR\acolumns\x12\x14\n" + + "\x05input\x18\x05 \x01(\bR\x05input\"{\n" + + "\x16LifecycleTerminalBegin\x12#\n" + + "\rinvocation_id\x18\x01 \x01(\tR\finvocationId\x12\x14\n" + + "\x05phase\x18\x02 \x01(\tR\x05phase\x12\x14\n" + + "\x05index\x18\x03 \x01(\rR\x05index\x12\x10\n" + + "\x03key\x18\x04 \x01(\tR\x03key\"n\n" + + "\x19LifecycleTerminalBeginAck\x12#\n" + + "\rinvocation_id\x18\x01 \x01(\tR\finvocationId\x12\x12\n" + + "\x04rows\x18\x02 \x01(\rR\x04rows\x12\x18\n" + + "\acolumns\x18\x03 \x01(\rR\acolumns\"n\n" + + "\x17LifecycleTerminalOutput\x12#\n" + + "\rinvocation_id\x18\x01 \x01(\tR\finvocationId\x12\x1a\n" + + "\bsequence\x18\x02 \x01(\x04R\bsequence\x12\x12\n" + + "\x04data\x18\x03 \x01(\fR\x04data\"Q\n" + + "\x16LifecycleTerminalInput\x12#\n" + + "\rinvocation_id\x18\x01 \x01(\tR\finvocationId\x12\x12\n" + + "\x04data\x18\x02 \x01(\fR\x04data\"l\n" + + "\x17LifecycleTerminalResize\x12#\n" + + "\rinvocation_id\x18\x01 \x01(\tR\finvocationId\x12\x12\n" + + "\x04rows\x18\x02 \x01(\rR\x04rows\x12\x18\n" + + "\acolumns\x18\x03 \x01(\rR\acolumns\">\n" + + "\x17LifecycleTerminalCancel\x12#\n" + + "\rinvocation_id\x18\x01 \x01(\tR\finvocationId\"\x92\x01\n" + + "\x15LifecycleTerminalExit\x12#\n" + + "\rinvocation_id\x18\x01 \x01(\tR\finvocationId\x12\x1b\n" + + "\texit_code\x18\x02 \x01(\x05R\bexitCode\x12\x16\n" + + "\x06signal\x18\x03 \x01(\tR\x06signal\x12\x1f\n" + + "\vstart_error\x18\x04 \x01(\tR\n" + + "startError\"?\n" + + "\x18LifecycleTerminalExitAck\x12#\n" + + "\rinvocation_id\x18\x01 \x01(\tR\finvocationId\"\x1b\n" + + "\x19LifecycleTerminalComplete*A\n" + "\bLogLevel\x12\t\n" + "\x05DEBUG\x10\x00\x12\b\n" + "\x04INFO\x10\x01\x12\b\n" + @@ -1008,7 +1977,7 @@ const file_tunnel_proto_rawDesc = "" + "\x1cSETUP_LOG_RESULT_UNSPECIFIED\x10\x00\x12\x1d\n" + "\x19SETUP_LOG_RESULT_ACCEPTED\x10\x01\x12\"\n" + "\x1eSETUP_LOG_RESULT_RETENTION_GAP\x10\x02\x12%\n" + - "!SETUP_LOG_RESULT_REJECTED_INVALID\x10\x032\xba\b\n" + + "!SETUP_LOG_RESULT_REJECTED_INVALID\x10\x032\x9a\t\n" + "\x06Tunnel\x12&\n" + "\x04Ping\x12\r.tunnel.Empty\x1a\r.tunnel.Empty\"\x00\x12*\n" + "\x03Log\x12\x12.tunnel.LogMessage\x1a\r.tunnel.Empty\"\x00\x12/\n" + @@ -1030,7 +1999,8 @@ const file_tunnel_proto_rawDesc = "" + "\x0fStopForwardPort\x12\x1e.tunnel.StopForwardPortRequest\x1a\x1f.tunnel.StopForwardPortResponse\"\x00\x123\n" + "\x0fStreamWorkspace\x12\r.tunnel.Empty\x1a\r.tunnel.Chunk\"\x000\x01\x12<\n" + "\vStreamMount\x12\x1a.tunnel.StreamMountRequest\x1a\r.tunnel.Chunk\"\x000\x01\x129\n" + - "\x15StreamSnapshotVolumes\x12\r.tunnel.Empty\x1a\r.tunnel.Chunk\"\x000\x01B-Z+github.com/devsy-org/devsy/pkg/agent/tunnelb\x06proto3" + "\x15StreamSnapshotVolumes\x12\r.tunnel.Empty\x1a\r.tunnel.Chunk\"\x000\x01\x12^\n" + + "\x11LifecycleTerminal\x12 .tunnel.LifecycleTerminalRequest\x1a!.tunnel.LifecycleTerminalResponse\"\x00(\x010\x01B-Z+github.com/devsy-org/devsy/pkg/agent/tunnelb\x06proto3" var ( file_tunnel_proto_rawDescOnce sync.Once @@ -1045,76 +2015,102 @@ func file_tunnel_proto_rawDescGZIP() []byte { } var file_tunnel_proto_enumTypes = make([]protoimpl.EnumInfo, 2) -var file_tunnel_proto_msgTypes = make([]protoimpl.MessageInfo, 16) +var file_tunnel_proto_msgTypes = make([]protoimpl.MessageInfo, 29) var file_tunnel_proto_goTypes = []any{ - (LogLevel)(0), // 0: tunnel.LogLevel - (SetupLogResult)(0), // 1: tunnel.SetupLogResult - (*StreamMountRequest)(nil), // 2: tunnel.StreamMountRequest - (*StopForwardPortRequest)(nil), // 3: tunnel.StopForwardPortRequest - (*StopForwardPortResponse)(nil), // 4: tunnel.StopForwardPortResponse - (*ForwardPortRequest)(nil), // 5: tunnel.ForwardPortRequest - (*ForwardPortResponse)(nil), // 6: tunnel.ForwardPortResponse - (*Message)(nil), // 7: tunnel.Message - (*Secret)(nil), // 8: tunnel.Secret - (*SecretsResponse)(nil), // 9: tunnel.SecretsResponse - (*Chunk)(nil), // 10: tunnel.Chunk - (*LogMessage)(nil), // 11: tunnel.LogMessage - (*StatusUpdate)(nil), // 12: tunnel.StatusUpdate - (*StatusError)(nil), // 13: tunnel.StatusError - (*AppendSetupLogsRequest)(nil), // 14: tunnel.AppendSetupLogsRequest - (*AppendSetupLogsResponse)(nil), // 15: tunnel.AppendSetupLogsResponse - (*Empty)(nil), // 16: tunnel.Empty - nil, // 17: tunnel.StatusError.ContextEntry + (LogLevel)(0), // 0: tunnel.LogLevel + (SetupLogResult)(0), // 1: tunnel.SetupLogResult + (*StreamMountRequest)(nil), // 2: tunnel.StreamMountRequest + (*StopForwardPortRequest)(nil), // 3: tunnel.StopForwardPortRequest + (*StopForwardPortResponse)(nil), // 4: tunnel.StopForwardPortResponse + (*ForwardPortRequest)(nil), // 5: tunnel.ForwardPortRequest + (*ForwardPortResponse)(nil), // 6: tunnel.ForwardPortResponse + (*Message)(nil), // 7: tunnel.Message + (*Secret)(nil), // 8: tunnel.Secret + (*SecretsResponse)(nil), // 9: tunnel.SecretsResponse + (*Chunk)(nil), // 10: tunnel.Chunk + (*LogMessage)(nil), // 11: tunnel.LogMessage + (*StatusUpdate)(nil), // 12: tunnel.StatusUpdate + (*StatusError)(nil), // 13: tunnel.StatusError + (*AppendSetupLogsRequest)(nil), // 14: tunnel.AppendSetupLogsRequest + (*AppendSetupLogsResponse)(nil), // 15: tunnel.AppendSetupLogsResponse + (*Empty)(nil), // 16: tunnel.Empty + (*LifecycleTerminalRequest)(nil), // 17: tunnel.LifecycleTerminalRequest + (*LifecycleTerminalResponse)(nil), // 18: tunnel.LifecycleTerminalResponse + (*LifecycleTerminalOpen)(nil), // 19: tunnel.LifecycleTerminalOpen + (*LifecycleTerminalReady)(nil), // 20: tunnel.LifecycleTerminalReady + (*LifecycleTerminalBegin)(nil), // 21: tunnel.LifecycleTerminalBegin + (*LifecycleTerminalBeginAck)(nil), // 22: tunnel.LifecycleTerminalBeginAck + (*LifecycleTerminalOutput)(nil), // 23: tunnel.LifecycleTerminalOutput + (*LifecycleTerminalInput)(nil), // 24: tunnel.LifecycleTerminalInput + (*LifecycleTerminalResize)(nil), // 25: tunnel.LifecycleTerminalResize + (*LifecycleTerminalCancel)(nil), // 26: tunnel.LifecycleTerminalCancel + (*LifecycleTerminalExit)(nil), // 27: tunnel.LifecycleTerminalExit + (*LifecycleTerminalExitAck)(nil), // 28: tunnel.LifecycleTerminalExitAck + (*LifecycleTerminalComplete)(nil), // 29: tunnel.LifecycleTerminalComplete + nil, // 30: tunnel.StatusError.ContextEntry } var file_tunnel_proto_depIdxs = []int32{ 8, // 0: tunnel.SecretsResponse.secrets:type_name -> tunnel.Secret 0, // 1: tunnel.LogMessage.logLevel:type_name -> tunnel.LogLevel 13, // 2: tunnel.StatusUpdate.error:type_name -> tunnel.StatusError - 17, // 3: tunnel.StatusError.context:type_name -> tunnel.StatusError.ContextEntry + 30, // 3: tunnel.StatusError.context:type_name -> tunnel.StatusError.ContextEntry 1, // 4: tunnel.AppendSetupLogsResponse.result:type_name -> tunnel.SetupLogResult - 16, // 5: tunnel.Tunnel.Ping:input_type -> tunnel.Empty - 11, // 6: tunnel.Tunnel.Log:input_type -> tunnel.LogMessage - 12, // 7: tunnel.Tunnel.Status:input_type -> tunnel.StatusUpdate - 14, // 8: tunnel.Tunnel.AppendSetupLogs:input_type -> tunnel.AppendSetupLogsRequest - 7, // 9: tunnel.Tunnel.SendResult:input_type -> tunnel.Message - 7, // 10: tunnel.Tunnel.DockerCredentials:input_type -> tunnel.Message - 7, // 11: tunnel.Tunnel.GitCredentials:input_type -> tunnel.Message - 16, // 12: tunnel.Tunnel.GitHubCredentials:input_type -> tunnel.Empty - 7, // 13: tunnel.Tunnel.GitSSHSignature:input_type -> tunnel.Message - 16, // 14: tunnel.Tunnel.GitUser:input_type -> tunnel.Empty - 7, // 15: tunnel.Tunnel.DevsyConfig:input_type -> tunnel.Message - 7, // 16: tunnel.Tunnel.GPGPublicKeys:input_type -> tunnel.Message - 7, // 17: tunnel.Tunnel.KubeConfig:input_type -> tunnel.Message - 16, // 18: tunnel.Tunnel.Secrets:input_type -> tunnel.Empty - 5, // 19: tunnel.Tunnel.ForwardPort:input_type -> tunnel.ForwardPortRequest - 3, // 20: tunnel.Tunnel.StopForwardPort:input_type -> tunnel.StopForwardPortRequest - 16, // 21: tunnel.Tunnel.StreamWorkspace:input_type -> tunnel.Empty - 2, // 22: tunnel.Tunnel.StreamMount:input_type -> tunnel.StreamMountRequest - 16, // 23: tunnel.Tunnel.StreamSnapshotVolumes:input_type -> tunnel.Empty - 16, // 24: tunnel.Tunnel.Ping:output_type -> tunnel.Empty - 16, // 25: tunnel.Tunnel.Log:output_type -> tunnel.Empty - 16, // 26: tunnel.Tunnel.Status:output_type -> tunnel.Empty - 15, // 27: tunnel.Tunnel.AppendSetupLogs:output_type -> tunnel.AppendSetupLogsResponse - 16, // 28: tunnel.Tunnel.SendResult:output_type -> tunnel.Empty - 7, // 29: tunnel.Tunnel.DockerCredentials:output_type -> tunnel.Message - 7, // 30: tunnel.Tunnel.GitCredentials:output_type -> tunnel.Message - 7, // 31: tunnel.Tunnel.GitHubCredentials:output_type -> tunnel.Message - 7, // 32: tunnel.Tunnel.GitSSHSignature:output_type -> tunnel.Message - 7, // 33: tunnel.Tunnel.GitUser:output_type -> tunnel.Message - 7, // 34: tunnel.Tunnel.DevsyConfig:output_type -> tunnel.Message - 7, // 35: tunnel.Tunnel.GPGPublicKeys:output_type -> tunnel.Message - 7, // 36: tunnel.Tunnel.KubeConfig:output_type -> tunnel.Message - 9, // 37: tunnel.Tunnel.Secrets:output_type -> tunnel.SecretsResponse - 6, // 38: tunnel.Tunnel.ForwardPort:output_type -> tunnel.ForwardPortResponse - 4, // 39: tunnel.Tunnel.StopForwardPort:output_type -> tunnel.StopForwardPortResponse - 10, // 40: tunnel.Tunnel.StreamWorkspace:output_type -> tunnel.Chunk - 10, // 41: tunnel.Tunnel.StreamMount:output_type -> tunnel.Chunk - 10, // 42: tunnel.Tunnel.StreamSnapshotVolumes:output_type -> tunnel.Chunk - 24, // [24:43] is the sub-list for method output_type - 5, // [5:24] is the sub-list for method input_type - 5, // [5:5] is the sub-list for extension type_name - 5, // [5:5] is the sub-list for extension extendee - 0, // [0:5] is the sub-list for field type_name + 19, // 5: tunnel.LifecycleTerminalRequest.open:type_name -> tunnel.LifecycleTerminalOpen + 21, // 6: tunnel.LifecycleTerminalRequest.begin:type_name -> tunnel.LifecycleTerminalBegin + 23, // 7: tunnel.LifecycleTerminalRequest.output:type_name -> tunnel.LifecycleTerminalOutput + 27, // 8: tunnel.LifecycleTerminalRequest.exit:type_name -> tunnel.LifecycleTerminalExit + 29, // 9: tunnel.LifecycleTerminalRequest.complete:type_name -> tunnel.LifecycleTerminalComplete + 20, // 10: tunnel.LifecycleTerminalResponse.ready:type_name -> tunnel.LifecycleTerminalReady + 22, // 11: tunnel.LifecycleTerminalResponse.begin_ack:type_name -> tunnel.LifecycleTerminalBeginAck + 24, // 12: tunnel.LifecycleTerminalResponse.input:type_name -> tunnel.LifecycleTerminalInput + 25, // 13: tunnel.LifecycleTerminalResponse.resize:type_name -> tunnel.LifecycleTerminalResize + 26, // 14: tunnel.LifecycleTerminalResponse.cancel:type_name -> tunnel.LifecycleTerminalCancel + 28, // 15: tunnel.LifecycleTerminalResponse.exit_ack:type_name -> tunnel.LifecycleTerminalExitAck + 16, // 16: tunnel.Tunnel.Ping:input_type -> tunnel.Empty + 11, // 17: tunnel.Tunnel.Log:input_type -> tunnel.LogMessage + 12, // 18: tunnel.Tunnel.Status:input_type -> tunnel.StatusUpdate + 14, // 19: tunnel.Tunnel.AppendSetupLogs:input_type -> tunnel.AppendSetupLogsRequest + 7, // 20: tunnel.Tunnel.SendResult:input_type -> tunnel.Message + 7, // 21: tunnel.Tunnel.DockerCredentials:input_type -> tunnel.Message + 7, // 22: tunnel.Tunnel.GitCredentials:input_type -> tunnel.Message + 16, // 23: tunnel.Tunnel.GitHubCredentials:input_type -> tunnel.Empty + 7, // 24: tunnel.Tunnel.GitSSHSignature:input_type -> tunnel.Message + 16, // 25: tunnel.Tunnel.GitUser:input_type -> tunnel.Empty + 7, // 26: tunnel.Tunnel.DevsyConfig:input_type -> tunnel.Message + 7, // 27: tunnel.Tunnel.GPGPublicKeys:input_type -> tunnel.Message + 7, // 28: tunnel.Tunnel.KubeConfig:input_type -> tunnel.Message + 16, // 29: tunnel.Tunnel.Secrets:input_type -> tunnel.Empty + 5, // 30: tunnel.Tunnel.ForwardPort:input_type -> tunnel.ForwardPortRequest + 3, // 31: tunnel.Tunnel.StopForwardPort:input_type -> tunnel.StopForwardPortRequest + 16, // 32: tunnel.Tunnel.StreamWorkspace:input_type -> tunnel.Empty + 2, // 33: tunnel.Tunnel.StreamMount:input_type -> tunnel.StreamMountRequest + 16, // 34: tunnel.Tunnel.StreamSnapshotVolumes:input_type -> tunnel.Empty + 17, // 35: tunnel.Tunnel.LifecycleTerminal:input_type -> tunnel.LifecycleTerminalRequest + 16, // 36: tunnel.Tunnel.Ping:output_type -> tunnel.Empty + 16, // 37: tunnel.Tunnel.Log:output_type -> tunnel.Empty + 16, // 38: tunnel.Tunnel.Status:output_type -> tunnel.Empty + 15, // 39: tunnel.Tunnel.AppendSetupLogs:output_type -> tunnel.AppendSetupLogsResponse + 16, // 40: tunnel.Tunnel.SendResult:output_type -> tunnel.Empty + 7, // 41: tunnel.Tunnel.DockerCredentials:output_type -> tunnel.Message + 7, // 42: tunnel.Tunnel.GitCredentials:output_type -> tunnel.Message + 7, // 43: tunnel.Tunnel.GitHubCredentials:output_type -> tunnel.Message + 7, // 44: tunnel.Tunnel.GitSSHSignature:output_type -> tunnel.Message + 7, // 45: tunnel.Tunnel.GitUser:output_type -> tunnel.Message + 7, // 46: tunnel.Tunnel.DevsyConfig:output_type -> tunnel.Message + 7, // 47: tunnel.Tunnel.GPGPublicKeys:output_type -> tunnel.Message + 7, // 48: tunnel.Tunnel.KubeConfig:output_type -> tunnel.Message + 9, // 49: tunnel.Tunnel.Secrets:output_type -> tunnel.SecretsResponse + 6, // 50: tunnel.Tunnel.ForwardPort:output_type -> tunnel.ForwardPortResponse + 4, // 51: tunnel.Tunnel.StopForwardPort:output_type -> tunnel.StopForwardPortResponse + 10, // 52: tunnel.Tunnel.StreamWorkspace:output_type -> tunnel.Chunk + 10, // 53: tunnel.Tunnel.StreamMount:output_type -> tunnel.Chunk + 10, // 54: tunnel.Tunnel.StreamSnapshotVolumes:output_type -> tunnel.Chunk + 18, // 55: tunnel.Tunnel.LifecycleTerminal:output_type -> tunnel.LifecycleTerminalResponse + 36, // [36:56] is the sub-list for method output_type + 16, // [16:36] is the sub-list for method input_type + 16, // [16:16] is the sub-list for extension type_name + 16, // [16:16] is the sub-list for extension extendee + 0, // [0:16] is the sub-list for field type_name } func init() { file_tunnel_proto_init() } @@ -1122,13 +2118,28 @@ func file_tunnel_proto_init() { if File_tunnel_proto != nil { return } + file_tunnel_proto_msgTypes[15].OneofWrappers = []any{ + (*LifecycleTerminalRequest_Open)(nil), + (*LifecycleTerminalRequest_Begin)(nil), + (*LifecycleTerminalRequest_Output)(nil), + (*LifecycleTerminalRequest_Exit)(nil), + (*LifecycleTerminalRequest_Complete)(nil), + } + file_tunnel_proto_msgTypes[16].OneofWrappers = []any{ + (*LifecycleTerminalResponse_Ready)(nil), + (*LifecycleTerminalResponse_BeginAck)(nil), + (*LifecycleTerminalResponse_Input)(nil), + (*LifecycleTerminalResponse_Resize)(nil), + (*LifecycleTerminalResponse_Cancel)(nil), + (*LifecycleTerminalResponse_ExitAck)(nil), + } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: unsafe.Slice(unsafe.StringData(file_tunnel_proto_rawDesc), len(file_tunnel_proto_rawDesc)), NumEnums: 2, - NumMessages: 16, + NumMessages: 29, NumExtensions: 0, NumServices: 1, }, diff --git a/pkg/agent/tunnel/tunnel.proto b/pkg/agent/tunnel/tunnel.proto index 0ddead9a5c..aa5fe4f47b 100644 --- a/pkg/agent/tunnel/tunnel.proto +++ b/pkg/agent/tunnel/tunnel.proto @@ -28,6 +28,8 @@ service Tunnel { rpc StreamWorkspace(Empty) returns (stream Chunk) {} rpc StreamMount(StreamMountRequest) returns (stream Chunk) {} rpc StreamSnapshotVolumes(Empty) returns (stream Chunk) {} + + rpc LifecycleTerminal(stream LifecycleTerminalRequest) returns (stream LifecycleTerminalResponse) {} } message StreamMountRequest { @@ -130,3 +132,82 @@ message AppendSetupLogsResponse { message Empty { } + +message LifecycleTerminalRequest { + oneof frame { + LifecycleTerminalOpen open = 1; + LifecycleTerminalBegin begin = 2; + LifecycleTerminalOutput output = 3; + LifecycleTerminalExit exit = 4; + LifecycleTerminalComplete complete = 5; + } +} + +message LifecycleTerminalResponse { + oneof frame { + LifecycleTerminalReady ready = 1; + LifecycleTerminalBeginAck begin_ack = 2; + LifecycleTerminalInput input = 3; + LifecycleTerminalResize resize = 4; + LifecycleTerminalCancel cancel = 5; + LifecycleTerminalExitAck exit_ack = 6; + } +} + +message LifecycleTerminalOpen { + uint32 protocol_version = 1; + string workspace_uid = 2; + string context = 3; + string nonce = 4; +} + +message LifecycleTerminalReady { + uint32 protocol_version = 1; + string term = 2; + uint32 rows = 3; + uint32 columns = 4; + bool input = 5; +} + +message LifecycleTerminalBegin { + string invocation_id = 1; + string phase = 2; + uint32 index = 3; + string key = 4; +} + +message LifecycleTerminalBeginAck { + string invocation_id = 1; + uint32 rows = 2; + uint32 columns = 3; +} + +message LifecycleTerminalOutput { + string invocation_id = 1; + uint64 sequence = 2; + bytes data = 3; +} + +message LifecycleTerminalInput { + string invocation_id = 1; + bytes data = 2; +} + +message LifecycleTerminalResize { + string invocation_id = 1; + uint32 rows = 2; + uint32 columns = 3; +} + +message LifecycleTerminalCancel { string invocation_id = 1; } + +message LifecycleTerminalExit { + string invocation_id = 1; + int32 exit_code = 2; + string signal = 3; + string start_error = 4; +} + +message LifecycleTerminalExitAck { string invocation_id = 1; } + +message LifecycleTerminalComplete {} diff --git a/pkg/agent/tunnel/tunnel_grpc.pb.go b/pkg/agent/tunnel/tunnel_grpc.pb.go index 1e636ca638..e0c87df580 100644 --- a/pkg/agent/tunnel/tunnel_grpc.pb.go +++ b/pkg/agent/tunnel/tunnel_grpc.pb.go @@ -40,6 +40,7 @@ const ( Tunnel_StreamWorkspace_FullMethodName = "/tunnel.Tunnel/StreamWorkspace" Tunnel_StreamMount_FullMethodName = "/tunnel.Tunnel/StreamMount" Tunnel_StreamSnapshotVolumes_FullMethodName = "/tunnel.Tunnel/StreamSnapshotVolumes" + Tunnel_LifecycleTerminal_FullMethodName = "/tunnel.Tunnel/LifecycleTerminal" ) // TunnelClient is the client API for Tunnel service. @@ -65,6 +66,7 @@ type TunnelClient interface { StreamWorkspace(ctx context.Context, in *Empty, opts ...grpc.CallOption) (grpc.ServerStreamingClient[Chunk], error) StreamMount(ctx context.Context, in *StreamMountRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[Chunk], error) StreamSnapshotVolumes(ctx context.Context, in *Empty, opts ...grpc.CallOption) (grpc.ServerStreamingClient[Chunk], error) + LifecycleTerminal(ctx context.Context, opts ...grpc.CallOption) (grpc.BidiStreamingClient[LifecycleTerminalRequest, LifecycleTerminalResponse], error) } type tunnelClient struct { @@ -292,6 +294,19 @@ func (c *tunnelClient) StreamSnapshotVolumes(ctx context.Context, in *Empty, opt // This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name. type Tunnel_StreamSnapshotVolumesClient = grpc.ServerStreamingClient[Chunk] +func (c *tunnelClient) LifecycleTerminal(ctx context.Context, opts ...grpc.CallOption) (grpc.BidiStreamingClient[LifecycleTerminalRequest, LifecycleTerminalResponse], error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + stream, err := c.cc.NewStream(ctx, &Tunnel_ServiceDesc.Streams[3], Tunnel_LifecycleTerminal_FullMethodName, cOpts...) + if err != nil { + return nil, err + } + x := &grpc.GenericClientStream[LifecycleTerminalRequest, LifecycleTerminalResponse]{ClientStream: stream} + return x, nil +} + +// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name. +type Tunnel_LifecycleTerminalClient = grpc.BidiStreamingClient[LifecycleTerminalRequest, LifecycleTerminalResponse] + // TunnelServer is the server API for Tunnel service. // All implementations must embed UnimplementedTunnelServer // for forward compatibility. @@ -315,6 +330,7 @@ type TunnelServer interface { StreamWorkspace(*Empty, grpc.ServerStreamingServer[Chunk]) error StreamMount(*StreamMountRequest, grpc.ServerStreamingServer[Chunk]) error StreamSnapshotVolumes(*Empty, grpc.ServerStreamingServer[Chunk]) error + LifecycleTerminal(grpc.BidiStreamingServer[LifecycleTerminalRequest, LifecycleTerminalResponse]) error mustEmbedUnimplementedTunnelServer() } @@ -382,6 +398,9 @@ func (UnimplementedTunnelServer) StreamMount(*StreamMountRequest, grpc.ServerStr func (UnimplementedTunnelServer) StreamSnapshotVolumes(*Empty, grpc.ServerStreamingServer[Chunk]) error { return status.Error(codes.Unimplemented, "method StreamSnapshotVolumes not implemented") } +func (UnimplementedTunnelServer) LifecycleTerminal(grpc.BidiStreamingServer[LifecycleTerminalRequest, LifecycleTerminalResponse]) error { + return status.Error(codes.Unimplemented, "method LifecycleTerminal not implemented") +} func (UnimplementedTunnelServer) mustEmbedUnimplementedTunnelServer() {} func (UnimplementedTunnelServer) testEmbeddedByValue() {} @@ -724,6 +743,13 @@ func _Tunnel_StreamSnapshotVolumes_Handler(srv interface{}, stream grpc.ServerSt // This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name. type Tunnel_StreamSnapshotVolumesServer = grpc.ServerStreamingServer[Chunk] +func _Tunnel_LifecycleTerminal_Handler(srv interface{}, stream grpc.ServerStream) error { + return srv.(TunnelServer).LifecycleTerminal(&grpc.GenericServerStream[LifecycleTerminalRequest, LifecycleTerminalResponse]{ServerStream: stream}) +} + +// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name. +type Tunnel_LifecycleTerminalServer = grpc.BidiStreamingServer[LifecycleTerminalRequest, LifecycleTerminalResponse] + // Tunnel_ServiceDesc is the grpc.ServiceDesc for Tunnel service. // It's only intended for direct use with grpc.RegisterService, // and not to be introspected or modified (even as a copy) @@ -812,6 +838,12 @@ var Tunnel_ServiceDesc = grpc.ServiceDesc{ Handler: _Tunnel_StreamSnapshotVolumes_Handler, ServerStreams: true, }, + { + StreamName: "LifecycleTerminal", + Handler: _Tunnel_LifecycleTerminal_Handler, + ServerStreams: true, + ClientStreams: true, + }, }, Metadata: "tunnel.proto", } diff --git a/pkg/agent/tunnelserver/lifecycle_terminal.go b/pkg/agent/tunnelserver/lifecycle_terminal.go new file mode 100644 index 0000000000..e5af7fd023 --- /dev/null +++ b/pkg/agent/tunnelserver/lifecycle_terminal.go @@ -0,0 +1,439 @@ +package tunnelserver + +import ( + "context" + "io" + "strings" + "sync" + "unicode" + "unicode/utf8" + + "github.com/devsy-org/devsy/pkg/agent/tunnel" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +var terminalOwners sync.Map + +func (t *tunnelServer) serveLifecycleTerminal(stream tunnel.Tunnel_LifecycleTerminalServer) error { + if t.lifecycleTerminalOwner == nil && t.lifecycleTerminalUpstream == nil { + return status.Error(codes.FailedPrecondition, "lifecycle terminal unavailable") + } + first, err := stream.Recv() + if err != nil { + return err + } + if !t.validTerminalOpen(first.GetOpen()) { + return status.Error(codes.PermissionDenied, "invalid lifecycle terminal identity") + } + if _, loaded := terminalOwners.LoadOrStore(t, struct{}{}); loaded { + return status.Error(codes.FailedPrecondition, "lifecycle terminal already reserved") + } + defer terminalOwners.Delete(t) + checked := &checkedTerminalStream{ + Tunnel_LifecycleTerminalServer: stream, + sends: make(chan serverSend), + stop: make(chan struct{}), + stopped: make(chan struct{}), + } + go checked.sendLoop() + defer checked.closeSender() + if t.lifecycleTerminalOwner != nil { + return t.lifecycleTerminalOwner.Serve(stream.Context(), checked) + } + return t.forwardLifecycleTerminal(checked, first) +} + +func (t *tunnelServer) forwardLifecycleTerminal( + checked *checkedTerminalStream, + first *tunnel.LifecycleTerminalRequest, +) error { + stream := tunnel.Tunnel_LifecycleTerminalServer(checked) + ctx, cancel := context.WithCancel(stream.Context()) + defer cancel() + upstream, err := t.lifecycleTerminalUpstream.LifecycleTerminal(ctx) + if err != nil { + return err + } + done := make(chan error, 1) + go forwardTerminalRequests(stream, upstream, first, func(err error, cancelStream bool) { + done <- err + if cancelStream { + cancel() + } + }) + return forwardTerminalResponses(stream, upstream, done) +} + +func forwardTerminalResponses( + stream tunnel.Tunnel_LifecycleTerminalServer, + upstream tunnel.Tunnel_LifecycleTerminalClient, + done <-chan error, +) error { + var err error + for { + frame, recvErr := upstream.Recv() + if recvErr != nil { + if recvErr == io.EOF { + return nil + } + select { + case sendErr := <-done: + if sendErr != nil { + return sendErr + } + default: + } + return recvErr + } + if err = stream.Send(frame); err != nil { + return err + } + } +} + +func forwardTerminalRequests( + stream tunnel.Tunnel_LifecycleTerminalServer, + upstream tunnel.Tunnel_LifecycleTerminalClient, + first *tunnel.LifecycleTerminalRequest, + finish func(error, bool), +) { + if sendErr := upstream.Send(first); sendErr != nil { + finish(sendErr, true) + return + } + for { + frame, recvErr := stream.Recv() + if recvErr == io.EOF { + finish(upstream.CloseSend(), false) + return + } + if recvErr != nil { + finish(recvErr, true) + return + } + if sendErr := upstream.Send(frame); sendErr != nil { + finish(sendErr, true) + return + } + if frame.GetComplete() != nil { + finish(upstream.CloseSend(), false) + return + } + } +} + +func (t *tunnelServer) validTerminalOpen(open *tunnel.LifecycleTerminalOpen) bool { + if open == nil { + return false + } + req := t.lifecycleTerminalRequest + if open.ProtocolVersion != tunnel.LifecycleTerminalProtocolVersion { + return false + } + return open.ProtocolVersion == req.ProtocolVersion && t.validTerminalIdentity(open) +} + +func (t *tunnelServer) validTerminalIdentity(open *tunnel.LifecycleTerminalOpen) bool { + req := t.lifecycleTerminalRequest + return open.WorkspaceUid != "" && open.WorkspaceUid == req.WorkspaceUID && + open.Context == req.Context && validIdentity(open.Context, false) && + validIdentity(open.WorkspaceUid, true) && validIdentity(open.Nonce, true) && + open.Nonce != "" && open.Nonce == req.Nonce +} + +type checkedTerminalStream struct { + tunnel.Tunnel_LifecycleTerminalServer + mu sync.Mutex + sends chan serverSend + stop chan struct{} + stopped chan struct{} + phase terminalClientPhase + invocation string + sequence uint64 + closed bool + sending bool +} + +func (s *checkedTerminalStream) Recv() (*tunnel.LifecycleTerminalRequest, error) { + frame, err := s.Tunnel_LifecycleTerminalServer.Recv() + if err != nil { + return nil, err + } + s.mu.Lock() + defer s.mu.Unlock() + + if frame == nil { + return nil, invalidTerminalRequest() + } + if err = s.dispatchRequest(frame); err != nil { + return nil, err + } + return frame, nil +} + +type serverSend struct { + frame *tunnel.LifecycleTerminalResponse + result chan error +} + +func (s *checkedTerminalStream) Send(frame *tunnel.LifecycleTerminalResponse) error { + item := serverSend{frame: frame, result: make(chan error, 1)} + select { + case s.sends <- item: + case <-s.stop: + return context.Canceled + case <-s.Context().Done(): + return s.Context().Err() + } + select { + case err := <-item.result: + return err + case <-s.stop: + return context.Canceled + case <-s.Context().Done(): + return s.Context().Err() + } +} + +func validIdentity(s string, required bool) bool { + if len(s) > 256 || required && s == "" { + return false + } + for _, r := range s { + if unicode.IsControl(r) { + return false + } + } + return true +} + +func terminalDisplayKey(key string) string { + var b strings.Builder + for _, r := range key { + if unicode.IsControl(r) { + r = ' ' + } + n := utf8.RuneLen(r) + if n < 0 { + r = utf8.RuneError + n = utf8.RuneLen(r) + } + if b.Len()+n > 256 { + break + } + b.WriteRune(r) + } + return b.String() +} + +func invalidTerminalRequest() error { + return status.Error(codes.FailedPrecondition, "unexpected lifecycle terminal request") +} + +func invalidTerminalResponse() error { + return status.Error(codes.FailedPrecondition, "unexpected lifecycle terminal response") +} + +func (s *checkedTerminalStream) dispatchRequest(frame *tunnel.LifecycleTerminalRequest) error { + var err error + switch f := frame.Frame.(type) { + case *tunnel.LifecycleTerminalRequest_Begin: + err = s.receiveBegin(f) + case *tunnel.LifecycleTerminalRequest_Output: + err = s.receiveOutput(f) + case *tunnel.LifecycleTerminalRequest_Exit: + err = s.receiveExit(f) + case *tunnel.LifecycleTerminalRequest_Complete: + err = s.receiveComplete(f) + default: + err = invalidTerminalRequest() + } + if err != nil { + return err + } + return nil +} + +func (s *checkedTerminalStream) receiveBegin(f *tunnel.LifecycleTerminalRequest_Begin) error { + v := f.Begin + if s.phase != clientIdle || v == nil { + return invalidTerminalRequest() + } + if !validIdentity(v.InvocationId, true) || !validIdentity(v.Key, false) || + v.Phase != "postStartCommand" { + return status.Error(codes.InvalidArgument, "invalid lifecycle terminal invocation") + } + s.invocation = v.InvocationId + s.sequence = 0 + s.phase = clientBeginAck + return nil +} + +func (s *checkedTerminalStream) receiveOutput(f *tunnel.LifecycleTerminalRequest_Output) error { + v := f.Output + if s.phase != clientActive || v == nil || v.InvocationId != s.invocation || + v.Sequence != s.sequence+1 { + return invalidTerminalRequest() + } + if len(v.Data) > tunnel.LifecycleTerminalMaxFrame { + return status.Error(codes.ResourceExhausted, "terminal output frame too large") + } + s.sequence = v.Sequence + return nil +} + +func (s *checkedTerminalStream) receiveExit(f *tunnel.LifecycleTerminalRequest_Exit) error { + v := f.Exit + if s.phase != clientActive || v == nil || v.InvocationId != s.invocation { + return invalidTerminalRequest() + } + if !validIdentity(v.Signal, false) || !validIdentity(v.StartError, false) { + return status.Error(codes.InvalidArgument, "invalid lifecycle terminal exit") + } + s.phase = clientExitAck + return nil +} + +func (s *checkedTerminalStream) receiveComplete(f *tunnel.LifecycleTerminalRequest_Complete) error { + if s.phase != clientIdle || f.Complete == nil { + return invalidTerminalRequest() + } + s.phase = clientClosed + return nil +} + +func (s *checkedTerminalStream) sendLoop() { + defer close(s.stopped) + for { + select { + case item := <-s.sends: + s.mu.Lock() + if s.closed { + s.mu.Unlock() + item.result <- context.Canceled + continue + } + err := s.validateResponse(item.frame) + if err == nil { + s.sending = true + } + s.mu.Unlock() + if err == nil { + err = s.Tunnel_LifecycleTerminalServer.Send(item.frame) + s.mu.Lock() + s.sending = false + s.mu.Unlock() + } + item.result <- err + case <-s.stop: + return + case <-s.Context().Done(): + return + } + } +} + +func (s *checkedTerminalStream) closeSender() { + s.mu.Lock() + s.closed = true + sending := s.sending + close(s.stop) + s.mu.Unlock() + if !sending { + <-s.stopped + } +} + +func (s *checkedTerminalStream) validateResponse(frame *tunnel.LifecycleTerminalResponse) error { + if frame == nil { + return invalidTerminalResponse() + } + return s.dispatchResponse(frame) +} + +func (s *checkedTerminalStream) dispatchResponse(frame *tunnel.LifecycleTerminalResponse) error { + switch f := frame.Frame.(type) { + case *tunnel.LifecycleTerminalResponse_Ready: + return s.validateReady(f) + case *tunnel.LifecycleTerminalResponse_BeginAck: + return s.validateBeginAck(f) + case *tunnel.LifecycleTerminalResponse_Input: + return s.validateInput(f) + case *tunnel.LifecycleTerminalResponse_Resize: + return s.validateResize(f) + case *tunnel.LifecycleTerminalResponse_Cancel: + return s.validateCancel(f) + case *tunnel.LifecycleTerminalResponse_ExitAck: + return s.validateExitAck(f) + default: + return invalidTerminalResponse() + } +} + +func (s *checkedTerminalStream) validateReady(f *tunnel.LifecycleTerminalResponse_Ready) error { + v := f.Ready + if s.phase != clientReady || v == nil { + return invalidTerminalResponse() + } + if v.ProtocolVersion != tunnel.LifecycleTerminalProtocolVersion || !v.Input || + !validSize(v.Rows, v.Columns) || + !validTerm(v.Term) { + return status.Error(codes.InvalidArgument, "invalid terminal ready") + } + s.phase = clientIdle + return nil +} + +func (s *checkedTerminalStream) validateBeginAck( + f *tunnel.LifecycleTerminalResponse_BeginAck, +) error { + v := f.BeginAck + if s.phase != clientBeginAck || v == nil || v.InvocationId != s.invocation { + return invalidTerminalResponse() + } + if !validSize(v.Rows, v.Columns) { + return status.Error(codes.InvalidArgument, "invalid terminal size") + } + s.phase = clientActive + return nil +} + +func (s *checkedTerminalStream) validateInput(f *tunnel.LifecycleTerminalResponse_Input) error { + v := f.Input + if (s.phase != clientActive && s.phase != clientExitAck) || v == nil || + v.InvocationId != s.invocation { + return invalidTerminalResponse() + } + if len(v.Data) > tunnel.LifecycleTerminalMaxFrame { + return status.Error(codes.ResourceExhausted, "terminal input frame too large") + } + return nil +} + +func (s *checkedTerminalStream) validateResize(f *tunnel.LifecycleTerminalResponse_Resize) error { + v := f.Resize + if (s.phase != clientActive && s.phase != clientExitAck) || v == nil || + v.InvocationId != s.invocation { + return invalidTerminalResponse() + } + if !validSize(v.Rows, v.Columns) { + return status.Error(codes.InvalidArgument, "invalid terminal size") + } + return nil +} + +func (s *checkedTerminalStream) validateCancel(f *tunnel.LifecycleTerminalResponse_Cancel) error { + if s.phase != clientActive || f.Cancel == nil || f.Cancel.InvocationId != s.invocation { + return invalidTerminalResponse() + } + return nil +} + +func (s *checkedTerminalStream) validateExitAck(f *tunnel.LifecycleTerminalResponse_ExitAck) error { + if s.phase != clientExitAck || f.ExitAck == nil || f.ExitAck.InvocationId != s.invocation { + return invalidTerminalResponse() + } + s.phase = clientIdle + return nil +} diff --git a/pkg/agent/tunnelserver/lifecycle_terminal_client.go b/pkg/agent/tunnelserver/lifecycle_terminal_client.go new file mode 100644 index 0000000000..a666100828 --- /dev/null +++ b/pkg/agent/tunnelserver/lifecycle_terminal_client.go @@ -0,0 +1,709 @@ +package tunnelserver + +import ( + "context" + "errors" + "fmt" + "io" + "math" + "strings" + "sync" + + "github.com/devsy-org/devsy/pkg/agent/tunnel" + provider2 "github.com/devsy-org/devsy/pkg/provider" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +type terminalClientPhase uint8 + +const ( + clientReady terminalClientPhase = iota + clientIdle + clientBeginAck + clientActive + clientExitAck + clientClosed +) + +type terminalSend struct { + frame *tunnel.LifecycleTerminalRequest + done chan error +} +type terminalClient struct { + ctx context.Context + cancel context.CancelFunc + stream tunnel.Tunnel_LifecycleTerminalClient + sends chan terminalSend + responses chan *tunnel.LifecycleTerminalResponse + recvDone chan struct{} + wg sync.WaitGroup + mu sync.Mutex + active *terminalAttachment + closed bool + err error + term string + phase terminalClientPhase +} +type terminalAttachment struct { + session *terminalClient + id string + window tunnel.TerminalWindow + input *terminalInput + resizes chan tunnel.TerminalWindow + canceled chan struct{} + cancelOnce sync.Once + outputCancel context.CancelFunc + outputCtx context.Context + mu sync.Mutex + sequence uint64 + exited bool +} + +func OpenLifecycleTerminal( + ctx context.Context, + client tunnel.TunnelClient, + req provider2.LifecycleTerminalRequest, +) (tunnel.TerminalSession, error) { + streamCtx, cancel := context.WithCancel(ctx) + stream, err := client.LifecycleTerminal(streamCtx) + if err != nil { + cancel() + return nil, err + } + s := &terminalClient{ + ctx: streamCtx, + cancel: cancel, + stream: stream, + sends: make(chan terminalSend), + responses: make(chan *tunnel.LifecycleTerminalResponse, 1), + recvDone: make(chan struct{}), + } + s.wg.Add(2) + go s.sendLoop() + go s.receiveLoop() + frame := &tunnel.LifecycleTerminalRequest{ + Frame: &tunnel.LifecycleTerminalRequest_Open{ + Open: &tunnel.LifecycleTerminalOpen{ + ProtocolVersion: req.ProtocolVersion, + WorkspaceUid: req.WorkspaceUID, + Context: req.Context, + Nonce: req.Nonce, + }, + }, + } + hctx, stop := context.WithTimeout(ctx, tunnel.LifecycleTerminalHandshakeTimeout) + defer stop() + if err = s.send(hctx, frame); err == nil { + var response *tunnel.LifecycleTerminalResponse + response, err = s.wait(hctx) + if err == nil { + err = s.acceptReady(response.GetReady()) + } + } + if err != nil { + s.shutdown(err) + return nil, err + } + return s, nil +} + +func validSize( + rows, cols uint32, +) bool { + return rows > 0 && rows <= 65535 && cols > 0 && cols <= 65535 +} + +func validTerm(term string) bool { + if term == "" || len(term) > 128 { + return false + } + for _, c := range term { + if !validTermCharacter(c) { + return false + } + } + return true +} + +func validTermCharacter(c rune) bool { + return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || + strings.ContainsRune("-_.+", c) +} + +func (s *terminalClient) Begin( + ctx context.Context, + inv tunnel.TerminalInvocation, +) (tunnel.TerminalAttachment, error) { + inv.Key = terminalDisplayKey(inv.Key) + if !validTerminalInvocation(inv) { + return nil, status.Error(codes.InvalidArgument, "invalid terminal invocation") + } + s.mu.Lock() + if !s.available() { + s.mu.Unlock() + return nil, status.Error(codes.FailedPrecondition, "terminal session unavailable") + } + outputCtx, outputCancel := context.WithCancel(s.ctx) + a := &terminalAttachment{ + outputCtx: outputCtx, outputCancel: outputCancel, + session: s, + id: inv.ID, + input: newTerminalInput(), + resizes: make(chan tunnel.TerminalWindow, 1), + canceled: make(chan struct{}), + } + s.active = a + s.phase = clientBeginAck + s.mu.Unlock() + hctx, cancel := context.WithTimeout(ctx, tunnel.LifecycleTerminalHandshakeTimeout) + defer cancel() + err := s.send( + hctx, + &tunnel.LifecycleTerminalRequest{ + Frame: &tunnel.LifecycleTerminalRequest_Begin{ + Begin: &tunnel.LifecycleTerminalBegin{ + InvocationId: inv.ID, + Phase: inv.Phase, + Index: terminalIndex(inv.Index), + Key: inv.Key, + }, + }, + }, + ) + if err == nil { + var response *tunnel.LifecycleTerminalResponse + response, err = s.wait(hctx) + if err == nil { + err = s.acceptBeginAck(a, inv.ID, response.GetBeginAck()) + } + } + if err != nil { + s.shutdown(err) + return nil, err + } + return a, nil +} + +func terminalWindow(term string, rows, columns uint32) tunnel.TerminalWindow { + r, ok := terminalUint16(rows) + if !ok { + panic("invalid terminal rows") + } + c, ok := terminalUint16(columns) + if !ok { + panic("invalid terminal columns") + } + return tunnel.TerminalWindow{Term: term, Rows: r, Columns: c} +} + +func terminalUint16(v uint32) (uint16, bool) { + if v > math.MaxUint16 { + return 0, false + } + return uint16(v), true +} + +func terminalIndex(v int) uint32 { + if v < 0 || uint64(v) > math.MaxUint32 { + panic("invalid terminal index") + } + return uint32(v) +} + +func terminalExitCode(v int) int32 { + n := int64(v) % (int64(math.MaxUint32) + 1) + if n > math.MaxInt32 { + n -= int64(math.MaxUint32) + 1 + } + if n < math.MinInt32 { + n += int64(math.MaxUint32) + 1 + } + if n < math.MinInt32 || n > math.MaxInt32 { + panic("unreachable exit code") + } + return int32(n) +} + +func validTerminalInvocation(inv tunnel.TerminalInvocation) bool { + return inv.ID != "" && len(inv.ID) <= 256 && len(inv.Key) <= 256 && + inv.Phase == "postStartCommand" && + inv.Index >= 0 && + uint64(inv.Index) <= uint64(^uint32(0)) +} + +func (s *terminalClient) Close(ctx context.Context) error { + s.mu.Lock() + if s.closed { + s.mu.Unlock() + return nil + } + s.closed = true + active := s.active + if active == nil { + s.phase = clientClosed + } + s.mu.Unlock() + if active != nil { + err := status.Error(codes.FailedPrecondition, "terminal invocation active") + s.shutdown(err) + return err + } + err := s.send( + ctx, + &tunnel.LifecycleTerminalRequest{ + Frame: &tunnel.LifecycleTerminalRequest_Complete{ + Complete: &tunnel.LifecycleTerminalComplete{}, + }, + }, + ) + if err == nil { + select { + case <-s.recvDone: + if !errors.Is(s.failure(), io.EOF) { + err = s.failure() + } + case <-ctx.Done(): + err = ctx.Err() + } + } + s.shutdown(io.EOF) + return err +} +func (a *terminalAttachment) Window() tunnel.TerminalWindow { return a.window } +func (a *terminalAttachment) Output() io.Writer { return a } +func (a *terminalAttachment) Input() io.ReadCloser { return a.input } +func (a *terminalAttachment) Resizes() <-chan tunnel.TerminalWindow { return a.resizes } +func (a *terminalAttachment) Canceled() <-chan struct{} { return a.canceled } +func (a *terminalAttachment) Abort() { a.outputCancel() } +func (a *terminalAttachment) Write(p []byte) (int, error) { + a.mu.Lock() + defer a.mu.Unlock() + if a.exited { + return 0, status.Error(codes.FailedPrecondition, "terminal invocation exited") + } + written := 0 + for len(p) > 0 { + n := min(len(p), tunnel.LifecycleTerminalMaxFrame) + a.sequence++ + err := a.session.sendOutput( + a.outputCtx, + &tunnel.LifecycleTerminalRequest{ + Frame: &tunnel.LifecycleTerminalRequest_Output{ + Output: &tunnel.LifecycleTerminalOutput{ + InvocationId: a.id, + Sequence: a.sequence, + Data: append([]byte(nil), p[:n]...), + }, + }, + }, + ) + if err != nil { + return written, err + } + written += n + p = p[n:] + } + return written, nil +} + +func (a *terminalAttachment) Exit(ctx context.Context, exit tunnel.TerminalExit) error { + a.Abort() + a.mu.Lock() + defer a.mu.Unlock() + if a.exited { + return status.Error(codes.FailedPrecondition, "terminal invocation exited") + } + a.exited = true + a.session.mu.Lock() + if a.session.phase != clientActive { + a.session.mu.Unlock() + return status.Error(codes.FailedPrecondition, "terminal invocation inactive") + } + a.session.phase = clientExitAck + a.session.mu.Unlock() + a.input.close(io.EOF) + ectx, cancel := context.WithTimeout(ctx, tunnel.LifecycleTerminalExitAckTimeout) + defer cancel() + err := a.session.send( + ectx, + &tunnel.LifecycleTerminalRequest{ + Frame: &tunnel.LifecycleTerminalRequest_Exit{ + Exit: &tunnel.LifecycleTerminalExit{ + InvocationId: a.id, + ExitCode: terminalExitCode(exit.ExitCode), + Signal: exit.Signal, + StartError: exit.StartError, + }, + }, + }, + ) + if err == nil { + var frame *tunnel.LifecycleTerminalResponse + frame, err = a.session.wait(ectx) + if err == nil && (frame.GetExitAck() == nil || frame.GetExitAck().InvocationId != a.id) { + err = fmt.Errorf("invalid terminal exit acknowledgement") + } + } + if err != nil { + a.session.shutdown(err) + return err + } + a.session.mu.Lock() + a.session.active = nil + a.session.mu.Unlock() + return nil +} + +type terminalInput struct { + mu sync.Mutex + data []byte + err error + wake chan struct{} +} + +func newTerminalInput() *terminalInput { return &terminalInput{wake: make(chan struct{}, 1)} } + +func (b *terminalInput) Read(p []byte) (int, error) { + if len(p) == 0 { + return 0, nil + } + for { + b.mu.Lock() + if len(b.data) > 0 { + n := copy(p, b.data) + b.data = b.data[n:] + b.mu.Unlock() + return n, nil + } + err := b.err + b.mu.Unlock() + if err != nil { + return 0, err + } + <-b.wake + } +} + +func (b *terminalInput) Close() error { b.close(io.EOF); return nil } + +func queueTerminalResize(resizes chan tunnel.TerminalWindow, size tunnel.TerminalWindow) { + select { + case resizes <- size: + default: + select { + case <-resizes: + default: + } + select { + case resizes <- size: + default: + } + } +} + +func (s *terminalClient) acceptReady(ready *tunnel.LifecycleTerminalReady) error { + if ready == nil || ready.ProtocolVersion != tunnel.LifecycleTerminalProtocolVersion || + !ready.Input || + !validSize(ready.Rows, ready.Columns) || + len(ready.Term) > 256 { + return errors.New("invalid lifecycle terminal ready") + } + s.term = ready.Term + return nil +} + +func (s *terminalClient) available() bool { + return !s.closed && s.active == nil && s.err == nil && s.phase == clientIdle +} + +func (s *terminalClient) acceptBeginAck( + a *terminalAttachment, + id string, + ack *tunnel.LifecycleTerminalBeginAck, +) error { + if ack == nil || ack.InvocationId != id || !validSize(ack.Rows, ack.Columns) { + return errors.New("invalid terminal begin acknowledgement") + } + a.window = terminalWindow(s.term, ack.Rows, ack.Columns) + return nil +} + +func (s *terminalClient) sendLoop() { + defer s.wg.Done() + for { + select { + case item := <-s.sends: + err := s.stream.Send(item.frame) + item.done <- err + if err != nil { + s.fail(err) + return + } + case <-s.ctx.Done(): + return + } + } +} + +func (s *terminalClient) receiveLoop() { + defer s.wg.Done() + defer close(s.recvDone) + for { + frame, err := s.stream.Recv() + if err != nil { + s.fail(err) + return + } + if err = s.handleResponse(frame); err != nil { + s.fail(err) + return + } + } +} + +func (s *terminalClient) handleResponse(frame *tunnel.LifecycleTerminalResponse) error { + s.mu.Lock() + defer s.mu.Unlock() + if frame == nil { + return invalidTerminalResponse() + } + enqueue, err := s.dispatchClientResponse(frame) + if err != nil || !enqueue { + return err + } + select { + case s.responses <- frame: + return nil + case <-s.ctx.Done(): + return s.ctx.Err() + } +} + +func (s *terminalClient) dispatchClientResponse( + frame *tunnel.LifecycleTerminalResponse, +) (bool, error) { + switch f := frame.Frame.(type) { + case *tunnel.LifecycleTerminalResponse_Ready, + *tunnel.LifecycleTerminalResponse_BeginAck, + *tunnel.LifecycleTerminalResponse_ExitAck: + if err := s.handleControlResponse(frame); err != nil { + return false, err + } + case *tunnel.LifecycleTerminalResponse_Input: + return false, s.handleInput(f) + case *tunnel.LifecycleTerminalResponse_Resize: + return false, s.handleResize(f) + case *tunnel.LifecycleTerminalResponse_Cancel: + return false, s.handleCancel(f) + default: + return false, invalidTerminalResponse() + } + return true, nil +} + +func (s *terminalClient) handleControlResponse(frame *tunnel.LifecycleTerminalResponse) error { + switch f := frame.Frame.(type) { + case *tunnel.LifecycleTerminalResponse_Ready: + return s.handleReady(f) + case *tunnel.LifecycleTerminalResponse_BeginAck: + return s.handleBeginAck(f) + case *tunnel.LifecycleTerminalResponse_ExitAck: + return s.handleExitAck(f) + default: + return invalidTerminalResponse() + } +} + +func (s *terminalClient) handleReady(f *tunnel.LifecycleTerminalResponse_Ready) error { + v := f.Ready + if s.phase != clientReady || v == nil { + return invalidTerminalResponse() + } + if v.ProtocolVersion != tunnel.LifecycleTerminalProtocolVersion || !v.Input || + !validSize(v.Rows, v.Columns) || + !validTerm(v.Term) { + return status.Error(codes.InvalidArgument, "invalid terminal ready") + } + s.term = v.Term + s.phase = clientIdle + return nil +} + +func (s *terminalClient) handleBeginAck(f *tunnel.LifecycleTerminalResponse_BeginAck) error { + a := s.active + v := f.BeginAck + if s.phase != clientBeginAck || a == nil || v == nil || v.InvocationId != a.id { + return invalidTerminalResponse() + } + if !validSize(v.Rows, v.Columns) { + return status.Error(codes.InvalidArgument, "invalid terminal size") + } + s.phase = clientActive + return nil +} + +func (s *terminalClient) handleExitAck(f *tunnel.LifecycleTerminalResponse_ExitAck) error { + a := s.active + v := f.ExitAck + if s.phase != clientExitAck || a == nil || v == nil || v.InvocationId != a.id { + return invalidTerminalResponse() + } + s.phase = clientIdle + return nil +} + +func (s *terminalClient) handleInput(f *tunnel.LifecycleTerminalResponse_Input) error { + a := s.active + v := f.Input + if (s.phase != clientActive && s.phase != clientExitAck) || a == nil || v == nil || + v.InvocationId != a.id { + return invalidTerminalResponse() + } + if len(v.Data) > tunnel.LifecycleTerminalMaxFrame { + return status.Error(codes.ResourceExhausted, "terminal input frame too large") + } + if s.phase == clientExitAck { + return nil + } + return a.input.append(v.Data) +} + +func (s *terminalClient) handleResize(f *tunnel.LifecycleTerminalResponse_Resize) error { + a := s.active + v := f.Resize + if (s.phase != clientActive && s.phase != clientExitAck) || a == nil || v == nil || + v.InvocationId != a.id { + return invalidTerminalResponse() + } + if !validSize(v.Rows, v.Columns) { + return status.Error(codes.InvalidArgument, "invalid terminal size") + } + size := terminalWindow(s.term, v.Rows, v.Columns) + queueTerminalResize(a.resizes, size) + return nil +} + +func (s *terminalClient) handleCancel(f *tunnel.LifecycleTerminalResponse_Cancel) error { + a := s.active + if s.phase != clientActive || a == nil || f.Cancel == nil || f.Cancel.InvocationId != a.id { + return invalidTerminalResponse() + } + return status.Error(codes.Canceled, "lifecycle terminal canceled") +} + +func (s *terminalClient) fail(err error) { + s.mu.Lock() + if s.err == nil { + s.err = err + } + a := s.active + s.mu.Unlock() + if a != nil { + a.input.close(err) + a.cancelOnce.Do(func() { close(a.canceled) }) + } + s.cancel() +} + +func (s *terminalClient) send(ctx context.Context, frame *tunnel.LifecycleTerminalRequest) error { + return s.sendItem(ctx, terminalSend{frame: frame, done: make(chan error, 1)}) +} + +func (s *terminalClient) sendItem(ctx context.Context, item terminalSend) error { + select { + case s.sends <- item: + case <-ctx.Done(): + return ctx.Err() + case <-s.ctx.Done(): + return s.failure() + } + select { + case err := <-item.done: + return err + case <-ctx.Done(): + s.fail(ctx.Err()) + return ctx.Err() + case <-s.ctx.Done(): + return s.failure() + } +} + +func (s *terminalClient) sendOutput( + ctx context.Context, + frame *tunnel.LifecycleTerminalRequest, +) error { + item := terminalSend{frame: frame, done: make(chan error, 1)} + select { + case s.sends <- item: + case <-ctx.Done(): + return ctx.Err() + case <-s.ctx.Done(): + return s.failure() + } + select { + case err := <-item.done: + return err + case <-ctx.Done(): + return ctx.Err() + case <-s.ctx.Done(): + return s.failure() + } +} + +func (s *terminalClient) failure() error { + s.mu.Lock() + defer s.mu.Unlock() + if s.err != nil { + return s.err + } + return s.ctx.Err() +} + +func (s *terminalClient) wait(ctx context.Context) (*tunnel.LifecycleTerminalResponse, error) { + select { + case frame := <-s.responses: + return frame, nil + case <-ctx.Done(): + return nil, ctx.Err() + case <-s.ctx.Done(): + return nil, s.failure() + } +} + +func (s *terminalClient) shutdown(err error) { + s.fail(err) + s.wg.Wait() +} + +func (b *terminalInput) append(p []byte) error { + b.mu.Lock() + defer b.mu.Unlock() + if b.err != nil { + if errors.Is(b.err, io.EOF) { + return nil + } + return b.err + } + if len(b.data)+len(p) > 256<<10 { + return status.Error(codes.ResourceExhausted, "lifecycle terminal input buffer full") + } + b.data = append(b.data, p...) + select { + case b.wake <- struct{}{}: + default: + } + return nil +} + +func (b *terminalInput) close(err error) { + b.mu.Lock() + if b.err == nil { + b.err = err + } + b.mu.Unlock() + select { + case b.wake <- struct{}{}: + default: + } +} diff --git a/pkg/agent/tunnelserver/lifecycle_terminal_test.go b/pkg/agent/tunnelserver/lifecycle_terminal_test.go new file mode 100644 index 0000000000..a929ba833b --- /dev/null +++ b/pkg/agent/tunnelserver/lifecycle_terminal_test.go @@ -0,0 +1,1187 @@ +package tunnelserver + +import ( + "bytes" + "context" + "io" + "net" + "strings" + "sync" + "testing" + "time" + "unicode/utf8" + + "github.com/devsy-org/devsy/pkg/agent/tunnel" + provider2 "github.com/devsy-org/devsy/pkg/provider" + "github.com/stretchr/testify/suite" + "go.uber.org/goleak" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/credentials/insecure" + "google.golang.org/grpc/status" + "google.golang.org/grpc/test/bufconn" +) + +const ( + testUID = "uid" + testNonce = "nonce" + testContext = "context" + testBad = "bad" + testPostStartCommand = "postStartCommand" +) + +type terminalSuite struct { + suite.Suite + req provider2.LifecycleTerminalRequest +} +type testTerminalOwner struct { + serve func(context.Context, tunnel.Tunnel_LifecycleTerminalServer) error +} + +func (o testTerminalOwner) Serve( + ctx context.Context, + stream tunnel.Tunnel_LifecycleTerminalServer, +) error { + return o.serve(ctx, stream) +} +func TestLifecycleTerminalSuite(t *testing.T) { suite.Run(t, new(terminalSuite)) } +func (s *terminalSuite) SetupTest() { + s.req = provider2.LifecycleTerminalRequest{ + ProtocolVersion: 1, + WorkspaceUID: testUID, + Context: "ctx", + Nonce: testNonce, + } +} + +func ready(stream tunnel.Tunnel_LifecycleTerminalServer) error { + return stream.Send( + &tunnel.LifecycleTerminalResponse{ + Frame: &tunnel.LifecycleTerminalResponse_Ready{ + Ready: &tunnel.LifecycleTerminalReady{ + ProtocolVersion: 1, + Term: "xterm", + Rows: 24, + Columns: 80, + Input: true, + }, + }, + }, + ) +} + +func ack(stream tunnel.Tunnel_LifecycleTerminalServer, id string) error { + return stream.Send( + &tunnel.LifecycleTerminalResponse{ + Frame: &tunnel.LifecycleTerminalResponse_BeginAck{ + BeginAck: &tunnel.LifecycleTerminalBeginAck{ + InvocationId: id, + Rows: 25, + Columns: 81, + }, + }, + }, + ) +} + +func openFrame(req provider2.LifecycleTerminalRequest) *tunnel.LifecycleTerminalRequest { + return &tunnel.LifecycleTerminalRequest{ + Frame: &tunnel.LifecycleTerminalRequest_Open{ + Open: &tunnel.LifecycleTerminalOpen{ + ProtocolVersion: req.ProtocolVersion, + WorkspaceUid: req.WorkspaceUID, + Context: req.Context, + Nonce: req.Nonce, + }, + }, + } +} + +func (s *terminalSuite) TestIdentityAndMissingOwner() { + owner := testTerminalOwner{ + serve: func(_ context.Context, st tunnel.Tunnel_LifecycleTerminalServer) error { return ready(st) }, + } + client := s.client(WithLifecycleTerminalOwner(owner, s.req)) + for _, field := range []string{testNonce, testUID, testContext} { + s.Run(field, func() { + req := s.req + switch field { + case testNonce: + req.Nonce = testBad + case testUID: + req.WorkspaceUID = testBad + case testContext: + req.Context = testBad + } + st, err := client.LifecycleTerminal(context.Background()) + s.Require().NoError(err) + s.Require().NoError(st.Send(openFrame(req))) + _, err = st.Recv() + s.Equal(codes.PermissionDenied, status.Code(err)) + }) + } + st, err := s.client().LifecycleTerminal(context.Background()) + s.Require().NoError(err) + _ = st.Send(openFrame(s.req)) + _, err = st.Recv() + s.Equal(codes.FailedPrecondition, status.Code(err)) +} + +func (s *terminalSuite) TestDuplicateOwner() { + release := make(chan struct{}) + owner := testTerminalOwner{ + serve: func(ctx context.Context, st tunnel.Tunnel_LifecycleTerminalServer) error { + if err := ready(st); err != nil { + return err + } + select { + case <-release: + return nil + case <-ctx.Done(): + return ctx.Err() + } + }, + } + client := s.client(WithLifecycleTerminalOwner(owner, s.req)) + first, err := client.LifecycleTerminal(context.Background()) + s.Require().NoError(err) + s.Require().NoError(first.Send(openFrame(s.req))) + _, err = first.Recv() + s.Require().NoError(err) + second, err := client.LifecycleTerminal(context.Background()) + s.Require().NoError(err) + s.Require().NoError(second.Send(openFrame(s.req))) + _, err = second.Recv() + s.Equal(codes.FailedPrecondition, status.Code(err)) + close(release) +} + +func (s *terminalSuite) TestSessionOutputAndUseAfterExit() { + received := make(chan []byte, 1) + owner := testTerminalOwner{ + serve: (&ownerSessionOutputAndUseAfterExit{received: received}).serve, + } + client := s.client(WithLifecycleTerminalOwner(owner, s.req)) + session, err := OpenLifecycleTerminal(context.Background(), client, s.req) + s.Require().NoError(err) + a, err := session.Begin( + context.Background(), + tunnel.TerminalInvocation{ID: "one", Phase: testPostStartCommand}, + ) + s.Require().NoError(err) + s.Equal(uint16(25), a.Window().Rows) + _, err = session.Begin( + context.Background(), + tunnel.TerminalInvocation{ID: "two", Phase: testPostStartCommand}, + ) + s.Equal(codes.FailedPrecondition, status.Code(err)) + data := bytes.Repeat([]byte("abcdef"), 100000) + var wg sync.WaitGroup + for range 2 { + wg.Go(func() { ; _, _ = a.Output().Write(data) }) + } + wg.Wait() + s.Require().NoError(a.Exit(context.Background(), tunnel.TerminalExit{ExitCode: 3})) + s.Equal(append(bytes.Clone(data), data...), <-received) + _, err = a.Output().Write([]byte("x")) + s.Equal(codes.FailedPrecondition, status.Code(err)) + s.Require().NoError(session.Close(context.Background())) + s.NoError(session.Close(context.Background())) +} + +func (s *terminalSuite) TestInputOverflow() { + proceed := make(chan struct{}) + owner := testTerminalOwner{ + serve: func(_ context.Context, st tunnel.Tunnel_LifecycleTerminalServer) error { + if err := ready(st); err != nil { + return err + } + f, err := st.Recv() + if err != nil { + return err + } + id := f.GetBegin().InvocationId + if err = ack(st, id); err != nil { + return err + } + <-proceed + for range 9 { + if err = st.Send( + &tunnel.LifecycleTerminalResponse{ + Frame: &tunnel.LifecycleTerminalResponse_Input{ + Input: &tunnel.LifecycleTerminalInput{ + InvocationId: id, + Data: make([]byte, 32<<10), + }, + }, + }, + ); err != nil { + return err + } + } + <-st.Context().Done() + return st.Context().Err() + }, + } + client := s.client(WithLifecycleTerminalOwner(owner, s.req)) + session, err := OpenLifecycleTerminal(context.Background(), client, s.req) + s.Require().NoError(err) + a, err := session.Begin( + context.Background(), + tunnel.TerminalInvocation{ID: "id", Phase: testPostStartCommand}, + ) + s.Require().NoError(err) + close(proceed) + select { + case <-a.Canceled(): + case <-time.After(5 * time.Second): + s.FailNow("input overflow did not cancel") + } + err = a.Exit(context.Background(), tunnel.TerminalExit{}) + s.Error(err) + s.Eventually(func() bool { + var buf [32768]byte + _, e := a.Input().Read(buf[:]) + return status.Code(e) == codes.ResourceExhausted + }, time.Second, time.Millisecond) +} + +var _ io.Reader = (*terminalInput)(nil) + +func (s *terminalSuite) TestInvalidRequestFrames() { + tests := invalidRequestCases() + for _, tc := range tests { + s.Run(tc.name, func() { + owner := testTerminalOwner{ + serve: func(_ context.Context, st tunnel.Tunnel_LifecycleTerminalServer) error { + if err := ready(st); err != nil { + return err + } + f, err := st.Recv() + if err != nil { + return err + } + if err = ack(st, f.GetBegin().InvocationId); err != nil { + return err + } + _, err = st.Recv() + return err + }, + } + client := s.client(WithLifecycleTerminalOwner(owner, s.req)) + st, err := client.LifecycleTerminal(context.Background()) + s.Require().NoError(err) + s.Require().NoError(st.Send(openFrame(s.req))) + _, err = st.Recv() + s.Require().NoError(err) + s.Require(). + NoError(st.Send(&tunnel.LifecycleTerminalRequest{ + Frame: &tunnel.LifecycleTerminalRequest_Begin{ + Begin: &tunnel.LifecycleTerminalBegin{ + InvocationId: "id", Phase: testPostStartCommand, + }, + }, + })) + _, err = st.Recv() + s.Require().NoError(err) + s.Require().NoError(st.Send(tc.frame)) + _, err = st.Recv() + s.Equal(tc.code, status.Code(err)) + }) + } +} + +func (s *terminalSuite) TestTwoHopAndOtherRPCsWhileStalled() { + started := make(chan struct{}) + release := make(chan struct{}) + finished := make(chan struct{}) + owner := testTerminalOwner{ + serve: func(ctx context.Context, st tunnel.Tunnel_LifecycleTerminalServer) error { + defer close(finished) + if err := ready(st); err != nil { + return err + } + close(started) + select { + case <-release: + return nil + case <-ctx.Done(): + return ctx.Err() + } + }, + } + cli := s.client(WithLifecycleTerminalOwner(owner, s.req)) + host := s.client( + WithLifecycleTerminalUpstream(cli, s.req), + WithAllowGitCredentials(true), + WithGitToken( + &provider2.GitToken{Host: "example.com", Username: "user", Token: "test-token"}, + ), + ) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + session, err := OpenLifecycleTerminal(ctx, host, s.req) + s.Require().NoError(err) + <-started + rpcCtx, stop := context.WithTimeout(context.Background(), 3*time.Second) + defer stop() + _, err = host.Status(rpcCtx, &tunnel.StatusUpdate{Phase: "test", State: "started"}) + s.NoError(err) + _, err = host.SendResult(rpcCtx, &tunnel.Message{Message: "{}"}) + s.NoError(err) + _, err = host.GitCredentials(rpcCtx, &tunnel.Message{Message: `{"host":"example.com"}`}) + s.NoError(err) + cancel() + close(release) + s.Eventually(func() bool { + select { + case <-finished: + return true + default: + return false + } + }, 3*time.Second, 10*time.Millisecond) + _ = session.Close(context.Background()) +} + +func (s *terminalSuite) TestExitAckTimeoutClosesSession() { + owner := testTerminalOwner{ + serve: func(ctx context.Context, st tunnel.Tunnel_LifecycleTerminalServer) error { + if err := ready(st); err != nil { + return err + } + f, err := st.Recv() + if err != nil { + return err + } + if err = ack(st, f.GetBegin().InvocationId); err != nil { + return err + } + _, err = st.Recv() + if err != nil { + return err + } + <-ctx.Done() + return ctx.Err() + }, + } + client := s.client(WithLifecycleTerminalOwner(owner, s.req)) + session, err := OpenLifecycleTerminal(context.Background(), client, s.req) + s.Require().NoError(err) + a, err := session.Begin( + context.Background(), + tunnel.TerminalInvocation{ID: "id", Phase: testPostStartCommand}, + ) + s.Require().NoError(err) + started := time.Now() + err = a.Exit(context.Background(), tunnel.TerminalExit{ExitCode: 17}) + s.GreaterOrEqual(time.Since(started), tunnel.LifecycleTerminalExitAckTimeout) + s.ErrorIs(err, context.DeadlineExceeded) + _, err = session.Begin( + context.Background(), + tunnel.TerminalInvocation{ID: "next", Phase: testPostStartCommand}, + ) + s.Error(err) +} + +func (s *terminalSuite) TestTwoHopFrameExchange() { + data := []byte("prompt without newline") + owner := testTerminalOwner{ + serve: (&ownerTwoHopFrameExchange{data: data}).serve, + } + cli := s.client(WithLifecycleTerminalOwner(owner, s.req)) + host := s.client(WithLifecycleTerminalUpstream(cli, s.req)) + session, err := OpenLifecycleTerminal(context.Background(), host, s.req) + s.Require().NoError(err) + a, err := session.Begin( + context.Background(), + tunnel.TerminalInvocation{ID: "id", Phase: testPostStartCommand}, + ) + s.Require().NoError(err) + _, err = a.Output().Write(data) + s.Require().NoError(err) + buf := make([]byte, 5) + _, err = io.ReadFull(a.Input(), buf) + s.Require().NoError(err) + s.Equal("reply", string(buf)) + s.NoError(a.Exit(context.Background(), tunnel.TerminalExit{})) + s.NoError(session.Close(context.Background())) +} + +func (s *terminalSuite) TestConcurrentOwnerSendsSerialize() { + owner := testTerminalOwner{ + serve: ownerConcurrentOwnerSendsSerialize, + } + client := s.client(WithLifecycleTerminalOwner(owner, s.req)) + session, err := OpenLifecycleTerminal(context.Background(), client, s.req) + s.Require().NoError(err) + a, err := session.Begin( + context.Background(), + tunnel.TerminalInvocation{ID: "id", Phase: testPostStartCommand}, + ) + s.Require().NoError(err) + buf := make([]byte, 2) + _, err = io.ReadFull(a.Input(), buf) + s.Require().NoError(err) + s.ElementsMatch([]byte("ab"), buf) + s.NoError(a.Exit(context.Background(), tunnel.TerminalExit{})) + s.NoError(session.Close(context.Background())) +} + +func (s *terminalSuite) TestGoroutinesExitAfterClose() { + owner := testTerminalOwner{ + serve: func(_ context.Context, st tunnel.Tunnel_LifecycleTerminalServer) error { + if err := ready(st); err != nil { + return err + } + _, err := st.Recv() + return err + }, + } + cli := s.client(WithLifecycleTerminalOwner(owner, s.req)) + host := s.client(WithLifecycleTerminalUpstream(cli, s.req)) + _, err := cli.Ping(context.Background(), &tunnel.Empty{}) + s.Require().NoError(err) + _, err = host.Ping(context.Background(), &tunnel.Empty{}) + s.Require().NoError(err) + opts := []goleak.Option{goleak.IgnoreCurrent()} + session, err := OpenLifecycleTerminal(context.Background(), host, s.req) + s.Require().NoError(err) + s.Require().NoError(session.Close(context.Background())) + s.Eventually( + func() bool { return goleak.Find(opts...) == nil }, + 5*time.Second, + 50*time.Millisecond, + ) + s.NoError(goleak.Find(opts...)) +} + +func (s *terminalSuite) TestInputCloseUnblocksRead() { + owner := testTerminalOwner{ + serve: func(_ context.Context, st tunnel.Tunnel_LifecycleTerminalServer) error { + if err := ready(st); err != nil { + return err + } + f, err := st.Recv() + if err != nil { + return err + } + id := f.GetBegin().InvocationId + if err = ack(st, id); err != nil { + return err + } + _, err = st.Recv() + if err != nil { + return err + } + if err = st.Send( + &tunnel.LifecycleTerminalResponse{ + Frame: &tunnel.LifecycleTerminalResponse_ExitAck{ + ExitAck: &tunnel.LifecycleTerminalExitAck{InvocationId: id}, + }, + }, + ); err != nil { + return err + } + _, err = st.Recv() + return err + }, + } + client := s.client(WithLifecycleTerminalOwner(owner, s.req)) + session, err := OpenLifecycleTerminal(context.Background(), client, s.req) + s.Require().NoError(err) + a, err := session.Begin( + context.Background(), + tunnel.TerminalInvocation{ID: "id", Phase: testPostStartCommand}, + ) + s.Require().NoError(err) + reader := a.Input() + done := make(chan error, 1) + go func() { var b [1]byte; _, readErr := reader.Read(b[:]); done <- readErr }() + s.Require().NoError(reader.Close()) + select { + case err = <-done: + s.ErrorIs(err, io.EOF) + case <-time.After(time.Second): + s.FailNow("read did not unblock") + } + s.NoError(a.Exit(context.Background(), tunnel.TerminalExit{})) + s.NoError(session.Close(context.Background())) +} + +func (s *terminalSuite) TestOutputBackpressurePreservesBytes() { + received := make(chan []byte, 1) + stalled := make(chan struct{}) + release := make(chan struct{}) + owner := testTerminalOwner{ + serve: (&ownerOutputBackpressurePreservesBytes{received: received, stalled: stalled, release: release}).serve, + } + client := s.client(WithLifecycleTerminalOwner(owner, s.req)) + session, err := OpenLifecycleTerminal(context.Background(), client, s.req) + s.Require().NoError(err) + a, err := session.Begin( + context.Background(), + tunnel.TerminalInvocation{ID: "id", Phase: testPostStartCommand}, + ) + s.Require().NoError(err) + payload := bytes.Repeat([]byte("12345678"), 1<<20) + done := make(chan error, 1) + go func() { _, writeErr := a.Output().Write(payload); done <- writeErr }() + <-stalled + select { + case err = <-done: + s.FailNow("write did not apply backpressure", err) + case <-time.After(100 * time.Millisecond): + } + close(release) + select { + case err = <-done: + s.NoError(err) + case <-time.After(20 * time.Second): + s.FailNow("write did not finish") + } + s.NoError(a.Exit(context.Background(), tunnel.TerminalExit{})) + s.Equal(payload, <-received) + s.NoError(session.Close(context.Background())) +} + +func (s *terminalSuite) TestAbortOutputThenExit() { + received := make(chan []byte, 1) + stalled := make(chan struct{}) + release := make(chan struct{}) + owner := testTerminalOwner{ + serve: (&ownerOutputBackpressurePreservesBytes{received: received, stalled: stalled, release: release}).serve, + } + client := s.client(WithLifecycleTerminalOwner(owner, s.req)) + session, err := OpenLifecycleTerminal(context.Background(), client, s.req) + s.Require().NoError(err) + a, err := session.Begin( + context.Background(), + tunnel.TerminalInvocation{ID: "id", Phase: testPostStartCommand}, + ) + s.Require().NoError(err) + done := make(chan error, 1) + go func() { _, writeErr := a.Output().Write(bytes.Repeat([]byte("x"), 8<<20)); done <- writeErr }() + <-stalled + select { + case err = <-done: + s.FailNow("write did not apply backpressure", err) + case <-time.After(100 * time.Millisecond): + } + a.Abort() + select { + case err = <-done: + s.ErrorIs(err, context.Canceled) + case <-time.After(time.Second): + s.FailNow("write did not abort") + } + close(release) + s.NoError(a.Exit(context.Background(), tunnel.TerminalExit{ExitCode: 7})) + <-received + s.NoError(session.Close(context.Background())) +} + +func (s *terminalSuite) TestInvalidResponseFrames() { + tests := []struct { + name string + frame *tunnel.LifecycleTerminalResponse + code codes.Code + }{ + { + "wrong_begin_ack", + &tunnel.LifecycleTerminalResponse{ + Frame: &tunnel.LifecycleTerminalResponse_BeginAck{ + BeginAck: &tunnel.LifecycleTerminalBeginAck{ + InvocationId: "wrong", + Rows: 20, + Columns: 80, + }, + }, + }, + codes.FailedPrecondition, + }, + { + "input_before_ack", + &tunnel.LifecycleTerminalResponse{ + Frame: &tunnel.LifecycleTerminalResponse_Input{ + Input: &tunnel.LifecycleTerminalInput{InvocationId: "id", Data: []byte("x")}, + }, + }, + codes.FailedPrecondition, + }, + } + for _, tc := range tests { + s.Run(tc.name, func() { + owner := testTerminalOwner{ + serve: func(_ context.Context, st tunnel.Tunnel_LifecycleTerminalServer) error { + if err := ready(st); err != nil { + return err + } + if _, err := st.Recv(); err != nil { + return err + } + return st.Send(tc.frame) + }, + } + client := s.client(WithLifecycleTerminalOwner(owner, s.req)) + session, err := OpenLifecycleTerminal(context.Background(), client, s.req) + s.Require().NoError(err) + _, err = session.Begin( + context.Background(), + tunnel.TerminalInvocation{ID: "id", Phase: testPostStartCommand}, + ) + s.Equal(tc.code, status.Code(err)) + }) + } +} + +func (s *terminalSuite) TestCancelWithFullInputBuffer() { + proceed := make(chan struct{}) + sent := make(chan struct{}) + owner := testTerminalOwner{ + serve: (&ownerCancelWithFullInputBuffer{proceed: proceed, sent: sent}).serve, + } + client := s.client(WithLifecycleTerminalOwner(owner, s.req)) + session, err := OpenLifecycleTerminal(context.Background(), client, s.req) + s.Require().NoError(err) + a, err := session.Begin( + context.Background(), + tunnel.TerminalInvocation{ID: "id", Phase: testPostStartCommand}, + ) + s.Require().NoError(err) + close(proceed) + <-sent + select { + case <-a.Canceled(): + case <-time.After(3 * time.Second): + s.FailNow("cancel did not arrive") + } + err = a.Exit(context.Background(), tunnel.TerminalExit{}) + s.Equal(codes.Canceled, status.Code(err)) +} + +func (s *terminalSuite) TestBlockedServerSendExitsAfterHandlerReturn() { + release := make(chan struct{}) + senderDone := make(chan struct{}) + sending := make(chan struct{}) + owner := testTerminalOwner{ + serve: (&ownerBlockedServerSendExitsAfterHandlerReturn{ + release: release, senderDone: senderDone, sending: sending, + }).serve, + } + client := s.client(WithLifecycleTerminalOwner(owner, s.req)) + st, err := client.LifecycleTerminal(context.Background()) + s.Require().NoError(err) + s.Require().NoError(st.Send(openFrame(s.req))) + _, err = st.Recv() + s.Require().NoError(err) + s.Require(). + NoError(st.Send(&tunnel.LifecycleTerminalRequest{ + Frame: &tunnel.LifecycleTerminalRequest_Begin{Begin: &tunnel.LifecycleTerminalBegin{ + InvocationId: "id", Phase: testPostStartCommand, + }}, + })) + _, err = st.Recv() + s.Require().NoError(err) + <-sending + time.Sleep(100 * time.Millisecond) + close(release) + s.Eventually(func() bool { + select { + case <-senderDone: + return true + default: + return false + } + }, 5*time.Second, 10*time.Millisecond) +} + +func (s *terminalSuite) TestOversizedInputFrame() { + proceed := make(chan struct{}) + owner := testTerminalOwner{ + serve: func(_ context.Context, st tunnel.Tunnel_LifecycleTerminalServer) error { + if err := ready(st); err != nil { + return err + } + f, err := st.Recv() + if err != nil { + return err + } + id := f.GetBegin().InvocationId + if err = ack(st, id); err != nil { + return err + } + <-proceed + return st.Send( + &tunnel.LifecycleTerminalResponse{ + Frame: &tunnel.LifecycleTerminalResponse_Input{ + Input: &tunnel.LifecycleTerminalInput{ + InvocationId: id, + Data: make([]byte, (32<<10)+1), + }, + }, + }, + ) + }, + } + client := s.client(WithLifecycleTerminalOwner(owner, s.req)) + session, err := OpenLifecycleTerminal(context.Background(), client, s.req) + s.Require().NoError(err) + a, err := session.Begin( + context.Background(), + tunnel.TerminalInvocation{ID: "id", Phase: testPostStartCommand}, + ) + s.Require().NoError(err) + close(proceed) + select { + case <-a.Canceled(): + case <-time.After(3 * time.Second): + s.FailNow("oversized input did not fail") + } + err = a.Exit(context.Background(), tunnel.TerminalExit{}) + s.Equal(codes.ResourceExhausted, status.Code(err)) +} + +func (s *terminalSuite) TestForwarderReceiveExitsAfterUpstreamFailure() { + release := make(chan struct{}) + owner := testTerminalOwner{ + serve: func(_ context.Context, st tunnel.Tunnel_LifecycleTerminalServer) error { + if err := ready(st); err != nil { + return err + } + <-release + return status.Error(codes.Aborted, "upstream stopped") + }, + } + cli := s.client(WithLifecycleTerminalOwner(owner, s.req)) + host := s.client(WithLifecycleTerminalUpstream(cli, s.req)) + _, err := cli.Ping(context.Background(), &tunnel.Empty{}) + s.Require().NoError(err) + _, err = host.Ping(context.Background(), &tunnel.Empty{}) + s.Require().NoError(err) + opts := []goleak.Option{goleak.IgnoreCurrent()} + session, err := OpenLifecycleTerminal(context.Background(), host, s.req) + s.Require().NoError(err) + close(release) + s.Eventually( + func() bool { return goleak.Find(opts...) == nil }, + 5*time.Second, + 50*time.Millisecond, + ) + s.NoError(goleak.Find(opts...)) + _ = session.Close(context.Background()) +} + +func (s *terminalSuite) TestDisplayKey() { + for _, key := range []string{"a\nb", strings.Repeat("x", 300), strings.Repeat("a", 255) + "\u00e9"} { + display := terminalDisplayKey(key) + s.True(validIdentity(display, false)) + s.LessOrEqual(len(display), 256) + s.True(utf8.ValidString(display)) + } + s.Equal("a b", terminalDisplayKey("a\nb")) +} + +func (s *terminalSuite) client(opts ...Option) tunnel.TunnelClient { + listener := bufconn.Listen(1 << 20) + server := grpc.NewServer() + tunnel.RegisterTunnelServer(server, New(opts...)) + go func() { _ = server.Serve(listener) }() + conn, err := grpc.NewClient( + "passthrough:///terminal", + grpc.WithTransportCredentials(insecure.NewCredentials()), + grpc.WithContextDialer( + func(context.Context, string) (net.Conn, error) { return listener.Dial() }, + ), + ) + s.Require().NoError(err) + s.T().Cleanup(func() { _ = conn.Close(); server.Stop(); _ = listener.Close() }) + return tunnel.NewTunnelClient(conn) +} + +type ownerSessionOutputAndUseAfterExit struct { + received chan []byte +} + +func (o *ownerSessionOutputAndUseAfterExit) serve( + _ context.Context, + st tunnel.Tunnel_LifecycleTerminalServer, +) error { + if err := ready(st); err != nil { + return err + } + f, err := st.Recv() + if err != nil { + return err + } + if err = ack(st, f.GetBegin().InvocationId); err != nil { + return err + } + return o.receiveOutput(st) +} + +type ownerTwoHopFrameExchange struct { + data []byte +} + +func (o *ownerTwoHopFrameExchange) serve( + _ context.Context, + st tunnel.Tunnel_LifecycleTerminalServer, +) error { + if err := ready(st); err != nil { + return err + } + f, err := st.Recv() + if err != nil { + return err + } + id := f.GetBegin().InvocationId + if err = ack(st, id); err != nil { + return err + } + return o.exchange(st, id) +} + +func ownerConcurrentOwnerSendsSerialize( + _ context.Context, + st tunnel.Tunnel_LifecycleTerminalServer, +) error { + if err := ready(st); err != nil { + return err + } + f, err := st.Recv() + if err != nil { + return err + } + id := f.GetBegin().InvocationId + if err = ack(st, id); err != nil { + return err + } + return concurrentOwnerExchange(st, id) +} + +type ownerOutputBackpressurePreservesBytes struct { + received chan []byte + stalled chan struct{} + release chan struct{} +} + +func (o *ownerOutputBackpressurePreservesBytes) serve( + ctx context.Context, + st tunnel.Tunnel_LifecycleTerminalServer, +) error { + if err := ready(st); err != nil { + return err + } + f, err := st.Recv() + if err != nil { + return err + } + id := f.GetBegin().InvocationId + if err = ack(st, id); err != nil { + return err + } + close(o.stalled) + select { + case <-o.release: + case <-ctx.Done(): + return ctx.Err() + } + return o.receiveOutput(st, id) +} + +type ownerCancelWithFullInputBuffer struct { + proceed chan struct{} + sent chan struct{} +} + +func (o *ownerCancelWithFullInputBuffer) serve( + ctx context.Context, + st tunnel.Tunnel_LifecycleTerminalServer, +) error { + if err := ready(st); err != nil { + return err + } + f, err := st.Recv() + if err != nil { + return err + } + id := f.GetBegin().InvocationId + if err = ack(st, id); err != nil { + return err + } + <-o.proceed + for range 8 { + if err = st.Send( + &tunnel.LifecycleTerminalResponse{ + Frame: &tunnel.LifecycleTerminalResponse_Input{ + Input: &tunnel.LifecycleTerminalInput{ + InvocationId: id, + Data: make([]byte, 32<<10), + }, + }, + }, + ); err != nil { + return err + } + } + if err = st.Send( + &tunnel.LifecycleTerminalResponse{ + Frame: &tunnel.LifecycleTerminalResponse_Cancel{ + Cancel: &tunnel.LifecycleTerminalCancel{InvocationId: id}, + }, + }, + ); err != nil { + return err + } + close(o.sent) + <-ctx.Done() + return ctx.Err() +} + +type ownerBlockedServerSendExitsAfterHandlerReturn struct { + release chan struct{} + senderDone chan struct{} + sending chan struct{} +} + +func (o *ownerBlockedServerSendExitsAfterHandlerReturn) serve( + _ context.Context, + st tunnel.Tunnel_LifecycleTerminalServer, +) error { + if err := ready(st); err != nil { + return err + } + f, err := st.Recv() + if err != nil { + return err + } + id := f.GetBegin().InvocationId + if err = ack(st, id); err != nil { + return err + } + go func() { + defer close(o.senderDone) + close(o.sending) + for { + err = st.Send( + &tunnel.LifecycleTerminalResponse{ + Frame: &tunnel.LifecycleTerminalResponse_Input{ + Input: &tunnel.LifecycleTerminalInput{ + InvocationId: id, + Data: make([]byte, 32<<10), + }, + }, + }, + ) + if err != nil { + return + } + } + }() + <-o.release + return nil +} + +func invalidRequestCases() []struct { + name string + frame *tunnel.LifecycleTerminalRequest + code codes.Code +} { + return []struct { + name string + frame *tunnel.LifecycleTerminalRequest + code codes.Code + }{ + { + "out_of_order", + &tunnel.LifecycleTerminalRequest{ + Frame: &tunnel.LifecycleTerminalRequest_Output{ + Output: &tunnel.LifecycleTerminalOutput{InvocationId: "id", Sequence: 2}, + }, + }, + codes.FailedPrecondition, + }, + { + "wrong_invocation", + &tunnel.LifecycleTerminalRequest{ + Frame: &tunnel.LifecycleTerminalRequest_Output{ + Output: &tunnel.LifecycleTerminalOutput{InvocationId: "other", Sequence: 1}, + }, + }, + codes.FailedPrecondition, + }, + { + "oversized", + &tunnel.LifecycleTerminalRequest{ + Frame: &tunnel.LifecycleTerminalRequest_Output{ + Output: &tunnel.LifecycleTerminalOutput{ + InvocationId: "id", + Sequence: 1, + Data: make([]byte, (32<<10)+1), + }, + }, + }, + codes.ResourceExhausted, + }, + { + "second_begin", + &tunnel.LifecycleTerminalRequest{ + Frame: &tunnel.LifecycleTerminalRequest_Begin{ + Begin: &tunnel.LifecycleTerminalBegin{ + InvocationId: "two", + Phase: testPostStartCommand, + }, + }, + }, + codes.FailedPrecondition, + }, + } +} + +func (o *ownerSessionOutputAndUseAfterExit) receiveOutput( + st tunnel.Tunnel_LifecycleTerminalServer, +) error { + var data []byte + var f *tunnel.LifecycleTerminalRequest + var err error + for { + f, err = st.Recv() + if err != nil { + return err + } + if out := f.GetOutput(); out != nil { + data = append(data, out.Data...) + continue + } + if ex := f.GetExit(); ex != nil { + o.received <- data + if err = st.Send( + &tunnel.LifecycleTerminalResponse{ + Frame: &tunnel.LifecycleTerminalResponse_ExitAck{ + ExitAck: &tunnel.LifecycleTerminalExitAck{ + InvocationId: ex.InvocationId, + }, + }, + }, + ); err != nil { + return err + } + break + } + } + _, err = st.Recv() + return err +} + +func (o *ownerTwoHopFrameExchange) exchange( + st tunnel.Tunnel_LifecycleTerminalServer, + id string, +) error { + f, err := st.Recv() + if err != nil { + return err + } + if !bytes.Equal(f.GetOutput().Data, o.data) { + return status.Error(codes.DataLoss, "wrong o.data") + } + if err = st.Send( + &tunnel.LifecycleTerminalResponse{ + Frame: &tunnel.LifecycleTerminalResponse_Input{ + Input: &tunnel.LifecycleTerminalInput{ + InvocationId: id, + Data: []byte("reply"), + }, + }, + }, + ); err != nil { + return err + } + f, err = st.Recv() + if err != nil { + return err + } + if f.GetExit() == nil { + return status.Error(codes.FailedPrecondition, "missing exit") + } + if err = st.Send( + &tunnel.LifecycleTerminalResponse{ + Frame: &tunnel.LifecycleTerminalResponse_ExitAck{ + ExitAck: &tunnel.LifecycleTerminalExitAck{InvocationId: id}, + }, + }, + ); err != nil { + return err + } + _, err = st.Recv() + return err +} + +func concurrentOwnerExchange(st tunnel.Tunnel_LifecycleTerminalServer, id string) error { + errs := make(chan error, 2) + for _, b := range []byte{'a', 'b'} { + go func(c byte) { + errs <- st.Send(&tunnel.LifecycleTerminalResponse{ + Frame: &tunnel.LifecycleTerminalResponse_Input{Input: &tunnel.LifecycleTerminalInput{ + InvocationId: id, Data: []byte{c}, + }}, + }) + }(b) + } + for range 2 { + if err := <-errs; err != nil { + return err + } + } + f, err := st.Recv() + if err != nil { + return err + } + if err = st.Send( + &tunnel.LifecycleTerminalResponse{ + Frame: &tunnel.LifecycleTerminalResponse_ExitAck{ + ExitAck: &tunnel.LifecycleTerminalExitAck{ + InvocationId: f.GetExit().InvocationId, + }, + }, + }, + ); err != nil { + return err + } + _, err = st.Recv() + return err +} + +func (o *ownerOutputBackpressurePreservesBytes) receiveOutput( + st tunnel.Tunnel_LifecycleTerminalServer, + id string, +) error { + var data []byte + var f *tunnel.LifecycleTerminalRequest + var err error + for { + f, err = st.Recv() + if err != nil { + return err + } + if out := f.GetOutput(); out != nil { + data = append(data, out.Data...) + continue + } + if f.GetExit() != nil { + o.received <- data + if err = st.Send( + &tunnel.LifecycleTerminalResponse{ + Frame: &tunnel.LifecycleTerminalResponse_ExitAck{ + ExitAck: &tunnel.LifecycleTerminalExitAck{InvocationId: id}, + }, + }, + ); err != nil { + return err + } + break + } + } + _, err = st.Recv() + return err +} diff --git a/pkg/agent/tunnelserver/options.go b/pkg/agent/tunnelserver/options.go index af7a10d3bf..fccf044684 100644 --- a/pkg/agent/tunnelserver/options.go +++ b/pkg/agent/tunnelserver/options.go @@ -1,6 +1,7 @@ package tunnelserver import ( + "context" "strings" "github.com/devsy-org/api/pkg/devsy" @@ -125,6 +126,32 @@ func WithSetupLogSink(sink *workspacelogs.Sink) Option { } } +type LifecycleTerminalOwner interface { + Serve(ctx context.Context, stream tunnel.Tunnel_LifecycleTerminalServer) error +} + +func WithLifecycleTerminalOwner( + owner LifecycleTerminalOwner, + req provider2.LifecycleTerminalRequest, +) Option { + return func(s *tunnelServer) *tunnelServer { + s.lifecycleTerminalOwner = owner + s.lifecycleTerminalRequest = req + return s + } +} + +func WithLifecycleTerminalUpstream( + client tunnel.TunnelClient, + req provider2.LifecycleTerminalRequest, +) Option { + return func(s *tunnelServer) *tunnelServer { + s.lifecycleTerminalUpstream = client + s.lifecycleTerminalRequest = req + return s + } +} + func toSecrets(entries []string, mount bool) []*tunnel.Secret { secrets := make([]*tunnel.Secret, 0, len(entries)) for _, entry := range entries { diff --git a/pkg/agent/tunnelserver/tunnelserver.go b/pkg/agent/tunnelserver/tunnelserver.go index 4bf7c77c74..daba80cb5e 100644 --- a/pkg/agent/tunnelserver/tunnelserver.go +++ b/pkg/agent/tunnelserver/tunnelserver.go @@ -173,9 +173,17 @@ type tunnelServer struct { gitToken *provider2.GitToken gitSSHSigningKey string + lifecycleTerminalOwner LifecycleTerminalOwner + lifecycleTerminalUpstream tunnel.TunnelClient + lifecycleTerminalRequest provider2.LifecycleTerminalRequest + statusReporter status.Reporter } +func (t *tunnelServer) LifecycleTerminal(stream tunnel.Tunnel_LifecycleTerminalServer) error { + return t.serveLifecycleTerminal(stream) +} + func (t *tunnelServer) RunWithResult( ctx context.Context, reader io.Reader, diff --git a/pkg/devcontainer/lifecycle_terminal_wiring_test.go b/pkg/devcontainer/lifecycle_terminal_wiring_test.go new file mode 100644 index 0000000000..99435da633 --- /dev/null +++ b/pkg/devcontainer/lifecycle_terminal_wiring_test.go @@ -0,0 +1,53 @@ +package devcontainer + +import ( + "context" + "encoding/json" + "testing" + + "github.com/devsy-org/devsy/pkg/agent/tunnel" + "github.com/devsy-org/devsy/pkg/provider" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" +) + +type wiringClient struct{ tunnel.TunnelClient } + +type LifecycleTerminalWiringSuite struct { + suite.Suite +} + +func TestLifecycleTerminalWiringSuite(t *testing.T) { + suite.Run(t, new(LifecycleTerminalWiringSuite)) +} + +func (s *LifecycleTerminalWiringSuite) TestUpOptionsTerminalClientNotSerialized() { + t := s.T() + data, err := json.Marshal(UpOptions{LifecycleTerminalClient: &wiringClient{}}) + require.NoError(t, err) + require.NotContains(t, string(data), "LifecycleTerminalClient") +} + +func (s *LifecycleTerminalWiringSuite) TestUpClearsTerminalClientOnError() { + t := s.T() + t.Setenv("DEVSY_CONFIG", t.TempDir()) + r := &runner{workspaceConfig: &provider.AgentWorkspaceInfo{Workspace: &provider.Workspace{}}} + _, err := r.Up( + context.Background(), + UpOptions{LifecycleTerminalClient: &wiringClient{}}, + 0, + nil, + ) + require.Error(t, err) + require.Nil(t, r.lifecycleTerminalClient) +} + +func (s *LifecycleTerminalWiringSuite) TestUpRejectsMissingTerminalClientBeforeInitialization() { + t := s.T() + r := &runner{workspaceConfig: &provider.AgentWorkspaceInfo{ + CLIOptions: provider.CLIOptions{LifecycleTerminal: &provider.LifecycleTerminalRequest{}}, + }} + _, err := r.Up(context.Background(), UpOptions{}, 0, nil) + require.ErrorContains(t, err, "lifecycle terminal client") + require.Nil(t, r.lifecycleTerminalClient) +} diff --git a/pkg/devcontainer/run.go b/pkg/devcontainer/run.go index aba3bf56e9..074e817e21 100644 --- a/pkg/devcontainer/run.go +++ b/pkg/devcontainer/run.go @@ -7,6 +7,7 @@ import ( "io" "time" + "github.com/devsy-org/devsy/pkg/agent/tunnel" "github.com/devsy-org/devsy/pkg/clierr" "github.com/devsy-org/devsy/pkg/devcontainer/config" "github.com/devsy-org/devsy/pkg/driver" @@ -61,6 +62,7 @@ var ErrRequireRunningUnsupported = errors.New("runtime does not support exec-onl // UpOptions configures a single Up invocation. type UpOptions struct { provider.CLIOptions + LifecycleTerminalClient tunnel.TunnelClient `json:"-"` // NoBuild is set by the container tunnel to force pre-built images; it is // distinct from the embedded CLIOptions.NoBuild used by the build command. @@ -108,8 +110,9 @@ type runner struct { imageDetails *config.ImageDetails imageDetailsImage string - reporter status.Reporter - wslgSnapshot *wslg.Snapshot + lifecycleTerminalClient tunnel.TunnelClient + reporter status.Reporter + wslgSnapshot *wslg.Snapshot } func NewRunner( @@ -155,6 +158,12 @@ func (r *runner) Up( timeout time.Duration, reporter status.Reporter, ) (*config.Result, error) { + if r.workspaceConfig.CLIOptions.LifecycleTerminal != nil && + options.LifecycleTerminalClient == nil { + return nil, fmt.Errorf("lifecycle terminal request requires a lifecycle terminal client") + } + r.lifecycleTerminalClient = options.LifecycleTerminalClient + defer func() { r.lifecycleTerminalClient = nil }() r.imageDetails = nil r.imageDetailsImage = "" if reporter == nil { diff --git a/pkg/devcontainer/setup.go b/pkg/devcontainer/setup.go index daad4dea4f..ec1e11336c 100644 --- a/pkg/devcontainer/setup.go +++ b/pkg/devcontainer/setup.go @@ -453,6 +453,9 @@ func (r *runner) buildSetupCommand(compressed, workspaceConfigCompressed string) shellescape.Quote(workspaceConfigCompressed), } + if r.workspaceConfig.CLIOptions.LifecycleTerminal != nil { + args = append(args, "--lifecycle-terminal-protocol=1") + } r.addSetupFlags(&args) return strings.Join(args, " ") } @@ -576,6 +579,13 @@ func (r *runner) executeSetup( secretsEnv, secretsMount := secretOpts.env, secretOpts.mount setupLogOptions := tunnelserver.SetupLogOptionsFrom(ctx) runSetupServer := func(ctx context.Context, stdin io.WriteCloser, stdout io.Reader) (*config.Result, error) { + options := setupLogOptions + if req := r.workspaceConfig.CLIOptions.LifecycleTerminal; req != nil { + options = append( + options, + tunnelserver.WithLifecycleTerminalUpstream(r.lifecycleTerminalClient, *req), + ) + } return tunnelserver.RunSetupServer(ctx, tunnelserver.SetupSpec{ Reader: stdout, Writer: stdin, @@ -590,7 +600,7 @@ func (r *runner) executeSetup( ), tunnelserver.WithGitToken(r.workspaceConfig.CLIOptions.GitToken), tunnelserver.WithStatusReporter(r.reporter), - }, setupLogOptions...), + }, options...), }) } diff --git a/pkg/devcontainer/setup/lifecycle_terminal.go b/pkg/devcontainer/setup/lifecycle_terminal.go new file mode 100644 index 0000000000..2778c2b176 --- /dev/null +++ b/pkg/devcontainer/setup/lifecycle_terminal.go @@ -0,0 +1,169 @@ +package setup + +import ( + "context" + "errors" + "fmt" + "strings" + "time" + + "github.com/devsy-org/devsy/pkg/agent/tunnel" + "github.com/devsy-org/devsy/pkg/log" + "github.com/devsy-org/devsy/pkg/subprocess" + "github.com/devsy-org/devsy/pkg/types" + "github.com/devsy-org/devsy/pkg/workspacelogs" +) + +func ValidateInteractivePostStart(hooks []types.LifecycleHook) error { + for i, hook := range hooks { + count := 0 + for _, command := range hook { + if len(command) > 0 { + count++ + } + } + if count > 1 { + return fmt.Errorf( + "%s merged index %d has multiple nonempty named commands", + PhasePostStart, + i, + ) + } + } + return nil +} + +func runInteractiveHook(p hookRunParams) error { + if p.content != "" { + skipped, err := markerExists(p.name, p.content) + if err != nil { + p.recordAll(workspacelogs.InvocationFailed, reasonMarkerCheckFailed) + return err + } + if skipped { + p.recordSkipped(reasonAlreadyAttempted) + return nil + } + } + launches, err := prepareInteractiveHooks(p) + if err != nil { + return err + } + if p.content != "" { + if err := writeMarker(p.name, p.content); err != nil { + p.recordAll(workspacelogs.InvocationFailed, reasonMarkerCheckFailed) + return err + } + } + return executeInteractiveHooks(p, launches) +} + +func executeInteractive(p hookRunParams, command hookCommand, launch interactiveLaunch) error { + inv := p.invocation(p.ctx, command.key) + attachment, err := p.terminal.Begin( + p.ctx, + tunnel.TerminalInvocation{ + ID: workspacelogs.NewID(), + Phase: string(PhasePostStart), + Index: p.index, + Key: command.key, + }, + ) + if err != nil { + inv.record(workspacelogs.InvocationFailed, reasonStartFailed) + return fmt.Errorf("%s: terminal begin: %w", p.name, err) + } + started := time.Now() + inv.record(workspacelogs.InvocationRunning, "") + result, runErr := runInteractiveProcess(p.ctx, p, launch, attachment) + result.Duration = time.Since(started) + finishCtx := p.ctx + if errors.Is(runErr, context.Canceled) && finishCtx.Err() == nil { + var cancelFinish context.CancelFunc + finishCtx, cancelFinish = context.WithCancel(finishCtx) + cancelFinish() + } + inv.finish(finishCtx, result, runErr) + exit := tunnel.TerminalExit{ExitCode: result.ExitCode, Signal: result.Signal} + if result.ExitCode < 0 && result.Signal == "" && runErr != nil { + exit.StartError = "failed to start terminal command" + } + exitCtx, cancel := context.WithTimeout( + context.Background(), + tunnel.LifecycleTerminalExitAckTimeout, + ) + logTerminalExit(attachment.Exit(exitCtx, exit)) + cancel() + if runErr == nil { + return nil + } + return interactiveError(p, command, result, runErr) +} + +func logTerminalExit(err error) { + if err != nil { + log.Warnf("lifecycle terminal exit: %v", err) + } +} + +func prepareInteractiveHooks(p hookRunParams) (map[int]interactiveLaunch, error) { + launches := make(map[int]interactiveLaunch) + for i, hook := range p.commands { + for _, command := range hook { + if len(command) == 0 { + continue + } + launch, err := prepareInteractiveLaunch(p.ctx, command, p.env.remoteUser) + if err != nil { + p.index = i + p.recordAll(workspacelogs.InvocationFailed, reasonStartFailed) + return nil, fmt.Errorf("%s: %w", p.name, err) + } + launches[i] = launch + } + } + return launches, nil +} + +func interactiveError( + p hookRunParams, + command hookCommand, + result subprocess.Result, + runErr error, +) error { + display := strings.Join(command.c, " ") + if p.redactor != nil { + display = p.redactor.Redact(display) + } + if errors.Is(runErr, context.Canceled) || errors.Is(runErr, context.DeadlineExceeded) { + return fmt.Errorf("%s: command %q canceled", p.name, display) + } + if result.Signal != "" { + return fmt.Errorf("%s: command %q failed: signal %s", p.name, display, result.Signal) + } + if result.ExitCode >= 0 { + return fmt.Errorf("%s: command %q failed: exit code %d", p.name, display, result.ExitCode) + } + return fmt.Errorf("%s: command %q failed to start", p.name, display) +} + +func executeInteractiveHooks(p hookRunParams, launches map[int]interactiveLaunch) error { + for i, hook := range p.commands { + p.index = i + for key, command := range hook { + if len(command) == 0 { + p.invocation(p.ctx, key).record(workspacelogs.InvocationSkipped, reasonNoCommand) + continue + } + if err := executeInteractive( + p, + hookCommand{key: key, c: command}, + launches[i], + ); err != nil { + p.recordFrom(i+1, workspacelogs.InvocationSkipped, reasonNotReached) + return err + } + } + } + return nil +} diff --git a/pkg/devcontainer/setup/lifecycle_terminal_linux.go b/pkg/devcontainer/setup/lifecycle_terminal_linux.go new file mode 100644 index 0000000000..4822f4b2e8 --- /dev/null +++ b/pkg/devcontainer/setup/lifecycle_terminal_linux.go @@ -0,0 +1,468 @@ +//go:build linux + +package setup + +import ( + "context" + "errors" + "fmt" + "io" + "math" + "os" + "os/exec" + "os/user" + "path/filepath" + "strconv" + "strings" + "syscall" + "time" + + "github.com/devsy-org/devsy/pkg/agent/tunnel" + "github.com/devsy-org/devsy/pkg/log" + "github.com/devsy-org/devsy/pkg/pty" + "github.com/devsy-org/devsy/pkg/ssh/server" + "github.com/devsy-org/devsy/pkg/subprocess" + "github.com/devsy-org/ssh" + "golang.org/x/sys/unix" +) + +const ( + interactiveDrainGrace = 2 * time.Second + interactiveDrainLimit = 5 * time.Second +) + +type interactiveLaunch struct { + binary string + args []string + identity interactiveIdentity +} + +type interactiveIdentity struct{ credentials server.ExecCredentials } + +func loginShell(ctx context.Context, name string) (string, error) { + lookup, _, _ := strings.Cut(name, ":") + path, err := exec.LookPath("getent") + if err == nil { + cmd := exec.CommandContext(ctx, "getent") + cmd.Path = path + cmd.Args = append(cmd.Args, "passwd", lookup) + if out, err := cmd.Output(); err == nil { + if shell := shellFromPasswd(string(out), lookup); shell != "" { + return shell, nil + } + } + } + if err := ctx.Err(); err != nil { + return "", err + } + data, err := os.ReadFile("/etc/passwd") + if err == nil { + if shell := shellFromPasswd(string(data), lookup); shell != "" { + return shell, nil + } + } + return "", fmt.Errorf("cannot resolve login shell for %q", lookup) +} + +func shellFromPasswd(data, name string) string { + for line := range strings.SplitSeq(data, "\n") { + fields := strings.Split(line, ":") + if len(fields) >= 7 && (fields[0] == name || fields[2] == name) { + return fields[6] + } + } + return "" +} + +func prepareInteractiveLaunch( + ctx context.Context, command []string, user string, +) (interactiveLaunch, error) { + shell, err := loginShell(ctx, user) + if err != nil { + return interactiveLaunch{}, err + } + credentials, err := server.ResolveExecCredentials(user) + if err != nil { + return interactiveLaunch{}, fmt.Errorf("resolve execution identity: %w", err) + } + args := command + if len(command) == 1 { + if credentials.MatchCurrent { + shell = "sh" + } + args = []string{shell, "-c", command[0]} + } + return interactiveLaunch{ + binary: args[0], + args: args, + identity: interactiveIdentity{credentials: credentials}, + }, nil +} + +func runInteractiveProcess( + ctx context.Context, + p hookRunParams, + launch interactiveLaunch, + a tunnel.TerminalAttachment, +) (subprocess.Result, error) { + result := subprocess.Result{ExitCode: -1} + input := a.Input() + terminal, process, err := startInteractive(p, launch, a) + if err != nil { + return result, err + } + + inputDone := make(chan struct{}) + outputDone := make(chan error, 1) + waitDone := make(chan error, 1) + resizeDone := make(chan struct{}) + stopResize := make(chan struct{}) + progress := make(chan struct{}, 1) + stopOutput := make(chan struct{}) + reader, err := newInteractiveReader(terminal, stopOutput) + if err != nil { + _ = process.Kill() + _ = terminal.Close() + _ = process.Wait() + _ = input.Close() + return result, err + } + go func() { defer close(inputDone); _, _ = io.Copy(terminal.InputWriter(), input) }() + go func() { + _, copyErr := io.Copy( + &interactiveOutput{writer: a.Output(), progress: progress}, + reader, + ) + outputDone <- copyErr + }() + go func() { waitDone <- process.Wait() }() + go resizeInteractive(terminal, a, stopResize, resizeDone) + state := &interactiveWait{waitDone: waitDone, outputDone: outputDone} + state.wait(ctx, a, progress) + if state.sessionErr != nil { + state.stop(process) + } + + finishInteractive( + terminal, + a, + state, + interactiveStops{stopResize, resizeDone, stopOutput, inputDone, input}, + ) + return interactiveResult(state.waitErr, state.sessionErr) +} + +type interactiveStops struct { + resize, resized, output, input chan struct{} + reader io.ReadCloser +} + +func finishInteractive( + terminal pty.PTYCmd, + a tunnel.TerminalAttachment, + state *interactiveWait, + stops interactiveStops, +) { + close(stops.resize) + <-stops.resized + if !state.drained { + close(stops.output) + a.Abort() + <-state.outputDone + } + _ = terminal.Close() + if !state.waited { + state.waitErr = <-state.waitDone + } + _ = stops.reader.Close() + <-stops.input +} + +func interactiveResult(waitErr, sessionErr error) (subprocess.Result, error) { + result := subprocess.Result{} + result.ExitCode = 0 + if exitErr, ok := errors.AsType[*exec.ExitError](waitErr); ok { + result.ExitCode = exitErr.ExitCode() + if status, ok := exitErr.Sys().(syscall.WaitStatus); ok && status.Signaled() { + result.Signal = status.Signal().String() + } + } else if waitErr != nil { + result.ExitCode = -1 + } + if sessionErr != nil { + return result, sessionErr + } + return result, waitErr +} + +type interactiveWait struct { + waitDone <-chan error + outputDone <-chan error + waitErr error + sessionErr error + waited bool + drained bool +} + +func resizeInteractive(terminal pty.PTYCmd, a tunnel.TerminalAttachment, stop, done chan struct{}) { + defer close(done) + sizes := a.Resizes() + for { + select { + case size, ok := <-sizes: + if !ok { + return + } + if size.Rows > 0 && size.Columns > 0 { + _ = terminal.Resize(size.Rows, size.Columns) + } + case <-stop: + return + } + } +} + +type interactiveReader struct { + fd int32 + stop <-chan struct{} +} + +func newInteractiveReader(terminal pty.PTYCmd, stop <-chan struct{}) (*interactiveReader, error) { + master, ok := terminal.InputWriter().(*os.File) + if !ok { + return nil, errors.New("terminal master is not a file") + } + fd := master.Fd() + if fd > math.MaxInt32 { + return nil, errors.New("terminal master descriptor is too large") + } + return &interactiveReader{fd: int32(fd & math.MaxInt32), stop: stop}, nil +} + +func (r *interactiveReader) Read(p []byte) (int, error) { + for { + ready, err := r.ready() + if err != nil { + return 0, err + } + if !ready { + continue + } + n, err := unix.Read(int(r.fd), p) + if errors.Is(err, unix.EINTR) { + continue + } + if errors.Is(err, unix.EIO) { + return 0, io.EOF + } + if err != nil { + return 0, err + } + return n, nil + } +} + +func (r *interactiveReader) ready() (bool, error) { + select { + case <-r.stop: + return false, io.EOF + default: + } + fds := []unix.PollFd{{Fd: r.fd, Events: unix.POLLIN}} + n, err := unix.Poll(fds, 100) + if errors.Is(err, unix.EINTR) { + return false, nil + } + return n > 0, err +} + +type interactiveOutput struct { + writer io.Writer + progress chan<- struct{} +} + +func (o *interactiveOutput) Write(p []byte) (int, error) { + n, err := o.writer.Write(p) + if n > 0 { + select { + case o.progress <- struct{}{}: + default: + } + } + return n, err +} + +type interactiveDrain struct { + grace, limit *time.Timer + graceC, limitC <-chan time.Time +} + +func (d *interactiveDrain) start() { + d.grace = time.NewTimer(interactiveDrainGrace) + d.limit = time.NewTimer(interactiveDrainLimit) + d.graceC, d.limitC = d.grace.C, d.limit.C +} + +func (d *interactiveDrain) reset() { + if d.grace == nil { + return + } + if !d.grace.Stop() { + select { + case <-d.grace.C: + default: + } + } + d.grace.Reset(interactiveDrainGrace) +} + +func (d *interactiveDrain) stop() { + if d.grace != nil { + d.grace.Stop() + d.limit.Stop() + } +} + +func (s *interactiveWait) wait( + ctx context.Context, + a tunnel.TerminalAttachment, + progress <-chan struct{}, +) { + d := &interactiveDrain{} + defer d.stop() + for !s.waited || !s.drained { + if s.waitStep(ctx, a, progress, d) { + return + } + } +} + +func (s *interactiveWait) waitStep( + ctx context.Context, + a tunnel.TerminalAttachment, + progress <-chan struct{}, + d *interactiveDrain, +) bool { + select { + case s.waitErr = <-s.waitDone: + s.processExited(d) + case err := <-s.outputDone: + s.drained = true + s.outputDone = nil + s.sessionErr = err + case <-progress: + d.reset() + case <-d.graceC: + return warnTruncatedOutput() + case <-d.limitC: + return warnTruncatedOutput() + case <-ctx.Done(): + s.sessionErr = ctx.Err() + case <-a.Canceled(): + s.sessionErr = context.Canceled + } + return s.sessionErr != nil +} + +func (s *interactiveWait) processExited(d *interactiveDrain) { + s.waited = true + s.waitDone = nil + if !s.drained { + d.start() + } +} + +func warnTruncatedOutput() bool { + log.Warnf("lifecycle terminal output was truncated after drain timeout") + return true +} + +func (s *interactiveWait) stop(process pty.Process) { + _ = syscall.Kill(-process.Pid(), syscall.SIGTERM) + timer := time.NewTimer(2 * time.Second) + defer timer.Stop() + for !s.waited || !s.drained { + select { + case s.waitErr = <-s.waitDone: + s.waited = true + s.waitDone = nil + case <-s.outputDone: + s.drained = true + s.outputDone = nil + case <-timer.C: + _ = syscall.Kill(-process.Pid(), syscall.SIGKILL) + return + } + } +} + +func startInteractive( + p hookRunParams, + launch interactiveLaunch, + a tunnel.TerminalAttachment, +) (pty.PTYCmd, pty.Process, error) { + window := a.Window() + cred := launch.identity.credentials + env := os.Environ() + if !cred.MatchCurrent { + target, err := user.LookupId(strconv.FormatUint(uint64(cred.UID), 10)) + if err != nil { + return nil, nil, fmt.Errorf("lookup execution user: %w", err) + } + shell, err := loginShell(p.ctx, target.Username) + if err != nil { + return nil, nil, err + } + env = append(env, "HOME="+target.HomeDir, "USER="+target.Username, + "LOGNAME="+target.Username, "SHELL="+shell) + } + path, err := interactiveLookPath(launch.binary, p.env.workspaceFolder, p.env.remoteEnv) + if err != nil { + return nil, nil, err + } + cmd := pty.CommandContext(context.Background(), path, launch.args[1:]...) + cmd.Dir = p.env.workspaceFolder + cmd.Env = append(append(env, buildEnvArr(p.env.remoteEnv)...), "TERM="+window.Term) + opts := []pty.Option{ + pty.WithSSHRequest( + ssh.Pty{ + Term: window.Term, + Window: ssh.Window{Width: int(window.Columns), Height: int(window.Rows)}, + }, + ), + } + if !cred.MatchCurrent { + cmd.SysProcAttr = &syscall.SysProcAttr{ + Credential: &syscall.Credential{Uid: cred.UID, Gid: cred.GID, Groups: cred.Groups}, + } + opts = append(opts, pty.WithTTYOwner(int(cred.UID), int(cred.GID))) + } + terminal, process, err := pty.Start(cmd, pty.WithPTYOption(opts...)) + if err != nil { + return nil, nil, fmt.Errorf("start terminal command: %w", err) + } + return terminal, process, nil +} + +func interactiveLookPath(binary, dir string, env map[string]string) (string, error) { + if strings.Contains(binary, "/") { + if !filepath.IsAbs(binary) { + binary = filepath.Join(dir, binary) + } + return exec.LookPath(binary) + } + path, ok := env["PATH"] + if !ok { + path = os.Getenv("PATH") + } + for _, entry := range filepath.SplitList(path) { + if !filepath.IsAbs(entry) { + entry = filepath.Join(dir, entry) + } + candidate := filepath.Join(entry, binary) + if found, err := exec.LookPath(candidate); err == nil { + return found, nil + } + } + return "", &exec.Error{Name: binary, Err: exec.ErrNotFound} +} diff --git a/pkg/devcontainer/setup/lifecycle_terminal_linux_test.go b/pkg/devcontainer/setup/lifecycle_terminal_linux_test.go new file mode 100644 index 0000000000..f4075b2f52 --- /dev/null +++ b/pkg/devcontainer/setup/lifecycle_terminal_linux_test.go @@ -0,0 +1,576 @@ +//go:build linux + +package setup + +import ( + "bytes" + "context" + "fmt" + "io" + "os" + "os/user" + "path/filepath" + "strconv" + "strings" + "sync" + "syscall" + "testing" + "time" + + "github.com/devsy-org/devsy/pkg/agent/tunnel" + "github.com/devsy-org/devsy/pkg/devcontainer/config" + "github.com/devsy-org/devsy/pkg/log" + "github.com/devsy-org/devsy/pkg/pty/ptytest" + "github.com/devsy-org/devsy/pkg/types" + "github.com/devsy-org/devsy/pkg/workspacelogs" + "github.com/stretchr/testify/suite" + "go.uber.org/zap/zapcore" +) + +const ( + testTempDir = "/tmp" + pathEnv = "PATH" +) + +type terminalTestSuite struct { + suite.Suite + username string +} + +type testTerminal struct { + input *io.PipeReader + writer *io.PipeWriter + output lockedOutput + window tunnel.TerminalWindow + resizes chan tunnel.TerminalWindow + canceled chan struct{} + exit tunnel.TerminalExit + exitErr error + exitOutput string + abort chan struct{} + abortOnce sync.Once + outputOverride io.Writer + invocations []tunnel.TerminalInvocation +} + +type lockedOutput struct { + mu sync.Mutex + bytes bytes.Buffer + changed chan struct{} +} + +func (b *lockedOutput) Write(p []byte) (int, error) { + b.mu.Lock() + defer b.mu.Unlock() + n, err := b.bytes.Write(p) + select { + case b.changed <- struct{}{}: + default: + } + return n, err +} +func (b *lockedOutput) String() string { b.mu.Lock(); defer b.mu.Unlock(); return b.bytes.String() } +func (b *lockedOutput) wait(t *testing.T, value string) { + t.Helper() + timer := time.NewTimer(10 * time.Second) + defer timer.Stop() + for !strings.Contains(b.String(), value) { + select { + case <-b.changed: + case <-timer.C: + t.Fatalf("output %q did not contain %q", b.String(), value) + } + } +} + +func newTestTerminal() *testTerminal { + r, w := io.Pipe() + return &testTerminal{ + input: r, + writer: w, + output: lockedOutput{changed: make(chan struct{}, 1)}, + window: tunnel.TerminalWindow{Term: "xterm", Rows: 31, Columns: 91}, + resizes: make(chan tunnel.TerminalWindow, 1), + canceled: make(chan struct{}), + abort: make(chan struct{}), + } +} + +func (a *testTerminal) Begin( + _ context.Context, + inv tunnel.TerminalInvocation, +) (tunnel.TerminalAttachment, error) { + a.invocations = append(a.invocations, inv) + return a, nil +} +func (*testTerminal) Close(context.Context) error { return nil } +func (a *testTerminal) Window() tunnel.TerminalWindow { return a.window } +func (a *testTerminal) Input() io.ReadCloser { return a.input } +func (a *testTerminal) Output() io.Writer { + if a.outputOverride != nil { + return a.outputOverride + } + return &a.output +} + +func (a *testTerminal) Abort() { a.abortOnce.Do(func() { close(a.abort) }) } +func (a *testTerminal) Resizes() <-chan tunnel.TerminalWindow { return a.resizes } +func (a *testTerminal) Canceled() <-chan struct{} { return a.canceled } +func (a *testTerminal) Exit(_ context.Context, exit tunnel.TerminalExit) error { + a.exit = exit + a.exitOutput = a.output.String() + _ = a.writer.Close() + return a.exitErr +} + +func TestTerminalTestSuite(t *testing.T) { suite.Run(t, new(terminalTestSuite)) } +func (s *terminalTestSuite) SetupTest() { + u, err := user.Current() + s.Require().NoError(err) + s.username = u.Username + dir := s.T().TempDir() + script := fmt.Sprintf( + "#!/bin/sh\nprintf '%%s\\n' '%s:x:%s:%s::%s:/bin/sh'\n", + u.Username, + u.Uid, + u.Gid, + u.HomeDir, + ) + s.Require().NoError(os.WriteFile(filepath.Join(dir, "getent"), []byte(script), 0o600)) + s.Require().NoError(os.Chmod(filepath.Join(dir, "getent"), executableTestMode())) + s.T().Setenv(pathEnv, dir+string(os.PathListSeparator)+os.Getenv(pathEnv)) +} + +func (s *terminalTestSuite) TestPromptTTYAndMarker() { + a := newTestTerminal() + p := s.params( + context.Background(), + a, + `test -t 0 && test -t 1 && printf 'Name:' && read name && printf '%s' "$name" > marker`, + ) + outer := ptytest.New(s.T()) + a.outputOverride = outer.Output().Writer + done := s.run(p) + outer.ExpectMatchContext(s.T().Context(), "Name:") + _, err := a.writer.Write([]byte("alice\n")) + s.Require().NoError(err) + s.Require().NoError(s.result(done)) + data, err := os.ReadFile(filepath.Join(p.env.workspaceFolder, "marker")) + s.Require().NoError(err) + s.Equal("alice", string(data)) + s.Len(a.invocations, 1) +} + +func (s *terminalTestSuite) TestInitialSizeAndResize() { + a := newTestTerminal() + p := s.params(context.Background(), a, `stty size; printf 'ready:'; read x; stty size`) + done := s.run(p) + a.output.wait(s.T(), "ready:") + s.Contains(a.output.String(), "31 91") + a.resizes <- tunnel.TerminalWindow{Rows: 42, Columns: 112} + time.Sleep(100 * time.Millisecond) + _, err := a.writer.Write([]byte("x\n")) + s.Require().NoError(err) + s.Require().NoError(s.result(done)) + s.Contains(a.output.String(), "42 112") +} + +func (s *terminalTestSuite) TestExitAndNoRetainedToken() { + run := newRecordedRun(s.T()) + a := newTestTerminal() + a.exitErr = fmt.Errorf("exit ack failed") + p := s.params(run.ctx, a, `printf 'Token:'; read token; exit 7`) + done := s.run(p) + a.output.wait(s.T(), "Token:") + _, err := a.writer.Write([]byte("typed-secret-3187\n")) + s.Require().NoError(err) + err = s.result(done) + s.ErrorContains(err, "exit code 7") + s.NotContains(err.Error(), "typed-secret-3187") + s.Equal(7, a.exit.ExitCode) + records := run.records(s.T()) + s.NotContains(fmt.Sprint(records), "typed-secret-3187") + got := byKey(s.T(), records, string(PhasePostStart)) + s.Len(got, 1) + for _, inv := range got { + s.Equal(workspacelogs.InvocationFailed, inv.final()) + s.Require().NotNil(inv.exitCode) + s.Equal(7, *inv.exitCode) + s.Empty(inv.stdout) + s.Empty(inv.stderr) + } +} + +func (s *terminalTestSuite) TestSignalAndCancel() { + for _, tc := range []struct { + name, command string + cancel bool + }{{"signal", `kill -TERM $$`, false}, {"cancel", `printf 'ready:'; while :; do sleep 1; done`, true}} { + s.Run(tc.name, func() { + run := newRecordedRun(s.T()) + a := newTestTerminal() + p := s.params(run.ctx, a, tc.command) + done := s.run(p) + if tc.cancel { + a.output.wait(s.T(), "ready:") + close(a.canceled) + } + s.Error(s.result(done)) + records := run.records(s.T()) + got := byKey(s.T(), records, string(PhasePostStart)) + s.Len(got, 1) + for _, inv := range got { + if tc.cancel { + s.Equal(workspacelogs.InvocationCanceled, inv.final()) + } else { + s.Equal(workspacelogs.InvocationFailed, inv.final()) + s.Equal(reasonSignaled, inv.reason) + } + } + s.NotEmpty(a.exit.Signal) + }) + } +} + +func (s *terminalTestSuite) TestTermGraceAndKill() { + a := newTestTerminal() + p := s.params( + context.Background(), + a, + `trap 'echo trapped' TERM; printf 'ready:'; while :; do :; done`, + ) + done := s.run(p) + a.output.wait(s.T(), "ready:") + started := time.Now() + close(a.canceled) + s.Error(s.result(done)) + s.GreaterOrEqual(time.Since(started), 1900*time.Millisecond) + s.Contains(a.output.String(), "trapped") + s.NotEmpty(a.exit.Signal) +} + +func (s *terminalTestSuite) TestRemoteUserTTYReopen() { + if os.Geteuid() != 0 { + s.T().Skip("requires root") + } + target, err := user.Lookup("nobody") + if err != nil { + s.T().Skip("nobody unavailable") + } + if target.Uid == "0" { + s.T().Skip("no unprivileged account") + } + a := newTestTerminal() + p := s.params(context.Background(), a, `test -w "$(tty)" && (echo reopened > "$(tty)")`) + p.env.remoteUser = target.Username + p.env.workspaceFolder = testTempDir + dir := s.T().TempDir() + script := fmt.Sprintf( + "#!/bin/sh\nprintf '%%s\\n' '%s:x:%s:%s::%s:/bin/sh'\n", + target.Username, + target.Uid, + target.Gid, + target.HomeDir, + ) + s.Require().NoError(os.WriteFile(filepath.Join(dir, "getent"), []byte(script), 0o600)) + s.Require().NoError(os.Chmod(filepath.Join(dir, "getent"), executableTestMode())) + s.T().Setenv(pathEnv, dir+string(os.PathListSeparator)+os.Getenv(pathEnv)) + s.Require().NoError(s.result(s.run(p))) + s.Contains(a.output.String(), "reopened") +} + +func (s *terminalTestSuite) TestExecutableResolution() { + for _, name := range []string{"./script.sh", "script.sh"} { + s.Run(name, func() { + a := newTestTerminal() + p := s.params(context.Background(), a, "") + scriptPath := filepath.Join(p.env.workspaceFolder, "script.sh") + s.Require(). + NoError(writeExecutableFile(scriptPath, []byte("#!/bin/sh\nprintf 'resolved'\n"))) + p.commands = []types.LifecycleHook{{"": {name, "argument"}}} + p.env.remoteEnv = map[string]string{pathEnv: p.env.workspaceFolder} + s.Require().NoError(s.result(s.run(p))) + s.Contains(a.output.String(), "resolved") + }) + } +} + +func (s *terminalTestSuite) TestSwitchedUserEnvironment() { + if os.Geteuid() != 0 { + s.T().Skip("requires root") + } + target, err := user.Lookup("nobody") + s.Require().NoError(err) + s.Require().NotEqual("0", target.Uid) + dir := s.T().TempDir() + script := fmt.Sprintf("#!/bin/sh\nprintf '%%s\n' '%s:x:%s:%s::%s:/bin/sh'\n", + target.Username, target.Uid, target.Gid, target.HomeDir) + s.Require().NoError(writeExecutableFile(filepath.Join(dir, "getent"), []byte(script))) + s.T().Setenv(pathEnv, dir+string(os.PathListSeparator)+os.Getenv(pathEnv)) + for _, override := range []bool{false, true} { + a := newTestTerminal() + p := s.params( + context.Background(), + a, + `printf '%s|%s|%s|%s' "$HOME" "$USER" "$LOGNAME" "$SHELL"`, + ) + p.env.remoteUser = target.Username + p.env.workspaceFolder = testTempDir + home := target.HomeDir + if override { + home = "/custom-home" + p.env.remoteEnv = map[string]string{"HOME": home} + } + s.Require().NoError(s.result(s.run(p))) + s.Contains(a.output.String(), home+"|"+target.Username+"|"+target.Username+"|/bin/sh") + } +} + +func (s *terminalTestSuite) TestValidationBeforeExecution() { + a := newTestTerminal() + dir := s.T().TempDir() + result := &config.Result{ + MergedConfig: &config.MergedDevContainerConfig{}, + ContainerDetails: &config.ContainerDetails{}, + SubstitutionContext: &config.SubstitutionContext{ContainerWorkspaceFolder: dir}, + } + result.MergedConfig.OnCreateCommands = []types.LifecycleHook{ + {"": {fmt.Sprintf("touch %s", filepath.Join(dir, "ran"))}}, + } + result.MergedConfig.PostStartCommands = []types.LifecycleHook{ + {"a": {trueCommand}, "b": {trueCommand}}, + } + _, err := RunPreAttachHooks(context.Background(), result, PreAttachOptions{Terminal: a}) + s.ErrorContains(err, "postStartCommand") + _, err = os.Stat(filepath.Join(dir, "ran")) + s.True(os.IsNotExist(err)) + _, err = SetupContainerPreAttach( + context.Background(), + &ContainerSetupConfig{SetupInfo: result, Terminal: a}, + ) + s.ErrorContains(err, "postStartCommand") +} + +func (s *terminalTestSuite) TestFloorAndStoredWaitFor() { + for _, configured := range []string{"", "postAttachCommand"} { + s.Run(configured, func() { + a := newTestTerminal() + dir := s.T().TempDir() + result := &config.Result{ + MergedConfig: &config.MergedDevContainerConfig{}, + ContainerDetails: &config.ContainerDetails{}, + SubstitutionContext: &config.SubstitutionContext{ContainerWorkspaceFolder: dir}, + } + result.MergedConfig.WaitFor = configured + result.MergedConfig.PostStartCommands = []types.LifecycleHook{{"": {trueCommand}}} + result.MergedConfig.RemoteUser = s.username + deferred, err := RunPreAttachHooks( + context.Background(), + result, + PreAttachOptions{Terminal: a}, + ) + s.Require().NoError(err) + s.Equal(configured, result.MergedConfig.WaitFor) + s.Len(a.invocations, 1) + for _, hook := range deferred.hooks { + s.Nil(hook.params.terminal) + } + }) + } +} + +func (s *terminalTestSuite) TestMarkerSkipAndShellFailure() { + a := newTestTerminal() + p := s.params(context.Background(), a, `printf ran`) + p.name = fmt.Sprintf("interactive-test-%d", time.Now().UnixNano()) + p.content = "started" + path := filepath.Join(containerDataDir(), p.name+".marker") + s.T().Cleanup(func() { _ = os.Remove(path) }) + p.env.remoteUser = "nonexistent-interactive-user" + s.Error(runHook(p)) + _, err := os.Stat(path) + s.True(os.IsNotExist(err)) + p.env.remoteUser = s.username + s.Require().NoError(runHook(p)) + s.Require().FileExists(path) + p.env.remoteUser = "nonexistent-interactive-user" + s.NoError(runHook(p)) + s.Len(a.invocations, 1) +} + +func (s *terminalTestSuite) TestArgvAndLargeInput() { + a := newTestTerminal() + p := s.params(context.Background(), a, "") + p.commands = []types.LifecycleHook{ + { + "argv": { + "sh", + "-c", + `stty raw -echo; printf 'ready:'; head -c 65536 /dev/zero | tr '\000' x; ` + + `dd bs=1024 count=128 of=/dev/null 2>/dev/null; printf done`, + }, + }, + } + done := s.run(p) + a.output.wait(s.T(), "ready:") + written := make(chan struct{}) + go func() { + defer close(written) + _, _ = a.writer.Write(bytes.Repeat([]byte("z"), 128*1024)) + }() + a.output.wait(s.T(), "done") + s.NoError(s.result(done)) + <-written +} + +func (s *terminalTestSuite) TestParentCancelAndExitAckSuccess() { + logs := log.InitTestObserved(s.T(), zapcore.WarnLevel) + a := newTestTerminal() + ctx, cancel := context.WithCancel(context.Background()) + p := s.params(ctx, a, `printf 'ready:'; exec sleep 30`) + run := s.run(p) + a.output.wait(s.T(), "ready:") + cancel() + s.ErrorContains(s.result(run), "canceled") + a = newTestTerminal() + a.exitErr = fmt.Errorf("ack timeout") + p = s.params(context.Background(), a, `printf 'final-output'`) + s.NoError(s.result(s.run(p))) + s.Contains(a.exitOutput, "final-output") + s.Equal(1, logs.FilterMessage("lifecycle terminal exit: ack timeout").Len()) + s.Equal(0, a.exit.ExitCode) +} + +func (s *terminalTestSuite) TestNoninteractivePostStartRemainsPipeBased() { + result := &config.Result{ + MergedConfig: &config.MergedDevContainerConfig{}, + ContainerDetails: &config.ContainerDetails{}, + SubstitutionContext: &config.SubstitutionContext{ContainerWorkspaceFolder: s.T().TempDir()}, + } + result.MergedConfig.RemoteUser = s.username + result.MergedConfig.WaitFor = "postStartCommand" + result.MergedConfig.PostStartCommands = []types.LifecycleHook{ + {"": {`test ! -t 0 && test ! -t 1`}}, + } + _, err := RunPreAttachHooks(context.Background(), result, PreAttachOptions{}) + s.NoError(err) +} + +func (s *terminalTestSuite) TestLoginShellCanceled() { + dir := s.T().TempDir() + s.Require().NoError(writeExecutableFile( + filepath.Join(dir, "getent"), []byte("#!/bin/sh\nexec /bin/sleep 30\n"), + )) + s.T().Setenv("PATH", dir) + ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) + defer cancel() + start := time.Now() + _, err := loginShell(ctx, s.username) + s.ErrorIs(err, context.DeadlineExceeded) + s.Less(time.Since(start), time.Second) +} + +type blockedTerminalOutput struct { + started chan struct{} + abort <-chan struct{} + once sync.Once +} + +func (b *blockedTerminalOutput) Write(p []byte) (int, error) { + b.once.Do(func() { close(b.started) }) + <-b.abort + return 0, context.Canceled +} + +func (s *terminalTestSuite) TestBackgroundOutputDrain() { + logs := log.InitTestObserved(s.T(), zapcore.WarnLevel) + a := newTestTerminal() + p := s.params( + context.Background(), + a, + `sh -c 'trap "" HUP; echo $$ > child.pid; exec sleep 30' & `+ + `while [ ! -s child.pid ]; do :; done; printf finished`, + ) + start := time.Now() + s.NoError(s.result(s.run(p))) + data, err := os.ReadFile(filepath.Join(p.env.workspaceFolder, "child.pid")) + s.Require().NoError(err) + pid, err := strconv.Atoi(strings.TrimSpace(string(data))) + s.Require().NoError(err) + s.T().Cleanup(func() { _ = syscall.Kill(pid, syscall.SIGKILL) }) + s.Less(time.Since(start), 5*time.Second) + s.Contains(a.output.String(), "finished") + s.Equal(0, a.exit.ExitCode) + s.Equal( + 1, + logs.FilterMessage("lifecycle terminal output was truncated after drain timeout").Len(), + ) +} + +func (s *terminalTestSuite) TestBlockedOutputDrain() { + a := newTestTerminal() + b := &blockedTerminalOutput{started: make(chan struct{}), abort: a.abort} + a.outputOverride = b + p := s.params(context.Background(), a, `printf blocked`) + start := time.Now() + done := s.run(p) + select { + case <-b.started: + case <-time.After(5 * time.Second): + s.T().Fatal("write did not start") + } + s.NoError(s.result(done)) + s.Less(time.Since(start), 5*time.Second) + s.Equal(0, a.exit.ExitCode) +} + +func (s *terminalTestSuite) TestFastOutputDrain() { + a := newTestTerminal() + p := s.params(context.Background(), a, `printf 'complete output'`) + s.NoError(s.result(s.run(p))) + s.Equal("complete output", a.output.String()) +} + +func (s *terminalTestSuite) params( + ctx context.Context, + a *testTerminal, + command string, +) hookRunParams { + return hookRunParams{ + ctx: ctx, + commands: []types.LifecycleHook{{"": {command}}}, + env: lifecycleEnv{remoteUser: s.username, workspaceFolder: s.T().TempDir()}, + name: "postStartCommands", + hook: string(PhasePostStart), + plan: &hookPlan{}, + terminal: a, + } +} + +func (s *terminalTestSuite) run(p hookRunParams) <-chan error { + done := make(chan error, 1) + go func() { done <- runHook(p) }() + return done +} + +func (s *terminalTestSuite) result(done <-chan error) error { + s.T().Helper() + select { + case err := <-done: + return err + case <-time.After(12 * time.Second): + s.T().Fatal("executor did not complete") + return nil + } +} + +func executableTestMode() os.FileMode { + return 0o600 | 0o100 +} + +func writeExecutableFile(path string, content []byte) error { + if err := os.WriteFile(path, content, 0o600); err != nil { + return err + } + return os.Chmod(path, executableTestMode()) +} diff --git a/pkg/devcontainer/setup/lifecycle_terminal_other.go b/pkg/devcontainer/setup/lifecycle_terminal_other.go new file mode 100644 index 0000000000..1fe4c3f2da --- /dev/null +++ b/pkg/devcontainer/setup/lifecycle_terminal_other.go @@ -0,0 +1,27 @@ +//go:build !linux + +package setup + +import ( + "context" + "errors" + + "github.com/devsy-org/devsy/pkg/agent/tunnel" + "github.com/devsy-org/devsy/pkg/subprocess" +) + +var errInteractiveUnsupported = errors.New( + "interactive lifecycle commands are only supported in Linux containers", +) + +type interactiveLaunch struct{} + +func prepareInteractiveLaunch(_ context.Context, _ []string, _ string) (interactiveLaunch, error) { + return interactiveLaunch{}, errInteractiveUnsupported +} + +func runInteractiveProcess( + _ context.Context, _ hookRunParams, _ interactiveLaunch, _ tunnel.TerminalAttachment, +) (subprocess.Result, error) { + return subprocess.Result{ExitCode: -1}, errInteractiveUnsupported +} diff --git a/pkg/devcontainer/setup/lifecycle_terminal_test.go b/pkg/devcontainer/setup/lifecycle_terminal_test.go new file mode 100644 index 0000000000..661b57b643 --- /dev/null +++ b/pkg/devcontainer/setup/lifecycle_terminal_test.go @@ -0,0 +1,28 @@ +package setup + +import ( + "testing" + + "github.com/devsy-org/devsy/pkg/types" + "github.com/stretchr/testify/suite" +) + +const trueCommand = "true" + +type interactiveValidationSuite struct{ suite.Suite } + +func TestInteractiveValidationSuite(t *testing.T) { suite.Run(t, new(interactiveValidationSuite)) } + +func (s *interactiveValidationSuite) TestValidation() { + s.NoError(ValidateInteractivePostStart(nil)) + s.NoError( + ValidateInteractivePostStart( + []types.LifecycleHook{{"a": {"echo"}, "b": nil}, {"c": {trueCommand}}}, + ), + ) + err := ValidateInteractivePostStart( + []types.LifecycleHook{nil, {"a": {"echo"}, "b": {trueCommand}}}, + ) + s.ErrorContains(err, "postStartCommand") + s.ErrorContains(err, "1") +} diff --git a/pkg/devcontainer/setup/lifecyclehooks.go b/pkg/devcontainer/setup/lifecyclehooks.go index 5cb5662ac8..9a87a962d9 100644 --- a/pkg/devcontainer/setup/lifecyclehooks.go +++ b/pkg/devcontainer/setup/lifecyclehooks.go @@ -17,6 +17,7 @@ import ( "time" "al.essio.dev/pkg/shellescape" + "github.com/devsy-org/devsy/pkg/agent/tunnel" "github.com/devsy-org/devsy/pkg/devcontainer/config" "github.com/devsy-org/devsy/pkg/log" "github.com/devsy-org/devsy/pkg/secrets" @@ -137,7 +138,8 @@ type hookRunParams struct { // index is the merged-config position of the command being run. index int // plan shares invocation IDs between planning and execution. - plan *hookPlan + plan *hookPlan + terminal tunnel.TerminalSession } // lifecycleEnv holds the resolved environment for running lifecycle hooks. @@ -308,6 +310,7 @@ type PreAttachOptions struct { SecretsMount []string Skip SkipPhases WaitFor LifecyclePhase + Terminal tunnel.TerminalSession } // RunPreAttachHooks runs lifecycle hooks up to and including the waitFor phase @@ -322,11 +325,21 @@ func RunPreAttachHooks( setupInfo *config.Result, opts PreAttachOptions, ) (DeferredHooks, error) { + if opts.Terminal != nil { + if err := ValidateInteractivePostStart( + setupInfo.MergedConfig.PostStartCommands, + ); err != nil { + return DeferredHooks{}, err + } + } env := resolveLifecycleEnv(ctx, setupInfo) redactor := mergeSecretsEnv(env.remoteEnv, opts.SecretsEnv, opts.SecretsMount) all := preAttachPhaseParams(ctx, setupInfo, env, opts.Prebuild) for i := range all { all[i].params.redactor = redactor + if all[i].phase == PhasePostStart { + all[i].params.terminal = opts.Terminal + } } // Insert the dotfiles phase between postCreate and postStart. @@ -343,7 +356,11 @@ func RunPreAttachHooks( return DeferredHooks{}, runPrebuildHooks(all) } - waitFor := resolveWaitFor(setupInfo.MergedConfig.WaitFor, opts.WaitFor) + floor := opts.WaitFor + if opts.Terminal != nil { + floor = resolveWaitFor(string(floor), PhasePostStart) + } + waitFor := resolveWaitFor(setupInfo.MergedConfig.WaitFor, floor) waitFor = promoteDotfilesWaitFor(waitFor, opts.Dotfiles) if !phaseHasCommands(all, waitFor) { @@ -551,6 +568,9 @@ func runHook(p hookRunParams) error { return nil } + if p.terminal != nil { + return runInteractiveHook(p) + } skip, err := shouldSkipHook(p.name, p.content) if err != nil { p.recordAll(workspacelogs.InvocationFailed, reasonMarkerCheckFailed) diff --git a/pkg/devcontainer/setup/setup.go b/pkg/devcontainer/setup/setup.go index 1cd3d98e8f..e448f7e70f 100644 --- a/pkg/devcontainer/setup/setup.go +++ b/pkg/devcontainer/setup/setup.go @@ -49,6 +49,7 @@ type ContainerSetupConfig struct { Prebuild bool PlatformOptions *devsy.PlatformOptions TunnelClient tunnel.TunnelClient + Terminal tunnel.TerminalSession Dotfiles DotfilesConfig SkipPostCreate bool SkipPostStart bool @@ -67,6 +68,14 @@ func SetupContainerPreAttach( return DeferredHooks{}, err } + if cfg.Terminal != nil { + if err := ValidateInteractivePostStart( + cfg.SetupInfo.MergedConfig.PostStartCommands, + ); err != nil { + return DeferredHooks{}, err + } + } + if err := writeResultFileWithContext(ctx, cfg); err != nil { return DeferredHooks{}, fmt.Errorf("write container result: %w", err) } @@ -100,7 +109,8 @@ func SetupContainerPreAttach( PostStart: cfg.SkipPostStart, PostAttach: cfg.SkipPostAttach, }, - WaitFor: cfg.WaitFor, + WaitFor: cfg.WaitFor, + Terminal: cfg.Terminal, }) if err != nil { return DeferredHooks{}, fmt.Errorf("lifecycle hooks pre-attach: %w", err) diff --git a/pkg/flags/names/names.go b/pkg/flags/names/names.go index 4f5da88af8..a7526f8a0d 100644 --- a/pkg/flags/names/names.go +++ b/pkg/flags/names/names.go @@ -127,6 +127,7 @@ const ( SkipHostRequirements = "skip-host-requirements" TerminalColumns = "terminal-columns" TerminalRows = "terminal-rows" + InteractivePostStart = "interactive-post-start" ) // IDE. diff --git a/pkg/flags/names/names_test.go b/pkg/flags/names/names_test.go index 6da7f58adf..5fdf92598b 100644 --- a/pkg/flags/names/names_test.go +++ b/pkg/flags/names/names_test.go @@ -63,6 +63,7 @@ var wireNames = []struct { {names.SkipHostRequirements, "skip-host-requirements"}, {names.TerminalColumns, "terminal-columns"}, {names.TerminalRows, "terminal-rows"}, + {names.InteractivePostStart, "interactive-post-start"}, {names.ID, "id"}, {names.Machine, "machine"}, {names.Source, "source"}, diff --git a/pkg/log/logger.go b/pkg/log/logger.go index e91870da34..acd8160317 100644 --- a/pkg/log/logger.go +++ b/pkg/log/logger.go @@ -41,7 +41,7 @@ func Init(cfg Config) { level := resolveLevel(cfg) encoder := resolveEncoder(cfg.Format) stderrWriter := zapcore.Lock(&redactingWriter{next: os.Stderr, redactor: cfg.Redactor}) - stderrCore := zapcore.NewCore(encoder, stderrWriter, level) + stderrCore := zapcore.NewCore(encoder, consoleWriter{next: stderrWriter}, level) // A separate core writes to the fanout sink with the same level/encoder // so AddSink consumers see the same output stderr does. sinkCore := zapcore.NewCore( @@ -55,6 +55,39 @@ func Init(cfg Config) { sugar.Store(logger.Sugar()) } +var consoleOutput = struct { + sync.RWMutex + suppressed int +}{} + +type consoleWriter struct{ next zapcore.WriteSyncer } + +func (w consoleWriter) Write(p []byte) (int, error) { + consoleOutput.RLock() + defer consoleOutput.RUnlock() + if consoleOutput.suppressed > 0 { + return len(p), nil + } + return w.next.Write(p) +} + +func (w consoleWriter) Sync() error { + consoleOutput.RLock() + defer consoleOutput.RUnlock() + if consoleOutput.suppressed > 0 { + return nil + } + return w.next.Sync() +} + +func SuppressConsoleOutput() func() { + consoleOutput.Lock() + consoleOutput.suppressed++ + consoleOutput.Unlock() + var once sync.Once + return func() { once.Do(func() { consoleOutput.Lock(); consoleOutput.suppressed--; consoleOutput.Unlock() }) } +} + type redactingWriter struct { mu sync.Mutex next io.Writer diff --git a/pkg/log/logger_test.go b/pkg/log/logger_test.go index 4e9e16c1e2..3664d3572e 100644 --- a/pkg/log/logger_test.go +++ b/pkg/log/logger_test.go @@ -2,6 +2,7 @@ package log import ( "bytes" + "io" "os" "strings" "sync" @@ -292,3 +293,41 @@ func (s *syncBuffer) String() string { defer s.mu.Unlock() return s.buf.String() } + +func TestSuppressConsoleOutput(t *testing.T) { + previous := sugar.Load() + oldStderr := os.Stderr + r, w, err := os.Pipe() + if err != nil { + t.Fatal(err) + } + os.Stderr = w + t.Cleanup(func() { os.Stderr = oldStderr; sugar.Store(previous); _ = r.Close(); _ = w.Close() }) + Init(Config{}) + var sink syncBuffer + remove := AddSink(&sink) + defer remove() + Error("before suppression") + restore := SuppressConsoleOutput() + Error("during suppression") + restore() + restore() + Error("after suppression") + _ = Sync() + if err = w.Close(); err != nil { + t.Fatal(err) + } + data, err := io.ReadAll(r) + if err != nil { + t.Fatal(err) + } + got := string(data) + if !strings.Contains(got, "before suppression") || + strings.Contains(got, "during suppression") || + !strings.Contains(got, "after suppression") { + t.Fatalf("unexpected console output %q", got) + } + if !strings.Contains(sink.String(), "during suppression") { + t.Fatal("suppressed sink output") + } +} diff --git a/pkg/provider/workspace.go b/pkg/provider/workspace.go index af90c5e63a..1bbe1c16fd 100644 --- a/pkg/provider/workspace.go +++ b/pkg/provider/workspace.go @@ -257,6 +257,13 @@ type GitToken struct { Token string `json:"token,omitempty"` } +type LifecycleTerminalRequest struct { + ProtocolVersion uint32 `json:"protocolVersion"` + Nonce string `json:"nonce"` + Context string `json:"context"` + WorkspaceUID string `json:"workspaceUid"` +} + type CLIOptions struct { // Platform are the platform options Platform devsy.PlatformOptions `json:"platformOptions"` @@ -266,7 +273,8 @@ type CLIOptions struct { // persisted state. SetupRunID string `json:"setupRunId,omitempty"` // SetupParentProducerID is the producer in the previous process hop. - SetupParentProducerID string `json:"setupParentProducerId,omitempty"` + SetupParentProducerID string `json:"setupParentProducerId,omitempty"` + LifecycleTerminal *LifecycleTerminalRequest `json:"lifecycleTerminal,omitempty"` // up options ID string `json:"id,omitempty"` diff --git a/pkg/pty/pty.go b/pkg/pty/pty.go index 37bb4f778c..2c4ebc57f7 100644 --- a/pkg/pty/pty.go +++ b/pkg/pty/pty.go @@ -75,6 +75,8 @@ type Process interface { // returned error is as for os.Process.Signal(), on Windows it's // as for os.Process.Kill(). Signal(sig os.Signal) error + + Pid() int } // Options represents a an option for a PTY. @@ -84,6 +86,12 @@ type ptyOptions struct { logger *log.Logger sshReq *ssh.Pty setGPGTTY bool + ttyOwner *ttyOwner +} + +type ttyOwner struct { + uid int + gid int } // WithSSHRequest applies the ssh.Pty request to the PTY. @@ -110,6 +118,12 @@ func WithGPGTTY() Option { } } +func WithTTYOwner(uid, gid int) Option { + return func(opts *ptyOptions) { + opts.ttyOwner = &ttyOwner{uid: uid, gid: gid} + } +} + // New constructs a new Pty. func New(opts ...Option) (PTY, error) { return newPty(opts...) diff --git a/pkg/pty/pty_other.go b/pkg/pty/pty_other.go index b0372cd6f9..9442eaef9d 100644 --- a/pkg/pty/pty_other.go +++ b/pkg/pty/pty_other.go @@ -38,6 +38,13 @@ func newPty(opt ...Option) (retPTY *otherPty, err error) { } }() + if opts.ttyOwner != nil { + err = opty.tty.Chown(opts.ttyOwner.uid, opts.ttyOwner.gid) + if err != nil { + return nil, err + } + } + if opts.sshReq != nil { err = opty.control(opty.tty, func(fd uintptr) error { return applyTerminalModesToFd(opts.logger, fd, *opts.sshReq) @@ -174,6 +181,10 @@ func (p *otherProcess) Signal(sig os.Signal) error { return p.cmd.Process.Signal(sig) } +func (p *otherProcess) Pid() int { + return p.cmd.Process.Pid +} + func (p *otherProcess) waitInternal() { // The GC can garbage collect the TTY FD before the command // has finished running. See: diff --git a/pkg/pty/pty_other_test.go b/pkg/pty/pty_other_test.go new file mode 100644 index 0000000000..6d0e97c010 --- /dev/null +++ b/pkg/pty/pty_other_test.go @@ -0,0 +1,70 @@ +//go:build !windows + +package pty_test + +import ( + "bufio" + "context" + "os" + "strconv" + "strings" + "syscall" + "testing" + "time" + + "github.com/devsy-org/devsy/pkg/pty" + "github.com/stretchr/testify/suite" +) + +type PTYOtherSuite struct { + suite.Suite +} + +func TestPTYOtherSuite(t *testing.T) { + suite.Run(t, new(PTYOtherSuite)) +} + +func (s *PTYOtherSuite) TestPidIsChildPid() { + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) + defer cancel() + + pc, ps, err := pty.Start(pty.CommandContext(ctx, "/bin/sh", "-c", "echo $$; exec sleep 30")) + s.Require().NoError(err) + defer func() { _ = pc.Close() }() + + line, err := bufio.NewReader(pc.OutputReader()).ReadString('\n') + s.Require().NoError(err) + pid, err := strconv.Atoi(strings.TrimSpace(line)) + s.Require().NoError(err) + s.Equal(pid, ps.Pid()) +} + +func (s *PTYOtherSuite) TestWithTTYOwner() { + uid, gid := os.Getuid(), os.Getgid() + if os.Geteuid() == 0 { + uid, gid = 4321, 4322 + } + + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) + defer cancel() + + pc, _, err := pty.Start( + pty.CommandContext(ctx, "/bin/sh", "-c", "tty; exec sleep 30"), + pty.WithPTYOption(pty.WithTTYOwner(uid, gid)), + ) + s.Require().NoError(err) + defer func() { _ = pc.Close() }() + + if os.Geteuid() != 0 { + return + } + + line, err := bufio.NewReader(pc.OutputReader()).ReadString('\n') + s.Require().NoError(err) + info, err := os.Stat(strings.TrimSpace(line)) + s.Require().NoError(err) + stat, ok := info.Sys().(*syscall.Stat_t) + s.Require().True(ok) + s.EqualValues(uid, stat.Uid) + s.EqualValues(gid, stat.Gid) +} diff --git a/pkg/pty/pty_windows.go b/pkg/pty/pty_windows.go index a3df377a09..e00190a7c0 100644 --- a/pkg/pty/pty_windows.go +++ b/pkg/pty/pty_windows.go @@ -263,6 +263,10 @@ func (p *windowsProcess) Signal(sig os.Signal) error { return p.Kill() } +func (p *windowsProcess) Pid() int { + return p.proc.Pid +} + // killOnContext waits for the context to be done and kills the process, unless it exits on its own first. func (p *windowsProcess) killOnContext(ctx context.Context) { select { diff --git a/pkg/pty/start_windows_test.go b/pkg/pty/start_windows_test.go index 786a665371..e46d2cee3d 100644 --- a/pkg/pty/start_windows_test.go +++ b/pkg/pty/start_windows_test.go @@ -3,11 +3,13 @@ package pty_test import ( + "context" "errors" "fmt" "os" "os/exec" "testing" + "time" "github.com/devsy-org/devsy/pkg/pty" "github.com/devsy-org/devsy/pkg/pty/ptytest" @@ -29,6 +31,16 @@ func (s *StartWindowsSuite) TestEcho() { s.Require().NoError(p.Close()) } +func (s *StartWindowsSuite) TestPid() { + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) + defer cancel() + p, ps := ptytest.Start(s.T(), pty.Command("cmd.exe", "/c", "echo", "test")) + s.Positive(ps.Pid()) + p.ExpectMatchContext(ctx, "test") + s.Require().NoError(ps.Wait()) + s.Require().NoError(p.Close()) +} + func (s *StartWindowsSuite) TestResize() { p, _ := ptytest.Start(s.T(), pty.Command("cmd.exe")) s.Require().NoError(p.Resize(100, 50)) diff --git a/pkg/ssh/server/exec_identity_unix.go b/pkg/ssh/server/exec_identity_unix.go index 1cc0bfb7f9..4abe5c0d52 100644 --- a/pkg/ssh/server/exec_identity_unix.go +++ b/pkg/ssh/server/exec_identity_unix.go @@ -233,3 +233,32 @@ func supplementaryExecGroups(account *user.User, lookups execIdentityLookups) ([ } return groups, nil } + +type ExecCredentials struct { + UID, GID uint32 + Groups []uint32 + MatchCurrent bool +} + +func ResolveExecCredentials(spec string) (ExecCredentials, error) { + identity, handled, err := parseRequestedExecIdentity(spec) + if err != nil { + return ExecCredentials{}, err + } + if !handled { + if spec == "" { + return ExecCredentials{}, fmt.Errorf("empty execution identity") + } + identity = requestedExecIdentity{user: spec} + } + credentials, err := resolveExecCredentials(identity, systemExecIdentityLookups()) + if err != nil { + return ExecCredentials{}, err + } + return ExecCredentials{ + UID: credentials.uid, + GID: credentials.gid, + Groups: credentials.groups, + MatchCurrent: execCredentialsMatchCurrentProcess(credentials), + }, nil +} diff --git a/pkg/ssh/server/exec_identity_windows.go b/pkg/ssh/server/exec_identity_windows.go index fc17010afa..cacfa683c1 100644 --- a/pkg/ssh/server/exec_identity_windows.go +++ b/pkg/ssh/server/exec_identity_windows.go @@ -19,3 +19,13 @@ func prepareExecIdentity(_ *exec.Cmd, spec string) (bool, error) { func prepareExecIdentityForAccount(cmd *exec.Cmd, spec string, _ *user.User) (bool, error) { return prepareExecIdentity(cmd, spec) } + +type ExecCredentials struct { + UID, GID uint32 + Groups []uint32 + MatchCurrent bool +} + +func ResolveExecCredentials(_ string) (ExecCredentials, error) { + return ExecCredentials{}, errors.New("execution identities are not supported on Windows") +} diff --git a/pkg/ssh/ssh_unsupported.go b/pkg/ssh/ssh_unsupported.go index 98196b24bd..72abef1d7f 100644 --- a/pkg/ssh/ssh_unsupported.go +++ b/pkg/ssh/ssh_unsupported.go @@ -19,7 +19,11 @@ func WatchWindowSize(ctx context.Context) <-chan os.Signal { return case <-ticker.C: } - windowSize <- nil + select { + case windowSize <- nil: + case <-ctx.Done(): + return + } } }() return windowSize diff --git a/pkg/terminal/attached_unix.go b/pkg/terminal/attached_unix.go new file mode 100644 index 0000000000..03f2c88876 --- /dev/null +++ b/pkg/terminal/attached_unix.go @@ -0,0 +1,33 @@ +//go:build !windows + +package terminal + +import ( + "errors" + "fmt" + "os" + + dockerterm "github.com/moby/term" + "golang.org/x/sys/unix" +) + +var ErrNotAttached = errors.New("stdin and stdout must be attached to the foreground terminal") + +var isForegroundProcessGroup = func(foreground, process int) bool { + return foreground == process +} + +func Attached(stdin, stdout *os.File) error { + if stdin == nil || stdout == nil || !dockerterm.IsTerminal(stdin.Fd()) || + !dockerterm.IsTerminal(stdout.Fd()) { + return ErrNotAttached + } + foreground, err := unix.IoctlGetInt(int(stdin.Fd()), unix.TIOCGPGRP) + if err != nil { + return fmt.Errorf("get terminal foreground process group: %w", err) + } + if !isForegroundProcessGroup(int(foreground), unix.Getpgrp()) { + return ErrNotAttached + } + return nil +} diff --git a/pkg/terminal/attached_windows.go b/pkg/terminal/attached_windows.go new file mode 100644 index 0000000000..a02f442df2 --- /dev/null +++ b/pkg/terminal/attached_windows.go @@ -0,0 +1,20 @@ +//go:build windows + +package terminal + +import ( + "errors" + "os" + + dockerterm "github.com/moby/term" +) + +var ErrNotAttached = errors.New("stdin and stdout must be attached to the console") + +func Attached(stdin, stdout *os.File) error { + if stdin == nil || stdout == nil || !dockerterm.IsTerminal(stdin.Fd()) || + !dockerterm.IsTerminal(stdout.Fd()) { + return ErrNotAttached + } + return nil +} diff --git a/pkg/terminal/input.go b/pkg/terminal/input.go new file mode 100644 index 0000000000..adfa53a8a1 --- /dev/null +++ b/pkg/terminal/input.go @@ -0,0 +1,22 @@ +package terminal + +import ( + "os" + + "github.com/muesli/cancelreader" +) + +type input struct { + cancelreader.CancelReader +} + +func NewInput(f *os.File) (Input, error) { + if f == nil { + return nil, os.ErrInvalid + } + r, err := cancelreader.NewReader(f) + if err != nil { + return nil, err + } + return &input{CancelReader: r}, nil +} diff --git a/pkg/terminal/raw.go b/pkg/terminal/raw.go new file mode 100644 index 0000000000..4f37afc36f --- /dev/null +++ b/pkg/terminal/raw.go @@ -0,0 +1,77 @@ +package terminal + +import ( + "io" + "os" + "slices" + "sync" + + "github.com/devsy-org/devsy/pkg/pty" +) + +var restores = struct { + sync.Mutex + items []*restoreEntry +}{} + +type Input interface { + io.Reader + Cancel() bool + Close() error +} + +func EnterRaw(stdin, stdout *os.File) (func(), error) { + if stdin == nil || stdout == nil { + return nil, ErrNotAttached + } + inState, err := pty.MakeInputRaw(stdin.Fd()) + if err != nil { + return nil, err + } + outState, err := pty.MakeOutputRaw(stdout.Fd()) + if err != nil { + _ = pty.RestoreTerminal(stdin.Fd(), inState) + return nil, err + } + restore := registerRestore(func() { + _ = pty.RestoreTerminal(stdout.Fd(), outState) + _ = pty.RestoreTerminal(stdin.Fd(), inState) + }) + return restore, nil +} + +type restoreEntry struct { + once sync.Once + fn func() +} + +func registerRestore(fn func()) func() { + entry := &restoreEntry{fn: fn} + restore := func() { + entry.once.Do(func() { + restores.Lock() + for i, item := range restores.items { + if item == entry { + restores.items = slices.Delete(restores.items, i, i+1) + break + } + } + restores.Unlock() + entry.fn() + }) + } + restores.Lock() + restores.items = append(restores.items, entry) + restores.Unlock() + return restore +} + +func RestoreAll() { + restores.Lock() + items := restores.items + restores.items = nil + restores.Unlock() + for _, item := range slices.Backward(items) { + item.once.Do(item.fn) + } +} diff --git a/pkg/terminal/terminal_test.go b/pkg/terminal/terminal_test.go new file mode 100644 index 0000000000..047ae9ac7d --- /dev/null +++ b/pkg/terminal/terminal_test.go @@ -0,0 +1,91 @@ +package terminal + +import ( + "os" + "runtime" + "testing" + "time" + + "github.com/stretchr/testify/suite" +) + +type TerminalSuite struct { + suite.Suite +} + +func TestTerminalSuite(t *testing.T) { + suite.Run(t, new(TerminalSuite)) +} + +func (s *TerminalSuite) TestAttachedRejectsPipes() { + inR, inW, err := os.Pipe() + s.Require().NoError(err) + defer func() { s.NoError(inR.Close()) }() + defer func() { s.NoError(inW.Close()) }() + outR, outW, err := os.Pipe() + s.Require().NoError(err) + defer func() { s.NoError(outR.Close()) }() + defer func() { s.NoError(outW.Close()) }() + s.ErrorIs(Attached(inR, outW), ErrNotAttached) +} + +func (s *TerminalSuite) TestInputCancelUnblocksRead() { + if runtime.GOOS == "windows" { + s.T().Skip("pipe reads are not cancellable on Windows") + } + r, w, err := os.Pipe() + s.Require().NoError(err) + defer func() { s.NoError(r.Close()) }() + defer func() { s.NoError(w.Close()) }() + input, err := NewInput(r) + s.Require().NoError(err) + done := make(chan error, 1) + go func() { + _, readErr := input.Read(make([]byte, 1)) + done <- readErr + }() + s.Eventually(func() bool { return input.Cancel() }, time.Second, time.Millisecond) + var readErr error + s.Eventually(func() bool { + select { + case readErr = <-done: + return true + default: + return false + } + }, time.Second, time.Millisecond) + s.Error(readErr) + _, err = w.Write([]byte("x")) + s.Require().NoError(err) + buf := make([]byte, 1) + _, err = input.Read(buf) + s.Error(err) + fresh, err := NewInput(r) + s.Require().NoError(err) + n, err := fresh.Read(buf) + s.NoError(err) + s.Equal(1, n) + s.Equal(byte('x'), buf[0]) + s.NoError(input.Close()) + s.NoError(fresh.Close()) +} + +func (s *TerminalSuite) TestRestoreRegistry() { + for range 100 { + restore := registerRestore(func() {}) + restore() + restore() + } + restores.Lock() + s.Empty(restores.items) + restores.Unlock() + var order []int + for i := range 3 { + registerRestore(func() { order = append(order, i) }) + } + RestoreAll() + s.Equal([]int{2, 1, 0}, order) + restores.Lock() + s.Empty(restores.items) + restores.Unlock() +} diff --git a/pkg/terminal/terminal_unix_test.go b/pkg/terminal/terminal_unix_test.go new file mode 100644 index 0000000000..9ec4057255 --- /dev/null +++ b/pkg/terminal/terminal_unix_test.go @@ -0,0 +1,44 @@ +//go:build !windows + +package terminal + +import ( + "os" + "testing" + + "github.com/devsy-org/devsy/pkg/pty/ptytest" + "github.com/stretchr/testify/suite" +) + +type UnixTerminalSuite struct { + suite.Suite +} + +func TestUnixTerminalSuite(t *testing.T) { + suite.Run(t, new(UnixTerminalSuite)) +} + +func (s *UnixTerminalSuite) TestAttachedForegroundAndBackgroundPTY() { + s.True(isForegroundProcessGroup(22, 22)) + s.False(isForegroundProcessGroup(22, 23)) + device := ptytest.New(s.T()) + f, err := os.OpenFile(device.Name(), os.O_RDWR, 0) + s.Require().NoError(err) + defer func() { s.NoError(f.Close()) }() + s.Error(Attached(f, f)) +} + +func (s *UnixTerminalSuite) TestRawModeRestoreAndRegistryOnPTY() { + device := ptytest.New(s.T()) + f, err := os.OpenFile(device.Name(), os.O_RDWR, 0) + s.Require().NoError(err) + defer func() { s.NoError(f.Close()) }() + restore, err := EnterRaw(f, f) + s.Require().NoError(err) + second, err := EnterRaw(f, f) + s.Require().NoError(err) + RestoreAll() + restore() + second() + RestoreAll() +} diff --git a/pkg/terminal/terminal_windows_test.go b/pkg/terminal/terminal_windows_test.go new file mode 100644 index 0000000000..07d2ca600b --- /dev/null +++ b/pkg/terminal/terminal_windows_test.go @@ -0,0 +1,73 @@ +//go:build windows + +package terminal + +import ( + "os" + "testing" + "time" + + "github.com/stretchr/testify/suite" + "golang.org/x/sys/windows" +) + +type WindowsTerminalSuite struct { + suite.Suite +} + +func TestWindowsTerminalSuite(t *testing.T) { + suite.Run(t, new(WindowsTerminalSuite)) +} + +func (s *WindowsTerminalSuite) TestVTArrowKeysArrive() { + r, w, err := os.Pipe() + s.Require().NoError(err) + defer func() { s.NoError(r.Close()) }() + defer func() { s.NoError(w.Close()) }() + input, err := NewInput(r) + s.Require().NoError(err) + defer func() { s.NoError(input.Close()) }() + _, err = w.Write([]byte("\x1b[A\x1b[B\x1b[C\x1b[D")) + s.Require().NoError(err) + buf := make([]byte, 12) + done := make(chan error, 1) + go func() { + _, readErr := input.Read(buf) + done <- readErr + }() + s.Require().Eventually(func() bool { + select { + case <-done: + return true + default: + return false + } + }, time.Second, time.Millisecond) + s.Equal([]byte("\x1b[A\x1b[B\x1b[C\x1b[D"), buf) +} + +func (s *WindowsTerminalSuite) TestConsoleInputCancelUnblocksRead() { + var mode uint32 + if err := windows.GetConsoleMode(windows.Handle(os.Stdin.Fd()), &mode); err != nil { + s.T().Skip("stdin is not a console") + } + input, err := NewInput(os.Stdin) + s.Require().NoError(err) + defer func() { s.NoError(input.Close()) }() + done := make(chan error, 1) + go func() { _, readErr := input.Read(make([]byte, 1)); done <- readErr }() + canceled := make(chan bool, 1) + go func() { canceled <- input.Cancel() }() + select { + case ok := <-canceled: + s.True(ok) + case <-time.After(5 * time.Second): + s.T().Fatal("cancel blocked") + } + select { + case readErr := <-done: + s.Error(readErr) + case <-time.After(5 * time.Second): + s.T().Fatal("read did not unblock") + } +} diff --git a/sites/docs-devsy-sh/content/docs/developing-in-workspaces/continuous-integration.mdx b/sites/docs-devsy-sh/content/docs/developing-in-workspaces/continuous-integration.mdx index 5800096c42..83c2130f87 100644 --- a/sites/docs-devsy-sh/content/docs/developing-in-workspaces/continuous-integration.mdx +++ b/sites/docs-devsy-sh/content/docs/developing-in-workspaces/continuous-integration.mdx @@ -96,6 +96,21 @@ devsy workspace logs --source setup --run RUN_ID --context CONTEXT --result-form Retention is limited. See [Setup history](../troubleshooting/setup-history.mdx). The history covers the build and lifecycle hooks. The stdout and stderr of your command after `--` stay on their normal streams. Devsy does not wait for hooks still running when the command ends. +## Hooks cannot ask for input + +CI jobs have no terminal to answer a prompt. `devsy ci` has no `--interactive-post-start` option. Write hooks that take their values from the job instead. + +Desktop launches, `--detach`, JSON output, and management or platform tasks also stay noninteractive. If you pass `--interactive-post-start` to `devsy workspace up` in one of these runs, Devsy rejects it with an error. It does not wait for input that nobody can see. + +Pass values to the workspace in one of these ways: + +- Environment variables: `devsy ci --workspace-env NAME=value -- make test`. Use `--workspace-env` for values that hooks need. The `--remote-env` flag applies only to the command that CI runs. +- Secrets: `--secret` injects a stored Devsy secret. See [Secrets](./secrets.mdx). +- Files: write the value to a file that the hook reads. +- Arguments: put a value that is not secret in the hook command, such as `"postStartCommand": ["./start.sh", "example-name"]`. + +Moving the prompt to `postAttachCommand` does not provide input. `postAttachCommand` runs without input in every mode. + ## Build and push images To build and publish a devcontainer image for reuse as a build cache, run `devsy workspace build`: diff --git a/sites/docs-devsy-sh/content/docs/developing-in-workspaces/create-a-workspace.mdx b/sites/docs-devsy-sh/content/docs/developing-in-workspaces/create-a-workspace.mdx index 7a8110fbe2..cc7e834a8b 100644 --- a/sites/docs-devsy-sh/content/docs/developing-in-workspaces/create-a-workspace.mdx +++ b/sites/docs-devsy-sh/content/docs/developing-in-workspaces/create-a-workspace.mdx @@ -23,7 +23,7 @@ Open **Workspaces** and click **Create**. The wizard has five steps: 4. **Review.** Check the workspace name and configuration. If the image has no build for your architecture, you can turn on **Run under emulation**. 5. **Launch.** Devsy creates the workspace and streams progress, then opens your IDE if you selected one. -The desktop app runs `devsy workspace up` for you. +The desktop app runs `devsy workspace up` for you. Desktop launches run without a terminal, so if a `postStartCommand` needs input, create or start the workspace in an attached CLI terminal and open it from Desktop afterward. ### Workspace IDs @@ -112,6 +112,37 @@ For a detached creation, Devsy waits up to 30 seconds for the background worker See [Troubleshooting](../troubleshooting/troubleshooting.mdx#find-the-right-log) for all options and [Setup history](../troubleshooting/setup-history.mdx) for limits and redaction. +## Answer prompts in postStartCommand + +Some `postStartCommand` hooks ask for input, such as a name or a token. Add `--interactive-post-start` to connect your terminal to that hook: + +```sh +devsy workspace up ./project --interactive-post-start --ide none +``` + +This `devcontainer.json` asks for a name when the workspace starts: + +```json +{ + "image": "mcr.microsoft.com/devcontainers/base:ubuntu", + "postStartCommand": "read -r -p 'Name: ' NAME && echo \"Hello, $NAME\"" +} +``` + +`read -p` works only in Bash. If the hook runs under `sh`, use `printf 'Name: '` and then `read -r NAME`. + +The prompt appears as soon as the hook asks for it. Type your answer in the terminal. For that run, `devsy workspace up` waits for `postStartCommand` to finish before it returns, even if your project sets an earlier `waitFor`. Hooks before `postStartCommand` still run in their usual order. Devsy does not change `waitFor` in your project, and a later `waitFor` value still applies. The option gives input only to `postStartCommand`. `postAttachCommand` and the other hooks still run without input. + +`postStartCommand` can be a string, an array of arguments, or an object with one named command. An object with two or more named commands fails before any post-start command runs. The error gives the object's position, for example `postStartCommand merged index 0 has multiple nonempty named commands`. Without the option, the named commands in one object still run in parallel. + +Running `devsy workspace up` on a workspace that has already started does not run `postStartCommand` again, with or without the option. No prompt appears and the start finishes normally. The skip reason is recorded in [Setup history](../troubleshooting/setup-history.mdx). Stopping and starting the workspace runs `postStartCommand` again. Start it with the option to answer the prompt: + +```sh +devsy workspace up my-workspace --interactive-post-start +``` + +The option needs an attached terminal for stdin and stdout, and plain output. Devsy rejects it before it starts the workspace when you combine it with `--detach`, `--prebuild`, `--recovery`, `--skip-post-start`, or `--skip-non-blocking-commands`. It also rejects it for JSON output, for a script or pipe without a terminal, and for Devsy Desktop launches. [Troubleshooting](../troubleshooting/troubleshooting.mdx#interactive-poststartcommand-errors) lists each error message. + ## .devsyignore Put a `.devsyignore` file in the root of a local workspace folder to leave files out of the upload when Devsy uploads the folder to the provider, as it does for Kubernetes. The file uses [Docker ignore syntax](https://docs.docker.com/build/concepts/context/#dockerignore-files). Paths are relative to the folder. The last matching rule wins. diff --git a/sites/docs-devsy-sh/content/docs/developing-in-workspaces/devcontainer-json.mdx b/sites/docs-devsy-sh/content/docs/developing-in-workspaces/devcontainer-json.mdx index e149fc5bcc..a23ca7ef8b 100644 --- a/sites/docs-devsy-sh/content/docs/developing-in-workspaces/devcontainer-json.mdx +++ b/sites/docs-devsy-sh/content/docs/developing-in-workspaces/devcontainer-json.mdx @@ -104,6 +104,12 @@ Devsy builds and validates the new image before stopping or replacing the existi Anything outside volumes is lost. The project folder and other mounted paths stay. +## postStartCommand and waitFor + +`waitFor` names the lifecycle hook that `devsy workspace up` waits for before it returns. The default is `updateContentCommand`, so `postStartCommand` can finish after `up` returns. + +A `postStartCommand` that asks for input needs an attached terminal. Add `--interactive-post-start` to that `devsy workspace up` run. The option does not change `waitFor` in this file. For the details and limits, see [Answer prompts in postStartCommand](./create-a-workspace.mdx#answer-prompts-in-poststartcommand). + ## Environment variables in devcontainer.json With the SSH provider, the variable in `${localEnv:NAME}` must reach the remote machine. diff --git a/sites/docs-devsy-sh/content/docs/troubleshooting/setup-history.mdx b/sites/docs-devsy-sh/content/docs/troubleshooting/setup-history.mdx index 187175fc54..6eb9cbe071 100644 --- a/sites/docs-devsy-sh/content/docs/troubleshooting/setup-history.mdx +++ b/sites/docs-devsy-sh/content/docs/troubleshooting/setup-history.mdx @@ -52,6 +52,10 @@ An unavailable history source returns a typed status in the response. The comman To fetch missing remote records, Devsy makes one read-only attempt with a five-second limit. It does not start, unpause, rebuild or install anything. If the workspace is stopped, you get the cached records and an availability note. +## Interactive hooks + +Output from a `postStartCommand` run with `--interactive-post-start` appears live in your terminal. Devsy does not store it in setup history, so `devsy workspace logs --source setup` does not show it. Devsy does not redact that live output, so a secret that the hook prints is visible on the terminal. + ## Desktop Open the workspace and choose **Logs**. diff --git a/sites/docs-devsy-sh/content/docs/troubleshooting/troubleshooting.mdx b/sites/docs-devsy-sh/content/docs/troubleshooting/troubleshooting.mdx index 82ec3f5cf8..83562a0849 100644 --- a/sites/docs-devsy-sh/content/docs/troubleshooting/troubleshooting.mdx +++ b/sites/docs-devsy-sh/content/docs/troubleshooting/troubleshooting.mdx @@ -119,6 +119,25 @@ Diagnostics omit executable paths, command arguments, folder URIs, editor output A dispatched launch request means the editor CLI or OS URI handler accepted it. Devsy does not track whether the editor stayed running or connected over SSH. If the editor closes or does not connect, collect its own logs and check them for private data before you share them. +### Interactive postStartCommand errors + +`devsy workspace up --interactive-post-start` checks the request before it starts work. It fails with one of these messages: + +| Message | Fix | +| --- | --- | +| `--interactive-post-start cannot be combined with --detach, --prebuild, --recovery, --skip-post-start, or --skip-non-blocking-commands` | Remove the conflicting flag, or drop `--interactive-post-start`. | +| `--interactive-post-start requires plain output; JSON result output cannot share the terminal` | Remove the JSON result format. | +| `--interactive-post-start requires an attached terminal and is unavailable in DEVSY_UI mode` | Run the command in a terminal. Devsy Desktop sets this mode. | +| `--interactive-post-start requires attached stdin and stdout terminals: ...` | Run the command in a terminal, not in a script, a pipe, or with redirected output. | +| `--interactive-post-start is unsupported with proxy workspaces`, `... with daemon workspaces`, or `... with platform-managed workspaces` | Run without the option. These routes do not support it yet. | +| `postStartCommand merged index N has multiple nonempty named commands` | Keep one named command in object `N`, or run without the option so they run in parallel. | + +### Interactive postStartCommand limits + +- A background job started without `nohup` receives SIGHUP when the hook exits. Start it with `nohup` and redirect its output, for example `nohup ./server > server.log 2>&1 &`. +- A host helper that reads `/dev/tty` while the hook runs, such as some credential helpers, can compete with the hook for input. +- Hook output appears only in the terminal. See [Setup history](./setup-history.mdx#interactive-hooks). + ### Windows: `line 2: $'\r': command not found` Windows line endings break shell scripts inside the container. Add this to `.gitattributes`: