1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
24 "git.arvados.org/arvados.git/lib/controller/rpc"
25 "git.arvados.org/arvados.git/lib/selfsigned"
26 "git.arvados.org/arvados.git/sdk/go/arvados"
27 "git.arvados.org/arvados.git/sdk/go/auth"
28 "git.arvados.org/arvados.git/sdk/go/ctxlog"
29 "git.arvados.org/arvados.git/sdk/go/httpserver"
30 "github.com/creack/pty"
31 "github.com/google/shlex"
32 "github.com/hashicorp/yamux"
33 "golang.org/x/crypto/ssh"
34 "golang.org/x/net/context"
37 type GatewayTarget interface {
38 // Command that will execute cmd inside the container
39 InjectCommand(ctx context.Context, detachKeys, username string, usingTTY bool, cmd []string) (*exec.Cmd, error)
41 // IP address inside container
42 IPAddress() (string, error)
45 type GatewayTargetStub struct{}
47 func (GatewayTargetStub) IPAddress() (string, error) { return "127.0.0.1", nil }
48 func (GatewayTargetStub) InjectCommand(ctx context.Context, detachKeys, username string, usingTTY bool, cmd []string) (*exec.Cmd, error) {
49 return exec.CommandContext(ctx, cmd[0], cmd[1:]...), nil
54 // Caller should set Address to "", or "host:0" or "host:port"
55 // where host is a known external IP address; port is a
56 // desired port number to listen on; and ":0" chooses an
57 // available dynamic port.
59 // If Address is "", Start() listens only on the loopback
60 // interface (and changes Address to "127.0.0.1:port").
61 // Otherwise it listens on all interfaces.
63 // If Address is "host:0", Start() updates Address to
69 Printf(fmt string, args ...interface{})
71 // If non-nil, set up a ContainerGatewayTunnel, so that the
72 // controller can connect to us even if our external IP
73 // address is unknown or not routable from controller.
74 ArvadosClient *arvados.Client
76 // When a tunnel is connected or reconnected, this func (if
77 // not nil) will be called with the InternalURL of the
78 // controller process at the other end of the tunnel.
79 UpdateTunnelURL func(url string)
81 sshConfig ssh.ServerConfig
86 // Start starts an http server that allows authenticated clients to open an
87 // interactive "docker exec" session and (in future) connect to tcp ports
88 // inside the docker container.
89 func (gw *Gateway) Start() error {
90 gw.sshConfig = ssh.ServerConfig{
92 PasswordCallback: func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
96 return nil, fmt.Errorf("cannot specify user %q via ssh client", c.User())
98 PublicKeyCallback: func(c ssh.ConnMetadata, pubKey ssh.PublicKey) (*ssh.Permissions, error) {
100 return &ssh.Permissions{
101 Extensions: map[string]string{
102 "pubkey-fp": ssh.FingerprintSHA256(pubKey),
106 return nil, fmt.Errorf("cannot specify user %q via ssh client", c.User())
109 pvt, err := rsa.GenerateKey(rand.Reader, 2048)
117 signer, err := ssh.NewSignerFromKey(pvt)
121 gw.sshConfig.AddHostKey(signer)
123 // Address (typically provided by arvados-dispatch-cloud) is
124 // HOST:PORT where HOST is our IP address or hostname as seen
125 // from arvados-controller, and PORT is either the desired
126 // port where we should run our gateway server, or "0" if we
127 // should choose an available port.
128 extAddr := gw.Address
129 // Generally we can't know which local interface corresponds
130 // to an externally reachable IP address, so if we expect to
131 // be reachable by external hosts, we listen on all
135 // If the dispatcher doesn't tell us our external IP
136 // address, controller will only be able to connect
137 // through the tunnel (see runTunnel), so our gateway
138 // server only needs to listen on the loopback
140 extAddr = "127.0.0.1:0"
141 listenHost = "127.0.0.1"
143 extHost, extPort, err := net.SplitHostPort(extAddr)
147 cert, err := selfsigned.CertGenerator{}.Generate()
151 h := hmac.New(sha256.New, []byte(gw.AuthSecret))
152 h.Write(cert.Certificate[0])
153 gw.requestAuth = fmt.Sprintf("%x", h.Sum(nil))
155 h.Write([]byte(gw.requestAuth))
156 gw.respondAuth = fmt.Sprintf("%x", h.Sum(nil))
158 srv := &httpserver.Server{
160 Handler: http.HandlerFunc(gw.handleSSH),
161 TLSConfig: &tls.Config{
162 Certificates: []tls.Certificate{cert},
165 Addr: net.JoinHostPort(listenHost, extPort),
173 gw.Log.Printf("gateway server stopped: %s", err)
175 // Get the port number we are listening on (extPort might be
176 // "0" or a port name, in which case this will be different).
177 _, listenPort, err := net.SplitHostPort(srv.Addr)
181 // When changing state to Running, the caller will want to set
182 // gateway_address to a "HOST:PORT" that, if controller
183 // connects to it, will reach this gateway server.
185 // The most likely thing to work is: HOST is our external
186 // hostname/IP as provided by the caller
187 // (arvados-dispatch-cloud) or 127.0.0.1 to indicate
188 // non-tunnel connections aren't available; and PORT is the
189 // port number we are listening on.
190 gw.Address = net.JoinHostPort(extHost, listenPort)
191 gw.Log.Printf("gateway server listening at %s", gw.Address)
192 if gw.ArvadosClient != nil {
193 go gw.maintainTunnel(gw.Address)
198 func (gw *Gateway) maintainTunnel(addr string) {
199 for ; ; time.Sleep(5 * time.Second) {
200 err := gw.runTunnel(addr)
201 gw.Log.Printf("runTunnel: %s", err)
205 // runTunnel connects to controller and sets up a tunnel through
206 // which controller can connect to the gateway server at the given
208 func (gw *Gateway) runTunnel(addr string) error {
209 ctx := auth.NewContext(context.Background(), auth.NewCredentials(gw.ArvadosClient.AuthToken))
210 arpc := rpc.NewConn("", &url.URL{Scheme: "https", Host: gw.ArvadosClient.APIHost}, gw.ArvadosClient.Insecure, rpc.PassthroughTokenProvider)
211 tun, err := arpc.ContainerGatewayTunnel(ctx, arvados.ContainerGatewayTunnelOptions{
212 UUID: gw.ContainerUUID,
213 AuthSecret: gw.AuthSecret,
216 return fmt.Errorf("error creating gateway tunnel: %s", err)
218 mux, err := yamux.Client(tun.Conn, nil)
220 return fmt.Errorf("error setting up mux client end: %s", err)
222 if url := tun.Header.Get("X-Arvados-Internal-Url"); url != "" && gw.UpdateTunnelURL != nil {
223 gw.UpdateTunnelURL(url)
226 muxconn, err := mux.AcceptStream()
230 gw.Log.Printf("tunnel connection %d started", muxconn.StreamID())
232 defer muxconn.Close()
233 gwconn, err := net.Dial("tcp", addr)
235 gw.Log.Printf("tunnel connection %d: error connecting to %s: %s", muxconn.StreamID(), addr, err)
239 var wg sync.WaitGroup
243 _, err := io.Copy(gwconn, muxconn)
245 gw.Log.Printf("tunnel connection %d: mux end: %s", muxconn.StreamID(), err)
251 _, err := io.Copy(muxconn, gwconn)
253 gw.Log.Printf("tunnel connection %d: gateway end: %s", muxconn.StreamID(), err)
258 gw.Log.Printf("tunnel connection %d finished", muxconn.StreamID())
263 // handleSSH connects to an SSH server that allows the caller to run
264 // interactive commands as root (or any other desired user) inside the
265 // container. The tunnel itself can only be created by an
266 // authenticated caller, so the SSH server itself is wide open (any
267 // password or key will be accepted).
269 // Requests must have path "/ssh" and the following headers:
271 // Connection: upgrade
273 // X-Arvados-Target-Uuid: uuid of container
274 // X-Arvados-Authorization: must match
275 // hmac(AuthSecret,certfingerprint) (this prevents other containers
276 // and shell nodes from connecting directly)
280 // X-Arvados-Detach-Keys: argument to "docker exec --detach-keys",
281 // e.g., "ctrl-p,ctrl-q"
282 // X-Arvados-Login-Username: argument to "docker exec --user": account
283 // used to run command(s) inside the container.
284 func (gw *Gateway) handleSSH(w http.ResponseWriter, req *http.Request) {
285 // In future we'll handle browser traffic too, but for now the
286 // only traffic we expect is an SSH tunnel from
287 // (*lib/controller/localdb.Conn)ContainerSSH()
288 if req.Method != "POST" || req.Header.Get("Upgrade") != "ssh" {
289 http.Error(w, "path not found", http.StatusNotFound)
293 if want := req.Form.Get("uuid"); want != gw.ContainerUUID {
294 http.Error(w, fmt.Sprintf("misdirected request: meant for %q but received by crunch-run %q", want, gw.ContainerUUID), http.StatusBadGateway)
297 if req.Header.Get("X-Arvados-Authorization") != gw.requestAuth {
298 http.Error(w, "bad X-Arvados-Authorization header", http.StatusUnauthorized)
301 detachKeys := req.Form.Get("detach_keys")
302 username := req.Form.Get("login_username")
306 hj, ok := w.(http.Hijacker)
308 http.Error(w, "ResponseWriter does not support connection upgrade", http.StatusInternalServerError)
311 netconn, _, err := hj.Hijack()
313 http.Error(w, err.Error(), http.StatusInternalServerError)
316 defer netconn.Close()
317 w.Header().Set("Connection", "upgrade")
318 w.Header().Set("Upgrade", "ssh")
319 w.Header().Set("X-Arvados-Authorization-Response", gw.respondAuth)
320 netconn.Write([]byte("HTTP/1.1 101 Switching Protocols\r\n"))
321 w.Header().Write(netconn)
322 netconn.Write([]byte("\r\n"))
326 conn, newchans, reqs, err := ssh.NewServerConn(netconn, &gw.sshConfig)
329 } else if err != nil {
330 gw.Log.Printf("ssh.NewServerConn: %s", err)
334 go ssh.DiscardRequests(reqs)
335 for newch := range newchans {
336 switch newch.ChannelType() {
338 go gw.handleDirectTCPIP(ctx, newch)
340 go gw.handleSession(ctx, newch, detachKeys, username)
342 go newch.Reject(ssh.UnknownChannelType, fmt.Sprintf("unsupported channel type %q", newch.ChannelType()))
347 func (gw *Gateway) handleDirectTCPIP(ctx context.Context, newch ssh.NewChannel) {
348 ch, reqs, err := newch.Accept()
350 gw.Log.Printf("accept direct-tcpip channel: %s", err)
354 go ssh.DiscardRequests(reqs)
356 // RFC 4254 7.2 (copy of channelOpenDirectMsg in
357 // golang.org/x/crypto/ssh)
364 err = ssh.Unmarshal(newch.ExtraData(), &msg)
366 fmt.Fprintf(ch.Stderr(), "unmarshal direct-tcpip extradata: %s\n", err)
370 case "localhost", "0.0.0.0", "127.0.0.1", "::1", "::":
372 fmt.Fprintf(ch.Stderr(), "cannot forward to ports on %q, only localhost\n", msg.Raddr)
376 dstaddr, err := gw.Target.IPAddress()
378 fmt.Fprintf(ch.Stderr(), "container has no IP address: %s\n", err)
380 } else if dstaddr == "" {
381 fmt.Fprintf(ch.Stderr(), "container has no IP address\n")
385 dst := net.JoinHostPort(dstaddr, fmt.Sprintf("%d", msg.Rport))
386 tcpconn, err := net.Dial("tcp", dst)
388 fmt.Fprintf(ch.Stderr(), "%s: %s\n", dst, err)
392 n, _ := io.Copy(ch, tcpconn)
393 ctxlog.FromContext(ctx).Debugf("tcpip: sent %d bytes\n", n)
396 n, _ := io.Copy(tcpconn, ch)
397 ctxlog.FromContext(ctx).Debugf("tcpip: received %d bytes\n", n)
400 func (gw *Gateway) handleSession(ctx context.Context, newch ssh.NewChannel, detachKeys, username string) {
401 ch, reqs, err := newch.Accept()
403 gw.Log.Printf("error accepting session channel: %s", err)
408 var pty0, tty0 *os.File
409 // Where to send errors/messages for the client to see
410 logw := io.Writer(ch.Stderr())
411 // How to end lines when sending errors/messages to the client
412 // (changes to \r\n when using a pty)
414 // Env vars to add to child process
415 termEnv := []string(nil)
418 wantClose := make(chan struct{})
422 case r, ok := <-reqs:
432 case "shell", "exec":
433 if started++; started != 1 {
434 // RFC 4254 6.5: "Only one of these
435 // requests can succeed per channel."
442 ssh.Unmarshal(req.Payload, &payload)
443 execargs, err := shlex.Split(payload.Command)
445 fmt.Fprintf(logw, "error parsing supplied command: %s"+eol, err)
448 if len(execargs) == 0 {
449 execargs = []string{"/bin/bash", "-login"}
456 ch.SendRequest("exit-status", false, ssh.Marshal(&resp))
460 cmd, err := gw.Target.InjectCommand(ctx, detachKeys, username, tty0 != nil, execargs)
462 fmt.Fprintln(ch.Stderr(), err)
473 // Send our own debug messages to tty as well.
476 // StdinPipe may seem
477 // superfluous here, but it's
478 // not: it causes cmd.Run() to
479 // return when the subprocess
480 // exits. Without it, Run()
481 // waits for stdin to close,
482 // which causes "ssh ... echo
483 // ok" (with the client's
484 // stdin connected to a
485 // terminal or something) to
487 stdin, err := cmd.StdinPipe()
489 fmt.Fprintln(ch.Stderr(), err)
499 cmd.Stderr = ch.Stderr()
501 cmd.SysProcAttr = &syscall.SysProcAttr{
502 Setctty: tty0 != nil,
505 cmd.Env = append(os.Environ(), termEnv...)
507 if exiterr, ok := err.(*exec.ExitError); ok {
508 if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
509 resp.Status = uint32(status.ExitStatus())
511 } else if err != nil {
512 // Propagate errors like `exec: "docker": executable file not found in $PATH`
513 fmt.Fprintln(ch.Stderr(), err)
515 errClose := ch.CloseWrite()
516 if resp.Status == 0 && (err != nil || errClose != nil) {
522 p, t, err := pty.Open()
524 fmt.Fprintf(ch.Stderr(), "pty failed: %s"+eol, err)
538 ssh.Unmarshal(req.Payload, &payload)
539 termEnv = []string{"TERM=" + payload.Term, "USE_TTY=1"}
540 err = pty.Setsize(pty0, &pty.Winsize{Rows: uint16(payload.Rows), Cols: uint16(payload.Cols), X: uint16(payload.X), Y: uint16(payload.Y)})
542 fmt.Fprintf(logw, "pty-req: setsize failed: %s"+eol, err)
544 case "window-change":
551 ssh.Unmarshal(req.Payload, &payload)
552 err := pty.Setsize(pty0, &pty.Winsize{Rows: uint16(payload.Rows), Cols: uint16(payload.Cols), X: uint16(payload.X), Y: uint16(payload.Y)})
554 fmt.Fprintf(logw, "window-change: setsize failed: %s"+eol, err)
559 // TODO: implement "env"
560 // requests by setting env
561 // vars in the docker-exec
562 // command (not docker-exec's
563 // own environment, which
564 // would be a gaping security
567 // fmt.Fprintf(logw, "declined request %q on ssh channel"+eol, req.Type)