forked from aws-cloudformation/cfn-lint
-
Notifications
You must be signed in to change notification settings - Fork 2
/
TaskDefinitionAwsVpc.py
81 lines (71 loc) · 2.8 KB
/
TaskDefinitionAwsVpc.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
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""
from __future__ import annotations
from collections import deque
from typing import Any, Iterator
from cfnlint.jsonschema import ValidationError, ValidationResult
from cfnlint.jsonschema.protocols import Validator
from cfnlint.rules.helpers import get_value_from_path
from cfnlint.rules.jsonschema.CfnLintKeyword import CfnLintKeyword
class TaskDefinitionAwsVpc(CfnLintKeyword):
id = "E3053"
shortdesc = "Validate ECS task definition is has correct values for 'HostPort'"
description = (
"The 'HostPort' must either be undefined or equal to "
"the 'ContainerPort' value"
)
tags = ["resources", "ecs"]
def __init__(self) -> None:
super().__init__(
keywords=["Resources/AWS::ECS::TaskDefinition/Properties"],
)
def _get_port_mappings(
self, validator: Validator, instance: Any
) -> Iterator[tuple[str | int | None, str | int | None, Validator]]:
for container_definition, container_definition_validator in get_value_from_path(
validator,
instance,
path=deque(["ContainerDefinitions", "*", "PortMappings", "*"]),
):
for host_port, host_port_validator in get_value_from_path(
container_definition_validator,
container_definition,
path=deque(["HostPort"]),
):
if not isinstance(host_port, (str, int)):
continue
for container_port, _ in get_value_from_path(
host_port_validator,
container_definition,
path=deque(["ContainerPort"]),
):
if not isinstance(container_port, (str, int)):
continue
if str(host_port) != str(container_port):
yield host_port, container_port, host_port_validator
def validate(
self, validator: Validator, _: Any, instance: Any, schema: dict[str, Any]
) -> ValidationResult:
for network_mode, _ in get_value_from_path(
validator,
instance,
path=deque(["NetworkMode"]),
):
if network_mode != "awsvpc":
continue
for (
host_port,
container_port,
port_mapping_validator,
) in self._get_port_mappings(
validator,
instance,
):
yield ValidationError(
f"{host_port!r} does not equal {container_port!r}",
validator="const",
rule=self,
path_override=port_mapping_validator.context.path.path,
)