mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(dynamodb): measure a table name the way each DynamoDB call does (#8538)
A bad DynamoDB table name now gets the errors each call gives for it. The table calls, and the older validator of a deeply nested PutItem, UpdateItem or DeleteItem, count the name in UTF-16 units and list every problem, the length first. GetItem, PutItem, DeleteItem, Query and Scan count characters and report the first problem. UpdateItem counts characters and lists the pattern before the length. A name inside a table ARN gets only its first problem, counted in UTF-16 units, with the whole ARN quoted and "Table name of ARN must".
This commit is contained in:
1 parent
e305ced3bd
commit
678bd347af
4 files changed
+139
-15
No files matched your search
@@ -6,6 +6,9 @@ import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
@@ -154,6 +157,68 @@ class DynamoDbTableNamesTest {
|
||||
() -> DynamoDbTableNames.resolve(input)).getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', textBlock = """
|
||||
😀😀 | pattern | short
|
||||
😀a | pattern | short
|
||||
a! | short, pattern | short
|
||||
éé | short, pattern | short
|
||||
ab | short | short
|
||||
""")
|
||||
void tableCallsCountUtf16UnitsAndListEveryProblem(String name, String tableCall, String itemCall) {
|
||||
assertEquals(nameErrors(name, tableCall), assertThrows(AwsException.class,
|
||||
() -> DynamoDbTableNames.resolve(name)).getMessage());
|
||||
assertEquals(nameErrors(name, tableCall), assertThrows(AwsException.class,
|
||||
() -> DynamoDbTableNames.requireShortName(name)).getMessage());
|
||||
assertEquals(nameErrors(name, itemCall), assertThrows(AwsException.class,
|
||||
() -> DynamoDbTableNames.resolveForItemCall(name)).getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anEmojiCountsTwiceTowardTheLongestTableName() {
|
||||
String name = "😀".repeat(128);
|
||||
assertEquals(nameErrors(name, "long, pattern"), assertThrows(AwsException.class,
|
||||
() -> DynamoDbTableNames.resolve(name)).getMessage());
|
||||
assertEquals(nameErrors(name, "pattern"), assertThrows(AwsException.class,
|
||||
() -> DynamoDbTableNames.resolveForItemCall(name)).getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', textBlock = """
|
||||
a! | have length greater than or equal to 3
|
||||
😀😀 | satisfy regular expression pattern: [a-zA-Z0-9_.-]+
|
||||
""")
|
||||
void everyCallReportsTheFirstProblemOfTheNameInAnArn(String name, String rule) {
|
||||
String arn = "arn:aws:dynamodb:eu-west-2:123456789012:table/" + name;
|
||||
String expected = "1 validation error detected: Value '" + arn
|
||||
+ "' at 'tableName' failed to satisfy constraint: Table name of ARN must " + rule;
|
||||
assertEquals(expected, assertThrows(AwsException.class, () -> DynamoDbTableNames.resolve(arn)).getMessage());
|
||||
assertEquals(expected, assertThrows(AwsException.class,
|
||||
() -> DynamoDbTableNames.resolveForItemCall(arn)).getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anEmojiCountsTwiceInTheNameOfAnArn() {
|
||||
String arn = "arn:aws:dynamodb:eu-west-2:123456789012:table/" + "😀".repeat(128);
|
||||
assertEquals("1 validation error detected: Value '" + arn + "' at 'tableName' failed to satisfy constraint: "
|
||||
+ "Table name of ARN must have length less than or equal to 255", assertThrows(AwsException.class,
|
||||
() -> DynamoDbTableNames.resolveForItemCall(arn)).getMessage());
|
||||
}
|
||||
|
||||
static String nameErrors(String name, String problems) {
|
||||
List<String> errors = new ArrayList<>();
|
||||
for (String problem : problems.split(", ")) {
|
||||
String rule = switch (problem) {
|
||||
case "short" -> "Member must have length greater than or equal to 3";
|
||||
case "long" -> "Member must have length less than or equal to 255";
|
||||
default -> "Member must satisfy regular expression pattern: [a-zA-Z0-9_.-]+";
|
||||
};
|
||||
errors.add("Value '" + name + "' at 'tableName' failed to satisfy constraint: " + rule);
|
||||
}
|
||||
return errors.size() + (errors.size() == 1 ? " validation error detected: " : " validation errors detected: ")
|
||||
+ String.join("; ", errors);
|
||||
}
|
||||
|
||||
private static String emptyToNull(String s) {
|
||||
return (s == null || s.isEmpty()) ? null : s;
|
||||
}
|
||||
|
||||
@@ -3447,6 +3447,24 @@ class NativeDynamoDbJsonHandlerTest {
|
||||
assertEquals(message, ex.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', textBlock = """
|
||||
DescribeTable | {} | pattern
|
||||
UpdateTable | {"BillingMode":"PAY_PER_REQUEST"} | pattern
|
||||
PutItem | {"Item":{"userId":{"S":"u1"}},"Expected":{"d":{"Value":"DEEP"}}} | pattern
|
||||
GetItem | {"Key":{"userId":{"S":"u1"}}} | short
|
||||
Query | {"KeyConditionExpression":"userId = :v","ExpressionAttributeValues":{":v":{"S":"u1"}}} | short
|
||||
UpdateItem | {"Key":{"userId":{"S":"u1"}}} | pattern, short
|
||||
""")
|
||||
void eachCallMeasuresATableNameItsOwnWay(String operation, String request, String problems) throws Exception {
|
||||
ObjectNode body = (ObjectNode) mapper.readTree(
|
||||
request.replace("\"DEEP\"", mapper.writeValueAsString(nestedMaps(63))));
|
||||
body.put("TableName", "😀😀");
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () -> handler.handle(operation, body, "eu-west-1"));
|
||||
assertEquals(DynamoDbTableNamesTest.nameErrors("😀😀", problems), ex.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', quoteCharacter = '`', textBlock = """
|
||||
61 | {"TransactItems":[{},{"Delete":{"TableName":"bad!name","Key":{}}}]} | Invalid Request: TransactWriteRequest should contain Delete or Put or Update request
|
||||
|
||||
Reference in new issue
Block a user