Fine-grained access control in DynamoDB - Time & Space Complexity
When using fine-grained access control in DynamoDB, we want to know how the time to check permissions grows as data or rules increase.
We ask: How does the cost of enforcing detailed access rules change with more data or conditions?
Analyze the time complexity of the following DynamoDB policy check snippet.
// Example: Checking user access to items with attribute filters
const params = {
TableName: "Documents",
KeyConditionExpression: "UserId = :uid",
FilterExpression: "AccessLevel = :level",
ExpressionAttributeValues: {
":uid": userId,
":level": userAccessLevel
}
};
const result = await dynamodb.query(params).promise();
// Returns items user can access based on fine-grained rules
This code queries items for a user and filters them by access level to enforce fine-grained control.
Look for repeated work in the query and filtering process.
- Primary operation: Querying items matching user ID, then filtering by access level.
- How many times: Each item for the user is checked once against the filter condition.
As the number of items for a user grows, the system checks each item's access level.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The number of checks grows directly with the number of items to filter.
Time Complexity: O(n)
This means the time to enforce fine-grained access grows linearly with the number of items checked.
[X] Wrong: "Filtering by access level happens instantly regardless of item count."
[OK] Correct: Each item must be checked against the access rule, so more items mean more work.
Understanding how access control scales helps you design systems that stay fast as data grows, a key skill in real projects.
"What if we added an index on AccessLevel to speed filtering? How would the time complexity change?"
Practice
Solution
Step 1: Understand fine-grained access control concept
Fine-grained access control limits access to individual items or attributes in a DynamoDB table.Step 2: Identify the main purpose
This control is used to restrict access based on keys, ensuring users see only their data.Final Answer:
To restrict access to specific items based on keys -> Option CQuick Check:
Fine-grained access control = restrict by keys [OK]
- Confusing access control with encryption
- Thinking it improves query speed
- Assuming it manages backups
Solution
Step 1: Identify IAM condition keys for DynamoDB
DynamoDB uses specific condition keys in IAM policies to control access.Step 2: Match condition key for fine-grained access
The key dynamodb:LeadingKeys restricts access based on partition key values.Final Answer:
dynamodb:LeadingKeys -> Option AQuick Check:
Fine-grained access uses dynamodb:LeadingKeys [OK]
- Choosing TableName which controls table-level access
- Confusing Attributes with keys
- Selecting unrelated encryption keys
{
"Effect": "Allow",
"Action": "dynamodb:GetItem",
"Resource": "arn:aws:dynamodb:us-east-1:123456789012:table/Orders",
"Condition": {
"ForAllValues:StringEquals": {
"dynamodb:LeadingKeys": ["user123"]
}
}
}Solution
Step 1: Analyze the IAM policy condition
The condition restricts access to items where the partition key matches 'user123'.Step 2: Determine accessible items
Only items with partition key 'user123' are accessible; others are denied.Final Answer:
Only items with partition key 'user123' -> Option BQuick Check:
Condition limits to 'user123' keys [OK]
- Assuming access to all items
- Thinking policy denies all access
- Ignoring the condition block
{
"Effect": "Allow",
"Action": "dynamodb:GetItem",
"Resource": "arn:aws:dynamodb:us-east-1:123456789012:table/Orders",
"Condition": {
"StringEquals": {
"dynamodb:LeadingKeys": "user456"
}
}
}What is the likely error?
Solution
Step 1: Check condition syntax for dynamodb:LeadingKeys
The condition expects an array with ForAllValues:StringEquals, not a single string with StringEquals.Step 2: Understand impact of incorrect condition
Using StringEquals with a string causes the condition to be ignored, allowing broader access.Final Answer:
Condition should use ForAllValues:StringEquals with an array -> Option DQuick Check:
Use ForAllValues:StringEquals and array for LeadingKeys [OK]
- Using StringEquals with a string instead of array
- Incorrect ARN format
- Confusing action types
- Changing Effect to Deny unnecessarily
Solution
Step 1: Identify correct condition operator for exact key match
For fine-grained access, ForAllValues:StringEquals with an array ensures all keys match exactly.Step 2: Evaluate options for correct syntax and semantics
{ "ForAllValues:StringEquals": { "dynamodb:LeadingKeys": ["alice"] } } uses correct operator and array format; others use wrong keys or operators.Final Answer:
{ "ForAllValues:StringEquals": { "dynamodb:LeadingKeys": ["alice"] } } -> Option AQuick Check:
Exact key match needs ForAllValues:StringEquals with array [OK]
- Using dynamodb:Attributes instead of LeadingKeys
- Using StringEquals without array
- Using StringLike which allows wildcards
Ready to prove it?
Pass the test and this pattern moves unknown
