Skip to content

Commit f72acc7

Browse files
committed
implement new version using permissions
1 parent f9c5d91 commit f72acc7

File tree

2 files changed

+21
-49
lines changed

2 files changed

+21
-49
lines changed

src/ec2/model.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ import { isCloud9 } from '../shared/extensionUtilities'
1212
import { ToolkitError } from '../shared/errors'
1313
import { SsmClient } from '../shared/clients/ssmClient'
1414
import { Ec2Client } from '../shared/clients/ec2Client'
15-
import { VscodeRemoteConnection, ensureDependencies, openRemoteTerminal } from '../shared/remoteSession'
15+
import {
16+
VscodeRemoteConnection,
17+
ensureDependencies,
18+
getDeniedSsmActions,
19+
openRemoteTerminal,
20+
} from '../shared/remoteSession'
1621
import { DefaultIamClient } from '../shared/clients/iamClient'
1722
import { ErrorInformation } from '../shared/errors'
1823
import { sshAgentSocketVariable, startSshAgent, startVscodeRemote } from '../shared/extensions/ssh'
@@ -72,13 +77,19 @@ export class Ec2ConnectionManager {
7277
}
7378
}
7479

75-
public async hasProperPolicies(IamRoleArn: string): Promise<boolean> {
76-
const attachedPolicies = (await this.iamClient.listAttachedRolePolicies(IamRoleArn)).map(
77-
policy => policy.PolicyName!
78-
)
79-
const requiredPolicies = ['AmazonSSMManagedInstanceCore', 'AmazonSSMManagedEC2InstanceDefaultPolicy']
80+
// public async hasProperPolicies(IamRoleArn: string): Promise<boolean> {
81+
// const attachedPolicies = (await this.iamClient.listAttachedRolePolicies(IamRoleArn)).map(
82+
// policy => policy.PolicyName!
83+
// )
84+
// const requiredPolicies = ['AmazonSSMManagedInstanceCore', 'AmazonSSMManagedEC2InstanceDefaultPolicy']
8085

81-
return requiredPolicies.length !== 0 && requiredPolicies.every(policy => attachedPolicies.includes(policy))
86+
// return requiredPolicies.length !== 0 && requiredPolicies.every(policy => attachedPolicies.includes(policy))
87+
// }
88+
89+
public async hasProperPermissions(IamRoleArn: string): Promise<boolean> {
90+
const deniedActions = await getDeniedSsmActions(this.iamClient, IamRoleArn)
91+
92+
return deniedActions.length !== 0
8293
}
8394

8495
public async isInstanceRunning(instanceId: string): Promise<boolean> {
@@ -108,7 +119,7 @@ export class Ec2ConnectionManager {
108119
this.throwConnectionError(message, selection, { code: 'EC2SSMPermission' })
109120
}
110121

111-
const hasProperPolicies = await this.hasProperPolicies(IamRole!.Arn)
122+
const hasProperPolicies = await this.hasProperPermissions(IamRole!.Arn)
112123

113124
if (!hasProperPolicies) {
114125
const message = `Ensure an IAM role with the required policies is attached to the instance. Found attached role: ${

src/test/ec2/model.test.ts

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -43,45 +43,6 @@ describe('Ec2ConnectClient', function () {
4343
})
4444
})
4545

46-
describe('hasProperPolicies', async function () {
47-
it('correctly determines if proper policies are included', async function () {
48-
async function assertAcceptsPolicies(policies: IAM.Policy[], expectedResult: boolean) {
49-
sinon.stub(DefaultIamClient.prototype, 'listAttachedRolePolicies').resolves(policies)
50-
51-
const result = await client.hasProperPolicies('')
52-
assert.strictEqual(result, expectedResult)
53-
54-
sinon.restore()
55-
}
56-
await assertAcceptsPolicies(
57-
[{ PolicyName: 'name' }, { PolicyName: 'name2' }, { PolicyName: 'name3' }],
58-
false
59-
)
60-
await assertAcceptsPolicies(
61-
[
62-
{ PolicyName: 'AmazonSSMManagedInstanceCore' },
63-
{ PolicyName: 'AmazonSSMManagedEC2InstanceDefaultPolicy' },
64-
],
65-
true
66-
)
67-
await assertAcceptsPolicies([{ PolicyName: 'AmazonSSMManagedEC2InstanceDefaultPolicy' }], false)
68-
await assertAcceptsPolicies([{ PolicyName: 'AmazonSSMManagedEC2InstanceDefaultPolicy' }], false)
69-
})
70-
71-
it('throws error when sdk throws error', async function () {
72-
sinon.stub(DefaultIamClient.prototype, 'listAttachedRolePolicies').throws(new ToolkitError('error'))
73-
74-
try {
75-
await client.hasProperPolicies('')
76-
assert.ok(false)
77-
} catch {
78-
assert.ok(true)
79-
}
80-
81-
sinon.restore()
82-
})
83-
})
84-
8546
describe('isInstanceRunning', async function () {
8647
it('only returns true with the instance is running', async function () {
8748
sinon.stub(Ec2Client.prototype, 'getInstanceStatus').callsFake(async (input: string) => input.split(':')[0])
@@ -132,7 +93,7 @@ describe('Ec2ConnectClient', function () {
13293
it('throws EC2SSMAgent error if instance is running and has IAM Role, but agent is not running', async function () {
13394
sinon.stub(Ec2ConnectionManager.prototype, 'isInstanceRunning').resolves(true)
13495
sinon.stub(Ec2ConnectionManager.prototype, 'getAttachedIamRole').resolves({ Arn: 'testRole' } as IAM.Role)
135-
sinon.stub(Ec2ConnectionManager.prototype, 'hasProperPolicies').resolves(true)
96+
sinon.stub(Ec2ConnectionManager.prototype, 'hasProperPermissions').resolves(true)
13697
sinon.stub(SsmClient.prototype, 'getInstanceAgentPingStatus').resolves('offline')
13798

13899
try {
@@ -148,7 +109,7 @@ describe('Ec2ConnectClient', function () {
148109
it('does not throw an error if all checks pass', async function () {
149110
sinon.stub(Ec2ConnectionManager.prototype, 'isInstanceRunning').resolves(true)
150111
sinon.stub(Ec2ConnectionManager.prototype, 'getAttachedIamRole').resolves({ Arn: 'testRole' } as IAM.Role)
151-
sinon.stub(Ec2ConnectionManager.prototype, 'hasProperPolicies').resolves(true)
112+
sinon.stub(Ec2ConnectionManager.prototype, 'hasProperPermissions').resolves(true)
152113
sinon.stub(SsmClient.prototype, 'getInstanceAgentPingStatus').resolves('Online')
153114

154115
assert.doesNotThrow(async () => await client.checkForStartSessionError(instanceSelection))

0 commit comments

Comments
 (0)