-
Notifications
You must be signed in to change notification settings - Fork 11
🤖 Implement SSH connection pooling #453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| import * as os from "os"; | ||
| import { getControlPath } from "./sshConnectionPool"; | ||
| import type { SSHRuntimeConfig } from "./SSHRuntime"; | ||
|
|
||
| describe("sshConnectionPool", () => { | ||
| describe("getControlPath", () => { | ||
| test("identical configs produce same controlPath", () => { | ||
| const config: SSHRuntimeConfig = { | ||
| host: "test.example.com", | ||
| srcBaseDir: "/work", | ||
| }; | ||
| const path1 = getControlPath(config); | ||
| const path2 = getControlPath(config); | ||
|
|
||
| expect(path1).toBe(path2); | ||
| }); | ||
|
|
||
| test("different hosts produce different controlPaths", () => { | ||
| const path1 = getControlPath({ | ||
| host: "host1.example.com", | ||
| srcBaseDir: "/work", | ||
| }); | ||
| const path2 = getControlPath({ | ||
| host: "host2.example.com", | ||
| srcBaseDir: "/work", | ||
| }); | ||
|
|
||
| expect(path1).not.toBe(path2); | ||
| }); | ||
|
|
||
| test("different ports produce different controlPaths", () => { | ||
| const config1: SSHRuntimeConfig = { | ||
| host: "test.com", | ||
| srcBaseDir: "/work", | ||
| port: 22, | ||
| }; | ||
| const config2: SSHRuntimeConfig = { | ||
| host: "test.com", | ||
| srcBaseDir: "/work", | ||
| port: 2222, | ||
| }; | ||
|
|
||
| expect(getControlPath(config1)).not.toBe(getControlPath(config2)); | ||
| }); | ||
|
|
||
| test("different identityFiles produce different controlPaths", () => { | ||
| const config1: SSHRuntimeConfig = { | ||
| host: "test.com", | ||
| srcBaseDir: "/work", | ||
| identityFile: "/path/to/key1", | ||
| }; | ||
| const config2: SSHRuntimeConfig = { | ||
| host: "test.com", | ||
| srcBaseDir: "/work", | ||
| identityFile: "/path/to/key2", | ||
| }; | ||
|
|
||
| expect(getControlPath(config1)).not.toBe(getControlPath(config2)); | ||
| }); | ||
|
|
||
| test("different srcBaseDirs produce different controlPaths", () => { | ||
| const config1: SSHRuntimeConfig = { | ||
| host: "test.com", | ||
| srcBaseDir: "/work1", | ||
| }; | ||
| const config2: SSHRuntimeConfig = { | ||
| host: "test.com", | ||
| srcBaseDir: "/work2", | ||
| }; | ||
|
|
||
| expect(getControlPath(config1)).not.toBe(getControlPath(config2)); | ||
| }); | ||
|
|
||
| test("controlPath is in tmpdir with expected format", () => { | ||
| const config: SSHRuntimeConfig = { | ||
| host: "test.com", | ||
| srcBaseDir: "/work", | ||
| }; | ||
| const controlPath = getControlPath(config); | ||
|
|
||
| expect(controlPath).toContain(os.tmpdir()); | ||
| expect(controlPath).toMatch(/cmux-ssh-[a-f0-9]{12}$/); | ||
| }); | ||
|
|
||
| test("missing port defaults to 22 in hash calculation", () => { | ||
| const config1: SSHRuntimeConfig = { | ||
| host: "test.com", | ||
| srcBaseDir: "/work", | ||
| port: 22, | ||
| }; | ||
| const config2: SSHRuntimeConfig = { | ||
| host: "test.com", | ||
| srcBaseDir: "/work", | ||
| // port omitted, should default to 22 | ||
| }; | ||
|
|
||
| expect(getControlPath(config1)).toBe(getControlPath(config2)); | ||
| }); | ||
|
|
||
| test("missing identityFile defaults to 'default' in hash calculation", () => { | ||
| const config1: SSHRuntimeConfig = { | ||
| host: "test.com", | ||
| srcBaseDir: "/work", | ||
| identityFile: undefined, | ||
| }; | ||
| const config2: SSHRuntimeConfig = { | ||
| host: "test.com", | ||
| srcBaseDir: "/work", | ||
| // identityFile omitted | ||
| }; | ||
|
|
||
| expect(getControlPath(config1)).toBe(getControlPath(config2)); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("username isolation", () => { | ||
| test("controlPath includes local username to prevent cross-user collisions", () => { | ||
| // This test verifies that os.userInfo().username is included in the hash | ||
| // On multi-user systems, different users connecting to the same remote | ||
| // would get different controlPaths, preventing permission errors | ||
| const config: SSHRuntimeConfig = { | ||
| host: "test.com", | ||
| srcBaseDir: "/work", | ||
| }; | ||
| const controlPath = getControlPath(config); | ||
|
|
||
| // The path should be deterministic for this user | ||
| expect(controlPath).toBe(getControlPath(config)); | ||
| expect(controlPath).toMatch(/^\/tmp\/cmux-ssh-[a-f0-9]{12}$/); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /** | ||
| * SSH Connection Pool - Stateless | ||
| * | ||
| * Generates deterministic ControlPath from SSH config to enable connection | ||
| * multiplexing across SSHRuntime instances targeting the same host. | ||
| * | ||
| * Design: | ||
| * - Pure function: same config → same controlPath | ||
| * - No state: filesystem is the state | ||
| * - No cleanup: ControlPersist + OS handle it | ||
| */ | ||
|
|
||
| import * as crypto from "crypto"; | ||
| import * as path from "path"; | ||
| import * as os from "os"; | ||
| import type { SSHRuntimeConfig } from "./SSHRuntime"; | ||
|
|
||
| /** | ||
| * Get deterministic controlPath for SSH config. | ||
| * Multiple calls with identical config return the same path, | ||
| * enabling ControlMaster to multiplex connections. | ||
| * | ||
| * Socket files are created by SSH and cleaned up automatically: | ||
| * - ControlPersist=60: Removes socket 60s after last use | ||
| * - OS: Cleans /tmp on reboot | ||
| * | ||
| * Includes local username in hash to prevent cross-user collisions on | ||
| * multi-user systems (different users connecting to same remote would | ||
| * otherwise generate same socket path, causing permission errors). | ||
| */ | ||
| export function getControlPath(config: SSHRuntimeConfig): string { | ||
| const key = makeConnectionKey(config); | ||
| const hash = hashKey(key); | ||
| return path.join(os.tmpdir(), `cmux-ssh-${hash}`); | ||
| } | ||
|
|
||
| /** | ||
| * Generate stable key from config. | ||
| * Identical configs produce identical keys. | ||
| * Includes local username to prevent cross-user socket collisions. | ||
| */ | ||
| function makeConnectionKey(config: SSHRuntimeConfig): string { | ||
| const parts = [ | ||
| os.userInfo().username, // Include local user to prevent cross-user collisions | ||
| config.host, | ||
| config.port?.toString() ?? "22", | ||
| config.srcBaseDir, | ||
| config.identityFile ?? "default", | ||
| ]; | ||
| return parts.join(":"); | ||
| } | ||
|
|
||
| /** | ||
| * Generate deterministic hash for controlPath naming. | ||
| * Uses first 12 chars of SHA-256 for human-readable uniqueness. | ||
| */ | ||
| function hashKey(key: string): string { | ||
| return crypto.createHash("sha256").update(key).digest("hex").substring(0, 12); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.