Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/aws_ecs_task_definition - prevent panic on null container definition #27263

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/27263.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_ecs_task_definition: Prevent panic when supplying a `null` value in `container_definitions`
```
6 changes: 6 additions & 0 deletions internal/service/ecs/task_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,12 @@ func expandContainerDefinitions(rawDefinitions string) ([]*ecs.ContainerDefiniti
return nil, fmt.Errorf("Error decoding JSON: %s", err)
}

for i, c := range definitions {
if c == nil {
return nil, fmt.Errorf("invalid container definition supplied at index (%d)", i)
}
}

return definitions, nil
}

Expand Down
46 changes: 46 additions & 0 deletions internal/service/ecs/task_definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,23 @@ func TestAccECSTaskDefinition_inferenceAccelerator(t *testing.T) {
})
}

func TestAccECSTaskDefinition_invalidContainerDefinition(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, ecs.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckTaskDefinitionDestroy,
Steps: []resource.TestStep{
{
Config: testAccTaskDefinitionConfig_invalidContainerDefinition(rName),
ExpectError: regexp.MustCompile(`invalid container definition supplied at index \(1\)`),
},
},
})
}

func testAccTaskDefinitionConfig_proxyConfiguration(rName string, containerName string, proxyType string,
ignoredUid string, ignoredGid string, appPorts string, proxyIngressPort string, proxyEgressPort string,
egressIgnoredPorts string, egressIgnoredIPs string) string {
Expand Down Expand Up @@ -2616,3 +2633,32 @@ resource "aws_fsx_windows_file_system" "test" {
}
`)
}

func testAccTaskDefinitionConfig_invalidContainerDefinition(rName string) string {
return fmt.Sprintf(`
resource "aws_ecs_task_definition" "test" {
family = %[1]q

container_definitions = <<TASK_DEFINITION
[
{
"cpu": 10,
"command": ["sleep", "10"],
"entryPoint": ["/"],
"essential": true,
"image": "mongodb",
"memory": 128,
"name": "mongodb",
"portMappings": [
{
"containerPort": 28017,
"hostPort": 28017
}
]
},
null
]
TASK_DEFINITION
}
`, rName)
}