mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(dynamodb): authorize PartiQL statements by operation (#6168)
* fix(dynamodb): authorize PartiQL statements by operation * fix(dynamodb): authorize a statement the tokenizer refuses as ExecuteStatement * fix(dynamodb): enforce PartiQL permissions on CBOR requests * refactor(iam): share JSON and CBOR request parsing
This commit is contained in:
1 parent
1f1e5db1bc
commit
5419b3cfd1
7 files changed
+263
-24
No files matched your search
@@ -745,7 +745,7 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
}
|
||||
String servingScope = iamServiceScope(rpcV2Route.service(), claimedScope);
|
||||
return new ResolvedAuthorization(servingScope,
|
||||
IamAuthorizedActions.actionFor(servingScope, rpcV2Route.operation()));
|
||||
actionRegistry.resolveOperation(servingScope, rpcV2Route.operation(), ctx));
|
||||
}
|
||||
|
||||
ResolvedAuthorization routeAuthorization = restRouteAuthorization(ctx);
|
||||
|
||||
@@ -108,6 +108,31 @@ public class DynamoDbPartiQLParser {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The IAM action for a single statement, using the same keyword tokens as execution. A statement
|
||||
* the tokenizer refuses has no action here, so it is authorized as ExecuteStatement and the
|
||||
* handler reports why it is malformed, as {@link #extractTable} leaves it.
|
||||
*/
|
||||
public static String iamAction(String statement) {
|
||||
if (statement == null || statement.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
List<Token> tokens;
|
||||
try {
|
||||
tokens = tokenize(statement.trim());
|
||||
} catch (Exception ignored) {
|
||||
// Execution reports malformed statements after credential and policy checks have run.
|
||||
return null;
|
||||
}
|
||||
return switch (tokens.getFirst().type()) {
|
||||
case SELECT -> "dynamodb:PartiQLSelect";
|
||||
case INSERT -> "dynamodb:PartiQLInsert";
|
||||
case UPDATE -> "dynamodb:PartiQLUpdate";
|
||||
case DELETE -> "dynamodb:PartiQLDelete";
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the target table name from a PartiQL statement using the parser tokenizer,
|
||||
* ensuring quoted attribute names containing SQL keywords are not mistakenly treated as table names.
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
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 io.github.hectorvent.floci.services.dynamodb.DynamoDbPartiQLParser;
|
||||
import io.github.hectorvent.floci.services.s3.S3ObjectActions;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
@@ -28,6 +30,7 @@ import java.util.regex.Pattern;
|
||||
public class IamActionRegistry {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(IamActionRegistry.class);
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
/**
|
||||
* A route and the action it is checked as. A rule naming a query parameter matches only a request
|
||||
@@ -441,12 +444,24 @@ public class IamActionRegistry {
|
||||
String target = ctx.getHeaderString("X-Amz-Target");
|
||||
if (target != null && target.contains(".")) {
|
||||
String operationName = target.substring(target.lastIndexOf('.') + 1);
|
||||
return IamAuthorizedActions.actionFor(credentialScope, operationName);
|
||||
return resolveOperation(credentialScope, operationName, ctx);
|
||||
}
|
||||
|
||||
return resolveRoute(credentialScope, ctx);
|
||||
}
|
||||
|
||||
/** The action for a dispatched operation, including statement-dependent JSON and CBOR calls. */
|
||||
public String resolveOperation(String credentialScope, String operationName, ContainerRequestContext ctx) {
|
||||
if ("dynamodb".equals(credentialScope) && "ExecuteStatement".equals(operationName)) {
|
||||
String statement = RequestBodyReader.documentField(ctx, MAPPER, "Statement");
|
||||
String statementAction = DynamoDbPartiQLParser.iamAction(statement);
|
||||
if (statementAction != null) {
|
||||
return statementAction;
|
||||
}
|
||||
}
|
||||
return IamAuthorizedActions.actionFor(credentialScope, operationName);
|
||||
}
|
||||
|
||||
/**
|
||||
* The action of a REST request, from its method and path alone. A REST request is the operation
|
||||
* whose route it reached, so neither an {@code X-Amz-Target} header nor a Query {@code Action}
|
||||
|
||||
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import org.jboss.logging.Logger;
|
||||
@@ -16,6 +17,7 @@ import java.nio.charset.StandardCharsets;
|
||||
public final class RequestBodyReader {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(RequestBodyReader.class);
|
||||
private static final ObjectMapper CBOR_MAPPER = new ObjectMapper(new CBORFactory());
|
||||
private static final String BUFFERED_BODY_PROPERTY =
|
||||
"io.github.hectorvent.floci.services.iam.RequestBodyReader.bufferedBody";
|
||||
|
||||
@@ -71,10 +73,10 @@ public final class RequestBodyReader {
|
||||
return null;
|
||||
}
|
||||
|
||||
static String jsonField(ContainerRequestContext ctx, ObjectMapper objectMapper, String key) {
|
||||
static String documentField(ContainerRequestContext ctx, ObjectMapper objectMapper, String key) {
|
||||
MediaType mt = ctx.getMediaType();
|
||||
if (mt == null || !"application".equalsIgnoreCase(mt.getType())
|
||||
|| mt.getSubtype() == null || !mt.getSubtype().startsWith("x-amz-json")) {
|
||||
|| mt.getSubtype() == null || (!isCbor(mt) && !mt.getSubtype().startsWith("x-amz-json"))) {
|
||||
return null;
|
||||
}
|
||||
byte[] body = buffer(ctx);
|
||||
@@ -82,14 +84,26 @@ public final class RequestBodyReader {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
JsonNode value = objectMapper.readTree(body).path(key);
|
||||
JsonNode value = readTree(body, mt, objectMapper).path(key);
|
||||
return value.isTextual() ? value.asText() : null;
|
||||
} catch (IOException e) {
|
||||
LOG.debugv(e, "Failed to parse JSON request body for IAM enforcement");
|
||||
LOG.debugv(e, "Failed to parse request document for IAM enforcement");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Both CBOR wire protocols carry the same document members as JSON requests. */
|
||||
static boolean isCbor(MediaType mediaType) {
|
||||
return mediaType != null
|
||||
&& "application".equalsIgnoreCase(mediaType.getType())
|
||||
&& ("cbor".equalsIgnoreCase(mediaType.getSubtype())
|
||||
|| "x-amz-cbor-1.1".equalsIgnoreCase(mediaType.getSubtype()));
|
||||
}
|
||||
|
||||
static JsonNode readTree(byte[] body, MediaType mediaType, ObjectMapper json) throws IOException {
|
||||
return (isCbor(mediaType) ? CBOR_MAPPER : json).readTree(body);
|
||||
}
|
||||
|
||||
private static Charset resolveCharset(MediaType mt) {
|
||||
String name = mt.getParameters().get("charset");
|
||||
if (name == null || name.isBlank()) {
|
||||
|
||||
@@ -2,7 +2,6 @@ package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.AwsQueryServiceResolver;
|
||||
@@ -18,7 +17,6 @@ import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
@@ -51,9 +49,6 @@ public class ResourceArnBuilder {
|
||||
/** What a service-linked role deletion task identifier starts with; see its resolver. */
|
||||
private static final String DELETION_TASK_PREFIX = "task" + ServiceLinkedRoles.PATH;
|
||||
|
||||
/** Reads the CBOR bodies the RPC v2 and {@code x-amz-cbor-1.1} paths carry. */
|
||||
private static final ObjectMapper CBOR_MAPPER = new ObjectMapper(new CBORFactory());
|
||||
|
||||
/**
|
||||
* Stands in for the random suffix of a secret that {@code CreateSecret} has not made yet. Each
|
||||
* {@code ?} matches the one character a {@code secret:<name>-??????} policy expects there.
|
||||
@@ -1408,7 +1403,7 @@ public class ResourceArnBuilder {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
JsonNode node = (isCbor(ctx.getMediaType()) ? CBOR_MAPPER : objectMapper).readTree(body);
|
||||
JsonNode node = RequestBodyReader.readTree(body, ctx.getMediaType(), objectMapper);
|
||||
ctx.setProperty("floci.bufferedJsonBody", node);
|
||||
return node;
|
||||
} catch (Exception e) {
|
||||
@@ -1416,18 +1411,6 @@ public class ResourceArnBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A CBOR body, which names its resources in the same members a JSON body does. Both CBOR
|
||||
* content types arrive as {@code application/cbor}: {@code AwsCborContentTypeFilter} rewrites
|
||||
* {@code x-amz-cbor-1.1} before routing.
|
||||
*/
|
||||
private static boolean isCbor(MediaType mediaType) {
|
||||
return mediaType != null
|
||||
&& "application".equalsIgnoreCase(mediaType.getType())
|
||||
&& ("cbor".equalsIgnoreCase(mediaType.getSubtype())
|
||||
|| "x-amz-cbor-1.1".equalsIgnoreCase(mediaType.getSubtype()));
|
||||
}
|
||||
|
||||
private String extractSegmentAfter(String path, String segment) {
|
||||
String marker = "/" + segment + "/";
|
||||
int idx = path.indexOf(marker);
|
||||
|
||||
Reference in new issue
Block a user