fix(iam): resolve EC2 network resources and ownership tags (#8288)

EC2 network operations with resource-scoped policies were checked against
`*`, and stored ownership conditions were missing. Every EC2 request is
now read by the generated request-resource rules, its EC2 Query list
names from botocore, so a resource-scoped policy applies to each EC2
operation whose request names a resource. A list a type's ARN variable
names is the request's targets only when the request names nothing
else, and the sg-, acl-, rtb- and eni- id prefixes come from botocore's
examples.

Each target is evaluated with its own stored tags, under every EC2
type's Service Reference resource-tag keys, and tags on creation are
authorized separately. A forged SDK spelling cannot replace an EC2 wire
value. ENI creation tags are kept in the regional tag index and stay in
sync with CreateTags and DeleteTags.

Closes #8286
Closes #8287
Closes #8291
Closes #8295
Closes #8300
Closes #8306
This commit is contained in:
luiseterc authored and GitHub committed 2026-11-14 13:38:41 -04:00
1 parent 4649d6c683
commit a138f88094
20 files changed
+2163 -587

No files matched your search

@@ -889,6 +889,33 @@ 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) {
if (!"ec2".equals(scope) || RequestResources.hasIndividualTargets(action)
|| !AwsArnUtils.isArn(resource) || !ec2Service.isResolvable()) {
return context;
}
AwsArnUtils.Arn arn = AwsArnUtils.parse(resource);
if (!"ec2".equals(arn.service()) || !region.equals(arn.region()) || !accountId.equals(arn.accountId())) {
return context;
}
String[] parts = arn.resource().split("/", 2);
if (parts.length != 2 || parts[1].contains("*") || parts[1].contains("?")) {
return context;
}
List<String> prefixes = RequestResourceMembers.table().resourceTagKeys()
.getOrDefault("ec2", Map.of()).getOrDefault(parts[0], List.of());
if (prefixes.isEmpty()) {
return context;
}
Map<String, List<String>> result = context == null ? new LinkedHashMap<>() : new LinkedHashMap<>(context);
result.keySet().removeIf(key -> prefixes.stream().anyMatch(key::startsWith));
putStoredTags(result, region, parts[1], prefixes);
return result;
}
private void addResourceTags(Map<String, List<String>> conditions, String resourceId,
RequestResourceMembers.Targets targets) {
if (resourceId == null || resourceId.isBlank() || !ec2Service.isResolvable()) {
@@ -903,6 +930,11 @@ public class IamConditionContextResolver {
break;
}
}
putStoredTags(conditions, region, resourceId, prefixes);
}
private void putStoredTags(Map<String, List<String>> conditions, String region, String resourceId,
List<String> prefixes) {
for (Tag tag : ec2Service.get().resourceTags(region, resourceId)) {
for (String prefix : prefixes) {
conditions.put(prefix + tag.getKey(), List.of(tag.getValue() == null ? "" : tag.getValue()));
@@ -1332,8 +1332,14 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
}
}
for (Map<String, List<String>> targetContext : resourceContexts) {
Map<String, List<String>> taggedContext = targetContext;
if ("ec2".equals(credentialScope) && !RequestResources.hasIndividualTargets(operationAction)
&& AwsArnUtils.isArn(resource)) {
taggedContext = conditionContextResolver.withEc2ResourceTags(
credentialScope, operationAction, resource, region, accountId, targetContext);
}
Map<String, List<String>> effectiveContext = IamConditionContextResolver.withGlobalContext(
targetContext, resource, region, accountId, resourceOwnerAccountId, action);
taggedContext, resource, region, accountId, resourceOwnerAccountId, action);
ResourcePolicyDecision resourcePolicyDecision = evaluator.evaluateResourcePolicyFor(
effectiveResourcePolicies, principal, action, resource, effectiveContext);
Decision decision = evaluator.evaluateResolvedResourcePolicy(
@@ -224,6 +224,7 @@ final class RequestResourceMembers {
record Table(Map<String, List<Resource>> operations, Map<String, List<String>> types,
Map<String, List<Created>> creates, Map<String, List<Resource>> makes,
Map<String, Targets> targets,
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,
Set<String> namespaces, Set<String> handBuilt, Set<String> notCovered, Set<String> otherNamespaces,
@@ -311,6 +312,7 @@ final class RequestResourceMembers {
Map<String, List<Created>> creates = new LinkedHashMap<>();
Map<String, List<Resource>> makes = new LinkedHashMap<>();
Map<String, Targets> targets = new LinkedHashMap<>();
Map<String, Map<Member, String>> ec2Query = new LinkedHashMap<>();
Map<String, Map<String, String>> located = new LinkedHashMap<>();
Map<String, List<Forwarded>> forwarded = new LinkedHashMap<>();
JsonNode forwardedActions = document.path("forwardedActions");
@@ -373,6 +375,20 @@ final class RequestResourceMembers {
if (!under.isEmpty()) {
creates.put(operation.getKey(), List.copyOf(under));
}
Map<Member, String> queryMembers = new LinkedHashMap<>();
operation.getValue().path("ec2Query").properties().forEach(member -> {
String path = member.getKey();
String wire = member.getValue().asText("");
if (!namespace.equals("ec2") || !path.matches("[A-Za-z][A-Za-z0-9_]*(\\[\\])?")
|| !LOCATED_MEMBER.matcher(wire).matches()) {
throw new IllegalStateException(RESOURCE_NAME + " has invalid EC2 Query member: " + path);
}
boolean list = path.endsWith("[]");
queryMembers.put(new Member(list ? path.substring(0, path.length() - 2) : path, list), wire);
});
if (!queryMembers.isEmpty()) {
ec2Query.put(operation.getKey(), Map.copyOf(queryMembers));
}
Map<String, String> where = new LinkedHashMap<>();
operation.getValue().path("located").properties().forEach(member -> {
if (!LOCATED_MEMBER.matcher(member.getKey()).matches()
@@ -395,7 +411,8 @@ final class RequestResourceMembers {
}
});
return new Table(Map.copyOf(entries), Map.copyOf(actsOn), Map.copyOf(creates), Map.copyOf(makes),
Map.copyOf(targets), Map.copyOf(located), Map.copyOf(formats), Map.copyOf(resourceTagKeys), namespaces,
Map.copyOf(targets), Map.copyOf(ec2Query), Map.copyOf(located), Map.copyOf(formats),
Map.copyOf(resourceTagKeys), namespaces,
names(document.path("handBuilt")),
Set.copyOf(document.path("notCovered").propertyStream().map(Map.Entry::getKey).toList()),
names(document.path("otherNamespaces")), Map.copyOf(forwarded));
@@ -115,7 +115,7 @@ public final class RequestResources {
}
Map<String, String> sent = IamConditionContextResolver.formParameters(ctx);
JsonNode body = sent.isEmpty() ? located(table, operation, PassedRoles.jsonBody(ctx), ctx, labels) : null;
Map<String, String> form = located(table, operation, sent, ctx, labels);
Map<String, String> form = ec2Query(table, operation, located(table, operation, sent, ctx, labels));
return resources.isEmpty() ? List.of()
: named(operation, resources, formats(table, operation), form, body, partition, region, accountId, stored);
}
@@ -309,7 +309,8 @@ public final class RequestResources {
String region, String accountId, Map<String, String> labels) {
RequestResourceMembers.Table table = RequestResourceMembers.table();
return created(table, operation, tagging, resources -> served(resources, ctx),
() -> located(table, operation, IamConditionContextResolver.formParameters(ctx), ctx, labels),
() -> ec2Query(table, operation,
located(table, operation, IamConditionContextResolver.formParameters(ctx), ctx, labels)),
() -> IamConditionContextResolver.formParameters(ctx).isEmpty()
? located(table, operation, PassedRoles.jsonBody(ctx), ctx, labels) : null,
partition, region, accountId);
@@ -857,6 +858,28 @@ public final class RequestResources {
}
}
/** Normalize only generated EC2 wire members; JSON task inputs keep their SDK names. */
private static Map<String, String> ec2Query(RequestResourceMembers.Table table, String operation,
Map<String, String> sent) {
Map<RequestResourceMembers.Member, String> members = table.ec2Query().get(operation);
if (sent.isEmpty() || members == null) {
return sent;
}
Map<String, String> form = new HashMap<>(sent);
members.forEach((member, wire) -> {
// Ignore forged SDK/Query spellings the EC2 handler itself does not read.
form.keySet().removeIf(key -> key.equals(member.name()) || key.startsWith(member.name() + "."));
if (member.list()) {
for (int index = 1; sent.containsKey(wire + "." + index); index++) {
form.put(member.name() + ".member." + index, sent.get(wire + "." + index));
}
} else if (sent.containsKey(wire)) {
form.put(member.name(), sent.get(wire));
}
});
return form;
}
/**
* The non-empty strings a request gives a member, read as its handler reads them: a form's parameter, or its
* list numbered under {@code member} from 1 to the first missing, or a body's value, as text whether a string,
@@ -11012,6 +11012,13 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
}
RouteTable rt = routeTables.get(storeKey).orElse(null);
if (rt != null) { rt.setTags(new ArrayList<>(tagList)); routeTables.put(storeKey, rt); return; }
NetworkInterface networkInterface = networkInterfaces.get(storeKey).orElse(null);
if (networkInterface != null) {
networkInterface.getTagSet().clear();
networkInterface.getTagSet().addAll(tagList);
networkInterfaces.put(storeKey, networkInterface);
return;
}
KeyPair kp = keyPairs.get(storeKey).orElse(null);
if (kp != null) { kp.setTags(new ArrayList<>(tagList)); keyPairs.put(storeKey, kp); return; }
LaunchTemplate lt = launchTemplates.get(storeKey).orElse(null);
@@ -15945,6 +15952,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
ni.setPrivateIpAddresses(ipList);
networkInterfaces.put(key(region, eniId), ni);
putTags(region, eniId, new ArrayList<>(ni.getTagSet()));
return ni;
}
}
@@ -15966,6 +15974,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
}
releaseInterfaceAddresses(region, networkInterfaceId, true);
networkInterfaces.delete(key(region, networkInterfaceId));
removeTags(region, networkInterfaceId);
releasePrivateIps(region, ni.getSubnetId(), List.of(ni.getPrivateIpAddress()));
}
}