mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(dynamodb): cancel TransactWriteItems on a key that does not match the schema (#3674)
* fix(dynamodb): cancel TransactWriteItems on a key that does not match the schema AWS checks every member's key against the table schema before it looks for duplicate items or evaluates a condition. A wrong type, a non-scalar value or a missing key attribute cancels the transaction. The first such member gets a ValidationError reason and every other member gets None. An empty key value still fails the whole request. Floci returned a top-level ValidationException. An Update that sets an index key to the wrong type now cancels as well, in both TransactWriteItems and ExecuteTransaction. Checked against DynamoDB in eu-west-2. * fix(dynamodb): look up an active table in the transaction key check The key check read the table through describeTable. That call counts every item in the table on each member, and it accepts a table that is still CREATING. DynamoDB answers ResourceNotFoundException "Requested resource not found" for a CREATING table even when a member key is wrong. Use the same active table lookup as the other transaction steps. Checked against DynamoDB in eu-west-2.
This commit is contained in:
1 parent
03d33350a7
commit
487cacb3bc
4 files changed
+284
-27
No files matched your search
+182
@@ -1679,4 +1679,186 @@ class DynamoDbJsonHandlerTest {
|
||||
assertEquals("Nesting Levels have exceeded supported limits", reasons.get(1).get("Message").asText());
|
||||
assertNull(service.getItem("Users", item("userId", "u2"), "eu-west-1"));
|
||||
}
|
||||
|
||||
private ObjectNode transactMember(String action, String tableName, String field, ObjectNode value) {
|
||||
ObjectNode op = mapper.createObjectNode();
|
||||
op.put("TableName", tableName);
|
||||
op.set(field, value);
|
||||
ObjectNode member = mapper.createObjectNode();
|
||||
member.set(action, op);
|
||||
return member;
|
||||
}
|
||||
|
||||
private ObjectNode transactWriteOf(ObjectNode... members) {
|
||||
ArrayNode items = mapper.createArrayNode();
|
||||
for (ObjectNode member : members) {
|
||||
items.add(member);
|
||||
}
|
||||
ObjectNode request = mapper.createObjectNode();
|
||||
request.set("TransactItems", items);
|
||||
return request;
|
||||
}
|
||||
|
||||
private String onlyValidationErrorMessage(Response response) {
|
||||
assertEquals(400, response.getStatus());
|
||||
JsonNode body = mapper.convertValue(response.getEntity(), JsonNode.class);
|
||||
assertEquals("TransactionCanceledException", body.get("__type").asText());
|
||||
assertEquals("Transaction cancelled, please refer cancellation reasons for specific reasons [ValidationError]",
|
||||
body.get("message").asText());
|
||||
JsonNode reason = body.get("CancellationReasons").get(0);
|
||||
assertEquals("ValidationError", reason.get("Code").asText());
|
||||
return reason.get("Message").asText();
|
||||
}
|
||||
|
||||
@Test
|
||||
void transactWriteItemsCancelsOnAPutKeyOfTheWrongType() throws Exception {
|
||||
createUsersTable("eu-west-1");
|
||||
ObjectNode item = mapper.createObjectNode();
|
||||
item.set("userId", attributeValue("N", "5"));
|
||||
|
||||
Response response = handler.handle("TransactWriteItems",
|
||||
transactWriteOf(transactMember("Put", "Users", "Item", item)), "eu-west-1");
|
||||
|
||||
assertEquals("One or more parameter values were invalid: Type mismatch for key userId expected: S actual: N",
|
||||
onlyValidationErrorMessage(response));
|
||||
}
|
||||
|
||||
@Test
|
||||
void transactWriteItemsCancelsOnAPutItemMissingItsKey() throws Exception {
|
||||
createUsersTable("eu-west-1");
|
||||
|
||||
Response response = handler.handle("TransactWriteItems",
|
||||
transactWriteOf(transactMember("Put", "Users", "Item", item("name", "x"))), "eu-west-1");
|
||||
|
||||
assertEquals("One or more parameter values were invalid: Missing the key userId in the item",
|
||||
onlyValidationErrorMessage(response));
|
||||
}
|
||||
|
||||
@Test
|
||||
void transactWriteItemsCancelsOnADeleteKeyOfTheWrongTypeWithTheSchemaMessage() throws Exception {
|
||||
createUsersTable("eu-west-1");
|
||||
ObjectNode key = mapper.createObjectNode();
|
||||
key.set("userId", attributeValue("N", "5"));
|
||||
|
||||
Response response = handler.handle("TransactWriteItems",
|
||||
transactWriteOf(transactMember("Delete", "Users", "Key", key)), "eu-west-1");
|
||||
|
||||
assertEquals("The provided key element does not match the schema", onlyValidationErrorMessage(response));
|
||||
}
|
||||
|
||||
@Test
|
||||
void transactWriteItemsCancelsOnAConditionCheckKeyMissingItsAttribute() throws Exception {
|
||||
createUsersTable("eu-west-1");
|
||||
|
||||
Response response = handler.handle("TransactWriteItems",
|
||||
transactWriteOf(transactMember("ConditionCheck", "Users", "Key", mapper.createObjectNode())),
|
||||
"eu-west-1");
|
||||
|
||||
assertEquals("The provided key element does not match the schema", onlyValidationErrorMessage(response));
|
||||
}
|
||||
|
||||
// Checked against DynamoDB in eu-west-2: the failing condition and the duplicate pair report None.
|
||||
@Test
|
||||
void transactWriteItemsCancelsAtTheFirstKeyMismatchBeforeConditionsAndDuplicates() throws Exception {
|
||||
createUsersTable("eu-west-1");
|
||||
ObjectNode check = transactMember("ConditionCheck", "Users", "Key", item("userId", "u9"));
|
||||
((ObjectNode) check.get("ConditionCheck")).put("ConditionExpression", "attribute_exists(userId)");
|
||||
ObjectNode firstWrong = mapper.createObjectNode();
|
||||
firstWrong.set("userId", attributeValue("N", "5"));
|
||||
ObjectNode secondWrong = mapper.createObjectNode();
|
||||
secondWrong.set("userId", attributeValue("N", "6"));
|
||||
|
||||
Response response = handler.handle("TransactWriteItems", transactWriteOf(check,
|
||||
transactMember("Put", "Users", "Item", item("userId", "u1")),
|
||||
transactMember("Put", "Users", "Item", item("userId", "u1")),
|
||||
transactMember("Put", "Users", "Item", firstWrong),
|
||||
transactMember("Put", "Users", "Item", secondWrong)), "eu-west-1");
|
||||
|
||||
assertEquals(400, response.getStatus());
|
||||
JsonNode body = mapper.convertValue(response.getEntity(), JsonNode.class);
|
||||
assertEquals("Transaction cancelled, please refer cancellation reasons for specific reasons "
|
||||
+ "[None, None, None, ValidationError, None]", body.get("message").asText());
|
||||
assertEquals("One or more parameter values were invalid: Type mismatch for key userId expected: S actual: N",
|
||||
body.get("CancellationReasons").get(3).get("Message").asText());
|
||||
assertNull(service.getItem("Users", item("userId", "u1"), "eu-west-1"));
|
||||
}
|
||||
|
||||
// Checked against DynamoDB in eu-west-2: a CREATING table is not found before any key is checked.
|
||||
@Test
|
||||
void transactWriteItemsAnswersResourceNotFoundForACreatingTableBeforeTheKeyCheck() {
|
||||
createUsersTable("eu-west-1").setTableStatus("CREATING");
|
||||
ObjectNode wrong = mapper.createObjectNode();
|
||||
wrong.set("userId", attributeValue("N", "5"));
|
||||
ObjectNode request = transactWriteOf(transactMember("Put", "Users", "Item", wrong));
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> handler.handle("TransactWriteItems", request, "eu-west-1"));
|
||||
assertEquals("ResourceNotFoundException", ex.getErrorCode());
|
||||
assertEquals("Requested resource not found", ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void transactWriteItemsFailsTheRequestOnAnEmptyKeyAheadOfAKeyMismatch() {
|
||||
createUsersTable("eu-west-1");
|
||||
ObjectNode wrong = mapper.createObjectNode();
|
||||
wrong.set("userId", attributeValue("N", "5"));
|
||||
ObjectNode request = transactWriteOf(
|
||||
transactMember("Put", "Users", "Item", item("userId", "")),
|
||||
transactMember("Put", "Users", "Item", wrong));
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> handler.handle("TransactWriteItems", request, "eu-west-1"));
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertEquals("One or more parameter values are not valid. The AttributeValue for a key attribute "
|
||||
+ "cannot contain an empty string value. Key: userId", ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void transactWriteItemsCancelsOnAPutIndexKeyOfTheWrongType() throws Exception {
|
||||
createProvisionedTableWithGsi("Movies", "eu-west-1");
|
||||
ObjectNode item = item("id", "m1");
|
||||
item.set("title", attributeValue("N", "1"));
|
||||
|
||||
Response response = handler.handle("TransactWriteItems",
|
||||
transactWriteOf(transactMember("Put", "Movies", "Item", item)), "eu-west-1");
|
||||
|
||||
assertEquals("One or more parameter values were invalid: Type mismatch for Index Key title "
|
||||
+ "Expected: S Actual: N IndexName: TitleIndex", onlyValidationErrorMessage(response));
|
||||
}
|
||||
|
||||
@Test
|
||||
void transactWriteItemsCancelsOnAnUpdateSettingAnIndexKeyOfTheWrongType() throws Exception {
|
||||
createProvisionedTableWithGsi("Movies", "eu-west-1");
|
||||
ObjectNode update = transactMember("Update", "Movies", "Key", item("id", "m1"));
|
||||
((ObjectNode) update.get("Update")).put("UpdateExpression", "SET title = :t");
|
||||
((ObjectNode) update.get("Update")).set("ExpressionAttributeValues",
|
||||
mapper.createObjectNode().set(":t", attributeValue("N", "1")));
|
||||
|
||||
Response response = handler.handle("TransactWriteItems", transactWriteOf(update), "eu-west-1");
|
||||
|
||||
assertEquals("One or more parameter values were invalid: Type mismatch for Index Key title "
|
||||
+ "Expected: S Actual: N IndexName: TitleIndex", onlyValidationErrorMessage(response));
|
||||
assertNull(service.getItem("Movies", item("id", "m1"), "eu-west-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void executeTransactionCancelsOnAnUpdateSettingAnIndexKeyOfTheWrongType() throws Exception {
|
||||
createProvisionedTableWithGsi("Movies", "eu-west-1");
|
||||
ObjectNode putRequest = mapper.createObjectNode();
|
||||
putRequest.put("TableName", "Movies");
|
||||
putRequest.set("Item", item("id", "m1", "title", "Old"));
|
||||
handler.handle("PutItem", putRequest, "eu-west-1");
|
||||
ObjectNode statement = mapper.createObjectNode();
|
||||
statement.put("Statement", "UPDATE \"Movies\" SET title = ? WHERE id = ?");
|
||||
statement.set("Parameters", mapper.createArrayNode()
|
||||
.add(attributeValue("N", "1")).add(attributeValue("S", "m1")));
|
||||
ObjectNode request = mapper.createObjectNode();
|
||||
request.set("TransactStatements", mapper.createArrayNode().add(statement));
|
||||
|
||||
Response response = handler.handle("ExecuteTransaction", request, "eu-west-1");
|
||||
|
||||
assertEquals("One or more parameter values were invalid: Type mismatch for Index Key title "
|
||||
+ "Expected: S Actual: N IndexName: TitleIndex", onlyValidationErrorMessage(response));
|
||||
assertEquals("Old", service.getItem("Movies", item("id", "m1"), "eu-west-1").get("title").get("S").asText());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user