mirror of
https://github.com/floci-io/floci.git
synced 2026-11-26 23:29:28 -05:00
fix(controltower): generate IAM routes and request resources (#7872)
This commit is contained in:
1 parent
c2aba45553
commit
ef1e00fba7
21 files changed
+916
-104
No files matched your search
@@ -3,6 +3,7 @@ package io.floci.aws.core.common;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.services.iam.IamActionRegistry;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
import jakarta.ws.rs.core.MultivaluedMap;
|
||||
|
||||
@@ -262,7 +263,7 @@ public final class RequestResources {
|
||||
*/
|
||||
private static JsonNode located(RequestResourceMembers.Table table, String operation, JsonNode body,
|
||||
ContainerRequestContext ctx, Map<String, String> labels) {
|
||||
Map<String, String> located = table.located().getOrDefault(operation, Map.of());
|
||||
Map<String, String> located = locatedMembers(table, operation, ctx);
|
||||
if (located.isEmpty()) {
|
||||
return body;
|
||||
}
|
||||
@@ -281,7 +282,7 @@ public final class RequestResources {
|
||||
private static Map<String, String> located(RequestResourceMembers.Table table, String operation,
|
||||
Map<String, String> form, ContainerRequestContext ctx,
|
||||
Map<String, String> labels) {
|
||||
Map<String, String> located = table.located().getOrDefault(operation, Map.of());
|
||||
Map<String, String> located = locatedMembers(table, operation, ctx);
|
||||
if (form.isEmpty() || located.isEmpty()) {
|
||||
return form;
|
||||
}
|
||||
@@ -291,6 +292,24 @@ public final class RequestResources {
|
||||
return overlaid;
|
||||
}
|
||||
|
||||
private static Map<String, String> locatedMembers(RequestResourceMembers.Table table, String operation,
|
||||
ContainerRequestContext ctx) {
|
||||
Map<String, String> located = table.located().getOrDefault(operation, Map.of());
|
||||
Map<String, String> alternatives = IamActionRegistry.routeMemberLocations(operation, ctx);
|
||||
if (alternatives.isEmpty()) {
|
||||
return located;
|
||||
}
|
||||
Map<String, String> merged = new HashMap<>(located);
|
||||
alternatives.forEach((member, source) -> {
|
||||
if ("body".equals(source)) {
|
||||
merged.remove(member);
|
||||
} else {
|
||||
merged.put(member, source);
|
||||
}
|
||||
});
|
||||
return merged;
|
||||
}
|
||||
|
||||
/**
|
||||
* The value the request gives each member the table locates, by the member's name: a path label's from
|
||||
* {@code labels}, a query parameter's first value; none for one the request does not give.
|
||||
@@ -469,7 +488,8 @@ public final class RequestResources {
|
||||
RequestResourceMembers.ArnFormat named = arnOf(resource, variable.getValue().path(), formats);
|
||||
if (resource.fill().size() == 1 && fromRequest == null) {
|
||||
List<String> asArns = values.stream()
|
||||
.filter(value -> !isArnOf(named, value) && format.pattern().matcher(value).matches()).toList();
|
||||
.filter(value -> !isArnOf(named, value)
|
||||
&& format.pattern().matcher(value).matches()).toList();
|
||||
asArns.forEach(value -> addNamed(arns, format.checked(value), format.awsOwned(), partition, region,
|
||||
accountId));
|
||||
values = values.stream().filter(value -> !asArns.contains(value)).toList();
|
||||
|
||||
@@ -434,11 +434,6 @@ public class IamActionRegistry {
|
||||
rule("eks", "POST", "^/tags(/.+)?$", "eks:TagResource"),
|
||||
rule("eks", "DELETE", "^/tags(/.+)?$", "eks:UntagResource"),
|
||||
|
||||
// ── Control Tower ──────────────────────────────────────────────────────
|
||||
rule("controltower", "GET", "^/tags(/.+)?$", "controltower:ListTagsForResource"),
|
||||
rule("controltower", "POST", "^/tags(/.+)?$", "controltower:TagResource"),
|
||||
rule("controltower", "DELETE", "^/tags(/.+)?$", "controltower:UntagResource"),
|
||||
|
||||
// ── Bedrock ────────────────────────────────────────────────────────────
|
||||
rule("bedrock", "POST", "^/resource-policy$", "bedrock:PutResourcePolicy"),
|
||||
rule("bedrock", "GET", "^/resource-policy/.+", "bedrock:GetResourcePolicy"),
|
||||
@@ -763,6 +758,19 @@ public class IamActionRegistry {
|
||||
.map(IamRestRoutes.Match::labels).orElse(Map.of());
|
||||
}
|
||||
|
||||
/** 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) {
|
||||
int separator = operation.indexOf(':');
|
||||
if (separator < 0 || ctx.getMethod() == null || ctx.getUriInfo() == null
|
||||
|| ctx.getUriInfo().getRequestUri() == null) {
|
||||
return Map.of();
|
||||
}
|
||||
return IamRestRoutes.vendored().match(operation.substring(0, separator), ctx.getMethod().toUpperCase(),
|
||||
routedPath(ctx), ctx.getUriInfo().getQueryParameters())
|
||||
.filter(match -> match.operation().equals(operation.substring(separator + 1)))
|
||||
.map(IamRestRoutes.Match::located).orElse(Map.of());
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a request of the operation is checked on the resources AWS checks its action on: those its request
|
||||
* names, which the vendored request resources read for the operation ({@link RequestResources#reads}) or
|
||||
|
||||
@@ -54,15 +54,16 @@ final class IamRestRoutes {
|
||||
private static final int ONE_SEGMENT = 1;
|
||||
private static final int REST_OF_PATH = 0;
|
||||
|
||||
/** A route a request matched: its operation, and the value its path gives each of the route's labels, decoded. */
|
||||
record Match(String operation, Map<String, String> labels) {
|
||||
/** A matched operation, decoded path labels, and generated alternative member locations. */
|
||||
record Match(String operation, Map<String, String> labels, Map<String, String> located) {
|
||||
}
|
||||
|
||||
/**
|
||||
* A route: its operation, its path as a pattern capturing each label in turn, the labels' names, each segment's
|
||||
* rank, and the query it names.
|
||||
*/
|
||||
private record Route(String operation, Pattern path, List<String> labels, int[] ranks, Map<String, String> query) {
|
||||
private record Route(String operation, Pattern path, List<String> labels, int[] ranks, Map<String, String> query,
|
||||
Map<String, String> located) {
|
||||
|
||||
boolean matches(String requestPath, MultivaluedMap<String, String> parameters) {
|
||||
if (!path.matcher(requestPath).matches()) {
|
||||
@@ -134,7 +135,7 @@ final class IamRestRoutes {
|
||||
best = route;
|
||||
}
|
||||
}
|
||||
return best == null ? Optional.empty() : Optional.of(new Match(best.operation(), best.labelValues(requestPath)));
|
||||
return best == null ? Optional.empty() : Optional.of(new Match(best.operation(), best.labelValues(requestPath), best.located()));
|
||||
}
|
||||
|
||||
/** The modelled denial status, or the existing protocol default when the model declares none. */
|
||||
@@ -188,7 +189,31 @@ final class IamRestRoutes {
|
||||
}
|
||||
routes.computeIfAbsent(scope, ignored -> new HashMap<>())
|
||||
.computeIfAbsent(method, ignored -> new ArrayList<>())
|
||||
.add(route(operation.getKey(), requestUri));
|
||||
.add(route(operation.getKey(), requestUri, Map.of()));
|
||||
JsonNode alternatives = operation.getValue().path("alternatives");
|
||||
if (!alternatives.isMissingNode() && !alternatives.isArray()) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " gives " + operation.getKey()
|
||||
+ " invalid alternative routes");
|
||||
}
|
||||
alternatives.forEach(alternative -> {
|
||||
String uri = alternative.path("requestUri").asText("");
|
||||
JsonNode members = alternative.path("located");
|
||||
if (!uri.startsWith("/") || !members.isObject()) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " gives " + operation.getKey()
|
||||
+ " an invalid alternative route");
|
||||
}
|
||||
Map<String, String> located = new HashMap<>();
|
||||
members.properties().forEach(member -> {
|
||||
String source = member.getValue().asText("");
|
||||
if (!member.getValue().isTextual() || member.getKey().isEmpty()
|
||||
|| !("body".equals(source) || source.startsWith("?") && source.length() > 1)) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " gives " + operation.getKey()
|
||||
+ " an invalid alternative member location");
|
||||
}
|
||||
located.put(member.getKey(), source);
|
||||
});
|
||||
routes.get(scope).get(method).add(route(operation.getKey(), uri, Map.copyOf(located)));
|
||||
});
|
||||
});
|
||||
});
|
||||
Map<String, Map<String, List<Route>>> copied = new HashMap<>();
|
||||
@@ -201,7 +226,7 @@ final class IamRestRoutes {
|
||||
}
|
||||
|
||||
/** A route of the operation from its request URI, its path's segments and the query after its {@code ?}. */
|
||||
private static Route route(String operation, String requestUri) {
|
||||
private static Route route(String operation, String requestUri, Map<String, String> located) {
|
||||
int question = requestUri.indexOf('?');
|
||||
String path = IamActionRegistry.withoutTrailingSlash(question < 0 ? requestUri : requestUri.substring(0, question));
|
||||
List<String> segments = path.equals("/") ? List.of() : List.of(path.substring(1).split("/", -1));
|
||||
@@ -238,6 +263,6 @@ final class IamRestRoutes {
|
||||
}
|
||||
}
|
||||
return new Route(operation, Pattern.compile(pattern.isEmpty() ? "/" : pattern.toString()), List.copyOf(labels),
|
||||
ranks, Map.copyOf(query));
|
||||
ranks, Map.copyOf(query), located);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user