mirror of
https://github.com/floci-io/floci.git
synced 2026-11-28 08:09:43 -05:00
fix(cloudwatch): route Query GET requests through CloudWatch (#5589)
* fix(cloudwatch): route public Query GET requests correctly Reuse Query dispatch and IAM classification for supported CloudWatch GET requests while preserving S3 and presigned URL routing. * test(cloudwatch): verify Query header name casing Signed-off-by: Andre Kurait <akurait@amazon.com> * fix(cloudwatch): match signed Query GET errors and add SDK coverage Signed-off-by: Andre Kurait <akurait@amazon.com> * fix(test): isolate TLS fixtures and escape invitation passwords Signed-off-by: Andre Kurait <akurait@amazon.com> * chore: declare directly used dependency APIs Signed-off-by: Andre Kurait <akurait@amazon.com> * fix(cloudwatch): enforce Query GET authentication and IAM policies Signed-off-by: Andre Kurait <akurait@amazon.com> * chore: follow existing framework dependency declarations Signed-off-by: Andre Kurait <akurait@amazon.com> * fix(cloudwatch): simplify signed Query GET routing Reuse the existing presigned credential resolver and let the signing scope select CloudWatch before browser or target headers. Match the current and legacy API versions observed in AWS, and cover header/presigned IAM checks. Remove unrelated CI fixture changes from this PR; they remain on the ASG branch. Signed-off-by: Andre Kurait <akurait@amazon.com> * fix(cloudwatch): isolate Query GET routing and authentication Signed-off-by: Andre Kurait <akurait@amazon.com> --------- Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
1 parent
9df4ffbfa1
commit
984e8187c9
17 files changed
+778
-38
No files matched your search
@@ -73,6 +73,10 @@ public class AwsProtocolClaimFilter implements ContainerRequestFilter {
|
||||
|
||||
ProtocolClaim resolved = claim.get();
|
||||
ctx.setProperty(CLAIM_PROPERTY, resolved);
|
||||
if (resolved.protocol() == WireProtocol.AWS_QUERY && "GET".equalsIgnoreCase(signals.method())) {
|
||||
ctx.setRequestUri(ctx.getUriInfo().getRequestUriBuilder()
|
||||
.replacePath(AwsQueryController.CLOUDWATCH_GET_PATH).build());
|
||||
}
|
||||
if (resolved.protocol() == WireProtocol.REST) {
|
||||
Optional<String> scope = SigV4CredentialScope.serviceName(signals.authorization());
|
||||
if (scope.isPresent() && catalog.byCredentialScope(scope.get()).isEmpty()) {
|
||||
|
||||
@@ -23,15 +23,17 @@ import io.github.hectorvent.floci.services.sns.SnsQueryHandler;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsQueryHandler;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.Consumes;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.HeaderParam;
|
||||
import jakarta.ws.rs.POST;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.Produces;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
import jakarta.ws.rs.core.Context;
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.MultivaluedMap;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import jakarta.ws.rs.core.UriInfo;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.List;
|
||||
@@ -52,6 +54,8 @@ import java.util.stream.Stream;
|
||||
@Path("/")
|
||||
public class AwsQueryController {
|
||||
|
||||
static final String CLOUDWATCH_GET_PATH = "/_cloudwatch-query";
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(AwsQueryController.class);
|
||||
|
||||
private static final Set<String> STS_ACTIONS = Set.of(
|
||||
@@ -298,7 +302,6 @@ public class AwsQueryController {
|
||||
@Produces(MediaType.APPLICATION_XML)
|
||||
public Response dispatch(
|
||||
@HeaderParam("Authorization") String authorization,
|
||||
@Context HttpHeaders httpHeaders,
|
||||
MultivaluedMap<String, String> formParams) {
|
||||
|
||||
String action = AwsQueryServiceResolver.action(formParams);
|
||||
@@ -310,7 +313,7 @@ public class AwsQueryController {
|
||||
String service = serviceResolver.resolve(authorization, action, formParams.getFirst("Version"));
|
||||
LOG.debugv("Query protocol service={0} action={1}", service, action);
|
||||
|
||||
String region = regionResolver.resolveRegion(httpHeaders);
|
||||
String region = regionResolver.resolveRegionFromAuth(authorization);
|
||||
|
||||
try {
|
||||
return dispatchToHandler(service, action, formParams, authorization, region);
|
||||
@@ -535,6 +538,8 @@ public class AwsQueryController {
|
||||
"ListTagsForResource"
|
||||
);
|
||||
|
||||
private static final String CLOUDWATCH_API_VERSION = "2010-08-01";
|
||||
|
||||
private static final Set<String> CLOUDWATCH_ACTIONS = Set.of(
|
||||
"PutMetricData", "ListMetrics", "GetMetricStatistics", "GetMetricData",
|
||||
"PutMetricAlarm", "DescribeAlarms", "DeleteAlarms", "SetAlarmState",
|
||||
@@ -651,6 +656,49 @@ public class AwsQueryController {
|
||||
"GetClusterCredentials", "GetClusterCredentialsWithIAM"
|
||||
);
|
||||
|
||||
/** Root GET requests that can use the CloudWatch Query handler without claiming S3 traffic. */
|
||||
public static boolean isCloudWatchQueryGet(String authorization, String target, String accept,
|
||||
MultivaluedMap<String, String> queryParams) {
|
||||
String auth = IamEnforcementFilter.requestAuthorization(authorization, queryParams);
|
||||
if (auth != null) {
|
||||
return SigV4CredentialScope.serviceName(auth).filter("monitoring"::equals).isPresent();
|
||||
}
|
||||
String action = AwsQueryServiceResolver.action(queryParams);
|
||||
return target == null && (accept == null || !accept.contains(MediaType.TEXT_HTML)) && action != null
|
||||
&& "monitoring".equals(inferServiceFromAction(action, queryParams.getFirst("Version")));
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path(CLOUDWATCH_GET_PATH)
|
||||
@Produces({MediaType.APPLICATION_XML, MediaType.TEXT_HTML})
|
||||
public Response dispatchCloudWatchQueryGet(@HeaderParam("Authorization") String authorization,
|
||||
@Context UriInfo uriInfo,
|
||||
@Context ContainerRequestContext request) {
|
||||
// Only the pre-matching Query claim may select this internal route.
|
||||
if (!(request.getProperty(AwsProtocolClaimFilter.CLAIM_PROPERTY) instanceof ProtocolClaim claim)
|
||||
|| claim.protocol() != WireProtocol.AWS_QUERY) {
|
||||
return Response.status(404).build();
|
||||
}
|
||||
MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
|
||||
String action = AwsQueryServiceResolver.action(queryParams);
|
||||
if (action == null) {
|
||||
return Response.status(404).type(MediaType.APPLICATION_XML)
|
||||
.entity("<UnknownOperationException/>").build();
|
||||
}
|
||||
String version = queryParams.getFirst("Version");
|
||||
version = version == null ? "2009-05-15" : version;
|
||||
Set<String> actions = switch (version) {
|
||||
case "2009-05-15" -> Set.of("ListMetrics", "GetMetricStatistics");
|
||||
case CLOUDWATCH_API_VERSION -> CLOUDWATCH_ACTIONS;
|
||||
default -> Set.of();
|
||||
};
|
||||
if (!actions.contains(action)) {
|
||||
return xmlErrorResponse("InvalidAction", "Could not find operation " + action + " for version "
|
||||
+ version, 404);
|
||||
}
|
||||
return dispatch(IamEnforcementFilter.requestAuthorization(authorization, queryParams), queryParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tried last, so by name alone SQS takes only what no other service defines, as it takes a name
|
||||
* nobody defines. Its {@code Version} is what wins back the two names SNS also defines.
|
||||
@@ -691,7 +739,7 @@ public class AwsQueryController {
|
||||
new QueryService("sns", Set.of("2010-03-31"), SNS_ACTIONS),
|
||||
new QueryService("elasticache", Set.of("2015-02-02"), ELASTICACHE_ACTIONS),
|
||||
new QueryService("rds", Set.of("2014-10-31"), RDS_ACTIONS),
|
||||
new QueryService("monitoring", Set.of("2010-08-01"), CLOUDWATCH_ACTIONS),
|
||||
new QueryService("monitoring", Set.of(CLOUDWATCH_API_VERSION), CLOUDWATCH_ACTIONS),
|
||||
new QueryService("cloudformation", Set.of("2010-05-15"), CLOUDFORMATION_ACTIONS),
|
||||
new QueryService("email", Set.of("2010-12-01"), SES_ACTIONS),
|
||||
new QueryService("cognito-idp", Set.of(), COGNITO_ACTIONS),
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
import jakarta.ws.rs.core.MultivaluedMap;
|
||||
|
||||
/**
|
||||
* The non-payload identification signals a request's wire protocol is claimed
|
||||
* from: HTTP method, path, and the claim-relevant headers. The entity stream
|
||||
* from: HTTP method, path, query parameters, and claim-relevant headers. The entity stream
|
||||
* is deliberately absent — claiming must never touch it.
|
||||
*
|
||||
* @param method the HTTP method
|
||||
@@ -14,9 +15,12 @@ import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
* @param smithyProtocol the raw Smithy-Protocol header, or null
|
||||
* @param xAmzTarget the raw X-Amz-Target header, or null
|
||||
* @param authorization the raw Authorization header, or null
|
||||
* @param accept the raw Accept header, or null
|
||||
* @param queryParameters the request query parameters
|
||||
*/
|
||||
public record ClaimSignals(String method, String path, String contentType,
|
||||
String smithyProtocol, String xAmzTarget, String authorization) {
|
||||
String smithyProtocol, String xAmzTarget, String authorization,
|
||||
String accept, MultivaluedMap<String, String> queryParameters) {
|
||||
|
||||
static ClaimSignals from(ContainerRequestContext ctx) {
|
||||
String contentType = ctx.getHeaderString(AwsCborContentTypeFilter.ORIGINAL_CONTENT_TYPE_HEADER);
|
||||
@@ -29,6 +33,8 @@ public record ClaimSignals(String method, String path, String contentType,
|
||||
contentType,
|
||||
ctx.getHeaderString(ProtocolClaimer.SMITHY_PROTOCOL_HEADER),
|
||||
ctx.getHeaderString("X-Amz-Target"),
|
||||
ctx.getHeaderString("Authorization"));
|
||||
ctx.getHeaderString("Authorization"),
|
||||
ctx.getHeaderString("Accept"),
|
||||
ctx.getUriInfo().getQueryParameters());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -552,7 +552,7 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
private ResolvedAuthorization resolveAuthorization(String auth, String claimedScope,
|
||||
ContainerRequestContext ctx) {
|
||||
Object claimValue = ctx.getProperty(AwsProtocolClaimFilter.CLAIM_PROPERTY);
|
||||
// Authorization must not depend on the pre-matching claim filter running first.
|
||||
// Form POSTs are also recognized without a claim; GET dispatch requires the Query claim.
|
||||
if (IamActionRegistry.isQueryRequest(ctx)) {
|
||||
String queryAction = actionRegistry.queryAction(ctx);
|
||||
if (queryAction == null || queryAction.isBlank()) {
|
||||
@@ -774,7 +774,7 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
// Sorting the rest keeps the choice stable, since credentialScopes is a Set.of whose
|
||||
// iteration order changes per JVM run.
|
||||
if (descriptor.credentialScopes().contains(descriptor.externalKey())) {
|
||||
return descriptor.externalKey();
|
||||
return catalog.canonicalCredentialScope(descriptor.externalKey());
|
||||
}
|
||||
return descriptor.credentialScopes().stream()
|
||||
.filter(scope -> scope.equals(catalog.canonicalCredentialScope(scope)))
|
||||
@@ -1454,7 +1454,7 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
if (isCbor(protocol)) {
|
||||
return cborError(ctx, code, message, status);
|
||||
}
|
||||
if (isFormEncoded(ctx.getMediaType())) {
|
||||
if (protocol == WireProtocol.AWS_QUERY || isFormEncoded(ctx.getMediaType())) {
|
||||
return queryXmlError(queryCode, message, status);
|
||||
}
|
||||
return jsonError(code, message, status);
|
||||
|
||||
@@ -15,10 +15,11 @@ import java.util.regex.Pattern;
|
||||
* first protocol whose "identification for claiming" signals match claims the
|
||||
* request.
|
||||
* <p>
|
||||
* Claiming uses non-payload signals only (method, path, headers) so it is safe
|
||||
* Claiming uses non-payload signals only (method, path, headers, query parameters) so it is safe
|
||||
* to run in a pre-matching filter without touching the entity stream. As a
|
||||
* consequence the awsQuery claim is narrowed to POST {@code /} with the form
|
||||
* content type; the spec's body-level {@code Action}/{@code Version} checks
|
||||
* consequence the awsQuery claim covers POST {@code /} with the form
|
||||
* content type and the CloudWatch GET requests the dispatcher accepts.
|
||||
* The spec's body-level {@code Action}/{@code Version} checks
|
||||
* stay downstream in {@code AwsQueryController}, which already rejects a
|
||||
* missing {@code Action} with the AWS {@code MissingAction} error. Nothing
|
||||
* below awsQuery in the precision order claims such requests, so the narrowed
|
||||
@@ -92,6 +93,14 @@ public class ProtocolClaimer {
|
||||
return Optional.of(rpcV2Claim(WireProtocol.RPCV2_CBOR, rpcV2Path));
|
||||
}
|
||||
|
||||
if ("GET".equalsIgnoreCase(signals.method()) && isRootPath(path)
|
||||
&& AwsQueryController.isCloudWatchQueryGet(signals.authorization(),
|
||||
signals.xAmzTarget(), signals.accept(), signals.queryParameters())) {
|
||||
ServiceDescriptor service = catalog.byCredentialScope("monitoring").orElse(null);
|
||||
return Optional.of(new ProtocolClaim(WireProtocol.AWS_QUERY, service,
|
||||
AwsQueryServiceResolver.action(signals.queryParameters()), null));
|
||||
}
|
||||
|
||||
if (post && isRootPath(path)) {
|
||||
String target = signals.xAmzTarget();
|
||||
if (target != null) {
|
||||
|
||||
@@ -104,6 +104,7 @@ public class ResolvedServiceCatalog {
|
||||
java.util.Map.of(
|
||||
"s3express", "s3",
|
||||
"iot-jobs-data", "iot",
|
||||
"monitoring", "cloudwatch",
|
||||
"awsssoportal", "sso");
|
||||
|
||||
private final ServiceCatalog catalog;
|
||||
|
||||
+3
-1
@@ -448,7 +448,9 @@ public class SigV4HeaderSignatureFilter implements ContainerRequestFilter {
|
||||
CborErrorResponses.mediaTypeFor(contentType)));
|
||||
return;
|
||||
}
|
||||
if (isFormEncoded(ctx.getMediaType())) {
|
||||
if (isFormEncoded(ctx.getMediaType())
|
||||
|| ctx.getProperty(AwsProtocolClaimFilter.CLAIM_PROPERTY) instanceof ProtocolClaim claim
|
||||
&& claim.protocol() == WireProtocol.AWS_QUERY) {
|
||||
ctx.abortWith(AwsQueryResponse.error(queryCode, message, null, status));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsProtocolClaimFilter;
|
||||
import io.github.hectorvent.floci.core.common.AwsQueryServiceResolver;
|
||||
import io.github.hectorvent.floci.core.common.ProtocolClaim;
|
||||
import io.github.hectorvent.floci.core.common.WireProtocol;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
@@ -318,32 +321,42 @@ public class IamActionRegistry {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Query-protocol action from the form body while preserving the entity stream for
|
||||
* the controller. The controller dispatches only the form body, using the same Action-first,
|
||||
* Operation-second rule. A URL Action must not override the operation that will execute.
|
||||
* Returns the Query action from GET query parameters or the POST form body, preserving the
|
||||
* entity stream. Both use the controller's Action-first, Operation-second rule; a POST URL
|
||||
* Action must not override the form operation that will execute.
|
||||
*/
|
||||
public String queryAction(ContainerRequestContext ctx) {
|
||||
if ("GET".equalsIgnoreCase(ctx.getMethod())) {
|
||||
return AwsQueryServiceResolver.action(ctx.getUriInfo().getQueryParameters());
|
||||
}
|
||||
return AwsQueryServiceResolver.action(
|
||||
RequestBodyReader.formField(ctx, "Action"),
|
||||
RequestBodyReader.formField(ctx, "Operation"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the API version a Query request declares, from the form body for the same reason as
|
||||
* {@link #queryAction}: it is the value the controller routes on.
|
||||
*/
|
||||
/** Returns the API version from the same Query input that the controller dispatches. */
|
||||
public String queryVersion(ContainerRequestContext ctx) {
|
||||
if ("GET".equalsIgnoreCase(ctx.getMethod())) {
|
||||
return ctx.getUriInfo().getQueryParameters().getFirst("Version");
|
||||
}
|
||||
return RequestBodyReader.formField(ctx, "Version");
|
||||
}
|
||||
|
||||
/** The same form POST at the root that the Query controller dispatches. */
|
||||
/** The root form POST or CloudWatch GET that the Query controller dispatches. */
|
||||
public static boolean isQueryRequest(ContainerRequestContext ctx) {
|
||||
if (!"POST".equalsIgnoreCase(ctx.getMethod()) || ctx.getUriInfo() == null) {
|
||||
if ("GET".equalsIgnoreCase(ctx.getMethod())) {
|
||||
return ctx.getProperty(AwsProtocolClaimFilter.CLAIM_PROPERTY) instanceof ProtocolClaim claim
|
||||
&& claim.protocol() == WireProtocol.AWS_QUERY;
|
||||
}
|
||||
if (ctx.getUriInfo() == null) {
|
||||
return false;
|
||||
}
|
||||
String path = ctx.getUriInfo().getPath();
|
||||
if (path != null && !path.isEmpty() && !"/".equals(path)) {
|
||||
return false;
|
||||
}
|
||||
MediaType mediaType = ctx.getMediaType();
|
||||
return (path == null || path.isEmpty() || "/".equals(path))
|
||||
return "POST".equalsIgnoreCase(ctx.getMethod())
|
||||
&& mediaType != null
|
||||
&& "application".equalsIgnoreCase(mediaType.getType())
|
||||
&& "x-www-form-urlencoded".equalsIgnoreCase(mediaType.getSubtype());
|
||||
|
||||
Reference in new issue
Block a user