Skip to content

Commit 3ec5875

Browse files
committed
fix(policychecks): Update Policy Checks to use profile selected by AWS Toolkits instead of always default
1 parent fb2b23e commit 3ec5875

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

packages/core/src/awsService/accessanalyzer/vue/iamPolicyChecks.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ export class IamPolicyChecksWebview extends VueWebview {
179179
documentType,
180180
inputPolicyType: policyType ? policyType : 'None',
181181
})
182-
this.client.config.credentials = new SharedIniFileCredentials() // We need to detect changes in the user's credentials
182+
this.client.config.credentials = new SharedIniFileCredentials({
183+
profile: `${getProfileName()}`,
184+
}) // We need to detect changes in the user's credentials
183185
this.client.validatePolicy(
184186
{
185187
policyDocument: IamPolicyChecksWebview.editedDocument,
@@ -276,6 +278,8 @@ export class IamPolicyChecksWebview extends VueWebview {
276278
`${this.region}`,
277279
'--config',
278280
`${globals.context.asAbsolutePath(defaultTerraformConfigPath)}`,
281+
'--profile',
282+
`${getProfileName()}`,
279283
]
280284
this.executeValidatePolicyCommand({
281285
command,
@@ -296,7 +300,15 @@ export class IamPolicyChecksWebview extends VueWebview {
296300
case 'CloudFormation': {
297301
if (isCloudFormationTemplate(document)) {
298302
const command = 'cfn-policy-validator'
299-
const args = ['validate', '--template-path', `${document}`, '--region', `${this.region}`]
303+
const args = [
304+
'validate',
305+
'--template-path',
306+
`${document}`,
307+
'--region',
308+
`${this.region}`,
309+
'--profile',
310+
`${getProfileName()}`,
311+
]
300312
if (cfnParameterPath !== '') {
301313
args.push('--template-configuration-file', `${cfnParameterPath}`)
302314
}
@@ -356,6 +368,8 @@ export class IamPolicyChecksWebview extends VueWebview {
356368
`${tempFilePath}`,
357369
'--reference-policy-type',
358370
`${policyType}`,
371+
'--profile',
372+
`${getProfileName()}`,
359373
]
360374
this.executeCustomPolicyChecksCommand({
361375
command,
@@ -387,6 +401,8 @@ export class IamPolicyChecksWebview extends VueWebview {
387401
`${tempFilePath}`,
388402
'--reference-policy-type',
389403
`${policyType}`,
404+
'--profile',
405+
`${getProfileName()}`,
390406
]
391407
if (cfnParameterPath !== '') {
392408
args.push('--template-configuration-file', `${cfnParameterPath}`)
@@ -447,6 +463,8 @@ export class IamPolicyChecksWebview extends VueWebview {
447463
`${this.region}`,
448464
'--config',
449465
`${globals.context.asAbsolutePath(defaultTerraformConfigPath)}`,
466+
'--profile',
467+
`${getProfileName()}`,
450468
]
451469
if (actions !== '') {
452470
args.push('--actions', `${actions}`)
@@ -479,6 +497,8 @@ export class IamPolicyChecksWebview extends VueWebview {
479497
`${document}`,
480498
'--region',
481499
`${this.region}`,
500+
'--profile',
501+
`${getProfileName()}`,
482502
]
483503
if (actions !== '') {
484504
args.push('--actions', `${actions}`)
@@ -524,6 +544,8 @@ export class IamPolicyChecksWebview extends VueWebview {
524544
`${this.region}`,
525545
'--config',
526546
`${globals.context.asAbsolutePath(defaultTerraformConfigPath)}`,
547+
'--profile',
548+
`${getProfileName()}`,
527549
]
528550
this.executeCustomPolicyChecksCommand({
529551
command,
@@ -550,6 +572,8 @@ export class IamPolicyChecksWebview extends VueWebview {
550572
`${document}`,
551573
'--region',
552574
`${this.region}`,
575+
'--profile',
576+
`${getProfileName()}`,
553577
]
554578
if (cfnParameterPath !== '') {
555579
args.push('--template-configuration-file', `${cfnParameterPath}`)
@@ -919,6 +943,11 @@ export function isJsonPolicyLanguage(document: string) {
919943
return policyLanguageFileTypes.some((t) => document.endsWith(t))
920944
}
921945

946+
export function getProfileName(): string | undefined {
947+
// We neeed to split the name on 'profile:' to extract the correct profile name
948+
return globals.awsContext.getCredentialProfileName()?.split('profile:')[1]
949+
}
950+
922951
export class PolicyChecksError extends ToolkitError {
923952
constructor(message: string, code: PolicyChecksErrorCode) {
924953
super(message, { code })

0 commit comments

Comments
 (0)