mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(iam): check DynamoDB transaction item permissions (#5576)
* fix(iam): check DynamoDB transaction item permissions * test(iam): cover transaction attribute and item permissions * test(iam): cover transaction policy boundaries * fix(iam): bind transaction permissions to the accessed table * fix(dynamodb): preserve omitted transaction return values * docs(iam): clarify transaction return-value conditions
This commit is contained in:
1 parent
e52ce672fe
commit
69bbd2242b
8 files changed
+901
-7
No files matched your search
@@ -5,9 +5,11 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbConditionKeys;
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbFacade;
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbTableNames;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Tag;
|
||||
import io.github.hectorvent.floci.services.iam.ResourceArnBuilder;
|
||||
import io.github.hectorvent.floci.services.s3.S3Service;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
@@ -561,6 +563,61 @@ public class IamConditionContextResolver {
|
||||
return conditions.isEmpty() ? null : conditions;
|
||||
}
|
||||
|
||||
/** One transaction item keeps its action, table and condition keys together. */
|
||||
record DynamoDbTransactionItem(String action, String resource, Map<String, List<String>> context) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves every recognized item before the handler can mutate anything. Unknown envelope fields
|
||||
* cannot turn item checks back into a transaction-verb permission check.
|
||||
*/
|
||||
List<DynamoDbTransactionItem> dynamoDbTransactionItems(String action, ContainerRequestContext ctx,
|
||||
String region, String accountId) {
|
||||
boolean read = "dynamodb:TransactGetItems".equals(action);
|
||||
if (!read && !"dynamodb:TransactWriteItems".equals(action)) {
|
||||
return List.of();
|
||||
}
|
||||
JsonNode body = bufferedJsonBody(ctx);
|
||||
JsonNode items = body == null ? null : body.get("TransactItems");
|
||||
if (items == null || !items.isArray()) {
|
||||
return List.of();
|
||||
}
|
||||
List<DynamoDbTransactionItem> checks = new ArrayList<>();
|
||||
List<String> operations = read ? List.of("Get") : List.of("Put", "Delete", "Update", "ConditionCheck");
|
||||
for (JsonNode item : items) {
|
||||
for (String operation : operations) {
|
||||
JsonNode request = item.get(operation);
|
||||
if (request == null || !request.isObject()) {
|
||||
continue;
|
||||
}
|
||||
String tableName = targetTableName(request);
|
||||
if (tableName == null) {
|
||||
continue;
|
||||
}
|
||||
// Execution resolves ARN inputs within the request account; authorize that same table.
|
||||
String executionTable = DynamoDbTableNames.resolveWithRegion(tableName, region).name();
|
||||
String itemAction = "dynamodb:" + operation + "Item";
|
||||
DynamoDbConditionKeys.Result keys = DynamoDbConditionKeys.extract(
|
||||
itemAction, request, describeTargetTable(executionTable));
|
||||
Map<String, List<String>> context = new LinkedHashMap<>();
|
||||
context.put("dynamodb:EnclosingOperation", List.of(read ? "TransactGetItems" : "TransactWriteItems"));
|
||||
if (!keys.leadingKeys().isEmpty()) {
|
||||
context.put("dynamodb:LeadingKeys", keys.leadingKeys());
|
||||
}
|
||||
if (!keys.attributes().isEmpty()) {
|
||||
context.put("dynamodb:Attributes", keys.attributes());
|
||||
}
|
||||
if (!read && request.hasNonNull("ReturnValuesOnConditionCheckFailure")) {
|
||||
context.put("dynamodb:ReturnValues",
|
||||
List.of(request.path("ReturnValuesOnConditionCheckFailure").asText()));
|
||||
}
|
||||
checks.add(new DynamoDbTransactionItem(itemAction,
|
||||
ResourceArnBuilder.toDynamoDbTableArn(executionTable, region, accountId), context));
|
||||
}
|
||||
}
|
||||
return checks;
|
||||
}
|
||||
|
||||
/** Resolves each batch entry with its own schema, optionally restricted to one resource. */
|
||||
Map<String, List<String>> dynamoDbBatchConditionContext(String action, ContainerRequestContext ctx,
|
||||
String resourceArn) {
|
||||
|
||||
@@ -464,17 +464,33 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
if (callerArns.isPresent()) {
|
||||
caller = caller.withPrincipalArn(callerArns.get().callerArn());
|
||||
conditionContext = conditionContext == null ? new HashMap<>() : new HashMap<>(conditionContext);
|
||||
conditionContext.put("aws:PrincipalArn", List.of(callerArns.get().principalArn()));
|
||||
putPrincipalKeys(conditionContext, callerArns.get());
|
||||
}
|
||||
List<Map<String, List<String>>> targetContexts = new ArrayList<>();
|
||||
targetContexts.add(conditionContext);
|
||||
for (Map<String, List<String>> target : remainingTargets) {
|
||||
Map<String, List<String>> targetContext = new HashMap<>(target);
|
||||
callerArns.ifPresent(arns -> targetContext.put("aws:PrincipalArn", List.of(arns.principalArn())));
|
||||
callerArns.ifPresent(arns -> putPrincipalKeys(targetContext, arns));
|
||||
targetContexts.add(targetContext);
|
||||
}
|
||||
|
||||
RequestPrincipal principal = RequestPrincipal.caller(callerArns.orElse(null));
|
||||
List<IamConditionContextResolver.DynamoDbTransactionItem> transactionItems =
|
||||
"dynamodb".equals(credentialScope)
|
||||
? conditionContextResolver.dynamoDbTransactionItems(action, ctx, region, accountId)
|
||||
: List.of();
|
||||
if (!transactionItems.isEmpty()) {
|
||||
for (IamConditionContextResolver.DynamoDbTransactionItem item : transactionItems) {
|
||||
Map<String, List<String>> itemContext = new HashMap<>(item.context());
|
||||
callerArns.ifPresent(arns -> putPrincipalKeys(itemContext, arns));
|
||||
if (abortIfDenied(ctx, caller, principal, item.action(), credentialScope,
|
||||
resolveResourceArns(credentialScope, List.of(item.resource())), List.of(itemContext),
|
||||
region, accountId, akid)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (abortIfDenied(ctx, caller, principal, action, credentialScope, resources, targetContexts,
|
||||
region, accountId, akid)) {
|
||||
return;
|
||||
@@ -653,6 +669,10 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
private record ResolvedAuthorization(String credentialScope, String action) {
|
||||
}
|
||||
|
||||
private static void putPrincipalKeys(Map<String, List<String>> context, CallerArns arns) {
|
||||
context.put("aws:PrincipalArn", List.of(arns.principalArn()));
|
||||
}
|
||||
|
||||
/** Floci's account-root principal, which is both the caller and the request's aws:PrincipalArn. */
|
||||
private CallerArns accountRootArns(String accountId) {
|
||||
String rootArn = AwsArnUtils.Arn.global(requestPartition(), "iam", accountId, "root").toString();
|
||||
@@ -918,7 +938,7 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
if (callerArns.isPresent()) {
|
||||
caller = caller.withPrincipalArn(callerArns.get().callerArn());
|
||||
conditionContext = new HashMap<>();
|
||||
conditionContext.put("aws:PrincipalArn", List.of(callerArns.get().principalArn()));
|
||||
putPrincipalKeys(conditionContext, callerArns.get());
|
||||
}
|
||||
|
||||
ResourcePolicyDecision effectiveDecision = resourcePolicyDecision;
|
||||
|
||||
+2
-1
@@ -49,7 +49,8 @@ public final class DynamoDbConditionKeys {
|
||||
Set<String> attributes = new LinkedHashSet<>();
|
||||
|
||||
switch (action == null ? "" : action) {
|
||||
case "dynamodb:GetItem", "dynamodb:DeleteItem", "dynamodb:UpdateItem" -> {
|
||||
case "dynamodb:GetItem", "dynamodb:DeleteItem", "dynamodb:UpdateItem",
|
||||
"dynamodb:ConditionCheckItem" -> {
|
||||
JsonNode key = body.get("Key");
|
||||
addAttributeNames(attributes, key);
|
||||
addLeadingKey(leadingKeys, key, pkName);
|
||||
|
||||
@@ -828,7 +828,7 @@ public class ResourceArnBuilder {
|
||||
return io.github.hectorvent.floci.services.dynamodb.DynamoDbPartiQLParser.extractTable(statement);
|
||||
}
|
||||
|
||||
private String toDynamoDbTableArn(String tableName, String region, String accountId) {
|
||||
public static String toDynamoDbTableArn(String tableName, String region, String accountId) {
|
||||
if (AwsArnUtils.isArnFor(tableName, "dynamodb")) {
|
||||
return tableName;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user