diff --git a/docs/services/kinesis.md b/docs/services/kinesis.md index 3870e40bdb..375bb37f4d 100644 --- a/docs/services/kinesis.md +++ b/docs/services/kinesis.md @@ -39,6 +39,17 @@ | `UpdateShardCount` | - | +## Stream Listing + +`ListStreams` returns sorted `StreamNames`, with at most 100 names per page by default. +`Limit` accepts integers from 1 to 10000, but values above 100 still return at most 100 names. +When `HasMoreStreams` is true, pass the last returned name as `ExclusiveStartStreamName` +to continue. Truncated pages also return `NextToken`, which CLI and SDK paginators can send +back without an `ExclusiveStartStreamName`. Tokens are scoped to this operation and region, +use the same configurable lifetime as `ListShards` tokens (see the TTL setting below), +and remain usable if the cursor stream is deleted. +`StreamSummaries` are not currently modeled. + ## Local Inspection Endpoints These endpoints are Floci-local read-only helpers for the UI and tests. AWS SDK @@ -100,7 +111,7 @@ action, so that limit is intentionally unenforced here. | Variable | Default | Description | |---|---|---| | `FLOCI_SERVICES_KINESIS_ENABLED` | `true` | Enable or disable the service | -| `FLOCI_SERVICES_KINESIS_LIST_SHARDS_NEXT_TOKEN_TTL_MILLIS` | `300000` | Lifetime of a `ListShards` `NextToken` in milliseconds; AWS expires tokens after 300000 ms. Lower it to exercise `ExpiredNextTokenException` without waiting | +| `FLOCI_SERVICES_KINESIS_LIST_SHARDS_NEXT_TOKEN_TTL_MILLIS` | `300000` | Lifetime of `ListShards` and `ListStreams` `NextToken` values in milliseconds. Lower it to exercise `ExpiredNextTokenException` without waiting | ## Enhanced Fan-Out (EFO) diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java index 2e34c2c1ee..853bec0f63 100644 --- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java +++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java @@ -1988,9 +1988,8 @@ public interface EmulatorConfig { boolean enabled(); /** - * Lifetime of a ListShards NextToken, in milliseconds. AWS expires these tokens 300000 - * milliseconds after they are issued; lowering it lets tests exercise the expiry path - * without waiting. + * Lifetime of ListShards and ListStreams NextTokens, in milliseconds. Defaults to 300000; + * lowering it lets tests exercise the expiry path without waiting. */ @WithDefault("300000") long listShardsNextTokenTtlMillis(); diff --git a/src/main/java/io/github/hectorvent/floci/services/kinesis/KinesisJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/kinesis/KinesisJsonHandler.java index 96e6ae8aac..f548505bca 100644 --- a/src/main/java/io/github/hectorvent/floci/services/kinesis/KinesisJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/kinesis/KinesisJsonHandler.java @@ -37,17 +37,17 @@ public class KinesisJsonHandler { private final KinesisService service; private final ObjectMapper objectMapper; - private final long listShardsNextTokenTtlMillis; + private final long nextTokenTtlMillis; @Inject public KinesisJsonHandler(KinesisService service, ObjectMapper objectMapper, EmulatorConfig config) { this(service, objectMapper, config.services().kinesis().listShardsNextTokenTtlMillis()); } - KinesisJsonHandler(KinesisService service, ObjectMapper objectMapper, long listShardsNextTokenTtlMillis) { + KinesisJsonHandler(KinesisService service, ObjectMapper objectMapper, long nextTokenTtlMillis) { this.service = service; this.objectMapper = objectMapper; - this.listShardsNextTokenTtlMillis = listShardsNextTokenTtlMillis; + this.nextTokenTtlMillis = nextTokenTtlMillis; } public Response handle(String action, JsonNode request, String region) { @@ -225,14 +225,55 @@ public class KinesisJsonHandler { } private Response handleListStreams(JsonNode request, String region) { - List streamNames = service.listStreams(region); + int limit = 100; + if (request.hasNonNull("Limit")) { + JsonNode value = request.get("Limit"); + if (!value.isIntegralNumber() || !value.canConvertToInt() || value.intValue() < 1 || value.intValue() > 10000) { + throw new AwsException("InvalidArgumentException", "Limit must be an integer between 1 and 10000", 400); + } + limit = Math.min(value.intValue(), 100); + } + JsonNode start = request.path("ExclusiveStartStreamName"); + String startName = start.asText(null); + if (!start.isMissingNode() && !start.isNull() + && (!start.isTextual() || !startName.matches("[a-zA-Z0-9_.-]{1,128}"))) { + throw new AwsException("InvalidArgumentException", "ExclusiveStartStreamName must be a valid stream name", 400); + } + JsonNode token = request.path("NextToken"); + if (!token.isMissingNode() && !token.isNull() && !token.isTextual()) { + throw invalidNextToken(); + } + String after = token.isTextual() ? streamCursorFromToken(decodeNextToken(token.textValue()), region) : startName; + List streamNames = service.listStreams(region).stream() + .filter(name -> after == null || name.compareTo(after) > 0) + .toList(); + List page = streamNames.stream().limit(limit).toList(); ObjectNode response = objectMapper.createObjectNode(); ArrayNode names = response.putArray("StreamNames"); - streamNames.forEach(names::add); - response.put("HasMoreStreams", false); + page.forEach(names::add); + response.put("HasMoreStreams", streamNames.size() > limit); + if (streamNames.size() > limit) { + response.put("NextToken", encodeNextToken("ListStreams|" + region, + Base64Url.encode(page.getLast()), System.currentTimeMillis())); + } return Response.ok(response).build(); } + private String streamCursorFromToken(PaginationToken token, String region) { + if (!("ListStreams|" + region).equals(token.scope())) { + throw invalidNextToken(); + } + try { + String cursor = new String(Base64.getUrlDecoder().decode(token.cursor()), StandardCharsets.UTF_8); + if (!Base64Url.encode(cursor).equals(token.cursor())) { + throw invalidNextToken(); + } + return cursor; + } catch (IllegalArgumentException e) { + throw invalidNextToken(); + } + } + private Response handleDescribeStream(JsonNode request, String region) { String streamName = resolveStreamName(request); KinesisStream stream = service.describeStream(streamName, region); @@ -705,7 +746,7 @@ public class KinesisJsonHandler { private Response handleListShards(JsonNode request, String region) { String nextToken = request.hasNonNull("NextToken") ? request.path("NextToken").asText() : null; - ShardToken token = nextToken == null ? null : decodeShardToken(nextToken); + PaginationToken token = nextToken == null ? null : decodeNextToken(nextToken); String resolvedStreamName = token == null ? resolveStreamName(request) : streamNameFromToken(request, token); @@ -727,7 +768,7 @@ public class KinesisJsonHandler { int start = token == null ? 0 : resumeShardIndex(token, snapshot); List page = paginateShards(snapshot.subList(start, snapshot.size()), maxResults); String nextCursor = start + page.size() < snapshot.size() && !page.isEmpty() - ? encodeShardToken(resolvedStreamName, page.get(page.size() - 1).getShardId(), + ? encodeNextToken(resolvedStreamName, page.get(page.size() - 1).getShardId(), System.currentTimeMillis()) : null; @@ -760,36 +801,44 @@ public class KinesisJsonHandler { } /** - * Opaque, stream-bound cursor for ListShards. It carries the stream name, the last shard id of - * the emitted page, and the issue time, so a client can resume from the token alone without - * resending the stream name and without depending on a shard's position in the live list. + * Opaque cursor shared by ListStreams and ListShards, bound to a scope and issue time. + * ListStreams scopes include the operation and region; ListShards scopes name the stream. */ - static String encodeShardToken(String streamName, String lastShardId, long issuedAtEpochMilli) { - String raw = streamName + "|" + lastShardId + "|" + issuedAtEpochMilli; + static String encodeNextToken(String scope, String cursor, long issuedAtEpochMilli) { + String raw = scope + "|" + cursor + "|" + issuedAtEpochMilli; return Base64Url.encode(raw); } - ShardToken decodeShardToken(String nextToken) { + private PaginationToken decodeNextToken(String nextToken) { + if (nextToken.isEmpty() || nextToken.length() > 1_048_576) { + throw invalidNextToken(); + } String decoded; try { decoded = new String(Base64.getUrlDecoder().decode(nextToken), StandardCharsets.UTF_8); } catch (IllegalArgumentException e) { throw invalidNextToken(); } - String[] parts = decoded.split("\\|", -1); - if (parts.length != 3 || parts[0].isEmpty() || parts[1].isEmpty()) { + int timestampSeparator = decoded.lastIndexOf('|'); + int cursorSeparator = decoded.lastIndexOf('|', timestampSeparator - 1); + if (cursorSeparator < 1) { throw invalidNextToken(); } long issuedAtEpochMilli; try { - issuedAtEpochMilli = Long.parseLong(parts[2]); + issuedAtEpochMilli = Long.parseLong(decoded.substring(timestampSeparator + 1)); } catch (NumberFormatException e) { throw invalidNextToken(); } - if (System.currentTimeMillis() - issuedAtEpochMilli >= listShardsNextTokenTtlMillis) { + long now = System.currentTimeMillis(); + if (issuedAtEpochMilli > now) { + throw invalidNextToken(); + } + if (issuedAtEpochMilli <= now - nextTokenTtlMillis) { throw new AwsException("ExpiredNextTokenException", "The NextToken has expired.", 400); } - return new ShardToken(parts[0], parts[1], issuedAtEpochMilli); + return new PaginationToken(decoded.substring(0, cursorSeparator), + decoded.substring(cursorSeparator + 1, timestampSeparator), issuedAtEpochMilli); } /** @@ -798,24 +847,27 @@ public class KinesisJsonHandler { * {@code StreamARN} is tolerated when it names the same stream the token does, which keeps an * SDK that fills the ARN in for routing working, and rejected when it names a different one. */ - private String streamNameFromToken(JsonNode request, ShardToken token) { + private String streamNameFromToken(JsonNode request, PaginationToken token) { + if (token.scope().contains("|") || token.cursor().isEmpty()) { + throw invalidNextToken(); + } if (request.hasNonNull("StreamName")) { throw new AwsException("InvalidArgumentException", "StreamName cannot be specified together with NextToken.", 400); } if (request.hasNonNull("StreamARN")) { String arnStreamName = parseStreamNameFromArn(request.path("StreamARN").asText()); - if (!token.streamName().equals(arnStreamName)) { + if (!token.scope().equals(arnStreamName)) { throw new AwsException("InvalidArgumentException", "The NextToken does not belong to the stream named by StreamARN.", 400); } } - return token.streamName(); + return token.scope(); } - private static int resumeShardIndex(ShardToken token, List shards) { + private static int resumeShardIndex(PaginationToken token, List shards) { for (int i = 0; i < shards.size(); i++) { - if (shards.get(i).getShardId().equals(token.lastShardId())) { + if (shards.get(i).getShardId().equals(token.cursor())) { return i + 1; } } @@ -827,10 +879,9 @@ public class KinesisJsonHandler { } /** - * Decoded ListShards cursor: the stream it belongs to, the last shard id it covered, and when it - * was issued (epoch millis, for expiry). + * Decoded scoped cursor and its issue time (epoch millis). */ - record ShardToken(String streamName, String lastShardId, long issuedAtEpochMilli) { + private record PaginationToken(String scope, String cursor, long issuedAtEpochMilli) { } /** diff --git a/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisListStreamsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisListStreamsIntegrationTest.java new file mode 100644 index 0000000000..7de25bdce8 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisListStreamsIntegrationTest.java @@ -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 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 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("/"); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisListStreamsTest.java b/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisListStreamsTest.java new file mode 100644 index 0000000000..1627113b01 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisListStreamsTest.java @@ -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 names(ObjectNode response) { + return mapper.convertValue(response.path("StreamNames"), + mapper.getTypeFactory().constructCollectionType(List.class, String.class)); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisListStreamsTokenTest.java b/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisListStreamsTokenTest.java new file mode 100644 index 0000000000..7a5c0389a2 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisListStreamsTokenTest.java @@ -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 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 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 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 names(ObjectNode response) { + return mapper.convertValue(response.path("StreamNames"), + mapper.getTypeFactory().constructCollectionType(List.class, String.class)); + } +}