# ################################### ## Rule Specification ## ##################################### # # Rule Name: # iam_managed_policy_no_statements_with_admin_access_check # # Description: # This control checks whether AWS Identity and Access Management (IAM) customer managed policies do not include "Effect": "Allow" with "Action": "*" over "Resource": "*". # # Reports on: # AWS::IAM::ManagedPolicy # # Evaluates: # AWS CloudFormation, AWS CloudFormation hook # # Rule Parameters: # None # # Scenarios: # Scenario: 1 # Given: The input document is an AWS CloudFormation or CloudFormation hook document # And: The input document does not contain any IAM managed policy resources # Then: SKIP # Scenario: 2 # Given: The input document is an AWS CloudFormation or CloudFormation hook document # And: The input document contains an IAM managed policy resource # And: The policy has no statements with 'Effect' set to 'Allow' # Then: SKIP # Scenario: 3 # Given: The input document is an AWS CloudFormation or CloudFormation hook document # And: The input document contains an IAM managed policy resource # And: The policy has a statement with 'Effect' set to 'Allow' # And: The policy does not have both Action and Resource statements # Then: SKIP # Scenario: 4 # Given: The input document is an AWS CloudFormation or CloudFormation hook document # And: The input document contains an IAM managed policy resource # And: The policy has a statement with 'Effect' set to 'Allow' # And: The policy statement has one or more Action statements and one or more Resource statements # And: Within a single policy statement at least one Action statement allows all actions (Action value of '*') # And: Within the same policy statement at least one Resource statement is a wildcard representing all # resources (Resource value of '*') # Then: FAIL # Scenario: 5 # Given: The input document is an AWS CloudFormation or CloudFormation hook document # And: The input document contains an IAM managed policy resource # And: The policy has a statement with 'Effect' set to 'Allow' # And: The policy has one or more Action statements and one or more Resource statements # And: Within a single policy statement no Action statements allow all actions (Action value of '*') # And: Within the same policy statement no Resources are wildcards representing all # resources (Resource value of '*') # Then: PASS # # Constants # let AWS_IAM_MANAGED_POLICY_TYPE = "AWS::IAM::ManagedPolicy" let INPUT_DOCUMENT = this # # Assignments # let iam_managed_policies = Resources.*[ Type == %AWS_IAM_MANAGED_POLICY_TYPE ] # # Primary Rules # rule iam_managed_policy_no_statements_with_admin_access_check when is_cfn_template(%INPUT_DOCUMENT) %iam_managed_policies not empty { check(%iam_managed_policies.Properties) << [CT.IAM.PR.2]: Require that AWS Identity and Access Management (IAM) customer-managed policies do not contain a statement that includes "*" in the Action and Resource elements [FIX]: Remove AWS IAM inline policy statements with "Effect": "Allow" that permit "Action": "*" over "Resource": "*". >> } rule iam_managed_policy_no_statements_with_admin_access_check when is_cfn_hook(%INPUT_DOCUMENT, %AWS_IAM_MANAGED_POLICY_TYPE) { check(%INPUT_DOCUMENT.%AWS_IAM_MANAGED_POLICY_TYPE.resourceProperties) << [CT.IAM.PR.2]: Require that AWS Identity and Access Management (IAM) customer-managed policies do not contain a statement that includes "*" in the Action and Resource elements [FIX]: Remove AWS IAM inline policy statements with "Effect": "Allow" that permit "Action": "*" over "Resource": "*". >> } # # Parameterized Rules # rule check(policy) { %policy [ filter_policy_document_with_statement_provided(this) ] { PolicyDocument { check_statement(Statement) } } } rule check_statement(statement) { %statement [ filter_allow_on_action_and_resource(this) ] { Action exists Resource exists check_admin_access(Action, Resource) } } rule filter_allow_on_action_and_resource(statement) { %statement { Effect == "Allow" Action exists Resource exists } } rule filter_policy_document_with_statement_provided(policy) { %policy { PolicyDocument exists PolicyDocument is_struct PolicyDocument { Statement exists filter_statement_non_empty_list(Statement) or Statement is_struct } } } rule filter_statement_non_empty_list(statement) { %statement { this is_list this not empty } } rule check_admin_access(actions, resources) { when some %actions[*] == "*" { %resources[*] != "*" } } # # Utility Rules # rule is_cfn_template(doc) { %doc { AWSTemplateFormatVersion exists or Resources exists } } rule is_cfn_hook(doc, RESOURCE_TYPE) { %doc.%RESOURCE_TYPE.resourceProperties exists }