fix(iam): check ARNs inside structures and EventBridge's resources (#6561)

The table of request resources read only top-level members, so an
operation naming its resource inside a structure was checked on *:
SetRulePriorities' RulePriorities[].RuleArn and a state machine alias's
routingConfiguration[].stateMachineVersionArn. A member ending in Arn or
Arns one level inside a structure, or inside each of a list's
structures, now names a resource as a top-level one does, the value's
format deciding which of the action's types it is. Such a member never
fills a variable, holds a type or names a create's parent. A form's list
is read to the first element without the field, as the ELB handler
loops.

EventBridge joins the table (71 operations). Its rules are named by the
rule's name on the event bus EventBusName names, by name or by ARN, and
on the default bus when it names none ("If you omit this, the default
event bus is used"), whose rules are rule/<name>, with no bus.
Corrections give the eight rule operations the default bus's rule when
EventBusName is default or absent, and the custom bus's otherwise, with
two new keys: unless, the complement of when, and absent, the value a
missing member is read as. DescribeEventBus naming no bus is checked on
event-bus/default ("If you omit this, the default event bus is
displayed"). A member whose botocore pattern takes an ARN before a name,
(arn:...:event-bus/)?<name>, is recorded as arnOf: an ARN given for it
is read as that type's one variable, and the resource is in the ARN's
account and Region, so a bus of another account, its default bus
included, names that account's rule, as DescribeRule's CreatedBy says,
checked as given and as the caller's own, which Floci acts on. An ARN
leaving its account and Region empty, which EventBridge's handler takes
as the caller's bus of that name, names the caller's, and a JSON null
for a member read as a value when absent is absent too, as the handler's
asText(null) reads it, so a null EventBusName is the default bus.
EventBridge V2's client, AWSEventsV2 over Smithy RPC v2 CBOR, which
Floci does not serve, shares the operations' names and is left out
(clientsNotRead), and verify ignores it.

EventBridge mints a connection's ARN as connection/<name>/<id> and an
API destination's as api-destination/<name>/<id>, as botocore's ARN
patterns give them, and checks a connection on it (the user guide grants
events:RetrieveConnectionCredentials on connection/<name>/*), while the
Service Reference's formats stop at the name. MINTED_IDS records those
types (mintedIds), and a request naming one by its name is checked on
the ARN its service stores it under, which a StoredResourceArns the
service registers finds for the caller in the request's Region
(EventBridgeService does), or on <name>/* when there is none yet, a
create's included.

Not in this change: PutEvents, whose entries each name their bus, stays
on *, and UpdateEventBus with no Name, which no document says is the
default bus, stays on * too.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-25 21:21:53 -05:00
1 parent 8d5c135f0a
commit c5e873ff84
12 files changed
+1100 -134

No files matched your search

@@ -32,23 +32,40 @@ final class RequestResourceMembers {
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final Pattern VARIABLE = Pattern.compile("\\$\\{(\\w+)}");
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_]*(\\[])?)?");
/** 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-]*)(.*)$");
/** A format's resource that is a literal and one variable. */
private static final Pattern ONE_VARIABLE = Pattern.compile("[^$]*\\$\\{\\w+}");
/** The one id a child type's format appends to its parent's. */
private static final Pattern APPENDED_ID = Pattern.compile("[/:]\\$\\{\\w+}");
/** A top-level request member that holds a string, or a list of them when {@code list}. */
record Member(String name, boolean list) {
/**
* A top-level request member that holds a string, or a list of them when {@code list}, or, with a
* {@code field}, a structure or a list of them, of which that field is read.
*/
record Member(String name, boolean list, Member field) {
Member(String name, boolean list) {
this(name, list, null);
}
}
/**
* A resource type's ARN format, with {@code ${Partition}}, {@code ${Region}}, {@code ${Account}} and the
* variables a request fills, and the pattern an ARN of the type matches; {@code unqualified} when its service
* checks an ARN of it given with a qualifier, one more {@code :<name>} past the format, without that qualifier
* (Step Functions checks a version or an alias on its state machine's ARN).
* (Step Functions checks a version or an alias on its state machine's ARN); {@code minted} when its service
* follows the one variable the format ends in with an id it mints, {@code /<id>}, which the format leaves out
* (EventBridge's {@code connection/<name>/<id>}).
*/
record ArnFormat(String format, Pattern pattern, boolean unqualified) {
record ArnFormat(String format, Pattern pattern, boolean unqualified, boolean minted) {
ArnFormat(String format, Pattern pattern, boolean unqualified) {
this(format, pattern, unqualified, false);
}
/** The ARN as its service checks it: an unqualified type's without the qualifier it may carry. */
String checked(String arn) {
@@ -65,16 +82,27 @@ final class RequestResourceMembers {
}
return arn.substring(0, at);
}
/** The one variable of a format whose resource ends in it ({@code event-bus/${EventBusName}}), from an ARN of it. */
String variable(String arn) {
String resource = format.split(":", 6)[5];
return AwsArnUtils.parse(arn).resource().substring(resource.indexOf("${"));
}
}
/**
* A resource a request names: by a member holding its ARN, which names a resource of one of {@code types} when
* it matches the type's format, or by members filling the variables of one type's format, the others open,
* only {@code when} each member named has one of its values, an empty one for the member absent, in the
* account an {@code account} member gives (ECR's {@code registryId}), or else the request's. An ARN member's
* value is read up to the character {@code before}, when there is one, as its handler drops the rest.
* only {@code when} each member named has one of its values and {@code unless} it has one of those, an empty
* one for the member absent, in the account an {@code account} member gives (ECR's {@code registryId}), or else
* 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.
*/
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,
Member account, String before) {
}
@@ -118,6 +146,7 @@ final class RequestResourceMembers {
Map<String, Map<String, ArnFormat>> formats = new LinkedHashMap<>();
document.path("arnFormats").properties().forEach(namespace -> {
Set<String> unqualified = names(document.path("unqualified").path(namespace.getKey()));
Set<String> minted = names(document.path("mintedIds").path(namespace.getKey()));
Map<String, ArnFormat> types = new LinkedHashMap<>();
namespace.getValue().properties().forEach(type -> {
String format = type.getValue().asText("");
@@ -125,7 +154,8 @@ final class RequestResourceMembers {
throw new IllegalStateException(RESOURCE_NAME + " gives " + namespace.getKey() + "'s "
+ type.getKey() + " no ARN format");
}
types.put(type.getKey(), new ArnFormat(format, pattern(format), unqualified.contains(type.getKey())));
types.put(type.getKey(), new ArnFormat(format, pattern(format), unqualified.contains(type.getKey()),
minted.contains(type.getKey())));
});
formats.put(namespace.getKey(), Map.copyOf(types));
});
@@ -136,6 +166,14 @@ final class RequestResourceMembers {
+ namespace.getValue() + " without a qualifier, no types with an ARN format");
}
});
document.path("mintedIds").properties().forEach(namespace -> {
Map<String, ArnFormat> types = formats.getOrDefault(namespace.getKey(), Map.of());
if (!namespace.getValue().isArray() || !names(namespace.getValue()).stream().allMatch(type ->
types.containsKey(type) && ONE_VARIABLE.matcher(types.get(type).format().split(":", 6)[5]).matches())) {
throw new IllegalStateException(RESOURCE_NAME + " gives " + namespace.getKey() + "'s "
+ namespace.getValue() + " a minted id, no types whose format ends in its one variable");
}
});
Map<String, List<Resource>> entries = new LinkedHashMap<>();
Map<String, List<Created>> creates = new LinkedHashMap<>();
operations.properties().forEach(operation -> {
@@ -179,8 +217,8 @@ final class RequestResourceMembers {
throw new IllegalStateException(RESOURCE_NAME + " reads an ARN for " + operation
+ " before " + before + ", no one character");
}
return new Resource(types, member(operation, resource.path("arn").asText("")), Map.of(), Map.of(), null,
before);
return new Resource(types, arnMember(operation, resource.path("arn").asText("")), Map.of(), Map.of(),
Map.of(), Map.of(), Map.of(), null, before);
}
String type = resource.path("type").asText("");
ArnFormat format = formats.get(type);
@@ -201,21 +239,48 @@ final class RequestResourceMembers {
throw new IllegalStateException(RESOURCE_NAME + " fills " + type + " for " + operation
+ " from more than one list");
}
Map<String, List<String>> when = new LinkedHashMap<>();
resource.path("when").properties().forEach(member -> {
if (member(operation, member.getKey()).list() || !member.getValue().isArray() || member.getValue().isEmpty()) {
throw new IllegalStateException(RESOURCE_NAME + " names a " + type + " for " + operation
+ " when " + member.getKey() + " has no values");
Map<String, List<String>> when = values(operation, type, resource.path("when"));
Map<String, List<String>> unless = values(operation, type, resource.path("unless"));
Map<String, String> absent = new LinkedHashMap<>();
resource.path("absent").properties().forEach(member -> {
if (member(operation, member.getKey()).list() || !member.getValue().isTextual()
|| member.getValue().asText().isEmpty()) {
throw new IllegalStateException(RESOURCE_NAME + " reads " + member.getKey() + " for " + operation
+ " when absent as no value");
}
when.put(member.getKey(), member.getValue().valueStream().map(value -> value.isNull() ? "" : value.asText())
.toList());
absent.put(member.getKey(), member.getValue().asText());
});
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
|| !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");
}
arnOf.put(member.getKey(), member.getValue().asText());
});
Member account = resource.has("account") ? member(operation, resource.path("account").asText("")) : null;
if (account != null && (account.list() || !format.format().contains("${Account}"))) {
throw new IllegalStateException(RESOURCE_NAME + " takes " + type + "'s account for " + operation
+ " from " + account.name() + ", no member of one account or a type of none");
}
return new Resource(List.of(type), null, Map.copyOf(fill), Map.copyOf(when), account, null);
return new Resource(List.of(type), null, Map.copyOf(fill), when, unless, Map.copyOf(absent), Map.copyOf(arnOf),
account, null);
}
/** The values each member named in {@code when} or {@code unless} may have, an empty one for the member absent. */
private static Map<String, List<String>> values(String operation, String type, JsonNode members) {
Map<String, List<String>> values = new LinkedHashMap<>();
members.properties().forEach(member -> {
if (member(operation, member.getKey()).list() || !member.getValue().isArray() || member.getValue().isEmpty()) {
throw new IllegalStateException(RESOURCE_NAME + " names a " + type + " for " + operation
+ " when or unless " + member.getKey() + " has no values");
}
values.put(member.getKey(), member.getValue().valueStream().map(value -> value.isNull() ? "" : value.asText())
.toList());
});
return Map.copyOf(values);
}
private static Created created(String operation, JsonNode created, Map<String, ArnFormat> formats) {
@@ -241,6 +306,19 @@ final class RequestResourceMembers {
return path.endsWith("[]") ? new Member(path.substring(0, path.length() - 2), true) : new Member(path, false);
}
/** 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()) {
throw new IllegalStateException(RESOURCE_NAME + " reads " + operation + " at " + path + ", which is no member");
}
int dot = path.indexOf('.');
if (dot < 0) {
return member(operation, path);
}
Member outer = member(operation, path.substring(0, dot));
return new Member(outer.name(), outer.list(), member(operation, path.substring(dot + 1)));
}
/**
* What an ARN of the format matches: its literal parts, a partition, any Region and account, and anything for
* each other variable but nothing.
@@ -8,6 +8,7 @@ import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -19,7 +20,9 @@ import java.util.regex.Pattern;
*
* <p>A request names a resource by a member holding its ARN, which counts when it matches the ARN format of a
* resource type the action acts on, or by members filling the variables of a type's format, the others open as
* {@code *} (a resource a request creates has the id its service mints). An ARN with a qualifier its service drops
* {@code *} (a resource a request creates has the id its service mints). A type whose service follows the name a
* request gives with an id it mints ({@code connection/<name>/<id>}) is the resource its service stores under that
* name, or else has that id open. An ARN with a qualifier its service drops
* to check it is checked without one (a state machine's version or alias, on the state machine). An ARN naming
* another account or Region than the request's is checked as given and as each resource of that name a handler may
* act on instead, in the ARN's account or the caller's and in the ARN's Region or the request's, and the request is
@@ -42,6 +45,16 @@ public final class RequestResources {
*/
public static List<String> of(String operation, ContainerRequestContext ctx, String partition, String region,
String accountId) {
return of(operation, ctx, partition, region, accountId, null);
}
/**
* As {@link #of(String, ContainerRequestContext, String, String, String)}, a resource of a type whose service
* mints an id after its name being the one {@code stored} finds for the caller under the name the request gives,
* or else its ARN with that id open, as it is with no {@code stored}.
*/
public static List<String> of(String operation, ContainerRequestContext ctx, String partition, String region,
String accountId, StoredResourceArns stored) {
RequestResourceMembers.Table table = RequestResourceMembers.table();
List<RequestResourceMembers.Resource> resources = operation == null ? null : table.operations().get(operation);
if (resources == null || resources.isEmpty()) {
@@ -52,7 +65,7 @@ public final class RequestResources {
Map<String, RequestResourceMembers.ArnFormat> formats = formats(table, operation);
Set<String> arns = new LinkedHashSet<>();
for (RequestResourceMembers.Resource resource : resources) {
if (!applies(resource, form, body)) {
if (!applies(resource, form, body, formats)) {
continue;
}
if (resource.arn() != null) {
@@ -63,7 +76,7 @@ public final class RequestResources {
accountId));
}
} else {
fill(arns, resource, formats.get(resource.types().getFirst()), form, body, partition, region, accountId);
fill(arns, resource, formats, form, body, partition, region, accountId, stored);
}
}
return List.copyOf(arns);
@@ -112,17 +125,73 @@ public final class RequestResources {
return at < 0 ? value : value.substring(0, at);
}
/** Whether each member the resource is named {@code when} has one of its values, an empty one for absent. */
private static boolean applies(RequestResourceMembers.Resource resource, Map<String, String> form, JsonNode body) {
/**
* Whether each member the resource is named {@code when} has one of its values and no member it is named
* {@code unless} has one of those, an empty one for absent.
*/
private static boolean applies(RequestResourceMembers.Resource resource, Map<String, String> form, JsonNode body,
Map<String, RequestResourceMembers.ArnFormat> formats) {
for (Map.Entry<String, List<String>> member : resource.when().entrySet()) {
List<String> values = values(form, body, new RequestResourceMembers.Member(member.getKey(), false));
if (!member.getValue().contains(values.isEmpty() ? "" : values.getFirst())) {
if (!member.getValue().contains(first(resource, form, body, member.getKey(), formats))) {
return false;
}
}
for (Map.Entry<String, List<String>> member : resource.unless().entrySet()) {
if (member.getValue().contains(first(resource, form, body, member.getKey(), formats))) {
return false;
}
}
return true;
}
private static String first(RequestResourceMembers.Resource resource, Map<String, String> form, JsonNode body,
String member, Map<String, RequestResourceMembers.ArnFormat> formats) {
List<String> given = given(resource, form, body, new RequestResourceMembers.Member(member, false), formats);
return given.isEmpty() ? "" : given.getFirst();
}
/**
* 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.
*/
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);
return named == null ? values : values.stream()
.map(value -> named.pattern().matcher(value).matches() ? named.variable(value) : value).toList();
}
/** 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) {
String type = resource.arnOf().get(member);
return type == null ? null : formats.get(type);
}
/**
* 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.
*/
private static AwsArnUtils.Arn arnGiven(RequestResourceMembers.Resource resource, Map<String, String> form,
JsonNode body, Map<String, RequestResourceMembers.ArnFormat> formats) {
for (String member : resource.arnOf().keySet()) {
RequestResourceMembers.ArnFormat named = arnOf(resource, member, formats);
for (String value : values(form, body, new RequestResourceMembers.Member(member, false))) {
if (named.pattern().matcher(value).matches()) {
return AwsArnUtils.parse(value);
}
}
}
return null;
}
/** The ARN formats of the operation's namespace, the part of {@code <namespace>:<Operation>} before its colon. */
private static Map<String, RequestResourceMembers.ArnFormat> formats(RequestResourceMembers.Table table,
String operation) {
@@ -133,11 +202,17 @@ public final class RequestResources {
* The ARNs of a type the request names through the variables it fills: one for each value of a list member,
* none when it gives none of them. A value that is itself an ARN of the type names that resource. They are in
* 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.
* 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
* 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.
*/
private static void fill(Set<String> arns, RequestResourceMembers.Resource resource,
RequestResourceMembers.ArnFormat format, Map<String, String> form, JsonNode body,
String partition, String region, String accountId) {
Map<String, RequestResourceMembers.ArnFormat> formats, Map<String, String> form,
JsonNode body, String partition, String region, String accountId,
StoredResourceArns stored) {
RequestResourceMembers.ArnFormat format = formats.get(resource.types().getFirst());
String account = accountId;
if (resource.account() != null) {
List<String> given = isNull(body, resource.account()) ? List.of() : values(form, body, resource.account());
@@ -146,13 +221,15 @@ 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()));
for (Map.Entry<String, RequestResourceMembers.Member> variable : resource.fill().entrySet()) {
List<String> values = values(form, body, variable.getValue());
List<String> values = given(resource, form, body, variable.getValue(), formats);
if (values.isEmpty()) {
continue;
}
if (resource.fill().size() == 1 && values.stream().allMatch(value -> format.pattern().matcher(value).matches())) {
if (resource.fill().size() == 1 && from == null
&& values.stream().allMatch(value -> format.pattern().matcher(value).matches())) {
values.forEach(value -> addNamed(arns, format.checked(value), partition, region, accountId));
return;
}
@@ -170,13 +247,40 @@ public final class RequestResources {
return;
}
for (Map<String, String> filling : fillings) {
arns.add(render(format.format(), filling, partition, region, account));
if (from == null) {
arns.add(withId(render(format.format(), filling, partition, region, account), format,
resource.types().getFirst(), filling, account.equals(accountId) ? stored : null, region));
} else {
addNamed(arns, withId(render(format.format(), filling, from.partition(),
from.region().isEmpty() ? region : from.region(),
from.accountId().isEmpty() ? accountId : from.accountId()), format,
resource.types().getFirst(), filling, null, region), partition, region, accountId);
}
}
}
/**
* Whether a body gives the member a JSON null, which a handler reading an account with {@code asText(null)} reads
* as none (ECR's {@code registryId}, its caller's registry then).
* 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
* type's as rendered.
*/
private static String withId(String arn, RequestResourceMembers.ArnFormat format, String type,
Map<String, String> filling, StoredResourceArns stored, String region) {
if (!format.minted()) {
return arn;
}
if (stored != null && filling.size() == 1) {
Optional<String> found = stored.find(type, filling.values().iterator().next(), region);
if (found.isPresent()) {
return found.get();
}
}
return arn + "/*";
}
/**
* Whether a body gives the member a JSON null, which a handler reading it with {@code asText(null)} reads as
* none (ECR's {@code registryId}, its caller's registry then; EventBridge's {@code EventBusName}, the default bus).
*/
private static boolean isNull(JsonNode body, RequestResourceMembers.Member member) {
return body != null && body.isObject() && body.path(member.name()).isNull();
@@ -222,34 +326,74 @@ public final class RequestResources {
/**
* 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,
* a number, a boolean or a null, and a list member's only from a list, a single value giving none.
* a number, a boolean or a null, and a list member's only from a list, a single value giving none. A member
* with a field gives that field of its structure, or of each of its list's structures.
*/
private static List<String> values(Map<String, String> form, JsonNode body, RequestResourceMembers.Member member) {
List<String> values = new ArrayList<>();
if (!form.isEmpty()) {
if (!member.list()) {
add(values, form.get(member.name()));
return values;
}
for (int i = 1; form.containsKey(member.name() + ".member." + i); i++) {
add(values, form.get(member.name() + ".member." + i));
}
return values;
}
JsonNode value = body == null || !body.isObject() ? null : body.get(member.name());
if (value == null) {
return values;
}
if (!member.list()) {
addValue(values, value);
} else if (value.isArray()) {
for (JsonNode element : value) {
addValue(values, element);
}
addFormValues(values, form, "", member);
} else if (body != null && body.isObject()) {
addBodyValues(values, body, member);
}
return values;
}
/**
* 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.
*/
private static void addFormValues(List<String> values, Map<String, String> form, String prefix,
RequestResourceMembers.Member member) {
String key = prefix + member.name();
if (!member.list()) {
if (member.field() == null) {
add(values, form.get(key));
} else {
addFormValues(values, form, key + ".", member.field());
}
return;
}
for (int i = 1; given(form, key + ".member." + i, member.field()); i++) {
if (member.field() == null) {
add(values, form.get(key + ".member." + i));
} else {
addFormValues(values, form, key + ".member." + i + ".", member.field());
}
}
}
/** Whether a form gives a list's element, or the field of it that is read. */
private static boolean given(Map<String, String> form, String element, RequestResourceMembers.Member field) {
if (field == null) {
return form.containsKey(element);
}
return form.containsKey(element + "." + field.name() + (field.list() ? ".member.1" : ""));
}
/** A body's values of a member, and of its field in its structure or in each of its list's structures. */
private static void addBodyValues(List<String> values, JsonNode node, RequestResourceMembers.Member member) {
JsonNode value = node.get(member.name());
if (value == null) {
return;
}
if (!member.list()) {
addElement(values, value, member.field());
} else if (value.isArray()) {
for (JsonNode element : value) {
addElement(values, element, member.field());
}
}
}
private static void addElement(List<String> values, JsonNode element, RequestResourceMembers.Member field) {
if (field == null) {
addValue(values, element);
} else if (element.isObject()) {
addBodyValues(values, element, field);
}
}
/**
* A body's value as text, as a handler's {@code asText()} reads it, a null as {@code null}, and nothing for a
* structure, which a handler reads as blank.
@@ -0,0 +1,23 @@
package io.github.hectorvent.floci.core.common;
import java.util.Optional;
/**
* Finds the ARN a service stores a resource under, for a type of the request-resources table whose service follows
* the name a request gives with an id it mints ({@code connection/<name>/<id>}), which only the service knows.
*
* <p>Implemented by the service that owns such resources (EventBridge, for its connections and API destinations);
* looked up lazily by {@code ResourceArnBuilder} through {@code Instance<StoredResourceArns>} for the namespace a
* request is of, so IAM never depends on that service directly.</p>
*/
public interface StoredResourceArns {
/** The namespace of the resource types it finds, as the table names it ({@code events}). */
String namespace();
/**
* The ARN the caller's resource of the type, named {@code name} in the Region, is stored under; empty when there
* is none, or the type is not one it mints an id for.
*/
Optional<String> find(String type, String name, String region);
}
@@ -19,6 +19,7 @@ import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.Pagination;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.SmithyValidationErrors;
import io.github.hectorvent.floci.core.common.StoredResourceArns;
import io.github.hectorvent.floci.core.resource.ExplorerResource;
import io.github.hectorvent.floci.core.resource.ResourceProvider;
import io.github.hectorvent.floci.core.resource.SupportedResourceType;
@@ -60,7 +61,7 @@ import java.util.UUID;
import java.util.regex.Pattern;
@ApplicationScoped
public class EventBridgeService implements ResourceProvider {
public class EventBridgeService implements ResourceProvider, StoredResourceArns {
private static final Logger LOG = Logger.getLogger(EventBridgeService.class);
private static final int MAX_LIST_LIMIT = 100;
@@ -1582,6 +1583,24 @@ public class EventBridgeService implements ResourceProvider {
return connection;
}
@Override
public String namespace() {
return "events";
}
/**
* The ARN of the caller's connection or API destination of the name in the Region, which follows the name with
* the id it was minted with, as its handlers look it up.
*/
@Override
public Optional<String> find(String type, String name, String region) {
return switch (type) {
case "connection" -> connectionStore.get(connectionKey(region, name)).map(Connection::getConnectionArn);
case "api-destination" -> apiDestinationStore.get(apiDestinationKey(region, name)).map(ApiDestination::getArn);
default -> Optional.empty();
};
}
public Connection describeConnection(String name, String region) {
return connectionStore.get(connectionKey(region, name))
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
@@ -10,6 +10,7 @@ import io.github.hectorvent.floci.core.common.AwsRegions;
import io.github.hectorvent.floci.core.common.RequestResources;
import io.github.hectorvent.floci.core.common.ServicePrincipals;
import io.github.hectorvent.floci.core.common.SigV4CredentialScope;
import io.github.hectorvent.floci.core.common.StoredResourceArns;
import io.github.hectorvent.floci.services.cloudwatch.logs.LogGroupTagTarget;
import io.github.hectorvent.floci.services.eks.EksService;
import io.github.hectorvent.floci.services.kms.KmsService;
@@ -75,19 +76,34 @@ public class ResourceArnBuilder {
private final Instance<EksService> eksService;
/** Looked up lazily for the same reason, to resolve a key named by an id, an alias or an ARN to its stored ARN. */
private final Instance<KmsService> kmsService;
/**
* Looked up lazily for the same reason, to read the ARN a resource the request-resources table names by a name
* its service follows with a minted id is stored under.
*/
private final Instance<StoredResourceArns> storedResourceArns;
@Inject
public ResourceArnBuilder(ObjectMapper objectMapper, Instance<IamService> iamService,
Instance<SecretsManagerService> secretsManagerService,
Instance<LambdaMicrovmsService> microvmsService,
Instance<EksService> eksService,
Instance<KmsService> kmsService) {
Instance<KmsService> kmsService,
Instance<StoredResourceArns> storedResourceArns) {
this.objectMapper = objectMapper;
this.iamService = iamService;
this.secretsManagerService = secretsManagerService;
this.microvmsService = microvmsService;
this.eksService = eksService;
this.kmsService = kmsService;
this.storedResourceArns = storedResourceArns;
}
public ResourceArnBuilder(ObjectMapper objectMapper, Instance<IamService> iamService,
Instance<SecretsManagerService> secretsManagerService,
Instance<LambdaMicrovmsService> microvmsService,
Instance<EksService> eksService,
Instance<KmsService> kmsService) {
this(objectMapper, iamService, secretsManagerService, microvmsService, eksService, kmsService, null);
}
public ResourceArnBuilder(ObjectMapper objectMapper, Instance<IamService> iamService,
@@ -173,15 +189,29 @@ public class ResourceArnBuilder {
/**
* The resources a request of a namespace this builder has no branch for names, from the vendored table of
* request resources (see {@link RequestResources}); {@code *} when it names none, or the operation is not one
* of the namespace.
* of the namespace. A resource whose service mints an id after its name is the one the namespace's
* {@link StoredResourceArns} finds under that name.
*/
private static List<String> namedResources(String credentialScope, String operation, ContainerRequestContext ctx,
String region, String accountId, String partition) {
private List<String> namedResources(String credentialScope, String operation, ContainerRequestContext ctx,
String region, String accountId, String partition) {
List<String> named = operation == null || !operation.startsWith(credentialScope + ":") ? List.of()
: RequestResources.of(operation, ctx, partition, region, accountId);
: RequestResources.of(operation, ctx, partition, region, accountId, storedResourceArns(credentialScope));
return named.isEmpty() ? List.of("*") : named;
}
/** The registered finder of the namespace's stored ARNs; null when there is none. */
private StoredResourceArns storedResourceArns(String namespace) {
if (storedResourceArns == null || storedResourceArns.isUnsatisfied()) {
return null;
}
for (StoredResourceArns stored : storedResourceArns) {
if (stored.namespace().equals(namespace)) {
return stored;
}
}
return null;
}
private String buildResourceGroupArn(ContainerRequestContext ctx, String region, String accountId) {
JsonNode body = readJsonBody(ctx);
if (body == null) {