Skip to content

fix(ssh): SaveSSHKeyPair no longer removes the shared key directory#4988

Open
ilia-db wants to merge 1 commit intomainfrom
fix-ssh-key-save-removes-directory
Open

fix(ssh): SaveSSHKeyPair no longer removes the shared key directory#4988
ilia-db wants to merge 1 commit intomainfrom
fix-ssh-key-save-removes-directory

Conversation

@ilia-db
Copy link
Copy Markdown
Contributor

@ilia-db ilia-db commented Apr 16, 2026

Summary

  • SaveSSHKeyPair was calling os.RemoveAll on ~/.databricks/ssh-tunnel-keys/
    (the entire shared directory) before writing new keys, which deleted keys belonging
    to all other concurrent sessions and broke their SSH authentication.
  • Fix: drop the RemoveAll and let os.WriteFile create or overwrite the two
    session-specific files directly.
  • Add tests covering: fresh creation, overwrite of existing keys, auto-creation of
    a missing directory, and isolation between concurrent sessions.

Root cause

Initially suspected to be a race between the ProxyCommand writing keys and OpenSSH
reading them. Investigation of the OpenSSH source (ssh.c, sshconnect.c) showed that
ssh_connect() returns immediately after fork+pipe, so load_public_identity_files()
can run concurrently with the proxy — but this is benign, since SSH falls back to
loading the private key during ssh_userauth2(), which only runs after actual I/O
flows on the pipe (by which point the proxy has finished writing). The real bug was
always the RemoveAll nuking the shared directory.

Test plan

  • go test ./experimental/ssh/internal/keys/...
  • Manual: connect to two clusters simultaneously, verify both sessions stay authenticated

This pull request was AI-assisted by Isaac.

The previous implementation called os.RemoveAll on the parent directory
(~/.databricks/ssh-tunnel-keys/) before writing new keys. This would
silently delete keys belonging to other concurrent sessions on different
clusters, causing their authentication to fail.

Fix: drop the RemoveAll and let os.WriteFile create or overwrite the
session-specific files directly. Add tests covering fresh creation,
overwrite, directory auto-creation, and isolation between sessions.

Co-authored-by: Isaac
@ilia-db ilia-db requested review from anton-107 and pietern April 16, 2026 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants