forked from aws-cloudformation/cfn-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.py
53 lines (42 loc) · 1.69 KB
/
Config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""
from cfnlint.rules import CloudFormationLintRule
from cfnlint.rules import RuleMatch
class Config(CloudFormationLintRule):
"""Check if Metadata configuration is properly configured"""
id = 'E4002'
shortdesc = 'Validate the configuration of the Metadata section'
description = 'Validates that Metadata section is an object and has no null values'
source_url = 'https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/metadata-section-structure.html'
tags = ['metadata']
def _check_object(self, obj, path):
results = []
if isinstance(obj, (dict)):
for k, v in obj.items():
results.extend(self._check_object(v, path + [k]))
if isinstance(obj, (list)):
for i, v in enumerate(obj):
results.extend(self._check_object(v, path + [i]))
if obj is None:
message = 'Metadata value cannot be null'
results.append(RuleMatch(
path,
message.format(message)
))
return results
def match(self, cfn):
"""Check CloudFormation Metadata Interface Configuration"""
matches = []
metadata_obj = cfn.template.get('Metadata', {})
if metadata_obj is None:
message = 'Metadata value has to be an object'
matches.append(RuleMatch(
['Metadata'],
message.format(message)
))
if metadata_obj:
if isinstance(metadata_obj, dict):
matches.extend(self._check_object(metadata_obj, ['Metadata']))
return matches