- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Cloud
- Security
Validating IAM Policies as Automated Unit Tests
During this hands-on lab, you will build upon an existing AWS CodePipelien governance pipeline that contains linting stage for catching misconfigured CloudFormation templates. By integrating `cfn-guard` into an additional, separate governance stage, you will work to automate scanning against IAM policies with overly permissive policies that have been declared in your CloudFormation templates. **Important Information**: Please ensure that you read through the **Additional Information** section.
Lab Info
Table of Contents
-
Challenge
Locally install cfn-guard
The first thing you need to do is install CloudFormation Guard locally so that you can test it later on. To do this, you can download the pre-built binary for testing.
- Navigate to AWS CloudShell.
- Install Guard from a pre-built release binary (https://docs.aws.amazon.com/cfn-guard/latest/ug/setting-up-linux.html)
- Set your path variable to include CloudFormation Guard.
- Check the version of CloudFormation Guard to ensure that the tool is ready to go.
-
Challenge
Test cfn-guard locally
Now that you've installed CloudFormation Guard locally to your CloudShell instance, you need to test it against a rule set and a template file.
- Change to the
~/Path-Proactive-Security-in-Your-AWS-CI-CD-Pipeline/5-lab-validating-iam-policies-as-automated-unit-testsdirectory in AWS CloudShell. - Inspect the
cfn-guard/iam-no-s3-wildcard.guardfile. - Inspect the
infra/template.ymlCloudFormation file. - Validate the CloudFormation template against your cfn-guard rules using both provided templates.
- Ensure there are no errors before moving on.
- Change to the
-
Challenge
Update the buildspec for the guard governance stage
Your next step is to create a separate buildspec.yml file for a brand new governance stage that you'll add within your pipeline.
- Inspect the
configuration/buildspec-guard.ymlfile that will be used for the new stage. - Ensure there are three phases defined within the file.
installpre_buildbuild
- Recursively zip up the
configuration,infra, andcfn-guarddirectories and their files into anartifacts.zipfile.
- Inspect the
-
Challenge
Test the AWS CodePipeline pipeline
With all of the pipeline pieces in place, you can now upload your artifacts.zip file to test the pipeline runs successfully.
- Upload the ZIP file to the Amazon S3 bucket from your AWS CloudShell window.
- Verify the pipeline succeeds.
- Inspect the logs for each stage within the pipeline.
-
Challenge
Create the new guard governance stage
With the CodeBuild project ready to go, you can now integrate it into a brand new stage within the existing pipeline.
- Edit the existing
governance-pipelinepipeline: In thegovernance-pipelinepipeline, add a brand new stage calledGovernanceGuardimmediately after GovernanceLint - Add a new Add action group for the new stage:
- Input artifacts:
GovernanceLintOutputs - Project name:
governance-guardexisting CodeBuild project - Define buildspec override - optional: enabled and set to
configuration/buildspec-guard.yml - Output artifacts:
GovernanceGuardOutputs
- Input artifacts:
And, modify the Deploy stage as well:
- Set Input artifacts to
GovernanceGuardOutputs - Set Template > Artifact name to
GovernanceGuardOutputs
- Edit the existing
-
Challenge
Create the new CodeBuild guard project
Now that you have the guard policy rules set ready to go, and your buildpsec files ready, you need to create a new CodeBuild project as well as a new stage within your governance pipeline.
- Clone the existing
governance-lintAWS CodeBuild project and name itgovernance-guard. - For the Source 1 - Primary set it to AWS CodePipeline with no source version set.
- Use the existing service role for the service role permissions (Example:
arn:aws:iam::123456789876:role/governance-codebuild-role)
- Clone the existing
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.