Skip to content
Open
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
5 changes: 5 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ linters:
- "**"
- "!**/cmd/cmd.go"
- "!**/cmd/experimental/**"
# Sandbox SSH command files integrate the shared SSH implementation.
- "!**/cmd/sandbox/server.go"
- "!**/cmd/sandbox/ssh.go"
- "!**/cmd/sandbox/ssh_setup.go"
- "!**/cmd/sandbox/ssh_test.go"
- "!**/experimental/**"
deny:
- pkg: "github.com/databricks/cli/experimental"
Expand Down
1 change: 1 addition & 0 deletions cmd/sandbox/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Common workflows:
`,
}

cmd.AddCommand(newServerCommand())
cmd.AddCommand(newRegisterCommand())
cmd.AddCommand(newSSHKeyCommand())
cmd.AddCommand(newSetDefaultCommand())
Expand Down
42 changes: 42 additions & 0 deletions cmd/sandbox/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package sandbox

import (
"errors"
"fmt"
"net"
"time"

sshtunnel "github.com/databricks/cli/experimental/ssh/sandbox"
"github.com/spf13/cobra"
)

func newServerCommand() *cobra.Command {
var config, sshd string
var port int
var idleTimeout time.Duration
cmd := &cobra.Command{
Use: "server", Short: "Run the sandbox WebSocket SSH relay", Hidden: true,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if port != 0 && (port < 1024 || port > 65535) {
return errors.New("port must be zero or between 1024 and 65535")
}
if idleTimeout <= 0 {
return errors.New("idle timeout must be positive")
}
listener, err := net.Listen("tcp4", fmt.Sprintf("0.0.0.0:%d", port))
if err != nil {
return fmt.Errorf("listen for SSH WebSockets: %w", err)
}
return sshtunnel.Serve(cmd.Context(), listener, sshtunnel.ServerOptions{
SSHD: sshd, Config: config, IdleTimeout: idleTimeout,
}, cmd.OutOrStdout())
},
}
cmd.Flags().StringVar(&config, "sshd-config", "", "Prepared sshd configuration file")
cmd.MarkFlagRequired("sshd-config")
cmd.Flags().StringVar(&sshd, "sshd", "/usr/sbin/sshd", "OpenSSH server executable")
cmd.Flags().IntVar(&port, "port", 40000, "HTTP/WebSocket listener port (0 selects a free port)")
cmd.Flags().DurationVar(&idleTimeout, "idle-timeout", time.Minute, "Stop after this duration without an SSH connection")
return cmd
}
56 changes: 56 additions & 0 deletions cmd/sandbox/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package sandbox

import (
"bytes"
"encoding/json"
"net"
"os"
"path/filepath"
"runtime"
"strconv"
"testing"

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

func TestServerCommandValidatesArgumentsWithoutAuthentication(t *testing.T) {
for _, args := range [][]string{
{"--sshd-config", "config", "--port", "22"},
{"--sshd-config", "config", "--idle-timeout", "0s"},
{"--sshd-config", "config", "unexpected"},
} {
cmd := newServerCommand()
cmd.SetArgs(args)
err := cmd.ExecuteContext(t.Context())
require.Error(t, err)
assert.NotContains(t, err.Error(), "credential")
}
}

func TestServerCommandReportsReadiness(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("sandbox servers run on Linux")
}
binary := filepath.Join(t.TempDir(), "sshd")
require.NoError(t, os.WriteFile(binary, []byte("#!/bin/sh\nexit 0\n"), 0o700))
cmd := newServerCommand()
var output bytes.Buffer
cmd.SetOut(&output)
cmd.SetArgs([]string{"--sshd", binary, "--sshd-config", "prepared-config", "--port", "0", "--idle-timeout", "20ms"})
require.NoError(t, cmd.ExecuteContext(t.Context()))
var ready struct {
Port int `json:"port"`
}
require.NoError(t, json.Unmarshal(output.Bytes(), &ready))
assert.GreaterOrEqual(t, ready.Port, 1024)
}

func TestServerCommandReportsOccupiedPort(t *testing.T) {
listener, err := net.Listen("tcp4", "0.0.0.0:0")
require.NoError(t, err)
defer listener.Close()
cmd := newServerCommand()
cmd.SetArgs([]string{"--sshd-config", "prepared-config", "--port", strconv.Itoa(listener.Addr().(*net.TCPAddr).Port)})
require.ErrorContains(t, cmd.ExecuteContext(t.Context()), "listen for SSH WebSockets")
}
12 changes: 10 additions & 2 deletions experimental/ssh/internal/proxy/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewConnectionsManager(maxClients int, shutdownDelay time.Duration) *Connect
maxClients: maxClients,
shutdownDelay: shutdownDelay,
connections: make(map[string]*proxyConnection),
TimedOut: make(chan bool),
TimedOut: make(chan bool, 1),
}
cm.startShutdownTimer(shutdownDelay)
return cm
Expand Down Expand Up @@ -86,7 +86,10 @@ func (cm *ConnectionsManager) startShutdownTimer(delay time.Duration) {
cm.shutdownTimer.Stop()
}
cm.shutdownTimer = time.AfterFunc(delay, func() {
cm.TimedOut <- true
select {
case cm.TimedOut <- true:
default:
}
})
}

Expand All @@ -97,3 +100,8 @@ func (cm *ConnectionsManager) cancelShutdownTimer() {
cm.shutdownTimer.Stop()
}
}

// Close releases the idle timer when the relay owning this manager stops.
func (cm *ConnectionsManager) Close() {
cm.cancelShutdownTimer()
}
13 changes: 13 additions & 0 deletions experimental/ssh/internal/proxy/connections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,16 @@ func TestConnectionsManager_ShutdownTimer_NoRestartWhenConnectionsRemain(t *test
assert.False(t, timedOut, "Unexpected timeout signal while connections still exist")
})
}

func TestConnectionsManagerCloseStopsIdleTimer(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
cm := NewConnectionsManager(1, time.Second)
cm.Close()
time.Sleep(2 * time.Second)
select {
case <-cm.TimedOut:
t.Fatal("closed manager timed out")
default:
}
})
}
121 changes: 121 additions & 0 deletions experimental/ssh/sandbox/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package sandbox

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"os/exec"
"sync"
"time"

"github.com/databricks/cli/experimental/ssh/internal/proxy"
)

// ServerOptions describes a relay whose SSH configuration was prepared by the caller.
type ServerOptions struct {
SSHD string
Config string
IdleTimeout time.Duration
}

// Readiness is written to command output only after the listener has been bound.
type Readiness struct {
Port int `json:"port"`
}

// Serve runs a sandbox relay without workspace credentials or compute initialization.
// Each invocation owns its listener and all the SSH processes it starts.
func Serve(ctx context.Context, listener net.Listener, opts ServerOptions, output io.Writer) error {
defer listener.Close()
if opts.IdleTimeout <= 0 {
return errors.New("idle timeout must be positive")
}
if opts.Config == "" || opts.SSHD == "" {
return errors.New("sshd executable and configuration are required")
}
// Validate the configuration before announcing readiness.
check := exec.CommandContext(ctx, opts.SSHD, "-t", "-f", opts.Config)
if diagnostic, err := check.CombinedOutput(); err != nil {
return fmt.Errorf("validate sshd configuration: %w: %s", err, diagnostic)
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
connections := proxy.NewConnectionsManager(1, opts.IdleTimeout)
defer connections.Close()
handler := proxy.NewProxyServer(ctx, connections, func(ctx context.Context) *exec.Cmd {
return exec.CommandContext(ctx, opts.SSHD, "-i", "-f", opts.Config)
})

// One relay belongs to one invocation; the sandbox client does not negotiate
// compute's handover or resume protocols.
var requests sync.WaitGroup
var requestMu sync.Mutex
active, closing := false, false
mux := http.NewServeMux()
mux.HandleFunc("GET /ssh", func(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
if query.Has("delivered") || query.Has("reattach") || query.Has("resume_version") {
http.Error(w, "Session resume is not supported by this relay", http.StatusBadRequest)
return
}
requestMu.Lock()
if active || closing {
requestMu.Unlock()
http.Error(w, "SSH connection already active or relay is stopping", http.StatusServiceUnavailable)
return
}
active = true
requests.Add(1)
requestMu.Unlock()
defer func() {
requestMu.Lock()
active = false
connections.ExtendIdleTimeout(opts.IdleTimeout)
requestMu.Unlock()
requests.Done()
}()
handler.ServeHTTP(w, r)
})
server := &http.Server{Handler: mux, ReadHeaderTimeout: 10 * time.Second}
stopped := make(chan struct{})
var serveErr error
go func() { serveErr = server.Serve(listener); close(stopped) }()
defer func() {
requestMu.Lock()
closing = true
requestMu.Unlock()
cancel()
server.Close()
<-stopped
requests.Wait()
}()
if err := json.NewEncoder(output).Encode(Readiness{Port: listener.Addr().(*net.TCPAddr).Port}); err != nil {
return fmt.Errorf("report relay readiness: %w", err)
}
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-connections.TimedOut:
// A timer may fire just as a connection is admitted. Admission and the idle
// decision share a lock so an already queued timeout cannot end that session.
requestMu.Lock()
if active {
requestMu.Unlock()
continue
}
closing = true
requestMu.Unlock()
return nil
case <-stopped:
if errors.Is(serveErr, http.ErrServerClosed) {
return nil
}
return serveErr
}
}
}
Loading
Loading