Skip to content

Commit

Permalink
refactor throwing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Hweinstock committed Nov 14, 2024
1 parent 151b103 commit 734dae4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
10 changes: 5 additions & 5 deletions packages/core/src/shared/extensions/ssh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,15 @@ export async function startVscodeRemote(
* @returns
*/
export async function getSshVersion(sshPath: string): Promise<string | undefined> {
const result = await new ChildProcess(sshPath, ['-V']).run()
const result = await new ChildProcess(sshPath, ['-V'], { collect: true }).run()

return parseSshVersion(result.stdout)
return parseSshVersion(result.stdout == '' ? result.stderr : result.stdout)
}

export async function assertSshVersionGte(sshPath: string, minVersion: string): Promise<void | never> {
export async function ensureSshVersionGte(sshPath: string, minVersion: string): Promise<void | never> {
const sshVersion = await getSshVersion(sshPath)
if (sshVersion && semver.lt(sshVersion, minVersion)) {
const msg = `SSH version ${sshVersion} is not supported, please upgrade to 7.6 or higher`
const msg = `SSH version ${sshVersion} is not supported, please upgrade OpenSSH to ${minVersion} or higher`
getLogger().error(msg)
throw new Error(msg)
}
Expand All @@ -179,5 +179,5 @@ function parseSshVersion(output: string): string | undefined {
const major = parseInt(match[1], 10)
const minor = parseInt(match[2], 10)

return `${major}.${minor}`
return `${major}.${minor}.0`
}
8 changes: 6 additions & 2 deletions packages/core/src/shared/remoteSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { findSshPath, getVscodeCliPath } from './utilities/pathFind'
import { IamClient } from './clients/iamClient'
import { IAM } from 'aws-sdk'
import { getIdeProperties } from './extensionUtilities'
import { assertSshVersionGte } from './extensions/ssh'
import { ensureSshVersionGte } from './extensions/ssh'

const policyAttachDelay = 5000

Expand Down Expand Up @@ -103,7 +103,11 @@ export async function ensureDependencies(): Promise<Result<DependencyPaths, Canc
if (tools.isOk()) {
const sshPath = tools.unwrap().ssh
// Pre 7.6 does not support accept-new as StrictHostKeyChecking
assertSshVersionGte(sshPath, '7.6')
try {
await ensureSshVersionGte(sshPath, '7.6.0')
} catch (e) {
return Result.err(e as Error)
}
}

return tools
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/test/shared/extensions/ssh.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
import * as assert from 'assert'
import { ChildProcess } from '../../../shared/utilities/processUtils'
import { assertSshVersionGte, getSshVersion, startSshAgent } from '../../../shared/extensions/ssh'
import { ensureSshVersionGte, getSshVersion, startSshAgent } from '../../../shared/extensions/ssh'
import { createExecutableFile, createTestWorkspaceFolder } from '../../testUtil'
import { isWin } from '../../../shared/vscode/env'
import path from 'path'
Expand Down Expand Up @@ -62,8 +62,8 @@ describe('SSH Versioning', function () {
it('asserts version is above threshold', async function () {
const sshPath = path.join(tempDir.uri.fsPath, `ssh3${isWin() ? '.cmd' : ''}`)
await createExecutableFile(sshPath, `echo "'OpenSSH_9.7p1, LibreSSL 3.3.6'"`)
assert.rejects(async () => await assertSshVersionGte('', '9.10'))
assert.doesNotReject(async () => await assertSshVersionGte('', '9.7'))
assert.doesNotReject(async () => await assertSshVersionGte('', '9.2'))
assert.rejects(async () => await ensureSshVersionGte('', '9.10'))
assert.doesNotReject(async () => await ensureSshVersionGte('', '9.7'))
assert.doesNotReject(async () => await ensureSshVersionGte('', '9.2'))
})
})

0 comments on commit 734dae4

Please sign in to comment.