Skip to content

Commit

Permalink
Merge pull request #2018 from FabianKramm/master
Browse files Browse the repository at this point in the history
fix: set session env via command
  • Loading branch information
FabianKramm authored Mar 31, 2022
2 parents 34d32b8 + 1358467 commit 1882ba3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 36 deletions.
3 changes: 2 additions & 1 deletion helper/cmd/proxy_commands/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 12 additions & 24 deletions helper/cmd/proxy_commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"golang.org/x/crypto/ssh"
"io/ioutil"
"os"
"strings"
)

// RunCmd holds the ssh cmd flags
Expand Down Expand Up @@ -59,36 +58,20 @@ 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
}
// check if we should use a pty#
var (
width = 0
height = 0
)

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 {
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: 14400,
ssh.TTY_OP_OSPEED: 14400,
})
if err != nil {
return errors.Wrap(err, "request pty")
}

// TODO: terminal resize
width = int(winSize.Width)
height = int(winSize.Height)
}
}
}
Expand All @@ -101,6 +84,11 @@ 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,
}
Expand Down
5 changes: 5 additions & 0 deletions helper/types/proxy_command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
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"`
}
32 changes: 21 additions & 11 deletions pkg/devspace/services/proxycommands/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -166,9 +176,9 @@ 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
return cmd, command, nil
}

func (s *Server) transformPath(originalPath string) string {
Expand Down

0 comments on commit 1882ba3

Please sign in to comment.