diff --git a/docs/services/dynamodb.md b/docs/services/dynamodb.md index 6d023d42bf..2124576bf3 100644 --- a/docs/services/dynamodb.md +++ b/docs/services/dynamodb.md @@ -542,3 +542,11 @@ are not durable and are lost on restart (this is an emulator, not an at-least-on or overflow) are counted and logged, and per-destination delivery health (forwarded/retried/dropped counts, queue depth, last error, and current health) is tracked for inspection. ``` + +### Single-statement PartiQL IAM actions + +With IAM enforcement enabled, `ExecuteStatement` checks `dynamodb:PartiQLSelect`, +`dynamodb:PartiQLInsert`, `dynamodb:PartiQLUpdate` or `dynamodb:PartiQLDelete` according to the +statement's operation. A policy can grant that action on the specific table ARN; a wildcard +resource or a `dynamodb:ExecuteStatement` permission is not required. This mapping covers the +single-statement API; batch and transaction authorization are separate paths. diff --git a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java index 1abe91b86f..d054a8905e 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java @@ -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); diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLParser.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLParser.java index e9da8976d5..e178703783 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLParser.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbPartiQLParser.java @@ -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 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. diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java b/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java index 80efb79fe1..4a339757b9 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java +++ b/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java @@ -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} diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/RequestBodyReader.java b/src/main/java/io/github/hectorvent/floci/services/iam/RequestBodyReader.java index 53657dcaa8..e9ba62ec26 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iam/RequestBodyReader.java +++ b/src/main/java/io/github/hectorvent/floci/services/iam/RequestBodyReader.java @@ -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()) { diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java b/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java index 6ba09bb84e..29483ae6b9 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java +++ b/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java @@ -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:-??????} 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); diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/DynamoDbPartiQLAuthorizationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/DynamoDbPartiQLAuthorizationIntegrationTest.java new file mode 100644 index 0000000000..7735598860 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/iam/DynamoDbPartiQLAuthorizationIntegrationTest.java @@ -0,0 +1,194 @@ +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.testing.EnforcementFixtures.UserCredentials; +import io.github.hectorvent.floci.testing.IamEnforcementProfile; +import io.github.hectorvent.floci.testing.PartitionCleanup; +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.common.http.TestHTTPResource; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import io.restassured.response.ValidatableResponse; +import io.restassured.specification.RequestSpecification; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.net.URI; +import java.util.Map; +import java.util.UUID; + +import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth; +import static io.github.hectorvent.floci.testing.EnforcementFixtures.createUser; +import static io.github.hectorvent.floci.testing.EnforcementFixtures.putUserPolicy; +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@QuarkusTest +@TestProfile(IamEnforcementProfile.class) +class DynamoDbPartiQLAuthorizationIntegrationTest { + + @RegisterExtension + final PartitionCleanup cleanup = new PartitionCleanup(); + + @TestHTTPResource + URI endpoint; + + @BeforeAll + static void configureContentType() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @ParameterizedTest + @ValueSource(strings = {"Select", "Insert", "Update", "Delete"}) + void statementUsesItsPartiQLActionOnTheSpecificTable(String operation) { + String table = table(); + String key = user("dynamodb:PartiQL" + operation, arn(table)); + String statement = switch (operation) { + case "Select" -> " select * FROM \"" + table + "\" WHERE pk = 'a'"; + case "Insert" -> "INSERT INTO \"" + table + "\" VALUE {'pk':'b'}"; + case "Update" -> "UPDATE \"" + table + "\" SET data = 'new' WHERE pk = 'a'"; + case "Delete" -> "DELETE FROM \"" + table + "\" WHERE pk = 'a'"; + default -> throw new IllegalArgumentException(operation); + }; + execute(key, statement).statusCode(200); + if (!"Select".equals(operation)) { + execute(key, "SELECT * FROM \"" + table + "\"").statusCode(400) + .body(containsString("dynamodb:PartiQLSelect")); + } + } + + @Test + void tableScopedSelectCannotReadAnotherTableOrWrite() { + String table = table(); + String other = table(); + String key = user("dynamodb:PartiQLSelect", arn(table)); + execute(key, "SELECT * FROM \"" + table + "\" WHERE pk = 'a'") + .statusCode(200).body("Items[0].pk.S", equalTo("a")); + execute(key, "SELECT * FROM \"" + other + "\"").statusCode(400) + .body(containsString("AccessDeniedException")); + execute(key, "DELETE FROM \"" + table + "\" WHERE pk = 'a'").statusCode(400) + .body(containsString("dynamodb:PartiQLDelete")); + execute("test", "SELECT * FROM \"" + table + "\"") + .statusCode(200).body("Items[0].pk.S", equalTo("a")); + } + + @Test + void wireOperationPermissionDoesNotGrantPartiQLAccess() { + String table = table(); + String key = user("dynamodb:ExecuteStatement", arn(table)); + execute(key, "SELECT * FROM \"" + table + "\"").statusCode(400) + .body(containsString("dynamodb:PartiQLSelect")); + } + + @ParameterizedTest + @ValueSource(strings = {"-- only a comment", "/* only a comment */", "SELECT * FROM t /* unterminated"}) + void statementWithNoOperationIsAuthorizedAsExecuteStatement(String statement) { + String table = table(); + execute(user("dynamodb:PartiQLSelect", arn(table)), statement).statusCode(400) + .body(containsString("AccessDeniedException")) + .body(containsString("dynamodb:ExecuteStatement")); + execute(user("dynamodb:ExecuteStatement", "*"), statement).statusCode(400) + .body(containsString("ValidationException")); + } + + @ParameterizedTest + @CsvSource({"Select,target", "Insert,target", "Update,target", "Delete,target", + "Select,amz-target", "Insert,amz-target", "Update,amz-target", "Delete,amz-target", + "Select,route", "Insert,route", "Update,route", "Delete,route"}) + void binaryStatementsRequireTheirPartiQLGrant(String operation, String protocol) throws Exception { + String table = table(); + String statement = switch (operation) { + case "Select" -> "SELECT * FROM \"" + table + "\" WHERE pk = 'a'"; + case "Insert" -> "INSERT INTO \"" + table + "\" VALUE {'pk':'b'}"; + case "Update" -> "UPDATE \"" + table + "\" SET data = 'new' WHERE pk = 'a'"; + case "Delete" -> "DELETE FROM \"" + table + "\" WHERE pk = 'a'"; + default -> throw new IllegalArgumentException(operation); + }; + JsonNode denied = binary(user("dynamodb:ExecuteStatement", arn(table)), statement, protocol, 403); + assertTrue(denied.path("__type").asText().contains("AccessDeniedException"), denied.toString()); + assertTrue(denied.path("message").asText().contains("dynamodb:PartiQL" + operation), denied.toString()); + binary(user("dynamodb:PartiQL" + operation, arn(table)), statement, protocol, 200); + binary(user("dynamodb:PartiQL" + operation, arn(table())), statement, protocol, 403); + } + + @ParameterizedTest + @ValueSource(strings = {"target", "amz-target", "route"}) + void binaryDeleteHonorsAnExplicitPartiQLDeny(String protocol) throws Exception { + String table = table(); + String name = "partiql-" + UUID.randomUUID(); + UserCredentials credentials = createUser(cleanup, endpoint.getPort(), auth("test", "iam"), name); + putUserPolicy(endpoint.getPort(), auth("test", "iam"), name, "deny-delete", """ + {"Version":"2012-10-17","Statement":[ + {"Effect":"Allow","Action":"dynamodb:*","Resource":"*"}, + {"Effect":"Deny","Action":"dynamodb:PartiQLDelete","Resource":"%s"} + ]} + """.formatted(arn(table))); + JsonNode denied = binary(credentials.accessKeyId(), + "DELETE FROM \"" + table + "\" WHERE pk = 'a'", protocol, 403); + assertTrue(denied.path("__type").asText().contains("AccessDeniedException"), denied.toString()); + execute("test", "SELECT * FROM \"" + table + "\"") + .statusCode(200).body("Items[0].pk.S", equalTo("a")); + } + + private JsonNode binary(String key, String statement, String protocol, int status) throws Exception { + ObjectMapper cbor = new ObjectMapper(new CBORFactory()); + String path = "route".equals(protocol) ? "/service/DynamoDB_20120810/operation/ExecuteStatement" : "/"; + String contentType = "amz-target".equals(protocol) ? "application/x-amz-cbor-1.1" : "application/cbor"; + RequestSpecification request = given().port(endpoint.getPort()) + .header("Authorization", auth(key, "dynamodb")) + // The route dispatches ExecuteStatement even if a target header names another operation. + .header("X-Amz-Target", "DynamoDB_20120810." + ("route".equals(protocol) ? "ListTables" : "ExecuteStatement")) + .contentType(contentType).accept(contentType) + .body(cbor.writeValueAsBytes(Map.of("Statement", statement))); + if ("route".equals(protocol)) { + request.header("smithy-protocol", "rpc-v2-cbor"); + } + byte[] response = request.post(path).then().statusCode(status).extract().asByteArray(); + JsonNode body = cbor.readTree(response); + assertTrue(body.isObject()); + return body; + } + + private String arn(String table) { + return "arn:aws:dynamodb:us-east-1:000000000000:table/" + table; + } + + private String user(String action, String resource) { + String name = "partiql-" + UUID.randomUUID(); + UserCredentials credentials = createUser(cleanup, endpoint.getPort(), auth("test", "iam"), name); + putUserPolicy(endpoint.getPort(), auth("test", "iam"), name, "statement", """ + {"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"%s","Resource":"%s"}]} + """.formatted(action, resource)); + return credentials.accessKeyId(); + } + + private String table() { + String name = "partiql-" + UUID.randomUUID(); + cleanup.register(() -> json("test", "DeleteTable", Map.of("TableName", name)).statusCode(200)); + json("test", "CreateTable", Map.of("TableName", name, + "AttributeDefinitions", new Object[]{Map.of("AttributeName", "pk", "AttributeType", "S")}, + "KeySchema", new Object[]{Map.of("AttributeName", "pk", "KeyType", "HASH")}, + "BillingMode", "PAY_PER_REQUEST")).statusCode(200); + json("test", "PutItem", Map.of("TableName", name, "Item", Map.of("pk", Map.of("S", "a")))) + .statusCode(200); + return name; + } + + private ValidatableResponse execute(String key, String statement) { + return json(key, "ExecuteStatement", Map.of("Statement", statement)); + } + + private ValidatableResponse json(String key, String operation, Object body) { + return given().port(endpoint.getPort()).header("Authorization", auth(key, "dynamodb")) + .header("X-Amz-Target", "DynamoDB_20120810." + operation) + .contentType("application/x-amz-json-1.0").body(body).post("/").then(); + } +}