mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(lambda): paginate ListFunctions responses (#6700)
* fix(lambda): paginate ListFunctions responses * test(lambda): isolate ListFunctions pagination fixture * refactor(lambda): share signed list pagination
This commit is contained in:
1 parent
e9e2b38da6
commit
a30e484dae
8 files changed
+263
-67
No files matched your search
@@ -53,7 +53,8 @@ public final class Pagination {
|
||||
}
|
||||
int limit = maxResults != null ? maxResults : defaultPageSize;
|
||||
return slice(all, cursorOf, limit,
|
||||
decodeCursor(nextToken, "", token -> new AwsException(errorCode, "Invalid " + tokenName + ".", 400)), "");
|
||||
decodeCursor(nextToken, "", token -> new AwsException(errorCode, "Invalid " + tokenName + ".", 400)),
|
||||
"", Pagination::encodeToken);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,7 +71,26 @@ public final class Pagination {
|
||||
throw new IllegalArgumentException("limit must be at least 1, was " + limit);
|
||||
}
|
||||
String prefix = namespace + ":";
|
||||
return slice(all, cursorOf, limit, decodeCursor(nextToken, prefix, invalidToken), prefix);
|
||||
return slice(all, cursorOf, limit, decodeCursor(nextToken, prefix, invalidToken),
|
||||
prefix, Pagination::encodeToken);
|
||||
}
|
||||
|
||||
/** Paginates with instance-signed markers, optionally scoped by a prefix within the signed cursor. */
|
||||
public static <T> PaginatedResult<T> paginate(List<T> all, Function<T, String> cursorOf,
|
||||
int limit, String marker, SignedMarkers markers,
|
||||
String scope, Function<String, AwsException> invalidMarker) {
|
||||
if (limit < 1) {
|
||||
throw new IllegalArgumentException("limit must be at least 1, was " + limit);
|
||||
}
|
||||
String after = null;
|
||||
if (marker != null) {
|
||||
String cursor = markers.cursorOf(marker);
|
||||
if (cursor == null || !cursor.startsWith(scope)) {
|
||||
throw invalidMarker.apply(marker);
|
||||
}
|
||||
after = cursor.substring(scope.length());
|
||||
}
|
||||
return slice(all, cursorOf, limit, after, scope, markers::issue);
|
||||
}
|
||||
|
||||
/** Validates service-specific bounds before applying the existing namespaced cursor format. */
|
||||
@@ -113,7 +133,8 @@ public final class Pagination {
|
||||
}
|
||||
|
||||
private static <T> PaginatedResult<T> slice(List<T> all, Function<T, String> cursorOf, int limit,
|
||||
String after, String tokenPrefix) {
|
||||
String after, String tokenPrefix,
|
||||
Function<String, String> issueToken) {
|
||||
List<T> sorted = all.stream().sorted(Comparator.comparing(cursorOf)).collect(Collectors.toList());
|
||||
int start = 0;
|
||||
if (after != null) {
|
||||
@@ -128,7 +149,7 @@ public final class Pagination {
|
||||
List<T> page = sorted.stream().skip(start).limit(limit).collect(Collectors.toList());
|
||||
String token = null;
|
||||
if (limit < sorted.size() - start && !page.isEmpty()) {
|
||||
token = encodeToken(tokenPrefix + cursorOf.apply(page.get(page.size() - 1)));
|
||||
token = issueToken.apply(tokenPrefix + cursorOf.apply(page.get(page.size() - 1)));
|
||||
}
|
||||
return new PaginatedResult<>(page, token);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.Digests;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.services.lambda.model.EventSourceMapping;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
@@ -142,15 +143,20 @@ public class LambdaController {
|
||||
|
||||
@GET
|
||||
@Path("/functions")
|
||||
public Response listFunctions(@Context HttpHeaders headers) {
|
||||
public Response listFunctions(@Context HttpHeaders headers,
|
||||
@QueryParam("MaxItems") String maxItems,
|
||||
@QueryParam("Marker") String marker) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
List<LambdaFunction> functions = lambdaService.listFunctions(region);
|
||||
PaginatedResult<LambdaFunction> page = lambdaService.listFunctions(region, maxItems, marker);
|
||||
|
||||
ObjectNode root = objectMapper.createObjectNode();
|
||||
ArrayNode items = root.putArray("Functions");
|
||||
for (LambdaFunction fn : functions) {
|
||||
for (LambdaFunction fn : page.items()) {
|
||||
items.add(objectMapper.valueToTree(buildFunctionConfiguration(fn)));
|
||||
}
|
||||
if (page.nextToken() != null) {
|
||||
root.put("NextMarker", page.nextToken());
|
||||
}
|
||||
return Response.ok(root).build();
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,6 @@ import java.util.Base64;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -52,7 +51,7 @@ public class LambdaLayerService {
|
||||
private static final int MAX_LAYER_VERSION_ARN_LENGTH = 140;
|
||||
private static final int MAX_ITEMS_LIMIT = 50;
|
||||
|
||||
/** Signs pagination markers; see {@link #decodeMarker}. */
|
||||
/** Signs pagination markers for both layer listing operations. */
|
||||
private final SignedMarkers markers = new SignedMarkers();
|
||||
|
||||
private static final List<String> COMPATIBLE_ARCHITECTURES = List.of("x86_64", "arm64");
|
||||
@@ -232,7 +231,8 @@ public class LambdaLayerService {
|
||||
.filter(lv -> matchesFilters(lv, compatibleRuntime, compatibleArchitecture))
|
||||
.toList();
|
||||
// Zero-padded so the cursor's string ordering matches the numeric version ordering.
|
||||
return page(versions, lv -> String.format("%019d", lv.getVersion()), limit, marker);
|
||||
return LambdaPagination.paginate(versions, lv -> String.format("%019d", lv.getVersion()),
|
||||
limit, MAX_ITEMS_LIMIT, marker, markers, "");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -253,7 +253,8 @@ public class LambdaLayerService {
|
||||
.orElseThrow())
|
||||
.sorted(Comparator.comparing(LambdaLayerVersion::getLayerName))
|
||||
.toList();
|
||||
return page(latestMatching, LambdaLayerVersion::getLayerName, limit, marker);
|
||||
return LambdaPagination.paginate(latestMatching, LambdaLayerVersion::getLayerName,
|
||||
limit, MAX_ITEMS_LIMIT, marker, markers, "");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -278,20 +279,6 @@ public class LambdaLayerService {
|
||||
return true;
|
||||
}
|
||||
|
||||
private <T> PaginatedResult<T> page(List<T> items, Function<T, String> cursorOf,
|
||||
Integer maxItems, String marker) {
|
||||
String after = decodeMarker(marker);
|
||||
List<T> remaining = after == null ? items
|
||||
: items.stream().filter(i -> cursorOf.apply(i).compareTo(after) > 0).toList();
|
||||
List<T> pageItems = remaining.stream()
|
||||
.limit(maxItems != null ? maxItems : MAX_ITEMS_LIMIT)
|
||||
.toList();
|
||||
String nextMarker = pageItems.size() < remaining.size()
|
||||
? markers.issue(cursorOf.apply(pageItems.get(pageItems.size() - 1)))
|
||||
: null;
|
||||
return new PaginatedResult<>(pageItems, nextMarker);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the three parseable list parameters and returns the effective MaxItems.
|
||||
*
|
||||
@@ -303,15 +290,8 @@ public class LambdaLayerService {
|
||||
*/
|
||||
private static Integer validateListParameters(String maxItems, String compatibleRuntime,
|
||||
String compatibleArchitecture) {
|
||||
Integer parsed = parseMaxItems(maxItems);
|
||||
List<String> errors = new ArrayList<>();
|
||||
if (parsed != null && parsed < 1) {
|
||||
errors.add(constraintFailure(maxItems, "maxItems",
|
||||
"Member must have value greater than or equal to 1"));
|
||||
} else if (parsed != null && parsed > MAX_ITEMS_LIMIT) {
|
||||
errors.add(constraintFailure(maxItems, "maxItems",
|
||||
"Member must have value less than or equal to " + MAX_ITEMS_LIMIT));
|
||||
}
|
||||
Integer parsed = LambdaPagination.parseMaxItems(maxItems, MAX_ITEMS_LIMIT, errors);
|
||||
if (compatibleRuntime != null && !COMPATIBLE_RUNTIMES.contains(compatibleRuntime)) {
|
||||
errors.add(constraintFailure(compatibleRuntime, "compatibleRuntime",
|
||||
"Member must satisfy enum value set: " + COMPATIBLE_RUNTIMES));
|
||||
@@ -327,45 +307,11 @@ public class LambdaLayerService {
|
||||
return parsed;
|
||||
}
|
||||
|
||||
private static Integer parseMaxItems(String maxItems) {
|
||||
if (maxItems == null || maxItems.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return Integer.valueOf(maxItems);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new AwsException("SerializationException",
|
||||
"'" + maxItems + "' can not be converted to Integer", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static String constraintFailure(String value, String member, String requirement) {
|
||||
return "Value '" + value + "' at '" + member + "' failed to satisfy constraint: "
|
||||
+ requirement;
|
||||
}
|
||||
|
||||
/**
|
||||
* The live service's Marker is an encrypted token it rejects when it did not issue it.
|
||||
* Floci signs the cursor instead, with a key generated at startup: a fabricated or edited
|
||||
* marker fails the check, which is what makes an arbitrary string a 400 rather than a
|
||||
* silently skipped or empty page. A marker from an earlier run is stale for the same
|
||||
* reason, matching a client that replays an expired token against AWS.
|
||||
*/
|
||||
private String decodeMarker(String marker) {
|
||||
if (marker == null) {
|
||||
return null;
|
||||
}
|
||||
String cursor = markers.cursorOf(marker);
|
||||
if (cursor == null) {
|
||||
throw invalidPaginationKey();
|
||||
}
|
||||
return cursor;
|
||||
}
|
||||
|
||||
private static AwsException invalidPaginationKey() {
|
||||
return new AwsException("InvalidParameterValueException", "Invalid pagination key.", 400);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a specific layer version.
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.Pagination;
|
||||
import io.github.hectorvent.floci.core.common.SignedMarkers;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
final class LambdaPagination {
|
||||
|
||||
private LambdaPagination() {}
|
||||
|
||||
static Integer parseMaxItems(String raw, int maxAllowed, List<String> errors) {
|
||||
if (raw == null || raw.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
int requested;
|
||||
try {
|
||||
requested = Integer.parseInt(raw);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new AwsException("SerializationException", "'" + raw + "' can not be converted to Integer", 400);
|
||||
}
|
||||
if (requested < 1 || requested > maxAllowed) {
|
||||
String bound = requested < 1 ? "greater than or equal to 1"
|
||||
: "less than or equal to " + maxAllowed;
|
||||
errors.add("Value '" + raw + "' at 'maxItems' failed to satisfy constraint: "
|
||||
+ "Member must have value " + bound);
|
||||
}
|
||||
return requested;
|
||||
}
|
||||
|
||||
static <T> PaginatedResult<T> paginate(List<T> items, Function<T, String> cursorOf,
|
||||
Integer maxItems, int pageSize, String marker,
|
||||
SignedMarkers markers, String scope) {
|
||||
int limit = maxItems == null ? pageSize : Math.min(maxItems, pageSize);
|
||||
return Pagination.paginate(items, cursorOf, limit, marker, markers, scope,
|
||||
token -> new AwsException("InvalidParameterValueException", "Invalid pagination key.", 400));
|
||||
}
|
||||
}
|
||||
@@ -11,8 +11,10 @@ import io.github.hectorvent.floci.core.common.AwsPartition;
|
||||
import io.github.hectorvent.floci.core.common.AwsPartitions;
|
||||
import io.github.hectorvent.floci.core.common.AwsRegions;
|
||||
import io.github.hectorvent.floci.core.common.CustomResourceLiveness;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.RequestScopes;
|
||||
import io.github.hectorvent.floci.core.common.SignedMarkers;
|
||||
import io.github.hectorvent.floci.core.common.SmithyValidationErrors;
|
||||
import io.github.hectorvent.floci.core.resource.ExplorerResource;
|
||||
import io.github.hectorvent.floci.core.resource.ResourceProvider;
|
||||
@@ -99,6 +101,8 @@ public class LambdaService implements ResourceProvider {
|
||||
private static final int MAX_TOPIC_LENGTH = 249;
|
||||
private static final int MAX_TOPICS = 1;
|
||||
private static final int MAX_SOURCE_ACCESS_CONFIGURATIONS = 23;
|
||||
private static final int LIST_FUNCTIONS_PAGE_SIZE = 50;
|
||||
private static final int MAX_LIST_FUNCTIONS_ITEMS = 10_000;
|
||||
// UpdateFunctionCode S3Bucket (3-63 characters, [0-9A-Za-z\.\-_]*(?<!\.)) and S3Key (1-1024) shapes.
|
||||
private static final Pattern S3_BUCKET_PATTERN = Pattern.compile("[0-9A-Za-z\\.\\-_]*(?<!\\.)");
|
||||
// Create/UpdateAlias FunctionVersion (VersionWithLatestPublished: 1-1024, pattern below) and Description (max 256).
|
||||
@@ -199,6 +203,7 @@ public class LambdaService implements ResourceProvider {
|
||||
private Map<String, FunctionEventInvokeConfig> eventInvokeConfigs = new ConcurrentHashMap<>();
|
||||
private final ConcurrentHashMap<String, Object> policyMutationLocks = new ConcurrentHashMap<>();
|
||||
private final ConcurrentHashMap<String, Object> versionCounterLocks = new ConcurrentHashMap<>();
|
||||
private final SignedMarkers listFunctionMarkers = new SignedMarkers();
|
||||
|
||||
/**
|
||||
* Package-private constructor for testing without CDI. Config defaults
|
||||
@@ -731,6 +736,17 @@ public class LambdaService implements ResourceProvider {
|
||||
return functionStore.list(region);
|
||||
}
|
||||
|
||||
public PaginatedResult<LambdaFunction> listFunctions(String region, String maxItems, String marker) {
|
||||
List<String> errors = new ArrayList<>();
|
||||
Integer requested = LambdaPagination.parseMaxItems(maxItems, MAX_LIST_FUNCTIONS_ITEMS, errors);
|
||||
if (!errors.isEmpty()) {
|
||||
throw SmithyValidationErrors.validationException(errors);
|
||||
}
|
||||
String scope = regionResolver.getAccountId() + "\n" + region + "\n";
|
||||
return LambdaPagination.paginate(listFunctions(region), LambdaFunction::getFunctionName,
|
||||
requested, LIST_FUNCTIONS_PAGE_SIZE, marker, listFunctionMarkers, scope);
|
||||
}
|
||||
|
||||
public LambdaFunction updateFunctionCode(String region, String functionName, Map<String, Object> request) {
|
||||
LambdaFunction fn = getFunction(region, functionName);
|
||||
// publishVersion copies roughly thirty fields off this same live object. Without the
|
||||
|
||||
Reference in new issue
Block a user