-
Notifications
You must be signed in to change notification settings - Fork 598
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rule E3042 - at least one essential AWS::ECS::TaskDefinition.Containe…
…rDefinition (#1548)
- Loading branch information
Showing
6 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/cfnlint/rules/resources/ecs/TaskDefinitionEssentialContainer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
""" | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: MIT-0 | ||
""" | ||
from cfnlint.rules import CloudFormationLintRule | ||
from cfnlint.rules import RuleMatch | ||
|
||
|
||
class TaskDefinitionEssentialContainer(CloudFormationLintRule): | ||
"""Check ECS TaskDefinition ContainerDefinitions Property Specifies at least one Essential Container""" | ||
id = 'E3042' | ||
shortdesc = 'Check at least one essential container is specified' | ||
description = 'Check that every TaskDefinition specifies at least one essential container' | ||
source_url = 'https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ecs-taskdefinition-containerdefinitions.html#cfn-ecs-taskdefinition-containerdefinition-essential' | ||
tags = ['properties', 'ecs', 'task', 'container', 'fargate'] | ||
|
||
def match(self, cfn): | ||
"""Check at least one essential container is specified""" | ||
|
||
matches = [] | ||
|
||
results = cfn.get_resource_properties(['AWS::ECS::TaskDefinition', 'ContainerDefinitions']) | ||
|
||
for result in results: | ||
path = result['Path'] | ||
|
||
has_essential_container = False | ||
|
||
for container in result['Value']: | ||
if 'Essential' in container: | ||
if container['Essential']: | ||
has_essential_container = True | ||
else: | ||
pass | ||
else: | ||
# If 'Essential' is not specified, it defaults to an essential container | ||
has_essential_container = True | ||
|
||
if not has_essential_container: | ||
message = 'No essential containers defined for {0}' | ||
rule_match = RuleMatch(path, message.format('/'.join(map(str, path)))) | ||
matches.append(rule_match) | ||
|
||
return matches |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
""" | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: MIT-0 | ||
""" |
53 changes: 53 additions & 0 deletions
53
test/fixtures/templates/bad/resources/ecs/test_ecs_task_definition_essential_container.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--- | ||
Resources: | ||
|
||
BadTaskDefinitionNoEssentialContainersSpecified: | ||
Type: AWS::ECS::TaskDefinition | ||
Properties: | ||
ContainerDefinitions: | ||
- Essential: false | ||
Image: amazon/aws-cli | ||
Memory: 40 | ||
Name: amazon-cli | ||
|
||
GoodTaskDefinitionEssentialContainerSpecified: | ||
Type: AWS::ECS::TaskDefinition | ||
Properties: | ||
ContainerDefinitions: | ||
- Essential: true | ||
Image: amazon/aws-cli | ||
Memory: 40 | ||
Name: amazon-cli | ||
|
||
GoodTaskDefinitionEssentialContainerDefault: | ||
Type: AWS::ECS::TaskDefinition | ||
Properties: | ||
ContainerDefinitions: | ||
- Image: amazon/aws-cli | ||
Memory: 40 | ||
Name: amazon-cli | ||
|
||
GoodTaskDefinitionEssentialContainerSpecifiedTwo: | ||
Type: AWS::ECS::TaskDefinition | ||
Properties: | ||
ContainerDefinitions: | ||
- Essential: true | ||
Image: amazon/aws-cli | ||
Memory: 40 | ||
Name: amazon-cli | ||
- Essential: false | ||
Image: amazon/aws-cli | ||
Memory: 40 | ||
Name: amazon-cli-two | ||
|
||
GoodTaskDefinitionMultipleEssentialContainersSpecified: | ||
Type: AWS::ECS::TaskDefinition | ||
Properties: | ||
ContainerDefinitions: | ||
- Essential: true | ||
Image: amazon/aws-cli | ||
Memory: 40 | ||
Name: amazon-cli | ||
- Image: amazon/aws-cli | ||
Memory: 40 | ||
Name: amazon-cli-two |
44 changes: 44 additions & 0 deletions
44
test/fixtures/templates/good/resources/ecs/test_ecs_task_definition_essential_container.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
Resources: | ||
|
||
GoodTaskDefinitionEssentialContainerSpecified: | ||
Type: AWS::ECS::TaskDefinition | ||
Properties: | ||
ContainerDefinitions: | ||
- Essential: true | ||
Image: amazon/aws-cli | ||
Memory: 40 | ||
Name: amazon-cli | ||
|
||
GoodTaskDefinitionEssentialContainerDefault: | ||
Type: AWS::ECS::TaskDefinition | ||
Properties: | ||
ContainerDefinitions: | ||
- Image: amazon/aws-cli | ||
Memory: 40 | ||
Name: amazon-cli | ||
|
||
GoodTaskDefinitionEssentialContainerSpecifiedTwo: | ||
Type: AWS::ECS::TaskDefinition | ||
Properties: | ||
ContainerDefinitions: | ||
- Essential: true | ||
Image: amazon/aws-cli | ||
Memory: 40 | ||
Name: amazon-cli | ||
- Essential: false | ||
Image: amazon/aws-cli | ||
Memory: 40 | ||
Name: amazon-cli-two | ||
|
||
GoodTaskDefinitionMultipleEssentialContainersSpecified: | ||
Type: AWS::ECS::TaskDefinition | ||
Properties: | ||
ContainerDefinitions: | ||
- Essential: true | ||
Image: amazon/aws-cli | ||
Memory: 40 | ||
Name: amazon-cli | ||
- Image: amazon/aws-cli | ||
Memory: 40 | ||
Name: amazon-cli-two |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
""" | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: MIT-0 | ||
""" |
26 changes: 26 additions & 0 deletions
26
test/unit/rules/resources/ecs/test_ecs_task_definition_essential_container.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
""" | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: MIT-0 | ||
""" | ||
from test.unit.rules import BaseRuleTestCase | ||
from cfnlint.rules.resources.ecs.TaskDefinitionEssentialContainer import TaskDefinitionEssentialContainer # pylint: disable=E0401 | ||
|
||
|
||
class TestECSTaskDefinitionEssentialContainer(BaseRuleTestCase): | ||
"""Test ECS Task Definition has at least one essential container defined""" | ||
|
||
def setUp(self): | ||
"""Setup""" | ||
super(TestECSTaskDefinitionEssentialContainer, self).setUp() | ||
self.collection.register(TaskDefinitionEssentialContainer()) | ||
self.success_templates = [ | ||
'test/fixtures/templates/good/resources/ecs/test_ecs_task_definition_essential_container.yml', | ||
] | ||
|
||
def test_file_positive(self): | ||
"""Test Positive""" | ||
self.helper_file_positive() | ||
|
||
def test_file_negative(self): | ||
"""Test failure""" | ||
self.helper_file_negative('test/fixtures/templates/bad/resources/ecs/test_ecs_task_definition_essential_container.yml', 1) |