fix(iam): read iam:PermissionsBoundary from the stored role or user an action names (#6897)

On the 15 actions where the Service Reference lists iam:PermissionsBoundary and the request carries no boundary, such as AttachRolePolicy, PutUserPolicy and GetRole, the key is the boundary stored on the role or user named by RoleName or UserName. The generator writes these pairs to a fromTarget section, and IamConditionContextResolver looks the boundary up through IamService. A role or user without one carries no value for the key.
This commit is contained in:
lex00 authored and GitHub committed 2026-10-29 00:07:50 -04:00
1 parent 9ce1b1755e
commit 5a84d08383
9 files changed
+257 -68

No files matched your search

@@ -12,7 +12,10 @@ 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.IamActionRegistry;
import io.github.hectorvent.floci.services.iam.IamService;
import io.github.hectorvent.floci.services.iam.ResourceArnBuilder;
import io.github.hectorvent.floci.services.iam.model.IamRole;
import io.github.hectorvent.floci.services.iam.model.IamUser;
import io.github.hectorvent.floci.services.s3.S3ControlTagConditionKeys;
import io.github.hectorvent.floci.services.s3.S3ObjectActions;
import io.github.hectorvent.floci.services.s3.S3ObjectKeys;
@@ -88,6 +91,7 @@ public class IamConditionContextResolver {
private final Instance<Ec2Service> ec2Service;
private final Instance<S3Service> s3Service;
private final Instance<CloudWatchLogsService> logsService;
private final Instance<IamService> iamService;
private final RequestContext requestContext;
private final EmulatorConfig config;
private final CurrentVertxRequest currentVertxRequest;
@@ -103,11 +107,12 @@ public class IamConditionContextResolver {
Instance<Ec2Service> ec2Service,
Instance<S3Service> s3Service,
Instance<CloudWatchLogsService> logsService,
Instance<IamService> iamService,
RequestContext requestContext,
EmulatorConfig config,
CurrentVertxRequest currentVertxRequest,
ResolvedServiceCatalog serviceCatalog) {
this(dynamoDbFacade, ec2Service, s3Service, logsService, requestContext, config, currentVertxRequest,
this(dynamoDbFacade, ec2Service, s3Service, logsService, iamService, requestContext, config, currentVertxRequest,
serviceCatalog, Clock.systemUTC());
}
@@ -115,6 +120,7 @@ public class IamConditionContextResolver {
Instance<Ec2Service> ec2Service,
Instance<S3Service> s3Service,
Instance<CloudWatchLogsService> logsService,
Instance<IamService> iamService,
RequestContext requestContext,
EmulatorConfig config,
CurrentVertxRequest currentVertxRequest,
@@ -123,6 +129,7 @@ public class IamConditionContextResolver {
this.ec2Service = ec2Service;
this.s3Service = s3Service;
this.logsService = logsService;
this.iamService = iamService;
this.requestContext = requestContext;
this.config = config;
this.currentVertxRequest = currentVertxRequest;
@@ -209,9 +216,10 @@ public class IamConditionContextResolver {
return conditions;
}
private static Map<String, List<String>> iamConditionContext(String action, ContainerRequestContext ctx) {
private Map<String, List<String>> iamConditionContext(String action, ContainerRequestContext ctx) {
Map<String, String> members = IamConditionKeys.members(action);
if (members.isEmpty()) {
Map<String, String> targets = IamConditionKeys.targets(action);
if (members.isEmpty() && targets.isEmpty()) {
return null;
}
Map<String, String> form = formParameters(ctx);
@@ -222,9 +230,28 @@ public class IamConditionContextResolver {
conditions.put(key, List.of(value));
}
});
// iam:PermissionsBoundary on an existing role or user is the boundary stored on it, left off when it has none.
targets.forEach((key, target) -> {
String value = storedPermissionsBoundary(target, form.get(target));
if (value != null) {
conditions.put(key, List.of(value));
}
});
return conditions.isEmpty() ? null : conditions;
}
private String storedPermissionsBoundary(String target, String name) {
if (name == null || !iamService.isResolvable()) {
return null;
}
String account = requestContext.getAccountId();
return switch (target) {
case "RoleName" -> iamService.get().findRole(account, name).map(IamRole::getPermissionsBoundaryArn).orElse(null);
case "UserName" -> iamService.get().findUser(account, name).map(IamUser::getPermissionsBoundaryArn).orElse(null);
default -> throw new IllegalStateException("Unknown IAM condition key target: " + target);
};
}
public static Map<String, List<String>> withGlobalContext(Map<String, List<String>> serviceContext,
String resourceArn, String region,
String accountId, String resourceOwnerAccountId) {
@@ -7,34 +7,50 @@ import java.util.LinkedHashMap;
import java.util.Map;
/**
* The {@code iam:} condition keys an IAM request carries in its own members, generated from the
* AWS Service Reference and pinned botocore by {@code tools/aws/regen_iam_condition_keys.py}.
* The {@code iam:} condition keys an IAM request carries in its own members, and those read from the
* role or user it names, generated from the AWS Service Reference and pinned botocore by
* {@code tools/aws/regen_iam_condition_keys.py}.
*/
final class IamConditionKeys {
private static final Map<String, Map<String, String>> MEMBERS = VendoredResources.read(
private record Table(Map<String, Map<String, String>> members, Map<String, Map<String, String>> targets) {}
private static final Table TABLE = VendoredResources.read(
"aws/iam-condition-keys.json", "IAM condition keys", input -> {
JsonNode read = new ObjectMapper().readTree(input).path("read");
if (!read.isArray() || read.isEmpty()) {
throw new IllegalStateException("Malformed IAM condition key table");
}
Map<String, Map<String, String>> members = new LinkedHashMap<>();
for (JsonNode entry : read) {
if (!entry.path("action").isTextual() || !entry.path("key").isTextual()
|| !entry.path("member").isTextual()) {
throw new IllegalStateException("Malformed IAM condition key entry: " + entry);
}
members.computeIfAbsent(entry.get("action").asText(), action -> new LinkedHashMap<>())
.put(entry.get("key").asText(), entry.get("member").asText());
}
members.replaceAll((action, keys) -> Map.copyOf(keys));
return Map.copyOf(members);
JsonNode document = new ObjectMapper().readTree(input);
return new Table(section(document.path("read"), "member"),
section(document.path("fromTarget"), "target"));
});
private IamConditionKeys() {}
private static Map<String, Map<String, String>> section(JsonNode entries, String field) {
if (!entries.isArray() || entries.isEmpty()) {
throw new IllegalStateException("Malformed IAM condition key table");
}
Map<String, Map<String, String>> byAction = new LinkedHashMap<>();
for (JsonNode entry : entries) {
if (!entry.path("action").isTextual() || !entry.path("key").isTextual()
|| !entry.path(field).isTextual()) {
throw new IllegalStateException("Malformed IAM condition key entry: " + entry);
}
byAction.computeIfAbsent(entry.get("action").asText(), action -> new LinkedHashMap<>())
.put(entry.get("key").asText(), entry.get(field).asText());
}
byAction.replaceAll((action, keys) -> Map.copyOf(keys));
return Map.copyOf(byAction);
}
/** Each condition key a request for {@code action} carries, to the member that holds it; empty when none. */
static Map<String, String> members(String action) {
return MEMBERS.getOrDefault(action, Map.of());
return TABLE.members().getOrDefault(action, Map.of());
}
/**
* Each condition key a request for {@code action} takes from the stored role or user it names, to the
* member that names it ({@code RoleName} or {@code UserName}); empty when none.
*/
static Map<String, String> targets(String action) {
return TABLE.targets().getOrDefault(action, Map.of());
}
}