mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -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
+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) {
|
||||
|
||||
Reference in new issue
Block a user