Skip to content

Commit 05eabcc

Browse files
committed
move check into its own function
1 parent 0999351 commit 05eabcc

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,18 @@ export class SshKeyPair {
3333
return new SshKeyPair(keyPath, lifetime)
3434
}
3535

36-
public static async generateSshKeyPair(keyPath: string): Promise<void> {
37-
const keyGenerated = await this.tryKeyTypes(keyPath, ['ed25519', 'rsa'])
38-
if (!keyGenerated || !(await fs.existsFile(keyPath))) {
39-
throw new ToolkitError('ec2: Unable to generate ssh key pair')
36+
private static async assertGenerated(keyPath: string, keyGenerated: boolean): Promise<never | void> {
37+
if (!keyGenerated) {
38+
throw new ToolkitError('ec2: Unable to generate ssh key pair with either ed25519 or rsa')
39+
}
40+
if (!(await fs.exists(keyPath))) {
41+
throw new ToolkitError(`ec2: Failed to generate keys, resulting key not found at ${keyPath}`)
4042
}
43+
}
44+
45+
public static async generateSshKeyPair(keyPath: string): Promise<void> {
46+
const keyGenerated = await SshKeyPair.tryKeyTypes(keyPath, ['ed25519', 'rsa'])
47+
await SshKeyPair.assertGenerated(keyPath, keyGenerated)
4148
// Should already be the case, but just in case we assert permissions.
4249
// skip on Windows since it only allows write permission to be changed.
4350
if (!globals.isWeb && os.platform() !== 'win32') {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,14 @@ describe('Ec2ConnectClient', function () {
155155
instanceId: 'test-id',
156156
region: 'test-region',
157157
}
158-
const keyPath = path.join((await createTestWorkspaceFolder()).uri.path, 'keys')
158+
const testWorkspaceFolder = await createTestWorkspaceFolder()
159+
const keyPath = path.join(testWorkspaceFolder.uri.path, 'keys')
159160
const keys = await SshKeyPair.getSshKeyPair(keyPath, 30000)
160161
await client.sendSshKeyToInstance(testSelection, keys, 'test-user')
161162
const privKey = await fs.readFileText(keys.getPrivateKeyPath())
162163
assertNoTelemetryMatch(privKey)
163164
sinon.restore()
164-
await keys.delete()
165+
await fs.delete(testWorkspaceFolder.uri)
165166
})
166167
})
167168

0 commit comments

Comments
 (0)