mirror of
https://github.com/floci-io/floci.git
synced 2026-12-04 02:28:08 -05:00
fix(query): route signed GET requests through the Query dispatcher (#6007)
Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
1 parent
b955aa09ff
commit
a103b938d2
14 files changed
+451
-31
No files matched your search
@@ -75,7 +75,7 @@ public class AwsProtocolClaimFilter implements ContainerRequestFilter {
|
||||
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());
|
||||
.replacePath(AwsQueryController.QUERY_GET_PATH).build());
|
||||
}
|
||||
if (resolved.protocol() == WireProtocol.REST) {
|
||||
Optional<String> scope = SigV4CredentialScope.serviceName(signals.authorization());
|
||||
|
||||
@@ -55,7 +55,7 @@ import java.util.stream.Stream;
|
||||
@Path("/")
|
||||
public class AwsQueryController {
|
||||
|
||||
static final String CLOUDWATCH_GET_PATH = "/_cloudwatch-query";
|
||||
static final String QUERY_GET_PATH = "/_query";
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(AwsQueryController.class);
|
||||
|
||||
@@ -379,22 +379,10 @@ public class AwsQueryController {
|
||||
"AdminAddUserToGroup", "AdminRemoveUserFromGroup", "AdminListGroupsForUser"
|
||||
);
|
||||
|
||||
/** 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)
|
||||
@Path(QUERY_GET_PATH)
|
||||
@Produces({MediaType.APPLICATION_XML, MediaType.TEXT_HTML})
|
||||
public Response dispatchCloudWatchQueryGet(@HeaderParam("Authorization") String authorization,
|
||||
public Response dispatchQueryGet(@HeaderParam("Authorization") String authorization,
|
||||
@Context UriInfo uriInfo,
|
||||
@Context ContainerRequestContext request) {
|
||||
// Only the pre-matching Query claim may select this internal route.
|
||||
@@ -404,6 +392,11 @@ public class AwsQueryController {
|
||||
}
|
||||
MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
|
||||
String action = AwsQueryServiceResolver.action(queryParams);
|
||||
String auth = IamEnforcementFilter.requestAuthorization(authorization, queryParams);
|
||||
String service = serviceResolver.resolve(auth, action, queryParams.getFirst("Version"));
|
||||
if (!"monitoring".equals(service)) {
|
||||
return dispatch(auth, queryParams);
|
||||
}
|
||||
if (action == null) {
|
||||
return Response.status(404).type(MediaType.APPLICATION_XML)
|
||||
.entity("<UnknownOperationException/>").build();
|
||||
|
||||
@@ -11,6 +11,7 @@ import io.github.hectorvent.floci.services.dynamodb.DynamoDbTableNames;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Tag;
|
||||
import io.github.hectorvent.floci.services.iam.IamActionRegistry;
|
||||
import io.github.hectorvent.floci.services.iam.ResourceArnBuilder;
|
||||
import io.github.hectorvent.floci.services.s3.S3ObjectActions;
|
||||
import io.github.hectorvent.floci.services.s3.S3ObjectKeys;
|
||||
@@ -663,6 +664,15 @@ public class IamConditionContextResolver {
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
static Map<String, String> formParameters(ContainerRequestContext ctx) {
|
||||
if ("GET".equalsIgnoreCase(ctx.getMethod()) && IamActionRegistry.isQueryRequest(ctx)) {
|
||||
Map<String, String> parameters = new LinkedHashMap<>();
|
||||
ctx.getUriInfo().getQueryParameters().forEach((key, values) -> {
|
||||
if (!values.isEmpty()) {
|
||||
parameters.put(key, values.getFirst());
|
||||
}
|
||||
});
|
||||
return parameters;
|
||||
}
|
||||
Object cached = ctx.getProperty(BUFFERED_FORM_BODY);
|
||||
if (cached instanceof Map<?, ?> map) {
|
||||
return (Map<String, String>) map;
|
||||
|
||||
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Optional;
|
||||
@@ -18,7 +19,7 @@ import java.util.regex.Pattern;
|
||||
* 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 covers POST {@code /} with the form
|
||||
* content type and the CloudWatch GET requests the dispatcher accepts.
|
||||
* content type and the signed Query 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
|
||||
@@ -94,9 +95,12 @@ public class ProtocolClaimer {
|
||||
}
|
||||
|
||||
if ("GET".equalsIgnoreCase(signals.method()) && isRootPath(path)
|
||||
&& AwsQueryController.isCloudWatchQueryGet(signals.authorization(),
|
||||
signals.xAmzTarget(), signals.accept(), signals.queryParameters())) {
|
||||
ServiceDescriptor service = catalog.byCredentialScope("monitoring").orElse(null);
|
||||
&& isQueryGet(signals)) {
|
||||
String authorization = IamEnforcementFilter.requestAuthorization(
|
||||
signals.authorization(), signals.queryParameters());
|
||||
ServiceDescriptor service = SigV4CredentialScope.serviceName(authorization)
|
||||
.flatMap(catalog::byCredentialScope)
|
||||
.orElseGet(() -> catalog.byCredentialScope("monitoring").orElse(null));
|
||||
return Optional.of(new ProtocolClaim(WireProtocol.AWS_QUERY, service,
|
||||
AwsQueryServiceResolver.action(signals.queryParameters()), null));
|
||||
}
|
||||
@@ -146,6 +150,20 @@ public class ProtocolClaimer {
|
||||
return path != null && RPC_V2_PATH.matcher(path).matches();
|
||||
}
|
||||
|
||||
private boolean isQueryGet(ClaimSignals signals) {
|
||||
String authorization = IamEnforcementFilter.requestAuthorization(
|
||||
signals.authorization(), signals.queryParameters());
|
||||
if (authorization != null) {
|
||||
return SigV4CredentialScope.serviceName(authorization).flatMap(catalog::byCredentialScope)
|
||||
.filter(service -> service.supportsProtocol(ServiceProtocol.QUERY)).isPresent();
|
||||
}
|
||||
String action = AwsQueryServiceResolver.action(signals.queryParameters());
|
||||
return signals.xAmzTarget() == null
|
||||
&& (signals.accept() == null || !signals.accept().contains(MediaType.TEXT_HTML))
|
||||
&& action != null && "monitoring".equals(AwsQueryController.inferServiceFromAction(
|
||||
action, signals.queryParameters().getFirst("Version")));
|
||||
}
|
||||
|
||||
private static boolean isRootPath(String path) {
|
||||
return path == null || path.isEmpty() || "/".equals(path);
|
||||
}
|
||||
|
||||
@@ -45,6 +45,9 @@ public final class RequestBodyReader {
|
||||
}
|
||||
|
||||
public static String formField(ContainerRequestContext ctx, String key) {
|
||||
if ("GET".equalsIgnoreCase(ctx.getMethod()) && IamActionRegistry.isQueryRequest(ctx)) {
|
||||
return ctx.getUriInfo().getQueryParameters().getFirst(key);
|
||||
}
|
||||
MediaType mt = ctx.getMediaType();
|
||||
if (mt == null
|
||||
|| !"application".equalsIgnoreCase(mt.getType())
|
||||
|
||||
Reference in new issue
Block a user