Skip to content

Commit 9434fda

Browse files
committed
Connect sandbox SSH through authenticated WebSocket port forwarding
1 parent 36dc63c commit 9434fda

30 files changed

Lines changed: 2981 additions & 405 deletions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Use local SSH keys and authenticated WebSocket port forwarding for `databricks sandbox ssh`, including sandboxes created through the public API. ([#7589](https://github.com/databricks/cli/pull/7589))

‎NOTICE‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ This Software includes software developed at Databricks (https://www.databricks.
44

55
This Software contains code from the following open source projects, licensed under the Apache 2.0 license:
66

7+
grpc/grpc-go - https://github.com/grpc/grpc-go
8+
Copyright The gRPC Authors
9+
License - https://github.com/grpc/grpc-go/blob/master/LICENSE
10+
711
spf13/cobra - https://github.com/spf13/cobra
812
Copyright cobra authors
913
License - https://github.com/spf13/cobra/blob/main/LICENSE.txt
@@ -33,6 +37,10 @@ License - https://github.com/gorilla/websocket/blob/main/LICENSE
3337

3438
This Software contains code from the following open source projects, licensed under the BSD (3-clause) license:
3539

40+
protocolbuffers/protobuf-go - https://github.com/protocolbuffers/protobuf-go
41+
Copyright The Go Authors
42+
License - https://github.com/protocolbuffers/protobuf-go/blob/master/LICENSE
43+
3644
spf13/pflag - https://github.com/spf13/pflag
3745
Copyright (c) 2012 Alex Ogier. All rights reserved.
3846
Copyright (c) 2012 The Go Authors. All rights reserved.

‎cmd/sandbox/sandbox.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ tooling (Python, Node.js, Rust, Databricks CLI) and persistent storage.
1818
1919
Getting started:
2020
databricks auth login --host https://... # authenticate to a Databricks workspace
21-
databricks sandbox register # generate and register an SSH key
2221
databricks sandbox create # provision a sandbox (becomes your default)
2322
databricks sandbox ssh # SSH to your default sandbox
2423

‎cmd/sandbox/server.go‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package sandbox
22

33
import (
4+
"bytes"
45
"errors"
56
"fmt"
7+
"io"
68
"net"
9+
"os"
10+
"runtime"
711
"time"
812

913
sshtunnel "github.com/databricks/cli/experimental/ssh/sandbox"
@@ -28,9 +32,15 @@ func newServerCommand() *cobra.Command {
2832
if err != nil {
2933
return fmt.Errorf("listen for SSH WebSockets: %w", err)
3034
}
35+
output, err := openServerOutput(cmd.OutOrStdout())
36+
if err != nil {
37+
listener.Close()
38+
return fmt.Errorf("open command output: %w", err)
39+
}
40+
defer output.Close()
3141
return sshtunnel.Serve(cmd.Context(), listener, sshtunnel.ServerOptions{
32-
SSHD: sshd, Config: config, IdleTimeout: idleTimeout,
33-
}, cmd.OutOrStdout())
42+
SSHD: sshd, Config: config, IdleTimeout: idleTimeout, OutputHeartbeat: 30 * time.Second,
43+
}, output)
3444
},
3545
}
3646
cmd.Flags().StringVar(&config, "sshd-config", "", "Prepared sshd configuration file")
@@ -40,3 +50,27 @@ func newServerCommand() *cobra.Command {
4050
cmd.Flags().DurationVar(&idleTimeout, "idle-timeout", time.Minute, "Stop after this duration without an SSH connection")
4151
return cmd
4252
}
53+
54+
func openServerOutput(output io.Writer) (io.WriteCloser, error) {
55+
if _, ok := output.(*bytes.Buffer); ok || output == io.Discard {
56+
return serverOutputWriter{output}, nil
57+
}
58+
file, ok := output.(*os.File)
59+
if !ok || runtime.GOOS != "linux" {
60+
return nil, errors.New("server output must be a Linux command-execution pipe")
61+
}
62+
info, err := file.Stat()
63+
if err != nil {
64+
return nil, err
65+
}
66+
if info.Mode()&os.ModeNamedPipe == 0 {
67+
return nil, errors.New("server output must be a Linux command-execution pipe")
68+
}
69+
// Reopening inherited stdout registers its pipe with Go's poller, so
70+
// Close interrupts blocked writes without changing the inherited fd.
71+
return os.OpenFile(fmt.Sprintf("/proc/self/fd/%d", file.Fd()), os.O_WRONLY, 0)
72+
}
73+
74+
type serverOutputWriter struct{ io.Writer }
75+
76+
func (serverOutputWriter) Close() error { return nil }

‎cmd/sandbox/server_test.go‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,65 @@
11
package sandbox
22

33
import (
4+
"bufio"
45
"bytes"
6+
"context"
57
"encoding/json"
8+
"fmt"
9+
"io"
610
"net"
711
"os"
12+
"os/exec"
813
"path/filepath"
914
"runtime"
1015
"strconv"
1116
"testing"
17+
"time"
1218

1319
"github.com/stretchr/testify/assert"
1420
"github.com/stretchr/testify/require"
1521
)
1622

23+
func TestServerOutputInterruptsInheritedStdout(t *testing.T) {
24+
if runtime.GOOS != "linux" {
25+
t.Skip("Linux sandbox command output uses /proc/self/fd")
26+
}
27+
const marker = "server output write interrupted\n"
28+
if os.Getenv("DATABRICKS_TEST_SERVER_OUTPUT") == "1" {
29+
output, err := openServerOutput(os.Stdout)
30+
require.NoError(t, err)
31+
defer output.Close()
32+
file := output.(*os.File)
33+
require.NoError(t, file.SetWriteDeadline(time.Now().Add(100*time.Millisecond)))
34+
_, err = file.Write(make([]byte, 16<<20))
35+
require.ErrorIs(t, err, os.ErrDeadlineExceeded)
36+
fmt.Fprint(os.Stderr, marker)
37+
return
38+
}
39+
executable, err := os.Executable()
40+
require.NoError(t, err)
41+
ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second)
42+
defer cancel()
43+
cmd := exec.CommandContext(ctx, executable, "-test.run=^TestServerOutputInterruptsInheritedStdout$")
44+
cmd.Env = append(os.Environ(), "DATABRICKS_TEST_SERVER_OUTPUT=1")
45+
stdout, err := cmd.StdoutPipe()
46+
require.NoError(t, err)
47+
stderr, err := cmd.StderrPipe()
48+
require.NoError(t, err)
49+
require.NoError(t, cmd.Start())
50+
t.Cleanup(func() {
51+
_ = cmd.Process.Kill()
52+
_ = cmd.Wait()
53+
})
54+
// Keep stdout full until the child's write has been interrupted.
55+
line, err := bufio.NewReader(stderr).ReadString('\n')
56+
require.NoError(t, err)
57+
require.Equal(t, marker, line)
58+
_, err = io.Copy(io.Discard, stdout)
59+
require.NoError(t, err)
60+
require.NoError(t, cmd.Wait())
61+
}
62+
1763
func TestServerCommandValidatesArgumentsWithoutAuthentication(t *testing.T) {
1864
for _, args := range [][]string{
1965
{"--sshd-config", "config", "--port", "22"},

0 commit comments

Comments
 (0)