From afa917d4c44ed82ee9c6314e1ee0a73ae78879b7 Mon Sep 17 00:00:00 2001 From: Fabian Kramm Date: Thu, 31 Mar 2022 11:19:28 +0200 Subject: [PATCH 1/3] fix: set session env via command --- helper/cmd/proxy_commands/run.go | 19 +++---------------- helper/types/proxy_command.go | 1 + pkg/devspace/services/proxycommands/server.go | 2 +- 3 files changed, 5 insertions(+), 17 deletions(-) diff --git a/helper/cmd/proxy_commands/run.go b/helper/cmd/proxy_commands/run.go index 30c5d02620..b1d1d583e5 100644 --- a/helper/cmd/proxy_commands/run.go +++ b/helper/cmd/proxy_commands/run.go @@ -10,7 +10,6 @@ import ( "golang.org/x/crypto/ssh" "io/ioutil" "os" - "strings" ) // RunCmd holds the ssh cmd flags @@ -59,19 +58,6 @@ func (cmd *RunCmd) Run(_ *cobra.Command, args []string) error { } defer session.Close() - // set environment variables - for _, v := range os.Environ() { - splitted := strings.Split(v, "=") - if len(splitted) < 2 { - continue - } - - err = session.Setenv(splitted[0], strings.Join(splitted[1:], "=")) - if err != nil { - return errors.Wrap(err, "set session env") - } - } - // check if we should use a pty tty, t := terminal.SetupTTY(os.Stdin, os.Stdout) if tty { @@ -81,8 +67,8 @@ func (cmd *RunCmd) Run(_ *cobra.Command, args []string) error { if err == nil { err = session.RequestPty("xterm", int(winSize.Height), int(winSize.Width), ssh.TerminalModes{ ssh.ECHO: 0, - ssh.TTY_OP_ISPEED: 14400, - ssh.TTY_OP_OSPEED: 14400, + ssh.TTY_OP_ISPEED: 28800, + ssh.TTY_OP_OSPEED: 28800, }) if err != nil { return errors.Wrap(err, "request pty") @@ -101,6 +87,7 @@ func (cmd *RunCmd) Run(_ *cobra.Command, args []string) error { // marshal command and execute command proxyCommand := &types.ProxyCommand{ + Env: os.Environ(), Args: args, WorkingDir: currentWorkingDir, } diff --git a/helper/types/proxy_command.go b/helper/types/proxy_command.go index 6e7a98b95b..34ca1a15a6 100644 --- a/helper/types/proxy_command.go +++ b/helper/types/proxy_command.go @@ -1,6 +1,7 @@ package types type ProxyCommand struct { + Env []string `json:"env,omitempty"` Args []string `json:"args,omitempty"` WorkingDir string `json:"workingDir,omitempty"` } diff --git a/pkg/devspace/services/proxycommands/server.go b/pkg/devspace/services/proxycommands/server.go index 8984f921f4..a55c36efdb 100644 --- a/pkg/devspace/services/proxycommands/server.go +++ b/pkg/devspace/services/proxycommands/server.go @@ -166,7 +166,7 @@ func (s *Server) getCommand(sess ssh.Session) (*exec.Cmd, error) { } s.log.Debugf("run command '%s %s' locally", c, strings.Join(args, " ")) - cmd.Env = append(cmd.Env, sess.Environ()...) + cmd.Env = append(cmd.Env, command.Env...) cmd.Env = append(cmd.Env, os.Environ()...) return cmd, nil } From a45391285cbc75e663771b55b27440bd1760f9dc Mon Sep 17 00:00:00 2001 From: Fabian Kramm Date: Thu, 31 Mar 2022 13:20:08 +0200 Subject: [PATCH 2/3] fix: improve proxy commands --- helper/cmd/proxy_commands/run.go | 23 +++++++------- helper/types/proxy_command.go | 4 +++ pkg/devspace/services/proxycommands/server.go | 30 ++++++++++++------- 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/helper/cmd/proxy_commands/run.go b/helper/cmd/proxy_commands/run.go index b1d1d583e5..77d35d329e 100644 --- a/helper/cmd/proxy_commands/run.go +++ b/helper/cmd/proxy_commands/run.go @@ -58,23 +58,20 @@ func (cmd *RunCmd) Run(_ *cobra.Command, args []string) error { } defer session.Close() - // check if we should use a pty + // check if we should use a pty# + var ( + width = 0 + height = 0 + ) + tty, t := terminal.SetupTTY(os.Stdin, os.Stdout) if tty { info, ok := term.GetFdInfo(t.In) if ok { winSize, err := term.GetWinsize(info) if err == nil { - err = session.RequestPty("xterm", int(winSize.Height), int(winSize.Width), ssh.TerminalModes{ - ssh.ECHO: 0, - ssh.TTY_OP_ISPEED: 28800, - ssh.TTY_OP_OSPEED: 28800, - }) - if err != nil { - return errors.Wrap(err, "request pty") - } - - // TODO: terminal resize + width = int(winSize.Width) + height = int(winSize.Height) } } } @@ -87,6 +84,10 @@ func (cmd *RunCmd) Run(_ *cobra.Command, args []string) error { // marshal command and execute command proxyCommand := &types.ProxyCommand{ + TTY: tty, + Width: width, + Height: height, + Env: os.Environ(), Args: args, WorkingDir: currentWorkingDir, diff --git a/helper/types/proxy_command.go b/helper/types/proxy_command.go index 34ca1a15a6..c86852e6e3 100644 --- a/helper/types/proxy_command.go +++ b/helper/types/proxy_command.go @@ -1,6 +1,10 @@ package types type ProxyCommand struct { + TTY bool `json:"tty,omitempty"` + Height int `json:"height,omitempty"` + Width int `json:"width,omitempty"` + Env []string `json:"env,omitempty"` Args []string `json:"args,omitempty"` WorkingDir string `json:"workingDir,omitempty"` diff --git a/pkg/devspace/services/proxycommands/server.go b/pkg/devspace/services/proxycommands/server.go index a55c36efdb..e67bc83a77 100644 --- a/pkg/devspace/services/proxycommands/server.go +++ b/pkg/devspace/services/proxycommands/server.go @@ -79,7 +79,7 @@ type Server struct { } func (s *Server) handler(sess ssh.Session) { - cmd, err := s.getCommand(sess) + cmd, payload, err := s.getCommand(sess) if err != nil { s.exitWithError(sess, errors.Wrap(err, "construct command")) return @@ -98,9 +98,19 @@ func (s *Server) handler(sess ssh.Session) { } // start shell session - ptyReq, winCh, isPty := sess.Pty() - if isPty && runtime.GOOS != "windows" { - err = sshhelper.HandlePTY(sess, ptyReq, winCh, cmd, func(reader io.Reader) io.Reader { + if payload.TTY && runtime.GOOS != "windows" { + winSizeChan := make(chan ssh.Window, 1) + winSizeChan <- ssh.Window{ + Width: payload.Width, + Height: payload.Height, + } + err = sshhelper.HandlePTY(sess, ssh.Pty{ + Term: "xterm", + Window: ssh.Window{ + Width: payload.Width, + Height: payload.Height, + }, + }, winSizeChan, cmd, func(reader io.Reader) io.Reader { return reader }) } else { @@ -113,19 +123,19 @@ func (s *Server) handler(sess ssh.Session) { s.exitWithError(sess, err) } -func (s *Server) getCommand(sess ssh.Session) (*exec.Cmd, error) { +func (s *Server) getCommand(sess ssh.Session) (*exec.Cmd, *types.ProxyCommand, error) { var cmd *exec.Cmd rawCommand := sess.RawCommand() if len(rawCommand) == 0 { - return nil, fmt.Errorf("command required") + return nil, nil, fmt.Errorf("command required") } command := &types.ProxyCommand{} err := json.Unmarshal([]byte(rawCommand), &command) if err != nil { - return nil, fmt.Errorf("parse command: %v", err) + return nil, nil, fmt.Errorf("parse command: %v", err) } else if len(command.Args) == 0 { - return nil, fmt.Errorf("command is empty") + return nil, nil, fmt.Errorf("command is empty") } var reverseCommand *latest.ProxyCommand @@ -136,7 +146,7 @@ func (s *Server) getCommand(sess ssh.Session) (*exec.Cmd, error) { } } if reverseCommand == nil { - return nil, fmt.Errorf("command not allowed") + return nil, nil, fmt.Errorf("command not allowed") } c := reverseCommand.Command @@ -168,7 +178,7 @@ func (s *Server) getCommand(sess ssh.Session) (*exec.Cmd, error) { s.log.Debugf("run command '%s %s' locally", c, strings.Join(args, " ")) cmd.Env = append(cmd.Env, command.Env...) cmd.Env = append(cmd.Env, os.Environ()...) - return cmd, nil + return cmd, command, nil } func (s *Server) transformPath(originalPath string) string { From 1358467ad49f77b47c3689e18a09b215c0e97aee Mon Sep 17 00:00:00 2001 From: Fabian Kramm Date: Thu, 31 Mar 2022 13:29:44 +0200 Subject: [PATCH 3/3] refactor: add executing shell to proxy command --- helper/cmd/proxy_commands/configure.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/helper/cmd/proxy_commands/configure.go b/helper/cmd/proxy_commands/configure.go index eeb31d2a8b..63a4402620 100644 --- a/helper/cmd/proxy_commands/configure.go +++ b/helper/cmd/proxy_commands/configure.go @@ -55,7 +55,8 @@ func (cmd *ConfigureCmd) Run(_ *cobra.Command, _ []string) error { // first configure the commands for _, c := range cmd.Commands { filePath := "/usr/local/bin/" + c - executeCommand := fmt.Sprintf("/tmp/devspacehelper proxy-commands run %s $@", c) + executeCommand := fmt.Sprintf(`#!/bin/sh +/tmp/devspacehelper proxy-commands run %s $@`, c) err := ioutil.WriteFile(filePath, []byte(executeCommand), 0777) if err != nil { return fmt.Errorf("error writing command '%s': %v", filePath, err)