1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
22 "git.arvados.org/arvados.git/lib/selfsigned"
23 "git.arvados.org/arvados.git/sdk/go/httpserver"
24 "github.com/creack/pty"
25 "github.com/google/shlex"
26 "golang.org/x/crypto/ssh"
30 DockerContainerID *string
32 Address string // listen host:port; if port=0, Start() will change it to the selected port
35 Printf(fmt string, args ...interface{})
38 sshConfig ssh.ServerConfig
43 // startGatewayServer starts an http server that allows authenticated
44 // clients to open an interactive "docker exec" session and (in
45 // future) connect to tcp ports inside the docker container.
46 func (gw *Gateway) Start() error {
47 gw.sshConfig = ssh.ServerConfig{
49 PasswordCallback: func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
53 return nil, fmt.Errorf("cannot specify user %q via ssh client", c.User())
56 PublicKeyCallback: func(c ssh.ConnMetadata, pubKey ssh.PublicKey) (*ssh.Permissions, error) {
58 return &ssh.Permissions{
59 Extensions: map[string]string{
60 "pubkey-fp": ssh.FingerprintSHA256(pubKey),
64 return nil, fmt.Errorf("cannot specify user %q via ssh client", c.User())
68 pvt, err := rsa.GenerateKey(rand.Reader, 2048)
76 signer, err := ssh.NewSignerFromKey(pvt)
80 gw.sshConfig.AddHostKey(signer)
82 // Address (typically provided by arvados-dispatch-cloud) is
83 // HOST:PORT where HOST is our IP address or hostname as seen
84 // from arvados-controller, and PORT is either the desired
85 // port where we should run our gateway server, or "0" if we
86 // should choose an available port.
87 host, port, err := net.SplitHostPort(gw.Address)
91 cert, err := selfsigned.CertGenerator{}.Generate()
95 h := hmac.New(sha256.New, []byte(gw.AuthSecret))
96 h.Write(cert.Certificate[0])
97 gw.requestAuth = fmt.Sprintf("%x", h.Sum(nil))
99 h.Write([]byte(gw.requestAuth))
100 gw.respondAuth = fmt.Sprintf("%x", h.Sum(nil))
102 srv := &httpserver.Server{
104 Handler: http.HandlerFunc(gw.handleSSH),
105 TLSConfig: &tls.Config{
106 Certificates: []tls.Certificate{cert},
115 // Get the port number we are listening on (the port might be
116 // "0" or a port name, in which case this will be different).
117 _, port, err = net.SplitHostPort(srv.Addr)
121 // When changing state to Running, we will set
122 // gateway_address to "HOST:PORT" where HOST is our
123 // external hostname/IP as provided by arvados-dispatch-cloud,
124 // and PORT is the port number we ended up listening on.
125 gw.Address = net.JoinHostPort(host, port)
129 // handleSSH connects to an SSH server that allows the caller to run
130 // interactive commands as root (or any other desired user) inside the
131 // container. The tunnel itself can only be created by an
132 // authenticated caller, so the SSH server itself is wide open (any
133 // password or key will be accepted).
135 // Requests must have path "/ssh" and the following headers:
137 // Connection: upgrade
139 // X-Arvados-Target-Uuid: uuid of container
140 // X-Arvados-Authorization: must match
141 // hmac(AuthSecret,certfingerprint) (this prevents other containers
142 // and shell nodes from connecting directly)
146 // X-Arvados-Detach-Keys: argument to "docker exec --detach-keys",
147 // e.g., "ctrl-p,ctrl-q"
148 // X-Arvados-Login-Username: argument to "docker exec --user": account
149 // used to run command(s) inside the container.
150 func (gw *Gateway) handleSSH(w http.ResponseWriter, req *http.Request) {
151 // In future we'll handle browser traffic too, but for now the
152 // only traffic we expect is an SSH tunnel from
153 // (*lib/controller/localdb.Conn)ContainerSSH()
154 if req.Method != "GET" || req.Header.Get("Upgrade") != "ssh" {
155 http.Error(w, "path not found", http.StatusNotFound)
158 if want := req.Header.Get("X-Arvados-Target-Uuid"); want != gw.ContainerUUID {
159 http.Error(w, fmt.Sprintf("misdirected request: meant for %q but received by crunch-run %q", want, gw.ContainerUUID), http.StatusBadGateway)
162 if req.Header.Get("X-Arvados-Authorization") != gw.requestAuth {
163 http.Error(w, "bad X-Arvados-Authorization header", http.StatusUnauthorized)
166 detachKeys := req.Header.Get("X-Arvados-Detach-Keys")
167 username := req.Header.Get("X-Arvados-Login-Username")
171 hj, ok := w.(http.Hijacker)
173 http.Error(w, "ResponseWriter does not support connection upgrade", http.StatusInternalServerError)
176 netconn, _, err := hj.Hijack()
178 http.Error(w, err.Error(), http.StatusInternalServerError)
181 defer netconn.Close()
182 w.Header().Set("Connection", "upgrade")
183 w.Header().Set("Upgrade", "ssh")
184 w.Header().Set("X-Arvados-Authorization-Response", gw.respondAuth)
185 netconn.Write([]byte("HTTP/1.1 101 Switching Protocols\r\n"))
186 w.Header().Write(netconn)
187 netconn.Write([]byte("\r\n"))
191 conn, newchans, reqs, err := ssh.NewServerConn(netconn, &gw.sshConfig)
193 gw.Log.Printf("ssh.NewServerConn: %s", err)
197 go ssh.DiscardRequests(reqs)
198 for newch := range newchans {
199 if newch.ChannelType() != "session" {
200 newch.Reject(ssh.UnknownChannelType, fmt.Sprintf("unsupported channel type %q", newch.ChannelType()))
203 ch, reqs, err := newch.Accept()
205 gw.Log.Printf("accept channel: %s", err)
208 var pty0, tty0 *os.File
210 // Where to send errors/messages for the
212 logw := io.Writer(ch.Stderr())
213 // How to end lines when sending
214 // errors/messages to the client (changes to
215 // \r\n when using a pty)
217 // Env vars to add to child process
218 termEnv := []string(nil)
219 for req := range reqs {
222 case "shell", "exec":
227 ssh.Unmarshal(req.Payload, &payload)
228 execargs, err := shlex.Split(payload.Command)
230 fmt.Fprintf(logw, "error parsing supplied command: %s"+eol, err)
233 if len(execargs) == 0 {
234 execargs = []string{"/bin/bash", "-login"}
237 cmd := exec.CommandContext(ctx, "docker", "exec", "-i", "--detach-keys="+detachKeys, "--user="+username)
240 cmd.Stderr = ch.Stderr()
242 cmd.Args = append(cmd.Args, "-t")
246 var wg sync.WaitGroup
249 go func() { io.Copy(ch, pty0); wg.Done() }()
250 go func() { io.Copy(pty0, ch); wg.Done() }()
251 // Send our own debug messages to tty as well.
254 cmd.Args = append(cmd.Args, *gw.DockerContainerID)
255 cmd.Args = append(cmd.Args, execargs...)
256 cmd.SysProcAttr = &syscall.SysProcAttr{
257 Setctty: tty0 != nil,
260 cmd.Env = append(os.Environ(), termEnv...)
265 if exiterr, ok := err.(*exec.ExitError); ok {
266 if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
267 resp.Status = uint32(status.ExitStatus())
269 } else if err != nil {
270 // Propagate errors like `exec: "docker": executable file not found in $PATH`
271 fmt.Fprintln(ch.Stderr(), err)
273 errClose := ch.CloseWrite()
274 if resp.Status == 0 && (err != nil || errClose != nil) {
277 ch.SendRequest("exit-status", false, ssh.Marshal(&resp))
282 p, t, err := pty.Open()
284 fmt.Fprintf(ch.Stderr(), "pty failed: %s"+eol, err)
298 ssh.Unmarshal(req.Payload, &payload)
299 termEnv = []string{"TERM=" + payload.Term, "USE_TTY=1"}
300 err = pty.Setsize(pty0, &pty.Winsize{Rows: uint16(payload.Rows), Cols: uint16(payload.Cols), X: uint16(payload.X), Y: uint16(payload.Y)})
302 fmt.Fprintf(logw, "pty-req: setsize failed: %s"+eol, err)
304 case "window-change":
311 ssh.Unmarshal(req.Payload, &payload)
312 err := pty.Setsize(pty0, &pty.Winsize{Rows: uint16(payload.Rows), Cols: uint16(payload.Cols), X: uint16(payload.X), Y: uint16(payload.Y)})
314 fmt.Fprintf(logw, "window-change: setsize failed: %s"+eol, err)
319 // TODO: implement "env"
320 // requests by setting env
321 // vars in the docker-exec
322 // command (not docker-exec's
323 // own environment, which
324 // would be a gaping security
327 // fmt.Fprintf(logw, "declining %q req"+eol, req.Type)