Skip to content

chore: refactored security guardian tool and security-guardian action. Enables local run. #34158

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

Merged
merged 9 commits into from
May 1, 2025

Conversation

QuantumNeuralCoder
Copy link
Contributor

Issue # (if applicable)

None
Closes #.
NA

Reason for this change

With this change, developers can locally run security guardian against committed files to detect changed .template.json and run the 2 part scanner

  1. cfn-guard to detect inline
  2. custom scanner to detect intrinsics

Please note that this will detect templates where the developer has explicitly provided broadened scope permissions like
new AccountPrincipal();
We will use this as an opportunity to review if that is really needed or can be scoped down.

> cd tools/@aws-cdk/security-guardian
>yarn security-guardian 

Description of changes

Describe any new or updated permissions being added

Description of how you validated changes

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@aws-cdk-automation aws-cdk-automation requested a review from a team April 16, 2025 00:57
@github-actions github-actions bot added the p2 label Apr 16, 2025
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Apr 16, 2025
@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Apr 16, 2025
@moelasmar
Copy link
Contributor

can you also update the contributing readme to include the instructions to run this tool

@shikha372 shikha372 self-assigned this Apr 23, 2025
Copy link
Member

@godwingrs22 godwingrs22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @QuantumNeuralCoder for implementing this tool. Left some comments for your consideration.

Comment on lines +77 to +78
await exec.exec('cfn-guard', [
'validate',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: may be good to check if the cfn-guard is installed before this execution. Given the tool can be executed locally it might be good to throw an error if it is not installed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solution is to add something like

execSync('cfn-guard --version', { stdio: 'ignore' });

and then log it. Dont know if that would make much of a difference for local run.
In the Github action, its installed and built as 2 steps prior to running this tool

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we want to run locally, does yarn install will automatically install the cfn-guard or we should manually install similar to these steps in the Github action. If we need to install manually, would be good to update the readme for the cfn-guard installation in the local developement section.

let matches: Array<{ filePath: string, statements: any[] }> = [];
let totalFiles = 0;

function isRootPrincipal(statement: any): boolean {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to define as an interface for IAMStatement and use it here instead of using as any type.

Eg:

interface IAMStatement {
  Effect: ...;
  Principal?: {
    AWS?: ...;
    Service?: ...;
  };
  Action?: ...;
  Resource?: ...;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem is it will limit the pattern match to statements. Currently its for statements, we want to catch/ highlight if this pattern is used anywhere else (ie future proof). Let me know if you feel strongly about it.


async function run(): Promise<void> {
const errors: string[] = [];
let workingDir: string = './changed_templates';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand we are using the workingDir to store all the changed templates and run the cfnguard and intrinsic checks on this directory. However, I have a concern about file traceability. Currently, when files are copied to the working directory (./changed_templates), the mapping to their original paths is lost. This might make it difficult for developers to trace back the issues found by cfn-guard and intrinsic checks to the actual source files. For example, in a PR with multiple changed template files, developers might struggle to identify which specific template in their repository needs fixing.

I'm thinking a file mapping mechanism to maintain the relationship between original file paths and their copies in the working directory might help so the validation result can show the path of the original file path in the output.

Also another approach could be avoiding copying the changed templates to a separate directory and instead process the scan directly from the original file where we could able to track. However, this might not be feasible as cfn-guard's --data option may have limitations with processing multiple template files. I'm not sure about this.

Let me know your thoughts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/ are replaced by _. Full path is given in the filename. Makes it an easier read except for large PRs. Currently I am deleting the changed templates. which maybe something we need to look at from local run perspective. let me think.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed offline. This needs more deep dive and will work on a separate PR. Not a blocker for this PR.

Copy link
Contributor

mergify bot commented May 1, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 629bb91
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

Copy link
Contributor

mergify bot commented May 1, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit 1445955 into aws:main May 1, 2025
13 of 15 checks passed
Copy link
Contributor

github-actions bot commented May 1, 2025

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 1, 2025
@QuantumNeuralCoder QuantumNeuralCoder deleted the s-g3 branch May 14, 2025 21:18
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
contribution/core This is a PR that came from AWS. p2 pr/needs-maintainer-review This PR needs a review from a Core Team Member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants