Skip to content

Commit 7d8eb11

Browse files
authored
Add user known hosts file support for SSH connections (#4085)
Main use case is e2e tests - running them locally will lead to changes in ~/.ssh/known_hosts file. Which later will lead to failures, when our clenaup jobs invalidate secrete scopes with cluster ssh keys.
1 parent 1fb3125 commit 7d8eb11

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# not actually checked by tests
2+
cluster ssh-rsa key

acceptance/ssh/connection/script

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
errcode $CLI ssh connect --cluster=$TEST_DEFAULT_CLUSTER_ID --releases-dir=$CLI_RELEASES_DIR -- "echo 'Connection successful'" >out.stdout.txt 2>LOG.stderr
1+
errcode $CLI ssh connect --cluster=$TEST_DEFAULT_CLUSTER_ID --releases-dir=$CLI_RELEASES_DIR --user-known-hosts-file=known_hosts -- "echo 'Connection successful'" >out.stdout.txt 2>LOG.stderr
22

33
if ! grep -q "Connection successful" out.stdout.txt; then
44
run_id=$(cat LOG.stderr | grep -o "Job submitted successfully with run ID: [0-9]*" | grep -o "[0-9]*$")

experimental/ssh/cmd/connect.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ the SSH server and handling the connection proxy.
2929
var handoverTimeout time.Duration
3030
var releasesDir string
3131
var autoStartCluster bool
32+
var userKnownHostsFile string
3233

3334
cmd.Flags().StringVar(&clusterID, "cluster", "", "Databricks cluster ID (required)")
3435
cmd.MarkFlagRequired("cluster")
@@ -46,6 +47,9 @@ the SSH server and handling the connection proxy.
4647
cmd.Flags().StringVar(&releasesDir, "releases-dir", "", "Directory for local SSH tunnel development releases")
4748
cmd.Flags().MarkHidden("releases-dir")
4849

50+
cmd.Flags().StringVar(&userKnownHostsFile, "user-known-hosts-file", "", "Path to user known hosts file for SSH client")
51+
cmd.Flags().MarkHidden("user-known-hosts-file")
52+
4953
cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
5054
// CLI in the proxy mode is executed by the ssh client and can't prompt for input
5155
if proxyMode {
@@ -73,6 +77,7 @@ the SSH server and handling the connection proxy.
7377
AutoStartCluster: autoStartCluster,
7478
ClientPublicKeyName: clientPublicKeyName,
7579
ClientPrivateKeyName: clientPrivateKeyName,
80+
UserKnownHostsFile: userKnownHostsFile,
7681
AdditionalArgs: args,
7782
}
7883
return client.Run(ctx, wsClient, opts)

experimental/ssh/internal/client/client.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ type ClientOptions struct {
6868
Profile string
6969
// Additional arguments to pass to the SSH client in the non proxy mode.
7070
AdditionalArgs []string
71+
// Optional path to the user known hosts file.
72+
UserKnownHostsFile string
7173
}
7274

7375
func Run(ctx context.Context, client *databricks.WorkspaceClient, opts ClientOptions) error {
@@ -253,8 +255,11 @@ func spawnSSHClient(ctx context.Context, userName, privateKeyPath string, server
253255
"-o", "StrictHostKeyChecking=accept-new",
254256
"-o", "ConnectTimeout=360",
255257
"-o", "ProxyCommand=" + proxyCommand,
256-
opts.ClusterID,
257258
}
259+
if opts.UserKnownHostsFile != "" {
260+
sshArgs = append(sshArgs, "-o", "UserKnownHostsFile="+opts.UserKnownHostsFile)
261+
}
262+
sshArgs = append(sshArgs, opts.ClusterID)
258263
sshArgs = append(sshArgs, opts.AdditionalArgs...)
259264

260265
cmdio.LogString(ctx, "Launching SSH client: ssh "+strings.Join(sshArgs, " "))

0 commit comments

Comments
 (0)