diff --git a/docs/services/sqs.md b/docs/services/sqs.md index 1a3ee70cf8..32086e1818 100644 --- a/docs/services/sqs.md +++ b/docs/services/sqs.md @@ -98,7 +98,7 @@ aws sqs set-queue-attributes \ `MaximumMessageSize` defaults to `1048576` bytes, the AWS default and maximum since August 2025, when AWS raised both from `262144`. `CreateQueue` and `SetQueueAttributes` reject values outside 1024 to 1048576 with `InvalidAttributeValue`; raising `FLOCI_SERVICES_SQS_MAX_MESSAGE_SIZE` above the AWS maximum raises that accepted ceiling with it. Lowering the variable below 1048576 only changes the default a new queue receives: the accepted ceiling stays at the AWS maximum, so a queue can still be set to `1048576` explicitly. -`SqsManagedSseEnabled` is `true` for a queue with no `KmsMasterKeyId`, matching AWS, and `false` once a KMS key is set. It is derived on read rather than stored, so clearing `KmsMasterKeyId` returns the queue to `true`, unless you set `SqsManagedSseEnabled` yourself, in which case your value stands. AWS does not document the cleared-key case crisply, so that behaviour is Floci's choice rather than a copied one. +`SqsManagedSseEnabled` is `true` for a queue with no `KmsMasterKeyId`, matching AWS, and `false` once a KMS key is set. It is derived on read rather than stored, so clearing `KmsMasterKeyId` returns the queue to `true`, unless you set `SqsManagedSseEnabled` yourself, in which case your value stands. AWS does not document the cleared-key case crisply, so that behaviour is Floci's choice rather than a copied one. `CreateQueue` on an existing name compares requested attributes against these reported values, so asking for `SqsManagedSseEnabled=false` on a queue that reports `true` fails with `QueueAlreadyExists` (`QueueNameExists` over JSON). ## Message Retention diff --git a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java index bea83779b5..36f8368ce4 100644 --- a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java @@ -639,12 +639,24 @@ public class SqsService implements Resettable, ResourceProvider { Set readOnlyAttrs = Set.of("QueueArn", "CreatedTimestamp", "LastModifiedTimestamp", "ApproximateNumberOfMessages", "ApproximateNumberOfMessagesNotVisible", "ApproximateNumberOfMessagesDelayed"); + Map effective = new LinkedHashMap<>(existing.getAttributes()); + applyDerivedAttributes(effective); for (Map.Entry entry : attributes.entrySet()) { if (readOnlyAttrs.contains(entry.getKey())) { continue; } - String storedValue = existing.getAttributes().get(entry.getKey()); - if (storedValue != null && !storedValue.equals(entry.getValue())) { + // Compare against what GetQueueAttributes reports, not the raw stored map: + // SqsManagedSseEnabled is derived on read and never stored, so the stored map + // alone would let a differing value through. + String storedValue = effective.get(entry.getKey()); + String requested = entry.getValue(); + // The effective MaximumMessageSize is canonical ("1024"); canonicalise the + // already-validated request too so "01024" is not a conflict with itself, + // and an empty value maps to the same default the effective map reports. + if ("MaximumMessageSize".equals(entry.getKey())) { + requested = String.valueOf(parseMaxMessageSize(requested)); + } + if (storedValue != null && !storedValue.equals(requested)) { throw new AwsException("QueueAlreadyExists", "A queue already exists with the same name but different attributes.", 400); } @@ -760,6 +772,25 @@ public class SqsService implements Resettable, ResourceProvider { attrs.put("ApproximateNumberOfMessagesNotVisible", String.valueOf(counts.inFlight())); attrs.put("ApproximateNumberOfMessagesDelayed", String.valueOf(counts.delayed())); + applyDerivedAttributes(attrs); + + if (attributeNames == null || attributeNames.contains("All")) { + return attrs; + } + Map filtered = new LinkedHashMap<>(); + for (String name : attributeNames) { + if (attrs.containsKey(name)) { + filtered.put(name, attrs.get(name)); + } + } + return filtered; + } + + /** + * Fills in the attributes reported on read but not necessarily stored. Shared by + * GetQueueAttributes and the CreateQueue conflict check so the two cannot drift. + */ + private void applyDerivedAttributes(Map attrs) { // Derived at read time and never stored, so it cannot go stale behind a // KmsMasterKeyId that was later cleared. A KMS key always wins; otherwise an // explicit SqsManagedSseEnabled stands, and a queue with neither reports the AWS @@ -779,17 +810,6 @@ public class SqsService implements Resettable, ResourceProvider { attrs.putIfAbsent("DelaySeconds", "0"); attrs.putIfAbsent("ReceiveMessageWaitTimeSeconds", "0"); attrs.putIfAbsent("MessageRetentionPeriod", "345600"); - - if (attributeNames == null || attributeNames.contains("All")) { - return attrs; - } - Map filtered = new LinkedHashMap<>(); - for (String name : attributeNames) { - if (attrs.containsKey(name)) { - filtered.put(name, attrs.get(name)); - } - } - return filtered; } /** diff --git a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsIntegrationTest.java index 05be7eaa59..dc250db4e2 100644 --- a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsIntegrationTest.java @@ -590,6 +590,76 @@ class SqsIntegrationTest { .post("/"); } + @Test + void createQueue_conflictOnDerivedSqsManagedSseEnabled_returns400() { + String queueName = "conflict-sse-queue"; + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "CreateQueue") + .formParam("QueueName", queueName) + .when() + .post("/") + .then() + .statusCode(200); + + // The queue reports SqsManagedSseEnabled=true without storing it. + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "CreateQueue") + .formParam("QueueName", queueName) + .formParam("Attribute.1.Name", "SqsManagedSseEnabled") + .formParam("Attribute.1.Value", "false") + .when() + .post("/") + .then() + .statusCode(400) + .body("ErrorResponse.Error.Code", equalTo("QueueAlreadyExists")); + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "CreateQueue") + .formParam("QueueName", queueName) + .formParam("Attribute.1.Name", "SqsManagedSseEnabled") + .formParam("Attribute.1.Value", "true") + .when() + .post("/") + .then() + .statusCode(200); + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "DeleteQueue") + .formParam("QueueUrl", "http://localhost:4566/000000000000/" + queueName) + .when() + .post("/"); + } + + @Test + void createQueue_recreateWithSameEmptyMaximumMessageSize_returnsExistingQueue() { + String queueName = "empty-max-size-queue"; + for (int i = 0; i < 2; i++) { + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "CreateQueue") + .formParam("QueueName", queueName) + .formParam("Attribute.1.Name", "MaximumMessageSize") + .formParam("Attribute.1.Value", "") + .when() + .post("/") + .then() + .statusCode(200) + .body("CreateQueueResponse.CreateQueueResult.QueueUrl", endsWith("/" + queueName)); + } + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "DeleteQueue") + .formParam("QueueUrl", "http://localhost:4566/000000000000/" + queueName) + .when() + .post("/"); + } + @Test void jsonProtocol_nonExistentQueue_returnsQueueDoesNotExist() { given() diff --git a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsServiceTest.java index 6e225175fc..8fe9043d68 100644 --- a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsServiceTest.java @@ -162,6 +162,51 @@ class SqsServiceTest { assertEquals(Map.of("SqsManagedSseEnabled", "true"), attrs); } + @Test + void createQueue_conflictsOnADerivedAttributeThatIsNeverStored() { + String region = "eu-west-1"; + Queue queue = sqsService.createQueue("derived-conflict-queue", null, region); + + // SqsManagedSseEnabled reads back as "true" but is not in the stored map, so a + // re-create asking for "false" must still see the difference. + AwsException ex = assertThrows(AwsException.class, + () -> sqsService.createQueue("derived-conflict-queue", + Map.of("SqsManagedSseEnabled", "false"), region)); + assertEquals("QueueAlreadyExists", ex.getErrorCode()); + + Queue same = sqsService.createQueue("derived-conflict-queue", + Map.of("SqsManagedSseEnabled", "true", "VisibilityTimeout", "30"), region); + assertEquals(queue.getQueueUrl(), same.getQueueUrl()); + assertEquals("true", sqsService.getQueueAttributes(queue.getQueueUrl(), + List.of("SqsManagedSseEnabled"), region).get("SqsManagedSseEnabled")); + } + + @Test + void createQueue_recreateWithSameNonCanonicalMaximumMessageSizeIsNotAConflict() { + String region = "eu-west-1"; + Queue queue = sqsService.createQueue("padded-size-queue", Map.of("MaximumMessageSize", "01024"), region); + + Queue same = sqsService.createQueue("padded-size-queue", Map.of("MaximumMessageSize", "01024"), region); + assertEquals(queue.getQueueUrl(), same.getQueueUrl()); + + AwsException ex = assertThrows(AwsException.class, + () -> sqsService.createQueue("padded-size-queue", Map.of("MaximumMessageSize", "2048"), region)); + assertEquals("QueueAlreadyExists", ex.getErrorCode()); + } + + @Test + void createQueue_recreateWithSameEmptyMaximumMessageSizeIsNotAConflict() { + String region = "eu-west-1"; + Queue queue = sqsService.createQueue("empty-size-queue", Map.of("MaximumMessageSize", ""), region); + + Queue same = sqsService.createQueue("empty-size-queue", Map.of("MaximumMessageSize", ""), region); + assertEquals(queue.getQueueUrl(), same.getQueueUrl()); + + AwsException ex = assertThrows(AwsException.class, + () -> sqsService.createQueue("empty-size-queue", Map.of("MaximumMessageSize", "2048"), region)); + assertEquals("QueueAlreadyExists", ex.getErrorCode()); + } + @Test void createQueue_rejectsMaximumMessageSizeOutsideAwsRange() { for (String invalid : List.of("1048577", "1023", "0", "-1", "abc")) {