fix(ec2): resolve interface identity for tag authorization (#8352)

* fix(ec2): resolve interface identity for tag authorization
* fix(ec2): read resource condition keys through the generated table
* fix(ec2): look up a tag batch's interfaces in one pass
This commit is contained in:
luiseterc authored and GitHub committed 2026-11-14 18:42:23 -04:00
1 parent 4de81e8370
commit 16745c3804
13 files changed
+542 -25

No files matched your search

@@ -1,6 +1,7 @@
package io.floci.aws.core.common;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.services.cloudwatch.logs.CloudWatchLogsCrossAccountService;
@@ -70,10 +71,14 @@ import java.util.function.Function;
public class IamConditionContextResolver {
private static final Logger LOG = Logger.getLogger(IamConditionContextResolver.class);
/** Reads a resource EC2 holds as its fields, java.time ones included. */
private static final ObjectMapper RESOURCE_MAPPER = new ObjectMapper().findAndRegisterModules();
/** Set by {@code ResourceArnBuilder.readJsonBody}, which runs earlier in the same filter pass. */
static final String BUFFERED_JSON_BODY = "floci.bufferedJsonBody";
private static final String BUFFERED_FORM_BODY = "floci.bufferedFormBody";
/** Prefixes the action whose numbered EC2 targets {@link #heldTargets} looked up for the request. */
private static final String HELD_TARGETS = "floci.heldTargets.";
private static final String REQUEST_TAG_PREFIX = "aws:RequestTag/";
private static final String RESOURCE_TAG_PREFIX = "aws:ResourceTag/";
/**
@@ -105,6 +110,7 @@ public class IamConditionContextResolver {
private final Instance<LambdaWebService> lambdaWebService;
private final Instance<RequestConditionSpelling> requestConditionSpellings;
private final RequestContext requestContext;
private final RegionResolver regionResolver;
private final EmulatorConfig config;
private final CurrentVertxRequest currentVertxRequest;
private final Clock clock;
@@ -124,11 +130,12 @@ public class IamConditionContextResolver {
Instance<LambdaWebService> lambdaWebService,
Instance<RequestConditionSpelling> requestConditionSpellings,
RequestContext requestContext,
RegionResolver regionResolver,
EmulatorConfig config,
CurrentVertxRequest currentVertxRequest,
ResolvedServiceCatalog serviceCatalog) {
this(dynamoDbFacade, ec2Service, s3Service, logsService, crossAccountService, iamService,
lambdaWebService, requestConditionSpellings, requestContext,
lambdaWebService, requestConditionSpellings, requestContext, regionResolver,
config, currentVertxRequest, serviceCatalog, Clock.systemUTC());
}
@@ -141,6 +148,7 @@ public class IamConditionContextResolver {
Instance<LambdaWebService> lambdaWebService,
Instance<RequestConditionSpelling> requestConditionSpellings,
RequestContext requestContext,
RegionResolver regionResolver,
EmulatorConfig config,
CurrentVertxRequest currentVertxRequest,
ResolvedServiceCatalog serviceCatalog, Clock clock) {
@@ -153,6 +161,7 @@ public class IamConditionContextResolver {
this.lambdaWebService = lambdaWebService;
this.requestConditionSpellings = requestConditionSpellings;
this.requestContext = requestContext;
this.regionResolver = regionResolver;
this.config = config;
this.currentVertxRequest = currentVertxRequest;
this.clock = clock;
@@ -838,6 +847,8 @@ public class IamConditionContextResolver {
}
if (targets != null) {
addResourceTags(conditions, form.get(targets.query() + ".1"), targets);
addResourceKeys(conditions, form.get(targets.query() + ".1"), targets,
heldTargets(action, ctx, form, targets));
}
return conditions.isEmpty() ? null : conditions;
}
@@ -892,17 +903,50 @@ public class IamConditionContextResolver {
// The handlers read numbered members from 1 up to the first gap, and so does this.
return targets;
}
Map<String, Object> held = heldTargets(action, ctx, form, generatedTargets);
for (int i = 2; form.containsKey(idParameter + i); i++) {
Map<String, List<String>> conditions = new LinkedHashMap<>();
if ("ec2:CreateTags".equals(action)) {
addRequestTags(conditions, form, "Tag.");
}
addResourceTags(conditions, form.get(idParameter + i), generatedTargets);
addResourceKeys(conditions, form.get(idParameter + i), generatedTargets, held);
targets.add(conditions);
}
return targets;
}
/**
* What EC2 holds under each numbered target of an {@code action} request whose type has generated resource keys,
* keyed by id. The targets are looked up together, once per request and action, so the first target's context
* and the remaining targets' contexts share a single pass over EC2's stores however many the request names.
*/
private Map<String, Object> heldTargets(String action, ContainerRequestContext ctx, Map<String, String> form,
RequestResourceMembers.Targets targets) {
String property = HELD_TARGETS + action;
if (ctx.getProperty(property) instanceof HeldTargets cached) {
return cached.byId();
}
Map<String, Object> held = new LinkedHashMap<>();
if (ec2Service.isResolvable()) {
Map<String, Set<String>> idsByType = new LinkedHashMap<>();
for (int i = 1; form.containsKey(targets.query() + "." + i); i++) {
String id = form.get(targets.query() + "." + i);
String type = targetType(id, targets);
if (type != null && !ec2ResourceKeys(type).isEmpty()) {
idsByType.computeIfAbsent(type, ignored -> new LinkedHashSet<>()).add(id);
}
}
idsByType.forEach((type, ids) -> held.putAll(ec2Service.get().heldResources(requestRegion(), type, ids)));
}
ctx.setProperty(property, new HeldTargets(held));
return held;
}
/** The resources {@link #heldTargets} found for a request, kept on it under the action they were read for. */
private record HeldTargets(Map<String, Object> byId) {
}
private static void addRequestTags(Map<String, List<String>> conditions, Map<String, String> form,
String prefix) {
for (int i = 1; form.containsKey(prefix + i + ".Key"); i++) {
@@ -917,10 +961,13 @@ public class IamConditionContextResolver {
}
}
/** Stored tags for one generated EC2 ARN, never borrowed from another target in the same request. */
public Map<String, List<String>> withEc2ResourceTags(String scope, String action, String resource,
String region, String accountId,
Map<String, List<String>> context) {
/**
* The stored tags and the generated resource keys of one generated EC2 ARN (see {@link #putResourceKeys}), never
* borrowed from another target in the same request.
*/
public Map<String, List<String>> withEc2ResourceContext(String scope, String action, String resource,
String region, String accountId,
Map<String, List<String>> context) {
if (!"ec2".equals(scope) || RequestResources.hasIndividualTargets(action)
|| !AwsArnUtils.isArn(resource) || !ec2Service.isResolvable()) {
return context;
@@ -935,12 +982,17 @@ public class IamConditionContextResolver {
}
List<String> prefixes = RequestResourceMembers.table().resourceTagKeys()
.getOrDefault("ec2", Map.of()).getOrDefault(parts[0], List.of());
if (prefixes.isEmpty()) {
Map<String, RequestResourceMembers.ResourceKey> keys = ec2ResourceKeys(parts[0]);
if (prefixes.isEmpty() && keys.isEmpty()) {
return context;
}
Map<String, List<String>> result = context == null ? new LinkedHashMap<>() : new LinkedHashMap<>(context);
result.keySet().removeIf(key -> prefixes.stream().anyMatch(key::startsWith));
result.keySet().removeIf(key -> prefixes.stream().anyMatch(key::startsWith) || keys.containsKey(key));
putStoredTags(result, region, parts[1], prefixes);
if (!keys.isEmpty()) {
putResourceKeys(result, region, parts[0],
ec2Service.get().heldResources(region, parts[0], List.of(parts[1])).get(parts[1]));
}
return result;
}
@@ -949,16 +1001,80 @@ public class IamConditionContextResolver {
if (resourceId == null || resourceId.isBlank() || !ec2Service.isResolvable()) {
return;
}
String region = requestContext.getRegion() == null ? config.defaultRegion() : requestContext.getRegion();
List<String> prefixes = List.of(RESOURCE_TAG_PREFIX);
String type = targetType(resourceId, targets);
List<String> prefixes = type == null ? List.of(RESOURCE_TAG_PREFIX)
: RequestResourceMembers.table().resourceTagKeys().getOrDefault("ec2", Map.of())
.getOrDefault(type, List.of());
putStoredTags(conditions, requestRegion(), resourceId, prefixes);
}
/**
* The generated resource keys of the type whose id prefix {@code resourceId} carries, from what {@code held} says
* EC2 holds under it (see {@link #putResourceKeys}).
*/
private void addResourceKeys(Map<String, List<String>> conditions, String resourceId,
RequestResourceMembers.Targets targets, Map<String, Object> held) {
String type = targetType(resourceId, targets);
if (type != null) {
putResourceKeys(conditions, requestRegion(), type, held.get(resourceId));
}
}
/** The generated EC2 resource type whose id prefix {@code resourceId} carries, or null when none does. */
private static String targetType(String resourceId, RequestResourceMembers.Targets targets) {
if (resourceId == null || resourceId.isBlank()) {
return null;
}
for (Map.Entry<String, String> type : targets.prefixes().entrySet()) {
if (resourceId.startsWith(type.getValue())) {
prefixes = RequestResourceMembers.table().resourceTagKeys()
.getOrDefault("ec2", Map.of()).getOrDefault(type.getKey(), List.of());
break;
return type.getKey();
}
}
putStoredTags(conditions, region, resourceId, prefixes);
return null;
}
/** The condition keys the request-resources table reads from a member of EC2's resource {@code type}. */
private static Map<String, RequestResourceMembers.ResourceKey> ec2ResourceKeys(String type) {
return RequestResourceMembers.table().resourceKeys().getOrDefault("ec2", Map.of())
.getOrDefault(type, Map.of());
}
private String requestRegion() {
return requestContext.getRegion() == null ? config.defaultRegion() : requestContext.getRegion();
}
/**
* Each condition key the request-resources table gives EC2's resource {@code type} as a member of its botocore
* shape, valued from that member of {@code held}, the resource EC2 holds under the target's id, as the ARN of the
* key's type where the Service Reference types the key as one. A member with no value, or no resource held,
* leaves the key absent.
*/
private void putResourceKeys(Map<String, List<String>> conditions, String region, String type, Object held) {
Map<String, RequestResourceMembers.ResourceKey> keys = ec2ResourceKeys(type);
if (keys.isEmpty() || held == null) {
return;
}
JsonNode resource = RESOURCE_MAPPER.valueToTree(held);
String account = requestContext.getAccountId();
String partition = regionResolver.partitionForRegion(region);
keys.forEach((key, spec) -> {
String value = memberValue(resource, spec.member());
if (value != null && !value.isBlank()) {
conditions.put(key, List.of(spec.arn() == null ? value
: RequestResources.idArn("ec2", spec.arn(), value, partition, region, account)));
}
});
}
/** The scalar the resource holds in the botocore member {@code member}, its field of that name, case aside. */
private static String memberValue(JsonNode resource, String member) {
for (Map.Entry<String, JsonNode> field : resource.properties()) {
if (field.getKey().equalsIgnoreCase(member) && field.getValue().isValueNode()
&& !field.getValue().isNull()) {
return field.getValue().asText();
}
}
return null;
}
private void putStoredTags(Map<String, List<String>> conditions, String region, String resourceId,
@@ -1340,7 +1340,7 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
Map<String, List<String>> taggedContext = targetContext;
if ("ec2".equals(credentialScope) && !RequestResources.hasIndividualTargets(operationAction)
&& AwsArnUtils.isArn(resource)) {
taggedContext = conditionContextResolver.withEc2ResourceTags(
taggedContext = conditionContextResolver.withEc2ResourceContext(
credentialScope, operationAction, resource, region, accountId, targetContext);
}
Map<String, List<String>> effectiveContext = IamConditionContextResolver.withGlobalContext(
@@ -265,6 +265,14 @@ final class RequestResourceMembers {
record Targets(Member member, String query, Map<String, String> prefixes) {
}
/**
* A condition key of a resource type that a member of the type's botocore shape names ({@code ec2:Vpc}, a
* network interface's {@code VpcId}): its value is that member of the resource its service holds, as the ARN of
* the namespace's type {@code arn} where the Service Reference types the key as an ARN, as given otherwise.
*/
record ResourceKey(String member, String arn) {
}
/**
* A request an operation's service makes to another service as the caller: {@code action}, on that service's
* resource of {@code type} which the record of the operation's resource of type {@code of} holds in
@@ -290,6 +298,7 @@ final class RequestResourceMembers {
Map<String, Map<Member, String>> ec2Query,
Map<String, Map<String, String>> located, Map<String, Map<String, ArnFormat>> formats,
Map<String, Map<String, List<String>>> resourceTagKeys,
Map<String, Map<String, Map<String, ResourceKey>>> resourceKeys,
Set<String> namespaces, Set<String> handBuilt, Set<String> notCovered, Set<String> otherNamespaces,
Map<String, List<Forwarded>> forwarded) {
}
@@ -372,6 +381,7 @@ final class RequestResourceMembers {
});
resourceTagKeys.put(namespace.getKey(), Map.copyOf(types));
});
Map<String, Map<String, Map<String, ResourceKey>>> resourceKeys = resourceKeys(document, formats);
Map<String, List<Resource>> entries = new LinkedHashMap<>();
Map<String, List<String>> actsOn = new LinkedHashMap<>();
Map<String, List<Created>> creates = new LinkedHashMap<>();
@@ -477,12 +487,44 @@ final class RequestResourceMembers {
});
return new Table(Map.copyOf(entries), Map.copyOf(actsOn), Map.copyOf(creates), Map.copyOf(makes),
Map.copyOf(targets), Map.copyOf(ec2Query), Map.copyOf(located), Map.copyOf(formats),
Map.copyOf(resourceTagKeys), namespaces,
Map.copyOf(resourceTagKeys), resourceKeys, namespaces,
names(document.path("handBuilt")),
Set.copyOf(document.path("notCovered").propertyStream().map(Map.Entry::getKey).toList()),
names(document.path("otherNamespaces")), Map.copyOf(forwarded));
}
/** Each resource type's member-named condition keys, by namespace, refusing a type or ARN type with no format. */
private static Map<String, Map<String, Map<String, ResourceKey>>> resourceKeys(
JsonNode document, Map<String, Map<String, ArnFormat>> formats) {
JsonNode section = document.path("resourceKeys");
if (!section.isMissingNode() && !section.isObject()) {
throw new IllegalStateException(RESOURCE_NAME + " has malformed resource keys");
}
Map<String, Map<String, Map<String, ResourceKey>>> result = new LinkedHashMap<>();
section.properties().forEach(namespace -> {
Map<String, ArnFormat> known = formats.getOrDefault(namespace.getKey(), Map.of());
Map<String, Map<String, ResourceKey>> types = new LinkedHashMap<>();
namespace.getValue().properties().forEach(type -> {
if (!known.containsKey(type.getKey()) || !type.getValue().isObject()) {
throw new IllegalStateException(RESOURCE_NAME + " has malformed resource keys");
}
Map<String, ResourceKey> keys = new LinkedHashMap<>();
type.getValue().properties().forEach(key -> {
String member = key.getValue().path("member").asText("");
JsonNode arn = key.getValue().path("arn");
if (!key.getKey().startsWith(namespace.getKey() + ":") || member.isEmpty()
|| !arn.isMissingNode() && !known.containsKey(arn.asText(""))) {
throw new IllegalStateException(RESOURCE_NAME + " has malformed resource keys");
}
keys.put(key.getKey(), new ResourceKey(member, arn.isMissingNode() ? null : arn.asText()));
});
types.put(type.getKey(), Map.copyOf(keys));
});
result.put(namespace.getKey(), Map.copyOf(types));
});
return Map.copyOf(result);
}
/**
* A request the operation's service makes to another as the caller, joined with what the file records of its
* action: the type it acts on and the {@code ViaService} key of its namespace, if any.
@@ -225,11 +225,7 @@ public final class RequestResources {
String arn = "*";
for (Map.Entry<String, String> prefix : targets.prefixes().entrySet()) {
if (id.startsWith(prefix.getValue())) {
String format = formats.get(prefix.getKey()).format();
Map<String, String> filling = new HashMap<>();
VARIABLE.matcher(format).results().map(match -> match.group(1))
.forEach(variable -> filling.put(variable, id));
arn = render(format, filling, partition, region, accountId);
arn = idArn(formats.get(prefix.getKey()).format(), id, partition, region, accountId);
break;
}
}
@@ -850,6 +846,21 @@ public final class RequestResources {
}
/** The format with each variable replaced once: the partition, Region and account, a value, or else {@code *}. */
/**
* The ARN of the namespace's resource of {@code type} named by {@code id}, the one variable of its format past
* partition, Region and account, as a request naming the resource by its id gives it.
*/
static String idArn(String namespace, String type, String id, String partition, String region, String accountId) {
return idArn(RequestResourceMembers.table().formats().get(namespace).get(type).format(), id, partition, region,
accountId);
}
private static String idArn(String format, String id, String partition, String region, String accountId) {
Map<String, String> filling = new HashMap<>();
VARIABLE.matcher(format).results().map(match -> match.group(1)).forEach(variable -> filling.put(variable, id));
return render(format, filling, partition, region, accountId);
}
private static String render(String format, Map<String, String> filling, String partition, String region,
String accountId) {
Matcher variable = VARIABLE.matcher(format);
@@ -15636,6 +15636,37 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
List<String> networkInterfaceIds,
List<Map.Entry<String, List<String>>> filters,
int maxResults, String nextToken) {
return describeNetworkInterfaces(accountId, region, networkInterfaceIds, filters, maxResults, nextToken, true);
}
/**
* The resources of {@code type} EC2 holds for the caller under {@code ids}, keyed by id, as its Describe operation
* answers them, which the IAM condition keys the request-resources table reads from a resource's members are
* valued from: a network interface of any kind, stored, held by an instance or owned by an endpoint or a NAT
* gateway. All of them come from one pass over the stores, so a request naming many costs no more lookups than one
* naming a single resource. Empty for a type it does not answer for. An id it holds nothing under is absent, and
* the request fails on it. Reads only: it creates no default resources.
*/
public Map<String, Object> heldResources(String region, String type, Collection<String> ids) {
if (!"network-interface".equals(type) || ids.isEmpty()) {
return Map.of();
}
// The id filter rather than the id list: a filter leaves out what it does not match, where an id the store
// lacks would fail the whole lookup with InvalidNetworkInterfaceID.NotFound.
Map<String, Object> held = new HashMap<>();
describeNetworkInterfaces(callerAccountId(), region, List.of(),
List.of(Map.entry("network-interface-id", List.copyOf(ids))), 0, null, false)
.networkInterfaces().stream()
.filter(ni -> ids.contains(ni.getNetworkInterfaceId()))
.forEach(ni -> held.put(ni.getNetworkInterfaceId(), ni));
return held;
}
private NetworkInterfaceListResult describeNetworkInterfaces(String accountId, String region,
List<String> networkInterfaceIds,
List<Map.Entry<String, List<String>>> filters,
int maxResults, String nextToken,
boolean createDefaults) {
// Validate pagination parameters
if (maxResults > 0 && !networkInterfaceIds.isEmpty()) {
throw new AwsException("InvalidParameterCombination",
@@ -15656,7 +15687,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
}
}
ensureDefaultResources(region);
if (createDefaults) {
ensureDefaultResources(region);
}
String safeAccount = accountId != null ? accountId : callerAccountId();
List<NetworkInterface> result = new ArrayList<>();
Set<String> foundIds = new HashSet<>();