in helpers/ssh/stub_ssh_server.go [365:431]
func (s *StubSSHServer) handleSession(ctx context.Context, channel ssh.NewChannel) error {
conn, reqs, err := channel.Accept()
if err != nil {
return err
}
defer conn.Close()
for req := range reqs {
switch req.Type {
case "exec":
if req.WantReply {
if err := req.Reply(true, nil); err != nil {
return err
}
}
var command struct {
Value []byte
}
if err := ssh.Unmarshal(req.Payload, &command); err != nil {
return fmt.Errorf("session unmarshal: %w", err)
}
if ctx.Err() != nil {
return ctx.Err()
}
if len(s.Shell) == 0 {
s.Shell = []string{"sh", "-c"}
}
args := append(s.Shell, string(command.Value)) //nolint:gocritic
cmd := exec.CommandContext(ctx, args[0], args[1:]...)
cmd.Dir = s.tempDir
cmd.Stdout = conn
cmd.Stderr = conn
cmd.Stdin = conn
runErr := runCmd(cmd)
if ctx.Err() != nil {
return ctx.Err()
}
var exitError *exec.ExitError
code := 0
if errors.As(runErr, &exitError) {
code = exitError.ExitCode()
}
var exit [4]byte
binary.BigEndian.PutUint32(exit[:], uint32(code))
if err := conn.CloseWrite(); err != nil {
return err
}
if _, err := conn.SendRequest("exit-status", false, exit[:]); err != nil {
return err
}
return runErr
default:
return fmt.Errorf("unknown request type: %s", req.Type)
}
}
return nil
}