Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
17 changes: 11 additions & 6 deletions packages/core/src/awsService/ec2/sshKeyPair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class SshKeyPair {
private deleted: boolean = false

private constructor(
private keyPath: string,
private readonly keyPath: string,
lifetime: number
) {
this.publicKeyPath = `${keyPath}.pub`
Expand All @@ -29,10 +29,6 @@ export class SshKeyPair {
}

public static async getSshKeyPair(keyPath: string, lifetime: number) {
// Overwrite key if already exists
if (await fs.existsFile(keyPath)) {
await fs.delete(keyPath)
}
await SshKeyPair.generateSshKeyPair(keyPath)
return new SshKeyPair(keyPath, lifetime)
}
Expand All @@ -50,7 +46,16 @@ export class SshKeyPair {
* @param keyType type of key to generate.
*/
public static async tryKeyGen(keyPath: string, keyType: sshKeyType): Promise<boolean> {
return !(await tryRun('ssh-keygen', ['-t', keyType, '-N', '', '-q', '-f', keyPath], 'yes', 'unknown key type'))
const overrideKeys = async (proc: any, _t: string) => {
await proc.send('yes')
}
return !(await tryRun(
'ssh-keygen',
['-t', keyType, '-N', '', '-q', '-f', keyPath],
'yes',
'unknown key type',
overrideKeys
))
}

public static async tryKeyTypes(keyPath: string, keyTypes: sshKeyType[]): Promise<boolean> {
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/shared/utilities/pathFind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ export async function tryRun(
p: string,
args: string[],
logging: 'yes' | 'no' | 'noresult' = 'yes',
expected?: string
expected?: string,
onStdout?: (proc: ChildProcess, text: string) => void
): Promise<boolean> {
const proc = new ChildProcess(p, args, { logging: 'no' })
const r = await proc.run()
const options = onStdout ? { onStdout: (text: string) => onStdout(proc, text) } : {}
const r = await proc.run(options)
const ok = r.exitCode === 0 && (expected === undefined || r.stdout.includes(expected))
if (logging === 'noresult') {
getLogger().info('tryRun: %s: %s', ok ? 'ok' : 'failed', proc)
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/test/awsService/ec2/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ describe('Ec2ConnectClient', function () {
instanceId: 'test-id',
region: 'test-region',
}
const mockKeys = await SshKeyPair.getSshKeyPair('', 30000)
await client.sendSshKeyToInstance(testSelection, mockKeys, '')
const mockKeys = await SshKeyPair.getSshKeyPair('fakeDir', 30000)
await client.sendSshKeyToInstance(testSelection, mockKeys, 'test-user')
sinon.assert.calledWith(sendCommandStub, testSelection.instanceId, 'AWS-RunShellScript')
sinon.restore()
})
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/test/awsService/ec2/sshKeyPair.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('SshKeyUtility', async function () {
})

after(async function () {
await keyPair.delete()
await fs.delete(temporaryDirectory, { recursive: true, force: true })
clock.uninstall()
sinon.restore()
})
Expand Down