-
Notifications
You must be signed in to change notification settings - Fork 735
fix(policychecks): Update Policy Checks to use profile selected by AWS Toolkits instead of always default #6074
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -179,7 +179,9 @@ export class IamPolicyChecksWebview extends VueWebview { | |
| documentType, | ||
| inputPolicyType: policyType ? policyType : 'None', | ||
| }) | ||
| this.client.config.credentials = new SharedIniFileCredentials() // We need to detect changes in the user's credentials | ||
| this.client.config.credentials = new SharedIniFileCredentials({ | ||
| profile: `${getProfileName()}`, | ||
| }) // We need to detect changes in the user's credentials | ||
| this.client.validatePolicy( | ||
| { | ||
| policyDocument: IamPolicyChecksWebview.editedDocument, | ||
|
|
@@ -277,6 +279,8 @@ export class IamPolicyChecksWebview extends VueWebview { | |
| `${this.region}`, | ||
| '--config', | ||
| `${globals.context.asAbsolutePath(defaultTerraformConfigPath)}`, | ||
| '--profile', | ||
| `${getProfileName()}`, | ||
| ] | ||
| await this.executeValidatePolicyCommand({ | ||
| command, | ||
|
|
@@ -297,7 +301,15 @@ export class IamPolicyChecksWebview extends VueWebview { | |
| case 'CloudFormation': { | ||
| if (isCloudFormationTemplate(document)) { | ||
| const command = 'cfn-policy-validator' | ||
| const args = ['validate', '--template-path', `${document}`, '--region', `${this.region}`] | ||
| const args = [ | ||
| 'validate', | ||
| '--template-path', | ||
| `${document}`, | ||
| '--region', | ||
| `${this.region}`, | ||
| '--profile', | ||
| `${getProfileName()}`, | ||
| ] | ||
| if (cfnParameterPath !== '') { | ||
| args.push('--template-configuration-file', `${cfnParameterPath}`) | ||
| } | ||
|
|
@@ -357,6 +369,8 @@ export class IamPolicyChecksWebview extends VueWebview { | |
| `${tempFilePath}`, | ||
| '--reference-policy-type', | ||
| `${policyType}`, | ||
| '--profile', | ||
| `${getProfileName()}`, | ||
| ] | ||
| await this.executeCustomPolicyChecksCommand({ | ||
| command, | ||
|
|
@@ -388,6 +402,8 @@ export class IamPolicyChecksWebview extends VueWebview { | |
| `${tempFilePath}`, | ||
| '--reference-policy-type', | ||
| `${policyType}`, | ||
| '--profile', | ||
| `${getProfileName()}`, | ||
| ] | ||
| if (cfnParameterPath !== '') { | ||
| args.push('--template-configuration-file', `${cfnParameterPath}`) | ||
|
|
@@ -448,6 +464,8 @@ export class IamPolicyChecksWebview extends VueWebview { | |
| `${this.region}`, | ||
| '--config', | ||
| `${globals.context.asAbsolutePath(defaultTerraformConfigPath)}`, | ||
| '--profile', | ||
| `${getProfileName()}`, | ||
| ] | ||
| if (actions !== '') { | ||
| args.push('--actions', `${actions}`) | ||
|
|
@@ -480,6 +498,8 @@ export class IamPolicyChecksWebview extends VueWebview { | |
| `${document}`, | ||
| '--region', | ||
| `${this.region}`, | ||
| '--profile', | ||
| `${getProfileName()}`, | ||
| ] | ||
| if (actions !== '') { | ||
| args.push('--actions', `${actions}`) | ||
|
|
@@ -525,6 +545,8 @@ export class IamPolicyChecksWebview extends VueWebview { | |
| `${this.region}`, | ||
| '--config', | ||
| `${globals.context.asAbsolutePath(defaultTerraformConfigPath)}`, | ||
| '--profile', | ||
| `${getProfileName()}`, | ||
| ] | ||
| await this.executeCustomPolicyChecksCommand({ | ||
| command, | ||
|
|
@@ -551,6 +573,8 @@ export class IamPolicyChecksWebview extends VueWebview { | |
| `${document}`, | ||
| '--region', | ||
| `${this.region}`, | ||
| '--profile', | ||
| `${getProfileName()}`, | ||
| ] | ||
| if (cfnParameterPath !== '') { | ||
| args.push('--template-configuration-file', `${cfnParameterPath}`) | ||
|
|
@@ -925,6 +949,11 @@ export function isJsonPolicyLanguage(document: string) { | |
| return policyLanguageFileTypes.some((t) => document.endsWith(t)) | ||
| } | ||
|
|
||
| export function getProfileName(): string | undefined { | ||
| // We neeed to split the name on 'profile:' to extract the correct profile name | ||
| return globals.awsContext.getCredentialProfileName()?.split('profile:')[1] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a constant format? I am trying to understand who enforces this format? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, can we add an error message on the backend to determine any incorrect format here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Toolkits uses the format in displaying the profile name to users after authenticating through the extension. It's stored with
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed we should probably refactor the profile name in to a data object instead. It doesnt need to be done in this PR.
There shouldn't be a need to determine incorrect formats as the format of this data should have been validated here and then right after we create the name with this format. |
||
| } | ||
|
|
||
| export class PolicyChecksError extends ToolkitError { | ||
| constructor(message: string, code: PolicyChecksErrorCode) { | ||
| super(message, { code }) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -155,6 +155,8 @@ describe('validatePolicy', function () { | |
| 'us-east-1', | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a test that will help us determine if there is any change to the expected format in the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The CLI tool tests check for a profile to be passed as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Had a chat with Kevin offline regarding if there is a way for us to catch the change in the format of the profile name, and he confirmed that it should be caught in the unit tests. |
||
| '--config', | ||
| `${globals.context.asAbsolutePath(defaultTerraformConfigPath)}`, | ||
| '--profile', | ||
| 'undefined', | ||
| ], | ||
| cfnParameterPathExists: false, | ||
| documentType, | ||
|
|
@@ -180,6 +182,8 @@ describe('validatePolicy', function () { | |
| IamPolicyChecksWebview.editedDocumentFileName, | ||
| '--region', | ||
| 'us-east-1', | ||
| '--profile', | ||
| 'undefined', | ||
| '--template-configuration-file', | ||
| cfnParameterPath, | ||
| ], | ||
|
|
@@ -449,6 +453,8 @@ describe('customChecks', function () { | |
| 'us-east-1', | ||
| '--config', | ||
| `${globals.context.asAbsolutePath(defaultTerraformConfigPath)}`, | ||
| '--profile', | ||
| 'undefined', | ||
| '--actions', | ||
| 'action1action2', | ||
| '--resources', | ||
|
|
@@ -486,6 +492,8 @@ describe('customChecks', function () { | |
| document, | ||
| '--region', | ||
| 'us-east-1', | ||
| '--profile', | ||
| 'undefined', | ||
| '--actions', | ||
| 'action1action2', | ||
| '--resources', | ||
|
|
@@ -548,6 +556,8 @@ describe('customChecks', function () { | |
| 'us-east-1', | ||
| '--config', | ||
| `${globals.context.asAbsolutePath(defaultTerraformConfigPath)}`, | ||
| '--profile', | ||
| 'undefined', | ||
| ], | ||
| cfnParameterPathExists: !!cfnParameterPath, | ||
| documentType, | ||
|
|
@@ -579,6 +589,8 @@ describe('customChecks', function () { | |
| document, | ||
| '--region', | ||
| 'us-east-1', | ||
| '--profile', | ||
| 'undefined', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All of the tests assert "undefined". If this PR is intended to support the non-undefined case, at least one test should assert that case
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These test cases do not actually ever use this argument, as we stub the actual execution of our CLI tool here. It simply looks at the global Is there a way to change the current profile in the test execution context? If we are able to, we can assert it is propagated properly.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Look at existing code. |
||
| '--template-configuration-file', | ||
| cfnParameterPath, | ||
| ], | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "type": "Bug Fix", | ||
| "description": "Policy Checks selected profile is always default" | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Firstly, thank you @kevluu-aws for fixing it! 🙌