fix(kinesis): honor stream listing limits (#6145)

* fix(kinesis): honor stream listing limits

* fix(kinesis): support stream listing continuation tokens

Return a region-scoped NextToken for truncated ListStreams pages and resume from it with five-minute expiry. Preserve strict name validation and current-account storage scope. Cover 150-stream traversal, deleted cursors, malformed and expired tokens, and the AWS JSON route.

* fix(kinesis): share configured pagination token expiry

Use one token codec and the existing configured TTL for ListStreams and ListShards. Preserve both previous token formats and verify emitted-token expiry with zero-TTL handlers instead of editing token timestamps.
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-10-21 16:04:12 -04:00
1 parent a6433d521a
commit 52c54834b0
6 files changed
+423 -31

No files matched your search

@@ -0,0 +1,63 @@
package io.github.hectorvent.floci.services.kinesis;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.Response;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@QuarkusTest
class KinesisListStreamsIntegrationTest {
private final List<String> streams = new ArrayList<>();
@BeforeAll
static void configureContentTypes() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@AfterEach
void deleteOwnedStreams() {
for (String stream : streams) {
kinesis("DeleteStream", Map.of("StreamName", stream)).then().statusCode(200);
}
}
@Test
void followsNameBasedPagesOverTheAwsJsonEndpoint() {
String prefix = "page-" + UUID.randomUUID();
for (String suffix : List.of("-a", "-b")) {
String stream = prefix + suffix;
kinesis("CreateStream", Map.of("StreamName", stream, "ShardCount", 1)).then().statusCode(200);
streams.add(stream);
}
Response first = kinesis("ListStreams", Map.of("Limit", 1, "ExclusiveStartStreamName", prefix))
.then().statusCode(200).extract().response();
assertEquals(List.of(streams.get(0)), first.jsonPath().getList("StreamNames", String.class));
assertTrue(first.jsonPath().getBoolean("HasMoreStreams"));
String token = first.jsonPath().getString("NextToken");
assertTrue(token != null && !token.isEmpty());
Response tokenPage = kinesis("ListStreams", Map.of("Limit", 1, "NextToken", token))
.then().statusCode(200).extract().response();
assertEquals(List.of(streams.get(1)), tokenPage.jsonPath().getList("StreamNames", String.class));
Response last = kinesis("ListStreams", Map.of("Limit", 1, "ExclusiveStartStreamName", streams.get(0)))
.then().statusCode(200).extract().response();
assertEquals(List.of(streams.get(1)), last.jsonPath().getList("StreamNames", String.class));
}
private Response kinesis(String action, Map<String, ?> body) {
return given().contentType("application/x-amz-json-1.1")
.header("Authorization", "AWS4-HMAC-SHA256 Credential=test/20261021/eu-central-2/kinesis/aws4_request")
.header("X-Amz-Target", "Kinesis_20131202." + action).body(body).post("/");
}
}
@@ -0,0 +1,102 @@
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.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
class KinesisListStreamsTest {
private static final String REGION = "us-east-1";
private final ObjectMapper mapper = new ObjectMapper();
private final KinesisService service = new KinesisService(new InMemoryStorage<>(), new InMemoryStorage<>(),
new RegionResolver(REGION, "000000000000"));
private final KinesisJsonHandler handler = new KinesisJsonHandler(service, mapper, 300_000);
@Test
void pagesSortedNamesUsingAnExclusiveStartName() throws Exception {
create("zeta", REGION);
create("alpha", REGION);
create("mu", REGION);
create("other-region", "eu-west-1");
ObjectNode first = list(Map.of("Limit", 1));
assertEquals(List.of("alpha"), names(first));
assertTrue(first.path("HasMoreStreams").asBoolean());
ObjectNode second = list(Map.of("Limit", 1, "ExclusiveStartStreamName", "alpha"));
assertEquals(List.of("mu"), names(second));
assertTrue(second.path("HasMoreStreams").asBoolean());
ObjectNode last = list(Map.of("Limit", 1, "ExclusiveStartStreamName", "mu"));
assertEquals(List.of("zeta"), names(last));
assertFalse(last.path("HasMoreStreams").asBoolean());
ObjectNode empty = list(Map.of("ExclusiveStartStreamName", "zeta"));
assertEquals(List.of(), names(empty));
assertFalse(empty.path("HasMoreStreams").asBoolean());
assertEquals(List.of("alpha", "mu", "zeta"), names(list(Map.of("Limit", 3))));
assertFalse(list(Map.of("Limit", 3)).path("HasMoreStreams").asBoolean());
}
@ParameterizedTest
@ValueSource(strings = {"{}", "{\"Limit\":101}", "{\"Limit\":10000}"})
void defaultsAndLargeLimitsStillCapEachPageAtOneHundred(String request) throws Exception {
for (int i = 0; i < 101; i++) {
create(String.format("stream-%03d", i), REGION);
}
ObjectNode first = list(mapper.readTree(request));
assertEquals(100, names(first).size());
assertTrue(first.path("HasMoreStreams").asBoolean());
ObjectNode last = list(Map.of("ExclusiveStartStreamName", "stream-099"));
assertEquals(List.of("stream-100"), names(last));
assertFalse(last.path("HasMoreStreams").asBoolean());
}
@ParameterizedTest
@ValueSource(strings = {"0", "-1", "10001", "1.5", "\"one\"", "999999999999999999999"})
void rejectsInvalidLimitsEvenForAnEmptyRegion(String limit) throws Exception {
ObjectNode request = (ObjectNode) mapper.readTree("{\"Limit\":" + limit + "}");
AwsException error = assertThrows(AwsException.class, () -> list(request));
assertEquals("InvalidArgumentException", error.getErrorCode());
assertEquals(400, error.getHttpStatus());
}
@ParameterizedTest
@ValueSource(strings = {"\"\"", "\"invalid/name\"", "123"})
void rejectsInvalidExclusiveStartNames(String name) throws Exception {
ObjectNode request = (ObjectNode) mapper.readTree("{\"ExclusiveStartStreamName\":" + name + "}");
AwsException error = assertThrows(AwsException.class, () -> list(request));
assertEquals("InvalidArgumentException", error.getErrorCode());
assertEquals(400, error.getHttpStatus());
}
private void create(String name, String region) {
try (Response response = handler.handle("CreateStream",
mapper.valueToTree(Map.of("StreamName", name, "ShardCount", 1)), region)) {
assertEquals(200, response.getStatus());
}
}
private ObjectNode list(Object request) {
try (Response response = handler.handle("ListStreams", mapper.valueToTree(request), REGION)) {
assertEquals(200, response.getStatus());
return (ObjectNode) response.getEntity();
}
}
private List<String> names(ObjectNode response) {
return mapper.convertValue(response.path("StreamNames"),
mapper.getTypeFactory().constructCollectionType(List.class, String.class));
}
}
@@ -0,0 +1,166 @@
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.core.common.AwsException;
import io.github.hectorvent.floci.core.common.Base64Url;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.Test;
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 org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
class KinesisListStreamsTokenTest {
private static final String REGION = "us-east-1";
private final ObjectMapper mapper = new ObjectMapper();
private final KinesisService service = new KinesisService(new InMemoryStorage<>(), new InMemoryStorage<>(),
new RegionResolver(REGION, "000000000000"));
private final KinesisJsonHandler handler = new KinesisJsonHandler(service, mapper, 300_000);
@Test
void collectsAllOneHundredFiftyStreamsUsingOnlyNextToken() {
List<String> expected = new ArrayList<>();
for (int i = 0; i < 150; i++) {
String name = String.format("stream-%03d", i);
service.createStream(name, 1, REGION);
expected.add(name);
}
ObjectNode first = list(Map.of());
List<String> seen = new ArrayList<>(names(first));
assertEquals(100, seen.size());
assertTrue(first.path("HasMoreStreams").asBoolean());
assertTrue(first.hasNonNull("NextToken"));
ObjectNode last = list(Map.of("NextToken", first.path("NextToken").asText()));
assertEquals(50, names(last).size());
seen.addAll(names(last));
assertEquals(expected, seen);
assertFalse(last.path("HasMoreStreams").asBoolean());
assertFalse(last.has("NextToken"));
}
@Test
void resumesAfterADeletedCursorAndAllowsADifferentPageSize() {
createThreeStreams();
ObjectNode first = list(Map.of("Limit", 1));
assertEquals(List.of("a"), names(first));
assertTrue(first.hasNonNull("NextToken"));
service.deleteStream("a", REGION);
ObjectNode last = list(Map.of("NextToken", first.path("NextToken").asText(), "Limit", 2));
assertEquals(List.of("b", "c"), names(last));
assertFalse(last.path("HasMoreStreams").asBoolean());
assertFalse(last.has("NextToken"));
assertFalse(list(Map.of("ExclusiveStartStreamName", "z")).has("NextToken"));
}
@ParameterizedTest
@ValueSource(strings = {"\"\"", "\"!\"", "\"YQ\"", "123", "{}", "[]", "false"})
void rejectsMalformedAndNonStringTokens(String token) throws Exception {
ObjectNode request = (ObjectNode) mapper.readTree("{\"NextToken\":" + token + "}");
AwsException error = assertThrows(AwsException.class, () -> list(request));
assertEquals("InvalidArgumentException", error.getErrorCode());
assertEquals(400, error.getHttpStatus());
}
@Test
void rejectsATokenFromAnotherRegionOrOperation() {
createThreeStreams();
ObjectNode first = list(Map.of("Limit", 1));
assertTrue(first.hasNonNull("NextToken"));
AwsException regionError = assertThrows(AwsException.class,
() -> handler.handle("ListStreams", mapper.valueToTree(Map.of(
"NextToken", first.path("NextToken").asText())), "eu-west-1"));
assertEquals("InvalidArgumentException", regionError.getErrorCode());
AwsException operationError = assertThrows(AwsException.class,
() -> list(Map.of("NextToken", KinesisJsonHandler.encodeNextToken("a", "shard-0",
System.currentTimeMillis()))));
assertEquals("InvalidArgumentException", operationError.getErrorCode());
AwsException reverseOperationError = assertThrows(AwsException.class,
() -> handler.handle("ListShards", mapper.valueToTree(Map.of(
"NextToken", first.path("NextToken").asText())), REGION));
assertEquals("InvalidArgumentException", reverseOperationError.getErrorCode());
}
@Test
void rejectsFutureTokens() {
String token = KinesisJsonHandler.encodeNextToken("ListStreams|" + REGION,
Base64Url.encode("a"), System.currentTimeMillis() + 60_000);
AwsException error = assertThrows(AwsException.class, () -> list(Map.of("NextToken", token)));
assertEquals("InvalidArgumentException", error.getErrorCode());
assertEquals(400, error.getHttpStatus());
}
@ParameterizedTest
@ValueSource(strings = {"ListStreams", "ListShards"})
void usesConfiguredExpiryForBothListOperations(String operation) {
service.createStream("a", 3, REGION);
service.createStream("b", 1, REGION);
KinesisJsonHandler expiringHandler = new KinesisJsonHandler(service, mapper, 0);
Map<String, ?> firstRequest = "ListStreams".equals(operation)
? Map.of("Limit", 1) : Map.of("StreamName", "a", "MaxResults", 1);
String token;
try (Response first = expiringHandler.handle(operation, mapper.valueToTree(firstRequest), REGION)) {
ObjectNode page = (ObjectNode) first.getEntity();
assertTrue(page.hasNonNull("NextToken"));
token = page.path("NextToken").asText();
}
AwsException error = assertThrows(AwsException.class,
() -> expiringHandler.handle(operation, mapper.valueToTree(Map.of("NextToken", token)), REGION));
assertEquals("ExpiredNextTokenException", error.getErrorCode());
assertEquals(400, error.getHttpStatus());
}
@ParameterizedTest
@ValueSource(strings = {"ListStreams", "ListShards"})
void acceptsTokensMintedByThePreviousCodec(String operation) {
service.createStream("a", 3, REGION);
service.createStream("b", 1, REGION);
String shardId = service.describeStream("a", REGION).getShards().getFirst().getShardId();
String oldPayload = "ListStreams".equals(operation)
? "ListStreams|" + REGION + "|" + Base64Url.encode("a") : "a|" + shardId;
String token = Base64Url.encode(oldPayload + "|" + System.currentTimeMillis());
try (Response response = handler.handle(operation, mapper.valueToTree(Map.of("NextToken", token)), REGION)) {
assertEquals(200, response.getStatus());
ObjectNode page = (ObjectNode) response.getEntity();
if ("ListStreams".equals(operation)) {
assertEquals(List.of("b"), names(page));
} else {
assertEquals(2, page.path("Shards").size());
assertEquals(service.describeStream("a", REGION).getShards().get(1).getShardId(),
page.path("Shards").get(0).path("ShardId").asText());
}
assertFalse(page.has("NextToken"));
}
}
private void createThreeStreams() {
for (String name : List.of("a", "b", "c")) {
service.createStream(name, 1, REGION);
}
}
private ObjectNode list(Object request) {
try (Response response = handler.handle("ListStreams", mapper.valueToTree(request), REGION)) {
assertEquals(200, response.getStatus());
return (ObjectNode) response.getEntity();
}
}
private List<String> names(ObjectNode response) {
return mapper.convertValue(response.path("StreamNames"),
mapper.getTypeFactory().constructCollectionType(List.class, String.class));
}
}