Light, fluffy, and always free - The AWS Local Emulator alternative
fix(cloudformation): map FIFO and redrive attributes for AWS::SQS::Queue (#1939)
Feyikemi Ogunsanya58112b1f3bd1
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/SqsCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/SqsCfnProvisioner.javaindex bdc004059a..1967457c12 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/SqsCfnProvisioner.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/SqsCfnProvisioner.java@@ -50,18 +50,44 @@ public class SqsCfnProvisioner implements CfnResourceProvisioner {}private void provisionQueue(StackResource r, JsonNode props, ProvisionContext ctx) {+ String fifoFlag = props != null && props.has("FifoQueue")+ ? ctx.engine().resolve(props.get("FifoQueue"))+ : null;+ boolean fifo = "true".equalsIgnoreCase(fifoFlag);String queueName = ctx.resolveOptional(props, "QueueName");if (queueName == null || queueName.isBlank()) {- queueName = ctx.generatePhysicalName(r.getLogicalId(), 80, false);+ // Like real CloudFormation, generated names of FIFO queues must end in .fifo+ // (SqsService rejects FifoQueue=true otherwise). Keep within the 80-char limit.+ queueName = fifo+ ? ctx.generatePhysicalName(r.getLogicalId(), 75, false) + ".fifo"+ : ctx.generatePhysicalName(r.getLogicalId(), 80, false);}Map<String, String> attrs = new HashMap<>();if (props != null) {+ if (fifoFlag != null) {+ attrs.put("FifoQueue", fifoFlag);+ }if (props.has("VisibilityTimeout")) {attrs.put("VisibilityTimeout", ctx.engine().resolve(props.get("VisibilityTimeout")));}if (props.has("ContentBasedDeduplication")) {attrs.put("ContentBasedDeduplication", ctx.engine().resolve(props.get("ContentBasedDeduplication")));}+ if (props.has("DeduplicationScope")) {+ attrs.put("DeduplicationScope", ctx.engine().resolve(props.get("DeduplicationScope")));+ }+ if (props.has("FifoThroughputLimit")) {+ attrs.put("FifoThroughputLimit", ctx.engine().resolve(props.get("FifoThroughputLimit")));+ }+ if (props.has("RedrivePolicy") && !props.path("RedrivePolicy").isNull()) {+ // Usually a JSON object in the template (deadLetterTargetArn is an Fn::GetAtt);+ // resolveNode resolves intrinsics in place and SqsService expects the JSON string.+ // CDK commonly emits RedrivePolicy as an already-serialized string via Fn::Join,+ // which resolveNode collapses to a TextNode — unwrap it instead of calling+ // toString(), which would JSON-re-encode (quote/escape) the string a second time.+ JsonNode redrivePolicy = ctx.engine().resolveNode(props.path("RedrivePolicy"));+ attrs.put("RedrivePolicy", redrivePolicy.isTextual() ? redrivePolicy.asText() : redrivePolicy.toString());+ }}Queue queue = sqsService.createQueue(queueName, attrs, ctx.region());// QueueArn is computed on demand in SqsService#getQueueAttributes and is not stored on thediff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.javaindex a10b0a7ba0..d62fa4a299 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@@ -33,6 +33,7 @@ import static org.hamcrest.Matchers.not;import static org.hamcrest.Matchers.notNullValue;import static org.hamcrest.Matchers.nullValue;import static org.hamcrest.Matchers.startsWith;+import static org.junit.jupiter.api.Assertions.assertTrue;@QuarkusTestclass CloudFormationIntegrationTest {@@ -9311,4 +9312,105 @@ class CloudFormationIntegrationTest {.body("Policy", containsString("222222222222"));}+ @Test+ void createStack_fifoQueueKeepsDeduplicationScopeThroughputLimitAndRedrivePolicy() {+ // #1907: the stack reached CREATE_COMPLETE but DeduplicationScope, FifoThroughputLimit+ // and RedrivePolicy were silently dropped on the CloudFormation-to-SQS path.+ String stackName = "cfn-1907-fifo-attrs-stack";+ String template = """+ {+ "Resources": {+ "Dlq": {+ "Type": "AWS::SQS::Queue",+ "Properties": {+ "FifoQueue": true,+ "ContentBasedDeduplication": true+ }+ },+ "MainQueue": {+ "Type": "AWS::SQS::Queue",+ "Properties": {+ "QueueName": "cfn-1907-main.fifo",+ "FifoQueue": true,+ "ContentBasedDeduplication": false,+ "DeduplicationScope": "messageGroup",+ "FifoThroughputLimit": "perMessageGroupId",+ "VisibilityTimeout": 30,+ "RedrivePolicy": {+ "deadLetterTargetArn": {"Fn::GetAtt": ["Dlq", "Arn"]},+ "maxReceiveCount": 5+ }+ }+ }+ }+ }+ """;++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "CreateStack")+ .formParam("StackName", stackName)+ .formParam("TemplateBody", template)+ .when()+ .post("/")+ .then()+ .statusCode(200);++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "DescribeStacks")+ .formParam("StackName", stackName)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>"));++ // The DLQ had no QueueName: like real CloudFormation, the generated physical name of a+ // FIFO queue must end in .fifo (SqsService rejects FifoQueue=true otherwise).+ String resourcesXml = given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "DescribeStackResources")+ .formParam("StackName", stackName)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .extract().asString();+ String dlqUrl = physicalIdByLogicalId(resourcesXml, "Dlq");+ String dlqName = dlqUrl.substring(dlqUrl.lastIndexOf('/') + 1);+ assertTrue(dlqName.endsWith(".fifo"),+ "generated DLQ physical name should end with .fifo but was: " + dlqName);++ // Every FIFO attribute and the redrive policy (with the resolved DLQ ARN) survives.+ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "GetQueueAttributes")+ .formParam("QueueUrl", "http://localhost:4566/000000000000/cfn-1907-main.fifo")+ .formParam("AttributeName.1", "All")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body(containsString("DeduplicationScope"))+ .body(containsString("messageGroup"))+ .body(containsString("FifoThroughputLimit"))+ .body(containsString("perMessageGroupId"))+ .body(containsString("RedrivePolicy"))+ .body(containsString("maxReceiveCount"))+ .body(containsString("arn:aws:sqs:us-east-1:000000000000:" + dlqName));++ // The generated-name DLQ is itself a FIFO queue.+ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "GetQueueAttributes")+ .formParam("QueueUrl", dlqUrl)+ .formParam("AttributeName.1", "All")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body(containsString("FifoQueue"));+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/SqsCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/SqsCfnProvisionerTest.javaindex 611f1a6fae..bc64f91943 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/SqsCfnProvisionerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/SqsCfnProvisionerTest.java@@ -8,8 +8,10 @@ import io.github.hectorvent.floci.services.cloudformation.model.StackResource;import io.github.hectorvent.floci.services.sqs.SqsService;import io.github.hectorvent.floci.services.sqs.model.Queue;import org.junit.jupiter.api.Test;+import org.mockito.ArgumentCaptor;import java.util.HashMap;+import java.util.Map;import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertNotNull;@@ -40,6 +42,23 @@ class SqsCfnProvisionerTest {JsonNode node = inv.getArgument(0);return node == null ? null : node.asText();});+ // Mirrors the engine's resolveNode contract for the flat objects these tests use:+ // Fn::GetAtt collapses to a text ARN, everything else passes through untouched.+ when(engine.resolveNode(any())).thenAnswer(inv -> {+ JsonNode node = inv.getArgument(0);+ if (node == null || !node.isObject()) {+ return node;+ }+ ObjectNode resolved = mapper.createObjectNode();+ node.fields().forEachRemaining(e -> {+ if (e.getValue().isObject() && e.getValue().has("Fn::GetAtt")) {+ resolved.put(e.getKey(), "arn:aws:sqs:us-east-1:000000000000:my-stack-Dlq.fifo");+ } else {+ resolved.set(e.getKey(), e.getValue());+ }+ });+ return resolved;+ });return new ProvisionContext(engine, "us-east-1", "000000000000", "my-stack");}@@ -78,6 +97,101 @@ class SqsCfnProvisionerTest {assertEquals("my-stack-MyQueue", r.getAttributes().get("QueueName").replaceAll("-[0-9a-f]{12}$", ""));}+ @Test+ void queuePassesFifoScalarAttributesToCreateQueue() {+ // Reproduces #1907: DeduplicationScope and FifoThroughputLimit from the template must+ // reach SqsService.createQueue — the engine already persists them (issue Control 2).+ when(sqs.createQueue(eq("orders.fifo"), any(), eq("us-east-1")))+ .thenReturn(new Queue("orders.fifo", "http://localhost:4566/000000000000/orders.fifo"));+ StackResource r = resource("AWS::SQS::Queue", "MyQueue");+ ObjectNode props = mapper.createObjectNode()+ .put("QueueName", "orders.fifo")+ .put("FifoQueue", true)+ .put("ContentBasedDeduplication", false)+ .put("DeduplicationScope", "messageGroup")+ .put("FifoThroughputLimit", "perMessageGroupId")+ .put("VisibilityTimeout", 30);++ provisioner.provision(r, props, ctx());++ Map<String, String> attrs = capturedCreateQueueAttributes("orders.fifo");+ assertEquals("messageGroup", attrs.get("DeduplicationScope"));+ assertEquals("perMessageGroupId", attrs.get("FifoThroughputLimit"));+ // The two attributes that already worked keep working.+ assertEquals("false", attrs.get("ContentBasedDeduplication"));+ assertEquals("30", attrs.get("VisibilityTimeout"));+ }++ @Test+ void queueSerializesRedrivePolicyWithResolvedDlqArn() {+ // Reproduces #1907: RedrivePolicy is a JSON object in the template (with an intrinsic+ // for the DLQ ARN) and must reach createQueue as the JSON string SqsService parses.+ when(sqs.createQueue(eq("orders.fifo"), any(), eq("us-east-1")))+ .thenReturn(new Queue("orders.fifo", "http://localhost:4566/000000000000/orders.fifo"));+ StackResource r = resource("AWS::SQS::Queue", "MyQueue");+ ObjectNode getAtt = mapper.createObjectNode();+ getAtt.set("Fn::GetAtt", mapper.createArrayNode().add("Dlq").add("Arn"));+ ObjectNode redrive = mapper.createObjectNode();+ redrive.set("deadLetterTargetArn", getAtt);+ redrive.put("maxReceiveCount", 5);+ ObjectNode props = mapper.createObjectNode().put("QueueName", "orders.fifo");+ props.set("RedrivePolicy", redrive);++ provisioner.provision(r, props, ctx());++ Map<String, String> attrs = capturedCreateQueueAttributes("orders.fifo");+ assertEquals(+ "{\"deadLetterTargetArn\":\"arn:aws:sqs:us-east-1:000000000000:my-stack-Dlq.fifo\",\"maxReceiveCount\":5}",+ attrs.get("RedrivePolicy"));+ }++ @Test+ void queuePassesThroughAlreadySerializedRedrivePolicyString() {+ // Reported on #1939: CDK commonly emits RedrivePolicy via Fn::Join, which resolveNode+ // collapses to an already-JSON-encoded TextNode. TextNode#toString() re-encodes it (quotes+ // and escapes the string), double-serializing the policy so SqsService can't parse it.+ when(sqs.createQueue(eq("orders.fifo"), any(), eq("us-east-1")))+ .thenReturn(new Queue("orders.fifo", "http://localhost:4566/000000000000/orders.fifo"));+ StackResource r = resource("AWS::SQS::Queue", "MyQueue");+ String serializedRedrive =+ "{\"maxReceiveCount\":5,\"deadLetterTargetArn\":\"arn:aws:sqs:us-east-1:000000000000:my-stack-Dlq.fifo\"}";+ ObjectNode props = mapper.createObjectNode().put("QueueName", "orders.fifo");+ props.put("RedrivePolicy", serializedRedrive);++ provisioner.provision(r, props, ctx());++ Map<String, String> attrs = capturedCreateQueueAttributes("orders.fifo");+ assertEquals(serializedRedrive, attrs.get("RedrivePolicy"));+ }++ @Test+ void fifoQueueWithoutNameGetsGeneratedFifoNameAndFifoAttribute() {+ // Like real CloudFormation, a FifoQueue: true resource without a QueueName must get a+ // generated physical name ending in .fifo — SqsService rejects FifoQueue=true otherwise.+ when(sqs.createQueue(anyString(), any(), eq("us-east-1")))+ .thenAnswer(inv -> new Queue(inv.getArgument(0), "http://q/" + inv.getArgument(0)));+ StackResource r = resource("AWS::SQS::Queue", "MyDlq");+ ObjectNode props = mapper.createObjectNode().put("FifoQueue", true);++ provisioner.provision(r, props, ctx());++ String queueName = r.getAttributes().get("QueueName");+ assertTrue(queueName.endsWith(".fifo"),+ "generated FIFO queue name should end with .fifo but was: " + queueName);+ assertTrue(queueName.length() <= 80, "queue name must stay within the 80-char limit");+ @SuppressWarnings("unchecked")+ ArgumentCaptor<Map<String, String>> captor = ArgumentCaptor.forClass(Map.class);+ verify(sqs).createQueue(eq(queueName), captor.capture(), eq("us-east-1"));+ assertEquals("true", captor.getValue().get("FifoQueue"));+ }++ private Map<String, String> capturedCreateQueueAttributes(String queueName) {+ @SuppressWarnings("unchecked")+ ArgumentCaptor<Map<String, String>> captor = ArgumentCaptor.forClass(Map.class);+ verify(sqs).createQueue(eq(queueName), captor.capture(), eq("us-east-1"));+ return captor.getValue();+ }+@Testvoid queuePolicyGetsAPhysicalId() {StackResource r = resource("AWS::SQS::QueuePolicy", "MyPolicy");