22 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33 * SPDX-License-Identifier: Apache-2.0
44 */
5- import * as vscode from 'vscode'
65import assert from 'assert'
76import nodefs from 'fs' // eslint-disable-line no-restricted-imports
87import * as sinon from 'sinon'
9- import * as path from 'path'
108import * as os from 'os'
119import { SshKeyPair } from '../../../awsService/ec2/sshKeyPair'
1210import { installFakeClock } from '../../testUtil'
@@ -15,44 +13,38 @@ import { ChildProcess } from '../../../shared/utilities/processUtils'
1513import { fs , globals } from '../../../shared'
1614
1715describe ( 'SshKeyUtility' , async function ( ) {
18- let temporaryDirectory : string
19- let keyPath : string
20- let keyPair : SshKeyPair
2116 let clock : InstalledClock
17+ let keyPair : SshKeyPair
18+ let keyName : string
2219
2320 before ( async function ( ) {
24- // Setup a temporary directory inside of globalStorage since keys need to be inside globalStorage
25- temporaryDirectory = path . join ( globals . context . globalStorageUri . fsPath , 'SshKeyUtilityTests' )
26- await fs . mkdir ( temporaryDirectory )
27-
28- keyPath = path . join ( temporaryDirectory , 'testKeyPair' )
2921 clock = installFakeClock ( )
3022 } )
3123
3224 beforeEach ( async function ( ) {
33- keyPair = await SshKeyPair . getSshKeyPair ( keyPath , 30000 )
25+ keyName = 'testKeyPair'
26+ keyPair = await SshKeyPair . getSshKeyPair ( keyName , 30000 )
3427 } )
3528
3629 afterEach ( async function ( ) {
3730 await keyPair . delete ( )
3831 } )
3932
4033 after ( async function ( ) {
41- await fs . delete ( temporaryDirectory , { recursive : true } )
4234 clock . uninstall ( )
4335 sinon . restore ( )
4436 } )
4537
4638 it ( 'generates key in target file' , async function ( ) {
47- const contents = await fs . readFileBytes ( vscode . Uri . file ( keyPath ) )
39+ const contents = await fs . readFileBytes ( keyPair . getPrivateKeyPath ( ) )
4840 assert . notStrictEqual ( contents . length , 0 )
4941 } )
5042
5143 it ( 'generates unique key each time' , async function ( ) {
52- const beforeContent = await fs . readFileBytes ( vscode . Uri . file ( keyPath ) )
53- keyPair = await SshKeyPair . getSshKeyPair ( keyPath , 30000 )
54- const afterContent = await fs . readFileBytes ( vscode . Uri . file ( keyPath ) )
55- assert . notStrictEqual ( beforeContent , afterContent )
44+ const keyPair2 = await SshKeyPair . getSshKeyPair ( ` ${ keyName } 2` , 30000 )
45+ const content1 = await fs . readFileBytes ( keyPair2 . getPrivateKeyPath ( ) )
46+ const content2 = await fs . readFileBytes ( keyPair . getPrivateKeyPath ( ) )
47+ assert . notStrictEqual ( content1 , content2 )
5648 } )
5749
5850 it ( 'sets permission of the file to read/write owner' , async function ( ) {
@@ -63,7 +55,7 @@ describe('SshKeyUtility', async function () {
6355 } )
6456
6557 it ( 'defaults to ed25519 key type' , async function ( ) {
66- const process = new ChildProcess ( `ssh-keygen` , [ '-vvv' , '-l' , '-f' , keyPath ] )
58+ const process = new ChildProcess ( `ssh-keygen` , [ '-vvv' , '-l' , '-f' , keyPair . getPrivateKeyPath ( ) ] )
6759 const result = await process . run ( )
6860 // Check private key header for algorithm name
6961 assert . strictEqual ( result . stdout . includes ( '[ED25519 256]' ) , true )
@@ -74,29 +66,25 @@ describe('SshKeyUtility', async function () {
7466 const stub = sinon . stub ( SshKeyPair , 'tryKeyGen' )
7567 stub . onFirstCall ( ) . resolves ( false )
7668 stub . callThrough ( )
77- keyPair = await SshKeyPair . getSshKeyPair ( keyPath , 30000 )
78- const process = new ChildProcess ( `ssh-keygen` , [ '-vvv' , '-l' , '-f' , keyPath ] )
69+ const rsaKey = await SshKeyPair . getSshKeyPair ( 'rsa' , 30000 )
70+ const process = new ChildProcess ( `ssh-keygen` , [ '-vvv' , '-l' , '-f' , rsaKey . getPrivateKeyPath ( ) ] )
7971 const result = await process . run ( )
8072 // Check private key header for algorithm name
8173 assert . strictEqual ( result . stdout . includes ( '[RSA' ) , true )
8274 stub . restore ( )
8375 } )
8476
85- it ( 'properly names the public key' , function ( ) {
86- assert . strictEqual ( keyPair . getPublicKeyPath ( ) , `${ keyPath } .pub` )
87- } )
88-
8977 it ( 'reads in public ssh key that is non-empty' , async function ( ) {
9078 const key = await keyPair . getPublicKey ( )
9179 assert . notStrictEqual ( key . length , 0 )
9280 } )
9381
9482 it ( 'does overwrite existing keys on get call' , async function ( ) {
9583 const generateStub = sinon . spy ( SshKeyPair , 'generateSshKeyPair' )
96- const keyBefore = await fs . readFileBytes ( vscode . Uri . file ( keyPath ) )
97- keyPair = await SshKeyPair . getSshKeyPair ( keyPath , 30000 )
84+ const keyBefore = await fs . readFileBytes ( keyPair . getPrivateKeyPath ( ) )
85+ keyPair = await SshKeyPair . getSshKeyPair ( keyName , 30000 )
9886
99- const keyAfter = await fs . readFileBytes ( vscode . Uri . file ( keyPath ) )
87+ const keyAfter = await fs . readFileBytes ( keyPair . getPrivateKeyPath ( ) )
10088 sinon . assert . calledOnce ( generateStub )
10189
10290 assert . notStrictEqual ( keyBefore , keyAfter )
@@ -122,7 +110,7 @@ describe('SshKeyUtility', async function () {
122110 sinon . stub ( SshKeyPair , 'generateSshKeyPair' )
123111 const deleteStub = sinon . stub ( SshKeyPair . prototype , 'delete' )
124112
125- keyPair = await SshKeyPair . getSshKeyPair ( keyPath , 50 )
113+ keyPair = await SshKeyPair . getSshKeyPair ( keyName , 50 )
126114 await clock . tickAsync ( 10 )
127115 sinon . assert . notCalled ( deleteStub )
128116 await clock . tickAsync ( 100 )
0 commit comments