1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
20 "git.arvados.org/arvados.git/sdk/go/arvados"
21 "git.arvados.org/arvados.git/sdk/go/ctxlog"
22 "git.arvados.org/arvados.git/sdk/go/httpserver"
25 // ContainerSSH returns a connection to the SSH server in the
26 // appropriate crunch-run process on the worker node where the
27 // specified container is running.
29 // If the returned error is nil, the caller is responsible for closing
31 func (conn *Conn) ContainerSSH(ctx context.Context, opts arvados.ContainerSSHOptions) (sshconn arvados.ContainerSSHConnection, err error) {
32 ctr, err := conn.railsProxy.ContainerGet(ctx, arvados.GetOptions{UUID: opts.UUID})
36 if ctr.GatewayAddress == "" || ctr.State != arvados.ContainerStateRunning {
37 err = httpserver.ErrorWithStatus(fmt.Errorf("gateway is not available, container is %s", strings.ToLower(string(ctr.State))), http.StatusBadGateway)
40 // crunch-run uses a self-signed / unverifiable TLS
41 // certificate, so we use the following scheme to ensure we're
42 // not talking to a MITM.
44 // 1. Compute ctrKey = HMAC-SHA256(sysRootToken,ctrUUID) --
45 // this will be the same ctrKey that a-d-c supplied to
46 // crunch-run in the GatewayAuthSecret env var.
48 // 2. Compute requestAuth = HMAC-SHA256(ctrKey,serverCert) and
49 // send it to crunch-run as the X-Arvados-Authorization
50 // header, proving that we know ctrKey. (Note a MITM cannot
51 // replay the proof to a real crunch-run server, because the
52 // real crunch-run server would have a different cert.)
54 // 3. Compute respondAuth = HMAC-SHA256(ctrKey,requestAuth)
55 // and ensure the server returns it in the
56 // X-Arvados-Authorization-Response header, proving that the
57 // server knows ctrKey.
58 var requestAuth, respondAuth string
59 netconn, err := tls.Dial("tcp", ctr.GatewayAddress, &tls.Config{
60 InsecureSkipVerify: true,
61 VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
62 if len(rawCerts) == 0 {
63 return errors.New("no certificate received, cannot compute authorization header")
65 h := hmac.New(sha256.New, []byte(conn.cluster.SystemRootToken))
66 fmt.Fprint(h, opts.UUID)
67 authKey := fmt.Sprintf("%x", h.Sum(nil))
68 h = hmac.New(sha256.New, []byte(authKey))
70 requestAuth = fmt.Sprintf("%x", h.Sum(nil))
72 h.Write([]byte(requestAuth))
73 respondAuth = fmt.Sprintf("%x", h.Sum(nil))
80 if respondAuth == "" {
81 err = httpserver.ErrorWithStatus(errors.New("BUG: no respondAuth"), http.StatusInternalServerError)
84 bufr := bufio.NewReader(netconn)
85 bufw := bufio.NewWriter(netconn)
89 Host: ctr.GatewayAddress,
92 bufw.WriteString("GET " + u.String() + " HTTP/1.1\r\n")
93 bufw.WriteString("Host: " + u.Host + "\r\n")
94 bufw.WriteString("Upgrade: ssh\r\n")
95 bufw.WriteString("X-Arvados-Target-Uuid: " + opts.UUID + "\r\n")
96 bufw.WriteString("X-Arvados-Authorization: " + requestAuth + "\r\n")
97 bufw.WriteString("X-Arvados-Detach-Keys: " + opts.DetachKeys + "\r\n")
98 bufw.WriteString("X-Arvados-Login-Username: " + opts.LoginUsername + "\r\n")
99 bufw.WriteString("\r\n")
101 resp, err := http.ReadResponse(bufr, &http.Request{Method: "GET"})
103 err = httpserver.ErrorWithStatus(fmt.Errorf("error reading http response from gateway: %w", err), http.StatusBadGateway)
107 if resp.Header.Get("X-Arvados-Authorization-Response") != respondAuth {
108 err = httpserver.ErrorWithStatus(errors.New("bad X-Arvados-Authorization-Response header"), http.StatusBadGateway)
112 if strings.ToLower(resp.Header.Get("Upgrade")) != "ssh" ||
113 strings.ToLower(resp.Header.Get("Connection")) != "upgrade" {
114 err = httpserver.ErrorWithStatus(errors.New("bad upgrade"), http.StatusBadGateway)
118 sshconn.Conn = netconn
119 sshconn.Bufrw = &bufio.ReadWriter{Reader: bufr, Writer: bufw}
120 sshconn.Logger = ctxlog.FromContext(ctx)