Light, fluffy, and always free - The AWS Local Emulator alternative
fix(dynamodb): reject LSI KeySchema with other than one sort key (#2476)
Matheus Oliveira0397826b9087
diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.javaindex 835fa3c775..4d693b53e9 100644--- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java+++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java@@ -327,6 +327,12 @@ public class DynamoDbService {throw new AwsException("ValidationException","LocalSecondaryIndex partition key must match table partition key", 400);}+ if (lsi.getSortKeyNames().size() != 1) {+ throw new AwsException("ValidationException",+ "One or more parameter values were invalid: Local Secondary Index "+ + lsi.getIndexName() + " must have a sort key consisting of exactly "+ + "one scalar attribute", 400);+ }lsi.setIndexArn(table.getTableArn() + "/index/" + lsi.getIndexName());}table.setLocalSecondaryIndexes(new ArrayList<>(lsis));@@ -1591,7 +1597,7 @@ public class DynamoDbService {JsonNode exprAttrNames, JsonNode exprAttrValues, String returnValuesOnConditionCheckFailure) {DynamoDbReservedWords.check(conditionExpression, "ConditionExpression");if (!matchesFilterExpression(existingItem, conditionExpression, exprAttrNames, exprAttrValues)) {- if ("ALL_OLD".equals(returnValuesOnConditionCheckFailure)){+ if ("ALL_OLD".equals(returnValuesOnConditionCheckFailure)){throw new ConditionalCheckFailedException(existingItem);}else {diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.javaindex 36f2ae7bed..ae96b86dd4 100644--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java@@ -6,6 +6,7 @@ import io.github.hectorvent.floci.services.dynamodb.model.AttributeDefinition;import io.github.hectorvent.floci.services.dynamodb.model.ConditionalCheckFailedException;import io.github.hectorvent.floci.services.dynamodb.model.GlobalSecondaryIndex;import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement;+import io.github.hectorvent.floci.services.dynamodb.model.LocalSecondaryIndex;import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition;import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;@@ -100,6 +101,51 @@ class DynamoDbServiceTest {assertEquals(400, ex.getHttpStatus());}+ @Test+ void createTableRejectsLocalSecondaryIndexWithMultipleSortKeyAttributes() {+ LocalSecondaryIndex lsi = new LocalSecondaryIndex(+ "LSI1",+ List.of(+ new KeySchemaElement("PK", "HASH"),+ new KeySchemaElement("LSI1A", "RANGE"),+ new KeySchemaElement("LSI1B", "RANGE")),+ null, "ALL");++ AwsException ex = assertThrows(AwsException.class, () -> service.createTable("lsi-multi-sort",+ List.of(+ new KeySchemaElement("PK", "HASH"),+ new KeySchemaElement("SK", "RANGE")),+ List.of(+ new AttributeDefinition("PK", "S"),+ new AttributeDefinition("SK", "S"),+ new AttributeDefinition("LSI1A", "S"),+ new AttributeDefinition("LSI1B", "S")),+ 5L, 5L, List.of(), List.of(lsi), "eu-west-1"));++ assertEquals("ValidationException", ex.getErrorCode());+ assertEquals(400, ex.getHttpStatus());+ }++ @Test+ void createTableRejectsLocalSecondaryIndexWithNoSortKey() {+ LocalSecondaryIndex lsi = new LocalSecondaryIndex(+ "LSI1",+ List.of(new KeySchemaElement("PK", "HASH")),+ null, "ALL");++ AwsException ex = assertThrows(AwsException.class, () -> service.createTable("lsi-no-sort",+ List.of(+ new KeySchemaElement("PK", "HASH"),+ new KeySchemaElement("SK", "RANGE")),+ List.of(+ new AttributeDefinition("PK", "S"),+ new AttributeDefinition("SK", "S")),+ 5L, 5L, List.of(), List.of(lsi), "eu-west-1"));++ assertEquals("ValidationException", ex.getErrorCode());+ assertEquals(400, ex.getHttpStatus());+ }+@Testvoid describeTable() {String region = "eu-west-1";@@ -2289,7 +2335,7 @@ class DynamoDbServiceTest {@Testvoid updateItemConditionFailedReturnValuesNone() {createOrdersTable("us-east-1");-+ObjectNode order = item("customerId", "1", "orderId", "sort1", "testAttr", "testVal");ObjectNode key = item("customerId", "1", "orderId", "sort1");service.putItem("Orders", order, "us-east-1");@@ -2342,10 +2388,10 @@ class DynamoDbServiceTest {@Testvoid putItemNetNewConditionFailedReturnValuesNone() {createOrdersTable("us-east-1");-+ObjectNode order = item("customerId", "1", "orderId", "sort1", "testAttr", "testVal");ObjectNode key = item("customerId", "1", "orderId", "sort1");- ConditionalCheckFailedException ex = assertThrows(ConditionalCheckFailedException.class, () ->+ ConditionalCheckFailedException ex = assertThrows(ConditionalCheckFailedException.class, () ->service.putItem("Orders", order, "attribute_exists(customerId)", null, null, "us-east-1", "NONE"));JsonNode stored = service.getItem("Orders", key, "us-east-1");@@ -2357,10 +2403,10 @@ class DynamoDbServiceTest {@Testvoid putItemNetNewConditionFailedReturnValuesAllOld() {createOrdersTable("us-east-1");-+ObjectNode order = item("customerId", "1", "orderId", "sort1", "testAttr", "testVal");ObjectNode key = item("customerId", "1", "orderId", "sort1");- ConditionalCheckFailedException ex = assertThrows(ConditionalCheckFailedException.class, () ->+ ConditionalCheckFailedException ex = assertThrows(ConditionalCheckFailedException.class, () ->service.putItem("Orders", order, "attribute_exists(customerId)", null, null, "us-east-1", "ALL_OLD"));JsonNode stored = service.getItem("Orders", key, "us-east-1");@@ -2368,18 +2414,18 @@ class DynamoDbServiceTest {assertNull(ex.getItem());}-+@Testvoid putItemExistingConditionFailedReturnValuesNone() {createOrdersTable("us-east-1");-+ObjectNode order1 = item("customerId", "1", "orderId", "sort1", "testAttr", "testVal");ObjectNode order2 = item("customerId", "1", "orderId", "sort1", "testAttr", "testVal1");ObjectNode key = item("customerId", "1", "orderId", "sort1");service.putItem("Orders", order1, "us-east-1");- ConditionalCheckFailedException ex = assertThrows(ConditionalCheckFailedException.class, () ->+ ConditionalCheckFailedException ex = assertThrows(ConditionalCheckFailedException.class, () ->service.putItem("Orders", order2, "attribute_exists(someAttr)", null, null, "us-east-1", "NONE"));JsonNode stored = service.getItem("Orders", key, "us-east-1");@@ -2400,7 +2446,7 @@ class DynamoDbServiceTest {service.putItem("Orders", order1, "us-east-1");- ConditionalCheckFailedException ex = assertThrows(ConditionalCheckFailedException.class, () ->+ ConditionalCheckFailedException ex = assertThrows(ConditionalCheckFailedException.class, () ->service.putItem("Orders", order2, "attribute_exists(someAttr)", null, null, "us-east-1", "ALL_OLD"));JsonNode stored = service.getItem("Orders", key, "us-east-1");@@ -2414,18 +2460,18 @@ class DynamoDbServiceTest {assertEquals("testVal", returnedItem.get("testAttr").get("S").asText());}--++@Testvoid deleteItemConditionFailedReturnValuesNone() {createOrdersTable("us-east-1");-+ObjectNode order = item("customerId", "1", "orderId", "sort1");ObjectNode key = item("customerId", "1", "orderId", "sort1");service.putItem("Orders", order, "us-east-1");- ConditionalCheckFailedException ex = assertThrows(ConditionalCheckFailedException.class, () ->+ ConditionalCheckFailedException ex = assertThrows(ConditionalCheckFailedException.class, () ->service.deleteItem("Orders", key, "attribute_exists(someAttr)", null, null, "us-east-1", "NONE"));JsonNode stored = service.getItem("Orders", key, "us-east-1");@@ -2443,7 +2489,7 @@ class DynamoDbServiceTest {service.putItem("Orders", order, "us-east-1");- ConditionalCheckFailedException ex = assertThrows(ConditionalCheckFailedException.class, () ->+ ConditionalCheckFailedException ex = assertThrows(ConditionalCheckFailedException.class, () ->service.deleteItem("Orders", key, "attribute_exists(someAttr)", null, null, "us-east-1", "ALL_OLD"));JsonNode stored = service.getItem("Orders", key, "us-east-1");