forked from aws-cloudformation/cfn-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSplit.py
70 lines (64 loc) · 3 KB
/
Split.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
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""
import six
from cfnlint.rules import CloudFormationLintRule
from cfnlint.rules import RuleMatch
class Split(CloudFormationLintRule):
"""Check if Split values are correct"""
id = 'E1018'
shortdesc = 'Split validation of parameters'
description = 'Making sure the split function is properly configured'
source_url = 'https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-split.html'
tags = ['functions', 'split']
def match(self, cfn):
matches = []
split_objs = cfn.search_deep_keys('Fn::Split')
supported_functions = [
'Fn::Base64',
'Fn::FindInMap',
'Fn::GetAZs',
'Fn::GetAtt',
'Fn::If',
'Fn::ImportValue',
'Fn::Join',
'Fn::Select',
'Fn::Sub',
'Ref',
]
for split_obj in split_objs:
split_value_obj = split_obj[-1]
tree = split_obj[:-1]
if isinstance(split_value_obj, list):
if len(split_value_obj) == 2:
split_delimiter = split_value_obj[0]
split_string = split_value_obj[1]
if not isinstance(split_delimiter, six.string_types):
message = 'Split delimiter has to be of type string for {0}'
matches.append(RuleMatch(
tree + [0], message.format('/'.join(map(str, tree)))))
if isinstance(split_string, dict):
if len(split_string) == 1:
for key, _ in split_string.items():
if key not in supported_functions:
message = 'Fn::Split doesn\'t support the function {0} at {1}'
matches.append(RuleMatch(
tree + [key], message.format(key, '/'.join(map(str, tree)))))
else:
message = 'Split list of singular function or string for {0}'
matches.append(RuleMatch(
tree, message.format('/'.join(map(str, tree)))))
elif not isinstance(split_string, six.string_types):
message = 'Split has to be of type string or valid function for {0}'
matches.append(RuleMatch(
tree, message.format('/'.join(map(str, tree)))))
else:
message = 'Split should be an array of 2 for {0}'
matches.append(RuleMatch(
tree, message.format('/'.join(map(str, tree)))))
else:
message = 'Split should be an array of 2 for {0}'
matches.append(RuleMatch(
tree, message.format('/'.join(map(str, tree)))))
return matches