From a7d33ba8856b6d123e4b4344f98d665d65bed5fa Mon Sep 17 00:00:00 2001 From: Raiden van Bronkhorst Date: Tue, 14 Jul 2026 21:36:24 -0700 Subject: [PATCH] feat(cloudformation): Parse secrets in ContainerDefinitions (#1874) * feat(cloudformation): Parse secrets in ContainerDefinitions * test(cloudformation): cover TaskDefinition creation with secrets --- .../CloudFormationResourceProvisioner.java | 13 ++++ .../CloudFormationIntegrationTest.java | 75 +++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java index 4e2e97d7c0..9a5d3f02b3 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java @@ -46,6 +46,7 @@ import io.github.hectorvent.floci.services.ecs.model.LaunchType; import io.github.hectorvent.floci.services.ecs.model.NetworkConfiguration; import io.github.hectorvent.floci.services.ecs.model.NetworkMode; import io.github.hectorvent.floci.services.ecs.model.PortMapping; +import io.github.hectorvent.floci.services.ecs.model.Secret; import io.github.hectorvent.floci.services.ecs.model.TaskDefinition; import io.github.hectorvent.floci.services.elbv2.ElbV2Service; import io.github.hectorvent.floci.services.elbv2.model.Action; @@ -3354,6 +3355,7 @@ public class CloudFormationResourceProvisioner { } def.setPortMappings(parseCfnPortMappings(item.path("PortMappings"))); def.setEnvironment(parseCfnEnvironment(item.path("Environment"))); + def.setSecrets(parseCfnSecrets(item.path("Secrets"))); if (item.path("Command").isArray()) { List cmd = new ArrayList<>(); item.path("Command").forEach(c -> cmd.add(c.asText())); @@ -3394,6 +3396,17 @@ public class CloudFormationResourceProvisioner { return result; } + private List parseCfnSecrets(JsonNode node) { + List result = new ArrayList<>(); + if (node == null || !node.isArray()) { + return result; + } + for (JsonNode item : node) { + result.add(new Secret(item.path("Name").asText(), item.path("ValueFrom").asText())); + } + return result; + } + private List parseEcsLoadBalancers(JsonNode node, CloudFormationTemplateEngine engine) { List result = new ArrayList<>(); if (node == null || node.isNull()) { diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java index f220291f9a..37d4a92702 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java @@ -5641,6 +5641,81 @@ class CloudFormationIntegrationTest { .body("services[0].networkConfiguration.awsvpcConfiguration.assignPublicIp", equalTo("ENABLED")); } + @Test + void createStack_ecsTaskDefWithSecrets_resolvesRefToSecretArnInValueFrom() { + String template = """ + { + "Resources": { + "DbPassword": { + "Type": "AWS::SecretsManager::Secret", + "Properties": { + "Name": "cfn-ecs-secret", + "SecretString": "password" + } + }, + "TaskDef": { + "Type": "AWS::ECS::TaskDefinition", + "Properties": { + "Family": "cfn-ecs-secrets-taskdef", + "ContainerDefinitions": [ + { + "Name": "web", + "Image": "nginx:latest", + "Essential": true, + "Secrets": [ + { "Name": "DB_PASSWORD", "ValueFrom": { "Ref": "DbPassword" } } + ] + } + ] + } + } + }, + "Outputs": { + "SecretArn": { "Value": { "Ref": "DbPassword" } } + } + } + """; + + String stackName = "cfn-ecs-secrets-stack"; + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "CreateStack") + .formParam("StackName", stackName) + .formParam("TemplateBody", template) + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString("")); + + String describeXml = given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "DescribeStacks") + .formParam("StackName", stackName) + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString("CREATE_COMPLETE")) + .extract().asString(); + + // Ref on AWS::SecretsManager::Secret resolves to the secret's ARN (confirmed independently + // via the stack Output above). The same ARN, not the literal {"Ref": "DbPassword"} JSON, + // must end up in the registered task definition's Secrets[].ValueFrom. + String secretArn = outputValue(describeXml, "SecretArn"); + given() + .header("X-Amz-Target", ECS_TARGET_PREFIX + "DescribeTaskDefinition") + .contentType(ECS_CONTENT_TYPE) + .body("{\"taskDefinition\": \"cfn-ecs-secrets-taskdef\"}") + .when() + .post("/") + .then() + .statusCode(200) + .body("taskDefinition.containerDefinitions[0].secrets[0].name", equalTo("DB_PASSWORD")) + .body("taskDefinition.containerDefinitions[0].secrets[0].valueFrom", equalTo(secretArn)); + } + @Test void updateStack_ecsService_registersNewTaskDefRevisionAndUpdatesDesiredCount() { String stackName = "cfn-ecs-update-stack";