mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 14:10:49 -05:00
fix(dynamodb): enforce composite query order (#2471)
* fix(dynamodb): enforce composite query order Closes #2462 * fix(dynamodb): validate legacy composite queries
This commit is contained in:
1 parent
d3871e1f25
commit
e4356def24
4 files changed
+213
-4
No files matched your search
+41
-1
@@ -41,7 +41,8 @@ class DynamoDbAccessPathValidationTest {
|
||||
attribute("createdAt"), attribute("alternate"))
|
||||
.globalSecondaryIndexes(GlobalSecondaryIndex.builder()
|
||||
.indexName("status-index")
|
||||
.keySchema(key("status", KeyType.HASH), key("createdAt", KeyType.RANGE))
|
||||
.keySchema(key("status", KeyType.HASH), key("createdAt", KeyType.RANGE),
|
||||
key("alternate", KeyType.RANGE))
|
||||
.projection(Projection.builder()
|
||||
.projectionType(ProjectionType.INCLUDE)
|
||||
.nonKeyAttributes("summary")
|
||||
@@ -211,6 +212,45 @@ class DynamoDbAccessPathValidationTest {
|
||||
.exclusiveStartKey(invalidStartKey)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
void queryRejectsInvalidCompositeSortKeyConditions() {
|
||||
assertValidationException(() -> ddb.query(request -> request
|
||||
.tableName(TABLE)
|
||||
.indexName("status-index")
|
||||
.keyConditionExpression("#status = :status AND alternate = :alternate")
|
||||
.expressionAttributeNames(Map.of("#status", "status"))
|
||||
.expressionAttributeValues(Map.of(
|
||||
":status", value("open"),
|
||||
":alternate", value("a1")))));
|
||||
|
||||
assertValidationException(() -> ddb.query(request -> request
|
||||
.tableName(TABLE)
|
||||
.indexName("status-index")
|
||||
.keyConditionExpression("#status = :status AND createdAt > :createdAt "
|
||||
+ "AND alternate = :alternate")
|
||||
.expressionAttributeNames(Map.of("#status", "status"))
|
||||
.expressionAttributeValues(Map.of(
|
||||
":status", value("open"),
|
||||
":createdAt", value("2026-01-01"),
|
||||
":alternate", value("a1")))));
|
||||
|
||||
assertValidationException(() -> ddb.query(request -> request
|
||||
.tableName(TABLE)
|
||||
.indexName("status-index")
|
||||
.keyConditions(Map.of(
|
||||
"status", condition(ComparisonOperator.EQ, "open"),
|
||||
"alternate", condition(ComparisonOperator.EQ, "a1")))));
|
||||
|
||||
assertValidationException(() -> ddb.query(request -> request
|
||||
.tableName(TABLE)
|
||||
.indexName("status-index")
|
||||
.keyConditions(Map.of(
|
||||
"status", condition(ComparisonOperator.EQ, "open"),
|
||||
"createdAt", condition(ComparisonOperator.GT, "2026-01-01"),
|
||||
"alternate", condition(ComparisonOperator.EQ, "a1")))));
|
||||
}
|
||||
|
||||
private static void assertValidationException(org.assertj.core.api.ThrowableAssert.ThrowingCallable call) {
|
||||
assertThatThrownBy(call)
|
||||
.isInstanceOf(DynamoDbException.class)
|
||||
|
||||
+33
-2
@@ -18,8 +18,10 @@ import io.github.hectorvent.floci.services.dynamodb.ExpressionEvaluator.TokenTyp
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.AttributeDefinition;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
final class DynamoDbAccessPathValidator {
|
||||
@@ -98,10 +100,11 @@ final class DynamoDbAccessPathValidator {
|
||||
? and.operands() : List.of(root);
|
||||
List<String> partitionKeys = accessPath.partitionKeyNames();
|
||||
Set<String> partitionKeySet = Set.copyOf(partitionKeys);
|
||||
Set<String> sortKeys = Set.copyOf(accessPath.sortKeyNames());
|
||||
List<String> sortKeys = accessPath.sortKeyNames();
|
||||
Set<String> conditionedPartitionKeys = new HashSet<>();
|
||||
String partitionKeyValuePlaceholder = null;
|
||||
Set<String> conditionedAttributes = new HashSet<>();
|
||||
Map<String, Boolean> sortKeyEqualities = new HashMap<>();
|
||||
|
||||
for (Expr condition : conditions) {
|
||||
String attribute = conditionAttribute(condition, names);
|
||||
@@ -121,6 +124,7 @@ final class DynamoDbAccessPathValidator {
|
||||
if (!isSupportedSortKeyCondition(condition)) {
|
||||
throw new AwsException("ValidationException", "Query key condition not supported", 400);
|
||||
}
|
||||
sortKeyEqualities.put(attribute, isEqualityCondition(condition));
|
||||
} else {
|
||||
throw new AwsException("ValidationException", "Query key condition not supported", 400);
|
||||
}
|
||||
@@ -134,9 +138,32 @@ final class DynamoDbAccessPathValidator {
|
||||
throw new AwsException("ValidationException",
|
||||
"Query condition missed key schema element: " + missing, 400);
|
||||
}
|
||||
validateCompositeSortKeyConditions(sortKeys, sortKeyEqualities);
|
||||
return partitionKeyValuePlaceholder;
|
||||
}
|
||||
|
||||
private static void validateCompositeSortKeyConditions(List<String> sortKeys,
|
||||
Map<String, Boolean> equalities) {
|
||||
for (int laterIndex = 1; laterIndex < sortKeys.size(); laterIndex++) {
|
||||
String laterSortKey = sortKeys.get(laterIndex);
|
||||
if (!equalities.containsKey(laterSortKey)) {
|
||||
continue;
|
||||
}
|
||||
for (int priorIndex = 0; priorIndex < laterIndex; priorIndex++) {
|
||||
String priorSortKey = sortKeys.get(priorIndex);
|
||||
if (!Boolean.TRUE.equals(equalities.get(priorSortKey))) {
|
||||
throw validationException("RANGE key attributes " + priorSortKey
|
||||
+ " must have equality conditions specified in the query because a condition is present "
|
||||
+ "on key attribute " + laterSortKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isEqualityCondition(Expr condition) {
|
||||
return condition instanceof CompareExpr compare && compare.op() == TokenType.EQ;
|
||||
}
|
||||
|
||||
private static boolean isPartitionKeyEquality(Expr condition) {
|
||||
return condition instanceof CompareExpr compare
|
||||
&& compare.op() == TokenType.EQ
|
||||
@@ -216,7 +243,8 @@ final class DynamoDbAccessPathValidator {
|
||||
}
|
||||
|
||||
Set<String> partitionKeySet = Set.copyOf(partitionKeys);
|
||||
Set<String> sortKeys = Set.copyOf(accessPath.sortKeyNames());
|
||||
List<String> sortKeys = accessPath.sortKeyNames();
|
||||
Map<String, Boolean> sortKeyEqualities = new HashMap<>();
|
||||
keyConditions.fields().forEachRemaining(entry -> {
|
||||
String attribute = entry.getKey();
|
||||
String operator = entry.getValue().path("ComparisonOperator").asText();
|
||||
@@ -229,6 +257,8 @@ final class DynamoDbAccessPathValidator {
|
||||
|| !LEGACY_SORT_KEY_COMPARATORS.contains(operator)
|
||||
|| ("BETWEEN".equals(operator) ? values != 2 : values != 1)) {
|
||||
throw new AwsException("ValidationException", "Query key condition not supported", 400);
|
||||
} else {
|
||||
sortKeyEqualities.put(attribute, "EQ".equals(operator));
|
||||
}
|
||||
String expectedType = attributeType(table, attribute);
|
||||
entry.getValue().path("AttributeValueList").forEach(value -> {
|
||||
@@ -237,6 +267,7 @@ final class DynamoDbAccessPathValidator {
|
||||
}
|
||||
});
|
||||
});
|
||||
validateCompositeSortKeyConditions(sortKeys, sortKeyEqualities);
|
||||
}
|
||||
|
||||
private static String attributeType(TableDefinition table, String attribute) {
|
||||
|
||||
+54
-1
@@ -38,6 +38,7 @@ class DynamoDbAccessPathValidatorTest {
|
||||
new AttributeDefinition("sk", "S"),
|
||||
new AttributeDefinition("status", "S"),
|
||||
new AttributeDefinition("createdAt", "S"),
|
||||
new AttributeDefinition("sequence", "S"),
|
||||
new AttributeDefinition("alternate", "S"),
|
||||
new AttributeDefinition("tenantId", "S"),
|
||||
new AttributeDefinition("region", "S")));
|
||||
@@ -45,7 +46,8 @@ class DynamoDbAccessPathValidatorTest {
|
||||
new GlobalSecondaryIndex(
|
||||
"status-index",
|
||||
List.of(new KeySchemaElement("status", "HASH"),
|
||||
new KeySchemaElement("createdAt", "RANGE")),
|
||||
new KeySchemaElement("createdAt", "RANGE"),
|
||||
new KeySchemaElement("sequence", "RANGE")),
|
||||
null, "INCLUDE", List.of("summary")),
|
||||
new GlobalSecondaryIndex(
|
||||
"tenant-region-index",
|
||||
@@ -130,6 +132,33 @@ class DynamoDbAccessPathValidatorTest {
|
||||
null, null, names, expressionValues(":status", ":start", ":end")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void acceptsCompositeSortKeyPrefixWithInequalityLast() {
|
||||
assertDoesNotThrow(() -> validateExpression(gsiPath,
|
||||
"status = :status AND createdAt = :createdAt AND sequence >= :sequence"));
|
||||
assertDoesNotThrow(() -> validateExpression(gsiPath,
|
||||
"status = :status AND createdAt BETWEEN :start AND :end"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsSkippedCompositeSortKey() {
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> validateExpression(gsiPath, "status = :status AND sequence = :sequence"));
|
||||
|
||||
assertEquals("RANGE key attributes createdAt must have equality conditions specified in the query "
|
||||
+ "because a condition is present on key attribute sequence", error.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsCompositeSortKeyConditionAfterInequality() {
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> validateExpression(gsiPath,
|
||||
"status = :status AND createdAt > :createdAt AND sequence = :sequence"));
|
||||
|
||||
assertEquals("RANGE key attributes createdAt must have equality conditions specified in the query "
|
||||
+ "because a condition is present on key attribute sequence", error.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsMissingPartitionKeyCondition() {
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
@@ -194,6 +223,30 @@ class DynamoDbAccessPathValidatorTest {
|
||||
table, tablePath, missingPartitionKey, null, null, null, null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validatesLegacyCompositeSortKeyOrder() {
|
||||
ObjectNode valid = mapper.createObjectNode();
|
||||
valid.set("status", legacyCondition("EQ", attributeValues("open")));
|
||||
valid.set("createdAt", legacyCondition("EQ", attributeValues("2026-01-01")));
|
||||
valid.set("sequence", legacyCondition("GE", attributeValues("1")));
|
||||
assertDoesNotThrow(() -> DynamoDbAccessPathValidator.validateQuery(
|
||||
table, gsiPath, valid, null, null, null, null, null));
|
||||
|
||||
ObjectNode skipped = mapper.createObjectNode();
|
||||
skipped.set("status", legacyCondition("EQ", attributeValues("open")));
|
||||
skipped.set("sequence", legacyCondition("EQ", attributeValues("1")));
|
||||
AwsException skippedError = assertThrows(AwsException.class,
|
||||
() -> DynamoDbAccessPathValidator.validateQuery(
|
||||
table, gsiPath, skipped, null, null, null, null, null));
|
||||
assertEquals("RANGE key attributes createdAt must have equality conditions specified in the query "
|
||||
+ "because a condition is present on key attribute sequence", skippedError.getMessage());
|
||||
|
||||
ObjectNode nonFinalInequality = valid.deepCopy();
|
||||
nonFinalInequality.set("createdAt", legacyCondition("GT", attributeValues("2026-01-01")));
|
||||
assertThrows(AwsException.class, () -> DynamoDbAccessPathValidator.validateQuery(
|
||||
table, gsiPath, nonFinalInequality, null, null, null, null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsSelectedKeysInQueryFilters() {
|
||||
assertThrows(AwsException.class, () -> DynamoDbAccessPathValidator.validateQuery(
|
||||
|
||||
+85
@@ -280,4 +280,89 @@ class DynamoDbCompositeSortKeyQueryIntegrationTest {
|
||||
"2026-07-14T00:00:03Z"), collected,
|
||||
"Expected all rows once, in full composite order");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void queryRejectsSkippedCompositeSortKey() {
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.Query")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "%s",
|
||||
"IndexName": "%s",
|
||||
"KeyConditionExpression": "memberName = :pk AND createdAt >= :from",
|
||||
"ExpressionAttributeValues": {
|
||||
":pk": {"S": "alice"},
|
||||
":from": {"S": "2026-01-01T00:00:00Z"}
|
||||
}
|
||||
}
|
||||
""".formatted(TABLE, INDEX))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"))
|
||||
.body("message", equalTo("RANGE key attributes state must have equality conditions specified "
|
||||
+ "in the query because a condition is present on key attribute createdAt"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void queryRejectsCompositeSortKeyConditionAfterInequality() {
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.Query")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "%s",
|
||||
"IndexName": "%s",
|
||||
"KeyConditionExpression": "memberName = :pk AND #st > :st AND createdAt = :createdAt",
|
||||
"ExpressionAttributeNames": {"#st": "state"},
|
||||
"ExpressionAttributeValues": {
|
||||
":pk": {"S": "alice"},
|
||||
":st": {"S": "ACTIVE"},
|
||||
":createdAt": {"S": "2026-07-14T00:00:01Z"}
|
||||
}
|
||||
}
|
||||
""".formatted(TABLE, INDEX))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"))
|
||||
.body("message", equalTo("RANGE key attributes state must have equality conditions specified "
|
||||
+ "in the query because a condition is present on key attribute createdAt"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
void legacyQueryRejectsInvalidCompositeSortKeyConditions() {
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.Query")
|
||||
.contentType(DYNAMODB_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "%s",
|
||||
"IndexName": "%s",
|
||||
"KeyConditions": {
|
||||
"memberName": {
|
||||
"ComparisonOperator": "EQ",
|
||||
"AttributeValueList": [{"S": "alice"}]
|
||||
},
|
||||
"createdAt": {
|
||||
"ComparisonOperator": "GE",
|
||||
"AttributeValueList": [{"S": "2026-01-01T00:00:00Z"}]
|
||||
}
|
||||
}
|
||||
}
|
||||
""".formatted(TABLE, INDEX))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"))
|
||||
.body("message", equalTo("RANGE key attributes state must have equality conditions specified "
|
||||
+ "in the query because a condition is present on key attribute createdAt"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user