forked from aws-cloudformation/cfn-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new rule to validate a managed policy description against a reg…
…ex (aws-cloudformation#484)
- Loading branch information
Showing
6 changed files
with
139 additions
and
6 deletions.
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
58 changes: 58 additions & 0 deletions
58
src/cfnlint/rules/resources/iam/ManagedPolicyDescription.py
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,58 @@ | ||
""" | ||
Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of this | ||
software and associated documentation files (the "Software"), to deal in the Software | ||
without restriction, including without limitation the rights to use, copy, modify, | ||
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | ||
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | ||
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
""" | ||
import regex | ||
from cfnlint import CloudFormationLintRule | ||
from cfnlint import RuleMatch | ||
|
||
|
||
class ManagedPolicyDescription(CloudFormationLintRule): | ||
"""Check if IAM Policy Description is syntax correct""" | ||
id = 'E3507' | ||
shortdesc = 'Check if IAM Managed Policy description follows supported regex' | ||
description = 'IAM Managed Policy description much comply with the regex [\\p{L}\\p{M}\\p{Z}\\p{S}\\p{N}\\p{P}]*' | ||
source_url = 'https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-iam-managedpolicy.html' | ||
tags = ['properties', 'iam'] | ||
|
||
def __init__(self): | ||
"""Init""" | ||
super(ManagedPolicyDescription, self).__init__() | ||
self.resource_property_types.append('AWS::IAM::ManagedPolicy') | ||
|
||
def check_value(self, value, path): | ||
"""Check the value""" | ||
regex_string = r'^[\p{L}\p{M}\p{Z}\p{S}\p{N}\p{P}]+$' | ||
r = regex.compile(regex_string) | ||
if not r.match(value): | ||
message = 'ManagedPolicy Description needs to follow regex pattern "{0}"' | ||
return [ | ||
RuleMatch(path[:], message.format(regex_string)) | ||
] | ||
|
||
return [] | ||
|
||
def match_resource_properties(self, properties, _, path, cfn): | ||
"""Check CloudFormation Properties""" | ||
matches = [] | ||
|
||
matches.extend( | ||
cfn.check_value( | ||
obj=properties, key='Description', | ||
path=path[:], | ||
check_value=self.check_value | ||
)) | ||
|
||
return matches |
17 changes: 17 additions & 0 deletions
17
test/fixtures/templates/bad/resources/iam/managed_policy_description.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,17 @@ | ||
--- | ||
AWSTemplateFormatVersion: "2010-09-09" | ||
Resources: | ||
SomeManagedPolicy: | ||
Type: "AWS::IAM::ManagedPolicy" | ||
Properties: | ||
Description: | | ||
Example1 | ||
Managed Policy | ||
PolicyDocument: | ||
Version: "2012-10-17" | ||
Statement: | ||
- Effect: "Allow" | ||
Action: | ||
- "s3:ListBucket" | ||
- "s3:GetObject" | ||
Resource: '*' |
16 changes: 16 additions & 0 deletions
16
test/fixtures/templates/good/resources/iam/managed_policy_description.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,16 @@ | ||
--- | ||
AWSTemplateFormatVersion: "2010-09-09" | ||
Resources: | ||
SomeManagedPolicy: | ||
Type: "AWS::IAM::ManagedPolicy" | ||
Properties: | ||
Description: | | ||
Example1? Managed Policy | ||
PolicyDocument: | ||
Version: "2012-10-17" | ||
Statement: | ||
- Effect: "Allow" | ||
Action: | ||
- "s3:ListBucket" | ||
- "s3:GetObject" | ||
Resource: '*' |
37 changes: 37 additions & 0 deletions
37
test/rules/resources/iam/test_iam_managed_policy_description.py
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,37 @@ | ||
""" | ||
Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of this | ||
software and associated documentation files (the "Software"), to deal in the Software | ||
without restriction, including without limitation the rights to use, copy, modify, | ||
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | ||
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | ||
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
""" | ||
from cfnlint.rules.resources.iam.ManagedPolicyDescription import ManagedPolicyDescription # pylint: disable=E0401 | ||
from ... import BaseRuleTestCase | ||
|
||
|
||
class TestManagedPolicyDescription(BaseRuleTestCase): | ||
"""Test Managed Policy Description""" | ||
def setUp(self): | ||
"""Setup""" | ||
super(TestManagedPolicyDescription, self).setUp() | ||
self.collection.register(ManagedPolicyDescription()) | ||
self.success_templates = [ | ||
'fixtures/templates/good/resources/iam/managed_policy_description.yaml' | ||
] | ||
|
||
def test_file_positive(self): | ||
"""Test Positive""" | ||
self.helper_file_positive() | ||
|
||
def test_file_negative(self): | ||
"""Test failure""" | ||
self.helper_file_negative('fixtures/templates/bad/resources/iam/managed_policy_description.yaml', 1) |
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