forked from aws-cloudformation/cfn-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBase.py
83 lines (70 loc) · 2.92 KB
/
Base.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""
import datetime
from cfnlint.rules import CloudFormationLintRule
from cfnlint.rules import RuleMatch
class Base(CloudFormationLintRule):
"""Check Base Template Settings"""
id = 'E1001'
shortdesc = 'Basic CloudFormation Template Configuration'
description = 'Making sure the basic CloudFormation template components are properly configured'
source_url = 'https://github.com/aws-cloudformation/cfn-python-lint'
tags = ['base']
required_keys = ['Resources']
def __init__(self):
"""Init"""
super().__init__()
self.config_definition = {'sections': {'default': '', 'type': 'string'}}
self.configure()
def _validate_version(self, template):
results = []
valid_version = '2010-09-09'
if 'AWSTemplateFormatVersion' in template:
version = template.get('AWSTemplateFormatVersion')
if not isinstance(version, (str, datetime.date)):
message = 'AWSTemplateFormatVersion only valid value is {0}'
results.append(
RuleMatch(
['AWSTemplateFormatVersion'], message.format(valid_version)
)
)
else:
if (
version != valid_version
and version
!= datetime.datetime.strptime(valid_version, '%Y-%m-%d').date()
):
message = 'AWSTemplateFormatVersion only valid value is {0}'
results.append(
RuleMatch(
['AWSTemplateFormatVersion'], message.format(valid_version)
)
)
return results
def _validate_transform(self, transforms):
results = []
if not isinstance(transforms, (list, str)):
message = 'Transform has to be a list or string'
results.append(RuleMatch(['Transform'], message.format()))
return results
if not isinstance(transforms, list):
transforms = [transforms]
return results
def match(self, cfn):
matches = []
top_level = []
for x in cfn.template:
top_level.append(x)
if x not in cfn.sections and x != self.config['sections']:
message = 'Top level template section {0} is not valid'
matches.append(RuleMatch([x], message.format(x)))
for y in self.required_keys:
if y not in top_level:
message = 'Missing top level template section {0}'
matches.append(RuleMatch([y], message.format(y)))
matches.extend(self._validate_version(cfn.template))
if 'Transform' in cfn.template:
matches.extend(self._validate_transform(cfn.template.get('Transform')))
return matches