-
Notifications
You must be signed in to change notification settings - Fork 86
feat: display CloudFormation Guard Hook failures #1198
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
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7f1ac84
feat(toolkit-lib): emit full s3 output for aws guard hook failures
jkelley-godaddy 9693b81
only include rule name and message in output
jkelley-godaddy 293e261
improve formatting for long messages
jkelley-godaddy 5cb7af4
parse disjoint checks, add newline to formatting, and refactor into s…
jkelley-godaddy 674008a
add integration test
jkelley-godaddy 00ae174
chore: self mutation
github-actions[bot] ad7f206
move integration test resources to cdk
jkelley-godaddy 801c79b
fix: make GuardHookSetupStack more flexible and fix incorrect principal
mrgrain e2880dd
fix s3 guard hooks test
mrgrain 1bee844
change to use annotations as source
jkelley-godaddy 51e50a8
Merge branch 'aws:main' into guard-hook-emit-s3-output
jkelley-godaddy c7d12c8
Merge branch 'aws:main' into guard-hook-emit-s3-output
jkelley-godaddy 8948991
Merge branch 'aws:main' into guard-hook-emit-s3-output
jkelley-godaddy e7b1683
Add bootstrap upgrade warning
jkelley-godaddy 9095028
Merge branch 'main' into guard-hook-emit-s3-output
mrgrain c0e54c2
chore: self mutation
github-actions[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
packages/@aws-cdk-testing/cli-integ/resources/cdk-apps/guard-hook-app/app.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| const cdk = require('aws-cdk-lib/core'); | ||
| const s3 = require('aws-cdk-lib/aws-s3'); | ||
| const iam = require('aws-cdk-lib/aws-iam'); | ||
| const s3deploy = require('aws-cdk-lib/aws-s3-deployment'); | ||
|
|
||
| const stackPrefix = process.env.STACK_NAME_PREFIX; | ||
| if (!stackPrefix) { | ||
| throw new Error('the STACK_NAME_PREFIX environment variable is required'); | ||
| } | ||
|
|
||
| const guardRuleKey = 'rules/AWS_S3_Bucket_AccessControl.guard'; | ||
|
|
||
| const guardRuleContent = `rule AWS_S3_Bucket_AccessControl | ||
| { | ||
| let resources = Resources.*[ Type == "AWS::S3::Bucket" ] | ||
| %resources[*] { | ||
| Properties.AccessControl not exists | ||
| << | ||
| AccessControl is deprecated | ||
| >> | ||
| } | ||
| }`; | ||
|
|
||
| // Setup Guard Hook that will evaluate and fail the test stack | ||
| class GuardHookSetupStack extends cdk.Stack { | ||
| constructor(scope, id, props) { | ||
| super(scope, id, props); | ||
|
|
||
| // S3 bucket for Guard rules | ||
| const rulesBucket = new s3.Bucket(this, 'RulesBucket', { | ||
| removalPolicy: cdk.RemovalPolicy.DESTROY, | ||
| autoDeleteObjects: true, | ||
| }); | ||
|
|
||
| // S3 bucket for Guard logs | ||
| const logsBucket = new s3.Bucket(this, 'LogsBucket', { | ||
| removalPolicy: cdk.RemovalPolicy.DESTROY, | ||
| autoDeleteObjects: true, | ||
| }); | ||
|
|
||
| // Upload Guard rule to rules bucket | ||
| new s3deploy.BucketDeployment(this, 'UploadGuardRule', { | ||
| sources: [s3deploy.Source.data(guardRuleKey, guardRuleContent)], | ||
| destinationBucket: rulesBucket, | ||
| }); | ||
|
|
||
| // IAM role for Guard Hook execution | ||
| const hookRole = new iam.Role(this, 'GuardHookExecutionRole', { | ||
| assumedBy: new iam.ServicePrincipal('hooks.cloudformation.amazonaws.com'), | ||
| }); | ||
| hookRole.addToPolicy(new iam.PolicyStatement({ | ||
| effect: iam.Effect.ALLOW, | ||
| actions: ['s3:ListBucket', 's3:GetObject', 's3:GetObjectVersion'], | ||
| resources: [rulesBucket.bucketArn, rulesBucket.arnForObjects('*')], | ||
| })); | ||
| hookRole.addToPolicy(new iam.PolicyStatement({ | ||
| effect: iam.Effect.ALLOW, | ||
| actions: ['s3:PutObject'], | ||
| resources: [logsBucket.arnForObjects('*')], | ||
| })); | ||
|
|
||
| // Guard Hook - activates and configures the AWS Guard Hook in this account/region | ||
| new cdk.CfnGuardHook(this, 'GuardHook', { | ||
| alias: 'Private::Guard::TestHook', | ||
| executionRole: hookRole.roleArn, | ||
| failureMode: 'FAIL', | ||
| hookStatus: 'ENABLED', | ||
| ruleLocation: { | ||
| uri: `s3://${rulesBucket.bucketName}/${guardRuleKey}`, | ||
| }, | ||
| logBucket: logsBucket.bucketName, | ||
| targetOperations: ['RESOURCE'], | ||
| stackFilters: { | ||
| filteringCriteria: "ANY", | ||
| stackNames: { | ||
| // Do not evalute this stack with the hook | ||
| exclude: [cdk.Aws.STACK_NAME] | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| class GuardHookTestStack extends cdk.Stack { | ||
| constructor(scope, id, props) { | ||
| super(scope, id, props); | ||
|
|
||
| // This bucket violates the Guard rule by using the deprecated AccessControl property | ||
| new s3.CfnBucket(this, 'NonCompliantBucket', { | ||
| accessControl: 'Private', | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| const app = new cdk.App(); | ||
| new GuardHookSetupStack(app, `${stackPrefix}-guard-hook-setup`); | ||
| new GuardHookTestStack(app, `${stackPrefix}-guard-hook-test`); | ||
|
|
||
| app.synth(); |
4 changes: 4 additions & 0 deletions
4
packages/@aws-cdk-testing/cli-integ/resources/cdk-apps/guard-hook-app/cdk.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "app": "node app.js", | ||
| "versionReporting": false | ||
| } |
22 changes: 22 additions & 0 deletions
22
...ts/cli-integ-tests/deploy/cdk-deploy-guard-hook-failure-displays-annotations.integtest.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { integTest, withSpecificFixture } from '../../../lib'; | ||
|
|
||
| jest.setTimeout(2 * 60 * 60_000); | ||
|
|
||
| integTest( | ||
| 'deploy with guard hook failure displays hook annoations', | ||
| withSpecificFixture('guard-hook-app', async (fixture) => { | ||
| // Deploy the setup stack which creates the Guard Hook via CloudFormation | ||
| await fixture.cdkDeploy('guard-hook-setup'); | ||
|
|
||
| // Attempt to deploy non-compliant stack (should fail due to Guard Hook) | ||
| const deployOutput = await fixture.cdkDeploy('guard-hook-test', { | ||
| options: ['--no-rollback'], | ||
| allowErrExit: true, | ||
| }); | ||
| expect(deployOutput).toContain('CREATE_FAILED'); | ||
| expect(deployOutput).toContain('NonCompliant Rules:'); | ||
| expect(deployOutput).toContain('[AWS_S3_Bucket_AccessControl]'); | ||
| expect(deployOutput).toContain('• Check was not compliant as property [/Resources/NonCompliantBucket/Properties/AccessControl[L:0,C:91]] existed.'); | ||
| expect(deployOutput).toContain('Remediation: AccessControl is deprecated'); | ||
| }), | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.