fix(kinesis): paginate ListShards with an opaque NextToken (#4098)

This commit is contained in:
Daniël van Ziel authored and GitHub committed 2026-09-22 13:38:28 -04:00
1 parent f9e96f3d22
commit 8e6faffcaf
9 files changed
+412 -5

No files matched your search

@@ -2,8 +2,10 @@ package io.github.hectorvent.floci.services.kinesis;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.Response;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
@@ -12,6 +14,9 @@ import org.junit.jupiter.api.TestMethodOrder;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -1384,4 +1389,84 @@ class KinesisIntegrationTest {
.statusCode(400)
.body("__type", equalTo("ResourceNotFoundException"));
}
private static void createStream(String name, int shardCount) {
given()
.header("X-Amz-Target", "Kinesis_20131202.CreateStream")
.contentType(KINESIS_CONTENT_TYPE)
.body("{\"StreamName\": \"" + name + "\", \"ShardCount\": " + shardCount + "}")
.when()
.post("/")
.then()
.statusCode(200);
}
private static Response listShardsPage(String streamName, Integer maxResults, String nextToken) {
ObjectNode request = MAPPER.createObjectNode();
if (streamName != null) {
request.put("StreamName", streamName);
}
if (maxResults != null) {
request.put("MaxResults", maxResults);
}
if (nextToken != null) {
request.put("NextToken", nextToken);
}
return given()
.header("X-Amz-Target", "Kinesis_20131202.ListShards")
.contentType(KINESIS_CONTENT_TYPE)
.body(request.toString())
.when()
.post("/")
.then()
.extract().response();
}
@Test
@Order(68)
void listShardsPaginatesWithNextTokenAndExhausts() {
createStream("kinesis-pagination-test", 3);
List<String> seen = new ArrayList<>();
String nextToken = null;
int pages = 0;
do {
// AWS documents NextToken as the only stream identifier once paging has started, so only
// the first page names the stream.
Response response = listShardsPage(pages == 0 ? "kinesis-pagination-test" : null, 1, nextToken);
response.then().statusCode(200);
seen.addAll(response.jsonPath().getList("Shards.ShardId", String.class));
nextToken = response.jsonPath().getString("NextToken");
pages++;
} while (nextToken != null && pages < 10);
assertEquals(3, seen.size(), "every shard is seen exactly once across pages");
assertEquals(3, new HashSet<>(seen).size(), "no shard repeats across pages");
assertNull(nextToken, "the final page must omit NextToken");
}
@Test
@Order(69)
void listShardsRejectsStreamNameTogetherWithNextToken() {
createStream("kinesis-pagination-a", 2);
Response firstPage = listShardsPage("kinesis-pagination-a", 1, null);
firstPage.then().statusCode(200);
String token = firstPage.jsonPath().getString("NextToken");
assertNotNull(token, "a partial first page must carry a NextToken");
listShardsPage("kinesis-pagination-a", 1, token)
.then()
.statusCode(400)
.body("__type", equalTo("InvalidArgumentException"));
}
@Test
@Order(70)
void listShardsRejectsAMalformedToken() {
listShardsPage("list-shards-test", 1, "not-a-valid-token!")
.then()
.statusCode(400)
.body("__type", equalTo("InvalidArgumentException"));
}
}
@@ -52,7 +52,7 @@ class KinesisJsonHandlerTest {
new InMemoryStorage<>(),
new RegionResolver(REGION, ACCOUNT)
);
handler = new KinesisJsonHandler(service, MAPPER);
handler = new KinesisJsonHandler(service, MAPPER, 300_000);
}
private void createStream(String name) {
@@ -1584,6 +1584,97 @@ class KinesisJsonHandlerTest {
assertEquals(Map.of("Foo", "Bar"), service.listTagsForStream("test-stream", REGION));
}
@Test
void listShardsResumesFromTokenAloneWithoutTheStreamName() {
createStream("test-stream", 3);
ObjectNode first = MAPPER.createObjectNode();
first.put("StreamName", "test-stream");
first.put("MaxResults", 1);
ObjectNode firstPage = responseEntity(handler.handle("ListShards", first, REGION));
String token = firstPage.get("NextToken").asText();
String firstShardId = firstPage.get("Shards").get(0).get("ShardId").asText();
ObjectNode second = MAPPER.createObjectNode();
second.put("NextToken", token);
ObjectNode secondPage = responseEntity(handler.handle("ListShards", second, REGION));
assertEquals(2, secondPage.get("Shards").size(), "the token alone resumes after the first shard");
for (JsonNode shard : secondPage.get("Shards")) {
assertFalse(shard.get("ShardId").asText().equals(firstShardId));
}
}
@Test
void listShardsRejectsStreamNameTogetherWithNextToken() {
createStream("test-stream", 3);
ObjectNode first = MAPPER.createObjectNode();
first.put("StreamName", "test-stream");
first.put("MaxResults", 1);
String token = responseEntity(handler.handle("ListShards", first, REGION)).get("NextToken").asText();
ObjectNode both = MAPPER.createObjectNode();
both.put("StreamName", "test-stream");
both.put("NextToken", token);
AwsException ex = assertThrows(AwsException.class,
() -> handler.handle("ListShards", both, REGION));
assertEquals("InvalidArgumentException", ex.getErrorCode());
}
@Test
void listShardsAcceptsAStreamArnThatNamesTheTokenStream() {
createStream("test-stream", 3);
ObjectNode first = MAPPER.createObjectNode();
first.put("StreamName", "test-stream");
first.put("MaxResults", 1);
String token = responseEntity(handler.handle("ListShards", first, REGION)).get("NextToken").asText();
ObjectNode second = MAPPER.createObjectNode();
second.put("NextToken", token);
second.put("StreamARN", STREAM_ARN);
ObjectNode secondPage = responseEntity(handler.handle("ListShards", second, REGION));
assertEquals(2, secondPage.get("Shards").size());
}
@Test
void listShardsRejectsAStreamArnFromAnotherStream() {
createStream("test-stream", 3);
ObjectNode first = MAPPER.createObjectNode();
first.put("StreamName", "test-stream");
first.put("MaxResults", 1);
String token = responseEntity(handler.handle("ListShards", first, REGION)).get("NextToken").asText();
ObjectNode second = MAPPER.createObjectNode();
second.put("NextToken", token);
second.put("StreamARN", "arn:aws:kinesis:us-east-1:123456789012:stream/other-stream");
AwsException ex = assertThrows(AwsException.class,
() -> handler.handle("ListShards", second, REGION));
assertEquals("InvalidArgumentException", ex.getErrorCode());
}
@Test
void listShardsRejectsAnExpiredNextToken() {
KinesisJsonHandler zeroTtlHandler = new KinesisJsonHandler(service, MAPPER, 0);
createStream("test-stream", 3);
ObjectNode first = MAPPER.createObjectNode();
first.put("StreamName", "test-stream");
first.put("MaxResults", 1);
ObjectNode firstPage = (ObjectNode) zeroTtlHandler.handle("ListShards", first, REGION).getEntity();
String token = firstPage.get("NextToken").asText();
ObjectNode second = MAPPER.createObjectNode();
second.put("NextToken", token);
AwsException ex = assertThrows(AwsException.class,
() -> zeroTtlHandler.handle("ListShards", second, REGION));
assertEquals("ExpiredNextTokenException", ex.getErrorCode());
assertEquals(400, ex.getHttpStatus());
}
/** Every non-string tag value shape — number, boolean, null, object, array — plus a map mixing a valid and an invalid value. */
private static List<ObjectNode> nonStringTagValues() {
ObjectNode number = MAPPER.createObjectNode();
@@ -0,0 +1,79 @@
package io.github.hectorvent.floci.services.kinesis;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
import io.restassured.response.Response;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
/**
* Proves the ListShards NextToken expiry is driven by the
* {@code floci.services.kinesis.list-shards-next-token-ttl-millis} setting: with the TTL lowered to
* zero, the second page is rejected instead of waiting out AWS's 300 second window.
*/
@QuarkusTest
@TestProfile(KinesisListShardsTokenExpiryIntegrationTest.ZeroTtlProfile.class)
class KinesisListShardsTokenExpiryIntegrationTest {
private static final String KINESIS_CONTENT_TYPE = "application/x-amz-json-1.1";
private static final ObjectMapper MAPPER = new ObjectMapper();
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void listShardsRejectsAnExpiredNextToken() {
given()
.header("X-Amz-Target", "Kinesis_20131202.CreateStream")
.contentType(KINESIS_CONTENT_TYPE)
.body("{\"StreamName\": \"kinesis-expiry-test\", \"ShardCount\": 2}")
.when()
.post("/")
.then()
.statusCode(200);
ObjectNode first = MAPPER.createObjectNode();
first.put("StreamName", "kinesis-expiry-test");
first.put("MaxResults", 1);
Response firstPage = given()
.header("X-Amz-Target", "Kinesis_20131202.ListShards")
.contentType(KINESIS_CONTENT_TYPE)
.body(first.toString())
.when()
.post("/")
.then()
.statusCode(200)
.extract().response();
String token = firstPage.jsonPath().getString("NextToken");
ObjectNode second = MAPPER.createObjectNode();
second.put("NextToken", token);
given()
.header("X-Amz-Target", "Kinesis_20131202.ListShards")
.contentType(KINESIS_CONTENT_TYPE)
.body(second.toString())
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("ExpiredNextTokenException"));
}
public static final class ZeroTtlProfile implements QuarkusTestProfile {
@Override
public Map<String, String> getConfigOverrides() {
return Map.of("floci.services.kinesis.list-shards-next-token-ttl-millis", "0");
}
}
}