Merge pull request #3047 from bblommers/dynamodb-batch-keysize-validation

#3046 - DynamoDB - Add Key-size Validation for BatchGetItem
This commit is contained in:
Steve Pulec 2020-06-11 20:52:36 -05:00 committed by GitHub
commit 5b505e535c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 0 deletions

View File

@ -371,6 +371,26 @@ class DynamoHandler(BaseResponse):
results = {"ConsumedCapacity": [], "Responses": {}, "UnprocessedKeys": {}}
# Validation: Can only request up to 100 items at the same time
# Scenario 1: We're requesting more than a 100 keys from a single table
for table_name, table_request in table_batches.items():
if len(table_request["Keys"]) > 100:
return self.error(
"com.amazonaws.dynamodb.v20111205#ValidationException",
"1 validation error detected: Value at 'requestItems."
+ table_name
+ ".member.keys' failed to satisfy constraint: Member must have length less than or equal to 100",
)
# Scenario 2: We're requesting more than a 100 keys across all tables
nr_of_keys_across_all_tables = sum(
[len(req["Keys"]) for _, req in table_batches.items()]
)
if nr_of_keys_across_all_tables > 100:
return self.error(
"com.amazonaws.dynamodb.v20111205#ValidationException",
"Too many items requested for the BatchGetItem call",
)
for table_name, table_request in table_batches.items():
keys = table_request["Keys"]
if self._contains_duplicates(keys):

View File

@ -3038,6 +3038,54 @@ def test_batch_items_returns_all():
]
@mock_dynamodb2
def test_batch_items_throws_exception_when_requesting_100_items_for_single_table():
dynamodb = _create_user_table()
with assert_raises(ClientError) as ex:
dynamodb.batch_get_item(
RequestItems={
"users": {
"Keys": [
{"username": {"S": "user" + str(i)}} for i in range(0, 104)
],
"ConsistentRead": True,
}
}
)
ex.exception.response["Error"]["Code"].should.equal("ValidationException")
msg = ex.exception.response["Error"]["Message"]
msg.should.contain("1 validation error detected: Value")
msg.should.contain(
"at 'requestItems.users.member.keys' failed to satisfy constraint: Member must have length less than or equal to 100"
)
@mock_dynamodb2
def test_batch_items_throws_exception_when_requesting_100_items_across_all_tables():
dynamodb = _create_user_table()
with assert_raises(ClientError) as ex:
dynamodb.batch_get_item(
RequestItems={
"users": {
"Keys": [
{"username": {"S": "user" + str(i)}} for i in range(0, 75)
],
"ConsistentRead": True,
},
"users2": {
"Keys": [
{"username": {"S": "user" + str(i)}} for i in range(0, 75)
],
"ConsistentRead": True,
},
}
)
ex.exception.response["Error"]["Code"].should.equal("ValidationException")
ex.exception.response["Error"]["Message"].should.equal(
"Too many items requested for the BatchGetItem call"
)
@mock_dynamodb2
def test_batch_items_with_basic_projection_expression():
dynamodb = _create_user_table()