fix(sqs): store RedrivePolicy and RedriveAllowPolicy as compact JSON (#6531)

This commit is contained in:
Tal Efraim authored and GitHub committed 2026-10-25 14:13:02 -04:00
1 parent 4d35617518
commit a4196c19e3
3 files changed
+150 -1

No files matched your search

@@ -1,6 +1,8 @@
package io.github.hectorvent.floci.services.sqs;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
@@ -740,6 +742,7 @@ public class SqsService implements Resettable, ResourceProvider {
validateMaximumMessageSize(attributes);
attributes = validateMessageRetentionPeriod(attributes);
attributes = compactRedrivePolicies(attributes);
String accountId = regionResolver.getAccountId();
String queueUrl = buildQueueUrl(region, accountId, queueName);
@@ -752,7 +755,8 @@ public class SqsService implements Resettable, ResourceProvider {
Set<String> readOnlyAttrs = Set.of("QueueArn", "CreatedTimestamp", "LastModifiedTimestamp",
"ApproximateNumberOfMessages", "ApproximateNumberOfMessagesNotVisible",
"ApproximateNumberOfMessagesDelayed");
Map<String, String> effective = new LinkedHashMap<>(existing.getAttributes());
// A queue saved by an earlier build can hold a non-compact policy; compare compact forms.
Map<String, String> effective = new LinkedHashMap<>(compactRedrivePolicies(existing.getAttributes()));
applyDerivedAttributes(effective);
for (Map.Entry<String, String> entry : attributes.entrySet()) {
if (readOnlyAttrs.contains(entry.getKey())) {
@@ -1242,6 +1246,44 @@ public class SqsService implements Resettable, ResourceProvider {
return normalized;
}
private static final ObjectMapper REDRIVE_POLICY_MAPPER =
new ObjectMapper().enable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
/**
* AWS stores RedrivePolicy and RedriveAllowPolicy as compact JSON, so a document sent with
* whitespace (a trailing newline, pretty-printing) reads back without it. A value that is not
* a JSON object is left as supplied.
*/
private static Map<String, String> compactRedrivePolicies(Map<String, String> attributes) {
if (attributes == null) {
return null;
}
Map<String, String> compacted = attributes;
for (String name : List.of("RedrivePolicy", "RedriveAllowPolicy")) {
String value = attributes.get(name);
if (value == null || value.isEmpty()) {
continue;
}
String compact;
try {
JsonNode document = REDRIVE_POLICY_MAPPER.readTree(value);
if (document == null || !document.isObject()) {
continue;
}
compact = REDRIVE_POLICY_MAPPER.writeValueAsString(document);
} catch (JsonProcessingException ignored) {
continue;
}
if (!compact.equals(value)) {
if (compacted == attributes) {
compacted = new HashMap<>(attributes);
}
compacted.put(name, compact);
}
}
return compacted;
}
private static AwsException invalidMessageRetentionPeriod() {
return new AwsException("InvalidAttributeValue",
"Invalid value for the parameter MessageRetentionPeriod.", 400);
@@ -1659,6 +1701,7 @@ public class SqsService implements Resettable, ResourceProvider {
"The specified queue does not exist.", 400));
validateMaximumMessageSize(attributes);
attributes = validateMessageRetentionPeriod(attributes);
attributes = compactRedrivePolicies(attributes);
if (attributes != null) {
for (Map.Entry<String, String> entry : attributes.entrySet()) {
if (entry.getValue() == null || entry.getValue().isEmpty()) {
@@ -1048,6 +1048,49 @@ class SqsJsonProtocolTest {
return "{\"QueueUrl\":\"" + url + "\",\"Entries\":[" + entries + "]}";
}
@Test
void redrivePolicyReadsBackAsCompactJson() {
String dlqUrl = given()
.contentType(CONTENT_TYPE)
.header("X-Amz-Target", "AmazonSQS.CreateQueue")
.body("{\"QueueName\":\"json-redrive-compact-dlq\"}")
.when().post("/")
.then().statusCode(200)
.extract().path("QueueUrl");
String dlqArn = given()
.contentType(CONTENT_TYPE)
.header("X-Amz-Target", "AmazonSQS.GetQueueAttributes")
.body("{\"QueueUrl\":\"" + dlqUrl + "\",\"AttributeNames\":[\"QueueArn\"]}")
.when().post("/")
.then().statusCode(200)
.extract().path("Attributes.QueueArn");
String sourceUrl = given()
.contentType(CONTENT_TYPE)
.header("X-Amz-Target", "AmazonSQS.CreateQueue")
.body("{\"QueueName\":\"json-redrive-compact-source\"}")
.when().post("/")
.then().statusCode(200)
.extract().path("QueueUrl");
// A trailing newline, as a YAML block scalar leaves on the document.
String policy = "{\\\"deadLetterTargetArn\\\":\\\"" + dlqArn + "\\\",\\\"maxReceiveCount\\\":3}\\n";
given()
.contentType(CONTENT_TYPE)
.header("X-Amz-Target", "AmazonSQS.SetQueueAttributes")
.body("{\"QueueUrl\":\"" + sourceUrl + "\",\"Attributes\":{\"RedrivePolicy\":\"" + policy + "\"}}")
.when().post("/")
.then().statusCode(200);
given()
.contentType(CONTENT_TYPE)
.header("X-Amz-Target", "AmazonSQS.GetQueueAttributes")
.body("{\"QueueUrl\":\"" + sourceUrl + "\",\"AttributeNames\":[\"RedrivePolicy\"]}")
.when().post("/")
.then().statusCode(200)
.body("Attributes.RedrivePolicy",
equalTo("{\"deadLetterTargetArn\":\"" + dlqArn + "\",\"maxReceiveCount\":3}"));
}
@Test
void queueUrlsUseTheFlociFormByDefault() {
SqsQueueUrlRoundTrip.json(AUTH_SQS_EU_WEST_2, "eu-west-2", "json-floci-url-queue",
@@ -268,6 +268,69 @@ class SqsServiceTest {
List.of("MessageRetentionPeriod"), region).get("MessageRetentionPeriod"));
}
@Test
void redrivePolicyIsStoredAsCompactJson() {
String region = "eu-west-1";
Queue dlq = sqsService.createQueue("redrive-compact-dlq", null, region);
String dlqArn = sqsService.getQueueAttributes(dlq.getQueueUrl(), List.of("QueueArn"), region).get("QueueArn");
String spaced = "{\n \"deadLetterTargetArn\": \"" + dlqArn + "\",\n \"maxReceiveCount\": 3\n}\n";
String compact = "{\"deadLetterTargetArn\":\"" + dlqArn + "\",\"maxReceiveCount\":3}";
Queue created = sqsService.createQueue("redrive-compact-created", Map.of("RedrivePolicy", spaced), region);
assertEquals(compact, sqsService.getQueueAttributes(created.getQueueUrl(),
List.of("RedrivePolicy"), region).get("RedrivePolicy"));
Queue updated = sqsService.createQueue("redrive-compact-updated", null, region);
sqsService.setQueueAttributes(updated.getQueueUrl(), Map.of("RedrivePolicy", spaced), region);
assertEquals(compact, sqsService.getQueueAttributes(updated.getQueueUrl(),
List.of("RedrivePolicy"), region).get("RedrivePolicy"));
}
@Test
void redriveAllowPolicyIsStoredAsCompactJson() {
String region = "eu-west-1";
Queue queue = sqsService.createQueue("redrive-allow-compact", null, region);
sqsService.setQueueAttributes(queue.getQueueUrl(),
Map.of("RedriveAllowPolicy", "{ \"redrivePermission\": \"allowAll\" }\n"), region);
assertEquals("{\"redrivePermission\":\"allowAll\"}", sqsService.getQueueAttributes(queue.getQueueUrl(),
List.of("RedriveAllowPolicy"), region).get("RedriveAllowPolicy"));
}
@Test
void createQueue_recreateWithSameRedrivePolicyInOtherWhitespaceIsNotAConflict() {
String region = "eu-west-1";
Queue dlq = sqsService.createQueue("redrive-recreate-dlq", null, region);
String dlqArn = sqsService.getQueueAttributes(dlq.getQueueUrl(), List.of("QueueArn"), region).get("QueueArn");
String compact = "{\"deadLetterTargetArn\":\"" + dlqArn + "\",\"maxReceiveCount\":3}";
Queue queue = sqsService.createQueue("redrive-recreate", Map.of("RedrivePolicy", compact), region);
Queue same = sqsService.createQueue("redrive-recreate", Map.of("RedrivePolicy", compact + "\n"), region);
assertEquals(queue.getQueueUrl(), same.getQueueUrl());
}
@Test
void createQueue_recreateMatchesASavedNonCompactRedrivePolicy() {
String region = "eu-west-1";
InMemoryStorage<String, Queue> store = new InMemoryStorage<>();
SqsService service = new SqsService(store, 30, 1048576, BASE_URL, clock);
Queue dlq = service.createQueue("saved-redrive-dlq", null, region);
String dlqArn = service.getQueueAttributes(dlq.getQueueUrl(), List.of("QueueArn"), region).get("QueueArn");
String original = "{\"deadLetterTargetArn\":\"" + dlqArn + "\",\"maxReceiveCount\":3}\n";
Queue queue = service.createQueue("saved-redrive", Map.of("RedrivePolicy", original), region);
// A queue persisted by a build that stored the policy as sent, whitespace included.
String storageKey = store.keys().stream()
.filter(key -> key.endsWith("/saved-redrive"))
.findFirst().orElseThrow();
Queue stored = store.get(storageKey).orElseThrow();
stored.getAttributes().put("RedrivePolicy", original);
store.put(storageKey, stored);
Queue same = service.createQueue("saved-redrive", Map.of("RedrivePolicy", original), region);
assertEquals(queue.getQueueUrl(), same.getQueueUrl(),
"Resending the original CreateQueue request must not conflict with the saved queue");
}
@Test
void createQueue_acceptsTheAwsCeilingWhenTheConfiguredMaximumIsLower() {
String region = "eu-west-1";