Merge branch '17170-container-shell'
[arvados.git] / lib / crunchrun / container_gateway.go
index 92c11383f42dbb732dacc740a68a55355556f799..1116c4bb1285d11ae39b4e194f21df6fd01c5d24 100644 (file)
@@ -5,8 +5,11 @@
 package crunchrun
 
 import (
+       "crypto/hmac"
        "crypto/rand"
        "crypto/rsa"
+       "crypto/sha256"
+       "crypto/tls"
        "fmt"
        "io"
        "net"
@@ -16,17 +19,32 @@ import (
        "sync"
        "syscall"
 
+       "git.arvados.org/arvados.git/lib/selfsigned"
        "git.arvados.org/arvados.git/sdk/go/httpserver"
        "github.com/creack/pty"
        "github.com/google/shlex"
        "golang.org/x/crypto/ssh"
 )
 
+type Gateway struct {
+       DockerContainerID *string
+       ContainerUUID     string
+       Address           string // listen host:port; if port=0, Start() will change it to the selected port
+       AuthSecret        string
+       Log               interface {
+               Printf(fmt string, args ...interface{})
+       }
+
+       sshConfig   ssh.ServerConfig
+       requestAuth string
+       respondAuth string
+}
+
 // startGatewayServer starts an http server that allows authenticated
 // clients to open an interactive "docker exec" session and (in
 // future) connect to tcp ports inside the docker container.
-func (runner *ContainerRunner) startGatewayServer() error {
-       runner.gatewaySSHConfig = &ssh.ServerConfig{
+func (gw *Gateway) Start() error {
+       gw.sshConfig = ssh.ServerConfig{
                NoClientAuth: true,
                PasswordCallback: func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
                        if c.User() == "_" {
@@ -47,7 +65,7 @@ func (runner *ContainerRunner) startGatewayServer() error {
                        }
                },
        }
-       pvt, err := rsa.GenerateKey(rand.Reader, 4096)
+       pvt, err := rsa.GenerateKey(rand.Reader, 2048)
        if err != nil {
                return err
        }
@@ -59,20 +77,34 @@ func (runner *ContainerRunner) startGatewayServer() error {
        if err != nil {
                return err
        }
-       runner.gatewaySSHConfig.AddHostKey(signer)
+       gw.sshConfig.AddHostKey(signer)
 
-       // GatewayAddress (provided by arvados-dispatch-cloud) is
+       // Address (typically provided by arvados-dispatch-cloud) is
        // HOST:PORT where HOST is our IP address or hostname as seen
        // from arvados-controller, and PORT is either the desired
-       // port where we should run our gateway server, or "0" if
-       // we should choose an available port.
-       host, port, err := net.SplitHostPort(os.Getenv("GatewayAddress"))
+       // port where we should run our gateway server, or "0" if we
+       // should choose an available port.
+       host, port, err := net.SplitHostPort(gw.Address)
+       if err != nil {
+               return err
+       }
+       cert, err := selfsigned.CertGenerator{}.Generate()
        if err != nil {
                return err
        }
+       h := hmac.New(sha256.New, []byte(gw.AuthSecret))
+       h.Write(cert.Certificate[0])
+       gw.requestAuth = fmt.Sprintf("%x", h.Sum(nil))
+       h.Reset()
+       h.Write([]byte(gw.requestAuth))
+       gw.respondAuth = fmt.Sprintf("%x", h.Sum(nil))
+
        srv := &httpserver.Server{
                Server: http.Server{
-                       Handler: http.HandlerFunc(runner.handleSSH),
+                       Handler: http.HandlerFunc(gw.handleSSH),
+                       TLSConfig: &tls.Config{
+                               Certificates: []tls.Certificate{cert},
+                       },
                },
                Addr: ":" + port,
        }
@@ -90,12 +122,13 @@ func (runner *ContainerRunner) startGatewayServer() error {
        // gateway_address to "HOST:PORT" where HOST is our
        // external hostname/IP as provided by arvados-dispatch-cloud,
        // and PORT is the port number we ended up listening on.
-       runner.gatewayAddress = net.JoinHostPort(host, port)
+       gw.Address = net.JoinHostPort(host, port)
        return nil
 }
 
-// handleSSH connects to an SSH server that runs commands as root in
-// the container. The tunnel itself can only be created by an
+// handleSSH connects to an SSH server that allows the caller to run
+// interactive commands as root (or any other desired user) inside the
+// container. The tunnel itself can only be created by an
 // authenticated caller, so the SSH server itself is wide open (any
 // password or key will be accepted).
 //
@@ -104,15 +137,17 @@ func (runner *ContainerRunner) startGatewayServer() error {
 // Connection: upgrade
 // Upgrade: ssh
 // X-Arvados-Target-Uuid: uuid of container
-// X-Arvados-Authorization: must match GatewayAuthSecret provided by
-// a-d-c (this prevents other containers and shell nodes from
-// connecting directly)
+// X-Arvados-Authorization: must match
+// hmac(AuthSecret,certfingerprint) (this prevents other containers
+// and shell nodes from connecting directly)
 //
-// Optional header:
+// Optional headers:
 //
-// X-Arvados-Detach-Keys: argument to "docker attach --detach-keys",
+// X-Arvados-Detach-Keys: argument to "docker exec --detach-keys",
 // e.g., "ctrl-p,ctrl-q"
-func (runner *ContainerRunner) handleSSH(w http.ResponseWriter, req *http.Request) {
+// X-Arvados-Login-Username: argument to "docker exec --user": account
+// used to run command(s) inside the container.
+func (gw *Gateway) handleSSH(w http.ResponseWriter, req *http.Request) {
        // In future we'll handle browser traffic too, but for now the
        // only traffic we expect is an SSH tunnel from
        // (*lib/controller/localdb.Conn)ContainerSSH()
@@ -120,11 +155,11 @@ func (runner *ContainerRunner) handleSSH(w http.ResponseWriter, req *http.Reques
                http.Error(w, "path not found", http.StatusNotFound)
                return
        }
-       if want := req.Header.Get("X-Arvados-Target-Uuid"); want != runner.Container.UUID {
-               http.Error(w, fmt.Sprintf("misdirected request: meant for %q but received by crunch-run %q", want, runner.Container.UUID), http.StatusBadGateway)
+       if want := req.Header.Get("X-Arvados-Target-Uuid"); want != gw.ContainerUUID {
+               http.Error(w, fmt.Sprintf("misdirected request: meant for %q but received by crunch-run %q", want, gw.ContainerUUID), http.StatusBadGateway)
                return
        }
-       if req.Header.Get("X-Arvados-Authorization") != runner.gatewayAuthSecret {
+       if req.Header.Get("X-Arvados-Authorization") != gw.requestAuth {
                http.Error(w, "bad X-Arvados-Authorization header", http.StatusUnauthorized)
                return
        }
@@ -146,33 +181,32 @@ func (runner *ContainerRunner) handleSSH(w http.ResponseWriter, req *http.Reques
        defer netconn.Close()
        w.Header().Set("Connection", "upgrade")
        w.Header().Set("Upgrade", "ssh")
+       w.Header().Set("X-Arvados-Authorization-Response", gw.respondAuth)
        netconn.Write([]byte("HTTP/1.1 101 Switching Protocols\r\n"))
        w.Header().Write(netconn)
        netconn.Write([]byte("\r\n"))
 
        ctx := req.Context()
 
-       conn, newchans, reqs, err := ssh.NewServerConn(netconn, runner.gatewaySSHConfig)
+       conn, newchans, reqs, err := ssh.NewServerConn(netconn, &gw.sshConfig)
        if err != nil {
-               runner.CrunchLog.Printf("ssh.NewServerConn: %s", err)
+               gw.Log.Printf("ssh.NewServerConn: %s", err)
                return
        }
        defer conn.Close()
        go ssh.DiscardRequests(reqs)
        for newch := range newchans {
                if newch.ChannelType() != "session" {
-                       newch.Reject(ssh.UnknownChannelType, "unknown channel type")
+                       newch.Reject(ssh.UnknownChannelType, fmt.Sprintf("unsupported channel type %q", newch.ChannelType()))
                        continue
                }
                ch, reqs, err := newch.Accept()
                if err != nil {
-                       runner.CrunchLog.Printf("accept channel: %s", err)
+                       gw.Log.Printf("accept channel: %s", err)
                        return
                }
                var pty0, tty0 *os.File
                go func() {
-                       defer pty0.Close()
-                       defer tty0.Close()
                        // Where to send errors/messages for the
                        // client to see
                        logw := io.Writer(ch.Stderr())
@@ -217,7 +251,7 @@ func (runner *ContainerRunner) handleSSH(w http.ResponseWriter, req *http.Reques
                                                        // Send our own debug messages to tty as well.
                                                        logw = tty0
                                                }
-                                               cmd.Args = append(cmd.Args, runner.ContainerID)
+                                               cmd.Args = append(cmd.Args, *gw.DockerContainerID)
                                                cmd.Args = append(cmd.Args, execargs...)
                                                cmd.SysProcAttr = &syscall.SysProcAttr{
                                                        Setctty: tty0 != nil,
@@ -225,15 +259,18 @@ func (runner *ContainerRunner) handleSSH(w http.ResponseWriter, req *http.Reques
                                                }
                                                cmd.Env = append(os.Environ(), termEnv...)
                                                err := cmd.Run()
-                                               errClose := ch.CloseWrite()
                                                var resp struct {
                                                        Status uint32
                                                }
-                                               if err, ok := err.(*exec.ExitError); ok {
-                                                       if status, ok := err.Sys().(syscall.WaitStatus); ok {
+                                               if exiterr, ok := err.(*exec.ExitError); ok {
+                                                       if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
                                                                resp.Status = uint32(status.ExitStatus())
                                                        }
+                                               } else if err != nil {
+                                                       // Propagate errors like `exec: "docker": executable file not found in $PATH`
+                                                       fmt.Fprintln(ch.Stderr(), err)
                                                }
+                                               errClose := ch.CloseWrite()
                                                if resp.Status == 0 && (err != nil || errClose != nil) {
                                                        resp.Status = 1
                                                }
@@ -247,6 +284,8 @@ func (runner *ContainerRunner) handleSSH(w http.ResponseWriter, req *http.Reques
                                                fmt.Fprintf(ch.Stderr(), "pty failed: %s"+eol, err)
                                                break
                                        }
+                                       defer p.Close()
+                                       defer t.Close()
                                        pty0, tty0 = p, t
                                        ok = true
                                        var payload struct {