Skip to content

Commit d86124b

Browse files
authored
Disable and configure certain rules when template is from CDK (#2971)
* Disable certain rules when doing CDK
1 parent e0f78b2 commit d86124b

File tree

8 files changed

+173
-1
lines changed

8 files changed

+173
-1
lines changed

src/cfnlint/rules/resources/HardCodedArnProperties.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ def match_values(self, cfn):
7474
def match(self, cfn):
7575
matches = []
7676

77+
# Skip rule if CDK
78+
if cfn.is_cdk_template():
79+
return matches
80+
7781
transforms = cfn.transform_pre["Transform"]
7882
transforms = transforms if isinstance(transforms, list) else [transforms]
7983
if "AWS::Serverless-2016-10-31" in cfn.transform_pre["Transform"]:

src/cfnlint/rules/resources/properties/AvailabilityZone.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ def check(self, properties, resource_type, path, cfn):
6363
"""Check itself"""
6464
matches = []
6565

66+
# Skip rule if CDK
67+
if cfn.is_cdk_template():
68+
return matches
69+
6670
matches.extend(
6771
cfn.check_value(
6872
properties,

src/cfnlint/template/template.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,23 @@ def has_serverless_transform(self):
202202
)
203203
return bool(lang_extensions_transform in transform_type)
204204

205+
def is_cdk_template(self) -> bool:
206+
"""Check if the template was created by CDK"""
207+
resources = self.template.get("Resources")
208+
if not isinstance(resources, dict):
209+
return False
210+
211+
for _, properties in resources.items():
212+
if not isinstance(properties, dict):
213+
continue
214+
resource_type = properties.get("Type")
215+
if not isinstance(resource_type, str):
216+
continue
217+
if resource_type == "AWS::CDK::Metadata":
218+
return True
219+
220+
return False
221+
205222
def get_resources(self, resource_type=[]):
206223
"""
207224
Get Resources
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Resources:
2+
CdkMetadata:
3+
Type: AWS::CDK::Metadata
4+
Instance:
5+
Type: AWS::EC2::Subnet
6+
Properties:
7+
VpcId: vpc-1234567
8+
AvailabilityZone: us-east-1a
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
AWSTemplateFormatVersion: 2010-09-09
2+
Resources:
3+
CdkMetadata:
4+
Type: AWS::CDK::Metadata
5+
6+
S3BadBucket:
7+
Type: AWS::S3::Bucket
8+
Properties:
9+
AccessControl: Private
10+
NotificationConfiguration:
11+
TopicConfigurations:
12+
- Topic: !Sub arn:aws:sns:us-east-1:123456789012:TestTopic
13+
Event: s3:ReducedRedundancyLostObject
14+
15+
SampleBadBucketPolicy:
16+
Type: AWS::S3::BucketPolicy
17+
Properties:
18+
Bucket: !Ref S3BadBucket
19+
PolicyDocument:
20+
Statement:
21+
- Action:
22+
- s3:GetObject
23+
Effect: Allow
24+
Resource: !Sub arn:aws:s3:::${S3BadBucket}
25+
Principal: "*"
26+
27+
SampleRole:
28+
Type: AWS::IAM::Role
29+
Properties:
30+
AssumeRolePolicyDocument:
31+
Version: '2012-10-17'
32+
Statement:
33+
- Effect: Allow
34+
Principal:
35+
Service:
36+
- ec2.amazonaws.com
37+
Action:
38+
- 'sts:AssumeRole'
39+
Path: /
40+
41+
42+
SampleBadIAMPolicy1:
43+
Type: AWS::IAM::ManagedPolicy
44+
Properties:
45+
PolicyDocument:
46+
Version: '2012-10-17'
47+
Statement:
48+
- Effect: Allow
49+
Action:
50+
- sns:Publish
51+
Resource: !Sub arn:${AWS::Partition}:sns:us-east-1:${AWS::AccountId}:TestTopic
52+
Roles:
53+
- !Ref SampleRole
54+
55+
SampleBadIAMPolicy2:
56+
Type: AWS::IAM::ManagedPolicy
57+
Properties:
58+
PolicyDocument:
59+
Version: '2012-10-17'
60+
Statement:
61+
- Effect: Allow
62+
Action:
63+
- sns:Publish
64+
Resource:
65+
- !Sub arn:${AWS::Partition}:sns:us-east-1:${AWS::AccountId}:TestTopic
66+
- !Sub arn:${AWS::Partition}:sns:${AWS::Region}:${AWS::AccountId}:TestTopic
67+
Roles:
68+
- !Ref SampleRole
69+
70+
SampleBadIAMPolicy3:
71+
Type: AWS::IAM::ManagedPolicy
72+
Properties:
73+
PolicyDocument:
74+
Version: '2012-10-17'
75+
Statement:
76+
- Effect: Allow
77+
Action:
78+
- sns:Publish
79+
Resource:
80+
- !Sub arn:${AWS::Partition}:sns:${AWS::Partition}:${AWS::AccountId}:TestTopic
81+
Roles:
82+
- !Ref SampleRole

test/unit/module/test_template.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,3 +1254,58 @@ def test_schemas(self):
12541254
},
12551255
self.template.get_valid_getatts(),
12561256
)
1257+
1258+
def test_is_cdk_bad_type(self):
1259+
template = {
1260+
"Resources": {
1261+
"CDK": {
1262+
"Type": ["AWS::CDK::Metadata"],
1263+
"Properties": {
1264+
"AssumeRolePolicyDocument": {
1265+
"Version": "2012-10-17",
1266+
}
1267+
},
1268+
}
1269+
},
1270+
}
1271+
1272+
template = Template("test.yaml", template)
1273+
self.assertFalse(template.is_cdk_template())
1274+
1275+
def test_is_cdk_bad_resources(self):
1276+
template = {
1277+
"Resources": [
1278+
{
1279+
"CDK": {
1280+
"Type": ["AWS::CDK::Metadata"],
1281+
"Properties": {
1282+
"AssumeRolePolicyDocument": {
1283+
"Version": "2012-10-17",
1284+
}
1285+
},
1286+
}
1287+
}
1288+
],
1289+
}
1290+
1291+
template = Template("test.yaml", template)
1292+
self.assertFalse(template.is_cdk_template())
1293+
1294+
def test_is_cdk_bad_resource_props(self):
1295+
template = {
1296+
"Resources": {
1297+
"CDK": [
1298+
{
1299+
"Type": ["AWS::CDK::Metadata"],
1300+
"Properties": {
1301+
"AssumeRolePolicyDocument": {
1302+
"Version": "2012-10-17",
1303+
}
1304+
},
1305+
}
1306+
]
1307+
},
1308+
}
1309+
1310+
template = Template("test.yaml", template)
1311+
self.assertFalse(template.is_cdk_template())

test/unit/rules/resources/properties/test_availability_zone.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ def setUp(self):
1818
super(TestPropertyAvailabilityZone, self).setUp()
1919
self.collection.register(AvailabilityZone())
2020
self.success_templates = [
21-
"test/fixtures/templates/good/resources/properties/az.yaml"
21+
"test/fixtures/templates/good/resources/properties/az.yaml",
22+
"test/fixtures/templates/good/resources/properties/az_cdk.yaml",
2223
]
2324

2425
def test_file_positive(self):

test/unit/rules/resources/test_hardcodedarnproperties.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def setUp(self):
1919
self.collection.register(HardCodedArnProperties())
2020
self.success_templates = [
2121
"test/fixtures/templates/good/resources/properties/hard_coded_arn_properties_sam.yaml",
22+
"test/fixtures/templates/good/resources/properties/hard_coded_arn_properties_cdk.yaml",
2223
]
2324

2425
def test_file_positive(self):

0 commit comments

Comments
 (0)