File size: 4,497 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 | """
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""
from collections import deque
from typing import Any
from cfnlint.helpers import REGEX_DYN_REF
from cfnlint.jsonschema import ValidationError, Validator
from cfnlint.rules.functions._BaseFn import BaseFn
_all = {
"items": [
{"type": "string", "const": "resolve"},
{"type": "string", "enum": ["ssm", "ssm-secure", "secretsmanager"]},
],
"minItems": 3,
}
_ssm = {
"items": [
{"type": "string", "const": "resolve"},
{"type": "string", "enum": ["ssm", "ssm-secure"]},
{"type": "string", "pattern": "[a-zA-Z0-9_.-/]+"},
{"type": "string", "pattern": "\\d+"},
],
"maxItems": 4,
"type": "array",
}
_secrets_manager = {
"items": [
{"type": "string", "const": "resolve"},
{"type": "string", "const": "secretsmanager"},
{"type": "string", "pattern": "[ -~]*"}, # secret-id
{"type": "string", "enum": ["SecretString", ""]}, # secret-string
{"type": "string"}, # json-key
{"type": "string"}, # version-stage or version-id
{"type": "string"}, # version-stage or version-id
],
"minItems": 3,
"maxItems": 7,
"type": "array",
}
_secrets_manager_arn = {
"items": [
{"type": "string", "const": "resolve"},
{"type": "string", "const": "secretsmanager"},
{"type": "string", "const": "arn"}, # arn
{"type": "string"}, # partition
{"type": "string"}, # service
{"type": "string"}, # region
{"type": "string"}, # account ID
{"type": "string", "const": "secret"}, # secret
{"type": "string"}, # secret
{"type": "string", "enum": ["SecretString", ""]}, # secret-string
{"type": "string"}, # json-key
{"type": "string"}, # version-stage or version-id
{"type": "string"}, # version-stage or version-id
],
"minItems": 9,
"maxItems": 13,
"type": "array",
}
class DynamicReference(BaseFn):
id = "E1050"
shortdesc = "Validate the structure of a dynamic reference"
description = "Make sure dynamic reference strings have the correct syntax"
source_url = "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/dynamic-references.html"
tags = ["functions", "dynamic reference"]
def __init__(self) -> None:
super().__init__()
self.child_rules = {
"E1051": None,
"E1027": None,
}
def _clean_errors(self, err: ValidationError) -> ValidationError:
err.rule = self
err.path = deque([])
err.schema_path = deque([])
return err
def dynamicReference(
self, validator: Validator, s: Any, instance: Any, schema: Any
):
if not validator.is_type(instance, "string"):
return
# SSM parameters can be used in Resources and Outputs and Parameters
# SSM secrets are only used in a small number of locations
# Secrets manager can be used only in resource properties
validator = validator.evolve(
function_filter=validator.function_filter.evolve(
add_cfn_lint_keyword=False,
),
)
for v in REGEX_DYN_REF.findall(instance):
parts = v.split(":")
found = False
evolved = validator.evolve(schema=_all)
for err in evolved.iter_errors(parts):
yield self._clean_errors(err)
found = True
if found:
continue
if parts[1] == "ssm":
evolved = validator.evolve(schema=_ssm)
elif parts[1] == "ssm-secure":
evolved = validator.evolve(schema=_ssm)
rule = self.child_rules["E1027"]
if rule and hasattr(rule, "validate"):
yield from rule.validate(validator, {}, v, schema)
else:
if parts[2] == "arn":
evolved = validator.evolve(schema=_secrets_manager_arn)
else: # this is secrets manager
evolved = validator.evolve(schema=_secrets_manager)
rule = self.child_rules["E1051"]
if rule and hasattr(rule, "validate"):
yield from rule.validate(validator, {}, v, schema)
for err in evolved.iter_errors(parts):
yield self._clean_errors(err)
|