mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(servicecatalog): paginate portfolio listings (#6631)
* fix(servicecatalog): paginate portfolio listings * refactor(servicecatalog): reuse shared pagination validation
This commit is contained in:
1 parent
86d9115eba
commit
4506645d0f
4 files changed
+246
-1
No files matched your search
@@ -8,6 +8,14 @@ resource associations, provisioned products, and organization/account portfolio
|
||||
Portfolio-share operations complete immediately while preserving the AWS status-token
|
||||
workflow.
|
||||
|
||||
`ListPortfolios` supports `PageSize` from 0 to 100 and opaque `PageToken` /
|
||||
`NextPageToken` cursors. Floci uses 100 items when the size is omitted or zero;
|
||||
this is an emulator default, since AWS does not document those cases. Tokens
|
||||
are scoped to the action, account, and request region and may be resumed with a
|
||||
different page size. Final pages omit `NextPageToken`. Invalid sizes, malformed
|
||||
tokens, and tokens longer than 2024 characters return `InvalidParametersException`.
|
||||
The existing seeded Control Tower portfolio participates in pagination.
|
||||
|
||||
## Supported Actions
|
||||
|
||||
<!-- floci:actions:start -->
|
||||
|
||||
@@ -408,6 +408,12 @@
|
||||
<version>${aws.sdk.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>servicecatalog</artifactId>
|
||||
<version>${aws.sdk.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>redshift</artifactId>
|
||||
|
||||
+21
-1
@@ -6,6 +6,8 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
|
||||
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 jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
@@ -33,7 +35,7 @@ public class ServiceCatalogJsonHandler {
|
||||
case "CreatePortfolio" -> portfolioResponse(service.createPortfolio(request, region, accountId));
|
||||
case "UpdatePortfolio" -> portfolioResponse(service.updatePortfolio(text(request, "Id"), request));
|
||||
case "DescribePortfolio" -> portfolioResponse(service.describePortfolio(text(request, "Id")));
|
||||
case "ListPortfolios" -> listPortfolios(region, accountId);
|
||||
case "ListPortfolios" -> listPortfolios(request, region, accountId);
|
||||
case "DeletePortfolio" -> empty(() -> service.deletePortfolio(text(request, "Id")));
|
||||
case "CreateProduct" -> productResponse(service.createProduct(request, region, accountId));
|
||||
case "UpdateProduct" -> productResponse(service.updateProduct(text(request, "Id"), request));
|
||||
@@ -144,6 +146,24 @@ public class ServiceCatalogJsonHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private Response listPortfolios(JsonNode request, String region, String accountId) {
|
||||
Integer pageSize = Pagination.parseMaxResults(request, "PageSize", "InvalidParametersException");
|
||||
if (pageSize != null && pageSize == 0) {
|
||||
pageSize = null;
|
||||
}
|
||||
String pageToken = Pagination.parseNextToken(request, "PageToken", "InvalidParametersException");
|
||||
PaginatedResult<ObjectNode> page = Pagination.paginate(service.listPortfolios(region, accountId),
|
||||
item -> text(item, "Id"), pageSize, pageToken, 100, 100, 2024, "InvalidParametersException",
|
||||
"ListPortfolios:" + accountId + ":" + region);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode details = response.putArray("PortfolioDetails");
|
||||
page.items().forEach(details::add);
|
||||
if (page.nextToken() != null) {
|
||||
response.put("NextPageToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response listPortfolios(String region, String accountId) {
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode details = response.putArray("PortfolioDetails");
|
||||
|
||||
+211
@@ -0,0 +1,211 @@
|
||||
package io.github.hectorvent.floci.services.servicecatalog;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
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.auth.credentials.AwsBasicCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
import software.amazon.awssdk.awscore.exception.AwsServiceException;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.servicecatalog.ServiceCatalogClient;
|
||||
import software.amazon.awssdk.services.servicecatalog.model.ListPortfoliosResponse;
|
||||
import software.amazon.awssdk.services.servicecatalog.model.PortfolioDetail;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
class ServiceCatalogPortfolioPaginationIntegrationTest {
|
||||
|
||||
private static final String ACCOUNT = "741660500001";
|
||||
private static final String OTHER_ACCOUNT = "741660500002";
|
||||
private static final Region REGION = Region.US_EAST_1;
|
||||
|
||||
@Inject
|
||||
StorageFactory storageFactory;
|
||||
|
||||
@Inject
|
||||
ObjectMapper objectMapper;
|
||||
|
||||
@TestHTTPResource
|
||||
URI endpoint;
|
||||
|
||||
private final List<AccountAwareStorageBackend<ObjectNode>> stores = new ArrayList<>();
|
||||
private ServiceCatalogClient client;
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
for (String name : List.of("portfolios", "products", "associations")) {
|
||||
stores.add(storageFactory.create("servicecatalog", "servicecatalog-" + name + ".json",
|
||||
new TypeReference<Map<String, ObjectNode>>() {}));
|
||||
}
|
||||
client = client(ACCOUNT, REGION);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanUp() {
|
||||
client.close();
|
||||
for (AccountAwareStorageBackend<ObjectNode> store : stores) {
|
||||
for (String account : List.of(ACCOUNT, OTHER_ACCOUNT)) {
|
||||
store.keysForAccount(account).forEach(key -> store.deleteForAccount(account, key));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void sdkPaginatorReturnsEveryPortfolioIncludingTheSeedExactlyOnce() {
|
||||
List<String> expected = createTwo();
|
||||
ListPortfoliosResponse first = client.listPortfolios(request -> request.pageSize(1));
|
||||
assertEquals(List.of(expected.getFirst()), ids(first));
|
||||
assertNotNull(first.nextPageToken());
|
||||
List<String> actual = client.listPortfoliosPaginator(request -> request.pageSize(1)).stream()
|
||||
.flatMap(response -> response.portfolioDetails().stream()).map(PortfolioDetail::id).toList();
|
||||
assertEquals(expected, actual);
|
||||
ListPortfoliosResponse last = client.listPortfolios(request -> request.pageSize(100).pageToken(first.nextPageToken()));
|
||||
assertEquals(expected.subList(1, expected.size()), ids(last));
|
||||
assertNull(last.nextPageToken());
|
||||
}
|
||||
|
||||
@Test
|
||||
void omittedAndZeroPageSizesUseTheBoundedEmulatorDefault() {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
String id = "port-test" + i;
|
||||
stores.getFirst().putForAccount(ACCOUNT, id, objectMapper.createObjectNode()
|
||||
.put("Id", id).put("DisplayName", id).put("ProviderName", "test"));
|
||||
}
|
||||
for (Integer size : new Integer[] {null, 0, 100}) {
|
||||
ListPortfoliosResponse first = client.listPortfolios(request -> request.pageSize(size));
|
||||
assertEquals(100, first.portfolioDetails().size());
|
||||
assertNotNull(first.nextPageToken());
|
||||
ListPortfoliosResponse last = client.listPortfolios(request -> request.pageToken(first.nextPageToken()));
|
||||
assertEquals(1, last.portfolioDetails().size());
|
||||
assertNull(last.nextPageToken());
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {-1, 101})
|
||||
void outOfRangePageSizesAreRejected(int size) {
|
||||
invalid(() -> client.listPortfolios(request -> request.pageSize(size)));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"4294967297", "1.5", "\"1\"", "{}", "[]"})
|
||||
void malformedPageSizesAreRejectedBeforeCoercion(String value) {
|
||||
raw("{\"PageSize\":" + value + "}", 400);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"\"%%%%\"", "17", "{}", "[]"})
|
||||
void malformedPageTokensAreRejected(String value) {
|
||||
raw("{\"PageToken\":" + value + "}", 400);
|
||||
}
|
||||
|
||||
@Test
|
||||
void oversizedTokensWithAValidScopeAreRejected() {
|
||||
createTwo();
|
||||
String token = client.listPortfolios(request -> request.pageSize(1)).nextPageToken();
|
||||
assertNotNull(token);
|
||||
String cursor = new String(Base64.getUrlDecoder().decode(token), StandardCharsets.UTF_8);
|
||||
String oversized = Base64.getUrlEncoder().withoutPadding()
|
||||
.encodeToString((cursor + "x".repeat(2024)).getBytes(StandardCharsets.UTF_8));
|
||||
assertTrue(oversized.length() > 2024);
|
||||
invalid(() -> client.listPortfolios(request -> request.pageToken(oversized)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void tokensCannotCrossAccountOrRegion() {
|
||||
createTwo();
|
||||
String token = client.listPortfolios(request -> request.pageSize(1)).nextPageToken();
|
||||
assertNotNull(token);
|
||||
try (ServiceCatalogClient otherAccount = client(OTHER_ACCOUNT, REGION);
|
||||
ServiceCatalogClient otherRegion = client(ACCOUNT, Region.EU_WEST_1)) {
|
||||
invalid(() -> otherAccount.listPortfolios(request -> request.pageToken(token)));
|
||||
invalid(() -> otherRegion.listPortfolios(request -> request.pageToken(token)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletingTheCursorPortfolioStillResumesAtTheNextId() {
|
||||
List<String> expected = createTwo();
|
||||
ListPortfoliosResponse first = client.listPortfolios(request -> request.pageSize(1));
|
||||
assertEquals(1, first.portfolioDetails().size());
|
||||
String id = first.portfolioDetails().getFirst().id();
|
||||
client.deletePortfolio(request -> request.id(id));
|
||||
ListPortfoliosResponse last = client.listPortfolios(request -> request.pageSize(100).pageToken(first.nextPageToken()));
|
||||
assertEquals(expected.stream().filter(value -> !value.equals(id)).toList(), ids(last));
|
||||
assertNull(last.nextPageToken());
|
||||
}
|
||||
|
||||
@Test
|
||||
void seededOnlyFinalPageOmitsTokenAndAcceptsNullOrEmptyToken() {
|
||||
for (String body : List.of("{}", "{\"PageToken\":null}", "{\"PageToken\":\"\"}")) {
|
||||
Map<String, Object> response = raw(body, 200);
|
||||
assertFalse(response.containsKey("NextPageToken"));
|
||||
assertEquals(1, ((List<?>) response.get("PortfolioDetails")).size());
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> createTwo() {
|
||||
String first = client.createPortfolio(request -> request.displayName("pagination-a").providerName("test"))
|
||||
.portfolioDetail().id();
|
||||
String second = client.createPortfolio(request -> request.displayName("pagination-b").providerName("test"))
|
||||
.portfolioDetail().id();
|
||||
return List.of(first, second, ServiceCatalogService.CONTROL_TOWER_PORTFOLIO_ID).stream().sorted().toList();
|
||||
}
|
||||
|
||||
private List<String> ids(ListPortfoliosResponse response) {
|
||||
return response.portfolioDetails().stream().map(PortfolioDetail::id).toList();
|
||||
}
|
||||
|
||||
private void invalid(Runnable call) {
|
||||
AwsServiceException error = assertThrows(AwsServiceException.class, call::run);
|
||||
assertEquals(400, error.statusCode());
|
||||
assertEquals("InvalidParametersException", error.awsErrorDetails().errorCode());
|
||||
}
|
||||
|
||||
private Map<String, Object> raw(String body, int status) {
|
||||
Map<String, Object> response = given().contentType("application/x-amz-json-1.1")
|
||||
.header("X-Amz-Target", "AWS242ServiceCatalogService.ListPortfolios")
|
||||
.header("Authorization", "AWS4-HMAC-SHA256 Credential=" + ACCOUNT + "/20260101/us-east-1/servicecatalog/aws4_request")
|
||||
.body(body).when().post("/").then().statusCode(status).extract().jsonPath().getMap("");
|
||||
if (status == 400) {
|
||||
assertEquals("InvalidParametersException", response.get("__type"));
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
private ServiceCatalogClient client(String account, Region region) {
|
||||
return ServiceCatalogClient.builder().endpointOverride(endpoint).region(region)
|
||||
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(account, "test"))).build();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user