|
| 1 | +package sandbox |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/databricks/cli/cmd/root" |
| 11 | + "github.com/databricks/cli/libs/cmdctx" |
| 12 | + "github.com/databricks/cli/libs/cmdio" |
| 13 | + "github.com/databricks/databricks-sdk-go/config" |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +// NewSSHSetupCommand configures native SSH tools to use sandbox forwarding. |
| 18 | +func NewSSHSetupCommand(deps CommandDependencies) *cobra.Command { |
| 19 | + var alias, sshConfig, releasesDir string |
| 20 | + var overwrite bool |
| 21 | + cmd := &cobra.Command{ |
| 22 | + Use: "ssh-setup [sandbox-id]", Short: "Configure native SSH tools for a sandbox", |
| 23 | + Long: `Create a local SSH Host entry for native ssh, scp, sftp, and tools that use SSH. |
| 24 | +
|
| 25 | +The CLI creates or reuses a local key. On each connection, the generated |
| 26 | +ProxyCommand prepares the sandbox and opens authenticated WebSocket port forwarding. |
| 27 | +Pass --profile with a named Databricks profile so later connections select the same workspace. |
| 28 | +Databricks CLI and that profile's credentials must remain available locally. |
| 29 | +
|
| 30 | +Examples: |
| 31 | + databricks sandbox ssh-setup happy-panda-1234 --name my-sandbox --profile my-workspace |
| 32 | + ssh my-sandbox |
| 33 | + scp ./example.txt my-sandbox:/tmp/example.txt |
| 34 | +
|
| 35 | +Interactive 'databricks sandbox ssh' works without this setup step.`, |
| 36 | + Args: cobra.MaximumNArgs(1), |
| 37 | + PreRunE: func(cmd *cobra.Command, args []string) error { |
| 38 | + cmd.SetContext(root.SkipLoadBundle(cmd.Context())) |
| 39 | + return root.MustWorkspaceClient(cmd, args) |
| 40 | + }, |
| 41 | + ValidArgsFunction: deps.Complete, |
| 42 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 43 | + if err := ValidateHostAlias(alias); err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + ctx := cmd.Context() |
| 47 | + profileFlag := cmd.Flag("profile") |
| 48 | + if profileFlag == nil || !profileFlag.Changed || profileFlag.Value.String() == "" { |
| 49 | + return errors.New("ssh-setup requires a named Databricks profile; run 'databricks auth login --profile NAME' and rerun with --profile NAME") |
| 50 | + } |
| 51 | + w := cmdctx.WorkspaceClient(ctx) |
| 52 | + requested := "" |
| 53 | + if len(args) == 1 { |
| 54 | + requested = args[0] |
| 55 | + } |
| 56 | + name, err := deps.ResolveName(ctx, w, requested) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + identity, err := prepareSSHIdentity(ctx, w, name) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + executable, err := os.Executable() |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + options := sshProxyOptions{Profile: w.Config.Profile, ReleasesDir: releasesDir, IdentityScope: identity.HostKeyAlias} |
| 69 | + if releasesDir != "" { |
| 70 | + options.ReleasesDir, err = filepath.Abs(releasesDir) |
| 71 | + if err != nil { |
| 72 | + return fmt.Errorf("resolve SSH releases directory: %w", err) |
| 73 | + } |
| 74 | + options.ReleasesDir = filepath.ToSlash(options.ReleasesDir) |
| 75 | + } |
| 76 | + if w.Config.ConfigFile != "" { |
| 77 | + file, err := config.LoadFile(w.Config.ConfigFile) |
| 78 | + if err != nil { |
| 79 | + return fmt.Errorf("load SSH profile configuration: %w", err) |
| 80 | + } |
| 81 | + options.ConfigFile, err = filepath.Abs(file.Path()) |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("resolve SSH profile configuration path: %w", err) |
| 84 | + } |
| 85 | + options.ConfigFile = filepath.ToSlash(options.ConfigFile) |
| 86 | + } |
| 87 | + proxy, err := sshProxyCommand(executable, name, options) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + location, err := WriteHostConfig(ctx, alias, sshConfig, proxy, identity, overwrite) |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + cmdio.LogString(ctx, fmt.Sprintf("Created SSH configuration at %s. Connect with 'ssh %s' or 'scp ./file %s:/tmp/file'.", strings.ReplaceAll(location, `\`, "/"), alias, alias)) |
| 96 | + return nil |
| 97 | + }, |
| 98 | + } |
| 99 | + cmd.Flags().StringVar(&alias, "name", "", "Local SSH host alias") |
| 100 | + cmd.MarkFlagRequired("name") |
| 101 | + cmd.Flags().StringVar(&sshConfig, "ssh-config", "", "Path to SSH config file (default ~/.ssh/config)") |
| 102 | + cmd.Flags().StringVar(&releasesDir, "releases-dir", "", "Directory containing version-matched Linux CLI archives") |
| 103 | + cmd.Flags().BoolVar(&overwrite, "overwrite", false, "Replace an existing managed SSH host entry") |
| 104 | + return cmd |
| 105 | +} |
0 commit comments