1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
20 "git.arvados.org/arvados.git/lib/controller/rpc"
21 "git.arvados.org/arvados.git/sdk/go/arvados"
24 // shellCommand connects the terminal to an interactive shell on a
26 type shellCommand struct{}
28 func (shellCommand) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
29 f := flag.NewFlagSet(prog, flag.ContinueOnError)
32 _, prog := filepath.Split(prog)
33 fmt.Fprint(stderr, prog+`: open an interactive shell on a running container.
35 Usage: `+prog+` [options] [username@]container-uuid [ssh-options] [remote-command [args...]]
41 detachKeys := f.String("detach-keys", "ctrl-],ctrl-]", "set detach key sequence, as in docker-attach(1)")
44 fmt.Fprintln(stderr, err)
53 if !strings.Contains(target, "@") {
54 target = "root@" + target
56 sshargs := f.Args()[1:]
58 // Try setting up a tunnel, and exit right away if it
59 // fails. This tunnel won't get used -- we'll set up a new
60 // tunnel when running as SSH client's ProxyCommand child --
61 // but in most cases where the real tunnel setup would fail,
62 // we catch the problem earlier here. This makes it less
63 // likely that an error message about tunnel setup will get
64 // hidden behind noisy errors from SSH client like this:
66 // [useful tunnel setup error message here]
67 // kex_exchange_identification: Connection closed by remote host
68 // Connection closed by UNKNOWN port 65535
71 // In case our target is a container request, the probe also
72 // resolves it to a container, so we don't connect to two
73 // different containers in a race.
74 var probetarget bytes.Buffer
75 exitcode := connectSSHCommand{}.RunCommand(
76 "arvados-client connect-ssh",
77 []string{"-detach-keys=" + *detachKeys, "-probe-only=true", target},
78 &bytes.Buffer{}, &probetarget, stderr)
82 target = strings.Trim(probetarget.String(), "\n")
84 selfbin, err := os.Readlink("/proc/self/exe")
86 fmt.Fprintln(stderr, err)
89 sshargs = append([]string{
91 "-o", "ProxyCommand " + selfbin + " connect-ssh -detach-keys=" + shellescape(*detachKeys) + " " + shellescape(target),
92 "-o", "StrictHostKeyChecking no",
95 sshbin, err := exec.LookPath("ssh")
97 fmt.Fprintln(stderr, err)
100 err = syscall.Exec(sshbin, sshargs, os.Environ())
101 fmt.Fprintf(stderr, "exec(%q) failed: %s\n", sshbin, err)
105 // connectSSHCommand connects stdin/stdout to a container's gateway
106 // server (see lib/crunchrun/ssh.go).
108 // It is intended to be invoked with OpenSSH client's ProxyCommand
110 type connectSSHCommand struct{}
112 func (connectSSHCommand) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
113 f := flag.NewFlagSet(prog, flag.ContinueOnError)
116 _, prog := filepath.Split(prog)
117 fmt.Fprint(stderr, prog+`: connect to the gateway service for a running container.
119 NOTE: You almost certainly don't want to use this command directly. It
120 is meant to be used internally. Use "arvados-client shell" instead.
122 Usage: `+prog+` [options] [username@]container-uuid
128 probeOnly := f.Bool("probe-only", false, "do not transfer IO, just setup tunnel, print target UUID, and exit")
129 detachKeys := f.String("detach-keys", "", "set detach key sequence, as in docker-attach(1)")
130 if err := f.Parse(args); err != nil {
131 fmt.Fprintln(stderr, err)
133 } else if f.NArg() != 1 {
137 targetUUID := f.Args()[0]
138 loginUsername := "root"
139 if i := strings.Index(targetUUID, "@"); i >= 0 {
140 loginUsername = targetUUID[:i]
141 targetUUID = targetUUID[i+1:]
143 if os.Getenv("ARVADOS_API_HOST") == "" || os.Getenv("ARVADOS_API_TOKEN") == "" {
144 fmt.Fprintln(stderr, "fatal: ARVADOS_API_HOST and ARVADOS_API_TOKEN environment variables are not set")
147 insecure := os.Getenv("ARVADOS_API_HOST_INSECURE")
148 rpcconn := rpc.NewConn("",
151 Host: os.Getenv("ARVADOS_API_HOST"),
153 insecure == "1" || insecure == "yes" || insecure == "true",
154 func(context.Context) ([]string, error) {
155 return []string{os.Getenv("ARVADOS_API_TOKEN")}, nil
157 if strings.Contains(targetUUID, "-xvhdp-") {
158 crs, err := rpcconn.ContainerRequestList(context.TODO(), arvados.ListOptions{Limit: -1, Filters: []arvados.Filter{{"uuid", "=", targetUUID}}})
160 fmt.Fprintln(stderr, err)
163 if len(crs.Items) < 1 {
164 fmt.Fprintf(stderr, "container request %q not found\n", targetUUID)
168 if cr.ContainerUUID == "" {
169 fmt.Fprintf(stderr, "no container assigned, container request state is %s\n", strings.ToLower(string(cr.State)))
172 targetUUID = cr.ContainerUUID
173 fmt.Fprintln(stderr, "connecting to container", targetUUID)
174 } else if !strings.Contains(targetUUID, "-dz642-") {
175 fmt.Fprintf(stderr, "target UUID is not a container or container request UUID: %s\n", targetUUID)
178 sshconn, err := rpcconn.ContainerSSH(context.TODO(), arvados.ContainerSSHOptions{
180 DetachKeys: *detachKeys,
181 LoginUsername: loginUsername,
184 fmt.Fprintln(stderr, "error setting up tunnel:", err)
187 defer sshconn.Conn.Close()
190 fmt.Fprintln(stdout, targetUUID)
194 ctx, cancel := context.WithCancel(context.Background())
197 _, err := io.Copy(stdout, sshconn.Conn)
198 if err != nil && ctx.Err() == nil {
199 fmt.Fprintf(stderr, "receive: %v\n", err)
204 _, err := io.Copy(sshconn.Conn, stdin)
205 if err != nil && ctx.Err() == nil {
206 fmt.Fprintf(stderr, "send: %v\n", err)
213 func shellescape(s string) string {
214 return "'" + strings.Replace(s, "'", "'\\''", -1) + "'"