Skip to content

Commit

Permalink
Handling custom environment variables when running a password hook sc…
Browse files Browse the repository at this point in the history
…ript (#57)
  • Loading branch information
moul committed Oct 12, 2015
1 parent d90c99e commit 5748a5b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ $ docker run --privileged -v /var/lib/docker:/var/lib/docker -it --rm -p 2222:22

### master (unreleased)

* Handling custom environment variables when running a password hook script ([#57](https://github.com/moul/ssh2docker/issues/57))
* Replacing "_" by "/" on default image name to handle ControlMaster on clients
* Support of `--banner` option ([#26](https://github.com/moul/ssh2docker/issues/26))
* Add a not-yet-implemented warning for exec ([#51](https://github.com/moul/ssh2docker/issues/51))
Expand Down
29 changes: 13 additions & 16 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ type Client struct {
Reqs <-chan *ssh.Request
Server *Server
Pty, Tty *os.File
Env Environment
Config *ClientConfig
ClientID string
}

type ClientConfig struct {
ImageName string `json:"image-name",omitempty`
RemoteUser string `json:"remote-user",omitempty`
Allowed bool `json:"allowed",omitempty`
IsLocal bool `json:"is_local",omitempty`
ImageName string `json:"image-name",omitempty`
RemoteUser string `json:"remote-user",omitempty`
Allowed bool `json:"allowed",omitempty`
Env Environment `json:"env",omitempty`
IsLocal bool `json:"is_local",omitempty`
}

// NewClient initializes a new client
Expand All @@ -48,25 +48,22 @@ func NewClient(conn *ssh.ServerConn, chans <-chan ssh.NewChannel, reqs <-chan *s
Chans: chans,
Reqs: reqs,
Server: server,
Env: Environment{
"TERM": os.Getenv("TERM"),
"DOCKER_HOST": os.Getenv("DOCKER_HOST"),
"DOCKER_CERT_PATH": os.Getenv("DOCKER_CERT_PATH"),
"DOCKER_TLS_VERIFY": os.Getenv("DOCKER_TLS_VERIFY"),
},

// Default ClientConfig, will be overwritten if a hook is used
Config: &ClientConfig{
ImageName: strings.Replace(conn.User(), "_", "/", -1),
RemoteUser: "anonymous",
Env: Environment{},
},
}

if server.LocalUser != "" {
client.Config.IsLocal = client.Config.ImageName == server.LocalUser
}

server.ClientConfigs[client.ClientID] = client.Config
if _, found := server.ClientConfigs[client.ClientID]; !found {
server.ClientConfigs[client.ClientID] = client.Config
}

clientCounter++

Expand Down Expand Up @@ -174,7 +171,7 @@ func (c *Client) HandleChannelRequests(channel ssh.Channel, requests <-chan *ssh
args = append(args, c.Config.ImageName, c.Server.DefaultShell)
logrus.Debugf("Executing 'docker %s'", strings.Join(args, " "))
cmd = exec.Command("docker", args...)
cmd.Env = c.Env.List()
cmd.Env = c.Config.Env.List()
}
}

Expand Down Expand Up @@ -233,10 +230,10 @@ func (c *Client) HandleChannelRequests(channel ssh.Channel, requests <-chan *ssh
case "pty-req":
ok = true
termLen := req.Payload[3]
c.Env["TERM"] = string(req.Payload[4 : termLen+4])
c.Config.Env["TERM"] = string(req.Payload[4 : termLen+4])
w, h := parseDims(req.Payload[termLen+4:])
SetWinsize(c.Pty.Fd(), w, h)
logrus.Debugf("HandleChannelRequests.req pty-req: TERM=%q w=%q h=%q", c.Env["TERM"], int(w), int(h))
logrus.Debugf("HandleChannelRequests.req pty-req: TERM=%q w=%q h=%q", c.Config.Env["TERM"], int(w), int(h))

case "window-change":
w, h := parseDims(req.Payload)
Expand All @@ -249,7 +246,7 @@ func (c *Client) HandleChannelRequests(channel ssh.Channel, requests <-chan *ssh
valueLen := req.Payload[keyLen+7]
value := string(req.Payload[keyLen+8 : keyLen+8+valueLen])
logrus.Debugf("HandleChannelRequets.req 'env': %s=%q", key, value)
c.Env[key] = value
c.Config.Env[key] = value

default:
logrus.Debugf("Unhandled request type: %q: %v", req.Type, req)
Expand Down
15 changes: 14 additions & 1 deletion environment.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package ssh2docker

import "fmt"
import (
"fmt"
"os"
)

type Environment map[string]string

var defauldEnvVars = []string{"TERM", "DOCKER_HOST", "DOCKER_CERT_PATH", "DOCKER_TLS_VERIFY"}

func (e *Environment) List() []string {
list := []string{}
for k, v := range *e {
Expand All @@ -14,3 +19,11 @@ func (e *Environment) List() []string {
}
return list
}

func (e *Environment) ApplyDefaults() {
for _, name := range defauldEnvVars {
if _, found := (*e)[name]; !found {
(*e)[name] = os.Getenv(name)
}
}
}
1 change: 1 addition & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (s *Server) Handle(netConn net.Conn) error {
}
client := NewClient(conn, chans, reqs, s)
client.Config = s.ClientConfigs[conn.RemoteAddr().String()]
client.Config.Env.ApplyDefaults()

// Handle requests
if err = client.HandleRequests(); err != nil {
Expand Down

0 comments on commit 5748a5b

Please sign in to comment.