fix(dynamodb): attach X-Amz-Crc32 header to JSON protocol responses (#347)

* fix(dynamodb): attach X-Amz-Crc32 header to JSON protocol responses

The AWS SDK for Go v2 DynamoDB client wraps every response body in a
CRC32-verifying reader and emits "failed to close HTTP response body,
this may affect connection reuse" on every call when the header is
missing. Real AWS DynamoDB includes this header on all responses.

Add it at the JSON protocol boundary (AwsJsonController) for both
DynamoDB and DynamoDBStreams, using a new DynamoDbResponses helper
that serializes the entity to bytes, computes the CRC32, and rebuilds
the Response with the bytes + header. Error responses are wrapped as
well to match real AWS behavior.

The wrapping lives in AwsJsonController rather than DynamoDbJsonHandler
because the handler is also invoked from CBOR, API Gateway proxy, and
Step Functions task flows — those callers must keep the original
ObjectNode entity.

Add unit tests for DynamoDbResponses and an integration test that
verifies the header is present and matches the CRC32 of the response
body bytes on both success and error responses.

* test(dynamodb): use explicit UTF-8 charset in DynamoDbResponsesTest

Address Copilot review comment on floci-io/floci#347: ".getBytes()"
uses the platform default charset, which can make the test (and the
computed CRC32) vary across environments. The string is ASCII so the
bytes are the same on any sensible platform, but being explicit about
UTF-8 makes the determinism obvious and matches the rest of the
codebase.
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-04-11 11:27:32 -05:00
1 parent bc458235f3
commit 3d3f12d912
4 files changed
+254 -3

No files matched your search

@@ -2,14 +2,19 @@ package io.github.hectorvent.floci.services.dynamodb;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.Response;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import java.util.zip.CRC32;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@QuarkusTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@@ -892,4 +897,41 @@ class DynamoDbIntegrationTest {
.statusCode(400)
.body("__type", equalTo("UnknownOperationException"));
}
@Test
void responseIncludesCorrectXAmzCrc32Header() {
// The AWS SDK for Go v2 DynamoDB client wraps the response body in a CRC32-verifying
// reader and emits "failed to close HTTP response body" warnings when the header is
// missing. Verify floci attaches the header on both success and error responses and
// that the value matches the CRC32 of the response body bytes.
Response listResponse = given()
.header("X-Amz-Target", "DynamoDB_20120810.ListTables")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("{}")
.when()
.post("/");
listResponse.then().statusCode(200);
String crcHeader = listResponse.getHeader("X-Amz-Crc32");
assertNotNull(crcHeader, "ListTables response must carry X-Amz-Crc32");
assertEquals(Long.toString(crc32Of(listResponse.asByteArray())), crcHeader);
Response errorResponse = given()
.header("X-Amz-Target", "DynamoDB_20120810.DescribeTable")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("{\"TableName\":\"does-not-exist-crc32-check\"}")
.when()
.post("/");
errorResponse.then().statusCode(400);
String errorCrc = errorResponse.getHeader("X-Amz-Crc32");
assertNotNull(errorCrc, "Error response must carry X-Amz-Crc32");
assertEquals(Long.toString(crc32Of(errorResponse.asByteArray())), errorCrc);
}
private static long crc32Of(byte[] bytes) {
CRC32 crc = new CRC32();
crc.update(bytes);
return crc.getValue();
}
}
@@ -0,0 +1,115 @@
package io.github.hectorvent.floci.services.dynamodb;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.util.zip.CRC32;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
class DynamoDbResponsesTest {
private ObjectMapper mapper;
@BeforeEach
void setUp() {
mapper = new ObjectMapper();
}
private static long crc32Of(byte[] bytes) {
CRC32 crc = new CRC32();
crc.update(bytes);
return crc.getValue();
}
@Test
void withCrc32_serializesObjectNode_andAttachesCorrectChecksum() throws Exception {
ObjectNode entity = mapper.createObjectNode();
entity.put("TableName", "users");
entity.putObject("TableStatus").put("value", "ACTIVE");
byte[] expectedBytes = mapper.writeValueAsBytes(entity);
long expectedCrc = crc32Of(expectedBytes);
Response input = Response.ok(entity).build();
Response wrapped = DynamoDbResponses.withCrc32(input, mapper);
assertNotNull(wrapped);
assertEquals(200, wrapped.getStatus());
assertArrayEquals(expectedBytes, (byte[]) wrapped.getEntity());
assertEquals(Long.toString(expectedCrc), wrapped.getHeaderString("X-Amz-Crc32"));
assertEquals("application/x-amz-json-1.0", wrapped.getMediaType().toString());
}
@Test
void withCrc32_byteArrayEntity_usedAsIs() {
byte[] rawBody = "{\"TableNames\":[]}".getBytes(StandardCharsets.UTF_8);
long expectedCrc = crc32Of(rawBody);
Response input = Response.ok(rawBody).build();
Response wrapped = DynamoDbResponses.withCrc32(input, mapper);
assertArrayEquals(rawBody, (byte[]) wrapped.getEntity());
assertEquals(Long.toString(expectedCrc), wrapped.getHeaderString("X-Amz-Crc32"));
}
@Test
void withCrc32_nullEntity_emptyBodyAndCrc32OfEmpty() {
Response input = Response.status(204).build();
Response wrapped = DynamoDbResponses.withCrc32(input, mapper);
assertEquals(204, wrapped.getStatus());
assertArrayEquals(new byte[0], (byte[]) wrapped.getEntity());
assertEquals(Long.toString(crc32Of(new byte[0])), wrapped.getHeaderString("X-Amz-Crc32"));
}
@Test
void withCrc32_preservesStatusCode_forErrorResponse() throws Exception {
ObjectNode errorBody = mapper.createObjectNode();
errorBody.put("__type", "ResourceNotFoundException");
errorBody.put("message", "Table not found");
Response input = Response.status(400).entity(errorBody).build();
Response wrapped = DynamoDbResponses.withCrc32(input, mapper);
assertEquals(400, wrapped.getStatus());
byte[] expectedBytes = mapper.writeValueAsBytes(errorBody);
assertArrayEquals(expectedBytes, (byte[]) wrapped.getEntity());
assertEquals(Long.toString(crc32Of(expectedBytes)), wrapped.getHeaderString("X-Amz-Crc32"));
}
@Test
void withCrc32_preservesCustomHeaders_overridesContentTypeAndLength() throws Exception {
ObjectNode entity = mapper.createObjectNode();
entity.put("ok", true);
Response input = Response.ok(entity)
.header("x-amz-request-id", "req-123")
.header("x-amz-id-2", "id-456")
.header("Content-Type", MediaType.APPLICATION_JSON)
.header("Content-Length", 999)
.build();
Response wrapped = DynamoDbResponses.withCrc32(input, mapper);
assertEquals("req-123", wrapped.getHeaderString("x-amz-request-id"));
assertEquals("id-456", wrapped.getHeaderString("x-amz-id-2"));
// Content-Type must be the DynamoDB JSON protocol type, not the one from the input
assertEquals("application/x-amz-json-1.0", wrapped.getMediaType().toString());
// Content-Length from input must not leak through; JAX-RS will recompute from bytes
assertNull(wrapped.getHeaderString("Content-Length"));
}
@Test
void withCrc32_nullResponse_returnsNull() {
assertNull(DynamoDbResponses.withCrc32(null, mapper));
}
}