fix(cloudwatch): decompress gzip JSON PutMetricData bodies (#7123)

* fix(cloudwatch): decompress gzip JSON PutMetricData bodies

The AWS CLI and the SDKs that speak JSON 1.0 to CloudWatch gzip a
PutMetricData body of 10 KiB or more and send Content-Encoding: gzip.
AwsJsonController binds the body as a String, so Floci read the
compressed bytes as text and every such request failed with HTTP 400
SerializationException. The metrics were never stored.

Add a reader interceptor that decompresses that one operation's body
when the controller reads it. It runs after every request filter, so
the signature is still verified over the compressed bytes the client
signed, and IAM still decides the request from X-Amz-Target first,
which is sound because nothing in that decision comes from the body.
The decompressed body is bounded by the decoder the CBOR protocol
already uses: past 10 MiB it returns HTTP 413 PayloadTooLargeException,
and a body that cannot be decompressed returns HTTP 400
InvalidRequestException, both in the controller's JSON error format.

A name binding keeps the interceptor on AwsJsonController alone, so
every other resource reads its body as before. In the native image a
global reader interceptor breaks the tag endpoints that resources
inherit from AbstractTagsController, because Quarkus REST then looks
their methods up reflectively.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* test(cloudwatch): publish a compressed metric batch through boto3

botocore gzips a PutMetricData body of 10 KiB or more, and with the
pinned boto3 it calls CloudWatch over JSON 1.0. The Python
compatibility suite only ever sent a handful of datums, so it never
sent a compressed request.

Send a 100-datum batch, check that botocore sent it as JSON 1.0 with
Content-Encoding: gzip, and check that all 100 metrics are listed
afterwards. Checking the content type keeps the test from passing
through the CBOR path, which already decompressed gzip, should a later
botocore prefer CBOR.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* chore(cloudwatch): reuse signed IAM test users

---------

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
This commit is contained in:
Jackson Price authored and GitHub committed 2026-10-31 16:53:57 -04:00
1 parent 246473acf5
commit 3cb727a85e
6 files changed
+399

No files matched your search

@@ -36,10 +36,14 @@ import org.jboss.logging.Logger;
* - DynamoDB (DynamoDB_20120810.*)
* - SQS (AmazonSQS.*)
* <p>
* A gzip-encoded CloudWatch {@code PutMetricData} body arrives here decompressed, by
* {@link CloudWatchGzipReaderInterceptor}.
* <p>
* Requests run on virtual threads: SQS ReceiveMessage and Step Functions GetActivityTask wait up
* to 20 and 60 seconds for work, and that wait must not hold one of the shared request workers.
*/
@Path("/")
@CloudWatchGzipBody
public class AwsJsonController {
public static final String CONTENT_TYPE_AWS_JSON_1_0 = "application/x-amz-json-1.0";
@@ -0,0 +1,21 @@
package io.github.hectorvent.floci.core.common;
import jakarta.ws.rs.NameBinding;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Binds {@link CloudWatchGzipReaderInterceptor} to the resources annotated with it; every other
* resource reads its body with no reader interceptor. For each intercepted read Quarkus REST
* builds an interceptor context and looks up the resource method's parameter annotations
* reflectively, a lookup that fails in Floci's native image for the tag endpoints resources
* inherit from {@link AbstractTagsController}.
*/
@NameBinding
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface CloudWatchGzipBody {
}
@@ -0,0 +1,79 @@
package io.github.hectorvent.floci.core.common;
import jakarta.annotation.Priority;
import jakarta.inject.Inject;
import jakarta.ws.rs.Priorities;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.Provider;
import jakarta.ws.rs.ext.ReaderInterceptor;
import jakarta.ws.rs.ext.ReaderInterceptorContext;
import java.io.ByteArrayInputStream;
import java.io.IOException;
/**
* Decompresses a gzip-encoded JSON 1.0 {@code PutMetricData} body for {@link AwsJsonController}.
* It is the one operation the AWS CLI and the SDKs that call CloudWatch over JSON compress, which
* by default they do for any body of 10 KiB or more.
*
* <p>An entity interceptor runs when the resource method reads its body, after every request
* filter. By then {@link SigV4HeaderSignatureFilter} has verified the signature over the
* compressed bytes as sent, which are the bytes the client signed, and {@link IamEnforcementFilter}
* has decided the request from its {@code X-Amz-Target}. Deciding before decompression is sound
* only because nothing in the {@code PutMetricData} decision comes from the body: its resource is
* {@code *}. A condition key read from the body, such as {@code cloudwatch:namespace}, would need
* the body decompressed before IAM.
*
* <p>The decompressed body is bounded as the CBOR protocol bounds it
* ({@link AwsJsonCborController#decodeBody}), and a body that is too large or cannot be
* decompressed is refused in the controller's JSON error format. Every other request reads its
* body as sent.
*/
@Provider
@CloudWatchGzipBody
@Priority(Priorities.ENTITY_CODER)
public class CloudWatchGzipReaderInterceptor implements ReaderInterceptor {
private static final MediaType AWS_JSON_1_0 =
MediaType.valueOf(AwsJsonController.CONTENT_TYPE_AWS_JSON_1_0);
private final ResolvedServiceCatalog catalog;
@Inject
public CloudWatchGzipReaderInterceptor(ResolvedServiceCatalog catalog) {
this.catalog = catalog;
}
@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException {
MultivaluedMap<String, String> headers = context.getHeaders();
if (!AwsJsonCborController.isGZipped(headers.get(HttpHeaders.CONTENT_ENCODING))
|| context.getMediaType() == null || !context.getMediaType().isCompatible(AWS_JSON_1_0)
|| !isMetricPublication(headers.getFirst("X-Amz-Target"))) {
return context.proceed();
}
byte[] body = context.getInputStream().readAllBytes();
// No payload is an operation without input, as on the CBOR protocol, not a malformed gzip stream.
if (body.length > 0) {
try {
body = AwsJsonCborController.decodeBody(body);
} catch (AwsException e) {
throw new WebApplicationException(JsonErrorResponseUtils.createErrorResponse(e));
}
}
context.setInputStream(new ByteArrayInputStream(body));
return context.proceed();
}
private boolean isMetricPublication(String target) {
return catalog.matchTarget(target)
.filter(match -> "monitoring".equals(match.descriptor().externalKey()))
.map(ServiceCatalog.TargetMatch::action)
// CloudWatchMetricsJsonHandler runs putMetricData as PutMetricData too.
.filter(action -> "PutMetricData".equals(action) || "putMetricData".equals(action))
.isPresent();
}
}