fix(testing): retain region while polling executions (#8482)

StepFunctionsExecutions.runInRegion polled its execution through the
unsigned describe helper, so every poll after StartExecution went to the
default Region. It now polls with the same Region-scoped Authorization
header as its other calls. The regional and unsigned paths share one
DescribeExecution request and its status check, and
StepFunctionsExecutionsTest pins the Region on every poll for each
terminal status and that both unsigned helpers still send no
Authorization header.

Co-authored-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
Jackson PriceandAndre Kurait authored and GitHub committed 2026-11-15 14:50:44 -04:00
1 parent 42ead66324
commit b13db563c9
2 files changed
+142 -6

No files matched your search

@@ -3,11 +3,13 @@ package io.floci.aws.testing;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.function.Supplier;
import static io.restassured.RestAssured.given;
import static org.awaitility.Awaitility.await;
@@ -24,7 +26,11 @@ public final class StepFunctionsExecutions {
private StepFunctionsExecutions() {}
public static Response describeExecution(String executionArn) {
Response response = given()
return describeExecution(executionArn, given());
}
private static Response describeExecution(String executionArn, RequestSpecification request) {
Response response = request
.header("X-Amz-Target", "AWSStepFunctions.DescribeExecution")
.contentType(SFN_CONTENT_TYPE)
.body("""
@@ -40,10 +46,14 @@ public final class StepFunctionsExecutions {
* the terminal status: asserting which one is the caller's business.
*/
public static Response waitForTerminalState(String executionArn) {
return waitForTerminalState(executionArn, () -> describeExecution(executionArn));
}
private static Response waitForTerminalState(String executionArn, Supplier<Response> describe) {
return await("execution " + executionArn + " to leave RUNNING")
.atMost(TERMINAL_STATE_TIMEOUT)
.pollInterval(POLL_INTERVAL)
.until(() -> describeExecution(executionArn),
.until(describe::get,
response -> !"RUNNING".equals(response.jsonPath().getString("status")));
}
@@ -60,7 +70,8 @@ public final class StepFunctionsExecutions {
String executionArn = callInRegion(region, "StartExecution", MAPPER.createObjectNode()
.put("stateMachineArn", stateMachineArn).put("input", "{}").toString())
.then().statusCode(200).extract().path("executionArn");
return waitForTerminalState(executionArn);
return waitForTerminalState(executionArn,
() -> describeExecution(executionArn, requestInRegion(region)));
}
/** The history events of an execution that {@link #runInRegion} ran in {@code region}. */
@@ -74,12 +85,16 @@ public final class StepFunctionsExecutions {
}
private static Response callInRegion(String region, String action, String body) {
return given()
.header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260215/" + region
+ "/states/aws4_request, SignedHeaders=host, Signature=abc")
return requestInRegion(region)
.header("X-Amz-Target", "AWSStepFunctions." + action)
.contentType(SFN_CONTENT_TYPE)
.body(body)
.when().post("/");
}
private static RequestSpecification requestInRegion(String region) {
return given()
.header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260215/" + region
+ "/states/aws4_request, SignedHeaders=host, Signature=abc");
}
}
@@ -0,0 +1,121 @@
package io.floci.aws.testing;
import com.sun.net.httpserver.HttpServer;
import io.restassured.RestAssured;
import io.restassured.config.RestAssuredConfig;
import io.restassured.response.Response;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
class StepFunctionsExecutionsTest {
private static final String EXECUTION_ARN = "arn:aws:states:eu-west-3:000000000000:execution:machine:run";
private final List<String> requests = new CopyOnWriteArrayList<>();
private final List<String> authorizationHeaders = new CopyOnWriteArrayList<>();
private final AtomicInteger descriptions = new AtomicInteger();
private HttpServer server;
private String previousBaseUri;
private int previousPort;
private RestAssuredConfig previousConfig;
private volatile String terminalStatus = "SUCCEEDED";
private volatile boolean requireRegion = true;
@BeforeEach
void startServer() throws Exception {
previousBaseUri = RestAssured.baseURI;
previousPort = RestAssured.port;
previousConfig = RestAssured.config;
RestAssuredJsonUtils.configureAwsContentTypes();
server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
server.createContext("/", exchange -> {
String action = exchange.getRequestHeaders().getFirst("X-Amz-Target");
String authorization = exchange.getRequestHeaders().getFirst("Authorization");
requests.add(action);
authorizationHeaders.add(authorization);
String request = new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8);
int status = 200;
String body;
if (requireRegion && (authorization == null || !authorization.contains("/eu-west-3/states/"))) {
status = 400;
body = "{\"__type\":\"InvalidArn\"}";
} else {
body = switch (action) {
case "AWSStepFunctions.CreateStateMachine" ->
"{\"stateMachineArn\":\"arn:aws:states:eu-west-3:000000000000:stateMachine:machine\"}";
case "AWSStepFunctions.StartExecution" -> "{\"executionArn\":\"" + EXECUTION_ARN + "\"}";
case "AWSStepFunctions.DescribeExecution" -> {
if (!request.contains(EXECUTION_ARN)) {
status = 400;
yield "{\"__type\":\"InvalidArn\"}";
}
String state = descriptions.incrementAndGet() == 1 ? "RUNNING" : terminalStatus;
yield "{\"status\":\"" + state + "\"}";
}
default -> throw new IllegalArgumentException(action);
};
}
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
exchange.getResponseHeaders().set("Content-Type", "application/json");
exchange.sendResponseHeaders(status, bytes.length);
try (exchange) {
exchange.getResponseBody().write(bytes);
}
});
server.start();
RestAssured.baseURI = "http://127.0.0.1";
RestAssured.port = server.getAddress().getPort();
}
@AfterEach
void stopServer() {
server.stop(0);
RestAssured.baseURI = previousBaseUri;
RestAssured.port = previousPort;
RestAssured.config = previousConfig;
}
@ParameterizedTest
@ValueSource(strings = {"SUCCEEDED", "FAILED", "TIMED_OUT", "ABORTED"})
void retainsRegionForEveryPollAndReturnsAnyTerminalState(String status) {
terminalStatus = status;
Response response = StepFunctionsExecutions.runInRegion("eu-west-3", "{}");
assertEquals(status, response.jsonPath().getString("status"));
assertEquals(2, descriptions.get());
assertEquals(List.of("AWSStepFunctions.CreateStateMachine", "AWSStepFunctions.StartExecution",
"AWSStepFunctions.DescribeExecution", "AWSStepFunctions.DescribeExecution"), requests);
}
@Test
void existingUnsignedDescribeRemainsAvailable() {
requireRegion = false;
Response response = StepFunctionsExecutions.describeExecution(EXECUTION_ARN);
assertEquals("RUNNING", response.jsonPath().getString("status"));
assertTrue(requests.contains("AWSStepFunctions.DescribeExecution"));
assertNull(authorizationHeaders.getFirst());
}
@Test
void existingUnsignedWaitStillReturnsTheTerminalResponse() {
requireRegion = false;
terminalStatus = "FAILED";
Response response = StepFunctionsExecutions.waitForTerminalState(EXECUTION_ARN);
assertEquals("FAILED", response.jsonPath().getString("status"));
assertEquals(2, descriptions.get());
assertEquals(List.of("AWSStepFunctions.DescribeExecution", "AWSStepFunctions.DescribeExecution"), requests);
assertNull(authorizationHeaders.get(0));
assertNull(authorizationHeaders.get(1));
}
}