Light, fluffy, and always free - The AWS Local Emulator alternative
fix(dynamodb): validate GSI and LSI index names like AWS (#4601)
TATSUNO “Taz” Yasuhirodad5648b7b96
diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.javaindex 95f6234942..7bb2c71a92 100644--- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/NativeDynamoDbJsonHandler.java@@ -39,6 +39,7 @@ import java.util.List;import java.util.Map;import java.util.Set;import java.util.function.Supplier;+import java.util.regex.Pattern;/*** DynamoDB JSON protocol handler of the native engine, reached through the native backend@@ -48,6 +49,7 @@ import java.util.function.Supplier;public class NativeDynamoDbJsonHandler {private static final String SSE_TYPE_KMS = "KMS";+ private static final Pattern INDEX_NAME_CHARS = Pattern.compile("[a-zA-Z0-9_.\\-]+");private final DynamoDbService dynamoDbService;private final NativeDynamoDbTableService tableService;@@ -164,7 +166,8 @@ public class NativeDynamoDbJsonHandler {var gsiPosition = 0;for (JsonNode gsiNode : gsiArray) {gsiPosition++;- String indexName = gsiNode.path("IndexName").asText();+ String indexName = requireIndexName(gsiNode,+ memberPrefix + "globalSecondaryIndexes." + gsiPosition + ".member");List<KeySchemaElement> gsiKeySchema = new ArrayList<>();gsiNode.path("KeySchema").forEach(ks ->gsiKeySchema.add(new KeySchemaElement(@@ -205,7 +208,8 @@ public class NativeDynamoDbJsonHandler {var lsiPosition = 0;for (JsonNode lsiNode : lsiArray) {lsiPosition++;- String indexName = lsiNode.path("IndexName").asText();+ String indexName = requireIndexName(lsiNode,+ memberPrefix + "localSecondaryIndexes." + lsiPosition + ".member");List<KeySchemaElement> lsiKeySchema = new ArrayList<>();lsiNode.path("KeySchema").forEach(ks ->lsiKeySchema.add(new KeySchemaElement(@@ -289,6 +293,28 @@ public class NativeDynamoDbJsonHandler {// AWS reports an empty list as a length constraint on the 1-based member path, before the// projection type check. A null NonKeyAttributes counts as not specified.+ private static String requireIndexName(JsonNode node, String memberPath) {+ JsonNode indexNameNode = node.path("IndexName");+ String constraint = " at '" + memberPath + ".indexName' failed to satisfy constraint: ";+ if (indexNameNode.isMissingNode() || indexNameNode.isNull()) {+ throwValidationErrors(List.of("Value null" + constraint + "Member must not be null"));+ }+ String indexName = indexNameNode.asText();+ String value = "Value '" + indexName + "'" + constraint;+ List<String> errors = new ArrayList<>();+ if (!INDEX_NAME_CHARS.matcher(indexName).matches()) {+ errors.add(value + "Member must satisfy regular expression pattern: [a-zA-Z0-9_.-]+");+ }+ if (indexName.length() < 3) {+ errors.add(value + "Member must have length greater than or equal to 3");+ }+ if (255 < indexName.length()) {+ errors.add(value + "Member must have length less than or equal to 255");+ }+ throwValidationErrors(errors);+ return indexName;+ }+private static void rejectEmptyNonKeyAttributes(JsonNode nonKeyAttrArray, String memberPath) {if (nonKeyAttrArray.isArray() && nonKeyAttrArray.isEmpty()) {throw new AwsException("ValidationException",@@ -1676,7 +1702,8 @@ public class NativeDynamoDbJsonHandler {updatePosition++;JsonNode createNode = update.path("Create");if (!createNode.isMissingNode()) {- String indexName = createNode.path("IndexName").asText();+ String indexName = requireIndexName(createNode,+ "globalSecondaryIndexUpdates." + updatePosition + ".member.create");List<KeySchemaElement> gsiKeySchema = new ArrayList<>();createNode.path("KeySchema").forEach(ks ->gsiKeySchema.add(new KeySchemaElement(@@ -1712,13 +1739,15 @@ public class NativeDynamoDbJsonHandler {}JsonNode deleteNode = update.path("Delete");if (!deleteNode.isMissingNode()) {- gsiDeletes.add(deleteNode.path("IndexName").asText());+ gsiDeletes.add(requireIndexName(deleteNode,+ "globalSecondaryIndexUpdates." + updatePosition + ".member.delete"));}JsonNode updateNode = update.path("Update");if (updateNode.isObject()) {JsonNode gsiPt = updateNode.path("ProvisionedThroughput");gsiThroughputUpdates.add(new GsiThroughputUpdate(- updateNode.path("IndexName").asText(),+ requireIndexName(updateNode,+ "globalSecondaryIndexUpdates." + updatePosition + ".member.update"),gsiPt.isObject() ? parseThroughput(gsiPt) : null,parseOnDemandThroughput(updateNode.path("OnDemandThroughput"))));}diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.javaindex 8811bb78dc..cf9166cc05 100644--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbIntegrationTest.java@@ -594,6 +594,105 @@ class DynamoDbIntegrationTest {.statusCode(200);}+ @Test+ void createTableRejectsAnInvalidGsiIndexName() {+ assertIndexNameRejected("CreateTable", """+ {+ "TableName": "InvalidGsiNameTable",+ "KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],+ "AttributeDefinitions": [+ {"AttributeName": "pk", "AttributeType": "S"},+ {"AttributeName": "g", "AttributeType": "S"}+ ],+ "BillingMode": "PAY_PER_REQUEST",+ "GlobalSecondaryIndexes": [{+ "IndexName": "ab",+ "KeySchema": [{"AttributeName": "g", "KeyType": "HASH"}],+ "Projection": {"ProjectionType": "ALL"}+ }]+ }+ """, "1 validation error detected: Value 'ab' at 'globalSecondaryIndexes.1.member.indexName' "+ + "failed to satisfy constraint: Member must have length greater than or equal to 3");+ }++ @Test+ void createTableRejectsAnLsiWithoutAnIndexName() {+ assertIndexNameRejected("CreateTable", """+ {+ "TableName": "InvalidLsiNameTable",+ "KeySchema": [+ {"AttributeName": "pk", "KeyType": "HASH"},+ {"AttributeName": "sk", "KeyType": "RANGE"}+ ],+ "AttributeDefinitions": [+ {"AttributeName": "pk", "AttributeType": "S"},+ {"AttributeName": "sk", "AttributeType": "S"},+ {"AttributeName": "l", "AttributeType": "S"}+ ],+ "BillingMode": "PAY_PER_REQUEST",+ "LocalSecondaryIndexes": [{+ "KeySchema": [+ {"AttributeName": "pk", "KeyType": "HASH"},+ {"AttributeName": "l", "KeyType": "RANGE"}+ ],+ "Projection": {"ProjectionType": "ALL"}+ }]+ }+ """, "1 validation error detected: Value null at 'localSecondaryIndexes.1.member.indexName' "+ + "failed to satisfy constraint: Member must not be null");+ }++ @Test+ void updateTableRejectsAnInvalidIndexNameBeforeTheTableLookup() {+ assertIndexNameRejected("UpdateTable", """+ {+ "TableName": "NoSuchIndexNameTable",+ "AttributeDefinitions": [{"AttributeName": "g", "AttributeType": "S"}],+ "GlobalSecondaryIndexUpdates": [{"Create": {+ "IndexName": "",+ "KeySchema": [{"AttributeName": "g", "KeyType": "HASH"}],+ "Projection": {"ProjectionType": "ALL"}+ }}]+ }+ """, "2 validation errors detected: Value '' at 'globalSecondaryIndexUpdates.1.member.create.indexName' "+ + "failed to satisfy constraint: Member must satisfy regular expression pattern: [a-zA-Z0-9_.-]+; "+ + "Value '' at 'globalSecondaryIndexUpdates.1.member.create.indexName' "+ + "failed to satisfy constraint: Member must have length greater than or equal to 3");++ assertIndexNameRejected("UpdateTable", """+ {+ "TableName": "NoSuchIndexNameTable",+ "GlobalSecondaryIndexUpdates": [+ {"Update": {"IndexName": "bad name!", "OnDemandThroughput": {"MaxReadRequestUnits": 10}}}+ ]+ }+ """, "1 validation error detected: Value 'bad name!' at 'globalSecondaryIndexUpdates.1.member.update.indexName' "+ + "failed to satisfy constraint: Member must satisfy regular expression pattern: [a-zA-Z0-9_.-]+");++ String longName = "x".repeat(256);+ assertIndexNameRejected("UpdateTable", """+ {+ "TableName": "NoSuchIndexNameTable",+ "GlobalSecondaryIndexUpdates": [{"Delete": {"IndexName": "%s"}}]+ }+ """.formatted(longName), "1 validation error detected: Value '" + longName+ + "' at 'globalSecondaryIndexUpdates.1.member.delete.indexName' "+ + "failed to satisfy constraint: Member must have length less than or equal to 255");+ }++ private void assertIndexNameRejected(String action, String body, String message) {+ given()+ .header("X-Amz-Target", "DynamoDB_20120810." + action)+ .contentType(DYNAMODB_CONTENT_TYPE)+ .body(body)+ .when()+ .post("/")+ .then()+ .statusCode(400)+ .body("__type", equalTo("ValidationException"))+ .body("message", equalTo(message));+ }+@Testvoid createTableWithGsiAndLsi() {given()