mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(eks): page ListClusters with maxResults and nextToken (#7769)
* fix(eks): page ListClusters with maxResults and nextToken ListClusters returned every cluster in one response and ignored both pagination parameters. It now returns cluster names in a stable order, at most maxResults per page (default and maximum 100), with a nextToken on every page but the last. A maxResults outside 1 to 100 or an unreadable nextToken is refused with InvalidParameterException, as EKS does. Fixes #7625 * fix(eks): bind ListClusters tokens to the calling account A nextToken issued in one account was accepted in another and skipped the cluster names that sort before its cursor. The token now carries the account it was issued to, and another account refuses it with InvalidParameterException. The pagination tests also get distinct @Order values. * fix(eks): reuse the access entries page-size check in ListClusters ListClusters repeated the 1 to 100 maxResults check that EksAccessEntryService.pageSize already makes. That method is now package-private and ListClusters calls it.
This commit is contained in:
1 parent
8204ee4c4e
commit
ac39d83ce0
5 files changed
+156
-5
No files matched your search
@@ -11,7 +11,7 @@ EKS uses a standard REST API with JSON bodies: not the JSON 1.1 (`X-Amz-Target`)
|
||||
|---|---|
|
||||
| `CreateCluster` | Create a new EKS cluster |
|
||||
| `DescribeCluster` | Describe a cluster by name |
|
||||
| `ListClusters` | List all cluster names |
|
||||
| `ListClusters` | List cluster names with pagination (`maxResults` 1-100 and `nextToken`) |
|
||||
| `DeleteCluster` | Delete a cluster |
|
||||
| `CreateAccessEntry` | Create STANDARD or EC2_LINUX access-entry metadata |
|
||||
| `DescribeAccessEntry` | Describe an access entry by IAM principal ARN |
|
||||
|
||||
@@ -319,7 +319,7 @@ public class EksAccessEntryService {
|
||||
}
|
||||
}
|
||||
|
||||
private static int pageSize(Integer maxResults) {
|
||||
static int pageSize(Integer maxResults) {
|
||||
int limit = maxResults == null ? 100 : maxResults;
|
||||
if (limit < 1 || limit > 100) {
|
||||
throw invalid("maxResults must be between 1 and 100");
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.floci.aws.services.eks;
|
||||
|
||||
import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.Pagination;
|
||||
import io.floci.aws.services.eks.model.Addon;
|
||||
import io.floci.aws.services.eks.model.AssociateAccessPolicyRequest;
|
||||
@@ -78,9 +79,12 @@ public class EksController {
|
||||
|
||||
@GET
|
||||
@Path("/clusters")
|
||||
public Response listClusters() {
|
||||
List<String> clusterNames = eksService.listClusters();
|
||||
return Response.ok(Map.of("clusters", clusterNames)).build();
|
||||
public Response listClusters(@QueryParam("maxResults") String maxResults,
|
||||
@QueryParam("nextToken") String nextToken) {
|
||||
PaginatedResult<String> page = eksService.listClusters(
|
||||
Pagination.parseMaxResults(maxResults, "InvalidParameterException"), nextToken);
|
||||
return Response.ok(page.nextToken() == null ? Map.of("clusters", page.items())
|
||||
: Map.of("clusters", page.items(), "nextToken", page.nextToken())).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
|
||||
@@ -4,6 +4,8 @@ import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.Pagination;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.RequestScopes;
|
||||
import io.floci.aws.core.common.ReservedTags;
|
||||
@@ -859,6 +861,13 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public PaginatedResult<String> listClusters(Integer maxResults, String nextToken) {
|
||||
int limit = EksAccessEntryService.pageSize(maxResults);
|
||||
List<String> names = listClusters().stream().sorted().toList();
|
||||
return Pagination.paginate(names, name -> name, limit, nextToken, "clusters:" + regionResolver.getAccountId(),
|
||||
token -> new AwsException("InvalidParameterException", "Invalid nextToken", 400));
|
||||
}
|
||||
|
||||
public Cluster deleteCluster(String name) {
|
||||
// A first node group replacing the node holds this lock while the swap runs; deleting
|
||||
// meanwhile would find no container to stop and have its cluster written back. Read the
|
||||
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
package io.floci.aws.services.eks;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.path.json.JsonPath;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasItems;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.lessThanOrEqualTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class EksListClustersPaginationIntegrationTest {
|
||||
|
||||
private static final String JSON = "application/json";
|
||||
private static final List<String> CLUSTERS = List.of("page-it-a", "page-it-b", "page-it-c");
|
||||
private static final String OTHER_ACCOUNT_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=111111111111/20260101/us-east-1/eks/aws4_request, "
|
||||
+ "SignedHeaders=host, Signature=abc";
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createClusters() {
|
||||
for (String name : CLUSTERS) {
|
||||
given().contentType(JSON)
|
||||
.body("{\"name\":\"" + name + "\",\"roleArn\":\"arn:aws:iam::000000000000:role/eks-role\","
|
||||
+ "\"version\":\"1.29\"}")
|
||||
.when().post("/clusters")
|
||||
.then().statusCode(200);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void listClustersRefusesATokenIssuedToAnotherAccount() {
|
||||
String token = given()
|
||||
.queryParam("maxResults", 1)
|
||||
.when().get("/clusters")
|
||||
.then().statusCode(200)
|
||||
.extract().path("nextToken");
|
||||
given().header("Authorization", OTHER_ACCOUNT_AUTH).contentType(JSON)
|
||||
.body("{\"name\":\"aaa-other-account\",\"roleArn\":\"arn:aws:iam::111111111111:role/eks-role\","
|
||||
+ "\"version\":\"1.29\"}")
|
||||
.when().post("/clusters")
|
||||
.then().statusCode(200);
|
||||
try {
|
||||
given().header("Authorization", OTHER_ACCOUNT_AUTH)
|
||||
.queryParam("nextToken", token)
|
||||
.when().get("/clusters")
|
||||
.then().statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterException"));
|
||||
} finally {
|
||||
given().header("Authorization", OTHER_ACCOUNT_AUTH)
|
||||
.when().delete("/clusters/aaa-other-account")
|
||||
.then().statusCode(200);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
void deleteClusters() {
|
||||
for (String name : CLUSTERS) {
|
||||
given().when().delete("/clusters/" + name).then().statusCode(200);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void listClustersPagesOneClusterAtATimeUntilTheLastPage() {
|
||||
List<String> seen = new ArrayList<>();
|
||||
String token = null;
|
||||
int pages = 0;
|
||||
do {
|
||||
JsonPath page = given()
|
||||
.queryParam("maxResults", 1)
|
||||
.queryParams(token == null ? Map.of() : Map.of("nextToken", token))
|
||||
.when().get("/clusters")
|
||||
.then().statusCode(200)
|
||||
.extract().jsonPath();
|
||||
List<String> clusters = page.getList("clusters", String.class);
|
||||
assertThat(clusters, hasSize(lessThanOrEqualTo(1)));
|
||||
seen.addAll(clusters);
|
||||
token = page.getString("nextToken");
|
||||
pages++;
|
||||
} while (token != null && pages < 1000);
|
||||
|
||||
assertThat(seen, hasItems(CLUSTERS.toArray(new String[0])));
|
||||
assertEquals(seen.size(), seen.stream().distinct().count());
|
||||
assertEquals(seen.size(), pages);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void listClustersWithoutMaxResultsReturnsEveryClusterWithoutAToken() {
|
||||
given()
|
||||
.when().get("/clusters")
|
||||
.then().statusCode(200)
|
||||
.body("clusters", hasItems(CLUSTERS.toArray(new String[0])))
|
||||
.body("nextToken", nullValue());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@Order(4)
|
||||
@ValueSource(ints = {0, 101})
|
||||
void listClustersRefusesAMaxResultsOutsideOneToOneHundred(int maxResults) {
|
||||
given()
|
||||
.queryParam("maxResults", maxResults)
|
||||
.when().get("/clusters")
|
||||
.then().statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void listClustersRefusesAnUnreadableToken() {
|
||||
given()
|
||||
.queryParam("nextToken", "not-a-token")
|
||||
.when().get("/clusters")
|
||||
.then().statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterException"))
|
||||
.body("message", not(nullValue()));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user