mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(stepfunctions): paginate state machine listings (#6717)
This commit is contained in:
1 parent
2eaf268727
commit
28e72d2496
5 files changed
+267
-3
No files matched your search
+32
-2
@@ -37,11 +37,13 @@ import java.util.Set;
|
||||
public class StepFunctionsJsonHandler {
|
||||
|
||||
private static final Duration HISTORY_TOKEN_LIFETIME = Duration.ofHours(24);
|
||||
private static final Duration LIST_TOKEN_LIFETIME = Duration.ofHours(24);
|
||||
|
||||
private final StepFunctionsService service;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final Clock clock;
|
||||
private final SignedMarkers historyTokens = new SignedMarkers();
|
||||
private final SignedMarkers listTokens = new SignedMarkers();
|
||||
|
||||
@Inject
|
||||
public StepFunctionsJsonHandler(StepFunctionsService service, ObjectMapper objectMapper, Clock clock) {
|
||||
@@ -289,19 +291,47 @@ public class StepFunctionsJsonHandler {
|
||||
}
|
||||
|
||||
private Response handleListStateMachines(JsonNode request, String region) {
|
||||
List<StateMachine> list = service.listStateMachines(region);
|
||||
Integer maxResults = parseOptionalInt(request, "maxResults");
|
||||
String nextToken = optionalText(request, "nextToken");
|
||||
StepFunctionsPaging.pageSize(maxResults, nextToken);
|
||||
PaginatedResult<StateMachine> page = service.listStateMachines(region, maxResults,
|
||||
nextToken == null ? null : listTokenPosition(nextToken, region));
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode array = response.putArray("stateMachines");
|
||||
for (StateMachine sm : list) {
|
||||
for (StateMachine sm : page.items()) {
|
||||
ObjectNode item = array.addObject();
|
||||
item.put("stateMachineArn", sm.getStateMachineArn());
|
||||
item.put("name", sm.getName());
|
||||
item.put("type", sm.getType());
|
||||
item.put("creationDate", sm.getCreationDate());
|
||||
}
|
||||
if (page.nextToken() != null) {
|
||||
response.put("nextToken", listTokens.issue(
|
||||
clock.millis() + ":" + Digests.sha256Hex(region) + ":" + page.nextToken()));
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private String listTokenPosition(String nextToken, String region) {
|
||||
String issued = listTokens.cursorOf(nextToken);
|
||||
if (issued == null) {
|
||||
throw new AwsException("InvalidToken", "The provided token is not valid.", 400);
|
||||
}
|
||||
String[] fields = issued.split(":", 3);
|
||||
long now = clock.millis();
|
||||
long issuedAt;
|
||||
try {
|
||||
issuedAt = Long.parseLong(fields[0]);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new AwsException("InvalidToken", "The provided token is not valid.", 400);
|
||||
}
|
||||
if (fields.length != 3 || !fields[1].equals(Digests.sha256Hex(region))
|
||||
|| issuedAt > now || now - issuedAt > LIST_TOKEN_LIFETIME.toMillis()) {
|
||||
throw new AwsException("InvalidToken", "The provided token is not valid.", 400);
|
||||
}
|
||||
return fields[2];
|
||||
}
|
||||
|
||||
private Response handleDeleteStateMachine(JsonNode request) {
|
||||
service.deleteStateMachine(request.path("stateMachineArn").asText());
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ package io.github.hectorvent.floci.services.stepfunctions;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
|
||||
/**
|
||||
* The page request ListActivities and GetExecutionHistory check: a {@code maxResults} of 0 to 1000,
|
||||
* The page request ListActivities, ListStateMachines and GetExecutionHistory check: a {@code maxResults} of 0 to 1000,
|
||||
* where 0 or none means 100, and a {@code nextToken} of 1 to 1024 characters. Callers read both
|
||||
* fields before calling it, so a wrongly typed field is reported before either bound.
|
||||
*/
|
||||
|
||||
+7
@@ -528,6 +528,13 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
|
||||
return stateMachineStore.scan(k -> k.startsWith(regionArnPrefix(region)));
|
||||
}
|
||||
|
||||
public PaginatedResult<StateMachine> listStateMachines(String region, Integer maxResults, String nextToken) {
|
||||
int pageSize = StepFunctionsPaging.pageSize(maxResults, nextToken);
|
||||
return Pagination.paginate(listStateMachines(region), StateMachine::getStateMachineArn, pageSize, nextToken,
|
||||
"states:state-machines:" + region + ":" + regionResolver.getAccountId(),
|
||||
token -> new AwsException("InvalidToken", "Invalid nextToken.", 400));
|
||||
}
|
||||
|
||||
// ── State machine versions ──────────────────────────────────────────────
|
||||
|
||||
public StateMachineVersion publishStateMachineVersion(String stateMachineArn) {
|
||||
|
||||
+141
@@ -0,0 +1,141 @@
|
||||
package io.github.hectorvent.floci.services.stepfunctions;
|
||||
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.Response;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
class StepFunctionsListStateMachinesPaginationIntegrationTest {
|
||||
|
||||
private static final String CONTENT_TYPE = "application/x-amz-json-1.0";
|
||||
private static final String REGION = "me-central-1";
|
||||
private static final String AUTHORIZATION = "AWS4-HMAC-SHA256 Credential=000000000000/20261026/"
|
||||
+ REGION + "/states/aws4_request, SignedHeaders=host, Signature=abc";
|
||||
@Inject StepFunctionsService service;
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
void maxResultsCapsThePage() throws Exception {
|
||||
List<String> arns = new ArrayList<>();
|
||||
try {
|
||||
for (int index = 0; index < 2; index++) {
|
||||
arns.add(service.createStateMachine(
|
||||
"issue-page-" + UUID.randomUUID(),
|
||||
"{\"StartAt\":\"Done\",\"States\":{\"Done\":{\"Type\":\"Succeed\"}}}",
|
||||
"arn:aws:iam::000000000000:role/test-role", "STANDARD", REGION, null)
|
||||
.getStateMachineArn());
|
||||
}
|
||||
Response firstPage = list(Map.of("maxResults", 1)).then().statusCode(200)
|
||||
.body("stateMachines", hasSize(1))
|
||||
.body("nextToken", notNullValue()).extract().response();
|
||||
String nextToken = firstPage.jsonPath().getString("nextToken");
|
||||
List<String> firstArns = firstPage.jsonPath().getList("stateMachines.stateMachineArn");
|
||||
Response lastPage = list(Map.of("maxResults", 1, "nextToken", nextToken))
|
||||
.then().statusCode(200).extract().response();
|
||||
List<String> lastArns = lastPage.jsonPath().getList("stateMachines.stateMachineArn");
|
||||
assertEquals(1, lastArns.size());
|
||||
assertNull(lastPage.jsonPath().getString("nextToken"));
|
||||
Set<String> listed = new HashSet<>(firstArns);
|
||||
assertTrue(listed.addAll(lastArns), "the continuation must not repeat the first state machine");
|
||||
assertEquals(new HashSet<>(arns), listed);
|
||||
|
||||
Response foreignRegion = given().contentType(CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AWSStepFunctions.ListStateMachines")
|
||||
.header("Authorization", "AWS4-HMAC-SHA256 Credential=000000000000/20261026/"
|
||||
+ "us-west-2/states/aws4_request, SignedHeaders=host, Signature=abc")
|
||||
.body(Map.of("maxResults", 1, "nextToken", nextToken))
|
||||
.post("/").then().statusCode(400).extract().response();
|
||||
assertEquals("InvalidToken", foreignRegion.jsonPath().getString("__type"));
|
||||
|
||||
Response foreignAccount = given().contentType(CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AWSStepFunctions.ListStateMachines")
|
||||
.header("Authorization", "AWS4-HMAC-SHA256 Credential=000000000111/20261026/"
|
||||
+ REGION + "/states/aws4_request, SignedHeaders=host, Signature=abc")
|
||||
.body(Map.of("maxResults", 1, "nextToken", nextToken))
|
||||
.post("/").then().statusCode(400).extract().response();
|
||||
assertEquals("InvalidToken", foreignAccount.jsonPath().getString("__type"));
|
||||
} finally {
|
||||
for (String arn : arns) {
|
||||
service.deleteStateMachine(arn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void zeroUsesTheDefaultPageSizeOfOneHundred() throws Exception {
|
||||
List<String> arns = new ArrayList<>();
|
||||
try {
|
||||
for (int index = 0; index < 101; index++) {
|
||||
arns.add(service.createStateMachine(
|
||||
"issue-default-page-" + UUID.randomUUID(),
|
||||
"{\"StartAt\":\"Done\",\"States\":{\"Done\":{\"Type\":\"Succeed\"}}}",
|
||||
"arn:aws:iam::000000000000:role/test-role", "STANDARD", REGION, null)
|
||||
.getStateMachineArn());
|
||||
}
|
||||
Response firstPage = list(Map.of("maxResults", 0)).then().statusCode(200).extract().response();
|
||||
List<String> firstArns = firstPage.jsonPath().getList("stateMachines.stateMachineArn");
|
||||
assertEquals(100, firstArns.size());
|
||||
String nextToken = firstPage.jsonPath().getString("nextToken");
|
||||
assertFalse(nextToken == null || nextToken.isEmpty());
|
||||
|
||||
Response lastPage = list(Map.of("maxResults", 0, "nextToken", nextToken))
|
||||
.then().statusCode(200).extract().response();
|
||||
List<String> lastArns = lastPage.jsonPath().getList("stateMachines.stateMachineArn");
|
||||
assertEquals(1, lastArns.size());
|
||||
assertNull(lastPage.jsonPath().getString("nextToken"));
|
||||
Set<String> listed = new HashSet<>(firstArns);
|
||||
assertTrue(listed.addAll(lastArns));
|
||||
assertEquals(new HashSet<>(arns), listed);
|
||||
} finally {
|
||||
for (String arn : arns) {
|
||||
service.deleteStateMachine(arn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsInvalidPageSizeAndToken() {
|
||||
for (Object maxResults : List.of(-1, 1001, "1", 4294967296L)) {
|
||||
Response response = list(Map.of("maxResults", maxResults))
|
||||
.then().statusCode(400).extract().response();
|
||||
assertEquals("ValidationException", response.jsonPath().getString("__type"));
|
||||
}
|
||||
for (String nextToken : List.of("", "x".repeat(1025))) {
|
||||
Response response = list(Map.of("nextToken", nextToken))
|
||||
.then().statusCode(400).extract().response();
|
||||
assertEquals("ValidationException", response.jsonPath().getString("__type"));
|
||||
}
|
||||
Response response = list(Map.of("nextToken", "not-a-valid-token"))
|
||||
.then().statusCode(400).extract().response();
|
||||
assertEquals("InvalidToken", response.jsonPath().getString("__type"));
|
||||
}
|
||||
|
||||
private static Response list(Map<String, ?> request) {
|
||||
return given().contentType(CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AWSStepFunctions.ListStateMachines")
|
||||
.header("Authorization", AUTHORIZATION)
|
||||
.body(request).post("/");
|
||||
}
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
package io.github.hectorvent.floci.services.stepfunctions;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
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.PaginatedResult;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.model.StateMachine;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class StepFunctionsListStateMachinesTokenTest {
|
||||
|
||||
private static final long ISSUED = 1_790_000_000_000L;
|
||||
private static final long DAY = Duration.ofHours(24).toMillis();
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private final StepFunctionsService service = mock(StepFunctionsService.class);
|
||||
private final Clock clock = mock(Clock.class);
|
||||
private StepFunctionsJsonHandler handler;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
StateMachine machine = new StateMachine();
|
||||
machine.setStateMachineArn("arn:aws:states:us-east-1:000000000000:stateMachine:page-one");
|
||||
machine.setName("page-one");
|
||||
when(service.listStateMachines("us-east-1", 1, null))
|
||||
.thenReturn(new PaginatedResult<>(List.of(machine), "inner-token"));
|
||||
when(service.listStateMachines("us-east-1", 1, "inner-token"))
|
||||
.thenReturn(new PaginatedResult<>(List.of(), null));
|
||||
when(clock.millis()).thenReturn(ISSUED);
|
||||
handler = new StepFunctionsJsonHandler(service, mapper, clock);
|
||||
}
|
||||
|
||||
@Test
|
||||
void tokenExpiresAfterTwentyFourHours() {
|
||||
String token = firstPageToken();
|
||||
when(clock.millis()).thenReturn(ISSUED + DAY);
|
||||
assertEquals(0, page(handler, "us-east-1", token).path("stateMachines").size());
|
||||
|
||||
when(clock.millis()).thenReturn(ISSUED + DAY + 1);
|
||||
assertInvalid(handler, "us-east-1", token);
|
||||
}
|
||||
|
||||
@Test
|
||||
void tokenCannotBeUsedInAnotherRegionOrAfterRestart() {
|
||||
String token = firstPageToken();
|
||||
assertInvalid(handler, "us-west-2", token);
|
||||
assertInvalid(new StepFunctionsJsonHandler(service, mapper, clock), "us-east-1", token);
|
||||
}
|
||||
|
||||
@Test
|
||||
void editedTokenIsRejected() {
|
||||
String token = firstPageToken();
|
||||
int middle = token.length() / 2;
|
||||
String edited = token.substring(0, middle) + (token.charAt(middle) == 'A' ? 'B' : 'A')
|
||||
+ token.substring(middle + 1);
|
||||
assertInvalid(handler, "us-east-1", edited);
|
||||
}
|
||||
|
||||
private String firstPageToken() {
|
||||
return (String) page(handler, "us-east-1", null).path("nextToken").asText();
|
||||
}
|
||||
|
||||
private JsonNode page(StepFunctionsJsonHandler target, String region, String token) {
|
||||
ObjectNode request = mapper.createObjectNode().put("maxResults", 1);
|
||||
if (token != null) {
|
||||
request.put("nextToken", token);
|
||||
}
|
||||
return (JsonNode) target.handle("ListStateMachines", request, region).getEntity();
|
||||
}
|
||||
|
||||
private void assertInvalid(StepFunctionsJsonHandler target, String region, String token) {
|
||||
AwsException error = assertThrows(AwsException.class, () -> page(target, region, token));
|
||||
assertEquals("InvalidToken", error.getErrorCode());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user