Skip to content

Commit f75a2be

Browse files
refactor(ec2): improve error information in telemetry. (aws#6624)
## Problem Within telemetry, there are a large group of fatal errors within EC2 Connect being grouped together under the same general error code. Additionally, there is very little information in the error message to determine the source of this error. Specifically, it appears the the test SSM connection done before attempting to pass the connection to VS Code, implemented [here](https://github.com/aws/aws-toolkit-vscode/blob/b9af56c3097242fb796995479f864d95098bf713/packages/core/src/shared/extensions/ssh.ts#L133-L155), can fail, and gives us a general error code with little information about its root cause. ## Solution - Add a specific error code for this connection type. (reused an old unused one) - Pipe the error from the child process into the error message so that we get it in telemetry. - Add a docstring to test connect function, since its not obvious whats happening as it involves both ssh and ssm. Example Error: <img width="465" alt="image" src="https://github.com/user-attachments/assets/8b27746d-9291-48a4-a125-dcea761a2c13" /> --- - Treat all work as PUBLIC. Private `feature/x` branches will not be squash-merged at release time. - Your code changes must meet the guidelines in [CONTRIBUTING.md](https://github.com/aws/aws-toolkit-vscode/blob/master/CONTRIBUTING.md#guidelines). - License: I confirm that my contribution is made under the terms of the Apache 2.0 license. --------- Co-authored-by: Justin M. Keyes <[email protected]>
1 parent f475a6c commit f75a2be

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

packages/core/src/awsService/ec2/model.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import { SshKeyPair } from './sshKeyPair'
3636
import { Ec2SessionTracker } from './remoteSessionManager'
3737
import { getEc2SsmEnv } from './utils'
3838

39-
export type Ec2ConnectErrorCode = 'EC2SSMStatus' | 'EC2SSMPermission' | 'EC2SSMConnect' | 'EC2SSMAgentStatus'
39+
export type Ec2ConnectErrorCode = 'EC2SSMStatus' | 'EC2SSMPermission' | 'EC2SSMTestConnect' | 'EC2SSMAgentStatus'
4040

4141
export interface Ec2RemoteEnv extends VscodeRemoteConnection {
4242
selection: Ec2Selection
@@ -221,8 +221,8 @@ export class Ec2Connecter implements vscode.Disposable {
221221
remoteUser.name
222222
)
223223
} catch (err) {
224-
const message = err instanceof SshError ? 'Testing SSH connection to instance failed' : ''
225-
this.throwConnectionError(message, selection, err as Error)
224+
const message = err instanceof SshError ? `Testing SSM connection to instance failed: ${err.message}` : ''
225+
this.throwConnectionError(message, selection, { ...(err as Error), code: 'EC2SSMTestConnect' })
226226
} finally {
227227
await this.ssmClient.terminateSession(testSession)
228228
}

packages/core/src/shared/extensions/ssh.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,27 +130,34 @@ export class RemoteSshSettings extends Settings.define('remote.SSH', remoteSshTy
130130
}
131131
}
132132

133+
/**
134+
* Test a SSH connection over SSM.
135+
* @param ProcessClass given process to test the connection within.
136+
* @param hostname
137+
* @param sshPath
138+
* @param user
139+
* @param session SSM session credentials. These cannot be reused, so it may be required to create a seperate session for the test connection.
140+
* @returns
141+
*/
133142
export async function testSshConnection(
134143
ProcessClass: typeof ChildProcess,
135144
hostname: string,
136145
sshPath: string,
137146
user: string,
138147
session: SSM.StartSessionResponse
139148
): Promise<ChildProcessResult | never> {
149+
const env = { SESSION_ID: session.SessionId, STREAM_URL: session.StreamUrl, TOKEN: session.TokenValue }
150+
const process = new ProcessClass(sshPath, ['-T', `${user}@${hostname}`, 'echo "test connection succeeded" && exit'])
140151
try {
141-
const env = { SESSION_ID: session.SessionId, STREAM_URL: session.StreamUrl, TOKEN: session.TokenValue }
142-
const result = await new ProcessClass(sshPath, [
143-
'-T',
144-
`${user}@${hostname}`,
145-
'echo "test connection succeeded" && exit',
146-
]).run({
152+
return await process.run({
147153
spawnOptions: {
148154
env,
149155
},
150156
})
151-
return result
152157
} catch (error) {
153-
throw new SshError('SSH connection test failed', { cause: error as Error })
158+
throw new SshError(process.result()?.stderr ?? 'An unknown error occurred when testing the connection', {
159+
cause: error as Error,
160+
})
154161
}
155162
}
156163

0 commit comments

Comments
 (0)