Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

feat(sfn): support http invoke step (#1503)

Juri Duvalac8a3d7c8288
diff --git a/pom.xml b/pom.xml
index 439120a993..b1e731d11e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -116,6 +116,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx</artifactId>
</dependency>
+ <dependency>
+ <groupId>io.smallrye.reactive</groupId>
+ <artifactId>smallrye-mutiny-vertx-web-client</artifactId>
+ </dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-mqtt</artifactId>
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 07266f31f7..83db3e778b 100644
--- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
+++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
@@ -1015,6 +1015,10 @@ public interface EmulatorConfig {
interface StepFunctionsServiceConfig {
@WithDefault("true")
boolean enabled();
+
+ /** Allows invoking plain HTTP endpoints. By default, AWS only allows HTTPS. */
+ @WithDefault("true")
+ boolean allowPlaintextHttp();
}
interface CloudFormationServiceConfig {
diff --git a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java
index d85d47b752..17153d062a 100644
--- a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java
+++ b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.stepfunctions;
+import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
@@ -23,18 +24,27 @@ import io.github.hectorvent.floci.services.lambda.LambdaFunctionStore;
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
-import io.github.hectorvent.floci.services.s3.S3Service;
import io.github.hectorvent.floci.services.s3.model.S3Object;
import io.github.hectorvent.floci.services.sqs.SqsJsonHandler;
import io.github.hectorvent.floci.services.stepfunctions.model.Execution;
import io.github.hectorvent.floci.services.stepfunctions.model.HistoryEvent;
import io.github.hectorvent.floci.services.stepfunctions.model.StateMachine;
+import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.MissingNode;
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
+
+import io.vertx.core.http.HttpMethod;
+import io.vertx.core.impl.NoStackTraceTimeoutException;
+import io.vertx.mutiny.core.MultiMap;
+import io.vertx.mutiny.core.buffer.Buffer;
+import io.vertx.mutiny.core.Vertx;
+import io.vertx.mutiny.ext.web.client.HttpRequest;
+import io.vertx.mutiny.ext.web.client.HttpResponse;
+import io.vertx.mutiny.ext.web.client.WebClient;
import io.quarkus.arc.Arc;
import io.quarkus.arc.ArcContainer;
import io.quarkus.arc.ManagedContext;
@@ -45,17 +55,21 @@ import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap;
import org.jboss.logging.Logger;
+import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
+import java.util.LinkedHashMap;
import java.util.List;
+import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -63,6 +77,7 @@ import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.BiConsumer;
+import java.util.stream.Collectors;
@ApplicationScoped
public class AslExecutor {
@@ -84,6 +99,41 @@ public class AslExecutor {
private static final long ECS_SYNC_POLL_INTERVAL_MS = 100;
private static final String QUERY_LANGUAGE_JSONATA = "JSONata";
+ private static final Set<String> HTTP_ALLOWED_METHODS = Set.of(
+ "GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD");
+ private static final Set<String> HTTP_FORBIDDEN_HEADERS = Set.of(
+ "a-im",
+ "accept-charset",
+ "accept-datetime",
+ "accept-encoding",
+ "authorization",
+ "cache-control",
+ "connection",
+ "content-encoding",
+ "content-md5",
+ "date",
+ "expect",
+ "forwarded",
+ "from",
+ "host",
+ "http2-settings",
+ "if-match",
+ "if-modified-since",
+ "if-none-match",
+ "if-range",
+ "if-unmodified-since",
+ "max-forwards",
+ "origin",
+ "pragma",
+ "proxy-authorization",
+ "referer",
+ "server",
+ "te",
+ "trailer",
+ "transfer-encoding",
+ "upgrade",
+ "via",
+ "warning");
private final LambdaExecutorService lambdaExecutor;
private final LambdaFunctionStore functionStore;
@@ -98,6 +148,8 @@ public class AslExecutor {
private final ObjectMapper objectMapper;
private final JsonataEvaluator jsonataEvaluator;
private final Instance<StepFunctionsService> sfnService;
+ private final WebClient webClient;
+ private final EmulatorConfig config;
private final ExecutorService executor = Executors.newCachedThreadPool(r -> {
Thread t = new Thread(r, "sfn-executor");
t.setDaemon(true);
@@ -111,7 +163,7 @@ public class AslExecutor {
Ec2Service ec2Service, S3Service s3Service,
EcsService ecsService, EcsJsonHandler ecsJsonHandler,
ObjectMapper objectMapper, JsonataEvaluator jsonataEvaluator,
- Instance<StepFunctionsService> sfnService) {
+ Instance<StepFunctionsService> sfnService, EmulatorConfig config, Vertx vertx) {
this.lambdaExecutor = lambdaExecutor;
this.functionStore = functionStore;
this.dynamoDbService = dynamoDbService;
@@ -125,6 +177,14 @@ public class AslExecutor {
this.objectMapper = objectMapper;
this.jsonataEvaluator = jsonataEvaluator;
this.sfnService = sfnService;
+ this.config = config;
+ if (vertx != null) {
+ // This can be optimized further
+ // TODO Set WebclientOptions useragent to Amazon|StepFunctions|HttpInvoke|{{{{region}}}}
+ this.webClient = WebClient.wrap(vertx.createHttpClient());
+ } else {
+ webClient = null;
+ }
}
/**
@@ -503,6 +563,12 @@ public class AslExecutor {
return invokeOptimizedSqsSendMessage(input, region);
}
+ // HTTP optimized integration
+ if (resource.equals("arn:aws:states:::http:invoke")) {
+ String region = extractRegionFromArn(sm.getStateMachineArn());
+ return invokeHttp(input, region);
+ }
+
// AWS SDK service integration: SQS SendMessage
if (resource.equals("arn:aws:states:::aws-sdk:sqs:sendMessage")) {
String region = extractRegionFromArn(sm.getStateMachineArn());
@@ -1948,6 +2014,292 @@ public class AslExecutor {
return true;
}
+ /**
+ * Implements the Step Functions HTTP Task request flow for direct task-provided
+ * fields. EventBridge connection lookup and connection-level header, query
+ * parameter, and body merging are intentionally not implemented yet.
+ *
+ * TODO: Resolve Authentication/InvocationConfig ConnectionArn through the
+ * EventBridge connection store and merge connection credentials/parameters.
+ *
+ * TODO: Add HTTP retry support. This can be done via Mutiny's retry mechanism
+ * but most likely better to be done at a higher level to support other tasks.
+ *
+ * TODO: Add HTTP Task coverage for unsupported binary/media response content types.
+ */
+ private JsonNode invokeHttp(JsonNode input, String region) {
+ var rawUri = input.path("ApiEndpoint").asText(null);
+ var method = input.path("Method").asText(null);
+ var timeoutMillis = input.path("TimeoutSeconds").asLong(60) * 1_000;
+ var headers = input.path("Headers");
+ var queryParameters = input.path("QueryParameters");
+ var requestBody = input.path("RequestBody");
+ var requestBodyEncoding = input.path("Transform").path("RequestBodyEncoding").asText("NONE");
+
+ if (rawUri == null || rawUri.isBlank()) {
+ throw new FailStateException("States.Runtime", "ApiEndpoint is required for HTTP task");
+ }
+ var uri = URI.create(rawUri);
+ var isHttps = "https".equalsIgnoreCase(uri.getScheme());
+ var allowPlainHttp = config.services().stepfunctions().allowPlaintextHttp();
+ if (!allowPlainHttp && !isHttps) {
+ throw new FailStateException("States.Runtime", "The value for the 'ApiEndpoint' field must have the scheme 'https'. " +
+ "You can enable plaintext http via 'floci.services.stepfunctions.allow-plaintext-http=true'.");
+ }
+
+ validateHttpMethod(method);
+ validateConnectionArn(input);
+ validateHttpHeaders(headers);
+
+ var requestPayload = requestPayload(requestBody, requestBodyEncoding);
+ var requestHeaders = requestHeaders(headers, requestPayload.contentType());
+ var requestQueryParameters = queryParameters(queryParameters);
+
+ try {
+ var request = webClient.requestAbs(HttpMethod.valueOf(method), rawUri)
+ .timeout(timeoutMillis)
+ .putHeaders(requestHeaders);
+ request.queryParams().addAll(requestQueryParameters);
+
+ LOG.infov("Step Functions HTTP task sending request: method={0}, uri={1}", method, uri);
+ var response = sendHttpRequest(request, requestPayload);
+ validateHttpStatus(response);
+ validateHttpResponse(response);
+ return httpResultJson(response);
+ } catch (FailStateException e) {
+ throw e;
+ } catch (CompletionException e) {
+ if (e.getCause() instanceof NoStackTraceTimeoutException) {
+ throw new FailStateException("States.Http.Socket", e.getCause().getMessage());
+ } else {
+ throw new FailStateException("States.TaskFailed", e.getCause().getMessage());
+ }
+ } catch (Exception e) {
+ throw new FailStateException("States.TaskFailed", e.getMessage());
+ }
+ }
+
+ private HttpResponse<Buffer> sendHttpRequest(HttpRequest<Buffer> request, HttpRequestPayload payload) throws NoStackTraceTimeoutException {
+ if (payload.form() != null) {
+ return request.sendFormAndAwait(payload.form());
+ }
+ if (payload.body() != null) {
+ return request.sendBufferAndAwait(payload.body());
+ }
+ return request.sendAndAwait();
+ }
+
+ private void validateHttpStatus(HttpResponse<Buffer> response) {
+ int statusCode = response.statusCode();
+ if (statusCode < 200 || statusCode >= 300) {
+ throw new FailStateException("States.Http.StatusCode." + statusCode, response.bodyAsString());
+ }
+ }
+
+ private void validateHttpResponse(HttpResponse<Buffer> response) {
+ // TODO: Add HTTP Task coverage for unsupported binary/media response content types.
+ String contentType = response.getHeader("Content-Type");
+ if (contentType == null) {
+ return;
+ }
+
+ String normalized = contentType.toLowerCase(Locale.ROOT);
+ if (normalized.contains("application/octet-stream")
+ || normalized.startsWith("image/")
+ || normalized.startsWith("video/")
+ || normalized.startsWith("audio/")) {
+ throw new FailStateException("States.Runtime",
+ "HTTP task response contains unsupported content type: " + contentType);
+ }
+
+ try {
+ response.bodyAsString();
+ } catch (Exception e) {
+ throw new FailStateException("States.Runtime", "HTTP task response cannot be read as a string");
+ }
+ }
+
+ private JsonNode httpResultJson(HttpResponse<Buffer> response) {
+ var stepHttpResponse = new HttpTaskResponse(
+ response.statusCode(),
+ response.statusMessage(),
+ httpResponseHeaders(response),
+ httpResponseBody(response));
+ return objectMapper.valueToTree(stepHttpResponse);
+ }
+
+ private Map<String, List<String>> httpResponseHeaders(HttpResponse<Buffer> response) {
+ return response.headers().names().stream()
+ .collect(Collectors.toMap(
+ name -> name,
+ name -> List.copyOf(response.headers().getAll(name)),
+ (first, ignored) -> first,
+ LinkedHashMap::new));
+ }
+
+ private JsonNode httpResponseBody(HttpResponse<Buffer> response) {
+ String body = response.bodyAsString();
+ if (body == null || body.isBlank()) {
+ return NullNode.getInstance();
+ }
+ if (!isJsonResponse(response)) {
+ return objectMapper.getNodeFactory().textNode(body);
+ }
+ try {
+ return objectMapper.readTree(body);
+ } catch (Exception ignored) {
+ return objectMapper.getNodeFactory().textNode(body);
+ }
+ }
+
+ private boolean isJsonResponse(HttpResponse<Buffer> response) {
+ String contentType = response.getHeader("Content-Type");
+ return contentType != null && contentType.toLowerCase(Locale.ROOT).contains("application/json");
+ }
+
+ private MultiMap requestHeaders(JsonNode headers, String defaultContentType) {
+ MultiMap requestHeaders = MultiMap.caseInsensitiveMultiMap();
+ if (headers.isObject()) {
+ headers.fields().forEachRemaining(entry -> addHeaderValues(requestHeaders, entry.getKey(), entry.getValue()));
+ }
+ if (defaultContentType != null && headerValue(headers, "Content-Type") == null) {
+ requestHeaders.add("Content-Type", defaultContentType);
+ }
+ return requestHeaders;
+ }
+
+ private void addHeaderValues(MultiMap headers, String name, JsonNode value) {
+ if (value.isArray()) {
+ value.forEach(headerValue -> headers.add(name, headerValue.asText()));
+ } else if (!value.isNull()) {
+ headers.add(name, value.asText());
+ }
+ }
+
+ private HttpRequestPayload requestPayload(JsonNode requestBody, String requestBodyEncoding) {
+ if ("URL_ENCODED".equalsIgnoreCase(requestBodyEncoding)) {
+ // TODO: Implement Transform.RequestBodyEncoding URL_ENCODED with AWS-compatible array formats.
+ throw new FailStateException("States.TaskFailed", "URL-encoded request bodies are not supported yet");
+ } else if ("NONE".equalsIgnoreCase(requestBodyEncoding)) {
+ try {
+ if (requestBody.isMissingNode() || requestBody.isNull()) {
+ return new HttpRequestPayload(null, null, null);
+ }
+
+ return new HttpRequestPayload(
+ Buffer.buffer(objectMapper.writeValueAsString(requestBody)),
+ null,
+ "application/json");
+ } catch (Exception e) {
+ throw new FailStateException("States.TaskFailed",
+ "Failed to serialize HTTP request body to JSON: " + e.getMessage());
+ }
+ } else {
+ throw new FailStateException("States.TaskFailed",
+ "Unsupported body transformer: " + requestBodyEncoding);
+ }
+ }
+
+ private record HttpRequestPayload(Buffer body, MultiMap form, String contentType) {
+ }
+
+ private record HttpTaskResponse(
+ @JsonProperty("StatusCode") int statusCode,
+ @JsonProperty("StatusText") String statusText,
+ @JsonProperty("Headers") Map<String, List<String>> headers,
+ @JsonProperty("ResponseBody") JsonNode responseBody) {
+ }
+
+ private void validateHttpMethod(String method) {
+ if (method == null || method.isBlank()) {
+ throw new FailStateException("States.Runtime", "Method is required for HTTP task");
+ }
+
+ // TODO Uppercase methods to avoid user errros?
+ if (!HTTP_ALLOWED_METHODS.contains(method)) {
+ throw new FailStateException("States.Runtime", "Unsupported HTTP method for HTTP task: " + method);
+ }
+ }
+
+ private void validateConnectionArn(JsonNode input) {
+ String connectionArn = input.path("InvocationConfig").path("ConnectionArn").asText(null);
+ if (connectionArn == null || connectionArn.isBlank()) {
+ connectionArn = input.path("Authentication").path("ConnectionArn").asText(null);
+ }
+ if (connectionArn == null || connectionArn.isBlank()) {
+ throw new FailStateException("States.Runtime",
+ "ConnectionArn is required for HTTP task Authentication or InvocationConfig");
+ }
+ }
+
+ /**
+ * Stepfunction should reject certain headers as per <a href="https://docs.aws.amazon.com/step-functions/latest/dg/call-https-apis.html#connect-http-task-fields">docs</a>
+ */
+ private void validateHttpHeaders(JsonNode headers) {
+ if (headers.isMissingNode() || headers.isNull()) {
+ return;
+ }
+ if (!headers.isObject()) {
+ throw new FailStateException("States.Runtime", "Headers must be a JSON object for HTTP task");
+ }
+
+ Iterator<String> names = headers.fieldNames();
+ while (names.hasNext()) {
+ String name = names.next();
+ String normalized = name.toLowerCase(Locale.ROOT);
+ if (HTTP_FORBIDDEN_HEADERS.contains(normalized)
+ || normalized.startsWith("x-forwarded-")
+ || normalized.startsWith("x-amz-")
+ || normalized.startsWith("x-amzn-")) {
+ throw new FailStateException("States.Runtime",
+ "Header is not allowed in HTTP task definition: " + name);
+ }
+ }
+ }
+
+ private MultiMap queryParameters(JsonNode queryParameters) {
+ MultiMap params = MultiMap.caseInsensitiveMultiMap();
+ if (queryParameters.isMissingNode() || queryParameters.isNull()) {
+ return params;
+ }
+ if (!queryParameters.isObject()) {
+ throw new FailStateException("States.Runtime", "QueryParameters must be a JSON object for HTTP task");
+ }
+
+ queryParameters.properties().forEach(entry -> addQueryParameterValues(params, entry.getKey(), entry.getValue()));
+ return params;
+ }
+
+ private void addQueryParameterValues(MultiMap params, String name, JsonNode value) {
+ if (value.isArray()) {
+ value.forEach(queryValue -> {
+ if (!queryValue.isNull()) {
+ params.add(name, queryValue.asText());
+ }
+ });
+ } else if (!value.isNull()) {
+ params.add(name, value.asText());
+ }
+ }
+
+ private String headerValue(JsonNode headers, String name) {
+ if (!headers.isObject()) {
+ return null;
+ }
+
+ Iterator<Map.Entry<String, JsonNode>> fields = headers.fields();
+ while (fields.hasNext()) {
+ Map.Entry<String, JsonNode> entry = fields.next();
+ if (entry.getKey().equalsIgnoreCase(name)) {
+ return entry.getValue().isArray() && !entry.getValue().isEmpty()
+ ? entry.getValue().get(0).asText()
+ : entry.getValue().asText();
+ }
+ }
+ return null;
+ }
+
+
/**
* Evaluate a JSONPath-mode intrinsic function (States.*).
* Supports: States.StringToJson, States.JsonToString, States.Format,
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index efbfe8df00..d6b135c4d8 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -295,6 +295,7 @@ floci:
enabled: true
stepfunctions:
enabled: true
+ allow-plaintext-http: false
cloudformation:
enabled: true
acm:
diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorArrayParamsTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorArrayParamsTest.java
index ab5f2d940f..77d0a0bfea 100644
--- a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorArrayParamsTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorArrayParamsTest.java
@@ -19,7 +19,7 @@ class AslExecutorArrayParamsTest {
private AslExecutor newExecutor() {
return new AslExecutor(null, null, null, null, null, null, null, null,
- null, null, mapper, null, null);
+ null, null, mapper, null, null, null, null);
}
@Test
diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorCatchTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorCatchTest.java
index 9f326fb7c3..78f4cde8fd 100644
--- a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorCatchTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorCatchTest.java
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.stepfunctions;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
+import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.services.dynamodb.DynamoDbJsonHandler;
import io.github.hectorvent.floci.services.dynamodb.DynamoDbService;
import io.github.hectorvent.floci.services.lambda.LambdaExecutorService;
@@ -14,7 +15,10 @@ import io.github.hectorvent.floci.services.sqs.SqsJsonHandler;
import io.github.hectorvent.floci.services.stepfunctions.model.Execution;
import io.github.hectorvent.floci.services.stepfunctions.model.HistoryEvent;
import io.github.hectorvent.floci.services.stepfunctions.model.StateMachine;
+import io.quarkus.test.junit.QuarkusTest;
+import io.vertx.mutiny.core.Vertx;
import jakarta.enterprise.inject.Instance;
+import jakarta.inject.Inject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -32,6 +36,7 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+@QuarkusTest
class AslExecutorCatchTest {
private static final String REGION = "us-east-2";
@@ -48,6 +53,9 @@ class AslExecutorCatchTest {
private LambdaFunction cleanupFunction;
private AslExecutor executor;
+ @Inject
+ Vertx vertx;
+
@BeforeEach
void setUp() throws Exception {
lambdaExecutor = mock(LambdaExecutorService.class);
@@ -82,7 +90,7 @@ class AslExecutorCatchTest {
mock(io.github.hectorvent.floci.services.ecs.EcsJsonHandler.class),
objectMapper,
new JsonataEvaluator(objectMapper),
- mock(Instance.class));
+ mock(Instance.class), mock(EmulatorConfig.class), vertx);
}
@Test
diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorEcsRunTaskModeTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorEcsRunTaskModeTest.java
index 1469a0c4d9..83f4a6c400 100644
--- a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorEcsRunTaskModeTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorEcsRunTaskModeTest.java
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.stepfunctions;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
+import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.services.dynamodb.DynamoDbJsonHandler;
import io.github.hectorvent.floci.services.dynamodb.DynamoDbService;
import io.github.hectorvent.floci.services.ecs.EcsJsonHandler;
@@ -70,7 +71,9 @@ class AslExecutorEcsRunTaskModeTest {
ecsJsonHandler,
objectMapper,
new JsonataEvaluator(objectMapper),
- mock(Instance.class));
+ mock(Instance.class),
+ mock(EmulatorConfig.class),
+ null);
}
@Test
diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorHttpInvokeTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorHttpInvokeTest.java
new file mode 100644
index 0000000000..aa5e008090
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorHttpInvokeTest.java
@@ -0,0 +1,476 @@
+package io.github.hectorvent.floci.services.stepfunctions;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import io.github.hectorvent.floci.config.EmulatorConfig;
+import io.github.hectorvent.floci.services.dynamodb.DynamoDbJsonHandler;
+import io.github.hectorvent.floci.services.dynamodb.DynamoDbService;
+import io.github.hectorvent.floci.services.ecs.EcsJsonHandler;
+import io.github.hectorvent.floci.services.ecs.EcsService;
+import io.github.hectorvent.floci.services.lambda.LambdaExecutorService;
+import io.github.hectorvent.floci.services.lambda.LambdaFunctionStore;
+import io.github.hectorvent.floci.services.sqs.SqsJsonHandler;
+import io.github.hectorvent.floci.services.stepfunctions.model.Execution;
+import io.github.hectorvent.floci.services.stepfunctions.model.HistoryEvent;
+import io.github.hectorvent.floci.services.stepfunctions.model.StateMachine;
+import io.quarkus.logging.Log;
+import io.quarkus.test.junit.QuarkusTest;
+
+import io.smallrye.mutiny.Uni;
+import io.vertx.core.json.JsonObject;
+import io.vertx.mutiny.core.MultiMap;
+import io.vertx.mutiny.core.Vertx;
+import io.vertx.mutiny.core.buffer.Buffer;
+import io.vertx.mutiny.core.http.HttpServer;
+import io.vertx.mutiny.ext.web.Router;
+import io.vertx.mutiny.ext.web.RoutingContext;
+import jakarta.enterprise.inject.Instance;
+import jakarta.inject.Inject;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.startsWith;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+
+@QuarkusTest
+class AslExecutorHttpInvokeTest {
+
+ private static final String REGION = "us-west-2";
+ private static final String ACCOUNT = "000000000000";
+ private static final String CONNECTION_ARN =
+ "arn:aws:events:%s:%s:connection/test/11111111-1111-1111-1111-111111111111"
+ .formatted(REGION, ACCOUNT);
+
+ private final ObjectMapper objectMapper = new ObjectMapper();
+ private final List<RecordedRequest> receivedRequests = new CopyOnWriteArrayList<>();
+
+ private HttpServer server;
+ private String baseUrl;
+ private AslExecutor executor;
+
+ @Inject
+ Vertx vertx;
+
+ @Inject
+ EmulatorConfig emulatorConfig;
+
+ private record RecordedRequest(String method, String path, String query,
+ MultiMap headers, String body) {
+ String firstHeader(String name) {
+ return headers.entries().stream()
+ .filter(entry -> entry.getKey().equalsIgnoreCase(name))
+ .findFirst()
+ .map(entry -> entry.getValue().isEmpty() ? null : entry.getValue())
+ .orElse(null);
+ }
+
+ List<String> headerValues(String name) {
+ return headers.entries().stream()
+ .filter(entry -> entry.getKey().equalsIgnoreCase(name))
+ .map(Map.Entry::getValue)
+ .toList();
+ }
+ }
+
+ @BeforeEach
+ void setUp() {
+ server = vertx.createHttpServer();
+
+ var router = Router.router(vertx);
+ router.get("/json")
+ .handler(ctx -> {
+ ctx.request().body()
+ .onItem().invoke(body -> {
+ recordRequest(ctx, body.toString());
+ })
+ .onItem().call(() -> {
+ return ctx.response()
+ .putHeader("Content-Type", "application/json")
+ .end(Buffer.newInstance(JsonObject.of("ok", "true").toBuffer()));
+ })
+ .subscribe().with(_ -> {
+ });
+ });
+ router.post("/text")
+ .handler(ctx -> {
+ ctx.request().body()
+ .onItem().invoke(body -> {
+ recordRequest(ctx, body.toString());
+ })
+ .onItem().call(() -> {
+ return ctx.response()
+ .setStatusCode(201)
+ .putHeader("Content-Type", "text/plain")
+ .end();
+ })
+ .subscribe().with(_ -> {
+ });
+ });
+ router.get("/fail")
+ .handler(ctx -> {
+ ctx.request().body()
+ .onItem().invoke(body -> {
+ recordRequest(ctx, body.toString());
+ })
+ .onItem().call(() -> {
+ return ctx.response()
+ .setStatusCode(429)
+ .putHeader("Content-Type", "text/plain")
+ .end("slow down");
+ })
+ .subscribe().with(_ -> {
+ });
+ });
+ router.get("/jsonslow")
+ .handler(ctx -> {
+ Uni.createFrom().voidItem()
+ .onItem().delayIt().by(Duration.ofSeconds(2))
+ .onItem().transformToUni(ignore -> ctx.response()
+ .putHeader("Content-Type", "application/json")
+ .end(Buffer.newInstance(JsonObject.of("ok", "true").toBuffer())))
+ .subscribe().with(it -> {
+ });
+ });
+ router.get("/form")
+ .handler(ctx -> {
+ ctx.response()
+ .putHeader("Content-Type", "application/x-www-form-urlencoded")
+ .endAndForget("no_support");
+ });
+ router.errorHandler(500, ctx -> ctx.response()
+ .setStatusCode(500)
+ .putHeader("Content-Type", "application/json")
+ .endAndForget(Buffer.newInstance(JsonObject.of("ok", "false").toBuffer()))
+ );
+ server.requestHandler(router)
+ .listenAndAwait(0);
+ baseUrl = "http://127.0.0.1:" + server.actualPort();
+
+ executor = new AslExecutor(
+ mock(LambdaExecutorService.class),
+ mock(LambdaFunctionStore.class),
+ mock(DynamoDbService.class),
+ mock(DynamoDbJsonHandler.class),
+ mock(SqsJsonHandler.class),
+ mock(io.github.hectorvent.floci.services.cloudformation.CloudFormationQueryHandler.class),
+ mock(io.github.hectorvent.floci.services.ec2.Ec2Service.class),
+ mock(io.github.hectorvent.floci.services.s3.S3Service.class),
+ mock(EcsService.class),
+ mock(EcsJsonHandler.class),
+ objectMapper,
+ new JsonataEvaluator(objectMapper),
+ mock(Instance.class),
+ emulatorConfig,
+ vertx);
+ }
+
+ private void recordRequest(final RoutingContext ctx, String body) {
+ var request = ctx.request();
+ var requestRecord = new RecordedRequest(
+ request.method().name(),
+ request.uri(),
+ request.query(),
+ request.headers(),
+ body);
+ Log.info("sss"+ requestRecord);
+ receivedRequests.add(requestRecord);
+ }
+
+ @AfterEach
+ void tearDown() {
+ server.closeAndAwait();
+ }
+
+ @Test
+ void getWithHeadersAndQueryParametersReturnsStructuredResponse() throws Exception {
+ Execution execution = run("""
+ {
+ "StartAt": "CallHttp",
+ "States": {
+ "CallHttp": {
+ "Type": "Task",
+ "Resource": "arn:aws:states:::http:invoke",
+ "Parameters": {
+ "ApiEndpoint": "%s/json",
+ "Method": "GET",
+ "InvocationConfig": {
+ "ConnectionArn": "%s"
+ },
+ "Headers": {
+ "X-Custom": ["one", "two"]
+ },
+ "QueryParameters": {
+ "q": "hello",
+ "tag": ["blue", "green"],
+ "ignored": null
+ }
+ },
+ "End": true
+ }
+ }
+ }
+ """.formatted(baseUrl, CONNECTION_ARN), "{}");
+
+ assertEquals("SUCCEEDED", execution.getStatus());
+ JsonNode output = objectMapper.readTree(execution.getOutput());
+ assertEquals(200, output.path("StatusCode").asInt());
+ assertTrue(output.path("ResponseBody").path("ok").asBoolean());
+ assertTrue(output.path("Headers").has("Content-Type"));
+
+ RecordedRequest request = onlyRequest();
+ assertEquals("GET", request.method());
+ assertEquals("/json?q=hello&tag=blue&tag=green", request.path());
+ assertQueryContains(request.query(), "q=hello");
+ assertQueryContains(request.query(), "tag=blue");
+ assertQueryContains(request.query(), "tag=green");
+
+ assertFalse(request.query().contains("ignored"));
+ assertEquals(List.of("one", "two"), request.headerValues("X-Custom"));
+ }
+
+ @Test
+ void postJsonRequestBodyCanUseDynamicParametersAndReturnsTextResponseBody() throws Exception {
+ Execution execution = run("""
+ {
+ "StartAt": "CallHttp",
+ "States": {
+ "CallHttp": {
+ "Type": "Task",
+ "Resource": "arn:aws:states:::http:invoke",
+ "Parameters": {
+ "ApiEndpoint.$": "$.endpoint",
+ "Method.$": "$.method",
+ "Authentication": {
+ "ConnectionArn": "%s"
+ },
+ "RequestBody": {
+ "customerId.$": "$.customerId",
+ "active": true
+ }
+ },
+ "End": true
+ }
+ }
+ }
+ """.formatted(CONNECTION_ARN), """
+ {
+ "endpoint": "%s/text",
+ "method": "POST",
+ "customerId": "cust-123"
+ }
+ """.formatted(baseUrl));
+
+ assertEquals("SUCCEEDED", execution.getStatus());
+ JsonNode output = objectMapper.readTree(execution.getOutput());
+ assertEquals(201, output.path("StatusCode").asInt());
+
+ RecordedRequest request = onlyRequest();
+ assertEquals("POST", request.method());
+ assertEquals("application/json", request.firstHeader("Content-Type"));
+ JsonNode body = objectMapper.readTree(request.body());
+ assertEquals("cust-123", body.path("customerId").asText());
+ assertTrue(body.path("active").asBoolean());
+ }
+
+ @Test
+ void nonSuccessStatusFailsWithStatesHttpStatusCodeError() throws Exception {
+ Execution execution = run("""
+ {
+ "StartAt": "CallHttp",
+ "States": {
+ "CallHttp": {
+ "Type": "Task",
+ "Resource": "arn:aws:states:::http:invoke",
+ "Parameters": {
+ "ApiEndpoint": "%s/fail",
+ "Method": "GET",
+ "InvocationConfig": {
+ "ConnectionArn": "%s"
+ }
+ },
+ "End": true
+ }
+ }
+ }
+ """.formatted(baseUrl, CONNECTION_ARN), "{}");
+
+ assertEquals("FAILED", execution.getStatus());
+ assertEquals("States.Http.StatusCode.429", execution.getError());
+ assertEquals("slow down", execution.getCause());
+ assertEquals(1, receivedRequests.size());
+ }
+
+ @Test
+ void forbiddenHeadersFailBeforeSendingRequest() {
+ Execution execution = run("""
+ {
+ "StartAt": "CallHttp",
+ "States": {
+ "CallHttp": {
+ "Type": "Task",
+ "Resource": "arn:aws:states:::http:invoke",
+ "Parameters": {
+ "ApiEndpoint": "%s/json",
+ "Method": "GET",
+ "InvocationConfig": {
+ "ConnectionArn": "%s"
+ },
+ "Headers": {
+ "Authorization": "Bearer token"
+ }
+ },
+ "End": true
+ }
+ }
+ }
+ """.formatted(baseUrl, CONNECTION_ARN), "{}");
+
+ assertEquals("FAILED", execution.getStatus());
+ assertEquals("States.Runtime", execution.getError());
+ assertTrue(execution.getCause().contains("Authorization"));
+ assertEquals(0, receivedRequests.size());
+ }
+
+ @Test
+ void connectionArnIsRequiredBeforeSendingRequest() {
+ Execution execution = run("""
+ {
+ "StartAt": "CallHttp",
+ "States": {
+ "CallHttp": {
+ "Type": "Task",
+ "Resource": "arn:aws:states:::http:invoke",
+ "Parameters": {
+ "ApiEndpoint": "%s/json",
+ "Method": "GET"
+ },
+ "End": true
+ }
+ }
+ }
+ """.formatted(baseUrl), "{}");
+
+ assertEquals("FAILED", execution.getStatus());
+ assertEquals("States.Runtime", execution.getError());
+ assertTrue(execution.getCause().contains("ConnectionArn is required"));
+ assertEquals(0, receivedRequests.size());
+ }
+
+ @Test
+ void httpTimeoutFailsStepOnSlowApi() {
+ Execution execution = run("""
+ {
+ "StartAt": "CallHttp",
+ "States": {
+ "CallHttp": {
+ "Type": "Task",
+ "Resource": "arn:aws:states:::http:invoke",
+ "Parameters": {
+ "ApiEndpoint.$": "$.endpoint",
+ "Method.$": "$.method",
+ "TimeoutSeconds": 1,
+ "Authentication": {
+ "ConnectionArn": "%s"
+ }
+ },
+ "End": true
+ }
+ }
+ }
+ """.formatted(CONNECTION_ARN), """
+ {
+ "endpoint": "%s/jsonslow",
+ "method": "GET",
+ "customerId": "cust-123"
+ }
+ """.formatted(baseUrl));
+
+ assertEquals("FAILED", execution.getStatus());
+ assertEquals("States.Http.Socket", execution.getError());
+ assertThat(execution.getCause(), startsWith("The timeout period of 1000ms has been exceeded while executing GET /jsonslow for server"));
+ }
+
+ @Test
+ void formTransformationIsUnsupported() {
+ Execution execution = run("""
+ {
+ "StartAt": "CallHttp",
+ "States": {
+ "CallHttp": {
+ "Type": "Task",
+ "Resource": "arn:aws:states:::http:invoke",
+ "Parameters": {
+ "ApiEndpoint.$": "$.endpoint",
+ "Method.$": "$.method",
+ "TimeoutSeconds": 1,
+ "Authentication": {
+ "ConnectionArn": "%s"
+ },
+ "Transform": {
+ "RequestBodyEncoding": "URL_ENCODED",
+ "RequestEncodingOptions": {
+ "ArrayFormat": "COMMAS"
+ }
+ }
+ },
+ "End": true
+ }
+ }
+ }
+ """.formatted(CONNECTION_ARN), """
+ {
+ "endpoint": "%s/form",
+ "method": "GET",
+ "customerId": "cust-123"
+ }
+ """.formatted(baseUrl));
+
+ assertEquals("FAILED", execution.getStatus());
+ assertEquals("States.TaskFailed", execution.getError());
+ assertThat(execution.getCause(), startsWith("URL-encoded request bodies are not supported yet"));
+ }
+
+ private Execution run(String definition, String input) {
+ StateMachine stateMachine = new StateMachine();
+ stateMachine.setName("http-invoke-test");
+ stateMachine.setStateMachineArn("arn:aws:states:%s:%s:stateMachine:http-invoke-test"
+ .formatted(REGION, ACCOUNT));
+ stateMachine.setRoleArn("arn:aws:iam::%s:role/test-role".formatted(ACCOUNT));
+ stateMachine.setDefinition(definition);
+
+ Execution execution = new Execution();
+ execution.setName("http-invoke-test-execution");
+ execution.setExecutionArn("arn:aws:states:%s:%s:execution:http-invoke-test:http-invoke-test-execution"
+ .formatted(REGION, ACCOUNT));
+ execution.setStateMachineArn(stateMachine.getStateMachineArn());
+ execution.setInput(input);
+
+ List<HistoryEvent> history = new ArrayList<>();
+ executor.executeSync(stateMachine, execution, history, (updated, events) -> {
+ });
+ return execution;
+ }
+
+ private RecordedRequest onlyRequest() {
+ assertEquals(1, receivedRequests.size(), "expected exactly one backend request");
+ return receivedRequests.getFirst();
+ }
+
+ // TODO Can use Hamcrest
+ private void assertQueryContains(String query, String expected) {
+ assertNotNull(query);
+ assertTrue(query.contains(expected), "expected query to contain " + expected + " but was " + query);
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorIntrinsicContextTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorIntrinsicContextTest.java
index d1579fb8da..306aa80ce0 100644
--- a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorIntrinsicContextTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorIntrinsicContextTest.java
@@ -23,7 +23,7 @@ class AslExecutorIntrinsicContextTest {
/** Only ObjectMapper is exercised by the path/intrinsic resolution methods under test. */
private AslExecutor newExecutor() {
return new AslExecutor(null, null, null, null, null, null, null, null,
- null, null, mapper, null, null);
+ null, null, mapper, null, null, null, null);
}
@Test
diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorPathIntrinsicsTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorPathIntrinsicsTest.java
index 776967e535..41f8a69c7e 100644
--- a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorPathIntrinsicsTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorPathIntrinsicsTest.java
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.stepfunctions;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
+import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.services.dynamodb.DynamoDbJsonHandler;
import io.github.hectorvent.floci.services.dynamodb.DynamoDbService;
import io.github.hectorvent.floci.services.lambda.LambdaExecutorService;
@@ -43,7 +44,9 @@ class AslExecutorPathIntrinsicsTest {
mock(io.github.hectorvent.floci.services.ecs.EcsJsonHandler.class),
mapper,
new JsonataEvaluator(mapper),
- mock(Instance.class));
+ mock(Instance.class),
+ mock(EmulatorConfig.class),
+ null);
}
@Test
diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml
index 5e0df5b4b8..6558cad914 100644
--- a/src/test/resources/application.yml
+++ b/src/test/resources/application.yml
@@ -178,6 +178,7 @@ floci:
enabled: true
stepfunctions:
enabled: true
+ allow-plaintext-http: true
cloudformation:
enabled: true
acm: