fix(iam): check PutEvents on each entry's event bus (#6704)

PutEvents was checked on `*`: the request-resources table gave it no
resources, as its bus is a field of each entry, which no rule read. A
policy allowing or denying events:PutEvents on one event bus therefore
did not hold for publishing.

Each entry's EventBusName is "the name or ARN of the event bus to
receive the event", and "if you omit this, the default event bus is
used". Bus policies grant events:PutEvents on `event-bus/<name>`. The
EventBridge User Guide checks events:PutEvents against "the bus, once
per entry in the request", and says "Publishing is authorized against
the bus ARN for every entry in the request. If one entry fails an
authorization condition, EventBridge denies the whole request." It says
so in its section on the Custom Event Bus, which keeps the events: IAM
namespace of the Classic one; nothing in the guide says otherwise for a
Classic bus, whose API model lists AccessDeniedException among the error
codes of an entry without saying when.

The table now reads PutEvents' `Entries[].EventBusName`, a field of each
of a list's structures, one resource for each entry:

- The generator takes a correction filling a variable from such a field,
  and finds a field whose pattern takes an ARN before a name inside a
  list's structures, as it does a top-level member, so the field gets
  `arnOf` from botocore.
- The loader reads `Name[].Field` in `fill`, and in `absent` and `arnOf`
  when it fills a variable.
- The reader gives one value for each entry, the `absent` value for an
  entry that names no bus, gives a null or a blank, as the handler's
  `asText(null)` does, and none for no entries, which stay on `*`. Each
  value that is an ARN puts its own entry's bus in that ARN's account and
  Region, checked as given and as the caller's, which the handler
  publishes to; until now the first ARN a request gave set the account
  and Region of every value.

The filter checks every resource, so one entry for a bus the caller may
not publish to refuses the whole request, as the guide gives it.

Not in this change: the events:source and events:detail-type condition
keys (Floci supplies neither), EndpointId (the handler ignores it), and
PutPartnerEvents, which stays on `*`.

The upstream drift check on a pull request now verifies only the
vendored IAM files the pull request changes, each with its generator,
its tests and the modules that generator imports. It verified all six
files whichever one a pull request touched, so this change, which
touches only the table of the resources requests name, failed on the
service namespace list, which AWS's index stopped matching when it
dropped `codestar`: drift in a file the change leaves alone. The
pull request's job passes the paths its merge commit changes to
`make iam-data-verify CHANGED=...`, which names each check it skips;
the weekly and manual runs, and a diff that cannot be read, still
check every file, so the namespace list's drift is reported there.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-26 21:52:09 -05:00
1 parent 5c9eb3eb30
commit 0410a2b4ea
11 files changed
+384 -62

No files matched your search

@@ -34,6 +34,8 @@ final class RequestResourceMembers {
private static final Pattern MEMBER = Pattern.compile("[A-Za-z][A-Za-z0-9_]*(\\[])?");
/** A member holding an ARN may also be one level inside a structure or each of a list's structures. */
private static final Pattern ARN_MEMBER = Pattern.compile("[A-Za-z][A-Za-z0-9_]*(\\[])?(\\.[A-Za-z][A-Za-z0-9_]*(\\[])?)?");
/** A member filling a variable may also be a field of each of a list's structures, one value for each. */
private static final Pattern FILL_MEMBER = Pattern.compile("[A-Za-z][A-Za-z0-9_]*(\\[]|\\[]\\.[A-Za-z][A-Za-z0-9_]*)?");
/** An ARN format's leading resource type word, and the rest of its resource. */
private static final Pattern RESOURCE_PART = Pattern.compile(
"^arn:\\$\\{Partition}:[^:]*:[^:]*:[^:]*:([A-Za-z][A-Za-z0-9-]*)(.*)$");
@@ -51,6 +53,16 @@ final class RequestResourceMembers {
Member(String name, boolean list) {
this(name, list, null);
}
/** As the file writes it: {@code Name}, {@code Name[]}, {@code Name.Field} or {@code Name[].Field}. */
String path() {
return name + (list ? "[]" : "") + (field == null ? "" : "." + field.path());
}
/** Whether it is a field of each of a list's structures, which gives one value for each of them. */
boolean eachElement() {
return list && field != null && !field.list();
}
}
/**
@@ -98,8 +110,12 @@ final class RequestResourceMembers {
* the request's. A member {@code absent} from the request, or given a JSON null, is read as the value given for
* it (an EventBridge request naming no event bus names {@code default}), and one in {@code arnOf} may give an
* ARN of the type named for it instead of its name, read as that type's one variable, the resource then in the
* ARN's account and Region, or the caller's and the request's where the ARN leaves them empty. An ARN
* member's value is read up to the character {@code before}, when there is one, as its handler drops the rest.
* ARN's account and Region, or the caller's and the request's where the ARN leaves them empty. A member filling
* a variable may be a field of each of a list's structures ({@code Entries[].EventBusName}), which names one
* resource for each structure, read as {@code absent} gives for one that lacks it and in the account and Region
* of the ARN that structure gives, if any. Such a member is in {@code absent} or {@code arnOf} only when it
* fills a variable. An ARN member's value is read up to the character {@code before}, when there is one, as its
* handler drops the rest.
*/
record Resource(List<String> types, Member arn, Map<String, Member> fill, Map<String, List<String>> when,
Map<String, List<String>> unless, Map<String, String> absent, Map<String, String> arnOf,
@@ -233,7 +249,7 @@ final class RequestResourceMembers {
throw new IllegalStateException(RESOURCE_NAME + " fills " + variable.getKey() + " for " + operation
+ ", no variable of " + type + "'s format");
}
fill.put(variable.getKey(), member(operation, variable.getValue().asText("")));
fill.put(variable.getKey(), fillMember(operation, variable.getValue().asText("")));
});
if (fill.values().stream().filter(Member::list).count() > 1) {
throw new IllegalStateException(RESOURCE_NAME + " fills " + type + " for " + operation
@@ -241,9 +257,10 @@ final class RequestResourceMembers {
}
Map<String, List<String>> when = values(operation, type, resource.path("when"));
Map<String, List<String>> unless = values(operation, type, resource.path("unless"));
Set<String> filled = Set.copyOf(fill.values().stream().map(Member::path).toList());
Map<String, String> absent = new LinkedHashMap<>();
resource.path("absent").properties().forEach(member -> {
if (member(operation, member.getKey()).list() || !member.getValue().isTextual()
if (!readAsOne(operation, member.getKey(), filled) || !member.getValue().isTextual()
|| member.getValue().asText().isEmpty()) {
throw new IllegalStateException(RESOURCE_NAME + " reads " + member.getKey() + " for " + operation
+ " when absent as no value");
@@ -253,7 +270,7 @@ final class RequestResourceMembers {
Map<String, String> arnOf = new LinkedHashMap<>();
resource.path("arnOf").properties().forEach(member -> {
ArnFormat named = formats.get(member.getValue().asText(""));
if (member(operation, member.getKey()).list() || named == null
if (!readAsOne(operation, member.getKey(), filled) || named == null
|| !ONE_VARIABLE.matcher(named.format().split(":", 6)[5]).matches()) {
throw new IllegalStateException(RESOURCE_NAME + " reads " + member.getKey() + " for " + operation
+ " as an ARN of " + member.getValue() + ", no type named by one variable");
@@ -306,6 +323,26 @@ final class RequestResourceMembers {
return path.endsWith("[]") ? new Member(path.substring(0, path.length() - 2), true) : new Member(path, false);
}
/**
* A member filling a variable: {@code Name}, {@code Name[]}, or a field of each of a list's structures,
* {@code Name[].Field}.
*/
private static Member fillMember(String operation, String path) {
if (!FILL_MEMBER.matcher(path).matches()) {
throw new IllegalStateException(RESOURCE_NAME + " reads " + operation + " at " + path + ", which is no member");
}
return arnMember(operation, path);
}
/**
* Whether a member gives one value as it is read: a top-level one that is no list, or a field of each of a
* list's structures that fills a variable, one value for each structure.
*/
private static boolean readAsOne(String operation, String path, Set<String> filled) {
return FILL_MEMBER.matcher(path).matches() && path.contains("[].") ? filled.contains(path)
: !member(operation, path).list();
}
/** A member holding an ARN: {@code Name}, {@code Name[]}, or a field of it, {@code Name.Field} or {@code Name[].Field}. */
private static Member arnMember(String operation, String path) {
if (!ARN_MEMBER.matcher(path).matches()) {
@@ -5,6 +5,7 @@ import jakarta.ws.rs.container.ContainerRequestContext;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
@@ -34,6 +35,16 @@ public final class RequestResources {
private static final Pattern VARIABLE = Pattern.compile("\\$\\{(\\w+)}");
private static final Pattern ACCOUNT_ID = Pattern.compile("\\d{12}");
/** Values for a type's variables, and the ARN whose account and Region the resource they name is in, if any. */
private record Filling(Map<String, String> variables, AwsArnUtils.Arn from) {
Filling with(String variable, String value, AwsArnUtils.Arn arn) {
Map<String, String> more = new HashMap<>(variables);
more.put(variable, value);
return new Filling(more, from == null ? arn : from);
}
}
private RequestResources() {
}
@@ -151,23 +162,34 @@ public final class RequestResources {
}
/**
* The values a request gives a member as the resource reads them: the one it reads for the member absent or
* given a JSON null, as a handler reading it with {@code asText(null)} does (EventBridge's {@code EventBusName}),
* and, for a member that may give an ARN of a type instead of its name, that type's one variable of such an ARN.
* The values a request gives a member as the resource reads them, as {@link #read} gives them, and, for a member
* that may give an ARN of a type instead of its name, that type's one variable of such an ARN.
*/
private static List<String> given(RequestResourceMembers.Resource resource, Map<String, String> form,
JsonNode body, RequestResourceMembers.Member member,
Map<String, RequestResourceMembers.ArnFormat> formats) {
String absent = resource.absent().get(member.name());
List<String> values = absent != null && isNull(body, member) ? List.of() : values(form, body, member);
if (values.isEmpty() && absent != null) {
return List.of(absent);
}
RequestResourceMembers.ArnFormat named = arnOf(resource, member.name(), formats);
RequestResourceMembers.ArnFormat named = arnOf(resource, member.path(), formats);
List<String> values = read(resource, form, body, member);
return named == null ? values : values.stream()
.map(value -> named.pattern().matcher(value).matches() ? named.variable(value) : value).toList();
}
/**
* The values a request gives a member: the one the resource reads for the member absent or given a JSON null,
* as a handler reading it with {@code asText(null)} does (EventBridge's {@code EventBusName}), and, for a field
* of each of a list's structures, one for each structure, that one for a structure lacking it (a
* {@code PutEvents} entry naming no event bus is published to the default one), none for no structures.
*/
private static List<String> read(RequestResourceMembers.Resource resource, Map<String, String> form,
JsonNode body, RequestResourceMembers.Member member) {
String absent = resource.absent().get(member.path());
if (absent != null && member.eachElement()) {
return eachElement(form, body, member, absent);
}
List<String> values = absent != null && isNull(body, member) ? List.of() : values(form, body, member);
return values.isEmpty() && absent != null ? List.of(absent) : values;
}
/** The format of the type whose ARN a member of the resource may give instead of its name; null for none. */
private static RequestResourceMembers.ArnFormat arnOf(RequestResourceMembers.Resource resource, String member,
Map<String, RequestResourceMembers.ArnFormat> formats) {
@@ -176,12 +198,19 @@ public final class RequestResources {
}
/**
* The ARN a request gives for a name in a member of the resource that may give one, which puts the resource in
* that ARN's account and Region (a rule on the event bus it names, the default one included); null for none.
* The ARN a request gives for a name in a member of the resource that may give one and fills none of its
* variables, which puts the resource in that ARN's account and Region (a rule on the default event bus of the
* account the bus's ARN names); null for none. A member filling a variable puts each resource it names in the
* account and Region of the ARN it gives for it, as {@link #fill} reads it.
*/
private static AwsArnUtils.Arn arnGiven(RequestResourceMembers.Resource resource, Map<String, String> form,
JsonNode body, Map<String, RequestResourceMembers.ArnFormat> formats) {
Set<String> filled = new HashSet<>();
resource.fill().values().forEach(member -> filled.add(member.path()));
for (String member : resource.arnOf().keySet()) {
if (filled.contains(member)) {
continue;
}
RequestResourceMembers.ArnFormat named = arnOf(resource, member, formats);
for (String value : values(form, body, new RequestResourceMembers.Member(member, false))) {
if (named.pattern().matcher(value).matches()) {
@@ -204,7 +233,9 @@ public final class RequestResources {
* the account the resource's account member gives, which its handler acts in, and none when that member holds
* no account id; in the request's without one, or with a JSON null for it. An ARN given for a name puts the
* resource in that ARN's account and Region, the caller's and the request's where it leaves them empty, as its
* handler then acts on the caller's resource of that name, and it is named as an ARN the request gives is. A type
* handler then acts on the caller's resource of that name, and it is named as an ARN the request gives is; for
* a member naming one resource for each of a list's structures, each in the account and Region of the ARN its
* structure gives, the request's for a name (a {@code PutEvents} entry, on the bus it names). A type
* whose service mints an id after its name is the caller's resource {@code stored} finds under that name, or
* else has that id open.
*/
@@ -221,44 +252,52 @@ public final class RequestResources {
}
account = given.isEmpty() ? accountId : given.getFirst();
}
AwsArnUtils.Arn from = arnGiven(resource, form, body, formats);
List<Map<String, String>> fillings = new ArrayList<>(List.of(Map.of()));
AwsArnUtils.Arn fromRequest = arnGiven(resource, form, body, formats);
List<Filling> fillings = new ArrayList<>(List.of(new Filling(Map.of(), fromRequest)));
for (Map.Entry<String, RequestResourceMembers.Member> variable : resource.fill().entrySet()) {
List<String> values = given(resource, form, body, variable.getValue(), formats);
List<String> values = read(resource, form, body, variable.getValue());
if (values.isEmpty()) {
continue;
}
if (resource.fill().size() == 1 && from == null
&& values.stream().allMatch(value -> format.pattern().matcher(value).matches())) {
RequestResourceMembers.ArnFormat named = arnOf(resource, variable.getValue().path(), formats);
if (resource.fill().size() == 1 && fromRequest == null && values.stream().allMatch(value ->
!isArnOf(named, value) && format.pattern().matcher(value).matches())) {
values.forEach(value -> addNamed(arns, format.checked(value), partition, region, accountId));
return;
}
List<Map<String, String>> next = new ArrayList<>();
for (Map<String, String> filling : fillings) {
List<Filling> next = new ArrayList<>();
for (Filling filling : fillings) {
for (String value : values) {
Map<String, String> more = new HashMap<>(filling);
more.put(variable.getKey(), value);
next.add(more);
next.add(isArnOf(named, value)
? filling.with(variable.getKey(), named.variable(value), AwsArnUtils.parse(value))
: filling.with(variable.getKey(), value, null));
}
}
fillings = next;
}
if (!resource.fill().isEmpty() && fillings.getFirst().isEmpty()) {
if (!resource.fill().isEmpty() && fillings.getFirst().variables().isEmpty()) {
return;
}
for (Map<String, String> filling : fillings) {
for (Filling filling : fillings) {
AwsArnUtils.Arn from = filling.from();
if (from == null) {
arns.add(withId(render(format.format(), filling, partition, region, account), format,
resource.types().getFirst(), filling, account.equals(accountId) ? stored : null, region));
arns.add(withId(render(format.format(), filling.variables(), partition, region, account), format,
resource.types().getFirst(), filling.variables(), account.equals(accountId) ? stored : null,
region));
} else {
addNamed(arns, withId(render(format.format(), filling, from.partition(),
addNamed(arns, withId(render(format.format(), filling.variables(), from.partition(),
from.region().isEmpty() ? region : from.region(),
from.accountId().isEmpty() ? accountId : from.accountId()), format,
resource.types().getFirst(), filling, null, region), partition, region, accountId);
resource.types().getFirst(), filling.variables(), null, region), partition, region, accountId);
}
}
}
/** Whether a member that may give an ARN of a type instead of its name gives one. */
private static boolean isArnOf(RequestResourceMembers.ArnFormat named, String value) {
return named != null && named.pattern().matcher(value).matches();
}
/**
* The ARN of a resource of a type whose service follows its name with an id it mints: the one {@code stored}
* finds the caller's resource of that name under in the Region, or else the ARN with that id open; any other
@@ -339,6 +378,39 @@ public final class RequestResources {
return values;
}
/**
* The value of a field of each of a list's structures, {@code absent} for one that gives none or a JSON null, as
* a handler reading it with {@code asText(null)} does: a body's list, or a form's list numbered from 1 to the
* first structure it gives nothing of; none for no list.
*/
private static List<String> eachElement(Map<String, String> form, JsonNode body,
RequestResourceMembers.Member member, String absent) {
List<String> values = new ArrayList<>();
if (!form.isEmpty()) {
String key = member.name() + ".member.";
for (int i = 1; hasElement(form, key + i + "."); i++) {
List<String> found = new ArrayList<>();
add(found, form.get(key + i + "." + member.field().name()));
values.add(found.isEmpty() ? absent : found.getFirst());
}
} else if (body != null && body.isObject() && body.path(member.name()).isArray()) {
for (JsonNode element : body.path(member.name())) {
List<String> found = new ArrayList<>();
JsonNode value = element.path(member.field().name());
if (!value.isNull()) {
addValue(found, value);
}
values.add(found.isEmpty() ? absent : found.getFirst());
}
}
return values;
}
/** Whether a form gives anything of a list's structure under {@code prefix}. */
private static boolean hasElement(Map<String, String> form, String prefix) {
return form.keySet().stream().anyMatch(key -> key.startsWith(prefix));
}
/**
* A form's values of a member under {@code prefix}: each element of a list from 1 to the first that does not
* give the member, or its field, as a handler loops over them.