Sub topic 12
Validating Policies and Running Custom Policy Checks Before Deployment
This chapter assumes you can read IAM policy JSON (Effect, Principal, Action, Resource, Condition) and run AWS CLI commands. It also assumes you know the difference between identity-based and resource-based policies.
Findings tell you about access that already exists. Policy validation and custom policy checks work earlier. They examine a policy before you deploy it, so you can stop a bad change in a pull request instead of finding it in production later.
Two kinds of pre-deployment checks
IAM Access Analyzer offers two kinds of checks that don't need an analyzer resource.
- Policy validation checks a policy against IAM policy grammar and AWS best practices. It returns findings of four types:
ERROR,SECURITY_WARNING,WARNINGandSUGGESTION. - Custom policy checks answer a specific yes/no question about what a policy allows. They use automated reasoning, so the result comes from analyzing what the policy permits rather than from matching text patterns.
Validating a policy
Save a policy to policy.json, then run:
aws accessanalyzer validate-policy \
--policy-type IDENTITY_POLICY \
--policy-document file://policy.jsonThe --policy-type value tells the service how to interpret the document. Common values are IDENTITY_POLICY, RESOURCE_POLICY and SERVICE_CONTROL_POLICY. Each finding in the response includes a findingType, an issueCode, a locations list that points to the statement, and a link to documentation on how to fix it.
Typical results include a misspelled action name, a Resource that doesn't match the action, or a Principal of * that is too broad. Treat ERROR and SECURITY_WARNING as build failures. Review WARNING and SUGGESTION findings, but you can usually let them pass.
Checking for new access
check-no-new-access compares two versions of a policy. It reports FAIL if the proposed policy allows anything the existing one does not.
aws accessanalyzer check-no-new-access \
--policy-type IDENTITY_POLICY \
--existing-policy-document file://policy-current.json \
--new-policy-document file://policy-proposed.jsonThe response has a result of PASS or FAIL, plus a message and a reasons list that explains a failure. This check suits changes that are meant to tighten permissions, such as the cleanups you make after reading unused access findings. If a "right-sizing" change fails this check, it accidentally grants something new.
Two related checks work on a single policy:
check-access-not-grantedconfirms that a policy does not allow a list of actions or resources that you name. For example, it can confirm that no policy grantsiam:PassRoleon*.check-no-public-accessconfirms that a resource policy does not make a supported resource type public.
Using the result in a pipeline
The check commands return a normal exit code even when the result is FAIL, so your script has to read the result field.
result=$(aws accessanalyzer check-no-new-access \
--policy-type IDENTITY_POLICY \
--existing-policy-document file://policy-current.json \
--new-policy-document file://policy-proposed.json \
--query 'result' --output text)
if [ "$result" != "PASS" ]; then
echo "Policy grants new access" >&2
exit 1
fiCommon errors
- Wrong policy type. Validating a resource policy as
IDENTITY_POLICYproduces misleading findings, because identity policies have noPrincipalelement. - Ignoring the
resultfield. A script that only checks the command's exit code will pass everyFAIL. - Checking only the final template. If your policies are generated from infrastructure-as-code, render the template first and check the resulting JSON.
- Expecting runtime data. These checks look only at the policy documents you supply. They don't know which permissions are used. For that, use unused access findings.
Exercise
A teammate submits a policy change meant to remove unused S3 permissions from a role. Which command confirms that the change did not add any new access, and what value in the response means it is safe?
Show answer
Run aws accessanalyzer check-no-new-access with the current policy as --existing-policy-document and the edited policy as --new-policy-document. A result of PASS means the proposed policy grants nothing that the existing policy did not. A FAIL means it grants something new, and the reasons list explains why.