Light, fluffy, and always free - The AWS Local Emulator alternative
fix(dynamodb): support legacy table discovery and batch cleanup (#5986)
Andre Kurait0a37ea830baf
diff --git a/compatibility-tests/sdk-test-python/README.md b/compatibility-tests/sdk-test-python/README.mdindex b98b0d7fd6..a1c7815e82 100644--- a/compatibility-tests/sdk-test-python/README.md+++ b/compatibility-tests/sdk-test-python/README.md@@ -44,6 +44,20 @@ pytest tests/test_s3.pyjust test-python```+## Legacy DynamoDB client++`legacy/test_dynamodb_20111205.py` exercises the 2011-12-05 API with Boto 2,+including paginated queries and batch cleanup of tables created by Boto 3.+Public Boto 2.49.0 needs Python 3.9: it uses retired collections and import+interfaces. Run this suite in a separate environment; the ordinary Python+3.12 suite retains its existing dependencies.++```bash+python3.9 -m venv .venv-legacy+.venv-legacy/bin/pip install -r requirements-legacy.txt+FLOCI_ENDPOINT=http://localhost:4566 .venv-legacy/bin/python -m pytest legacy/+```+## Configuration| Variable | Default | Description |diff --git a/compatibility-tests/sdk-test-python/legacy/test_dynamodb_20111205.py b/compatibility-tests/sdk-test-python/legacy/test_dynamodb_20111205.pynew file mode 100644index 0000000000..f0578fac57--- /dev/null+++ b/compatibility-tests/sdk-test-python/legacy/test_dynamodb_20111205.py@@ -0,0 +1,109 @@+"""Explicit legacy-client suite: Boto 2 and Boto 3 share DynamoDB table storage."""++from urllib.parse import urlsplit+import uuid++from boto.dynamodb.layer2 import Layer2+from boto.dynamodb.condition import BETWEEN+from boto.regioninfo import RegionInfo+from boto.exception import DynamoDBResponseError+import pytest+++def legacy_client(aws_config, region=None, access_key=None):+ endpoint = urlsplit(aws_config["endpoint_url"])+ client = Layer2(+ aws_access_key_id=access_key or aws_config["aws_access_key_id"],+ aws_secret_access_key=aws_config["aws_secret_access_key"],+ port=endpoint.port,+ is_secure=endpoint.scheme == "https",+ region=RegionInfo(name=region or aws_config["region_name"], endpoint=endpoint.hostname),+ )+ # Boto 2 otherwise infers signing scope from the endpoint's DNS labels.+ client.layer1.auth_region_name = region or aws_config["region_name"]+ client.layer1.auth_service_name = "dynamodb"+ return client+++def metrics_table(dynamodb_client):+ name = "legacy-metrics-" + uuid.uuid4().hex+ dynamodb_client.create_table(+ TableName=name,+ KeySchema=[{"AttributeName": "id-type", "KeyType": "HASH"}, {"AttributeName": "range", "KeyType": "RANGE"}],+ AttributeDefinitions=[+ {"AttributeName": "id-type", "AttributeType": "S"},+ {"AttributeName": "range", "AttributeType": "N"},+ ],+ BillingMode="PAY_PER_REQUEST",+ )+ dynamodb_client.get_waiter("table_exists").wait(TableName=name)+ try:+ for partition in ("deleted-domain_q", "other-domain_q"):+ for index in range(4):+ dynamodb_client.put_item(+ TableName=name,+ Item={"id-type": {"S": partition}, "range": {"N": str(index)}, "payload": {"S": "keep"}},+ )+ yield name+ finally:+ dynamodb_client.delete_table(TableName=name)+++def test_missing_table_raises_native_handled_error(aws_config):+ client = legacy_client(aws_config)+ with pytest.raises(DynamoDBResponseError) as error:+ client.get_table("missing-" + uuid.uuid4().hex)+ assert error.value.status == 400+ assert "ResourceNotFoundException" in error.value.body["__type"]+++def test_paginated_cleanup_preserves_other_partitions(aws_config, dynamodb_client, metrics_table):+ client = legacy_client(aws_config)+ table = client.get_table(metrics_table)+ assert table.name == metrics_table+ assert table.schema.hash_key_name == "id-type"+ assert table.schema.range_key_name == "range"+ assert table.schema.range_key_type == "N"+ rows = table.query(+ hash_key="deleted-domain_q",+ range_key_condition=BETWEEN(0, 3),+ attributes_to_get=["id-type", "range"],+ consistent_read=True,+ scan_index_forward=False,+ request_limit=1,+ )+ items = list(rows)+ assert [int(item["range"]) for item in items] == [3, 2, 1, 0]+ assert all(set(item) == {"id-type", "range"} for item in items)+ assert rows.consumed_units > 0+ batch = client.new_batch_write_list()+ batch.add_batch(table, deletes=[(item["id-type"], item["range"]) for item in items])+ response = client.layer1.batch_write_item(batch.to_dict())+ assert response["UnprocessedItems"] == {}+ assert response["Responses"][metrics_table]["ConsumedCapacityUnits"] > 0+ assert list(table.query(hash_key="deleted-domain_q", consistent_read=True)) == []+ count = table.query(hash_key="other-domain_q", count=True)+ assert count.count == 4+ remaining = dynamodb_client.scan(TableName=metrics_table, ConsistentRead=True)["Items"]+ assert len(remaining) == 4+ assert {item["id-type"]["S"] for item in remaining} == {"other-domain_q"}+ assert dynamodb_client.describe_table(TableName=metrics_table)["Table"]["KeySchema"] == [+ {"AttributeName": "id-type", "KeyType": "HASH"}, {"AttributeName": "range", "KeyType": "RANGE"}+ ]+++def test_region_and_account_scoping_and_bad_keys(aws_config, dynamodb_client, metrics_table):+ other_region = "eu-west-1" if aws_config["region_name"] != "eu-west-1" else "us-west-2"+ for client in (legacy_client(aws_config, region=other_region),+ legacy_client(aws_config, access_key="999988887777")):+ with pytest.raises(DynamoDBResponseError) as error:+ client.get_table(metrics_table)+ assert "ResourceNotFoundException" in error.value.body["__type"]+ client = legacy_client(aws_config)+ with pytest.raises(DynamoDBResponseError) as error:+ client.layer1.batch_write_item({+ metrics_table: [{"DeleteRequest": {"Key": {"HashKeyElement": {"S": "deleted-domain_q"}}}}],+ })+ assert "ValidationException" in error.value.body["__type"]+ assert dynamodb_client.scan(TableName=metrics_table)["Count"] == 8diff --git a/compatibility-tests/sdk-test-python/requirements-legacy.txt b/compatibility-tests/sdk-test-python/requirements-legacy.txtnew file mode 100644index 0000000000..466d403a98--- /dev/null+++ b/compatibility-tests/sdk-test-python/requirements-legacy.txt@@ -0,0 +1,6 @@+boto==2.49.0+boto3==1.37.1+botocore==1.37.1+cryptography>=42.0.0+pytest>=8.0.0,<9.0.0+pytest-timeout>=2.3.0diff --git a/docs/services/dynamodb.md b/docs/services/dynamodb.mdindex 1531c71ee0..a92e5b97dd 100644--- a/docs/services/dynamodb.md+++ b/docs/services/dynamodb.md@@ -1,6 +1,6 @@# DynamoDB-**Protocol:** JSON 1.1 (`X-Amz-Target: DynamoDB_20120810.*`)+**Protocol:** JSON 1.0 (`X-Amz-Target: DynamoDB_20120810.*`)**Endpoint:** `POST http://localhost:4566/`## Supported Actions@@ -44,6 +44,24 @@| `DescribeImport` | Get import status and metadata || `ListImports` | List imports, optionally filtered by table ARN |+## Legacy 2011-12-05 clients++The JSON 1.0 target `DynamoDB_20111205` supports `DescribeTable`, `Query` and+`BatchWriteItem`. These operations use the selected DynamoDB backend and the+caller's account and region. Table schemas, query conditions and continuation+keys, batch delete keys, unprocessed writes and consumed capacity are translated+between the two API versions. Missing tables return `ResourceNotFoundException`.+Other operations under this legacy target return `UnknownOperationException`;+the modern target remains the full API.++The explicit Boto 2 suite in `compatibility-tests/sdk-test-python/legacy/`+exercises table discovery, paginated key queries and+batch deletion against the same tables used by Boto 3. The wire contracts follow+the AWS [2011-12-05 DescribeTable](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/API_DescribeTables_v20111205.html),+[Query](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/API_Query_v20111205.html)+and [BatchWriteItem](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/API_BatchWriteItem_v20111205.html)+documentation.+## Legacy conditional requestsThe native backend accepts `KeyConditions` together with `FilterExpression`.diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonCborController.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonCborController.javaindex 74db04543b..ce63d591d6 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonCborController.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonCborController.java@@ -396,7 +396,7 @@ public class AwsJsonCborController {// dispatch is an unknown operation, not an empty 204. The 204 fallback now applies// only to a resolved service whose action this handler does not dispatch.ServiceCatalog.TargetMatch targetMatch = catalog.matchTarget(target).orElse(null);- if (targetMatch == null) {+ if (targetMatch == null || targetMatch.jsonOnly()) {return cborUnknownOperationError("Unknown operation: " + target, httpHeaders);}diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.javaindex 8398ebf400..aa4ab70f07 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.java@@ -160,6 +160,9 @@ public class AwsJsonController {if (targetMatch.prefix().startsWith("DynamoDBStreams_")) {yield dynamoDbStreamsJsonHandler.handle(action, request, region);}+ if (targetMatch.jsonOnly()) {+ yield dynamoDbJsonHandler.handleLegacy(action, request, region);+ }DynamoDbRequestShapes.check(action, request);yield dynamoDbJsonHandler.handle(action, request, region);}@@ -197,6 +200,9 @@ public class AwsJsonController {// boundary so other callers of DynamoDbJsonHandler (CBOR, API Gateway proxy,// Step Functions tasks) keep their original ObjectNode entity.if ("dynamodb".equals(serviceKey)) {+ if (targetMatch.jsonOnly()) {+ response = DynamoDbResponses.withLegacyErrorNamespace(response, objectMapper);+ }return DynamoDbResponses.withCrc32(response, objectMapper);}return response;diff --git a/src/main/java/io/github/hectorvent/floci/core/common/IamConditionContextResolver.java b/src/main/java/io/github/hectorvent/floci/core/common/IamConditionContextResolver.javaindex d4cf09f472..680b0eb9ca 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/IamConditionContextResolver.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/IamConditionContextResolver.java@@ -90,6 +90,7 @@ public class IamConditionContextResolver {private final EmulatorConfig config;private final CurrentVertxRequest currentVertxRequest;private final Clock clock;+ private final ResolvedServiceCatalog serviceCatalog;/*** The system clock, not the CDI one: Object Lock's remaining retention days has to count from the@@ -102,9 +103,10 @@ public class IamConditionContextResolver {Instance<CloudWatchLogsService> logsService,RequestContext requestContext,EmulatorConfig config,- CurrentVertxRequest currentVertxRequest) {+ CurrentVertxRequest currentVertxRequest,+ ResolvedServiceCatalog serviceCatalog) {this(dynamoDbFacade, ec2Service, s3Service, logsService, requestContext, config, currentVertxRequest,- Clock.systemUTC());+ serviceCatalog, Clock.systemUTC());}IamConditionContextResolver(Instance<DynamoDbFacade> dynamoDbFacade,@@ -114,7 +116,7 @@ public class IamConditionContextResolver {RequestContext requestContext,EmulatorConfig config,CurrentVertxRequest currentVertxRequest,- Clock clock) {+ ResolvedServiceCatalog serviceCatalog, Clock clock) {this.dynamoDbFacade = dynamoDbFacade;this.ec2Service = ec2Service;this.s3Service = s3Service;@@ -123,6 +125,7 @@ public class IamConditionContextResolver {this.config = config;this.currentVertxRequest = currentVertxRequest;this.clock = clock;+ this.serviceCatalog = serviceCatalog;}public Map<String, List<String>> resolve(String credentialScope, String action,@@ -739,7 +742,7 @@ public class IamConditionContextResolver {return dynamoDbBatchConditionContext(action, ctx, null);}TableDefinition table = describeTargetTable(targetTableName(body));- DynamoDbConditionKeys.Result keys = DynamoDbConditionKeys.extract(action, body, table);+ DynamoDbConditionKeys.Result keys = dynamoDbKeys(action, body, table, ctx);Map<String, List<String>> conditions = new LinkedHashMap<>();if (!keys.leadingKeys().isEmpty()) {@@ -850,8 +853,8 @@ public class IamConditionContextResolver {ObjectNode single = ((ObjectNode) body).objectNode();single.setAll((ObjectNode) body);single.putObject("RequestItems").set(entry.getKey(), entry.getValue());- DynamoDbConditionKeys.Result keys = DynamoDbConditionKeys.extract(- action, single, describeTargetTable(reference));+ DynamoDbConditionKeys.Result keys = dynamoDbKeys(+ action, single, describeTargetTable(reference), ctx);leadingKeys.addAll(keys.leadingKeys());attributes.addAll(keys.attributes());});@@ -880,6 +883,21 @@ public class IamConditionContextResolver {return cached instanceof JsonNode node ? node : null;}+ private DynamoDbConditionKeys.Result dynamoDbKeys(+ String action, JsonNode body, TableDefinition table, ContainerRequestContext ctx) {+ String target = ctx.getHeaderString("X-Amz-Target");+ boolean legacy = action != null && action.startsWith("dynamodb:")+ && serviceCatalog.matchTarget(target)+ .filter(ServiceCatalog.TargetMatch::jsonOnly)+ .filter(match -> "dynamodb".equals(match.descriptor().externalKey()))+ .filter(match -> match.action().equals(action.substring("dynamodb:".length())))+ .isPresent()+ && ctx.getMediaType() != null+ && ctx.getMediaType().isCompatible(MediaType.valueOf("application/x-amz-json-1.0"));+ return legacy ? DynamoDbConditionKeys.extractLegacy(action, body, table)+ : DynamoDbConditionKeys.extract(action, body, table);+ }+/*** Looks up the table whose key schema names the partition key. Resolved lazily through* Instance so core.common keeps no hard dependency on the DynamoDB facade, and viadiff --git a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.javaindex abe1a2de7f..eb760dfd79 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java@@ -88,6 +88,7 @@ import jakarta.inject.Inject;import java.util.Arrays;import java.util.EnumSet;+import java.util.HashSet;import java.util.List;import java.util.Optional;import java.util.Set;@@ -135,7 +136,8 @@ public class ResolvedServiceCatalog {config.storage().services().dynamodb().flushIntervalMs(), null, ServiceProtocol.JSON,protocols(ServiceProtocol.JSON, ServiceProtocol.CBOR),Set.of("DynamoDB_20120810.", "DynamoDBStreams_20120810."),- Set.of("dynamodb"), Set.of("DynamoDB", "DynamoDB Streams"), Set.of()),+ Set.of("dynamodb"), Set.of("DynamoDB", "DynamoDB Streams"), Set.of(),+ "DynamoDB_20111205."),descriptor("sns", "sns", config.services().sns().enabled(), true,"sns", storageMode(config.storage().services().sns().mode(), config.storage().mode()),config.storage().services().sns().flushIntervalMs(), AwsNamespaces.SNS, ServiceProtocol.QUERY,@@ -850,8 +852,11 @@ public class ResolvedServiceCatalog {Set<String> targetPrefixes,Set<String> credentialScopes,Set<String> cborSdkServiceIds,- Set<Class<?>> resourceClasses+ Set<Class<?>> resourceClasses,+ String... jsonOnlyTargetPrefixes) {+ Set<String> resolvedTargetPrefixes = new HashSet<>(targetPrefixes);+ resolvedTargetPrefixes.addAll(Set.of(jsonOnlyTargetPrefixes));return new ServiceDescriptor(externalKey,configKey,@@ -863,10 +868,11 @@ public class ResolvedServiceCatalog {xmlNamespace,defaultProtocol,Set.copyOf(supportedProtocols),- Set.copyOf(targetPrefixes),+ Set.copyOf(resolvedTargetPrefixes),Set.copyOf(credentialScopes),Set.copyOf(cborSdkServiceIds),- Set.copyOf(resourceClasses)+ Set.copyOf(resourceClasses),+ Set.of(jsonOnlyTargetPrefixes));}diff --git a/src/main/java/io/github/hectorvent/floci/core/common/ServiceCatalog.java b/src/main/java/io/github/hectorvent/floci/core/common/ServiceCatalog.javaindex ebefd453e9..2ae957dae6 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/ServiceCatalog.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/ServiceCatalog.java@@ -56,7 +56,9 @@ public class ServiceCatalog {String rpcV2ServiceName = prefix.endsWith(".")? prefix.substring(0, prefix.length() - 1): prefix;- cborSdkServiceIds.putIfAbsent(rpcV2ServiceName, descriptor);+ if (!descriptor.jsonOnlyTargetPrefixes().contains(prefix)) {+ cborSdkServiceIds.putIfAbsent(rpcV2ServiceName, descriptor);+ }}}targets.sort(Comparator.comparingInt((Map.Entry<String, ServiceDescriptor> entry) -> entry.getKey().length())@@ -126,5 +128,8 @@ public class ServiceCatalog {}public record TargetMatch(ServiceDescriptor descriptor, String prefix, String action) {+ public boolean jsonOnly() {+ return descriptor.jsonOnlyTargetPrefixes().contains(prefix);+ }}}diff --git a/src/main/java/io/github/hectorvent/floci/core/common/ServiceDescriptor.java b/src/main/java/io/github/hectorvent/floci/core/common/ServiceDescriptor.javaindex 2d188ec045..e504d445c0 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/ServiceDescriptor.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/ServiceDescriptor.java@@ -16,7 +16,8 @@ public record ServiceDescriptor(Set<String> targetPrefixes,Set<String> credentialScopes,Set<String> cborSdkServiceIds,- Set<Class<?>> resourceClasses+ Set<Class<?>> resourceClasses,+ Set<String> jsonOnlyTargetPrefixes) {public boolean supportsStorage() {diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbConditionKeys.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbConditionKeys.javaindex 01af5e36c9..a3f6ca8205 100644--- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbConditionKeys.java+++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbConditionKeys.java@@ -138,6 +138,56 @@ public final class DynamoDbConditionKeys {textOrNull(body.get("Select")));}+ /** Condition keys for the separately routed 2011-12-05 JSON requests. */+ public static Result extractLegacy(String action, JsonNode body, TableDefinition table) {+ if (body == null || !body.isObject()) {+ return new Result(List.of(), List.of(), null);+ }+ String pkName = partitionKeyName(table);+ List<String> leadingKeys = new ArrayList<>();+ Set<String> attributes = new LinkedHashSet<>();+ if ("dynamodb:Query".equals(action)) {+ addLegacyKey(leadingKeys, attributes, body.get("HashKeyValue"), pkName);+ if (body.hasNonNull("RangeKeyCondition") && table != null && table.getKeySchema() != null) {+ table.getKeySchema().stream().filter(key -> "RANGE".equals(key.getKeyType()))+ .map(KeySchemaElement::getAttributeName).forEach(attributes::add);+ }+ addAttributesToGet(attributes, body.get("AttributesToGet"));+ } else if ("dynamodb:BatchWriteItem".equals(action)) {+ for (JsonNode tableRequest : requestItemsFor(body, table)) {+ if (!tableRequest.isArray()) {+ continue;+ }+ for (JsonNode write : tableRequest) {+ JsonNode put = write.path("PutRequest").get("Item");+ addAttributeNames(attributes, put);+ addLeadingKey(leadingKeys, put, pkName);+ JsonNode key = write.path("DeleteRequest").path("Key");+ addLegacyKey(leadingKeys, attributes, key.get("HashKeyElement"), pkName);+ if (key.hasNonNull("RangeKeyElement") && table != null && table.getKeySchema() != null) {+ table.getKeySchema().stream().filter(element -> "RANGE".equals(element.getKeyType()))+ .map(KeySchemaElement::getAttributeName).forEach(attributes::add);+ }+ }+ }+ }+ return new Result(List.copyOf(leadingKeys), List.copyOf(attributes),+ "dynamodb:Query".equals(action) && body.path("Count").isBoolean()+ && body.path("Count").booleanValue() ? "COUNT" : null);+ }++ private static void addLegacyKey(List<String> leadingKeys, Set<String> attributes, JsonNode value, String name) {+ if (value != null) {+ if (name != null) {+ attributes.add(name);+ }+ String scalar = scalarValue(value);+ if (scalar != null) {+ leadingKeys.add(scalar);+ }+ }+ }+/*** The extracted keys. {@code leadingKeys} holds partition-key values in request order* (duplicates preserved), {@code attributes} holds attribute names in first-seen order@@ -395,4 +445,4 @@ public final class DynamoDbConditionKeys {String text = node.asText();return text.isEmpty() ? null : text;}-}\ No newline at end of file+}diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.javaindex 3f30b316a0..c06baf8953 100644--- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandler.java@@ -37,6 +37,12 @@ public class DynamoDbJsonHandler {return toResponse(operations.execute(new Call(scope, Api.DYNAMODB, action, request)));}+ public Response handleLegacy(String action, JsonNode request, String region) throws Exception {+ Scope scope = new Scope(regionResolver.getAccountId(),+ region != null ? region : regionResolver.getDefaultRegion());+ return toResponse(new DynamoDbLegacyProtocol(operations, scope).execute(action, request));+ }+static Response toResponse(Reply reply) {Response.ResponseBuilder builder = Response.status(reply.status()).entity(reply.body());for (Map.Entry<String, List<String>> header : reply.headers().entrySet()) {diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbLegacyProtocol.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbLegacyProtocol.javanew file mode 100644index 0000000000..42ac48ba77--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbLegacyProtocol.java@@ -0,0 +1,249 @@+package io.github.hectorvent.floci.services.dynamodb;++import com.fasterxml.jackson.databind.JsonNode;+import com.fasterxml.jackson.databind.node.ArrayNode;+import com.fasterxml.jackson.databind.node.JsonNodeFactory;+import com.fasterxml.jackson.databind.node.ObjectNode;+import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations;+import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations.Api;+import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations.Call;+import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations.Reply;+import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations.Scope;++import java.util.HashMap;+import java.util.Iterator;+import java.util.Map;++/** The 2011-12-05 wire shapes for table discovery, queries and batch writes. */+final class DynamoDbLegacyProtocol {++ private final DynamoDbOperations operations;+ private final Scope scope;+ private final Map<String, JsonNode> tables = new HashMap<>();++ DynamoDbLegacyProtocol(DynamoDbOperations operations, Scope scope) {+ this.operations = operations;+ this.scope = scope;+ }++ Reply execute(String action, JsonNode request) throws Exception {+ return switch (action) {+ case "DescribeTable" -> describe(request);+ case "Query" -> query(request);+ case "BatchWriteItem" -> batchWrite(request);+ default -> throw new AwsException("UnknownOperationException",+ "Unknown operation: DynamoDB_20111205." + action, 400);+ };+ }++ private Reply describe(JsonNode request) throws Exception {+ Reply reply = call("DescribeTable", request);+ if (reply.status() >= 300) {+ return reply;+ }+ ObjectNode table = fields(reply.body().path("Table"), "TableName", "TableStatus", "CreationDateTime",+ "ItemCount", "TableSizeBytes", "ProvisionedThroughput");+ table.set("KeySchema", legacySchema(reply.body().path("Table")));+ ObjectNode body = object();+ body.set("Table", table);+ return body(reply, body);+ }++ private Reply query(JsonNode request) throws Exception {+ String tableName = tableName(request);+ JsonNode table = table(tableName);+ String hashName = keyName(table, "HASH");+ String rangeName = keyName(table, "RANGE");+ if (rangeName == null) {+ throw invalid("Query requires a table with a hash and range key");+ }+ ObjectNode converted = fields(request, "TableName", "AttributesToGet", "Limit",+ "ConsistentRead", "ScanIndexForward");+ ObjectNode conditions = object();+ ObjectNode hashCondition = object().put("ComparisonOperator", "EQ");+ hashCondition.putArray("AttributeValueList").add(requiredObject(request, "HashKeyValue"));+ conditions.set(hashName, hashCondition);+ if (request.hasNonNull("RangeKeyCondition")) {+ conditions.set(rangeName, requiredObject(request, "RangeKeyCondition"));+ }+ converted.set("KeyConditions", conditions);+ if (request.hasNonNull("ExclusiveStartKey")) {+ converted.set("ExclusiveStartKey", modernKey(request.get("ExclusiveStartKey"), table));+ }+ if (request.hasNonNull("Count")) {+ if (!request.get("Count").isBoolean()) {+ throw invalid("Count must be a boolean");+ }+ if (request.get("Count").booleanValue()) {+ converted.put("Select", "COUNT");+ }+ }+ converted.put("ReturnConsumedCapacity", "TOTAL");+ Reply reply = call("Query", converted);+ if (reply.status() >= 300) {+ return reply;+ }+ ObjectNode result = fields(reply.body(), "Items", "Count");+ JsonNode lastKey = reply.body().get("LastEvaluatedKey");+ if (lastKey != null && !lastKey.isEmpty()) {+ result.set("LastEvaluatedKey", legacyKey(lastKey, table));+ }+ JsonNode capacity = reply.body().path("ConsumedCapacity").get("CapacityUnits");+ if (capacity != null) {+ result.set("ConsumedCapacityUnits", capacity);+ }+ return body(reply, result);+ }++ private Reply batchWrite(JsonNode request) throws Exception {+ ObjectNode converted = object();+ converted.set("RequestItems", writes(requiredObject(request, "RequestItems"), false));+ converted.put("ReturnConsumedCapacity", "TOTAL");+ Reply reply = call("BatchWriteItem", converted);+ if (reply.status() >= 300) {+ return reply;+ }+ ObjectNode result = object();+ ObjectNode capacities = object();+ for (JsonNode capacity : reply.body().path("ConsumedCapacity")) {+ ObjectNode units = object();+ units.set("ConsumedCapacityUnits", capacity.get("CapacityUnits"));+ capacities.set(capacity.path("TableName").asText(), units);+ }+ result.set("Responses", capacities);+ JsonNode unprocessed = reply.body().get("UnprocessedItems");+ if (unprocessed != null) {+ result.set("UnprocessedItems", writes(unprocessed, true));+ }+ return body(reply, result);+ }++ private ObjectNode writes(JsonNode requestItems, boolean legacy) throws Exception {+ ObjectNode result = object();+ Iterator<Map.Entry<String, JsonNode>> entries = requestItems.fields();+ while (entries.hasNext()) {+ Map.Entry<String, JsonNode> entry = entries.next();+ if (!entry.getValue().isArray()) {+ throw invalid("RequestItems values must be arrays");+ }+ JsonNode table = table(entry.getKey());+ ArrayNode writes = result.putArray(entry.getKey());+ for (JsonNode write : entry.getValue()) {+ if (!write.isObject() || write.hasNonNull("PutRequest") == write.hasNonNull("DeleteRequest")) {+ throw invalid("A write must contain exactly one PutRequest or DeleteRequest");+ }+ ObjectNode converted = object();+ if (write.hasNonNull("DeleteRequest")) {+ JsonNode key = requiredObject(requiredObject(write, "DeleteRequest"), "Key");+ ObjectNode delete = object();+ delete.set("Key", legacy ? legacyKey(key, table) : modernKey(key, table));+ converted.set("DeleteRequest", delete);+ } else {+ ObjectNode put = object();+ put.set("Item", requiredObject(requiredObject(write, "PutRequest"), "Item"));+ converted.set("PutRequest", put);+ }+ writes.add(converted);+ }+ }+ return result;+ }++ private JsonNode table(String name) throws Exception {+ JsonNode table = tables.get(name);+ if (table == null) {+ table = call("DescribeTable", object().put("TableName", name)).successBody().path("Table");+ tables.put(name, table);+ }+ return table;+ }++ private Reply call(String action, JsonNode request) throws Exception {+ DynamoDbRequestShapes.check(action, request);+ return operations.execute(new Call(scope, Api.DYNAMODB, action, request));+ }++ private static ObjectNode modernKey(JsonNode key, JsonNode table) {+ ObjectNode converted = object();+ converted.set(keyName(table, "HASH"), requiredObject(key, "HashKeyElement"));+ String rangeName = keyName(table, "RANGE");+ if (rangeName != null) {+ converted.set(rangeName, requiredObject(key, "RangeKeyElement"));+ } else if (key.hasNonNull("RangeKeyElement")) {+ throw invalid("The provided key does not match the table schema");+ }+ return converted;+ }++ private static ObjectNode legacyKey(JsonNode key, JsonNode table) {+ ObjectNode converted = object();+ converted.set("HashKeyElement", key.get(keyName(table, "HASH")));+ String rangeName = keyName(table, "RANGE");+ if (rangeName != null) {+ converted.set("RangeKeyElement", key.get(rangeName));+ }+ return converted;+ }++ private static ObjectNode legacySchema(JsonNode table) {+ ObjectNode result = object();+ for (JsonNode key : table.path("KeySchema")) {+ String name = key.path("AttributeName").asText();+ for (JsonNode definition : table.path("AttributeDefinitions")) {+ if (name.equals(definition.path("AttributeName").asText())) {+ result.set("HASH".equals(key.path("KeyType").asText()) ? "HashKeyElement" : "RangeKeyElement",+ fields(definition, "AttributeName", "AttributeType"));+ }+ }+ }+ return result;+ }++ private static String keyName(JsonNode table, String type) {+ for (JsonNode key : table.path("KeySchema")) {+ if (type.equals(key.path("KeyType").asText())) {+ return key.path("AttributeName").asText();+ }+ }+ return null;+ }++ private static String tableName(JsonNode request) {+ JsonNode value = request.get("TableName");+ if (value == null || !value.isTextual() || value.asText().isBlank()) {+ throw invalid("TableName is required");+ }+ return value.asText();+ }++ private static JsonNode requiredObject(JsonNode parent, String name) {+ JsonNode value = parent.get(name);+ if (value == null || !value.isObject()) {+ throw invalid(name + " must be an object");+ }+ return value;+ }++ private static ObjectNode fields(JsonNode source, String... names) {+ ObjectNode result = object();+ for (String name : names) {+ if (source.hasNonNull(name)) {+ result.set(name, source.get(name).deepCopy());+ }+ }+ return result;+ }++ private static ObjectNode object() {+ return JsonNodeFactory.instance.objectNode();+ }++ private static Reply body(Reply reply, ObjectNode body) {+ return new Reply(reply.status(), body, reply.headers());+ }++ private static AwsException invalid(String message) {+ return new AwsException("com.amazon.coral.validate#ValidationException", message, 400);+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbResponses.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbResponses.javaindex 15877f18e3..d39d07a8f6 100644--- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbResponses.java+++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbResponses.java@@ -1,6 +1,8 @@package io.github.hectorvent.floci.services.dynamodb;+import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;+import com.fasterxml.jackson.databind.node.ObjectNode;import jakarta.ws.rs.core.MediaType;import jakarta.ws.rs.core.MultivaluedMap;import jakarta.ws.rs.core.Response;@@ -19,6 +21,23 @@ public final class DynamoDbResponses {private DynamoDbResponses() {}+ public static Response withLegacyErrorNamespace(Response response, ObjectMapper objectMapper) {+ if (response.getStatus() < 400 || response.getEntity() == null) {+ return response;+ }+ JsonNode body = objectMapper.valueToTree(response.getEntity());+ if (!(body instanceof ObjectNode error) || !error.path("__type").isTextual()) {+ return response;+ }+ String type = error.path("__type").asText();+ if (type.startsWith("com.amazon.coral.")) {+ return response;+ }+ ObjectNode legacyError = error.deepCopy();+ legacyError.put("__type", "com.amazonaws.dynamodb.v20111205#" + type.substring(type.lastIndexOf('#') + 1));+ return Response.fromResponse(response).entity(legacyError).build();+ }+/*** Pre-serialize the response entity and attach an {@code X-Amz-Crc32} header whose value* is the decimal CRC32 of the serialized bytes.diff --git a/src/test/java/io/github/hectorvent/floci/core/common/AwsQueryServiceResolverTest.java b/src/test/java/io/github/hectorvent/floci/core/common/AwsQueryServiceResolverTest.javaindex 19a770715f..9010377840 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/AwsQueryServiceResolverTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/AwsQueryServiceResolverTest.java@@ -111,6 +111,6 @@ class AwsQueryServiceResolverTest {private static ServiceDescriptor descriptor(String service, ServiceProtocol protocol) {return new ServiceDescriptor(service, service, true, true, service, "memory", 0L,- null, protocol, Set.of(protocol), Set.of(), Set.of(service), Set.of(), Set.of());+ null, protocol, Set.of(protocol), Set.of(), Set.of(service), Set.of(), Set.of(), Set.of());}}diff --git a/src/test/java/io/github/hectorvent/floci/core/common/IamConditionContextResolverTest.java b/src/test/java/io/github/hectorvent/floci/core/common/IamConditionContextResolverTest.javaindex 4672bce536..b800332402 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/IamConditionContextResolverTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/IamConditionContextResolverTest.java@@ -63,6 +63,7 @@ class IamConditionContextResolverTest {private RequestContext requestContext;private EmulatorConfig config;private IamConditionContextResolver resolver;+ private ResolvedServiceCatalog serviceCatalog;@SuppressWarnings("unchecked")@BeforeEach@@ -89,9 +90,16 @@ class IamConditionContextResolverTest {requestContext.setRegion("us-east-1");config = mock(EmulatorConfig.class);when(config.defaultRegion()).thenReturn("us-east-1");+ serviceCatalog = mock(ResolvedServiceCatalog.class);+ ServiceDescriptor dynamo = new ServiceDescriptor("dynamodb", "dynamodb", true, true,+ null, null, 0L, null, ServiceProtocol.JSON, Set.of(ServiceProtocol.JSON),+ Set.of("DynamoDB_20111205."), Set.of("dynamodb"), Set.of(), Set.of(),+ Set.of("DynamoDB_20111205."));+ ServiceCatalog catalog = new ServiceCatalog(List.of(dynamo));+ when(serviceCatalog.matchTarget(any())).thenAnswer(call -> catalog.matchTarget(call.getArgument(0)));resolver = new IamConditionContextResolver(dynamoDbFacadeInstance, ec2ServiceInstance, s3ServiceInstance, logsServiceInstance, requestContext, config,- null);+ null, serviceCatalog);}@Test@@ -360,7 +368,7 @@ class IamConditionContextResolverTest {@SuppressWarnings("unchecked")private IamConditionContextResolver resolverAt(Instant now) {return new IamConditionContextResolver(mock(Instance.class), mock(Instance.class), mock(Instance.class),- mock(Instance.class), requestContext, config, null, Clock.fixed(now, ZoneOffset.UTC));+ mock(Instance.class), requestContext, config, null, serviceCatalog, Clock.fixed(now, ZoneOffset.UTC));}@Test@@ -471,6 +479,47 @@ class IamConditionContextResolverTest {}}+ @Test+ void legacyQueryConditionsRequireTheLegacyJsonTarget() {+ ContainerRequestContext request = mock(ContainerRequestContext.class);+ when(request.getProperty("floci.bufferedJsonBody")).thenReturn(json("""+ {"TableName":"FgacTable","HashKeyValue":{"S":"USER_bob"},+ "RangeKeyCondition":{"ComparisonOperator":"EQ","AttributeValueList":[{"S":"private"}]},+ "Count":true,"AttributesToGet":["secret"],+ "KeyConditions":{"PK":{"ComparisonOperator":"EQ","AttributeValueList":[{"S":"USER_alice"}]}}}+ """));+ when(request.getMediaType()).thenReturn(MediaType.valueOf("application/x-amz-json-1.0"));+ when(request.getHeaderString("X-Amz-Target")).thenReturn("DynamoDB_20111205.Query");+ TableDefinition table = fgacTable();+ table.setKeySchema(List.of(new KeySchemaElement("PK", "HASH"), new KeySchemaElement("SK", "RANGE")));+ when(dynamoDbService.findTable("FgacTable", "us-east-1")).thenReturn(Optional.of(table));+ Map<String, List<String>> legacy = resolver.resolve("dynamodb", "dynamodb:Query", request);+ assertEquals(List.of("USER_bob"), legacy.get("dynamodb:LeadingKeys"));+ assertEquals(Set.of("PK", "SK", "secret"), new HashSet<>(legacy.get("dynamodb:Attributes")));+ assertEquals(List.of("COUNT"), legacy.get("dynamodb:Select"));+ when(request.getHeaderString("X-Amz-Target")).thenReturn("DynamoDB_20120810.Query");+ Map<String, List<String>> modern = resolver.resolve("dynamodb", "dynamodb:Query", request);+ assertEquals(List.of("USER_alice"), modern.get("dynamodb:LeadingKeys"));+ assertFalse(modern.containsKey("dynamodb:Select"));+ }++ @Test+ void legacyBatchDeleteCannotHidePartitionOrAttributeNames() {+ ContainerRequestContext request = mock(ContainerRequestContext.class);+ when(request.getProperty("floci.bufferedJsonBody")).thenReturn(json("""+ {"RequestItems":{"FgacTable":[{"DeleteRequest":{"Key":{+ "HashKeyElement":{"S":"USER_bob"},"RangeKeyElement":{"S":"private"}}}}]}}+ """));+ when(request.getMediaType()).thenReturn(MediaType.valueOf("application/x-amz-json-1.0"));+ when(request.getHeaderString("X-Amz-Target")).thenReturn("DynamoDB_20111205.BatchWriteItem");+ TableDefinition table = fgacTable();+ table.setKeySchema(List.of(new KeySchemaElement("PK", "HASH"), new KeySchemaElement("SK", "RANGE")));+ when(dynamoDbService.findTable("FgacTable", "us-east-1")).thenReturn(Optional.of(table));+ Map<String, List<String>> conditions = resolver.resolve("dynamodb", "dynamodb:BatchWriteItem", request);+ assertEquals(List.of("USER_bob"), conditions.get("dynamodb:LeadingKeys"));+ assertEquals(Set.of("PK", "SK"), new HashSet<>(conditions.get("dynamodb:Attributes")));+ }+@Testvoid returnsNullWhenNoBodyWasBuffered() {ContainerRequestContext containerRequest = mock(ContainerRequestContext.class);diff --git a/src/test/java/io/github/hectorvent/floci/core/common/IamEnforcementFilterTest.java b/src/test/java/io/github/hectorvent/floci/core/common/IamEnforcementFilterTest.javaindex f280cf8d6d..070f56ba42 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/IamEnforcementFilterTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/IamEnforcementFilterTest.java@@ -270,7 +270,7 @@ class IamEnforcementFilterTest {private static ServiceDescriptor pricingDescriptor() {return new ServiceDescriptor("pricing", "pricing", true, true, null, null, 5000L, null,ServiceProtocol.JSON, Set.of(ServiceProtocol.JSON), Set.of("AWSPriceListService."),- Set.of("pricing", "api.pricing"), Set.of(), Set.of());+ Set.of("pricing", "api.pricing"), Set.of(), Set.of(), Set.of());}@Test@@ -433,7 +433,7 @@ class IamEnforcementFilterTest {private static ServiceDescriptor dynamoDbDescriptor() {return new ServiceDescriptor("dynamodb", "dynamodb", true, true, "dynamodb", "memory", 0L, null,ServiceProtocol.JSON, Set.of(ServiceProtocol.JSON), Set.of("DynamoDB_20120810."),- Set.of("dynamodb"), Set.of(), Set.of());+ Set.of("dynamodb"), Set.of(), Set.of(), Set.of());}private static ResourceInfo resourceInfo(Class<?> resourceClass) {@@ -446,7 +446,7 @@ class IamEnforcementFilterTest {Set<String> scopes, Class<?> resourceClass) {Set<Class<?>> resourceClasses = resourceClass == null ? Set.of() : Set.of(resourceClass);return new ServiceDescriptor(service, service, true, true, service, "memory", 0L,- null, protocol, Set.of(protocol), Set.of(), scopes, Set.of(), resourceClasses);+ null, protocol, Set.of(protocol), Set.of(), scopes, Set.of(), resourceClasses, Set.of());}@Testdiff --git a/src/test/java/io/github/hectorvent/floci/core/common/ServiceCatalogRoutingIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/ServiceCatalogRoutingIntegrationTest.javaindex 2c4f1e0056..e5f33dd9b9 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/ServiceCatalogRoutingIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/ServiceCatalogRoutingIntegrationTest.java@@ -50,6 +50,8 @@ class ServiceCatalogRoutingIntegrationTest {assertEquals("sqs", catalog.byCborSdkServiceId("AmazonSQS").orElseThrow().externalKey());assertEquals("sns", catalog.byCborSdkServiceId("SNS_20100331").orElseThrow().externalKey());assertEquals("states", catalog.byCborSdkServiceId("AWSStepFunctions").orElseThrow().externalKey());+ assertTrue(catalog.byCborSdkServiceId("DynamoDB_20111205").isEmpty());+ assertEquals("DescribeTable", catalog.matchTarget("DynamoDB_20111205.DescribeTable").orElseThrow().action());}@Testdiff --git a/src/test/java/io/github/hectorvent/floci/core/common/ServiceCatalogTest.java b/src/test/java/io/github/hectorvent/floci/core/common/ServiceCatalogTest.javaindex 9eb7cb87b9..42800a1008 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/ServiceCatalogTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/ServiceCatalogTest.java@@ -6,6 +6,7 @@ import java.util.List;import java.util.Set;import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertFalse;import static org.junit.jupiter.api.Assertions.assertTrue;class ServiceCatalogTest {@@ -35,8 +36,22 @@ class ServiceCatalogTest {assertTrue(catalog.matchPlainJsonTarget("Example.Specific.Read").isEmpty());}+ @Test+ void jsonOnlyPrefixesCannotSelectCborAndAreRecognizedByTheirDescriptor() {+ ServiceDescriptor service = new ServiceDescriptor("example", "example", true, true,+ null, null, 0L, null, ServiceProtocol.JSON, Set.of(ServiceProtocol.JSON, ServiceProtocol.CBOR),+ Set.of("LegacyExample.", "Example."), Set.of("example"), Set.of(), Set.of(),+ Set.of("LegacyExample."));+ ServiceCatalog catalog = new ServiceCatalog(List.of(service));++ assertTrue(catalog.matchTarget("LegacyExample.Read").orElseThrow().jsonOnly());+ assertFalse(catalog.matchTarget("Example.Read").orElseThrow().jsonOnly());+ assertTrue(catalog.byCborSdkServiceId("LegacyExample").isEmpty());+ assertEquals(service, catalog.byCborSdkServiceId("Example").orElseThrow());+ }+private static ServiceDescriptor descriptor(String name, Set<String> prefixes, Set<ServiceProtocol> protocols) {return new ServiceDescriptor(name, name, true, true, null, null, 0L, null,- ServiceProtocol.JSON, protocols, prefixes, Set.of(name), Set.of(), Set.of());+ ServiceProtocol.JSON, protocols, prefixes, Set.of(name), Set.of(), Set.of(), Set.of());}}diff --git a/src/test/java/io/github/hectorvent/floci/core/storage/PersistentPathValidatorTest.java b/src/test/java/io/github/hectorvent/floci/core/storage/PersistentPathValidatorTest.javaindex 9fd206bd35..f8ffec9ce5 100644--- a/src/test/java/io/github/hectorvent/floci/core/storage/PersistentPathValidatorTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/storage/PersistentPathValidatorTest.java@@ -46,7 +46,7 @@ class PersistentPathValidatorTest {private static ServiceDescriptor descriptor(String key, boolean enabled, String storageKey, String mode) {return new ServiceDescriptor(key, key, enabled, true, storageKey, mode, 0L,null, ServiceProtocol.REST_XML, Set.of(ServiceProtocol.REST_XML),- Set.of(), Set.of(), Set.of(), Set.of());+ Set.of(), Set.of(), Set.of(), Set.of(), Set.of());}@Testdiff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbCborIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbCborIntegrationTest.javaindex 03b9d6bdbd..5197998357 100644--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbCborIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbCborIntegrationTest.java@@ -28,6 +28,19 @@ class DynamoDbCborIntegrationTest {private static final ObjectMapper CBOR_MAPPER = new ObjectMapper(new CBORFactory());private static final ObjectMapper JSON_MAPPER = new ObjectMapper();+ @Test+ void legacyDynamoDbTargetDoesNotEnterModernCborHandling() throws Exception {+ byte[] body = CBOR_MAPPER.writeValueAsBytes(JSON_MAPPER.readTree("{\"TableName\":\"CborTable\"}"));+ for (String contentType : new String[]{"application/cbor", "application/x-amz-cbor-1.1"}) {+ byte[] result = given().contentType(contentType).accept(contentType)+ .header("X-Amz-Target", "DynamoDB_20111205.DescribeTable").body(body)+ .post("/").then().statusCode(404).extract().asByteArray();+ assertThat(CBOR_MAPPER.readTree(result).path("__type").asText(), containsString("UnknownOperation"));+ }+ given().contentType("application/cbor").body(body)+ .post("/service/DynamoDB_20111205/operation/DescribeTable").then().statusCode(404);+ }+@Test@Order(1)void createTableViaCbor() throws Exception {diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbConditionKeysTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbConditionKeysTest.javaindex 121536f8d1..de5a47517b 100644--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbConditionKeysTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbConditionKeysTest.java@@ -38,6 +38,21 @@ class DynamoDbConditionKeysTest {}}+ @Test+ void legacyPartitionValuesRemainExplicitWhenTableMetadataIsUnavailable() {+ DynamoDbConditionKeys.Result query = DynamoDbConditionKeys.extractLegacy("dynamodb:Query",+ json("{\"TableName\":\"missing\",\"HashKeyValue\":{\"S\":\"USER_bob\"}}"), null);+ assertEquals(List.of("USER_bob"), query.leadingKeys());+ assertTrue(query.attributes().isEmpty());+ DynamoDbConditionKeys.Result deletes = DynamoDbConditionKeys.extractLegacy("dynamodb:BatchWriteItem",+ json("""+ {"RequestItems":{"missing":[{"DeleteRequest":{"Key":{+ "HashKeyElement":{"S":"USER_bob"},"RangeKeyElement":{"S":"profile"}}}}]}}+ """), null);+ assertEquals(List.of("USER_bob"), deletes.leadingKeys());+ assertTrue(deletes.attributes().isEmpty());+ }+@Testvoid getItemExposesTheKeyValueAndTheKeyAttributeNames() {DynamoDbConditionKeys.Result result = DynamoDbConditionKeys.extract(diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbLegacyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbLegacyIntegrationTest.javanew file mode 100644index 0000000000..a270734db8--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbLegacyIntegrationTest.java@@ -0,0 +1,77 @@+package io.github.hectorvent.floci.services.dynamodb;++import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;+import io.quarkus.test.junit.QuarkusTest;+import io.restassured.response.Response;+import org.junit.jupiter.api.BeforeAll;+import org.junit.jupiter.api.Test;++import java.nio.charset.StandardCharsets;+import java.util.UUID;+import java.util.zip.CRC32;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.*;+import static org.junit.jupiter.api.Assertions.assertEquals;++@QuarkusTest+class DynamoDbLegacyIntegrationTest {+ @BeforeAll+ static void configureJson() {+ RestAssuredJsonUtils.configureAwsContentTypes();+ }++ @Test+ void missingTableHasAwsErrorAndCrcInsteadOfAnUnrecognizedTarget() {+ Response response = call("20111205", "DescribeTable", "{\"TableName\":\"absent-" + UUID.randomUUID() + "\"}");+ response.then().statusCode(400).body("__type",+ equalTo("com.amazonaws.dynamodb.v20111205#ResourceNotFoundException"));+ CRC32 crc = new CRC32();+ crc.update(response.asString().getBytes(StandardCharsets.UTF_8));+ assertEquals(Long.toUnsignedString(crc.getValue()), response.header("x-amz-crc32"));+ given().header("X-Amz-Target", "DynamoDB_20111205.DescribeTable")+ .contentType("application/json").body("{\"TableName\":\"absent\"}")+ .post("/").then().statusCode(404);+ }++ @Test+ void legacyAndModernTableMetadataShareActualStorage() {+ String name = "legacy-" + UUID.randomUUID();+ call("20120810", "CreateTable", """+ {"TableName":"%s","KeySchema":[{"AttributeName":"pk","KeyType":"HASH"}],+ "AttributeDefinitions":[{"AttributeName":"pk","AttributeType":"S"}],+ "BillingMode":"PAY_PER_REQUEST"}+ """.formatted(name)).then().statusCode(200);+ try {+ call("20111205", "DescribeTable", "{\"TableName\":\"" + name + "\"}").then().statusCode(200)+ .body("Table.TableName", equalTo(name))+ .body("Table.KeySchema.HashKeyElement.AttributeName", equalTo("pk"))+ .body("Table.KeySchema.HashKeyElement.AttributeType", equalTo("S"))+ .body("Table.KeySchema.RangeKeyElement", nullValue());+ call("20120810", "DescribeTable", "{\"TableName\":\"" + name + "\"}").then().statusCode(200)+ .body("Table.KeySchema[0].KeyType", equalTo("HASH"))+ .body("Table.AttributeDefinitions[0].AttributeName", equalTo("pk"));+ call("20111205", "GetItem", "{\"TableName\":\"" + name + "\"}").then().statusCode(400)+ .body("__type", equalTo("com.amazonaws.dynamodb.v20111205#UnknownOperationException"));+ } finally {+ call("20120810", "DeleteTable", "{\"TableName\":\"" + name + "\"}").then().statusCode(200);+ }+ }++ @Test+ void adapterValidationErrorsUseLegacyNamespaceAndCorrectCrc() {+ Response response = call("20111205", "Query", "{}");+ response.then().statusCode(400)+ .body("__type", equalTo("com.amazon.coral.validate#ValidationException"));+ CRC32 crc = new CRC32();+ crc.update(response.asString().getBytes(StandardCharsets.UTF_8));+ assertEquals(Long.toUnsignedString(crc.getValue()), response.header("x-amz-crc32"));+ call("20120810", "Query", "{}").then().statusCode(400)+ .body("__type", not(containsString("v20111205")));+ }++ private static Response call(String version, String action, String body) {+ return given().header("X-Amz-Target", "DynamoDB_" + version + "." + action)+ .contentType("application/x-amz-json-1.0").body(body).post("/");+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbLegacyProtocolTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbLegacyProtocolTest.javanew file mode 100644index 0000000000..4beafaace8--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbLegacyProtocolTest.java@@ -0,0 +1,179 @@+package io.github.hectorvent.floci.services.dynamodb;++import com.fasterxml.jackson.databind.JsonNode;+import com.fasterxml.jackson.databind.ObjectMapper;+import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations;+import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations.Call;+import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations.Reply;+import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations.Scope;+import org.junit.jupiter.api.Test;++import java.util.ArrayList;+import java.util.List;+import java.util.Map;++import static org.junit.jupiter.api.Assertions.*;++class DynamoDbLegacyProtocolTest {+ private static final ObjectMapper MAPPER = new ObjectMapper();+ private static final Scope SCOPE = new Scope("123456789012", "eu-west-1");+ private static final String TABLE = """+ {"Table":{"TableName":"metrics","TableStatus":"ACTIVE",+ "KeySchema":[{"AttributeName":"id-type","KeyType":"HASH"},{"AttributeName":"range","KeyType":"RANGE"}],+ "AttributeDefinitions":[{"AttributeName":"id-type","AttributeType":"S"},+ {"AttributeName":"range","AttributeType":"N"}]}}+ """;++ @Test+ void describesActualSchemaAndPreservesBackendErrors() throws Exception {+ Reply described = new DynamoDbLegacyProtocol(call -> reply(TABLE), SCOPE)+ .execute("DescribeTable", json("{\"TableName\":\"metrics\"}"));+ assertEquals(json("""+ {"HashKeyElement":{"AttributeName":"id-type","AttributeType":"S"},+ "RangeKeyElement":{"AttributeName":"range","AttributeType":"N"}}+ """), described.body().path("Table").path("KeySchema"));+ assertEquals("metrics", described.body().path("Table").path("TableName").asText());+ Reply denied = new Reply(400, json("{\"__type\":\"ResourceNotFoundException\"}"),+ Map.of("x-test", List.of("kept")));+ assertSame(denied, new DynamoDbLegacyProtocol(call -> denied, SCOPE)+ .execute("DescribeTable", json("{\"TableName\":\"absent\"}")));+ }++ @Test+ void translatesQueryConditionsPaginationAndCapacityInOneScope() throws Exception {+ List<Call> calls = new ArrayList<>();+ DynamoDbOperations operations = call -> {+ calls.add(call);+ return reply("DescribeTable".equals(call.action()) ? TABLE : """+ {"Items":[{"range":{"N":"3"}}],"Count":1,+ "LastEvaluatedKey":{"id-type":{"S":"domain_q"},"range":{"N":"3"}},+ "ConsumedCapacity":{"TableName":"metrics","CapacityUnits":0.5}}+ """);+ };+ JsonNode request = json("""+ {"TableName":"metrics","HashKeyValue":{"S":"domain_q"},+ "RangeKeyCondition":{"ComparisonOperator":"BETWEEN","AttributeValueList":[{"N":"2"},{"N":"9"}]},+ "ExclusiveStartKey":{"HashKeyElement":{"S":"domain_q"},"RangeKeyElement":{"N":"2"}},+ "Limit":1,"ConsistentRead":true,"ScanIndexForward":false,"AttributesToGet":["range"]}+ """);+ JsonNode original = request.deepCopy();+ Reply reply = new DynamoDbLegacyProtocol(operations, SCOPE).execute("Query", request);+ assertEquals(original, request);+ assertEquals(List.of("DescribeTable", "Query"), calls.stream().map(Call::action).toList());+ assertTrue(calls.stream().allMatch(call -> call.scope().equals(SCOPE)));+ assertEquals(json("""+ {"id-type":{"ComparisonOperator":"EQ","AttributeValueList":[{"S":"domain_q"}]},+ "range":{"ComparisonOperator":"BETWEEN","AttributeValueList":[{"N":"2"},{"N":"9"}]}}+ """), calls.getLast().body().path("KeyConditions"));+ assertEquals(json("{\"id-type\":{\"S\":\"domain_q\"},\"range\":{\"N\":\"2\"}}"),+ calls.getLast().body().path("ExclusiveStartKey"));+ assertEquals(1, calls.getLast().body().path("Limit").asInt());+ assertTrue(calls.getLast().body().path("ConsistentRead").asBoolean());+ assertFalse(calls.getLast().body().path("ScanIndexForward").asBoolean());+ assertEquals(json("[\"range\"]"), calls.getLast().body().path("AttributesToGet"));+ assertEquals(json("{\"HashKeyElement\":{\"S\":\"domain_q\"},\"RangeKeyElement\":{\"N\":\"3\"}}"),+ reply.body().path("LastEvaluatedKey"));+ assertEquals(0.5, reply.body().path("ConsumedCapacityUnits").asDouble());+ assertEquals(1, reply.body().path("Count").asInt());+ }++ @Test+ void countUsesBackendCountAndOmitsAbsentContinuation() throws Exception {+ List<Call> calls = new ArrayList<>();+ DynamoDbLegacyProtocol protocol = new DynamoDbLegacyProtocol(call -> {+ calls.add(call);+ return reply("DescribeTable".equals(call.action()) ? TABLE : """+ {"Count":7,"LastEvaluatedKey":{},"ConsumedCapacity":{"CapacityUnits":2.0}}+ """);+ }, SCOPE);+ Reply reply = protocol.execute("Query",+ json("{\"TableName\":\"metrics\",\"HashKeyValue\":{\"S\":\"domain_q\"},\"Count\":true}"));+ assertEquals("COUNT", calls.getLast().body().path("Select").asText());+ assertEquals(7, reply.body().path("Count").asInt());+ assertFalse(reply.body().has("Items"));+ assertFalse(reply.body().has("LastEvaluatedKey"));+ }++ @Test+ void batchRetriesKeepLegacyKeysAndRealPerTableCapacity() throws Exception {+ List<Call> calls = new ArrayList<>();+ DynamoDbOperations operations = call -> {+ calls.add(call);+ return reply("DescribeTable".equals(call.action()) ? TABLE : """+ {"UnprocessedItems":{"metrics":[+ {"DeleteRequest":{"Key":{"id-type":{"S":"domain_q"},"range":{"N":"3"}}}},+ {"PutRequest":{"Item":{"id-type":{"S":"domain_d"},"range":{"N":"4"}}}}]},+ "ConsumedCapacity":[{"TableName":"metrics","CapacityUnits":2.0}]}+ """);+ };+ JsonNode request = json("""+ {"RequestItems":{"metrics":[+ {"DeleteRequest":{"Key":{"HashKeyElement":{"S":"domain_q"},"RangeKeyElement":{"N":"3"}}}},+ {"PutRequest":{"Item":{"id-type":{"S":"domain_d"},"range":{"N":"4"}}}}]}}+ """);+ JsonNode original = request.deepCopy();+ Reply reply = new DynamoDbLegacyProtocol(operations, SCOPE).execute("BatchWriteItem", request);+ assertEquals(original, request);+ assertEquals(2, calls.size());+ assertTrue(calls.stream().allMatch(call -> call.scope().equals(SCOPE)));+ assertEquals(json("{\"id-type\":{\"S\":\"domain_q\"},\"range\":{\"N\":\"3\"}}"),+ calls.getLast().body().path("RequestItems").path("metrics").get(0).path("DeleteRequest").path("Key"));+ assertEquals(request.path("RequestItems"), reply.body().path("UnprocessedItems"));+ assertEquals(2.0, reply.body().path("Responses").path("metrics").path("ConsumedCapacityUnits").asDouble());+ calls.clear();+ new DynamoDbLegacyProtocol(operations, SCOPE).execute("BatchWriteItem",+ MAPPER.createObjectNode().set("RequestItems", reply.body().path("UnprocessedItems")));+ assertEquals(2, calls.size());+ }++ @Test+ void invalidLegacyKeysAndUnsupportedActionsNeverWrite() throws Exception {+ List<Call> calls = new ArrayList<>();+ DynamoDbOperations operations = call -> {+ calls.add(call);+ assertEquals("DescribeTable", call.action());+ return reply(TABLE);+ };+ for (String request : List.of(+ "{\"TableName\":\"metrics\"}",+ "{\"TableName\":\"metrics\",\"HashKeyValue\":\"bad\"}",+ "{\"TableName\":\"metrics\",\"HashKeyValue\":{\"S\":\"x\"},\"ExclusiveStartKey\":{\"HashKeyElement\":{\"S\":\"x\"}}}",+ "{\"TableName\":\"metrics\",\"HashKeyValue\":{\"S\":\"x\"},\"Count\":\"true\"}")) {+ assertThrows(AwsException.class, () -> new DynamoDbLegacyProtocol(operations, SCOPE)+ .execute("Query", json(request)));+ }+ assertThrows(AwsException.class, () -> new DynamoDbLegacyProtocol(operations, SCOPE)+ .execute("BatchWriteItem", json("""+ {"RequestItems":{"metrics":[{"DeleteRequest":{"Key":{"HashKeyElement":{"S":"x"}}}}]}}+ """)));+ calls.clear();+ assertThrows(AwsException.class, () -> new DynamoDbLegacyProtocol(operations, SCOPE)+ .execute("CreateTable", json("{}")));+ assertTrue(calls.isEmpty());+ }++ @Test+ void missingOrDeniedMetadataCannotReachQueryOrWrite() throws Exception {+ for (String action : List.of("Query", "BatchWriteItem")) {+ List<Call> calls = new ArrayList<>();+ DynamoDbOperations operations = call -> {+ calls.add(call);+ return new Reply(400, json("{\"__type\":\"AccessDeniedException\",\"message\":\"denied\"}"), Map.of());+ };+ assertThrows(AwsException.class, () -> new DynamoDbLegacyProtocol(operations, SCOPE).execute(action,+ json("Query".equals(action)+ ? "{\"TableName\":\"metrics\",\"HashKeyValue\":{\"S\":\"x\"}}"+ : "{\"RequestItems\":{\"metrics\":[{\"DeleteRequest\":{\"Key\":{}}}]}}")));+ assertEquals(List.of("DescribeTable"), calls.stream().map(Call::action).toList());+ }+ }++ private static JsonNode json(String value) throws Exception {+ return MAPPER.readTree(value);+ }++ private static Reply reply(String value) throws Exception {+ return new Reply(200, json(value), Map.of());+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbResponsesTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbResponsesTest.javaindex e87bc1ba07..118523ab5c 100644--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbResponsesTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbResponsesTest.java@@ -14,6 +14,7 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals;import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertNotNull;import static org.junit.jupiter.api.Assertions.assertNull;+import static org.junit.jupiter.api.Assertions.assertSame;class DynamoDbResponsesTest {@@ -112,4 +113,28 @@ class DynamoDbResponsesTest {void withCrc32_nullResponse_returnsNull() {assertNull(DynamoDbResponses.withCrc32(null, mapper));}++ @Test+ void legacyErrorsRetainMessageHeadersAndOriginalBackendBody() {+ ObjectNode entity = mapper.createObjectNode()+ .put("__type", "com.amazonaws.dynamodb.v20120810#ResourceNotFoundException")+ .put("message", "Table not found");+ Response input = Response.status(400).entity(entity).header("x-request-id", "probe").build();+ Response legacy = DynamoDbResponses.withLegacyErrorNamespace(input, mapper);+ ObjectNode body = (ObjectNode) legacy.getEntity();+ assertEquals("com.amazonaws.dynamodb.v20111205#ResourceNotFoundException", body.path("__type").asText());+ assertEquals("Table not found", body.path("message").asText());+ assertEquals("probe", legacy.getHeaderString("x-request-id"));+ assertEquals(400, legacy.getStatus());+ assertEquals("com.amazonaws.dynamodb.v20120810#ResourceNotFoundException", entity.path("__type").asText());+ }++ @Test+ void legacyResponsesPreserveCoralValidationAndSuccessfulBodies() {+ Response validation = Response.status(400).entity(mapper.createObjectNode()+ .put("__type", "com.amazon.coral.validate#ValidationException")).build();+ assertSame(validation, DynamoDbResponses.withLegacyErrorNamespace(validation, mapper));+ Response success = Response.ok(mapper.createObjectNode().put("Count", 1)).build();+ assertSame(success, DynamoDbResponses.withLegacyErrorNamespace(success, mapper));+ }}diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbFacadeTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbFacadeTest.javaindex 3437c39795..d11570ab35 100644--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbFacadeTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbFacadeTest.java@@ -153,7 +153,7 @@ class DynamoDbFacadeTest {requestContext.setRegion(OTHER_REGION);IamConditionContextResolver resolver = new IamConditionContextResolver(dynamoDb,mock(Instance.class), mock(Instance.class), mock(Instance.class), requestContext, mock(EmulatorConfig.class),- null);+ null, mock(io.github.hectorvent.floci.core.common.ResolvedServiceCatalog.class));ContainerRequestContext request = mock(ContainerRequestContext.class);JsonNode body = mapper.readTree("""{"TableName": "recorded-table", "Key": {"id": {"S": "USER_alice"}}}diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/DynamoDbFgacEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/DynamoDbFgacEnforcementIntegrationTest.javaindex 0f572a5e8d..6037ea9f69 100644--- a/src/test/java/io/github/hectorvent/floci/services/iam/DynamoDbFgacEnforcementIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/iam/DynamoDbFgacEnforcementIntegrationTest.java@@ -36,6 +36,99 @@ class DynamoDbFgacEnforcementIntegrationTest {RestAssuredJsonUtils.configureAwsContentTypes();}+ @ParameterizedTest+ @ValueSource(strings = {"Query", "BatchWriteItem"})+ void legacyKeysCannotBypassLeadingKeyPolicyWithoutNullGuard(String action) {+ String suffix = UUID.randomUUID().toString().substring(0, 8);+ String tableName = "fgac-legacy-" + suffix;+ String roleName = "FgacLegacyRole" + suffix;+ given().header("Authorization", auth(ROLE_ACCOUNT_ID, "dynamodb"))+ .header("X-Amz-Target", "DynamoDB_20120810.CreateTable").contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {"TableName":"%s","KeySchema":[{"AttributeName":"PK","KeyType":"HASH"},+ {"AttributeName":"SK","KeyType":"RANGE"}],+ "AttributeDefinitions":[{"AttributeName":"PK","AttributeType":"S"},+ {"AttributeName":"SK","AttributeType":"S"}],"BillingMode":"PAY_PER_REQUEST"}+ """.formatted(tableName)).post("/").then().statusCode(200);+ createRole(roleName);+ putBroadDynamoDbRolePolicy(roleName);+ String accessKey = assumeRoleWithLeadingKeysSessionPolicy(roleName, false);+ for (String user : new String[]{"USER_bob", "USER_alice"}) {+ String body = "Query".equals(action)+ ? """+ {"TableName":"%s","HashKeyValue":{"S":"%s"}}+ """.formatted(tableName, user)+ : """+ {"RequestItems":{"%s":[{"DeleteRequest":{"Key":{+ "HashKeyElement":{"S":"%s"},"RangeKeyElement":{"S":"profile"}}}}]}}+ """.formatted(tableName, user);+ if ("USER_bob".equals(user)) {+ given().header("Authorization", auth(accessKey, "dynamodb"))+ .header("X-Amz-Target", "DynamoDB_20111205." + action)+ .contentType(DYNAMODB_CONTENT_TYPE).body(body).post("/").then()+ .statusCode(400).body(containsString("AccessDeniedException"));+ } else {+ given().header("Authorization", auth(accessKey, "dynamodb"))+ .header("X-Amz-Target", "DynamoDB_20111205." + action)+ .contentType(DYNAMODB_CONTENT_TYPE).body(body).post("/").then().statusCode(200);+ }+ }+ }++ @Test+ void legacyQueryHonorsProjectedAttributesAndExplicitPartitionDeny() {+ String suffix = UUID.randomUUID().toString().substring(0, 8);+ String tableName = "fgac-legacy-attributes-" + suffix;+ String roleName = "FgacLegacyAttributes" + suffix;+ given().header("Authorization", auth(ROLE_ACCOUNT_ID, "dynamodb"))+ .header("X-Amz-Target", "DynamoDB_20120810.CreateTable").contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {"TableName":"%s","KeySchema":[{"AttributeName":"PK","KeyType":"HASH"},+ {"AttributeName":"SK","KeyType":"RANGE"}],+ "AttributeDefinitions":[{"AttributeName":"PK","AttributeType":"S"},+ {"AttributeName":"SK","AttributeType":"S"}],"BillingMode":"PAY_PER_REQUEST"}+ """.formatted(tableName)).post("/").then().statusCode(200);+ given().header("Authorization", auth(ROLE_ACCOUNT_ID, "dynamodb"))+ .header("X-Amz-Target", "DynamoDB_20120810.PutItem").contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {"TableName":"%s","Item":{"PK":{"S":"USER_alice"},"SK":{"S":"one"},+ "public":{"S":"visible"},"secret":{"S":"fixture-private"}}}+ """.formatted(tableName)).post("/").then().statusCode(200);+ createRole(roleName);+ putBroadDynamoDbRolePolicy(roleName);+ String accessKey = assumeRoleWithSessionPolicy(roleName, """+ {"Version":"2012-10-17","Statement":[+ {"Effect":"Allow","Action":"dynamodb:Query","Resource":"*",+ "Condition":{"ForAllValues:StringEquals":{"dynamodb:Attributes":["PK","SK","public"]}}},+ {"Effect":"Deny","Action":"dynamodb:Query","Resource":"*",+ "Condition":{"ForAnyValue:StringEquals":{"dynamodb:LeadingKeys":["USER_denied"]}}}]}+ """);+ for (String[] query : new String[][]{+ {"USER_alice", "public", "200"}, {"USER_alice", "secret", "400"}, {"USER_denied", "public", "400"}}) {+ if ("200".equals(query[2])) {+ given().header("Authorization", auth(accessKey, "dynamodb"))+ .header("X-Amz-Target", "DynamoDB_20111205.Query").contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {"TableName":"%s","HashKeyValue":{"S":"%s"},"AttributesToGet":["%s"]}+ """.formatted(tableName, query[0], query[1]))+ .post("/").then().statusCode(200);+ } else {+ given().header("Authorization", auth(accessKey, "dynamodb"))+ .header("X-Amz-Target", "DynamoDB_20111205.Query").contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {"TableName":"%s","HashKeyValue":{"S":"%s"},"AttributesToGet":["%s"]}+ """.formatted(tableName, query[0], query[1]))+ .post("/").then().statusCode(400).body(containsString("AccessDeniedException"));+ }+ }+ given().header("Authorization", auth(accessKey, "dynamodb"))+ .header("X-Amz-Target", "DynamoDB_20111205.Query").contentType(DYNAMODB_CONTENT_TYPE)+ .body("""+ {"TableName":"%s","HashKeyValue":{"S":"USER_alice"}}+ """.formatted(tableName)).post("/").then().statusCode(200)+ .body("Items[0].secret.S", equalTo("fixture-private"));+ }+@Testvoid leadingKeysScopedSessionReadsItsOwnPartitionAndIsDeniedAnotherTenants() {String suffix = UUID.randomUUID().toString().substring(0, 8);@@ -393,6 +486,10 @@ class DynamoDbFgacEnforcementIntegrationTest {"Condition":{"ForAllValues:StringLike":{"dynamodb:LeadingKeys":["USER_alice*"]}%s}}]}""".formatted(nullGuard ? ",\"Null\":{\"dynamodb:LeadingKeys\":\"false\"}" : "");+ return assumeRoleWithSessionPolicy(roleName, policy);+ }++ private static String assumeRoleWithSessionPolicy(String roleName, String policy) {return given().formParam("Action", "AssumeRole").formParam("RoleArn", "arn:aws:iam::" + ROLE_ACCOUNT_ID + ":role/" + roleName)