feat(sfn): apply the EventBridge connection to HTTP Tasks (#7479)

* feat(sfn): apply the EventBridge connection to HTTP Tasks

An HTTP Task now resolves the connection named by InvocationConfig or
Authentication. Its authorization, headers, query parameters and body
parameters are merged over the task's, and the connection's value wins on
a duplicate key, as the AWS HTTP Task guide describes. The body is merged
before Transform encodes it. A string body with connection body
parameters fails with States.Runtime, and an unknown connection fails
with Events.ConnectionResource.ResourceNotFound.

The connection logic is the API destination's: EventBridgeInvoker exposes
it as connectionHttpParameters, so both callers share one implementation,
OAuth client credentials included.

* fix(sfn): keep case-distinct query parameters and bound OAuth by the HTTP Task timeout

A connection query parameter now replaces only a task parameter of the exact same name, since query names are case-sensitive. The connection's OAuth token exchange now spends the task's TimeoutSeconds, so the API request gets only the time left.

* chore: retrigger CI
This commit is contained in:
Ronald Li 🛠️ authored and GitHub committed 2026-11-04 05:58:02 -04:00
1 parent e105843426
commit 3b68495018
5 files changed
+298 -42

No files matched your search

@@ -612,14 +612,14 @@ public class EventBridgeInvoker {
HttpParameters httpParameters = target.getHttpParameters();
JsonNode authNode = readAuthParameters(connection);
JsonNode invocationHttp = authNode.path("InvocationHttpParameters");
ConnectionHttpParameters connectionParameters = invocationHttpParameters(authNode);
// The connection's parameters are applied last so a target cannot override what the connection configures
Map<String, String> queryParams = new LinkedHashMap<>();
if (httpParameters != null && httpParameters.getQueryStringParameters() != null) {
queryParams.putAll(httpParameters.getQueryStringParameters());
}
queryParams.putAll(readKeyValues(invocationHttp.path("QueryStringParameters")));
queryParams.putAll(connectionParameters.queryStringParameters());
String resolvedUrl = appendQueryParameters(
substitutePathParameters(rawUrl,
httpParameters != null ? httpParameters.getPathParameterValues() : null),
@@ -631,7 +631,7 @@ public class EventBridgeInvoker {
if (httpParameters != null && httpParameters.getHeaderParameters() != null) {
httpParameters.getHeaderParameters().forEach((name, value) -> putHeader(headers, name, value));
}
readKeyValues(invocationHttp.path("HeaderParameters")).forEach((name, value) -> putHeader(headers, name, value));
connectionParameters.headers().forEach((name, value) -> putHeader(headers, name, value));
applyConnectionAuth(connection, authNode, headers);
String method = destination.getHttpMethod() != null ? destination.getHttpMethod().toUpperCase() : "POST";
@@ -640,7 +640,7 @@ public class EventBridgeInvoker {
headers.put("Content-Type", "application/json; charset=utf-8");
}
String body = hasBody
? mergeBodyParameters(payload, readKeyValues(invocationHttp.path("BodyParameters")))
? mergeBodyParameters(payload, connectionParameters.bodyParameters())
: null;
HttpRequest request;
@@ -825,6 +825,29 @@ public class EventBridgeInvoker {
}
}
/**
* The parameters a connection adds to a request it authorizes, with its authorization among the headers.
* Callers apply them over their own: on a conflict, the connection's value wins.
*/
public ConnectionHttpParameters connectionHttpParameters(String connectionArn, String region) {
Connection connection = eventBridgeService.findConnectionByArn(connectionArn, region);
JsonNode authNode = readAuthParameters(connection);
ConnectionHttpParameters parameters = invocationHttpParameters(authNode);
applyConnectionAuth(connection, authNode, parameters.headers());
return parameters;
}
private static ConnectionHttpParameters invocationHttpParameters(JsonNode authNode) {
JsonNode invocationHttp = authNode.path("InvocationHttpParameters");
return new ConnectionHttpParameters(readKeyValues(invocationHttp.path("HeaderParameters")),
readKeyValues(invocationHttp.path("QueryStringParameters")),
readKeyValues(invocationHttp.path("BodyParameters")));
}
public record ConnectionHttpParameters(Map<String, String> headers, Map<String, String> queryStringParameters,
Map<String, String> bodyParameters) {
}
private static Map<String, String> readKeyValues(JsonNode params) {
Map<String, String> result = new LinkedHashMap<>();
for (JsonNode param : params) {
@@ -43,6 +43,8 @@ import io.github.hectorvent.floci.services.ecs.model.LaunchType;
import io.github.hectorvent.floci.services.ecs.model.NetworkConfiguration;
import io.github.hectorvent.floci.services.ecs.model.TaskDefinition;
import io.github.hectorvent.floci.services.eventbridge.EventBridgeHandler;
import io.github.hectorvent.floci.services.eventbridge.EventBridgeInvoker;
import io.github.hectorvent.floci.services.eventbridge.EventBridgeInvoker.ConnectionHttpParameters;
import io.github.hectorvent.floci.services.lambda.LambdaArnUtils;
import io.github.hectorvent.floci.services.lambda.LambdaExecutorService;
import io.github.hectorvent.floci.services.lambda.LambdaFunctionStore;
@@ -267,6 +269,7 @@ public class AslExecutor {
private final EcsService ecsService;
private final EcsJsonHandler ecsJsonHandler;
private final EventBridgeHandler eventBridgeHandler;
private final EventBridgeInvoker eventBridgeInvoker;
private final SchedulerService schedulerService;
private final SchedulerController schedulerController;
private final RdsDataService rdsDataService;
@@ -317,7 +320,7 @@ public class AslExecutor {
ServiceRegistry serviceRegistry, NestedTaskCredentials nestedTaskCredentials,
LambdaTaskAuthorizer lambdaTaskAuthorizer, ApiGatewayTaskInvoker apiGatewayTaskInvoker,
EmrTaskInvoker emrTaskInvoker, EmrServerlessTaskInvoker emrServerlessTaskInvoker,
SdkManagementTaskInvoker sdkManagementTaskInvoker) {
SdkManagementTaskInvoker sdkManagementTaskInvoker, EventBridgeInvoker eventBridgeInvoker) {
this(lambdaExecutor, targetResolver, dynamoDb, dynamoDbJsonHandler,
sqsJsonHandler, snsJsonHandler, cloudFormationHandler,
ec2Service, s3Service, ecsService, ecsJsonHandler,
@@ -325,7 +328,7 @@ public class AslExecutor {
objectMapper, jsonataEvaluator, sfnService, config, vertx, customResourceLiveness,
Clock.systemUTC(), TimeUnit.NANOSECONDS::sleep, null, redshiftDataHandler, serviceRegistry,
nestedTaskCredentials, lambdaTaskAuthorizer, null, null, apiGatewayTaskInvoker, emrTaskInvoker,
emrServerlessTaskInvoker, sdkManagementTaskInvoker, null);
emrServerlessTaskInvoker, sdkManagementTaskInvoker, null, eventBridgeInvoker);
}
AslExecutor(LambdaExecutorService lambdaExecutor, LambdaTargetResolver targetResolver,
@@ -345,7 +348,7 @@ public class AslExecutor {
Duration expressMaxExecutionTimeOverride, LongSupplier nanoTimeOverride,
ApiGatewayTaskInvoker apiGatewayTaskInvoker, EmrTaskInvoker emrTaskInvoker,
EmrServerlessTaskInvoker emrServerlessTaskInvoker, SdkManagementTaskInvoker sdkManagementTaskInvoker,
NestedWaiter nestedWaiter) {
NestedWaiter nestedWaiter, EventBridgeInvoker eventBridgeInvoker) {
this.apiGatewayTaskInvoker = apiGatewayTaskInvoker;
this.emrTaskInvoker = emrTaskInvoker;
this.emrServerlessTaskInvoker = emrServerlessTaskInvoker;
@@ -367,6 +370,7 @@ public class AslExecutor {
this.ecsService = ecsService;
this.ecsJsonHandler = ecsJsonHandler;
this.eventBridgeHandler = eventBridgeHandler;
this.eventBridgeInvoker = eventBridgeInvoker;
this.schedulerService = schedulerService;
this.schedulerController = schedulerController;
this.rdsDataService = rdsDataService;
@@ -414,7 +418,7 @@ public class AslExecutor {
eventBridgeHandler, schedulerService, schedulerController, rdsDataService,
objectMapper, jsonataEvaluator, sfnService, config, vertx, customResourceLiveness,
Clock.systemUTC(), TimeUnit.NANOSECONDS::sleep, null, null, null, null, null, null, null, null, null,
null, null, null);
null, null, null, null);
}
// Kept for tests that do not exercise the Redshift Data API integration.
@@ -436,7 +440,7 @@ public class AslExecutor {
eventBridgeHandler, schedulerService, schedulerController, rdsDataService,
objectMapper, jsonataEvaluator, sfnService, config, vertx, customResourceLiveness,
clock, sleeper, maxWaitSecondsOverride, null, null, null, null, null, null, null, null, null, null,
null);
null, null);
}
AslExecutor(LambdaExecutorService lambdaExecutor, LambdaFunctionStore functionStore,
@@ -5115,12 +5119,10 @@ public class AslExecutor {
}
/**
* 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.
* Implements the Step Functions HTTP Task request flow. The EventBridge connection's
* authorization, headers, query parameters and body parameters are merged over the
* task's, the connection's value winning on a conflict, as
* <a href="https://docs.aws.amazon.com/step-functions/latest/dg/call-https-apis.html#http-task-data-merge">documented</a>.
*
* 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.
@@ -5148,17 +5150,27 @@ public class AslExecutor {
}
validateHttpMethod(method);
validateConnectionArn(input);
String connectionArn = connectionArn(input);
validateHttpHeaders(headers);
// The connection's OAuth token exchange spends the same TimeoutSeconds as the request.
long deadlineNanos = nanoTime.getAsLong() + TimeUnit.MILLISECONDS.toNanos(timeoutMillis);
ConnectionHttpParameters connection = connectionHttpParameters(connectionArn, region);
long remainingMillis = TimeUnit.NANOSECONDS.toMillis(deadlineNanos - nanoTime.getAsLong());
if (remainingMillis <= 0) {
throw new FailStateException("States.Http.Socket", "The timeout period of " + timeoutMillis
+ "ms has been exceeded while authorizing with the connection");
}
HttpRequestPayload requestPayload = requestPayload(requestBody, requestBodyEncoding,
HttpRequestPayload requestPayload = requestPayload(
mergeConnectionBody(requestBody, connection.bodyParameters()), requestBodyEncoding,
input.path("Transform").path("RequestEncodingOptions"));
MultiMap requestHeaders = requestHeaders(headers, requestPayload.contentType());
MultiMap requestQueryParameters = queryParameters(queryParameters);
connection.headers().forEach(requestHeaders::set);
MultiMap requestQueryParameters = queryParameters(queryParameters, connection.queryStringParameters());
try {
HttpRequest<Buffer> request = webClient.requestAbs(HttpMethod.valueOf(method), rawUri)
.timeout(timeoutMillis)
.timeout(remainingMillis)
.putHeaders(requestHeaders)
.putHeader("User-Agent", "Amazon|StepFunctions|HttpInvoke|" + region);
request.queryParams().addAll(requestQueryParameters);
@@ -5323,7 +5335,7 @@ public class AslExecutor {
}
}
private void validateConnectionArn(JsonNode input) {
private String connectionArn(JsonNode input) {
String connectionArn = input.path("InvocationConfig").path("ConnectionArn").asText(null);
if (connectionArn == null || connectionArn.isBlank()) {
connectionArn = input.path("Authentication").path("ConnectionArn").asText(null);
@@ -5332,6 +5344,36 @@ public class AslExecutor {
throw new FailStateException("States.Runtime",
"ConnectionArn is required for HTTP task Authentication or InvocationConfig");
}
return connectionArn;
}
private ConnectionHttpParameters connectionHttpParameters(String connectionArn, String region) {
try {
return eventBridgeInvoker.connectionHttpParameters(connectionArn, region);
} catch (AwsException e) {
String error = "ResourceNotFoundException".equals(e.getErrorCode())
? "Events.ConnectionResource.ResourceNotFound"
: "Events.ConnectionResource.InternalError";
throw new FailStateException(error, e.getMessage());
}
}
// A string body cannot take the connection's body parameters; AWS fails the task rather than drop them
private JsonNode mergeConnectionBody(JsonNode requestBody, Map<String, String> bodyParameters) {
if (bodyParameters.isEmpty()) {
return requestBody;
}
ObjectNode merged;
if (requestBody.isMissingNode() || requestBody.isNull()) {
merged = objectMapper.createObjectNode();
} else if (requestBody instanceof ObjectNode objectBody) {
merged = objectBody.deepCopy();
} else {
throw new FailStateException("States.Runtime",
"The RequestBody cannot be merged with the connection's body parameters: it is not a JSON object");
}
bodyParameters.forEach(merged::put);
return merged;
}
/**
@@ -5359,16 +5401,21 @@ public class AslExecutor {
}
}
private MultiMap queryParameters(JsonNode queryParameters) {
/**
* The task's query parameters with the connection's merged over them. Query names are
* case-sensitive, so a connection parameter replaces only a task parameter of the same name.
*/
private MultiMap queryParameters(JsonNode queryParameters, Map<String, String> connectionParameters) {
MultiMap params = MultiMap.caseInsensitiveMultiMap();
if (queryParameters.isMissingNode() || queryParameters.isNull()) {
return params;
if (!queryParameters.isMissingNode() && !queryParameters.isNull()) {
if (!queryParameters.isObject()) {
throw new FailStateException("States.Runtime", "QueryParameters must be a JSON object for HTTP task");
}
queryParameters.properties().stream()
.filter(entry -> !connectionParameters.containsKey(entry.getKey()))
.forEach(entry -> addQueryParameterValues(params, entry.getKey(), entry.getValue()));
}
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()));
connectionParameters.forEach(params::add);
return params;
}