mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(iam): tell apart the operations of a REST route by what botocore requires (#9143)
Thirteen of botocore's S3 routes name more than one operation (PUT /{Bucket}/{Key+} is PutObject,
CopyObject, UploadPart or UploadPartCopy), which is why S3 is hand-routed. The route generator now gives
each operation of a shared route the query parameters and headers of the members its input requires, and
marks a deprecated one. An operation botocore's endpoint rules sign for another service on every request
is left out of the scope's routes and listed under signedElsewhere: S3's ListDirectoryBuckets
(s3express) and WriteGetObjectResponse (s3-object-lambda). The check that a route's operations leave
every request one operation now covers the hand-routed scopes too: no two current ones may require the
same, and what any two require together must be what one of them requires. It passes for S3.
IamRestRoutes matches a route only for a request carrying all its operation requires, headers in any
case, ranks a route requiring all another requires and more above it and a current operation above a
deprecated one, and gives a match its model. IamActionRegistry passes the request's headers wherever it
matches a route. S3 stays hand-routed and no route outside it is shared, so no request is authorized
differently.
This commit is contained in:
1 parent
4595e8d1ef
commit
2e00917c69
6 files changed
+390
-58
No files matched your search
@@ -710,8 +710,9 @@ public class IamActionRegistry {
|
||||
return rule.action();
|
||||
}
|
||||
}
|
||||
Optional<String> operation = restRoutes.operation(credentialScope, method, withoutInternalPrefix(ctx, path),
|
||||
ctx.getUriInfo() == null ? null : ctx.getUriInfo().getQueryParameters(), movedUnderPrefix(ctx));
|
||||
Optional<String> operation = restRoutes.match(credentialScope, method, withoutInternalPrefix(ctx, path),
|
||||
ctx.getUriInfo() == null ? null : ctx.getUriInfo().getQueryParameters(), movedUnderPrefix(ctx),
|
||||
ctx.getHeaders()).map(IamRestRoutes.Match::operation);
|
||||
if (operation.isPresent()) {
|
||||
String action = IamAuthorizedActions.actionFor(credentialScope, operation.get(), method);
|
||||
if (checkedAsAws(credentialScope, operation.get(), action)) {
|
||||
@@ -759,7 +760,8 @@ public class IamActionRegistry {
|
||||
private static Optional<IamRestRoutes.Match> matchedRoute(IamRestRoutes routes, String credentialScope,
|
||||
ContainerRequestContext ctx) {
|
||||
return routes.match(credentialScope, ctx.getMethod().toUpperCase(), withoutInternalPrefix(ctx, routedPath(ctx)),
|
||||
ctx.getUriInfo() == null ? null : ctx.getUriInfo().getQueryParameters(), movedUnderPrefix(ctx));
|
||||
ctx.getUriInfo() == null ? null : ctx.getUriInfo().getQueryParameters(), movedUnderPrefix(ctx),
|
||||
ctx.getHeaders());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -780,8 +782,10 @@ public class IamActionRegistry {
|
||||
return action;
|
||||
}
|
||||
String method = ctx.getMethod().toUpperCase();
|
||||
return restRoutes.operation(credentialScope, method, withoutInternalPrefix(ctx, routedPath(ctx)),
|
||||
ctx.getUriInfo() == null ? null : ctx.getUriInfo().getQueryParameters(), movedUnderPrefix(ctx))
|
||||
return restRoutes.match(credentialScope, method, withoutInternalPrefix(ctx, routedPath(ctx)),
|
||||
ctx.getUriInfo() == null ? null : ctx.getUriInfo().getQueryParameters(), movedUnderPrefix(ctx),
|
||||
ctx.getHeaders())
|
||||
.map(IamRestRoutes.Match::operation)
|
||||
.filter(operation -> action.equals(IamAuthorizedActions.actionFor(credentialScope, operation, method)))
|
||||
.map(operation -> credentialScope + ":" + operation)
|
||||
.orElse(action);
|
||||
@@ -796,7 +800,7 @@ public class IamActionRegistry {
|
||||
}
|
||||
return IamRestRoutes.vendored().match(operation.substring(0, separator), ctx.getMethod().toUpperCase(),
|
||||
withoutInternalPrefix(ctx, routedPath(ctx)), ctx.getUriInfo().getQueryParameters(),
|
||||
movedUnderPrefix(ctx))
|
||||
movedUnderPrefix(ctx), ctx.getHeaders())
|
||||
.filter(match -> match.operation().equals(operation.substring(separator + 1)))
|
||||
.map(IamRestRoutes.Match::located).orElse(Map.of());
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
@@ -71,18 +72,22 @@ final class IamRestRoutes {
|
||||
private static final int REST_OF_PATH = 0;
|
||||
|
||||
/**
|
||||
* A matched operation, decoded path labels, generated alternative member locations, and whether the route is a
|
||||
* documented model's, whose request carries none of the operation's members.
|
||||
* A matched operation, decoded path labels, generated alternative member locations, whether the route is a
|
||||
* documented model's, whose request carries none of the operation's members, and the model whose route it is.
|
||||
*/
|
||||
record Match(String operation, Map<String, String> labels, Map<String, String> located, boolean documented) {
|
||||
record Match(String operation, Map<String, String> labels, Map<String, String> located, boolean documented,
|
||||
String model) {
|
||||
}
|
||||
|
||||
/**
|
||||
* A route: its operation, its path as a pattern capturing each label in turn, the labels' names, each segment's
|
||||
* rank, the query it names, the members its alternative locates, and whether its model is a documented one.
|
||||
* rank, the query it names, the members its alternative locates, whether its model is a documented one, the
|
||||
* query parameters and headers its operation's request must carry where several operations share the route
|
||||
* ({@code requires}), whether botocore marks the operation deprecated, and its model.
|
||||
*/
|
||||
private record Route(String operation, Pattern path, Pattern spanning, List<String> labels, int[] ranks,
|
||||
Map<String, String> query, Map<String, String> located, boolean documented) {
|
||||
Map<String, String> query, Map<String, String> located, boolean documented,
|
||||
Set<String> requiredQuery, Set<String> requiredHeaders, boolean deprecated, String model) {
|
||||
|
||||
/** The route's pattern: each label taking one segment, or, spanning, the segments its value spans. */
|
||||
Pattern pattern(boolean spanned) {
|
||||
@@ -99,7 +104,16 @@ final class IamRestRoutes {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return requiredQuery.stream().allMatch(parameter -> parameters != null && parameters.containsKey(parameter));
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the request carries every header the route's operation requires; with no headers known, a route
|
||||
* requiring one cannot be told to match.
|
||||
*/
|
||||
boolean carries(MultivaluedMap<String, String> headers) {
|
||||
return requiredHeaders.isEmpty() || headers != null && requiredHeaders.stream()
|
||||
.allMatch(header -> headers.keySet().stream().anyMatch(header::equalsIgnoreCase));
|
||||
}
|
||||
|
||||
/** The value the path gives each label, decoded as the router decodes a path parameter. */
|
||||
@@ -116,10 +130,35 @@ final class IamRestRoutes {
|
||||
return Map.copyOf(values);
|
||||
}
|
||||
|
||||
/** Whether this route is more specific than the other, as {@link IamRestRoutes} orders them. */
|
||||
/**
|
||||
* Whether this route is more specific than the other, as {@link IamRestRoutes} orders them: by its segments, the
|
||||
* query it names, then by what its operation's request must carry, a route requiring all the other requires and
|
||||
* more outranking it, and a current operation before a deprecated one requiring the same. The generator lets a
|
||||
* route's operations through only when what any two require together is what one of them requires, so of the
|
||||
* routes a request matches one requires all the others do (UploadPartCopy, over CopyObject and UploadPart).
|
||||
*/
|
||||
boolean outranks(Route other) {
|
||||
int compared = Arrays.compare(ranks, other.ranks);
|
||||
return compared != 0 ? compared > 0 : query.size() > other.query.size();
|
||||
if (compared != 0) {
|
||||
return compared > 0;
|
||||
}
|
||||
if (query.size() != other.query.size()) {
|
||||
return query.size() > other.query.size();
|
||||
}
|
||||
Set<String> needs = needs();
|
||||
Set<String> otherNeeds = other.needs();
|
||||
if (!needs.equals(otherNeeds)) {
|
||||
return needs.containsAll(otherNeeds) || !otherNeeds.containsAll(needs) && needs.size() > otherNeeds.size();
|
||||
}
|
||||
return !deprecated && other.deprecated;
|
||||
}
|
||||
|
||||
/** What the route's operation's request must carry: its query parameters, then its headers. */
|
||||
private Set<String> needs() {
|
||||
Set<String> needs = new HashSet<>();
|
||||
requiredQuery.forEach(parameter -> needs.add("?" + parameter));
|
||||
requiredHeaders.forEach(header -> needs.add(header.toLowerCase(Locale.ROOT) + ":"));
|
||||
return needs;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -171,22 +210,32 @@ final class IamRestRoutes {
|
||||
/** As {@link #operation(String, String, String, MultivaluedMap, boolean)}, with the label values. */
|
||||
Optional<Match> match(String scope, String method, String path, MultivaluedMap<String, String> query,
|
||||
boolean movedUnderPrefix) {
|
||||
return match(scope, method, path, query, movedUnderPrefix, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* As {@link #match(String, String, String, MultivaluedMap, boolean)}, the headers a route's operation requires
|
||||
* checked against the request's {@code headers}.
|
||||
*/
|
||||
Optional<Match> match(String scope, String method, String path, MultivaluedMap<String, String> query,
|
||||
boolean movedUnderPrefix, MultivaluedMap<String, String> headers) {
|
||||
String requestPath = IamActionRegistry.withoutTrailingSlash(path);
|
||||
Optional<Match> match = best(scope, method, requestPath, query, false);
|
||||
return match.isPresent() || !movedUnderPrefix ? match : best(scope, method, requestPath, query, true);
|
||||
Optional<Match> match = best(scope, method, requestPath, query, false, headers);
|
||||
return match.isPresent() || !movedUnderPrefix ? match : best(scope, method, requestPath, query, true, headers);
|
||||
}
|
||||
|
||||
private Optional<Match> best(String scope, String method, String requestPath, MultivaluedMap<String, String> query,
|
||||
boolean spanned) {
|
||||
boolean spanned, MultivaluedMap<String, String> headers) {
|
||||
Route best = null;
|
||||
for (Route route : routes.getOrDefault(scope, Map.of()).getOrDefault(method, List.of())) {
|
||||
if (route.matches(requestPath, query, spanned) && (best == null || route.outranks(best))) {
|
||||
if (route.matches(requestPath, query, spanned) && route.carries(headers)
|
||||
&& (best == null || route.outranks(best))) {
|
||||
best = route;
|
||||
}
|
||||
}
|
||||
return best == null ? Optional.empty()
|
||||
: Optional.of(new Match(best.operation(), best.labelValues(requestPath, spanned), best.located(),
|
||||
best.documented()));
|
||||
best.documented(), best.model()));
|
||||
}
|
||||
|
||||
/** The modelled denial status, or the existing protocol default when the model declares none. */
|
||||
@@ -262,9 +311,12 @@ final class IamRestRoutes {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " gives " + model.getKey() + " "
|
||||
+ operation.getKey() + " no HTTP method or no request URI");
|
||||
}
|
||||
JsonNode requires = operation.getValue().path("requires");
|
||||
routes.computeIfAbsent(scope, ignored -> new HashMap<>())
|
||||
.computeIfAbsent(method, ignored -> new ArrayList<>())
|
||||
.add(route(operation.getKey(), requestUri, Map.of(), documented));
|
||||
.add(route(operation.getKey(), requestUri, Map.of(), documented, names(requires.path("query")),
|
||||
names(requires.path("headers")),
|
||||
operation.getValue().path("deprecated").asBoolean(false), model.getKey()));
|
||||
JsonNode alternatives = operation.getValue().path("alternatives");
|
||||
if (!alternatives.isMissingNode() && !alternatives.isArray()) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " gives " + operation.getKey()
|
||||
@@ -289,7 +341,8 @@ final class IamRestRoutes {
|
||||
located.put(member.getKey(), source);
|
||||
});
|
||||
routes.get(scope).computeIfAbsent(alternativeMethod, ignored -> new ArrayList<>())
|
||||
.add(route(operation.getKey(), uri, Map.copyOf(located), documented));
|
||||
.add(route(operation.getKey(), uri, Map.copyOf(located), documented, Set.of(), Set.of(),
|
||||
false, model.getKey()));
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -302,8 +355,17 @@ final class IamRestRoutes {
|
||||
return new IamRestRoutes(Map.copyOf(copied), Map.copyOf(denialStatuses), Set.copyOf(documentedScopes));
|
||||
}
|
||||
|
||||
/** The strings of a JSON array, none when it is missing. */
|
||||
private static Set<String> names(JsonNode array) {
|
||||
Set<String> found = new HashSet<>();
|
||||
array.forEach(name -> found.add(name.asText()));
|
||||
return Set.copyOf(found);
|
||||
}
|
||||
|
||||
/** 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, Map<String, String> located, boolean documented) {
|
||||
private static Route route(String operation, String requestUri, Map<String, String> located, boolean documented,
|
||||
Set<String> requiredQuery, Set<String> requiredHeaders, boolean deprecated,
|
||||
String model) {
|
||||
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));
|
||||
@@ -346,6 +408,6 @@ final class IamRestRoutes {
|
||||
}
|
||||
return new Route(operation, Pattern.compile(pattern.isEmpty() ? "/" : pattern.toString()),
|
||||
Pattern.compile(spanning.isEmpty() ? "/" : spanning.toString()), List.copyOf(labels), ranks,
|
||||
Map.copyOf(query), located, documented);
|
||||
Map.copyOf(query), located, documented, requiredQuery, requiredHeaders, deprecated, model);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user