5
5
import assert from 'assert'
6
6
import nodefs from 'fs' // eslint-disable-line no-restricted-imports
7
7
import * as sinon from 'sinon'
8
- import * as path from 'path'
9
8
import * as os from 'os'
10
9
import { SshKeyPair } from '../../../awsService/ec2/sshKeyPair'
11
- import { createTestWorkspaceFolder , installFakeClock } from '../../testUtil'
10
+ import { installFakeClock } from '../../testUtil'
12
11
import { InstalledClock } from '@sinonjs/fake-timers'
13
12
import { ChildProcess } from '../../../shared/utilities/processUtils'
14
13
import { fs , globals } from '../../../shared'
15
14
16
15
describe ( 'SshKeyPair' , async function ( ) {
17
- let temporaryDirectory : string
18
- let keyPath : string
19
- let keyPair : SshKeyPair
20
16
let clock : InstalledClock
17
+ let keyPair : SshKeyPair
18
+ let keyName : string
21
19
22
20
before ( async function ( ) {
23
- temporaryDirectory = ( await createTestWorkspaceFolder ( ) ) . uri . fsPath
24
- keyPath = path . join ( temporaryDirectory , 'testKeyPair' )
25
21
clock = installFakeClock ( )
26
22
} )
27
23
28
24
beforeEach ( async function ( ) {
29
- keyPair = await SshKeyPair . getSshKeyPair ( keyPath , 30000 )
25
+ keyName = 'testKeyPair'
26
+ keyPair = await SshKeyPair . getSshKeyPair ( keyName , 30000 )
30
27
} )
31
28
32
29
afterEach ( async function ( ) {
33
30
await keyPair . delete ( )
34
31
} )
35
32
36
33
after ( async function ( ) {
37
- await fs . delete ( temporaryDirectory , { recursive : true } )
38
34
clock . uninstall ( )
39
35
sinon . restore ( )
40
36
} )
41
37
42
38
it ( 'generates key in target file' , async function ( ) {
43
- const contents = await fs . readFileBytes ( keyPath )
39
+ const contents = await fs . readFileBytes ( keyPair . getPrivateKeyPath ( ) )
44
40
assert . notStrictEqual ( contents . length , 0 )
45
41
} )
46
42
47
43
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 ( )
52
49
} )
53
50
54
51
it ( 'sets permission of the file to read/write owner' , async function ( ) {
@@ -59,7 +56,7 @@ describe('SshKeyPair', async function () {
59
56
} )
60
57
61
58
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 ( ) ] )
63
60
const result = await process . run ( )
64
61
// Check private key header for algorithm name
65
62
assert . strictEqual ( result . stdout . includes ( '[ED25519 256]' ) , true )
@@ -70,29 +67,25 @@ describe('SshKeyPair', async function () {
70
67
const stub = sinon . stub ( SshKeyPair , 'tryKeyGen' )
71
68
stub . onFirstCall ( ) . resolves ( false )
72
69
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 ( ) ] )
75
72
const result = await process . run ( )
76
73
// Check private key header for algorithm name
77
74
assert . strictEqual ( result . stdout . includes ( '[RSA' ) , true )
78
75
stub . restore ( )
79
76
} )
80
77
81
- it ( 'properly names the public key' , function ( ) {
82
- assert . strictEqual ( keyPair . getPublicKeyPath ( ) , `${ keyPath } .pub` )
83
- } )
84
-
85
78
it ( 'reads in public ssh key that is non-empty' , async function ( ) {
86
79
const key = await keyPair . getPublicKey ( )
87
80
assert . notStrictEqual ( key . length , 0 )
88
81
} )
89
82
90
83
it ( 'does overwrite existing keys on get call' , async function ( ) {
91
84
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 )
94
87
95
- const keyAfter = await fs . readFileBytes ( keyPath )
88
+ const keyAfter = await fs . readFileBytes ( keyPair . getPrivateKeyPath ( ) )
96
89
sinon . assert . calledOnce ( generateStub )
97
90
98
91
assert . notStrictEqual ( keyBefore , keyAfter )
@@ -118,7 +111,7 @@ describe('SshKeyPair', async function () {
118
111
sinon . stub ( SshKeyPair , 'generateSshKeyPair' )
119
112
const deleteStub = sinon . stub ( SshKeyPair . prototype , 'delete' )
120
113
121
- keyPair = await SshKeyPair . getSshKeyPair ( keyPath , 50 )
114
+ keyPair = await SshKeyPair . getSshKeyPair ( keyName , 50 )
122
115
await clock . tickAsync ( 10 )
123
116
sinon . assert . notCalled ( deleteStub )
124
117
await clock . tickAsync ( 100 )
0 commit comments