fix(iam): resolve missing roles and EC2 tagging targets (#7596)

This commit is contained in:
luiseterc authored and GitHub committed 2026-11-09 18:28:31 -04:00
1 parent 69cc889552
commit 3c398103ed
14 files changed
+1490 -9

No files matched your search

@@ -1241,7 +1241,8 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
String action, String operationAction, String credentialScope,
List<String> resources, List<Map<String, List<String>>> targetContexts,
String region, String accountId, String akid) {
for (String resource : resources) {
for (int resourceIndex = 0; resourceIndex < resources.size(); resourceIndex++) {
String resource = resources.get(resourceIndex);
List<ResourcePolicyProvider.ResourcePolicy> resourcePolicies =
resolveResourcePolicies(credentialScope, operationAction, resource);
String resourceOwnerAccountId = resourcePolicies.isEmpty() ? null : resourcePolicies.getFirst().ownerAccountId();
@@ -1259,6 +1260,11 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
List<Map<String, List<String>>> batchContexts = "dynamodb".equals(credentialScope)
? conditionContextResolver.dynamoDbBatchConditionContexts(action, ctx, resource) : null;
List<Map<String, List<String>>> resourceContexts = targetContexts;
if (RequestResources.hasIndividualTargets(operationAction) && !"*".equals(resource)
&& resources.size() == targetContexts.size()) {
// Generated target lists preserve request order; wildcard targets retain every context.
resourceContexts = targetContexts.subList(resourceIndex, resourceIndex + 1);
}
if (batchContexts != null && !batchContexts.isEmpty()) {
resourceContexts = new ArrayList<>();
for (Map<String, List<String>> targetContext : targetContexts) {
@@ -114,6 +114,11 @@ public final class QueryConstraintCheck {
return of(model, SmithyValidationErrors.Violations.QUOTED);
}
/** Whether the generated input shape declares this top-level member. Unknown operations declare none. */
public boolean hasMember(String operation, String member) {
return shapes.path(operations.path(operation).asText("")).path("members").has(member);
}
public static QueryConstraintCheck of(JsonNode model, SmithyValidationErrors.Violations messages) {
return new QueryConstraintCheck(model, messages);
}
@@ -176,6 +176,10 @@ final class RequestResourceMembers {
record Created(String type, String parent, Member arn, String parentWord, String childWord, String separator) {
}
/** An ordered heterogeneous id list; unsupported ids retain wildcard authorization. */
record Targets(Member member, String query, Map<String, String> prefixes) {
}
/**
* The file: each operation's resources and the resources it creates under a parent, as
* {@code <namespace>:<Operation>}, the members a REST operation's request carries outside its body, each
@@ -184,6 +188,7 @@ final class RequestResourceMembers {
* query, {@code ?name}, by the member's name.
*/
record Table(Map<String, List<Resource>> operations, Map<String, List<Created>> creates,
Map<String, Targets> targets,
Map<String, Map<String, String>> located, Map<String, Map<String, ArnFormat>> formats,
Set<String> namespaces, Set<String> handBuilt, Set<String> notCovered, Set<String> otherNamespaces) {
}
@@ -240,6 +245,7 @@ final class RequestResourceMembers {
});
Map<String, List<Resource>> entries = new LinkedHashMap<>();
Map<String, List<Created>> creates = new LinkedHashMap<>();
Map<String, Targets> targets = new LinkedHashMap<>();
Map<String, Map<String, String>> located = new LinkedHashMap<>();
operations.properties().forEach(operation -> {
String namespace = operation.getKey().substring(0, Math.max(0, operation.getKey().indexOf(':')));
@@ -252,6 +258,33 @@ final class RequestResourceMembers {
resources.add(resource(operation.getKey(), resource, formats.getOrDefault(namespace, Map.of())));
}
entries.put(operation.getKey(), List.copyOf(resources));
JsonNode target = operation.getValue().path("targets");
if (!target.isMissingNode()) {
String member = target.path("member").asText("");
String query = target.path("query").asText("");
if (!member.matches("[A-Za-z][A-Za-z0-9_]*\\[\\]")
|| !LOCATED_MEMBER.matcher(query).matches()
|| !target.path("prefixes").isObject() || target.path("prefixes").isEmpty()) {
throw new IllegalStateException(RESOURCE_NAME + " has malformed id targets for " + operation.getKey());
}
Map<String, String> prefixes = new LinkedHashMap<>();
Set<String> allowed = names(operation.getValue().path("types"));
target.path("prefixes").properties().forEach(entry -> {
ArnFormat format = formats.getOrDefault(namespace, Map.of()).get(entry.getKey());
String prefix = entry.getValue().asText("");
if (!allowed.contains(entry.getKey()) || format == null || prefix.isEmpty()
|| VARIABLE.matcher(format.format()).results().map(match -> match.group(1))
.filter(variable -> !Set.of("Partition", "Region", "Account").contains(variable))
.distinct().count() != 1
|| prefixes.values().stream().anyMatch(other ->
prefix.startsWith(other) || other.startsWith(prefix))) {
throw new IllegalStateException(RESOURCE_NAME + " has an invalid id target for " + operation.getKey());
}
prefixes.put(entry.getKey(), prefix);
});
targets.put(operation.getKey(), new Targets(new Member(member.substring(0, member.length() - 2), true),
query, Map.copyOf(prefixes)));
}
List<Created> made = new ArrayList<>();
for (JsonNode created : operation.getValue().path("creates")) {
made.add(created(operation.getKey(), created, formats.getOrDefault(namespace, Map.of())));
@@ -272,7 +305,7 @@ final class RequestResourceMembers {
located.put(operation.getKey(), Map.copyOf(where));
}
});
return new Table(Map.copyOf(entries), Map.copyOf(creates), Map.copyOf(located), Map.copyOf(formats), namespaces,
return new Table(Map.copyOf(entries), Map.copyOf(creates), Map.copyOf(targets), Map.copyOf(located), Map.copyOf(formats), namespaces,
names(document.path("handBuilt")),
Set.copyOf(document.path("notCovered").propertyStream().map(Map.Entry::getKey).toList()),
names(document.path("otherNamespaces")));
@@ -93,6 +93,12 @@ public final class RequestResources {
public static List<String> of(String operation, ContainerRequestContext ctx, String partition, String region,
String accountId, StoredResourceArns stored, Map<String, String> labels) {
RequestResourceMembers.Table table = RequestResourceMembers.table();
RequestResourceMembers.Targets targets = operation == null ? null : table.targets().get(operation);
if (targets != null) {
Map<String, String> form = IamConditionContextResolver.formParameters(ctx);
return targetArns(targets, formats(table, operation), form,
form.isEmpty() ? PassedRoles.jsonBody(ctx) : null, partition, region, accountId);
}
List<RequestResourceMembers.Resource> resources = operation == null ? null
: served(table.operations().get(operation), ctx);
if (resources == null || resources.isEmpty()) {
@@ -112,6 +118,10 @@ public final class RequestResources {
public static List<String> ofBody(String operation, JsonNode body, String partition, String region,
String accountId, StoredResourceArns stored) {
RequestResourceMembers.Table table = RequestResourceMembers.table();
RequestResourceMembers.Targets targets = operation == null ? null : table.targets().get(operation);
if (targets != null) {
return targetArns(targets, formats(table, operation), Map.of(), body, partition, region, accountId);
}
List<RequestResourceMembers.Resource> resources = operation == null ? null : table.operations().get(operation);
if (resources == null || resources.isEmpty()) {
return List.of();
@@ -121,7 +131,43 @@ public final class RequestResources {
/** Whether the table holds the operation as one with no resource of its own, which is checked on {@code *}. */
public static boolean actsOnNoResource(String operation) {
return reads(operation) && RequestResourceMembers.table().operations().get(operation).isEmpty();
return reads(operation) && !hasIndividualTargets(operation)
&& RequestResourceMembers.table().operations().get(operation).isEmpty();
}
/** Whether the generated operation names targets in request order, one context per target. */
public static boolean hasIndividualTargets(String operation) {
return operation != null && RequestResourceMembers.table().targets().containsKey(operation);
}
private static List<String> targetArns(RequestResourceMembers.Targets targets,
Map<String, RequestResourceMembers.ArnFormat> formats,
Map<String, String> form, JsonNode body,
String partition, String region, String accountId) {
List<String> ids = new ArrayList<>();
if (!form.isEmpty()) {
for (int i = 1; form.containsKey(targets.query() + "." + i); i++) {
ids.add(form.get(targets.query() + "." + i));
}
} else if (body != null && body.path(targets.member().name()).isArray()) {
body.path(targets.member().name()).forEach(id -> ids.add(id.asText()));
}
List<String> arns = new ArrayList<>();
for (String id : ids) {
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);
break;
}
}
arns.add(arn);
}
return List.copyOf(arns);
}
/** The ARNs of the resources the request names, as {@link #of} reads them. */
@@ -41,6 +41,11 @@ public final class IamRequestShapes {
private IamRequestShapes() {
}
/** Whether the operation's generated input shape declares the member. */
static boolean hasMember(String operation, String member) {
return CHECK.hasMember(operation, member);
}
/** Throws {@code ValidationError} when the request breaks a constraint the model declares. */
public static void check(String action, MultivaluedMap<String, String> params) {
CHECK.check(action, params);
@@ -555,6 +555,12 @@ public class ResourceArnBuilder {
// resource has and leave a deny on the real one unmatched.
Optional<StoredEntity> stored = storedIamEntity(resourceType, name, accountId);
if (stored.isEmpty()) {
// Without a stored role or a modelled Path member, the request names only the root path.
if ("role".equals(resourceType) && IamRequestShapes.hasMember(action, "RoleName")
&& !IamRequestShapes.hasMember(action, "Path")) {
return List.of(AwsArnUtils.Arn.global(AwsRegions.partitionFor(region), "iam", accountId,
"role/" + name).toString());
}
return List.of("*");
}
String storedArn = stored.get().arn();