@@ -7,41 +7,124 @@ import * as assert from 'assert'
7
7
import * as sinon from 'sinon'
8
8
import { Ec2ConnectionManager } from '../../ec2/model'
9
9
import { SsmClient } from '../../shared/clients/ssmClient'
10
+ import { Ec2Client } from '../../shared/clients/ec2Client'
10
11
import { Ec2Selection } from '../../ec2/prompter'
11
12
import { ToolkitError } from '../../shared/errors'
12
13
import { IAM } from 'aws-sdk'
14
+ import { SshKeyPair } from '../../ec2/sshKeyPair'
13
15
import { DefaultIamClient } from '../../shared/clients/iamClient'
14
16
15
17
describe ( 'Ec2ConnectClient' , function ( ) {
18
+ let client : Ec2ConnectionManager
19
+
20
+ before ( function ( ) {
21
+ client = new Ec2ConnectionManager ( 'test-region' )
22
+ } )
23
+
24
+ describe ( 'getAttachedIamRole' , async function ( ) {
25
+ it ( 'only returns role if recieves ARN from instance profile' , async function ( ) {
26
+ let role : IAM . Role | undefined
27
+ const getInstanceProfileStub = sinon . stub ( Ec2Client . prototype , 'getAttachedIamInstanceProfile' )
28
+
29
+ getInstanceProfileStub . resolves ( { Arn : 'thisIsAnArn' } )
30
+ sinon
31
+ . stub ( DefaultIamClient . prototype , 'getIAMRoleFromInstanceProfile' )
32
+ . resolves ( { Arn : 'ThisIsARoleArn' } as IAM . Role )
33
+
34
+ role = await client . getAttachedIamRole ( 'test-instance' )
35
+ assert . ok ( role )
36
+ assert . ok ( role . Arn )
37
+
38
+ getInstanceProfileStub . resolves ( { } )
39
+ role = await client . getAttachedIamRole ( 'test-instance' )
40
+ assert . strictEqual ( role , undefined )
41
+ sinon . restore ( )
42
+ } )
43
+ } )
44
+
45
+ describe ( 'hasProperPolicies' , async function ( ) {
46
+ it ( 'correctly determines if proper policies are included' , async function ( ) {
47
+ async function assertAcceptsPolicies ( policies : IAM . Policy [ ] , expectedResult : boolean ) {
48
+ sinon . stub ( DefaultIamClient . prototype , 'listAttachedRolePolicies' ) . resolves ( policies )
49
+
50
+ const result = await client . hasProperPolicies ( '' )
51
+ assert . strictEqual ( result , expectedResult )
52
+
53
+ sinon . restore ( )
54
+ }
55
+ await assertAcceptsPolicies (
56
+ [ { PolicyName : 'name' } , { PolicyName : 'name2' } , { PolicyName : 'name3' } ] ,
57
+ false
58
+ )
59
+ await assertAcceptsPolicies (
60
+ [
61
+ { PolicyName : 'AmazonSSMManagedInstanceCore' } ,
62
+ { PolicyName : 'AmazonSSMManagedEC2InstanceDefaultPolicy' } ,
63
+ ] ,
64
+ true
65
+ )
66
+ await assertAcceptsPolicies ( [ { PolicyName : 'AmazonSSMManagedEC2InstanceDefaultPolicy' } ] , false )
67
+ await assertAcceptsPolicies ( [ { PolicyName : 'AmazonSSMManagedEC2InstanceDefaultPolicy' } ] , false )
68
+ } )
69
+
70
+ it ( 'throws error when sdk throws error' , async function ( ) {
71
+ sinon . stub ( DefaultIamClient . prototype , 'listAttachedRolePolicies' ) . throws ( new ToolkitError ( 'error' ) )
72
+
73
+ try {
74
+ await client . hasProperPolicies ( '' )
75
+ assert . ok ( false )
76
+ } catch {
77
+ assert . ok ( true )
78
+ }
79
+
80
+ sinon . restore ( )
81
+ } )
82
+ } )
83
+
84
+ describe ( 'isInstanceRunning' , async function ( ) {
85
+ it ( 'only returns true with the instance is running' , async function ( ) {
86
+ sinon . stub ( Ec2Client . prototype , 'getInstanceStatus' ) . callsFake ( async ( input : string ) => input . split ( ':' ) [ 0 ] )
87
+
88
+ const actualFirstResult = await client . isInstanceRunning ( 'running:instance' )
89
+ const actualSecondResult = await client . isInstanceRunning ( 'stopped:instance' )
90
+
91
+ assert . strictEqual ( true , actualFirstResult )
92
+ assert . strictEqual ( false , actualSecondResult )
93
+ sinon . restore ( )
94
+ } )
95
+ } )
96
+
16
97
describe ( 'handleStartSessionError' , async function ( ) {
17
- let client : Ec2ConnectionManager
18
98
let instanceSelection : Ec2Selection
19
99
20
100
before ( function ( ) {
21
- client = new Ec2ConnectionManager ( 'test-region' )
22
101
instanceSelection = { instanceId : 'testInstance' , region : 'testRegion' }
23
102
} )
24
103
25
104
it ( 'throws EC2SSMStatus error if instance is not running' , async function ( ) {
26
105
sinon . stub ( Ec2ConnectionManager . prototype , 'isInstanceRunning' ) . resolves ( false )
106
+
27
107
try {
28
108
await client . checkForStartSessionError ( instanceSelection )
29
109
assert . ok ( false )
30
110
} catch ( err ) {
31
111
assert . strictEqual ( ( err as ToolkitError ) . code , 'EC2SSMStatus' )
32
112
}
113
+
33
114
sinon . restore ( )
34
115
} )
35
116
36
117
it ( 'throws EC2SSMPermission error if instance is running but has no role' , async function ( ) {
37
118
sinon . stub ( Ec2ConnectionManager . prototype , 'isInstanceRunning' ) . resolves ( true )
38
119
sinon . stub ( Ec2ConnectionManager . prototype , 'getAttachedIamRole' ) . resolves ( undefined )
120
+
39
121
try {
40
122
await client . checkForStartSessionError ( instanceSelection )
41
123
assert . ok ( false )
42
124
} catch ( err ) {
43
125
assert . strictEqual ( ( err as ToolkitError ) . code , 'EC2SSMPermission' )
44
126
}
127
+
45
128
sinon . restore ( )
46
129
} )
47
130
@@ -50,12 +133,14 @@ describe('Ec2ConnectClient', function () {
50
133
sinon . stub ( Ec2ConnectionManager . prototype , 'getAttachedIamRole' ) . resolves ( { Arn : 'testRole' } as IAM . Role )
51
134
sinon . stub ( Ec2ConnectionManager . prototype , 'hasProperPolicies' ) . resolves ( true )
52
135
sinon . stub ( SsmClient . prototype , 'getInstanceAgentPingStatus' ) . resolves ( 'offline' )
136
+
53
137
try {
54
138
await client . checkForStartSessionError ( instanceSelection )
55
139
assert . ok ( false )
56
140
} catch ( err ) {
57
141
assert . strictEqual ( ( err as ToolkitError ) . code , 'EC2SSMAgentStatus' )
58
142
}
143
+
59
144
sinon . restore ( )
60
145
} )
61
146
@@ -64,49 +149,62 @@ describe('Ec2ConnectClient', function () {
64
149
sinon . stub ( Ec2ConnectionManager . prototype , 'getAttachedIamRole' ) . resolves ( { Arn : 'testRole' } as IAM . Role )
65
150
sinon . stub ( Ec2ConnectionManager . prototype , 'hasProperPolicies' ) . resolves ( true )
66
151
sinon . stub ( SsmClient . prototype , 'getInstanceAgentPingStatus' ) . resolves ( 'Online' )
152
+
67
153
assert . doesNotThrow ( async ( ) => await client . checkForStartSessionError ( instanceSelection ) )
154
+
68
155
sinon . restore ( )
69
156
} )
70
157
} )
71
158
72
- describe ( 'hasProperPolicies' , async function ( ) {
73
- let realClient : Ec2ConnectionManager
159
+ describe ( 'sendSshKeysToInstance' , async function ( ) {
160
+ it ( 'calls the sdk with the proper parameters' , async function ( ) {
161
+ const sendCommandStub = sinon . stub ( SsmClient . prototype , 'sendCommandAndWait' )
162
+
163
+ sinon . stub ( SshKeyPair , 'generateSshKeyPair' )
164
+ sinon . stub ( SshKeyPair . prototype , 'getPublicKey' ) . resolves ( 'test-key' )
165
+
166
+ const testSelection = {
167
+ instanceId : 'test-id' ,
168
+ region : 'test-region' ,
169
+ }
170
+ const mockKeys = await SshKeyPair . getSshKeyPair ( '' )
171
+ await client . sendSshKeyToInstance ( testSelection , mockKeys , '' )
172
+ sinon . assert . calledWith ( sendCommandStub , testSelection . instanceId , 'AWS-RunShellScript' )
173
+ sinon . restore ( )
174
+ } )
175
+ } )
176
+
177
+ describe ( 'getRemoteUser' , async function ( ) {
178
+ let getTargetPlatformNameStub : sinon . SinonStub < [ target : string ] , Promise < string > >
74
179
75
180
before ( async function ( ) {
76
- realClient = new Ec2ConnectionManager ( 'test-region ')
181
+ getTargetPlatformNameStub = sinon . stub ( SsmClient . prototype , 'getTargetPlatformName ')
77
182
} )
78
183
79
- it ( 'correctly determines if proper policies are included' , async function ( ) {
80
- async function assertAcceptsPolicies ( policies : IAM . Policy [ ] , expectedResult : boolean ) {
81
- sinon . stub ( DefaultIamClient . prototype , 'listAttachedRolePolicies' ) . resolves ( policies )
82
- const result = await realClient . hasProperPolicies ( '' )
83
- assert . strictEqual ( result , expectedResult )
84
- sinon . restore ( )
85
- }
86
- await assertAcceptsPolicies (
87
- [ { PolicyName : 'name' } , { PolicyName : 'name2' } , { PolicyName : 'name3' } ] ,
88
- false
89
- )
90
- await assertAcceptsPolicies (
91
- [
92
- { PolicyName : 'AmazonSSMManagedInstanceCore' } ,
93
- { PolicyName : 'AmazonSSMManagedEC2InstanceDefaultPolicy' } ,
94
- ] ,
95
- true
96
- )
97
- await assertAcceptsPolicies ( [ { PolicyName : 'AmazonSSMManagedEC2InstanceDefaultPolicy' } ] , false )
98
- await assertAcceptsPolicies ( [ { PolicyName : 'AmazonSSMManagedEC2InstanceDefaultPolicy' } ] , false )
184
+ after ( async function ( ) {
185
+ sinon . restore ( )
99
186
} )
100
187
101
- it ( 'throws error when sdk throws error' , async function ( ) {
102
- sinon . stub ( DefaultIamClient . prototype , 'listAttachedRolePolicies' ) . throws ( new ToolkitError ( 'error' ) )
188
+ it ( 'identifies the user for ubuntu as ubuntu' , async function ( ) {
189
+ getTargetPlatformNameStub . resolves ( 'Ubuntu' )
190
+ const remoteUser = await client . getRemoteUser ( 'testInstance' )
191
+ assert . strictEqual ( remoteUser , 'ubuntu' )
192
+ } )
193
+
194
+ it ( 'identifies the user for amazon linux as ec2-user' , async function ( ) {
195
+ getTargetPlatformNameStub . resolves ( 'Amazon Linux' )
196
+ const remoteUser = await client . getRemoteUser ( 'testInstance' )
197
+ assert . strictEqual ( remoteUser , 'ec2-user' )
198
+ } )
199
+
200
+ it ( 'throws error when not given known OS' , async function ( ) {
201
+ getTargetPlatformNameStub . resolves ( 'ThisIsNotARealOs!' )
103
202
try {
104
- await realClient . hasProperPolicies ( ' ')
203
+ await client . getRemoteUser ( 'testInstance ')
105
204
assert . ok ( false )
106
- } catch {
205
+ } catch ( exception ) {
107
206
assert . ok ( true )
108
207
}
109
- sinon . restore ( )
110
208
} )
111
209
} )
112
210
} )
0 commit comments