Sub topic 12
Validating policies and running custom policy checks before deployment
Earlier chapters used analyzers to find access that already exists. This chapter moves the check earlier, to the moment a policy is written. Access Analyzer provides policy validation and custom policy checks as API calls, so a pipeline can reject a bad policy before it is deployed.
This chapter assumes you can read IAM policy JSON, use the AWS CLI, and know the difference between identity-based and resource-based policies.
Two different questions
Access Analyzer's pre-deployment tools answer two separate questions.
- Is this policy well formed and sensible?
validate-policyanswers this. It checks grammar and best practices. - Does this policy grant (or avoid granting) specific access? The custom policy checks answer this. They use automated reasoning to evaluate what the policy allows, not just how it is written.
Neither call needs an analyzer. They are stateless API calls that take a policy document and return a result, so you can run them on a laptop or in a CI job without any setup in the account.
Validating a policy
validate-policy compares a policy against IAM grammar and AWS best practices. You must tell it what kind of policy you are passing, because the rules differ.
aws accessanalyzer validate-policy \
--policy-type IDENTITY_POLICY \
--policy-document file://policy.jsonThe supported policy types are IDENTITY_POLICY, RESOURCE_POLICY, SERVICE_CONTROL_POLICY and RESOURCE_CONTROL_POLICY. For a resource policy, add --validate-policy-resource-type (for example AWS::S3::Bucket) so the validator applies resource-specific checks.
Finding types
Each finding in the response has a findingType. The type tells you how to react.
| Finding type | Meaning | Typical pipeline action |
|---|---|---|
| `ERROR` | The policy is invalid or will not work, for example an unknown action or a malformed ARN. | Fail the build |
| `SECURITY_WARNING` | The policy is valid but grants permissions that are risky, such as `iam:PassRole` with `Resource: "*"`. | Fail the build, or require review |
| `WARNING` | The policy is valid but breaks a best practice or probably does not do what you intended. | Report |
| `SUGGESTION` | A way to make the policy simpler or clearer. | Report |
Each finding also carries an issueCode, a learnMoreLink and the locations in the document that triggered it. Use the issueCode to write exceptions in your pipeline rather than matching on message text.
A policy that fails validation
This policy has two problems. The action name is misspelled, and Resource is * for a PassRole grant.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:GetObjects", "iam:PassRole"],
"Resource": "*"
}
]
}Save it as policy.json and filter the output to only the findings that should block a deployment:
aws accessanalyzer validate-policy \
--policy-type IDENTITY_POLICY \
--policy-document file://policy.json \
--query "findings[?findingType=='ERROR' || findingType=='SECURITY_WARNING'].[findingType,issueCode,findingDetails]" \
--output tableThe misspelled action is reported as an error, and the broad iam:PassRole is reported as a security warning.
Custom policy checks
Validation tells you a policy is well formed. It does not tell you whether the policy grants more than before. Custom policy checks do. There are three.
| Check | Question it answers |
|---|---|
| `check-no-new-access` | Does the proposed policy grant anything the existing policy did not? |
| `check-access-not-granted` | Does the policy grant any of these specific actions or resources? |
| `check-no-public-access` | Can this resource policy grant public access? |
Each returns a result of PASS or FAIL, a message, and for failures a reasons list that points to the statements responsible.
check-no-new-access
Use this for changes that should only narrow permissions, such as a right-sizing change based on an unused access recommendation.
aws accessanalyzer check-no-new-access \
--policy-type IDENTITY_POLICY \
--existing-policy-document file://policy-current.json \
--new-policy-document file://policy-proposed.jsonIf the proposed policy only removes or restricts permissions, the result is PASS. If it adds even one action on one resource, the result is FAIL, and reasons names the statement. The comparison is semantic, so a rewritten policy that grants the same access still passes. This is the reason to use it instead of a text diff.
--policy-type here is IDENTITY_POLICY or RESOURCE_POLICY.
check-access-not-granted
Use this to enforce a guardrail: a list of actions or resources that a policy must never allow.
aws accessanalyzer check-access-not-granted \
--policy-type IDENTITY_POLICY \
--policy-document file://policy.json \
--access '[{"actions":["iam:CreateAccessKey","iam:PutRolePolicy"]}]'The --access value is a list. Each entry holds either actions, resources, or both. The check fails if the policy allows any of the listed access.
Two things to know:
- Entries in
actionsare checked against what the policy allows, including wildcards."Action": "iam:*"fails a check foriam:PutRolePolicy. - When an entry names both actions and resources, the check fails only if the policy grants those actions on those resources.
check-no-public-access
Use this on resource policies. You must say which resource type the policy is attached to.
aws accessanalyzer check-no-public-access \
--policy-type RESOURCE_POLICY \
--resource-type AWS::S3::Bucket \
--policy-document file://bucket-policy.jsonRun against the bucket policy from the earlier chapters, which allows s3:GetObject to Principal: "*", this check returns FAIL. The supported resource types include S3 buckets, DynamoDB tables, SQS queues, KMS keys, Lambda functions and several others. Check aws accessanalyzer check-no-public-access help for the current list.
Wiring the checks into CI
Because the CLI exits 0 regardless of the outcome, a pipeline script has to read the result and exit non-zero itself. This script validates a policy, then runs two custom checks. It stops on the first problem.
#!/usr/bin/env bash
set -euo pipefail
POLICY="policy-proposed.json"
CURRENT="policy-current.json"
# 1. Block on errors and security warnings
BLOCKING=$(aws accessanalyzer validate-policy \
--policy-type IDENTITY_POLICY \
--policy-document "file://${POLICY}" \
--query "length(findings[?findingType=='ERROR' || findingType=='SECURITY_WARNING'])" \
--output text)
if [ "$BLOCKING" -gt 0 ]; then
echo "Validation found ${BLOCKING} blocking finding(s)" >&2
exit 1
fi
# 2. The change must not add access
RESULT=$(aws accessanalyzer check-no-new-access \
--policy-type IDENTITY_POLICY \
--existing-policy-document "file://${CURRENT}" \
--new-policy-document "file://${POLICY}" \
--query result --output text)
if [ "$RESULT" != "PASS" ]; then
echo "Proposed policy grants new access" >&2
exit 1
fi
# 3. Guardrail: never allow IAM privilege escalation actions
RESULT=$(aws accessanalyzer check-access-not-granted \
--policy-type IDENTITY_POLICY \
--policy-document "file://${POLICY}" \
--access '[{"actions":["iam:CreateAccessKey","iam:PutRolePolicy","iam:AttachRolePolicy"]}]' \
--query result --output text)
if [ "$RESULT" != "PASS" ]; then
echo "Policy grants a forbidden action" >&2
exit 1
fi
echo "All policy checks passed"The same calls are available in the SDKs, so you can also run them in unit tests for infrastructure code. If you deploy with CloudFormation, extract the policy documents from the rendered template before checking them, because the checks accept policy JSON, not templates.
Choosing the right check
- Use
validate-policyon every policy, every time. It is cheap and catches typos that otherwise surface asAccessDeniedin production. - Use
check-no-new-accessfor changes that claim to reduce access, and for refactors that claim to keep it the same. - Use
check-access-not-grantedfor organization-wide rules that name specific dangerous actions. - Use
check-no-public-accesson every resource policy for resource types that can be made public.
Limits and failure modes
- Policy only. The checks evaluate the document you pass. They do not look at other policies attached to the same principal, permissions boundaries, or SCPs. A
PASSon one identity policy does not prove the principal is safe. - Existing policy must be accurate.
check-no-new-accesscompares against the document you supply as the existing policy. If your pipeline fetches a stale copy, the comparison is meaningless. - Cost. Policy validation has no charge, but custom policy checks are billed per API call. Run them on changed policies, not on every file in a repository on every commit. Confirm current pricing on the Access Analyzer pricing page.
- Unsupported constructs. A policy that cannot be parsed fails with an API error instead of a
FAILresult. Treat both as a blocked deployment. - Not a replacement for analyzers. Pre-deployment checks cannot see changes made through the console, or access that arrives through a path outside your pipeline. Keep the external and unused access analyzers running to catch what bypasses the pipeline.
Exercise
A pull request replaces an inline role policy with a rewritten version that is meant to be equivalent. Your CI job runs validate-policy and gets no ERROR findings. A reviewer asks how you know the rewrite does not grant more access than before. Which check do you run, with which inputs, and what result lets the change merge?
Show answer
Run check-no-new-access with --policy-type IDENTITY_POLICY, passing the current deployed policy as --existing-policy-document and the rewritten policy as --new-policy-document. The change can merge only if the result is PASS. validate-policy cannot answer the question, because it checks grammar and best practices, not whether the permissions differ. The job must read the result field (for example with --query result --output text) and exit non-zero on anything other than PASS, since the CLI exits 0 even when the result is FAIL.
Exercise
You want to guarantee that no identity policy in your repository ever allows iam:PassRole on every resource. Which check fits, and how do you express the rule in --access?
Show answer
Use check-access-not-granted with --policy-type IDENTITY_POLICY. Pass --access '[{"actions":["iam:PassRole"],"resources":["*"]}]'. Because the entry names both an action and a resource, the check fails only when the policy grants iam:PassRole on all resources, and it still passes for policies that scope PassRole to specific role ARNs.