Skip to content

Commit 0f8532f

Browse files
author
Kevin Formsma
authored
Update PassRole rules to not trigger on deny statements. Fixes #584 (#586)
1 parent 0de7906 commit 0f8532f

File tree

6 files changed

+71
-12
lines changed

6 files changed

+71
-12
lines changed

lib/cfn-nag/custom_rules/IamRolePassRoleWildcardResourceRule.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def audit_impl(cfn_model)
2323
violating_roles = cfn_model.resources_by_type('AWS::IAM::Role').select do |role|
2424
violating_policies = role.policy_objects.select do |policy|
2525
violating_statements = policy.policy_document.statements.select do |statement|
26-
passrole_action?(statement) && wildcard_resource?(statement)
26+
statement.effect == 'Allow' && passrole_action?(statement) && wildcard_resource?(statement)
2727
end
2828
!violating_statements.empty?
2929
end

lib/cfn-nag/custom_rules/passrole_base_rule.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def audit_impl(cfn_model)
1616

1717
violating_policies = policies.select do |policy|
1818
violating_statements = policy.policy_document.statements.select do |statement|
19-
passrole_action?(statement) && wildcard_resource?(statement)
19+
statement.effect == 'Allow' && passrole_action?(statement) && wildcard_resource?(statement)
2020
end
2121
!violating_statements.empty?
2222
end

spec/custom_rules/SPCMRule_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
rule = SPCMRule.new
3939
rule.spcm_threshold = 1
4040
actual_logical_resource_ids = rule.audit_impl cfn_model
41-
expected_logical_resource_ids = %w[InlinePolicyPass]
41+
expected_logical_resource_ids = %w[InlinePolicyPass InlinePolicyDenyPass]
4242

4343
expect(actual_logical_resource_ids).to eq expected_logical_resource_ids
4444
end

spec/test_templates/yaml/iam_passrole_resource_wildcard/iam_inline_policy_passrole_resource_wildcard_pass.yml

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Resources:
33

44
GenericGroup:
55
Type: AWS::IAM::Group
6-
Properties:
6+
Properties:
77
GroupName: GenericGroup
88

99
InlinePolicyPass:
@@ -15,7 +15,7 @@ Resources:
1515
Statement:
1616
-
1717
Effect: "Allow"
18-
Action:
18+
Action:
1919
- "s3:ListBucket"
2020
- "s3:GetBucketLocation"
2121
Resource: "arn:aws:s3:::*"
@@ -29,5 +29,31 @@ Resources:
2929
Effect: Allow
3030
Action: "iam:PassRole"
3131
Resource: "arn:aws:s3:::*"
32+
Groups:
33+
- !Ref GenericGroup
34+
35+
InlinePolicyDenyPass:
36+
Type: "AWS::IAM::Policy"
37+
Properties:
38+
PolicyName: WildcardDenyResourcePolicy
39+
PolicyDocument:
40+
Version: "2012-10-17"
41+
Statement:
42+
-
43+
Effect: "Allow"
44+
Action:
45+
- "s3:ListBucket"
46+
- "s3:GetBucketLocation"
47+
Resource: "arn:aws:s3:::*"
48+
-
49+
Effect: Allow
50+
Action:
51+
- "s3:ListBucket"
52+
- "s3:GetBucketLocation"
53+
Resource: "*"
54+
-
55+
Effect: Deny
56+
Action: "iam:PassRole"
57+
Resource: "*"
3258
Groups:
3359
- !Ref GenericGroup

spec/test_templates/yaml/iam_passrole_resource_wildcard/iam_managed_policy_passrole_resource_wildcard_pass.yml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Resources:
33

44
GenericGroup:
55
Type: AWS::IAM::Group
6-
Properties:
6+
Properties:
77
GroupName: GenericGroup
88

99
ManagedPolicyPass1:
@@ -14,7 +14,7 @@ Resources:
1414
Statement:
1515
-
1616
Effect: "Allow"
17-
Action:
17+
Action:
1818
- "s3:ListBucket"
1919
- "s3:GetBucketLocation"
2020
Resource: "arn:aws:s3:::*"
@@ -31,7 +31,7 @@ Resources:
3131
- "s3:ListBucket"
3232
- "s3:GetBucketLocation"
3333
Resource: "*"
34-
34+
3535
ManagedPolicyPass3:
3636
Type: "AWS::IAM::ManagedPolicy"
3737
Properties:
@@ -43,4 +43,15 @@ Resources:
4343
Action: "iam:PassRole"
4444
Resource: "arn:aws:s3:::*"
4545
Groups:
46-
- !Ref GenericGroup
46+
- !Ref GenericGroup
47+
48+
ManagedPolicyPass4:
49+
Type: "AWS::IAM::ManagedPolicy"
50+
Properties:
51+
PolicyDocument:
52+
Version: "2012-10-17"
53+
Statement:
54+
-
55+
Effect: Deny
56+
Action: "iam:PassRole"
57+
Resource: "*"

spec/test_templates/yaml/iam_passrole_resource_wildcard/iam_role_passrole_resource_wildcard_pass.yml

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
11
---
22
Resources:
3-
3+
RoleDeny:
4+
Type: AWS::IAM::Role
5+
Properties:
6+
AssumeRolePolicyDocument:
7+
Version: "2012-10-17"
8+
Statement:
9+
-
10+
Effect: Allow
11+
Principal:
12+
Service:
13+
- cloudformation.amazonaws.com
14+
Action:
15+
- sts:AssumeRole
16+
Policies:
17+
-
18+
PolicyName: PolicyDeny
19+
PolicyDocument:
20+
Version: "2012-10-17"
21+
Statement:
22+
-
23+
Effect: Deny
24+
Action: "iam:PassRole"
25+
Resource: "*"
426
RoleFail:
5-
Type: AWS::IAM::Role
27+
Type: AWS::IAM::Role
628
Properties:
729
AssumeRolePolicyDocument:
830
Version: "2012-10-17"
@@ -22,7 +44,7 @@ Resources:
2244
Statement:
2345
-
2446
Effect: "Allow"
25-
Action:
47+
Action:
2648
- "s3:ListBucket"
2749
- "s3:GetBucketLocation"
2850
Resource: "arn:aws:s3:::*"

0 commit comments

Comments
 (0)