Skip to content

Commit 1882ba3

Browse files
authored
Merge pull request #2018 from FabianKramm/master
fix: set session env via command
2 parents 34d32b8 + 1358467 commit 1882ba3

4 files changed

Lines changed: 40 additions & 36 deletions

File tree

helper/cmd/proxy_commands/configure.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ func (cmd *ConfigureCmd) Run(_ *cobra.Command, _ []string) error {
5555
// first configure the commands
5656
for _, c := range cmd.Commands {
5757
filePath := "/usr/local/bin/" + c
58-
executeCommand := fmt.Sprintf("/tmp/devspacehelper proxy-commands run %s $@", c)
58+
executeCommand := fmt.Sprintf(`#!/bin/sh
59+
/tmp/devspacehelper proxy-commands run %s $@`, c)
5960
err := ioutil.WriteFile(filePath, []byte(executeCommand), 0777)
6061
if err != nil {
6162
return fmt.Errorf("error writing command '%s': %v", filePath, err)

helper/cmd/proxy_commands/run.go

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"golang.org/x/crypto/ssh"
1111
"io/ioutil"
1212
"os"
13-
"strings"
1413
)
1514

1615
// RunCmd holds the ssh cmd flags
@@ -59,36 +58,20 @@ func (cmd *RunCmd) Run(_ *cobra.Command, args []string) error {
5958
}
6059
defer session.Close()
6160

62-
// set environment variables
63-
for _, v := range os.Environ() {
64-
splitted := strings.Split(v, "=")
65-
if len(splitted) < 2 {
66-
continue
67-
}
61+
// check if we should use a pty#
62+
var (
63+
width = 0
64+
height = 0
65+
)
6866

69-
err = session.Setenv(splitted[0], strings.Join(splitted[1:], "="))
70-
if err != nil {
71-
return errors.Wrap(err, "set session env")
72-
}
73-
}
74-
75-
// check if we should use a pty
7667
tty, t := terminal.SetupTTY(os.Stdin, os.Stdout)
7768
if tty {
7869
info, ok := term.GetFdInfo(t.In)
7970
if ok {
8071
winSize, err := term.GetWinsize(info)
8172
if err == nil {
82-
err = session.RequestPty("xterm", int(winSize.Height), int(winSize.Width), ssh.TerminalModes{
83-
ssh.ECHO: 0,
84-
ssh.TTY_OP_ISPEED: 14400,
85-
ssh.TTY_OP_OSPEED: 14400,
86-
})
87-
if err != nil {
88-
return errors.Wrap(err, "request pty")
89-
}
90-
91-
// TODO: terminal resize
73+
width = int(winSize.Width)
74+
height = int(winSize.Height)
9275
}
9376
}
9477
}
@@ -101,6 +84,11 @@ func (cmd *RunCmd) Run(_ *cobra.Command, args []string) error {
10184

10285
// marshal command and execute command
10386
proxyCommand := &types.ProxyCommand{
87+
TTY: tty,
88+
Width: width,
89+
Height: height,
90+
91+
Env: os.Environ(),
10492
Args: args,
10593
WorkingDir: currentWorkingDir,
10694
}

helper/types/proxy_command.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package types
22

33
type ProxyCommand struct {
4+
TTY bool `json:"tty,omitempty"`
5+
Height int `json:"height,omitempty"`
6+
Width int `json:"width,omitempty"`
7+
8+
Env []string `json:"env,omitempty"`
49
Args []string `json:"args,omitempty"`
510
WorkingDir string `json:"workingDir,omitempty"`
611
}

pkg/devspace/services/proxycommands/server.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ type Server struct {
7979
}
8080

8181
func (s *Server) handler(sess ssh.Session) {
82-
cmd, err := s.getCommand(sess)
82+
cmd, payload, err := s.getCommand(sess)
8383
if err != nil {
8484
s.exitWithError(sess, errors.Wrap(err, "construct command"))
8585
return
@@ -98,9 +98,19 @@ func (s *Server) handler(sess ssh.Session) {
9898
}
9999

100100
// start shell session
101-
ptyReq, winCh, isPty := sess.Pty()
102-
if isPty && runtime.GOOS != "windows" {
103-
err = sshhelper.HandlePTY(sess, ptyReq, winCh, cmd, func(reader io.Reader) io.Reader {
101+
if payload.TTY && runtime.GOOS != "windows" {
102+
winSizeChan := make(chan ssh.Window, 1)
103+
winSizeChan <- ssh.Window{
104+
Width: payload.Width,
105+
Height: payload.Height,
106+
}
107+
err = sshhelper.HandlePTY(sess, ssh.Pty{
108+
Term: "xterm",
109+
Window: ssh.Window{
110+
Width: payload.Width,
111+
Height: payload.Height,
112+
},
113+
}, winSizeChan, cmd, func(reader io.Reader) io.Reader {
104114
return reader
105115
})
106116
} else {
@@ -113,19 +123,19 @@ func (s *Server) handler(sess ssh.Session) {
113123
s.exitWithError(sess, err)
114124
}
115125

116-
func (s *Server) getCommand(sess ssh.Session) (*exec.Cmd, error) {
126+
func (s *Server) getCommand(sess ssh.Session) (*exec.Cmd, *types.ProxyCommand, error) {
117127
var cmd *exec.Cmd
118128
rawCommand := sess.RawCommand()
119129
if len(rawCommand) == 0 {
120-
return nil, fmt.Errorf("command required")
130+
return nil, nil, fmt.Errorf("command required")
121131
}
122132

123133
command := &types.ProxyCommand{}
124134
err := json.Unmarshal([]byte(rawCommand), &command)
125135
if err != nil {
126-
return nil, fmt.Errorf("parse command: %v", err)
136+
return nil, nil, fmt.Errorf("parse command: %v", err)
127137
} else if len(command.Args) == 0 {
128-
return nil, fmt.Errorf("command is empty")
138+
return nil, nil, fmt.Errorf("command is empty")
129139
}
130140

131141
var reverseCommand *latest.ProxyCommand
@@ -136,7 +146,7 @@ func (s *Server) getCommand(sess ssh.Session) (*exec.Cmd, error) {
136146
}
137147
}
138148
if reverseCommand == nil {
139-
return nil, fmt.Errorf("command not allowed")
149+
return nil, nil, fmt.Errorf("command not allowed")
140150
}
141151

142152
c := reverseCommand.Command
@@ -166,9 +176,9 @@ func (s *Server) getCommand(sess ssh.Session) (*exec.Cmd, error) {
166176
}
167177

168178
s.log.Debugf("run command '%s %s' locally", c, strings.Join(args, " "))
169-
cmd.Env = append(cmd.Env, sess.Environ()...)
179+
cmd.Env = append(cmd.Env, command.Env...)
170180
cmd.Env = append(cmd.Env, os.Environ()...)
171-
return cmd, nil
181+
return cmd, command, nil
172182
}
173183

174184
func (s *Server) transformPath(originalPath string) string {

0 commit comments

Comments
 (0)