Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nextchanges/cli/sandbox-ssh-port-forward.md
Original file line number Diff line number Diff line change
@@ -0,0 +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))
8 changes: 8 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ This Software includes software developed at Databricks (https://www.databricks.

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

grpc/grpc-go - https://github.com/grpc/grpc-go
Copyright The gRPC Authors
License - https://github.com/grpc/grpc-go/blob/master/LICENSE

spf13/cobra - https://github.com/spf13/cobra
Copyright cobra authors
License - https://github.com/spf13/cobra/blob/main/LICENSE.txt
Expand Down Expand Up @@ -33,6 +37,10 @@ License - https://github.com/gorilla/websocket/blob/main/LICENSE

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

protocolbuffers/protobuf-go - https://github.com/protocolbuffers/protobuf-go
Copyright The Go Authors
License - https://github.com/protocolbuffers/protobuf-go/blob/master/LICENSE

spf13/pflag - https://github.com/spf13/pflag
Copyright (c) 2012 Alex Ogier. All rights reserved.
Copyright (c) 2012 The Go Authors. All rights reserved.
Expand Down
1 change: 0 additions & 1 deletion cmd/sandbox/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ tooling (Python, Node.js, Rust, Databricks CLI) and persistent storage.

Getting started:
databricks auth login --host https://... # authenticate to a Databricks workspace
databricks sandbox register # generate and register an SSH key
databricks sandbox create # provision a sandbox (becomes your default)
databricks sandbox ssh # SSH to your default sandbox

Expand Down
38 changes: 36 additions & 2 deletions cmd/sandbox/server.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package sandbox

import (
"bytes"
"errors"
"fmt"
"io"
"net"
"os"
"runtime"
"time"

sshtunnel "github.com/databricks/cli/experimental/ssh/sandbox"
Expand All @@ -28,9 +32,15 @@ func newServerCommand() *cobra.Command {
if err != nil {
return fmt.Errorf("listen for SSH WebSockets: %w", err)
}
output, err := openServerOutput(cmd.OutOrStdout())
if err != nil {
listener.Close()
return fmt.Errorf("open command output: %w", err)
}
defer output.Close()
return sshtunnel.Serve(cmd.Context(), listener, sshtunnel.ServerOptions{
SSHD: sshd, Config: config, IdleTimeout: idleTimeout,
}, cmd.OutOrStdout())
SSHD: sshd, Config: config, IdleTimeout: idleTimeout, OutputHeartbeat: 30 * time.Second,
}, output)
},
}
cmd.Flags().StringVar(&config, "sshd-config", "", "Prepared sshd configuration file")
Expand All @@ -40,3 +50,27 @@ func newServerCommand() *cobra.Command {
cmd.Flags().DurationVar(&idleTimeout, "idle-timeout", time.Minute, "Stop after this duration without an SSH connection")
return cmd
}

func openServerOutput(output io.Writer) (io.WriteCloser, error) {
if _, ok := output.(*bytes.Buffer); ok || output == io.Discard {
return serverOutputWriter{output}, nil
}
file, ok := output.(*os.File)
if !ok || runtime.GOOS != "linux" {
return nil, errors.New("server output must be a Linux command-execution pipe")
}
info, err := file.Stat()
if err != nil {
return nil, err
}
if info.Mode()&os.ModeNamedPipe == 0 {
return nil, errors.New("server output must be a Linux command-execution pipe")
}
// Reopening inherited stdout registers its pipe with Go's poller, so
// Close interrupts blocked writes without changing the inherited fd.
return os.OpenFile(fmt.Sprintf("/proc/self/fd/%d", file.Fd()), os.O_WRONLY, 0)
}

type serverOutputWriter struct{ io.Writer }

func (serverOutputWriter) Close() error { return nil }
46 changes: 46 additions & 0 deletions cmd/sandbox/server_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,65 @@
package sandbox

import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestServerOutputInterruptsInheritedStdout(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skip("Linux sandbox command output uses /proc/self/fd")
}
const marker = "server output write interrupted\n"
if os.Getenv("DATABRICKS_TEST_SERVER_OUTPUT") == "1" {
output, err := openServerOutput(os.Stdout)
require.NoError(t, err)
defer output.Close()
file := output.(*os.File)
require.NoError(t, file.SetWriteDeadline(time.Now().Add(100*time.Millisecond)))
_, err = file.Write(make([]byte, 16<<20))
require.ErrorIs(t, err, os.ErrDeadlineExceeded)
fmt.Fprint(os.Stderr, marker)
return
}
executable, err := os.Executable()
require.NoError(t, err)
ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, executable, "-test.run=^TestServerOutputInterruptsInheritedStdout$")
cmd.Env = append(os.Environ(), "DATABRICKS_TEST_SERVER_OUTPUT=1")
stdout, err := cmd.StdoutPipe()
require.NoError(t, err)
stderr, err := cmd.StderrPipe()
require.NoError(t, err)
require.NoError(t, cmd.Start())
t.Cleanup(func() {
_ = cmd.Process.Kill()
_ = cmd.Wait()
})
// Keep stdout full until the child's write has been interrupted.
line, err := bufio.NewReader(stderr).ReadString('\n')
require.NoError(t, err)
require.Equal(t, marker, line)
_, err = io.Copy(io.Discard, stdout)
require.NoError(t, err)
require.NoError(t, cmd.Wait())
}

func TestServerCommandValidatesArgumentsWithoutAuthentication(t *testing.T) {
for _, args := range [][]string{
{"--sshd-config", "config", "--port", "22"},
Expand Down
Loading
Loading