55import assert from 'assert'
66import nodefs from 'fs' // eslint-disable-line no-restricted-imports
77import * as sinon from 'sinon'
8- import * as path from 'path'
98import * as os from 'os'
109import { SshKeyPair } from '../../../awsService/ec2/sshKeyPair'
11- import { createTestWorkspaceFolder , installFakeClock } from '../../testUtil'
10+ import { installFakeClock } from '../../testUtil'
1211import { InstalledClock } from '@sinonjs/fake-timers'
1312import { ChildProcess } from '../../../shared/utilities/processUtils'
1413import { fs , globals } from '../../../shared'
1514
1615describe ( 'SshKeyPair' , async function ( ) {
17- let temporaryDirectory : string
18- let keyPath : string
19- let keyPair : SshKeyPair
2016 let clock : InstalledClock
17+ let keyPair : SshKeyPair
18+ let keyName : string
2119
2220 before ( async function ( ) {
23- temporaryDirectory = ( await createTestWorkspaceFolder ( ) ) . uri . fsPath
24- keyPath = path . join ( temporaryDirectory , 'testKeyPair' )
2521 clock = installFakeClock ( )
2622 } )
2723
2824 beforeEach ( async function ( ) {
29- keyPair = await SshKeyPair . getSshKeyPair ( keyPath , 30000 )
25+ keyName = 'testKeyPair'
26+ keyPair = await SshKeyPair . getSshKeyPair ( keyName , 30000 )
3027 } )
3128
3229 afterEach ( async function ( ) {
3330 await keyPair . delete ( )
3431 } )
3532
3633 after ( async function ( ) {
37- await fs . delete ( temporaryDirectory , { recursive : true } )
3834 clock . uninstall ( )
3935 sinon . restore ( )
4036 } )
4137
4238 it ( 'generates key in target file' , async function ( ) {
43- const contents = await fs . readFileBytes ( keyPath )
39+ const contents = await fs . readFileBytes ( keyPair . getPrivateKeyPath ( ) )
4440 assert . notStrictEqual ( contents . length , 0 )
4541 } )
4642
4743 it ( 'generates unique key each time' , async function ( ) {
48- const beforeContent = await fs . readFileBytes ( keyPath )
49- keyPair = await SshKeyPair . getSshKeyPair ( keyPath , 30000 )
50- const afterContent = await fs . readFileBytes ( keyPath )
51- 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 )
48+ await keyPair2 . delete ( )
5249 } )
5350
5451 it ( 'sets permission of the file to read/write owner' , async function ( ) {
@@ -59,7 +56,7 @@ describe('SshKeyPair', async function () {
5956 } )
6057
6158 it ( 'defaults to ed25519 key type' , async function ( ) {
62- const process = new ChildProcess ( `ssh-keygen` , [ '-vvv' , '-l' , '-f' , keyPath ] )
59+ const process = new ChildProcess ( `ssh-keygen` , [ '-vvv' , '-l' , '-f' , keyPair . getPrivateKeyPath ( ) ] )
6360 const result = await process . run ( )
6461 // Check private key header for algorithm name
6562 assert . strictEqual ( result . stdout . includes ( '[ED25519 256]' ) , true )
@@ -70,29 +67,25 @@ describe('SshKeyPair', async function () {
7067 const stub = sinon . stub ( SshKeyPair , 'tryKeyGen' )
7168 stub . onFirstCall ( ) . resolves ( false )
7269 stub . callThrough ( )
73- keyPair = await SshKeyPair . getSshKeyPair ( keyPath , 30000 )
74- const process = new ChildProcess ( `ssh-keygen` , [ '-vvv' , '-l' , '-f' , keyPath ] )
70+ const rsaKey = await SshKeyPair . getSshKeyPair ( 'rsa' , 30000 )
71+ const process = new ChildProcess ( `ssh-keygen` , [ '-vvv' , '-l' , '-f' , rsaKey . getPrivateKeyPath ( ) ] )
7572 const result = await process . run ( )
7673 // Check private key header for algorithm name
7774 assert . strictEqual ( result . stdout . includes ( '[RSA' ) , true )
7875 stub . restore ( )
7976 } )
8077
81- it ( 'properly names the public key' , function ( ) {
82- assert . strictEqual ( keyPair . getPublicKeyPath ( ) , `${ keyPath } .pub` )
83- } )
84-
8578 it ( 'reads in public ssh key that is non-empty' , async function ( ) {
8679 const key = await keyPair . getPublicKey ( )
8780 assert . notStrictEqual ( key . length , 0 )
8881 } )
8982
9083 it ( 'does overwrite existing keys on get call' , async function ( ) {
9184 const generateStub = sinon . spy ( SshKeyPair , 'generateSshKeyPair' )
92- const keyBefore = await fs . readFileBytes ( keyPath )
93- keyPair = await SshKeyPair . getSshKeyPair ( keyPath , 30000 )
85+ const keyBefore = await fs . readFileBytes ( keyPair . getPrivateKeyPath ( ) )
86+ keyPair = await SshKeyPair . getSshKeyPair ( keyName , 30000 )
9487
95- const keyAfter = await fs . readFileBytes ( keyPath )
88+ const keyAfter = await fs . readFileBytes ( keyPair . getPrivateKeyPath ( ) )
9689 sinon . assert . calledOnce ( generateStub )
9790
9891 assert . notStrictEqual ( keyBefore , keyAfter )
@@ -118,7 +111,7 @@ describe('SshKeyPair', async function () {
118111 sinon . stub ( SshKeyPair , 'generateSshKeyPair' )
119112 const deleteStub = sinon . stub ( SshKeyPair . prototype , 'delete' )
120113
121- keyPair = await SshKeyPair . getSshKeyPair ( keyPath , 50 )
114+ keyPair = await SshKeyPair . getSshKeyPair ( keyName , 50 )
122115 await clock . tickAsync ( 10 )
123116 sinon . assert . notCalled ( deleteStub )
124117 await clock . tickAsync ( 100 )
0 commit comments