Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(dynamodb): cap GSI multi-attribute key parts at 4 attributes (#2475)

Matheus Oliveira13f68670d770
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.java
index 08e021ecaa..451941d02b 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
@@ -88,6 +88,8 @@ public class DynamoDbService {
private final ConcurrentHashMap<String, IdempotencyEntry> txIdempotency = new ConcurrentHashMap<>();
private static final long TX_IDEMPOTENCY_TTL_NANOS = java.time.Duration.ofMinutes(10).toNanos();
+ private static final int MAX_MULTI_ATTRIBUTE_KEY_PART_SIZE = 4;
+
private record IdempotencyEntry(String requestHash, long insertedAtNanos) {}
private final RegionResolver regionResolver;
private final ObjectMapper objectMapper;
@@ -314,6 +316,7 @@ public class DynamoDbService {
if (gsis != null && !gsis.isEmpty()) {
for (GlobalSecondaryIndex gsi : gsis) {
+ validateGsiKeySchemaArity(gsi);
gsi.setIndexArn(table.getTableArn() + "/index/" + gsi.getIndexName());
}
table.setGlobalSecondaryIndexes(new ArrayList<>(gsis));
@@ -344,6 +347,23 @@ public class DynamoDbService {
return table;
}
+ private void validateGsiKeySchemaArity(GlobalSecondaryIndex gsi) {
+ long hashCount = gsi.getKeySchema().stream().filter(k -> "HASH".equals(k.getKeyType())).count();
+ long rangeCount = gsi.getKeySchema().stream().filter(k -> "RANGE".equals(k.getKeyType())).count();
+ if (hashCount > MAX_MULTI_ATTRIBUTE_KEY_PART_SIZE) {
+ throw new AwsException("ValidationException",
+ "One or more parameter values were invalid: Global secondary index "
+ + gsi.getIndexName() + " must not have more than "
+ + MAX_MULTI_ATTRIBUTE_KEY_PART_SIZE + " partition key (HASH) attributes", 400);
+ }
+ if (rangeCount > MAX_MULTI_ATTRIBUTE_KEY_PART_SIZE) {
+ throw new AwsException("ValidationException",
+ "One or more parameter values were invalid: Global secondary index "
+ + gsi.getIndexName() + " must not have more than "
+ + MAX_MULTI_ATTRIBUTE_KEY_PART_SIZE + " sort key (RANGE) attributes", 400);
+ }
+ }
+
public TableDefinition describeTable(String tableName, String region) {
String canonicalTableName = canonicalTableName(region, tableName);
String storageKey = regionKey(region, canonicalTableName);
@@ -1327,6 +1347,7 @@ public class DynamoDbService {
"Attribute: " + k.getAttributeName() + " is not defined in AttributeDefinitions", 400);
}
}
+ validateGsiKeySchemaArity(newGsi);
}
for (String gsiName : gsiDeletes) {
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.java
index 3da2b62c43..579d81ba7d 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
@@ -146,6 +146,113 @@ class DynamoDbServiceTest {
assertEquals(400, ex.getHttpStatus());
}
+ @Test
+ void createTableRejectsGsiWithMoreThanFourSortKeyAttributes() {
+ GlobalSecondaryIndex gsi = new GlobalSecondaryIndex(
+ "GSI1",
+ List.of(
+ new KeySchemaElement("PK", "HASH"),
+ new KeySchemaElement("A", "RANGE"),
+ new KeySchemaElement("B", "RANGE"),
+ new KeySchemaElement("C", "RANGE"),
+ new KeySchemaElement("D", "RANGE"),
+ new KeySchemaElement("E", "RANGE")),
+ null, "ALL", null);
+
+ AwsException ex = assertThrows(AwsException.class, () -> service.createTable("gsi-too-many-sort-keys",
+ List.of(new KeySchemaElement("PK", "HASH")),
+ List.of(
+ new AttributeDefinition("PK", "S"),
+ new AttributeDefinition("A", "S"),
+ new AttributeDefinition("B", "S"),
+ new AttributeDefinition("C", "S"),
+ new AttributeDefinition("D", "S"),
+ new AttributeDefinition("E", "S")),
+ 5L, 5L, List.of(gsi), "eu-west-1"));
+
+ assertEquals("ValidationException", ex.getErrorCode());
+ assertEquals(400, ex.getHttpStatus());
+ }
+
+ @Test
+ void createTableRejectsGsiWithMoreThanFourPartitionKeyAttributes() {
+ GlobalSecondaryIndex gsi = new GlobalSecondaryIndex(
+ "GSI1",
+ List.of(
+ new KeySchemaElement("A", "HASH"),
+ new KeySchemaElement("B", "HASH"),
+ new KeySchemaElement("C", "HASH"),
+ new KeySchemaElement("D", "HASH"),
+ new KeySchemaElement("E", "HASH")),
+ null, "ALL", null);
+
+ AwsException ex = assertThrows(AwsException.class, () -> service.createTable("gsi-too-many-hash-keys",
+ List.of(new KeySchemaElement("PK", "HASH")),
+ List.of(
+ new AttributeDefinition("PK", "S"),
+ new AttributeDefinition("A", "S"),
+ new AttributeDefinition("B", "S"),
+ new AttributeDefinition("C", "S"),
+ new AttributeDefinition("D", "S"),
+ new AttributeDefinition("E", "S")),
+ 5L, 5L, List.of(gsi), "eu-west-1"));
+
+ assertEquals("ValidationException", ex.getErrorCode());
+ assertEquals(400, ex.getHttpStatus());
+ }
+
+ @Test
+ void updateTableRejectsGsiCreateWithMoreThanFourSortKeyAttributes() {
+ createUsersTable("eu-west-1");
+ GlobalSecondaryIndex gsi = new GlobalSecondaryIndex(
+ "GSI1",
+ List.of(
+ new KeySchemaElement("userId", "HASH"),
+ new KeySchemaElement("A", "RANGE"),
+ new KeySchemaElement("B", "RANGE"),
+ new KeySchemaElement("C", "RANGE"),
+ new KeySchemaElement("D", "RANGE"),
+ new KeySchemaElement("E", "RANGE")),
+ null, "ALL", null);
+ List<AttributeDefinition> newAttrs = List.of(
+ new AttributeDefinition("A", "S"), new AttributeDefinition("B", "S"),
+ new AttributeDefinition("C", "S"), new AttributeDefinition("D", "S"),
+ new AttributeDefinition("E", "S"));
+
+ AwsException ex = assertThrows(AwsException.class, () -> service.updateTable(
+ "Users", null, null, List.of(gsi), List.of(), newAttrs, "eu-west-1"));
+
+ assertEquals("ValidationException", ex.getErrorCode());
+ assertEquals(400, ex.getHttpStatus());
+ }
+
+ @Test
+ void updateTableRejectsGsiArityWithoutApplyingThroughputChange() {
+ String region = "eu-west-1";
+ createUsersTable(region);
+ GlobalSecondaryIndex invalidGsi = new GlobalSecondaryIndex(
+ "GSI1",
+ List.of(
+ new KeySchemaElement("A", "HASH"),
+ new KeySchemaElement("B", "HASH"),
+ new KeySchemaElement("C", "HASH"),
+ new KeySchemaElement("D", "HASH"),
+ new KeySchemaElement("E", "HASH")),
+ null, "ALL", null);
+ List<AttributeDefinition> newAttrs = List.of(
+ new AttributeDefinition("A", "S"), new AttributeDefinition("B", "S"),
+ new AttributeDefinition("C", "S"), new AttributeDefinition("D", "S"),
+ new AttributeDefinition("E", "S"));
+
+ assertThrows(AwsException.class, () -> service.updateTable(
+ "Users", 10L, 10L, List.of(invalidGsi), List.of(), newAttrs, region));
+
+ TableDefinition after = service.describeTable("Users", region);
+ assertEquals(5L, after.getProvisionedThroughput().getReadCapacityUnits());
+ assertEquals(5L, after.getProvisionedThroughput().getWriteCapacityUnits());
+ assertTrue(after.getGlobalSecondaryIndexes().isEmpty());
+ }
+
@Test
void describeTable() {
String region = "eu-west-1";