Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/core/src/awsService/ec2/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { SshKeyPair } from './sshKeyPair'
import { Ec2SessionTracker } from './remoteSessionManager'
import { getEc2SsmEnv } from './utils'

export type Ec2ConnectErrorCode = 'EC2SSMStatus' | 'EC2SSMPermission' | 'EC2SSMConnect' | 'EC2SSMAgentStatus'
export type Ec2ConnectErrorCode = 'EC2SSMStatus' | 'EC2SSMPermission' | 'EC2SSMTestConnect' | 'EC2SSMAgentStatus'

export interface Ec2RemoteEnv extends VscodeRemoteConnection {
selection: Ec2Selection
Expand Down Expand Up @@ -215,8 +215,9 @@ export class Ec2Connecter implements vscode.Disposable {
remoteUser.name
)
} catch (err) {
const message = err instanceof SshError ? 'Testing SSH connection to instance failed' : ''
this.throwConnectionError(message, selection, err as Error)
const message =
err instanceof SshError ? `Testing SSM connection to instance failed with error: ${err.message}` : ''
this.throwConnectionError(message, selection, { ...(err as Error), code: 'EC2SSMTestConnect' })
} finally {
await this.ssmClient.terminateSession(testSession)
}
Expand Down
23 changes: 15 additions & 8 deletions packages/core/src/shared/extensions/ssh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,27 +130,34 @@ export class RemoteSshSettings extends Settings.define('remote.SSH', remoteSshTy
}
}

/**
* Test a SSH connection over SSM.
* @param ProcessClass given process to test the connection within.
* @param hostname
* @param sshPath
* @param user
* @param session SSM session credentials. These cannot be reused, so it may be required to create a seperate session for the test connection.
* @returns
*/
export async function testSshConnection(
ProcessClass: typeof ChildProcess,
hostname: string,
sshPath: string,
user: string,
session: SSM.StartSessionResponse
): Promise<ChildProcessResult | never> {
const env = { SESSION_ID: session.SessionId, STREAM_URL: session.StreamUrl, TOKEN: session.TokenValue }
const process = new ProcessClass(sshPath, ['-T', `${user}@${hostname}`, 'echo "test connection succeeded" && exit'])
try {
const env = { SESSION_ID: session.SessionId, STREAM_URL: session.StreamUrl, TOKEN: session.TokenValue }
const result = await new ProcessClass(sshPath, [
'-T',
`${user}@${hostname}`,
'echo "test connection succeeded" && exit',
]).run({
return await process.run({
spawnOptions: {
env,
},
})
return result
} catch (error) {
throw new SshError('SSH connection test failed', { cause: error as Error })
throw new SshError(process.result()?.stderr ?? 'An unknown error occurred when testing the connection', {
cause: error as Error,
})
}
}

Expand Down
Loading