mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
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:
1 parent
bc458235f3
commit
3d3f12d912
4 files changed
+254
-3
No files matched your search
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsJsonHandler;
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbJsonHandler;
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbResponses;
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbStreamsJsonHandler;
|
||||
import io.github.hectorvent.floci.services.sns.SnsJsonHandler;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsJsonHandler;
|
||||
@@ -102,11 +103,12 @@ public class AwsJsonController {
|
||||
action = target.substring(prefix.length());
|
||||
LOG.debugv("{0} JSON action: {1}", serviceName, action);
|
||||
|
||||
Response response;
|
||||
try {
|
||||
JsonNode request = objectMapper.readTree(body);
|
||||
String region = regionResolver.resolveRegion(httpHeaders);
|
||||
|
||||
return switch (serviceName) {
|
||||
response = switch (serviceName) {
|
||||
case "DynamoDB" -> dynamoDbJsonHandler.handle(action, request, region);
|
||||
case "DynamoDBStreams" -> dynamoDbStreamsJsonHandler.handle(action, request, region);
|
||||
case "SQS" -> sqsJsonHandler.handle(action, request, region);
|
||||
@@ -116,10 +118,20 @@ public class AwsJsonController {
|
||||
default -> null;
|
||||
};
|
||||
} catch (AwsException e) {
|
||||
return JsonErrorResponseUtils.createErrorResponse(e);
|
||||
response = JsonErrorResponseUtils.createErrorResponse(e);
|
||||
} catch (Exception e) {
|
||||
LOG.error("Error processing " + serviceName + " JSON request", e);
|
||||
return JsonErrorResponseUtils.createErrorResponse(e);
|
||||
response = JsonErrorResponseUtils.createErrorResponse(e);
|
||||
}
|
||||
|
||||
// Real AWS DynamoDB attaches X-Amz-Crc32 to every response. The Go SDK DynamoDB
|
||||
// client verifies this header on body Close() and logs "failed to close HTTP
|
||||
// response body" when the header is missing — attach it here at the JSON protocol
|
||||
// boundary so other callers of DynamoDbJsonHandler (CBOR, API Gateway proxy,
|
||||
// Step Functions tasks) keep their original ObjectNode entity.
|
||||
if ("DynamoDB".equals(serviceName) || "DynamoDBStreams".equals(serviceName)) {
|
||||
return DynamoDbResponses.withCrc32(response, objectMapper);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user