Repository navigation
fix(ssh): report OpenSSH failures from cmux ssh instead of a Cloud VM error #14756
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
4 commits
Select commit
Hold shift + click to select a range
311797d
test: cover cmux ssh failing fast on refused and unreachable hosts
austinywang 9ff9017
fix(ssh): report OpenSSH failures from cmux ssh instead of a Cloud VM…
austinywang c344ce9
test: cover concurrent cmux ssh opens sharing one route check
austinywang 39e2c3a
fix(ssh): share one route check across concurrent cmux ssh opens
austinywang 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
93 changes: 93 additions & 0 deletions
93
Packages/macOS/CmuxCloud/Sources/CmuxCloud/Link/SSHTuiPreflight.swift
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,93 @@ | ||
| import CmuxFoundation | ||
| import Foundation | ||
|
|
||
| /// Proves an SSH route authenticates without a prompt before the headless carrier starts. | ||
| /// | ||
| /// The carrier cannot answer a password, keyboard-interactive or host-key prompt, | ||
| /// and it retries OpenSSH's own failures until its startup deadline. A batch-mode | ||
| /// `ssh … true` returns OpenSSH's diagnostic in seconds instead. With connection | ||
| /// sharing configured it also opens the master the carrier then multiplexes over. | ||
| public struct SSHTuiPreflight: Sendable { | ||
| public init(connection: SSHTuiConnection, commands: any CommandRunning = CommandRunner(), timeout: TimeInterval = 30) { | ||
| self.connection = connection | ||
| self.commands = commands | ||
| self.timeout = timeout | ||
| } | ||
|
|
||
| public let connection: SSHTuiConnection | ||
| private let commands: any CommandRunning | ||
| private let timeout: TimeInterval | ||
|
|
||
| public func run() async throws { | ||
| var arguments = connection.preflightArguments | ||
| var executable = arguments.removeFirst() | ||
| // The child inherits the app environment, so the caller's agent is | ||
| // passed the same way the carrier receives it. | ||
| if let agent = connection.configuration.agentSocketPath { | ||
| arguments = ["SSH_AUTH_SOCK=" + agent, executable] + arguments | ||
| executable = "/usr/bin/env" | ||
| } | ||
| let result = await commands.run( | ||
| directory: NSHomeDirectory(), executable: executable, arguments: arguments, timeout: timeout | ||
| ) | ||
| if let failure = result.executionError { | ||
| try Task.checkCancellation() | ||
| throw SSHTuiPreflightError.launchFailed(failure) | ||
| } | ||
| if result.timedOut { throw SSHTuiPreflightError.timedOut } | ||
| // 255 is OpenSSH's own failure. Any other status came from the remote | ||
| // `true`, so the route works and the carrier owns what follows. | ||
| if result.exitStatus == 255 { throw SSHTuiPreflightError.sshFailed(result.stderr ?? "") } | ||
| } | ||
| } | ||
|
|
||
| public enum SSHTuiPreflightError: LocalizedError, Equatable, Sendable { | ||
| /// OpenSSH failed its own connection; carries its standard error. | ||
| case sshFailed(String) | ||
| case timedOut | ||
| case launchFailed(String) | ||
|
|
||
| /// OpenSSH's whole standard error, or the empty string. Classify this, not | ||
| /// `diagnostic`, so chatty ProxyCommand output cannot push a marker out. | ||
| public var standardError: String { | ||
| guard case .sshFailed(let output) = self else { return "" } | ||
| return output | ||
| } | ||
|
|
||
| /// OpenSSH's diagnostic without blank lines, or the empty string. | ||
| public var diagnostic: String { | ||
| standardError.split(whereSeparator: \.isNewline) | ||
| .map { $0.trimmingCharacters(in: .whitespaces) } | ||
| .filter { !$0.isEmpty } | ||
| .suffix(8) | ||
| .joined(separator: "\n") | ||
| } | ||
|
|
||
| /// Whether the route stopped before OpenSSH could authenticate. A | ||
| /// ProxyCommand waiting on a login it cannot show stalls here, and an | ||
| /// interactive run lets that prompt reach the user. | ||
| public var stalledBeforeAuthentication: Bool { | ||
| switch self { | ||
| case .timedOut: return true | ||
| case .sshFailed: return standardError.localizedCaseInsensitiveContains("timed out during banner exchange") | ||
| case .launchFailed: return false | ||
| } | ||
| } | ||
|
|
||
| public var errorDescription: String? { | ||
| switch self { | ||
| case .sshFailed: | ||
| let diagnostic = diagnostic | ||
| return diagnostic.isEmpty | ||
| ? String(localized: "cloud.link.sshPreflight.failed", defaultValue: "SSH could not connect to the host.") | ||
| : diagnostic | ||
| case .timedOut: | ||
| return String(localized: "cloud.link.sshPreflight.timedOut", defaultValue: "SSH did not finish connecting in time.") | ||
| case .launchFailed(let detail): | ||
| return String( | ||
| format: String(localized: "cloud.link.sshPreflight.launchFailed", defaultValue: "SSH could not be started: %@"), | ||
| detail | ||
| ) | ||
| } | ||
| } | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.