-
-
Notifications
You must be signed in to change notification settings - Fork 176
fix(dynamodb): attach X-Amz-Crc32 header to JSON protocol responses #347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbResponses.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package io.github.hectorvent.floci.services.dynamodb; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import jakarta.ws.rs.core.MediaType; | ||
| import jakarta.ws.rs.core.MultivaluedMap; | ||
| import jakarta.ws.rs.core.Response; | ||
| import org.jboss.logging.Logger; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.zip.CRC32; | ||
|
|
||
| /** | ||
| * Helpers for building DynamoDB JSON protocol responses. | ||
| */ | ||
| public final class DynamoDbResponses { | ||
|
|
||
| private static final Logger LOG = Logger.getLogger(DynamoDbResponses.class); | ||
|
|
||
| private DynamoDbResponses() {} | ||
|
|
||
| /** | ||
| * Pre-serialize the response entity and attach an {@code X-Amz-Crc32} header whose value | ||
| * is the decimal CRC32 of the serialized bytes. | ||
| * | ||
| * <p>Real AWS DynamoDB includes this header on every response, and the AWS SDK for Go v2 | ||
| * DynamoDB client wraps the response body in a CRC32-verifying reader | ||
| * ({@code service/dynamodb/internal/customizations/checksum.go}). When the header is | ||
| * missing the wrapper compares the computed CRC32 against an expected value of 0 on | ||
| * {@code Close()}, returns a checksum error, and smithy-go logs | ||
| * "failed to close HTTP response body, this may affect connection reuse" for every | ||
| * call. Sending a correct header silences the warning and gives clients a real | ||
| * integrity check. | ||
| * | ||
| * <p>This is applied only at the JSON protocol boundary (e.g. {@code AwsJsonController}) | ||
| * because {@link DynamoDbJsonHandler} is also invoked from CBOR, API Gateway proxy, and | ||
| * Step Functions task flows — those callers keep the original {@code ObjectNode} entity. | ||
| */ | ||
| public static Response withCrc32(Response response, ObjectMapper objectMapper) { | ||
| if (response == null) { | ||
| return null; | ||
| } | ||
| Object entity = response.getEntity(); | ||
| byte[] bodyBytes; | ||
| try { | ||
| if (entity == null) { | ||
| bodyBytes = new byte[0]; | ||
| } else if (entity instanceof byte[] b) { | ||
| bodyBytes = b; | ||
| } else { | ||
| bodyBytes = objectMapper.writeValueAsBytes(entity); | ||
| } | ||
| } catch (Exception e) { | ||
| LOG.warn("Failed to serialize DynamoDB response for CRC32 computation", e); | ||
| return response; | ||
| } | ||
|
|
||
| CRC32 crc = new CRC32(); | ||
| crc.update(bodyBytes); | ||
|
|
||
| Response.ResponseBuilder builder = Response.status(response.getStatus()) | ||
| .entity(bodyBytes) | ||
| .type(MediaType.valueOf("application/x-amz-json-1.0")) | ||
| .header("X-Amz-Crc32", Long.toString(crc.getValue())); | ||
|
|
||
| MultivaluedMap<String, Object> existing = response.getHeaders(); | ||
| if (existing != null) { | ||
| for (Map.Entry<String, List<Object>> e : existing.entrySet()) { | ||
| String name = e.getKey(); | ||
| if ("Content-Type".equalsIgnoreCase(name) | ||
| || "Content-Length".equalsIgnoreCase(name) | ||
| || "X-Amz-Crc32".equalsIgnoreCase(name)) { | ||
| continue; | ||
| } | ||
| for (Object v : e.getValue()) { | ||
| builder.header(name, v); | ||
| } | ||
| } | ||
| } | ||
| return builder.build(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbResponsesTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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)); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.