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/auth"
22 "git.arvados.org/arvados.git/sdk/go/ctxlog"
23 "git.arvados.org/arvados.git/sdk/go/httpserver"
26 // ContainerSSH returns a connection to the SSH server in the
27 // appropriate crunch-run process on the worker node where the
28 // specified container is running.
30 // If the returned error is nil, the caller is responsible for closing
32 func (conn *Conn) ContainerSSH(ctx context.Context, opts arvados.ContainerSSHOptions) (sshconn arvados.ContainerSSHConnection, err error) {
33 user, err := conn.railsProxy.UserGetCurrent(ctx, arvados.GetOptions{})
37 ctr, err := conn.railsProxy.ContainerGet(ctx, arvados.GetOptions{UUID: opts.UUID})
42 ctxRoot := auth.NewContext(ctx, &auth.Credentials{Tokens: []string{conn.cluster.SystemRootToken}})
43 crs, err := conn.railsProxy.ContainerRequestList(ctxRoot, arvados.ListOptions{Limit: -1, Filters: []arvados.Filter{{"container_uuid", "=", opts.UUID}}})
47 for _, cr := range crs.Items {
48 if cr.ModifiedByUserUUID != user.UUID {
49 err = httpserver.ErrorWithStatus(errors.New("permission denied: container is associated with requests submitted by other users"), http.StatusForbidden)
53 if crs.ItemsAvailable != len(crs.Items) {
54 err = httpserver.ErrorWithStatus(errors.New("incomplete response while checking permission"), http.StatusInternalServerError)
58 ctr, err := conn.railsProxy.ContainerGet(ctx, arvados.GetOptions{UUID: opts.UUID})
62 if ctr.GatewayAddress == "" || ctr.State != arvados.ContainerStateRunning {
63 err = httpserver.ErrorWithStatus(fmt.Errorf("gateway is not available, container is %s", strings.ToLower(string(ctr.State))), http.StatusBadGateway)
66 // crunch-run uses a self-signed / unverifiable TLS
67 // certificate, so we use the following scheme to ensure we're
68 // not talking to a MITM.
70 // 1. Compute ctrKey = HMAC-SHA256(sysRootToken,ctrUUID) --
71 // this will be the same ctrKey that a-d-c supplied to
72 // crunch-run in the GatewayAuthSecret env var.
74 // 2. Compute requestAuth = HMAC-SHA256(ctrKey,serverCert) and
75 // send it to crunch-run as the X-Arvados-Authorization
76 // header, proving that we know ctrKey. (Note a MITM cannot
77 // replay the proof to a real crunch-run server, because the
78 // real crunch-run server would have a different cert.)
80 // 3. Compute respondAuth = HMAC-SHA256(ctrKey,requestAuth)
81 // and ensure the server returns it in the
82 // X-Arvados-Authorization-Response header, proving that the
83 // server knows ctrKey.
84 var requestAuth, respondAuth string
85 netconn, err := tls.Dial("tcp", ctr.GatewayAddress, &tls.Config{
86 InsecureSkipVerify: true,
87 VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
88 if len(rawCerts) == 0 {
89 return errors.New("no certificate received, cannot compute authorization header")
91 h := hmac.New(sha256.New, []byte(conn.cluster.SystemRootToken))
92 fmt.Fprint(h, opts.UUID)
93 authKey := fmt.Sprintf("%x", h.Sum(nil))
94 h = hmac.New(sha256.New, []byte(authKey))
96 requestAuth = fmt.Sprintf("%x", h.Sum(nil))
98 h.Write([]byte(requestAuth))
99 respondAuth = fmt.Sprintf("%x", h.Sum(nil))
106 if respondAuth == "" {
107 err = httpserver.ErrorWithStatus(errors.New("BUG: no respondAuth"), http.StatusInternalServerError)
110 bufr := bufio.NewReader(netconn)
111 bufw := bufio.NewWriter(netconn)
115 Host: ctr.GatewayAddress,
118 bufw.WriteString("GET " + u.String() + " HTTP/1.1\r\n")
119 bufw.WriteString("Host: " + u.Host + "\r\n")
120 bufw.WriteString("Upgrade: ssh\r\n")
121 bufw.WriteString("X-Arvados-Target-Uuid: " + opts.UUID + "\r\n")
122 bufw.WriteString("X-Arvados-Authorization: " + requestAuth + "\r\n")
123 bufw.WriteString("X-Arvados-Detach-Keys: " + opts.DetachKeys + "\r\n")
124 bufw.WriteString("X-Arvados-Login-Username: " + opts.LoginUsername + "\r\n")
125 bufw.WriteString("\r\n")
127 resp, err := http.ReadResponse(bufr, &http.Request{Method: "GET"})
129 err = httpserver.ErrorWithStatus(fmt.Errorf("error reading http response from gateway: %w", err), http.StatusBadGateway)
133 if resp.Header.Get("X-Arvados-Authorization-Response") != respondAuth {
134 err = httpserver.ErrorWithStatus(errors.New("bad X-Arvados-Authorization-Response header"), http.StatusBadGateway)
138 if strings.ToLower(resp.Header.Get("Upgrade")) != "ssh" ||
139 strings.ToLower(resp.Header.Get("Connection")) != "upgrade" {
140 err = httpserver.ErrorWithStatus(errors.New("bad upgrade"), http.StatusBadGateway)
144 sshconn.Conn = netconn
145 sshconn.Bufrw = &bufio.ReadWriter{Reader: bufr, Writer: bufw}
146 sshconn.Logger = ctxlog.FromContext(ctx)