Light, fluffy, and always free - The AWS Local Emulator alternative
fix(lambda): paginate code signing configurations (#6832)
Aniket Shukla3c391a06fb00
diff --git a/docs/services/lambda.md b/docs/services/lambda.mdindex 5515d22c3b..0ca069389a 100644--- a/docs/services/lambda.md+++ b/docs/services/lambda.md@@ -255,6 +255,13 @@ Not emulated yet:destinations for durable executions.- The per-execution limits of 3,000 operations and 100 MB of persisted payload.+## Code signing configuration pagination++`ListCodeSigningConfigs` accepts `MaxItems` from 1 to 10,000 and returns `NextMarker`+when more configurations remain. Pass that value as `Marker` to continue listing.+Omitting `MaxItems` returns up to 10,000 configurations. Markers are signed and scoped+to the account and region; the final page omits `NextMarker`.+## Hot-Reloading via Reactive S3 SyncFloci supports automatic hot reload for S3 deployments as a local development convenience.diff --git a/pom.xml b/pom.xmlindex 7d2a2c0484..d8018c1785 100644--- a/pom.xml+++ b/pom.xml@@ -438,6 +438,12 @@<version>${aws.sdk.version}</version><scope>test</scope></dependency>+ <dependency>+ <groupId>software.amazon.awssdk</groupId>+ <artifactId>lambda</artifactId>+ <version>${aws.sdk.version}</version>+ <scope>test</scope>+ </dependency><dependency><groupId>software.amazon.awssdk</groupId><artifactId>sqs</artifactId>diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaCodeSigningConfigFunctionsController.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaCodeSigningConfigFunctionsController.javaindex 64a04b2aac..4b0a34219d 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaCodeSigningConfigFunctionsController.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaCodeSigningConfigFunctionsController.java@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.lambda;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.node.ObjectNode;import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.core.common.PaginatedResult;import io.github.hectorvent.floci.core.common.RegionResolver;import io.github.hectorvent.floci.core.common.SmithyValidationErrors;import io.github.hectorvent.floci.services.lambda.model.CodeSigningConfig;@@ -20,8 +21,8 @@ import jakarta.ws.rs.core.Context;import jakarta.ws.rs.core.HttpHeaders;import jakarta.ws.rs.core.MediaType;import jakarta.ws.rs.core.Response;+import jakarta.ws.rs.core.UriInfo;-import java.util.List;import java.util.regex.Pattern;/**@@ -139,12 +140,17 @@ public class LambdaCodeSigningConfigFunctionsController {@GET@Path("/code-signing-configs")public Response listCodeSigningConfigs(@Context HttpHeaders headers,- @QueryParam("Marker") String marker,+ @Context UriInfo uriInfo,@QueryParam("MaxItems") String maxItems) {- parseMaxItems(maxItems);- List<CodeSigningConfig> configs = service.list(regionResolver.resolveRegion(headers));+ int limit = parseMaxItems(maxItems);+ String region = regionResolver.resolveRegion(headers);+ String marker = uriInfo.getQueryParameters().getFirst("Marker");+ PaginatedResult<CodeSigningConfig> page = service.list(region, limit, marker);ObjectNode root = objectMapper.createObjectNode();- root.set("CodeSigningConfigs", objectMapper.valueToTree(configs));+ root.set("CodeSigningConfigs", objectMapper.valueToTree(page.items()));+ if (page.nextToken() != null) {+ root.put("NextMarker", page.nextToken());+ }return Response.ok(root).build();}@@ -185,9 +191,9 @@ public class LambdaCodeSigningConfigFunctionsController {}}- private void parseMaxItems(String maxItems) {+ private int parseMaxItems(String maxItems) {if (maxItems == null || maxItems.isBlank()) {- return;+ return MAX_ITEMS_MAX;}int value;try {@@ -202,5 +208,6 @@ public class LambdaCodeSigningConfigFunctionsController {"Value '" + value + "' at 'maxItems' failed to satisfy constraint: "+ "Member must be between " + MAX_ITEMS_MIN + " and " + MAX_ITEMS_MAX, 400);}+ return value;}}diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaCodeSigningConfigService.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaCodeSigningConfigService.javaindex 0e2d4400b8..8000215b90 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaCodeSigningConfigService.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaCodeSigningConfigService.java@@ -3,6 +3,9 @@ package io.github.hectorvent.floci.services.lambda;import com.fasterxml.jackson.core.type.TypeReference;import io.github.hectorvent.floci.core.common.AwsArnUtils;import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.core.common.PaginatedResult;+import io.github.hectorvent.floci.core.common.RegionResolver;+import io.github.hectorvent.floci.core.common.SignedMarkers;import io.github.hectorvent.floci.core.storage.StorageBackend;import io.github.hectorvent.floci.core.storage.StorageFactory;import io.github.hectorvent.floci.services.lambda.model.CodeSigningConfig;@@ -36,6 +39,7 @@ public class LambdaCodeSigningConfigService {private static final int ID_LENGTH = 17;private static final int MAX_DESCRIPTION = 256;private static final int MAX_SIGNING_PROFILE_ARNS = 20;+ private static final int LIST_PAGE_SIZE = 10000;/** The model documents Warn as the default when the request names no policy. */static final String DEFAULT_UNTRUSTED_ARTIFACT_POLICY = "Warn";@@ -45,10 +49,13 @@ public class LambdaCodeSigningConfigService {DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").withZone(ZoneOffset.UTC);private final StorageBackend<String, CodeSigningConfig> store;+ private final RegionResolver regionResolver;+ private final SignedMarkers markers = new SignedMarkers();private final SecureRandom random = new SecureRandom();@Inject- public LambdaCodeSigningConfigService(StorageFactory storageFactory) {+ public LambdaCodeSigningConfigService(StorageFactory storageFactory, RegionResolver regionResolver) {+ this.regionResolver = regionResolver;this.store = storageFactory.create("lambda", "lambda-code-signing-configs.json",new TypeReference<Map<String, CodeSigningConfig>>() {});}@@ -127,6 +134,12 @@ public class LambdaCodeSigningConfigService {.toList();}+ public PaginatedResult<CodeSigningConfig> list(String region, int maxItems, String marker) {+ String scope = regionResolver.getAccountId() + "\n" + region + "\n";+ return LambdaPagination.paginate(list(region), CodeSigningConfig::getCodeSigningConfigId,+ maxItems, LIST_PAGE_SIZE, marker, markers, scope);+ }+private static AwsException notFound(String arn) {return new AwsException("ResourceNotFoundException","The code signing configuration " + arn + " does not exist.", 404);diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaCodeSigningMarkerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaCodeSigningMarkerIntegrationTest.javanew file mode 100644index 0000000000..068c0f9268--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaCodeSigningMarkerIntegrationTest.java@@ -0,0 +1,34 @@+package io.github.hectorvent.floci.services.lambda;++import io.github.hectorvent.floci.core.common.Base64Url;+import io.quarkus.test.junit.QuarkusTest;+import org.junit.jupiter.api.Test;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.equalTo;++@QuarkusTest+class LambdaCodeSigningMarkerIntegrationTest {+ private static final String ACCOUNT = "741681400003";+ private static final String REGION = "eu-west-2";+ private static final String AUTH = "AWS4-HMAC-SHA256 Credential=" + ACCOUNT + "/20261028/" + REGION + "/lambda/aws4_request";+ private static final String BASE = "/2020-04-22/code-signing-configs";++ @Test+ void unsignedCursorCannotBeUsedAsAMarker() {+ String marker = Base64Url.encode("ListCodeSigningConfigs:" + ACCOUNT + ":" + REGION+ + ":csc-aaaaaaaaaaaaaaaaa");+ given().header("Authorization", AUTH).queryParam("Marker", marker).get(BASE)+ .then().statusCode(400)+ .body("__type", equalTo("InvalidParameterValueException"))+ .body("message", equalTo("Invalid pagination key."));+ }++ @Test+ void emptyMarkerUsesTheLambdaPaginationError() {+ given().header("Authorization", AUTH).queryParam("Marker", "").get(BASE)+ .then().statusCode(400)+ .body("__type", equalTo("InvalidParameterValueException"))+ .body("message", equalTo("Invalid pagination key."));+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaCodeSigningPaginationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaCodeSigningPaginationIntegrationTest.javanew file mode 100644index 0000000000..65210f15e2--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaCodeSigningPaginationIntegrationTest.java@@ -0,0 +1,135 @@+package io.github.hectorvent.floci.services.lambda;++import io.github.hectorvent.floci.testing.AwsTestClients;+import io.quarkus.test.junit.QuarkusTest;+import org.junit.jupiter.api.AfterEach;+import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.ValueSource;+import software.amazon.awssdk.regions.Region;+import software.amazon.awssdk.services.lambda.LambdaClient;+import software.amazon.awssdk.services.lambda.model.CodeSigningConfig;+import software.amazon.awssdk.services.lambda.model.InvalidParameterValueException;+import software.amazon.awssdk.services.lambda.model.ListCodeSigningConfigsResponse;++import java.util.ArrayList;+import java.util.List;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.equalTo;+import static org.junit.jupiter.api.Assertions.*;++@QuarkusTest+class LambdaCodeSigningPaginationIntegrationTest {+ private static final String ACCOUNT = "741681400001";+ private static final String REGION = "eu-west-2";+ private static final String AUTH = "AWS4-HMAC-SHA256 Credential=" + ACCOUNT + "/20261028/" + REGION + "/lambda/aws4_request";+ private static final String BASE = "/2020-04-22/code-signing-configs";+ private static final String PROFILE = "arn:aws:signer:" + REGION + ":" + ACCOUNT + ":/signing-profiles/prof/AbCdEfGhIj";+ private final List<String> created = new ArrayList<>();+ private LambdaClient client;++ @BeforeEach+ void openClient() {+ client = AwsTestClients.create(LambdaClient.builder(), ACCOUNT, Region.of(REGION));+ }++ @AfterEach+ void cleanUp() {+ try {+ for (String arn : created) {+ client.deleteCodeSigningConfig(request -> request.codeSigningConfigArn(arn));+ }+ } finally {+ client.close();+ }+ }++ @Test+ void sdkPagesAndAutomaticPaginatorTraverseEachConfigurationOnce() {+ create(3);+ ListCodeSigningConfigsResponse first = client.listCodeSigningConfigs(request -> request.maxItems(1));+ assertEquals(1, first.codeSigningConfigs().size());+ assertNotNull(first.nextMarker());+ ListCodeSigningConfigsResponse last = client.listCodeSigningConfigs(request -> request+ .marker(first.nextMarker()).maxItems(2));+ assertEquals(2, last.codeSigningConfigs().size());+ assertNull(last.nextMarker());+ List<String> ids = new ArrayList<>(arns(first));+ ids.addAll(arns(last));+ assertEquals(created.stream().sorted().toList(), ids);+ assertEquals(ids, client.listCodeSigningConfigsPaginator(request -> request.maxItems(1))+ .stream().flatMap(page -> page.codeSigningConfigs().stream())+ .map(CodeSigningConfig::codeSigningConfigArn).toList());+ }++ @Test+ void omittedAndMaximumPageSizesReturnACompleteFinalPage() {+ create(3);+ for (Integer size : new Integer[]{null, 10000}) {+ ListCodeSigningConfigsResponse page = client.listCodeSigningConfigs(request -> request.maxItems(size));+ assertEquals(created.stream().sorted().toList(), arns(page));+ assertNull(page.nextMarker());+ }+ }++ @Test+ void cursorSurvivesDeletionOfItsConfiguration() {+ create(3);+ ListCodeSigningConfigsResponse first = client.listCodeSigningConfigs(request -> request.maxItems(1));+ assertNotNull(first.nextMarker());+ String deleted = first.codeSigningConfigs().getFirst().codeSigningConfigArn();+ client.deleteCodeSigningConfig(request -> request.codeSigningConfigArn(deleted));+ created.remove(deleted);+ ListCodeSigningConfigsResponse last = client.listCodeSigningConfigs(request -> request+ .marker(first.nextMarker()).maxItems(2));+ assertEquals(created.stream().sorted().toList(), arns(last));+ assertNull(last.nextMarker());+ }++ @Test+ void markersCannotCrossAccountOrRegion() {+ create(2);+ String marker = client.listCodeSigningConfigs(request -> request.maxItems(1)).nextMarker();+ assertNotNull(marker);+ try (LambdaClient other = AwsTestClients.create(LambdaClient.builder(), "741681400002", Region.of(REGION));+ LambdaClient region = AwsTestClients.create(LambdaClient.builder(), ACCOUNT, Region.US_EAST_1)) {+ assertTrue(other.listCodeSigningConfigs(request -> request.maxItems(1)).codeSigningConfigs().isEmpty());+ assertTrue(region.listCodeSigningConfigs(request -> request.maxItems(1)).codeSigningConfigs().isEmpty());+ assertThrows(InvalidParameterValueException.class, () -> other.listCodeSigningConfigs(request -> request.marker(marker)));+ assertThrows(InvalidParameterValueException.class, () -> region.listCodeSigningConfigs(request -> request.marker(marker)));+ }+ }++ @Test+ void emptyListingHasNoMarker() {+ ListCodeSigningConfigsResponse page = client.listCodeSigningConfigs(request -> request.maxItems(1));+ assertTrue(page.codeSigningConfigs().isEmpty());+ assertNull(page.nextMarker());+ }++ @ParameterizedTest+ @ValueSource(strings = {"0", "-1", "10001", "2147483648", "4294967297", "1.5", "invalid"})+ void invalidPageSizesKeepAwsValidationErrors(String size) {+ given().header("Authorization", AUTH).queryParam("MaxItems", size).get(BASE)+ .then().statusCode(400).body("__type", equalTo("InvalidParameterValueException"));+ }++ @ParameterizedTest+ @ValueSource(strings = {"", "%%%", "not-a-marker", "AA", "1AA"})+ void malformedMarkersAreRejected(String marker) {+ assertThrows(InvalidParameterValueException.class, () -> client.listCodeSigningConfigs(request -> request.marker(marker)));+ }++ private void create(int count) {+ for (int i = 0; i < count; i++) {+ created.add(client.createCodeSigningConfig(request -> request.allowedPublishers(publishers -> publishers+ .signingProfileVersionArns(PROFILE))).codeSigningConfig().codeSigningConfigArn());+ }+ }++ private List<String> arns(ListCodeSigningConfigsResponse page) {+ return page.codeSigningConfigs().stream().map(CodeSigningConfig::codeSigningConfigArn).toList();+ }+}