Skip to content

Commit 48760da

Browse files
authored
Provide configuration for exceptions to E3019 (#3972)
1 parent fd830c8 commit 48760da

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

src/cfnlint/rules/resources/PrimaryIdentifiers.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ class PrimaryIdentifiers(CloudFormationLintRule):
3131
source_url = "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resources-section-structure.html"
3232
tags = ["parameters", "resources"]
3333

34+
def __init__(self):
35+
super().__init__()
36+
37+
self.config_definition = {
38+
"exceptions": {
39+
"default": {
40+
"AWS::CodeBuild::Project": {
41+
"primaryIdentifier": ["/properties/Name"]
42+
}
43+
},
44+
"type": "object",
45+
"itemtype": "object",
46+
}
47+
}
48+
self.configure()
49+
3450
def _merge_conditions(
3551
self, conditions1: Dict[str, Set[bool]], conditions2: Dict[str, Set[bool]]
3652
):
@@ -139,16 +155,21 @@ def match(self, cfn: Template) -> RuleMatches:
139155
matches = []
140156
for t in tS:
141157
try:
142-
schema = PROVIDER_SCHEMA_MANAGER.get_resource_schema(cfn.regions[0], t)
158+
if t in self.config.get("exceptions", {}):
159+
schema = self.config.get("exceptions", {}).get(t)
160+
else:
161+
schema = PROVIDER_SCHEMA_MANAGER.get_resource_schema(
162+
cfn.regions[0], t
163+
).schema
143164

144165
# we are worried about primary identifiers that can be set
145166
# by the customer so if any primary identifiers are read
146167
# only we have to skip evaluation
147-
primary_ids = schema.schema.get("primaryIdentifier", [])
168+
primary_ids = schema.get("primaryIdentifier", [])
148169
if not primary_ids:
149170
continue
150171

151-
read_only_ids = schema.schema.get("readOnlyProperties", [])
172+
read_only_ids = schema.get("readOnlyProperties", [])
152173

153174
if any(id in read_only_ids for id in primary_ids):
154175
continue

test/fixtures/templates/bad/resources/primary_identifiers.yaml

+39
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,42 @@ Resources:
162162
Type: MyCompany::MODULE
163163
Properties:
164164
Attribute2: test
165+
Project1:
166+
Type: AWS::CodeBuild::Project
167+
Properties:
168+
Name: myProjectName
169+
ServiceRole: arn
170+
Artifacts:
171+
Type: no_artifacts
172+
Environment:
173+
Type: LINUX_CONTAINER
174+
ComputeType: BUILD_GENERAL1_SMALL
175+
Image: aws/codebuild/java:openjdk-8
176+
EnvironmentVariables:
177+
- Name: varName
178+
Type: varType
179+
Value: varValue
180+
Source:
181+
Location: codebuild-demo-test/0123ab9a371ebf0187b0fe5614fbb72c
182+
Type: S3
183+
TimeoutInMinutes: 10
184+
185+
Project2:
186+
Type: AWS::CodeBuild::Project
187+
Properties:
188+
Name: myProjectName
189+
ServiceRole: arn
190+
Artifacts:
191+
Type: no_artifacts
192+
Environment:
193+
Type: LINUX_CONTAINER
194+
ComputeType: BUILD_GENERAL1_SMALL
195+
Image: aws/codebuild/java:openjdk-8
196+
EnvironmentVariables:
197+
- Name: varName
198+
Type: varType
199+
Value: varValue
200+
Source:
201+
Location: codebuild-demo-test/0123ab9a371ebf0187b0fe5614fbb72c
202+
Type: S3
203+
TimeoutInMinutes: 10

test/unit/rules/resources/test_primary_identifiers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ def test_file_positive(self):
2727
def test_file_negative_alias(self):
2828
"""Test failure"""
2929
self.helper_file_negative(
30-
"test/fixtures/templates/bad/resources/primary_identifiers.yaml", 8
30+
"test/fixtures/templates/bad/resources/primary_identifiers.yaml", 10
3131
)

0 commit comments

Comments
 (0)