File size: 5,835 Bytes
b40c49c | 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | """
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""
import regex as re
from cfnlint._typing import RuleMatches
from cfnlint.helpers import VALID_PARAMETER_TYPES_LIST
from cfnlint.rules import CloudFormationLintRule, RuleMatch
from cfnlint.template import Template
class Default(CloudFormationLintRule):
"""Check if Parameters are configured correctly"""
id = "E2015"
shortdesc = "Default value is within parameter constraints"
description = (
"Making sure the parameters have a default value inside AllowedValues,"
" MinValue, MaxValue, AllowedPattern"
)
source_url = "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html"
tags = ["parameters"]
def check_allowed_pattern(self, allowed_value, allowed_pattern, path):
"""
Check allowed value against allowed pattern
"""
message = "Default should be allowed by AllowedPattern"
try:
if not re.match(allowed_pattern, str(allowed_value)):
return [RuleMatch(path, message)]
except re.error as ex:
self.logger.debug(
'Regex pattern "%s" isn\'t supported by Python: %s', allowed_pattern, ex
)
return []
def check_min_value(self, allowed_value, min_value, path):
"""
Check allowed value against min value
"""
message = "Default should be equal to or higher than MinValue"
if isinstance(allowed_value, int) and isinstance(min_value, int):
if allowed_value < min_value:
return [RuleMatch(path, message)]
return []
def check_max_value(self, allowed_value, max_value, path):
"""
Check allowed value against max value
"""
message = "Default should be less than or equal to MaxValue"
if isinstance(allowed_value, int) and isinstance(max_value, int):
if allowed_value > max_value:
return [RuleMatch(path, message)]
return []
def check_allowed_values(self, allowed_value, allowed_values, path):
"""
Check allowed value against allowed values
"""
message = "Default should be a value within AllowedValues"
if allowed_value not in allowed_values:
return [RuleMatch(path, message)]
return []
def check_min_length(self, allowed_value, min_length, path):
"""
Check allowed value against MinLength
"""
message = "Default should have a length above or equal to MinLength"
value = allowed_value if isinstance(allowed_value, str) else str(allowed_value)
if isinstance(min_length, int):
if len(value) < min_length:
return [RuleMatch(path, message)]
return []
def check_max_length(self, allowed_value, max_length, path):
"""
Check allowed value against MaxLength
"""
message = "Default should have a length below or equal to MaxLength"
value = allowed_value if isinstance(allowed_value, str) else str(allowed_value)
if isinstance(max_length, int):
if len(value) > max_length:
return [RuleMatch(path, message)]
return []
def match_default_value(self, paramname, paramvalue, default_value):
matches = []
if default_value is not None:
path = ["Parameters", paramname, "Default"]
allowed_pattern = paramvalue.get("AllowedPattern")
if allowed_pattern:
matches.extend(
self.check_allowed_pattern(default_value, allowed_pattern, path)
)
min_value = paramvalue.get("MinValue")
if min_value:
matches.extend(self.check_min_value(default_value, min_value, path))
max_value = paramvalue.get("MaxValue")
if max_value is not None:
matches.extend(self.check_max_value(default_value, max_value, path))
allowed_values = paramvalue.get("AllowedValues")
if allowed_values:
matches.extend(
self.check_allowed_values(default_value, allowed_values, path)
)
min_length = paramvalue.get("MinLength")
if min_length is not None:
matches.extend(self.check_min_length(default_value, min_length, path))
max_length = paramvalue.get("MaxLength")
if max_length is not None:
matches.extend(self.check_max_length(default_value, max_length, path))
return matches
def is_list(self, paramvalue):
return paramvalue.get("Type") in VALID_PARAMETER_TYPES_LIST
def match(self, cfn: Template) -> RuleMatches:
matches = []
for paramname, paramvalue in cfn.get_parameters_valid().items():
param_cdl_matches = []
param_matches = []
default_value = paramvalue.get("Default")
if default_value is not None and self.is_list(paramvalue):
comma_delimited_default_values = [
x.strip() for x in default_value.split(",")
]
for value in comma_delimited_default_values:
param_cdl_matches.extend(
self.match_default_value(paramname, paramvalue, value)
)
if param_cdl_matches or not self.is_list(paramvalue):
param_matches.extend(
self.match_default_value(paramname, paramvalue, default_value)
)
if param_matches:
matches.extend(param_matches)
matches.extend(param_cdl_matches)
return matches
|