-
Notifications
You must be signed in to change notification settings - Fork 594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Iam sid errors #1812
Open
jnewbigin
wants to merge
5
commits into
aws-cloudformation:main
Choose a base branch
from
jnewbigin:iam_sid_errors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Iam sid errors #1812
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
33ec340
Add Sid validation
jnewbigin 70cb305
Merge remote-tracking branch 'origin/master' into iam_sid_errors
jnewbigin 05d350f
fix linting issues
jnewbigin 52ad4d4
Merge remote-tracking branch 'upstream/master' into iam_sid_errors
jnewbigin 1fde08d
Remove non-IAM resources which have different rules
jnewbigin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
""" | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: MIT-0 | ||
""" | ||
import re | ||
from cfnlint.rules import CloudFormationLintRule | ||
from cfnlint.rules import RuleMatch | ||
|
||
|
||
class Sid(CloudFormationLintRule): | ||
"""Check if IAM Policy Sid is correct""" | ||
id = 'W2512' | ||
shortdesc = 'Check IAM Resource Policies Sid syntax' | ||
description = 'See if the elements inside an IAM Resource policy are configured correctly.' | ||
source_url = 'https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_grammar.html' | ||
tags = ['properties', 'iam'] | ||
|
||
def __init__(self): | ||
"""Init""" | ||
super(Sid, self).__init__() | ||
self.idp_and_keys = { | ||
'AWS::IAM::Group': 'Policies', | ||
'AWS::IAM::ManagedPolicy': 'PolicyDocument', | ||
'AWS::IAM::Policy': 'PolicyDocument', | ||
'AWS::IAM::Role': 'Policies', | ||
'AWS::IAM::User': 'Policies', | ||
} | ||
for resource_type in self.idp_and_keys: | ||
self.resource_property_types.append(resource_type) | ||
self.sid_regex = re.compile('^[A-Za-z0-9]*$') | ||
|
||
def check_policy_document(self, value, path): | ||
"""Check policy document""" | ||
sids = [] # sids must be unique | ||
matches = [] | ||
|
||
if not isinstance(value, dict): | ||
return matches | ||
|
||
for e_v, e_p in value.items_safe(path[:]): | ||
for p_vs, p_p in e_v.items_safe(e_p[:]): | ||
statements = p_vs.get('Statement', []) | ||
if 'get' in dir(statements): | ||
statements = [statements] | ||
for statement in statements: | ||
if 'Sid' in statement: | ||
sid = statement.get('Sid') | ||
if self.sid_regex.search(sid) is None: | ||
message = 'basic alphanumeric characters (A-Z,a-z,0-9) are the only allowed characters in the Sid value' | ||
matches.append(RuleMatch(p_p + ['Sid'], message)) | ||
if sid in sids: | ||
message = 'IAM Sid values should be unique' | ||
matches.append(RuleMatch(p_p + ['Sid'], message)) | ||
else: | ||
sids.append(sid) | ||
|
||
return matches | ||
|
||
def match_resource_properties(self, properties, resourcetype, path, cfn): | ||
"""Check CloudFormation Properties""" | ||
matches = [] | ||
|
||
key = self.idp_and_keys.get(resourcetype) | ||
|
||
if key == 'Policies': | ||
for index, policy in enumerate(properties.get(key, [])): | ||
matches.extend( | ||
cfn.check_value( | ||
obj=policy, key='PolicyDocument', | ||
path=path[:] + ['Policies', index], | ||
check_value=self.check_policy_document, | ||
)) | ||
else: | ||
matches.extend( | ||
cfn.check_value( | ||
obj=properties, key=key, | ||
path=path[:], | ||
check_value=self.check_policy_document | ||
)) | ||
|
||
return matches |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
--- | ||
AWSTemplateFormatVersion: "2010-09-09" | ||
Description: > | ||
Test bad Policy Version | ||
Resources: | ||
rIamRole: | ||
Type: AWS::IAM::Role | ||
Properties: | ||
AssumeRolePolicyDocument: {} | ||
Policies: | ||
- PolicyName: String | ||
PolicyDocument: | ||
Version: "2012-10-17" | ||
Statement: | ||
- Sid: "invalid regex" | ||
Effect: "Allow" | ||
Action: | ||
- "s3:ListBucket" | ||
Resource: '*' | ||
- Effect: "Allow" | ||
Sid: "Sid1" | ||
Action: | ||
- "s3:GetBucketLocation" | ||
Resource: '*' | ||
- Effect: "Allow" | ||
Sid: "Sid1" | ||
Action: | ||
- "s3:GetObject" | ||
Resource: '*' |
19 changes: 19 additions & 0 deletions
19
test/fixtures/templates/bad/resources/iam/policy_sid_2.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
AWSTemplateFormatVersion: "2010-09-09" | ||
Description: > | ||
Test bad Policy Version | ||
Resources: | ||
rIamRole: | ||
Type: AWS::IAM::Role | ||
Properties: | ||
AssumeRolePolicyDocument: {} | ||
Policies: | ||
- PolicyName: String | ||
PolicyDocument: | ||
Version: "2012-10-17" | ||
Statement: | ||
Sid: "invalid regex" | ||
Effect: "Allow" | ||
Action: | ||
- "s3:ListBucket" | ||
Resource: '*' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
""" | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: MIT-0 | ||
""" | ||
from test.unit.rules import BaseRuleTestCase | ||
from cfnlint.rules.resources.iam.Sid import Sid # pylint: disable=E0401 | ||
|
||
|
||
class TestSid(BaseRuleTestCase): | ||
"""Test IAM Resource Policies""" | ||
|
||
def setUp(self): | ||
"""Setup""" | ||
super(TestSid, self).setUp() | ||
self.collection.register(Sid()) | ||
self.success_templates = [ | ||
#'test/fixtures/templates/good/resources/iam/resource_policy.yaml', | ||
'test/fixtures/templates/good/resources/iam/policy.yaml', | ||
] | ||
|
||
def test_file_positive(self): | ||
"""Test Positive""" | ||
self.helper_file_positive() | ||
|
||
def test_file_negative(self): | ||
"""Test failure""" | ||
self.helper_file_negative( | ||
'test/fixtures/templates/bad/resources/iam/policy_sid.yaml', 2) | ||
self.helper_file_negative( | ||
'test/fixtures/templates/bad/resources/iam/policy_sid_2.yaml', 1) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SIDs for ManagedPolicies have a different regex than SIDs in an inline policy, IIRC. I believe spaces are allowed in an inline policy SID
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar discussion here: #708
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I started working off the cloudformation error I revieved on my stack.
Then I turned to the AWS docs to try and find more details.
The docs are pretty vague - particularly for resource policies
If I get a chance I'll set up some test templates and try them out. Seems to be where #708 got to
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can confirm that ECR and KMS policies do not limit to that regex or require uniqueness.
I have not tested the other types but it seems that the rules are definitely different between resource types.
Given that, I think for now these rules should only apply to IAM resources.
I will update my PR to reflect that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still to confirm the correctness for inline policies