Skip to content

Commit b0b4d6d

Browse files
committed
refactor delay logic into remoteSession
1 parent 90b658f commit b0b4d6d

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/ec2/model.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
ensureDependencies,
1818
getDeniedSsmActions,
1919
openRemoteTerminal,
20-
promptToAddPolicies,
20+
promptToAddInlinePolicy,
2121
} from '../shared/remoteSession'
2222
import { DefaultIamClient } from '../shared/clients/iamClient'
2323
import { ErrorInformation } from '../shared/errors'
@@ -38,7 +38,6 @@ interface Ec2RemoteEnv extends VscodeRemoteConnection {
3838

3939
const ec2ConnectScriptPrefix = 'ec2_connect'
4040
const hostNamePrefix = 'ec2-'
41-
const policyAttachDelay = 5000
4241

4342
export class Ec2ConnectionManager {
4443
protected ssmClient: SsmClient
@@ -128,7 +127,7 @@ export class Ec2ConnectionManager {
128127
const hasPermission = await this.hasProperPermissions(IamRole!.Arn)
129128

130129
if (!hasPermission) {
131-
const policiesAdded = await promptToAddPolicies(this.iamClient, IamRole!.Arn!)
130+
const policiesAdded = await promptToAddInlinePolicy(this.iamClient, IamRole!.Arn!)
132131

133132
if (!policiesAdded) {
134133
const message = `Did not add permissions. Ensure an IAM role with the proper permissions is attached to the instance. Found attached role: ${
@@ -139,13 +138,6 @@ export class Ec2ConnectionManager {
139138
documentationUri: this.policyDocumentationUri,
140139
})
141140
}
142-
const timeout = new Timeout(policyAttachDelay)
143-
144-
function delay(ms: number) {
145-
return new Promise(resolve => setTimeout(resolve, ms))
146-
}
147-
await showMessageWithCancel(`Adding Inline Policy to ${IamRole!.Arn}`, timeout)
148-
await delay(policyAttachDelay).finally(() => timeout.cancel())
149141
}
150142
}
151143

src/shared/remoteSession.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ const minimumSsmActions = [
3535
'ssmmessages:OpenDataChannel',
3636
]
3737

38+
const policyAttachDelay = 5000
39+
3840
export async function openRemoteTerminal(options: vscode.TerminalOptions, onClose: () => void) {
3941
const timeout = new Timeout(60000)
4042

@@ -187,19 +189,31 @@ export async function getDeniedSsmActions(client: IamClient, roleArn: string): P
187189
return deniedActions
188190
}
189191

190-
export async function promptToAddPolicies(client: IamClient, roleArn: string): Promise<boolean> {
192+
export async function promptToAddInlinePolicy(client: IamClient, roleArn: string): Promise<boolean> {
191193
const promptText = `${
192194
getIdeProperties().company
193195
} Toolkit will add required actions to role ${roleArn}:\n${getFormattedSsmActions()}`
194196
const confirmation = await showConfirmationMessage({ prompt: promptText, confirm: 'Approve' })
195197

196198
if (confirmation) {
197-
addSsmActionsToInlinePolicy(client, roleArn)
199+
await addInlinePolicyWithDelay(client, roleArn)
198200
}
199201

200202
return confirmation
201203
}
202204

205+
async function addInlinePolicyWithDelay(client: IamClient, roleArn: string) {
206+
const timeout = new Timeout(policyAttachDelay)
207+
await showMessageWithCancel(`Adding Inline Policy to ${roleArn}`, timeout)
208+
await addSsmActionsToInlinePolicy(client, roleArn)
209+
210+
function delay(ms: number) {
211+
return new Promise(resolve => setTimeout(resolve, ms))
212+
}
213+
214+
await delay(policyAttachDelay).finally(() => timeout.cancel())
215+
}
216+
203217
function getFormattedSsmActions() {
204218
const formattedActions = minimumSsmActions.map(action => `"${action}",\n`).reduce((l, r) => l + r)
205219

0 commit comments

Comments
 (0)