mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(iam): read members clients locate apart, S3's key shape and unread bodies (#9151)
Four rules of the request-resources generator that S3 needs to be read from the table, which leave the
table as it is while S3 is still built by hand.
A member the clients of an operation carry in different places, a body the reader reads among them, is
recorded under each client locating it (locatedApart: S3's CreateBucket carries its Bucket as {Bucket},
S3 Control's as {name}), and the reader takes the members of the client whose vendored route the request
matched; a split by a client with no vendored routes is refused. A member whose botocore shape is named
as the type and Key fills the type's name (S3's Key, an ObjectKey, the object's ${ObjectName}). A type
named exactly as what an operation makes rules out one only ending with that name (PutObject makes an
object, not an accesspointobject). An operation of a REST-XML model naming what it acts on in its body
by such a key shape, which the reader does not read, is recorded unread (DeleteObjects'
Delete.Objects[].Key), and RequestResources.reads, of and ofBody leave it to its service.
This commit is contained in:
1 parent
27de6fd46e
commit
1a107f8f46
6 files changed
+360
-33
No files matched your search
@@ -7,6 +7,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -53,6 +54,11 @@ final class RequestResourceMembers {
|
||||
/** A member a REST request carries outside its body, by its name, and where: a path label or a query parameter. */
|
||||
private static final Pattern LOCATED_MEMBER = Pattern.compile("[A-Za-z][A-Za-z0-9_]*");
|
||||
private static final Pattern LOCATION = Pattern.compile("\\{[A-Za-z0-9_]+}|\\?[A-Za-z0-9_.-]+");
|
||||
// A botocore client, as the vendored routes name its model.
|
||||
private static final Pattern CLIENT = Pattern.compile("[a-z0-9][a-z0-9-]*");
|
||||
// A member of a request's body at any depth: Delete.Objects[].Key.
|
||||
private static final Pattern BODY_PATH = Pattern.compile(
|
||||
"[A-Za-z][A-Za-z0-9_]*(\\[])?(\\.[A-Za-z][A-Za-z0-9_]*(\\[])?)*");
|
||||
/** The one id a child type's format appends to its parent's. */
|
||||
private static final Pattern APPENDED_ID = Pattern.compile("[/:]\\$\\{\\w+}");
|
||||
/** A word of a child type's own and one id, which its format appends to its parent's whole resource. */
|
||||
@@ -326,9 +332,11 @@ final class RequestResourceMembers {
|
||||
* {@code <namespace>:<Operation>}, the members a REST operation's request carries outside its body, each
|
||||
* namespace's ARN formats, and the namespaces it reads, those built by hand, those not read yet, and the others
|
||||
* with resource types. A located member is a label of the request's path, {@code {Label}}, or a parameter of its
|
||||
* query, {@code ?name}, by the member's name. Each operation's {@code forwarded} requests are those its service
|
||||
* makes to another as the caller, and its {@code statements} those it makes as the caller to run each statement its
|
||||
* request may carry.
|
||||
* query, {@code ?name}, by the member's name, and one the clients of an operation locate apart is under each
|
||||
* client ({@code locatedApart}), whose vendored route tells a request's client. Each operation's {@code forwarded}
|
||||
* requests are those its service makes to another as the caller, and its {@code statements} those it makes as the
|
||||
* caller to run each statement its request may carry. An {@code unread} operation names what it acts on in a body
|
||||
* the reader does not read.
|
||||
*/
|
||||
record Table(Map<String, List<Resource>> operations, Map<String, List<String>> types,
|
||||
Map<String, List<Created>> creates, Map<String, List<Resource>> makes,
|
||||
@@ -338,7 +346,8 @@ final class RequestResourceMembers {
|
||||
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, Map<String, Map<String, List<StatementRequest>>> statements) {
|
||||
Map<String, List<Forwarded>> forwarded, Map<String, Map<String, List<StatementRequest>>> statements,
|
||||
Map<String, Map<String, Map<String, String>>> locatedApart, Set<String> unread) {
|
||||
}
|
||||
|
||||
private static final class Holder {
|
||||
@@ -443,6 +452,8 @@ final class RequestResourceMembers {
|
||||
Map<String, Targets> targets = new LinkedHashMap<>();
|
||||
Map<String, Map<Member, String>> ec2Query = new LinkedHashMap<>();
|
||||
Map<String, Map<String, String>> located = new LinkedHashMap<>();
|
||||
Map<String, Map<String, Map<String, String>>> locatedApart = new LinkedHashMap<>();
|
||||
Set<String> unread = new LinkedHashSet<>();
|
||||
Map<String, List<Forwarded>> forwarded = new LinkedHashMap<>();
|
||||
JsonNode forwardedActions = document.path("forwardedActions");
|
||||
operations.properties().forEach(operation -> {
|
||||
@@ -530,6 +541,48 @@ final class RequestResourceMembers {
|
||||
if (!where.isEmpty()) {
|
||||
located.put(operation.getKey(), Map.copyOf(where));
|
||||
}
|
||||
JsonNode locatedApartNode = operation.getValue().path("locatedApart");
|
||||
if (!locatedApartNode.isMissingNode() && (!locatedApartNode.isObject() || locatedApartNode.isEmpty())) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " locates " + operation.getKey()
|
||||
+ "'s members apart under no client");
|
||||
}
|
||||
Map<String, Map<String, String>> apart = new LinkedHashMap<>();
|
||||
locatedApartNode.properties().forEach(client -> {
|
||||
if (!client.getValue().isObject()) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " locates " + operation.getKey() + "'s members for "
|
||||
+ client.getKey() + " as " + client.getValue() + ", no members");
|
||||
}
|
||||
Map<String, String> members = new LinkedHashMap<>();
|
||||
client.getValue().properties().forEach(member -> {
|
||||
if (!CLIENT.matcher(client.getKey()).matches() || !LOCATED_MEMBER.matcher(member.getKey()).matches()
|
||||
|| !LOCATION.matcher(member.getValue().asText("")).matches()) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " locates " + operation.getKey() + "'s "
|
||||
+ member.getKey() + " at " + member.getValue() + " for " + client.getKey()
|
||||
+ ", no client's label of a path or query parameter");
|
||||
}
|
||||
members.put(member.getKey(), member.getValue().asText());
|
||||
});
|
||||
if (members.isEmpty()) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " locates none of " + operation.getKey()
|
||||
+ "'s members for " + client.getKey());
|
||||
}
|
||||
apart.put(client.getKey(), Map.copyOf(members));
|
||||
});
|
||||
if (!apart.isEmpty()) {
|
||||
locatedApart.put(operation.getKey(), Map.copyOf(apart));
|
||||
}
|
||||
JsonNode unreadNode = operation.getValue().path("unread");
|
||||
if (!unreadNode.isMissingNode() && (!unreadNode.isArray() || unreadNode.isEmpty())) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " leaves " + operation.getKey()
|
||||
+ " unread on no member of its body");
|
||||
}
|
||||
for (JsonNode member : unreadNode) {
|
||||
if (!member.isTextual() || !BODY_PATH.matcher(member.asText()).matches()) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " leaves " + operation.getKey() + "'s " + member
|
||||
+ " unread, no member of a request's body");
|
||||
}
|
||||
unread.add(operation.getKey());
|
||||
}
|
||||
List<Forwarded> forwards = new ArrayList<>();
|
||||
for (JsonNode use : operation.getValue().path("forwarded")) {
|
||||
forwards.add(forwarded(operation.getKey(), use, types, formats.getOrDefault(namespace, Map.of()),
|
||||
@@ -544,7 +597,8 @@ final class RequestResourceMembers {
|
||||
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), statements(document, formats));
|
||||
names(document.path("otherNamespaces")), Map.copyOf(forwarded), statements(document, formats),
|
||||
Map.copyOf(locatedApart), Set.copyOf(unread));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -65,9 +65,18 @@ public final class RequestResources {
|
||||
private RequestResources() {
|
||||
}
|
||||
|
||||
/** Whether the generated table owns this operation, including those of hand-built namespaces it reads. */
|
||||
/**
|
||||
* Whether the generated table owns this operation, including those of hand-built namespaces it reads. An operation
|
||||
* naming what it acts on in a body the reader does not read ({@code unread}: S3's DeleteObjects, its keys in an XML
|
||||
* body) is not read, as checked on the rest of its request it would be checked on none of what it names.
|
||||
*/
|
||||
public static boolean reads(String operation) {
|
||||
return operation != null && RequestResourceMembers.table().operations().containsKey(operation);
|
||||
return readable(RequestResourceMembers.table(), operation);
|
||||
}
|
||||
|
||||
/** Whether {@code table} reads the operation's requests: it has an entry for it, and not an unread one. */
|
||||
static boolean readable(RequestResourceMembers.Table table, String operation) {
|
||||
return operation != null && table.operations().containsKey(operation) && !table.unread().contains(operation);
|
||||
}
|
||||
|
||||
/** Whether {@code ResourceArnBuilder} builds the resources of this namespace's requests itself. */
|
||||
@@ -175,7 +184,8 @@ public final class RequestResources {
|
||||
return targetArns(targets, formats(table, operation), form,
|
||||
form.isEmpty() ? PassedRoles.jsonBody(ctx) : null, partition, region, accountId);
|
||||
}
|
||||
List<RequestResourceMembers.Resource> resources = operation == null ? null
|
||||
// An operation the table does not read, an unread one among them, names nothing from it.
|
||||
List<RequestResourceMembers.Resource> resources = !readable(table, operation) ? null
|
||||
: served(table.operations().get(operation), ctx);
|
||||
if (resources == null) {
|
||||
return List.of();
|
||||
@@ -199,7 +209,8 @@ public final class RequestResources {
|
||||
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);
|
||||
List<RequestResourceMembers.Resource> resources = !readable(table, operation) ? null
|
||||
: table.operations().get(operation);
|
||||
if (resources == null || resources.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
@@ -509,12 +520,26 @@ public final class RequestResources {
|
||||
|
||||
private static Map<String, String> locatedMembers(RequestResourceMembers.Table table, String operation,
|
||||
ContainerRequestContext ctx) {
|
||||
IamActionRegistry.RouteMembers route = IamActionRegistry.routeMembers(operation, ctx);
|
||||
return locatedMembers(table, operation, route.model(), route.located());
|
||||
}
|
||||
|
||||
/**
|
||||
* The members the table locates for a request of the operation whose route is the {@code model}'s: those every
|
||||
* client locates alike, those it locates apart for that model ({@code locatedApart}: S3 Control's CreateBucket
|
||||
* carries its {@code Bucket} as {@code {name}}, S3's as {@code {Bucket}}), and those a generated alternative route
|
||||
* locates ({@code alternatives}), {@code body} for one its body carries there.
|
||||
*/
|
||||
static Map<String, String> locatedMembers(RequestResourceMembers.Table table, String operation, String model,
|
||||
Map<String, String> alternatives) {
|
||||
Map<String, String> located = table.located().getOrDefault(operation, Map.of());
|
||||
Map<String, String> alternatives = IamActionRegistry.routeMemberLocations(operation, ctx);
|
||||
if (alternatives.isEmpty()) {
|
||||
Map<String, String> apart = model == null ? Map.of()
|
||||
: table.locatedApart().getOrDefault(operation, Map.of()).getOrDefault(model, Map.of());
|
||||
if (alternatives.isEmpty() && apart.isEmpty()) {
|
||||
return located;
|
||||
}
|
||||
Map<String, String> merged = new HashMap<>(located);
|
||||
merged.putAll(apart);
|
||||
alternatives.forEach((member, source) -> {
|
||||
if ("body".equals(source)) {
|
||||
merged.remove(member);
|
||||
|
||||
@@ -792,18 +792,27 @@ public class IamActionRegistry {
|
||||
.orElse(action);
|
||||
}
|
||||
|
||||
/** Member locations of a generated alternative route; absent entries keep the operation's model locations. */
|
||||
public static Map<String, String> routeMemberLocations(String operation, ContainerRequestContext ctx) {
|
||||
/**
|
||||
* The vendored route a request of an operation matched: the member locations of a generated alternative route
|
||||
* ({@code located}; absent entries keep the operation's model locations) and the botocore model whose route it is
|
||||
* ({@code model}), which tells apart the clients of an operation several serve.
|
||||
*/
|
||||
public record RouteMembers(Map<String, String> located, String model) {
|
||||
static final RouteMembers NONE = new RouteMembers(Map.of(), null);
|
||||
}
|
||||
|
||||
/** The route a request of the operation matches, {@link RouteMembers#NONE} for one it matches none of. */
|
||||
public static RouteMembers routeMembers(String operation, ContainerRequestContext ctx) {
|
||||
int separator = operation.indexOf(':');
|
||||
if (separator < 0 || ctx.getMethod() == null || ctx.getUriInfo() == null
|
||||
|| ctx.getUriInfo().getRequestUri() == null) {
|
||||
return Map.of();
|
||||
return RouteMembers.NONE;
|
||||
}
|
||||
return IamRestRoutes.vendored().match(operation.substring(0, separator), ctx.getMethod().toUpperCase(),
|
||||
withoutInternalPrefix(ctx, routedPath(ctx)), ctx.getUriInfo().getQueryParameters(),
|
||||
movedUnderPrefix(ctx), ctx.getHeaders())
|
||||
.filter(match -> match.operation().equals(operation.substring(separator + 1)))
|
||||
.map(IamRestRoutes.Match::located).orElse(Map.of());
|
||||
.map(match -> new RouteMembers(match.located(), match.model())).orElse(RouteMembers.NONE);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user