Validate IAM policies within CloudFormation templates making use of IAM Access Analyzer
In this blog post, I introduce IAM Policy Validator for AWS CloudFormation (cfn-policy-validator) , an open source tool that extracts AWS Identity and Access Management (IAM) policies from an AWS CloudFormation template, and allows you to run existing IAM Access Analyzer policy validation APIs against the template. I also show you how to run the tool in a continuous integration and continuous delivery (CI/CD) pipeline to validate IAM policies in a CloudFormation template before they are deployed to your AWS environment.
Embedding this validation in a CI/CD pipeline can help prevent IAM policies that have IAM Access Analyzer findings from being deployed to your AWS environment. This tool acts as a guardrail that can allow you to delegate the creation of IAM policies to the developers in your organization. You can also use the tool to provide additional confidence in your existing policy authoring process, enabling you to catch mistakes to IAM policy deployment prior.
What is IAM Access Analyzer?
IAM Access Analyzer analyzes < mathematically;a href=”https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html#policies_resource-based” target=”_blank” rel=”noopener noreferrer”>access control policies that are attached to resources, and determines which resources can be accessed or from other accounts publicly. IAM Access Analyzer can validate both identity and resource policies against over 100 checks also, each designed to improve your security posture and to help you to simplify policy management at scale.
The IAM Policy Validator for AWS CloudFormation tool
IAM Policy Validator for AWS CloudFormation (cfn-policy-validator) is a new command-line tool that parses identity-based and resource-based IAM policies from your CloudFormation template, and runs the policies through IAM Access Analyzer checks. The tool is designed to run in the CI/CD pipeline that deploys your CloudFormation templates, and to prevent a deployment when an IAM Access Analyzer finding is detected. This ensures that noticeable changes made to IAM policies are validated before they can be deployed.
The cfn-policy-validator tool looks for all identity-based policies, and a subset of resource-based policies, from your templates. For the full list of supported resource-based policies, see the cfn-policy-validator GitHub repository.
Parsing IAM policies from a CloudFormation template
One of the challenges you can face when parsing IAM policies from a CloudFormation template is that these policies often contain CloudFormation intrinsic functions (such as Ref and Fn::GetAtt) and pseudo parameters (such as AWS::AccountId and AWS::Region). As an example, it’s common for least privileged IAM policies to reference the Amazon Resource Name (ARN) of another CloudFormation resource. Take a look at the following example CloudFormation resources that create an Amazon Simple Queue Service (Amazon SQS) queue, and an IAM role with a policy that grants access to perform the sqs:SendMessage action on the SQS queue.
As you can see in Figure 1, line 21 uses the function Fn::Sub to restrict this policy to MySQSQueue created earlier in the template.
In this example, if you were to pass the root policy (lines 15-21) as written to IAM Access Analyzer, you would get an error because !Sub $MySQSQueue.Arn is syntax that is specific to CloudFormation. The cfn-policy-validator tool takes the policy and translates the CloudFormation syntax to valid IAM policy syntax that Access Analyzer can parse.
The cfn-policy-validator tool recognizes when an intrinsic function such as !Sub $MySQSQueue.Arn evaluates to a resource’s ARN, and generates a valid ARN for the resource. The tool creates the ARN by mapping the type of CloudFormation resource (in this example AWS::SQS::Queue) to a pattern that represents what the ARN of the resource shall look like when it is deployed. For example, the following is what the mapping looks like for the SQS queue referenced previously:
The cfn-policy-validator tool substitutes this generated ARN for !Sub $MySQSQueue.Arn , which allows the cfn-policy-validator tool to parse a policy from the template that can be fed into IAM Access Analyzer for validation. The cfn-policy-validator tool walks through your entire CloudFormation template and performs this ARN substitution until it has generated ARNs for all policies in your template.
Validating the policies with IAM Access Analyzer
After the cfn-policy-validator tool has your IAM policies in a valid format (with no CloudFormation intrinsic functions or pseudo parameters), those policies can be taken by it and feed them into IAM Access Analyzer for validation. The cfn-policy-validator tool runs resource-based and identity-based policies in your CloudFormation template through the ValidatePolicy action of the IAM Access Analyzer. ValidatePolicy is what ensures that your policies have correct grammar and follow IAM policy best practices (for example, not allowing iam:PassRole to all resources). The cfn-policy-validator tool also makes a call to the CreateAccessPreview action for supported resource policies to determine if the policy would grant unintended public or cross-account access to your resource.
The cfn-policy-validator tool categorizes findings from IAM Access Analyzer into the categories blocking or non-blocking . Findings categorized as blocking cause the tool to exit with a non-zero exit code, thereby causing your deployment to fail and preventing your CI/CD pipeline from continuing. If there are no findings, or only non-blocking findings detected, the tool will exit with an exit code of zero (0) and your pipeline can to continue to the next stage. For more information about how the cfn-policy-validator tool decides what findings to categorize as blocking and non-blocking , as well as how to customize the categorization, see the cfn-policy-validator GitHub repository .
Example of running the cfn-policy-validator tool
This section guides you through an example of what happens when you run a CloudFormation template that has some policy violations through the cfn-policy-validator tool.
The following template has two CloudFormation resources with policy findings: an SQS queue policy that grants account 111122223333 access to the SQS queue, and an IAM role with a policy that allows the role to perform a misspelled sqs:ReceiveMessages action. These presssing issues are highlighted in the policy below.
Important : The policy in Figure 2 is written to illustrate a CloudFormation template with potentially undesirable IAM policies. You should be careful when setting the Principal element to an account that is not your own.
When you pass this template as a parameter to the cfn-policy-validator tool, you specify the AWS Region that you want to deploy the template to, as follows:
After the cfn-policy-validator tool runs, the validation is returned by it results, which includes the actual response from IAM Access Analyzer:
The output from the type is included by the cfn-policy-validator tool of finding, the code for the finding, and a message that explains the finding. It includes the resource and policy name from the CloudFormation template also, to allow you to track down and resolve the finding in your template quickly. In the previous example, you can see that IAM Access Analyzer has detected two findings, one security warning and one error, which the cfn-policy-validator tool has classified as blocking. The actual response from IAM Access Analyzer is returned under details , but is excluded above for brevity.
If account 111122223333 is an account that you trust and you are certain that it should have access to the SQS queue, then you can suppress the finding for external access from the 111122223333 account in this example. Modify the call to the cfn-policy-validator tool to ignore this specific finding by using the –-allow-external-principals flag, as follows:
When you look at the output that follows, you’re left with only the blocking finding that states that sqs:ReceiveMessages does not exist.
{
To resolve this finding,|resolve this finding
To,} update the template to have the correct spelling, sqs:ReceiveMessage (without trailing s).
{
For the full list of available flags and commands supported,|
For the full list of available commands and flags supported,} see the cfn-policy-validator GitHub repository .
Now that you’ve seen an example of how you can run the cfn-policy-validator tool to validate policies that are on your local machine, you will take it a step further and see how you can embed the cfn-policy-validator tool in a CI/CD pipeline. By embedding the cfn-policy-validator tool in your CI/CD pipeline, {you can ensure that your IAM policies are validated each time a commit is made to your repository.|you can ensure that your IAM policies are validated each right time a commit is made to your repository.}
Embedding the cfn-policy-validator tool in a CI/CD pipeline
The CI/CD pipeline you will create in this post uses AWS CodePipeline and AWS CodeBuild . AWS CodePipeline is a continuous delivery service that enables you to model, visualize, and automate the steps required to release your software. AWS CodeBuild is a fully-managed continuous integration service that compiles source code, runs tests, and produces software packages that are ready to deploy. You will also use AWS CodeCommit as the source repository where your CloudFormation template is stored. {AWS CodeCommit is a fully managed source-control service that hosts secure Git-based repositories.|AWS CodeCommit is a managed source-control service that hosts secure Git-based repositories fully.}
To deploy the pipeline
- {
- To deploy the CloudFormation template that builds the source repository and AWS CodePipeline pipeline,|
- To deploy the CloudFormation template that builds the source AWS and repository CodePipeline pipeline,} select the following Launch Stack button.
- In the CloudFormation console, choose Next until you reach the Review page.
- Select I acknowledge that AWS CloudFormation might create IAM resources and choose Create stack .
- Open the AWS CodeCommit console and choose Repositories .
- Select cfn-policy-validator-source-repository .
- Download the template.json and template-configuration.json files to your machine.
- In the cfn-policy-validator-source-repository , on the right side, select Add file and choose Upload file .
- Choose Choose File and select the template.json file that you downloaded previously.
- Enter an Author name and an E-mail address and choose Commit changes .
- In the cfn-policy-validator-source-repository , repeat steps 7-9 for the template-configuration.json file.
To view validation in the pipeline
-
- In the AWS CodePipeline console choose the IAMPolicyValidatorPipeline .
-
- Watch as your commit travels through the pipeline. If you followed the previous instructions and made two separate commits, you can ignore the failed results of the first pipeline execution. As shown in Figure 3, you will see that the pipeline fails in the Validation stage on the CfnPolicyValidator action, because it detected a blocking finding in the template you committed, which prevents the invalid policy from reaching your AWS environment.
-
- Under CfnPolicyValidator , choose Details , as shown in Figure 3.
-
- In the Action execution failed pop-up, choose Link to execution details to view the cfn-policy-validator tool output in AWS CodeBuild.
Architectural overview of deploying cfn-policy-validator in your pipeline
You can see the architecture diagram for the CI/CD pipeline you deployed in Figure 4.
Figure 4 shows the following steps, starting with the CodeCommit source action on the left:
-
- The pipeline starts when you commit to your AWS CodeCommit source code repository. The AWS CodeCommit repository is what contains the CloudFormation template that has the IAM policies that you would like to deploy to your AWS environment.
{
- AWS CodePipeline detects the change in your source code repository and begins the Validation stage.|
- AWS CodePipeline detects the noticeable change in your source code repository and begins the Validation stage.} The first step it takes is to start an AWS CodeBuild project that runs the CloudFormation template through the AWS CloudFormation Linter (cfn-lint) . The cfn-lint tool validates your template against the CloudFormation resource specification. Taking this initial step ensures that you have a valid CloudFormation template before validating your IAM policies. This is an optional step, but a recommended one. {Early schema validation provides fast feedback for any typos or mistakes in your template.|Early schema validation provides feedback for any typos or mistakes in your template fast.} There’s little benefit to running additional static analysis tools if your template has an invalid schema.
- If the cfn-lint tool completes successfully, you then call a separate AWS CodeBuild project that invokes the IAM Policy Validator for AWS CloudFormation (cfn-policy-validator) . {The cfn-policy-validator tool then extracts the identity-based and resource-based policies from your template,|The cfn-policy-validator tool then extracts the resource-based and identity-based policies from your template,} as described earlier, and runs the policies through IAM Access Analyzer.
Note: if your template has parameters , {then you need to provide them to the cfn-policy-validator tool.|you need to provide them to the cfn-policy-validator tool then.} You can provide parameters as command-line arguments, or use a template configuration file . It is recommended to use a template configuration file when running validation with an AWS CodePipeline pipeline. The same file can also be used in the deploy stage to deploy the CloudFormation template. By using the template configuration file, you can ensure that you use the same parameters to validate and deploy your template. The CloudFormation template for the pipeline provided with this blog post defaults to using a template configuration file.
If there are no blocking findings found in the policy validation, the cfn-policy-validator tool exits with an exit code of zero (0) and the pipeline moves to the next stage. If any blocking findings are detected, {the cfn-policy-validator tool will exit with a non-zero exit code and the pipeline stops,|the cfn-policy-validator tool shall exit with a non-zero exit code and the pipeline stops,} to prevent the deployment of undesired IAM policies.
- The final stage of the pipeline uses the AWS CloudFormation action in AWS CodePipeline to deploy the template to your environment. Your template will only make it to this stage if it passes all static analysis checks run in the Validation Stage.
Cleaning Up
{
To avoid incurring future charges,|avoid incurring future charges
To,} in the AWS CloudFormation console delete the validate-iam-policy-pipeline stack. This will remove the validation pipeline from your AWS account.
Summary
In this blog post, I introduced the IAM Policy Validator for AWS CloudFormation (cfn-policy-validator). The cfn-policy-validator tool automates the parsing of identity-based and resource-based IAM policies from your CloudFormation templates and runs those policies through IAM Access Analyzer. This enables you to validate that the policies in your templates follow IAM best practices and do not allow unintended external access to your AWS resources.
I showed you how the IAM Policy Validator for AWS CloudFormation can be included in a CI/CD pipeline. This allows you to run validation on your IAM policies on every commit to your repository and only deploy the template if validation succeeds.
For more information, {or to provide feedback and feature requests,|or to provide feature and feedback requests,} see the cfn-policy-validator GitHub repository .
If you have feedback about this post, submit comments in the Comments section below.
Want more AWS Security how-to content, news, and feature announcements? Follow us on Twitter .
You must be logged in to post a comment.