Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

feat(cloudformation): Parse secrets in ContainerDefinitions (#1874)

Raiden van Bronkhorsta7d33ba8856b
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<String> cmd = new ArrayList<>();
item.path("Command").forEach(c -> cmd.add(c.asText()));
@@ -3394,6 +3396,17 @@ public class CloudFormationResourceProvisioner {
return result;
}
+ private List<Secret> parseCfnSecrets(JsonNode node) {
+ List<Secret> 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<EcsLoadBalancer> parseEcsLoadBalancers(JsonNode node, CloudFormationTemplateEngine engine) {
List<EcsLoadBalancer> 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("<StackId>"));
+
+ String describeXml = given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStacks")
+ .formParam("StackName", stackName)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>"))
+ .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";