Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 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
14 changes: 8 additions & 6 deletions packages/core/src/awsService/ec2/sshKeyPair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ToolkitError } from '../../shared/errors'
import { tryRun } from '../../shared/utilities/pathFind'
import { Timeout } from '../../shared/utilities/timeoutUtils'
import { findAsync } from '../../shared/utilities/collectionUtils'
import { RunParameterContext } from '../../shared/utilities/processUtils'

type sshKeyType = 'rsa' | 'ed25519'

Expand All @@ -17,7 +18,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 +30,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 +47,12 @@ 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 (_t: string, context: RunParameterContext) => {
await context.send('yes')
}
return !(await tryRun('ssh-keygen', ['-t', keyType, '-N', '', '-q', '-f', keyPath], 'yes', 'unknown key type', {
onStdout: overrideKeys,
}))
}

public static async tryKeyTypes(keyPath: string, keyTypes: sshKeyType[]): Promise<boolean> {
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/shared/utilities/pathFind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as vscode from 'vscode'
import * as path from 'path'
import fs from '../../shared/fs/fs'
import { ChildProcess } from './processUtils'
import { ChildProcess, ChildProcessOptions } from './processUtils'
import { GitExtension } from '../extensions/git'
import { Settings } from '../settings'
import { getLogger } from '../logger/logger'
Expand All @@ -30,10 +30,11 @@ export async function tryRun(
p: string,
args: string[],
logging: 'yes' | 'no' | 'noresult' = 'yes',
expected?: string
expected?: string,
opt?: ChildProcessOptions
): Promise<boolean> {
const proc = new ChildProcess(p, args, { logging: 'no' })
const r = await proc.run()
const r = await proc.run(opt)
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
5 changes: 4 additions & 1 deletion packages/core/src/shared/utilities/processUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import * as logger from '../logger'
import { Timeout, CancellationError, waitUntil } from './timeoutUtils'
import { isWeb } from '../extensionGlobals'

interface RunParameterContext {
export interface RunParameterContext {
/** Reports an error parsed from the stdin/stdout streams. */
reportError(err: string | Error): void
/** Attempts to stop the running process. See {@link ChildProcess.stop}. */
stop(force?: boolean, signal?: string): void
/** Send string to stdin */
send(text: string): Promise<void>
/** The active `Timeout` object (if applicable). */
readonly timeout?: Timeout
/** The logger being used by the process. */
Expand Down Expand Up @@ -161,6 +163,7 @@ export class ChildProcess {
timeout,
logger: this.#log,
stop: this.stop.bind(this),
send: this.send.bind(this),
reportError: (err) => errorHandler(err instanceof Error ? err : new Error(err)),
}

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