forked from aws-cloudformation/cfn-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecordSetName.py
59 lines (52 loc) · 2.83 KB
/
RecordSetName.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
"""
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 RecordSetName(CloudFormationLintRule):
"""Check if a Route53 Resoruce Records Name is valid with a HostedZoneName"""
id = 'E3041'
shortdesc = 'RecordSet HostedZoneName is a superdomain of Name'
description = 'In a RecordSet, the HostedZoneName must be a superdomain of the Name being validated'
source_url = 'https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-route53-recordset.html#cfn-route53-recordset-name'
tags = ['resource', 'properties', 'route53']
def __init__(self):
""" Init """
super(RecordSetName, self).__init__()
self.resource_property_types = ['AWS::Route53::RecordSet']
def match_resource_properties(self, properties, _, path, cfn):
matches = []
property_sets = cfn.get_object_without_conditions(properties, ['Name', 'HostedZoneName'])
for property_set in property_sets:
props = property_set.get('Object')
scenario = property_set.get('Scenario')
name = props.get('Name', None)
hz_name = props.get('HostedZoneName', None)
if isinstance(name, six.string_types) and isinstance(hz_name, six.string_types):
if hz_name[-1] != '.':
message = 'HostedZoneName must end in a dot at {}'
if scenario is None:
matches.append(
RuleMatch(path[:] + ['HostedZoneName'], message.format('/'.join(map(str, path)))))
else:
scenario_text = ' and '.join(
['when condition "%s" is %s' % (k, v) for (k, v) in scenario.items()])
matches.append(
RuleMatch(path[:] + ['HostedZoneName'], message.format('/'.join(map(str, path)) + ' ' + scenario_text)))
if hz_name[-1] == '.':
hz_name = hz_name[:-1]
if name[-1] == '.':
name = name[:-1]
if hz_name not in [name, name[-len(hz_name):]]:
message = 'Name must be a superdomain of HostedZoneName at {}'
if scenario is None:
matches.append(
RuleMatch(path[:] + ['Name'], message.format('/'.join(map(str, path)))))
else:
scenario_text = ' and '.join(
['when condition "%s" is %s' % (k, v) for (k, v) in scenario.items()])
matches.append(
RuleMatch(path[:] + ['Name'], message.format('/'.join(map(str, path)) + ' ' + scenario_text)))
return matches