|
| 1 | +""" |
| 2 | +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | +SPDX-License-Identifier: MIT-0 |
| 4 | +""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +from collections import deque |
| 9 | +from typing import Any, Iterator |
| 10 | + |
| 11 | +from cfnlint.helpers import ensure_list, is_function |
| 12 | +from cfnlint.jsonschema import ValidationError, ValidationResult |
| 13 | +from cfnlint.jsonschema.protocols import Validator |
| 14 | +from cfnlint.rules.helpers import get_resource_by_name, get_value_from_path |
| 15 | +from cfnlint.rules.jsonschema.CfnLintKeyword import CfnLintKeyword |
| 16 | + |
| 17 | + |
| 18 | +class ServiceFargate(CfnLintKeyword): |
| 19 | + id = "E3054" |
| 20 | + shortdesc = ( |
| 21 | + "Validate ECS service using Fargate uses TaskDefinition that allows Fargate" |
| 22 | + ) |
| 23 | + description = ( |
| 24 | + "When using an ECS service with 'LaunchType' of 'FARGATE' " |
| 25 | + "the associated task definition must have 'RequiresCompatibilities' " |
| 26 | + "specified with 'FARGATE' listed" |
| 27 | + ) |
| 28 | + tags = ["resources", "ecs"] |
| 29 | + |
| 30 | + def __init__(self) -> None: |
| 31 | + super().__init__( |
| 32 | + keywords=["Resources/AWS::ECS::Service/Properties"], |
| 33 | + ) |
| 34 | + |
| 35 | + def _filter_resource_name(self, instance: Any) -> str | None: |
| 36 | + fn_k, fn_v = is_function(instance) |
| 37 | + if fn_k is None: |
| 38 | + return None |
| 39 | + if fn_k == "Ref": |
| 40 | + if isinstance(fn_v, str): |
| 41 | + return fn_v |
| 42 | + elif fn_k == "Fn::GetAtt": |
| 43 | + name = ensure_list(fn_v)[0].split(".")[0] |
| 44 | + if isinstance(name, str): |
| 45 | + return name |
| 46 | + return None |
| 47 | + |
| 48 | + def _get_service_properties( |
| 49 | + self, validator: Validator, instance: Any |
| 50 | + ) -> Iterator[tuple[str, str, Validator]]: |
| 51 | + for task_definition_id, task_definition_validator in get_value_from_path( |
| 52 | + validator, instance, deque(["TaskDefinition"]) |
| 53 | + ): |
| 54 | + task_definition_resource_name = self._filter_resource_name( |
| 55 | + task_definition_id |
| 56 | + ) |
| 57 | + if task_definition_resource_name is None: |
| 58 | + continue |
| 59 | + |
| 60 | + for ( |
| 61 | + launch_type, |
| 62 | + launch_type_validator, |
| 63 | + ) in get_value_from_path( |
| 64 | + task_definition_validator, instance, deque(["LaunchType"]) |
| 65 | + ): |
| 66 | + yield ( |
| 67 | + task_definition_resource_name, |
| 68 | + launch_type, |
| 69 | + launch_type_validator, |
| 70 | + ) |
| 71 | + |
| 72 | + def _get_task_definition_properties( |
| 73 | + self, validator: Validator, resource_name: Any |
| 74 | + ) -> Iterator[tuple[list[Any] | None, Validator]]: |
| 75 | + task_definition, task_definition_validator = get_resource_by_name( |
| 76 | + validator, resource_name, ["AWS::ECS::TaskDefinition"] |
| 77 | + ) |
| 78 | + if not task_definition: |
| 79 | + return |
| 80 | + |
| 81 | + for capabilities, capabilities_validator in get_value_from_path( |
| 82 | + task_definition_validator, |
| 83 | + task_definition, |
| 84 | + path=deque(["Properties", "RequiresCompatibilities"]), |
| 85 | + ): |
| 86 | + if capabilities is None: |
| 87 | + yield capabilities, capabilities_validator |
| 88 | + continue |
| 89 | + if not isinstance(capabilities, list): |
| 90 | + continue |
| 91 | + for capibility, _ in get_value_from_path( |
| 92 | + capabilities_validator, |
| 93 | + capabilities, |
| 94 | + path=deque(["*"]), |
| 95 | + ): |
| 96 | + if isinstance(capibility, dict) or capibility == "FARGATE": |
| 97 | + break |
| 98 | + else: |
| 99 | + yield capabilities, capabilities_validator |
| 100 | + |
| 101 | + def validate( |
| 102 | + self, validator: Validator, _: Any, instance: Any, schema: dict[str, Any] |
| 103 | + ) -> ValidationResult: |
| 104 | + |
| 105 | + for ( |
| 106 | + task_definition_resource_name, |
| 107 | + launch_type, |
| 108 | + service_validator, |
| 109 | + ) in self._get_service_properties( |
| 110 | + validator, |
| 111 | + instance, |
| 112 | + ): |
| 113 | + if launch_type != "FARGATE": |
| 114 | + continue |
| 115 | + for ( |
| 116 | + capabilities, |
| 117 | + capabilities_validator, |
| 118 | + ) in self._get_task_definition_properties( |
| 119 | + service_validator, |
| 120 | + task_definition_resource_name, |
| 121 | + ): |
| 122 | + if capabilities is None: |
| 123 | + yield ValidationError( |
| 124 | + "'RequiresCompatibilities' is a required property", |
| 125 | + validator="required", |
| 126 | + rule=self, |
| 127 | + path_override=deque( |
| 128 | + list(capabilities_validator.context.path.path)[:-1] |
| 129 | + ), |
| 130 | + ) |
| 131 | + continue |
| 132 | + |
| 133 | + yield ValidationError( |
| 134 | + f"{capabilities!r} does not contain items matching 'FARGATE'", |
| 135 | + validator="contains", |
| 136 | + rule=self, |
| 137 | + path_override=capabilities_validator.context.path.path, |
| 138 | + ) |
0 commit comments