fix(lambda): pass ClientContext to synchronous invocations (#5014)

Emit Lambda-Runtime-Client-Context from the decoded X-Amz-Client-Context header.

* Fix #4968

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-10-03 15:16:00 -05:00
1 parent ae0a591bb1
commit b112c62949
9 files changed
+198 -12

No files matched your search

@@ -1,16 +1,17 @@
package io.github.hectorvent.floci.services.lambda;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.services.lambda.model.EventSourceMapping;
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
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.LambdaAlias;
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
@@ -29,12 +30,14 @@ import jakarta.ws.rs.core.UriBuilder;
import jakarta.ws.rs.core.UriInfo;
import org.jboss.logging.Logger;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Base64;
import java.util.HexFormat;
import java.util.List;
import java.util.Map;
@@ -262,7 +265,10 @@ public class LambdaController {
.build();
}
InvokeResult result = lambdaService.invoke(region, functionName, qualifier, payload, type);
String clientContext = type == InvocationType.RequestResponse
? decodeClientContext(headers.getHeaderString("X-Amz-Client-Context")) : null;
InvokeResult result = lambdaService.invoke(region, functionName, qualifier, payload, type, clientContext);
if (type != InvocationType.Event
&& result.getPayload() != null
@@ -292,6 +298,20 @@ public class LambdaController {
return builder.build();
}
private String decodeClientContext(String encoded) {
if (encoded == null || encoded.isEmpty()) {
return null;
}
try {
byte[] decoded = Base64.getDecoder().decode(encoded);
return objectMapper.writer().with(JsonGenerator.Feature.ESCAPE_NON_ASCII)
.writeValueAsString(objectMapper.readTree(decoded));
} catch (IOException | IllegalArgumentException e) {
throw new AwsException("InvalidRequestContentException",
"Could not parse the client context: it must be base64-encoded JSON.", 400);
}
}
// ──────────────────────────── Event Source Mappings ────────────────────────────
@POST
@@ -162,6 +162,11 @@ public class LambdaExecutorService implements Resettable {
InvokeResult invoke(LambdaFunction fn, byte[] payload, InvocationType type, int chainDepth,
String invokedQualifier) {
return invoke(fn, payload, type, chainDepth, invokedQualifier, null);
}
InvokeResult invoke(LambdaFunction fn, byte[] payload, InvocationType type, int chainDepth,
String invokedQualifier, String clientContext) {
String requestId = UUID.randomUUID().toString();
if (type == InvocationType.DryRun) {
@@ -215,7 +220,7 @@ public class LambdaExecutorService implements Resettable {
}
try {
return executeSync(fn, payload, requestId);
return executeSync(fn, payload, requestId, clientContext);
} finally {
permit.close();
}
@@ -271,7 +276,7 @@ public class LambdaExecutorService implements Resettable {
}
InvokeResult result;
try (permit) {
result = executeSync(event.fn(), event.payload(), event.requestId());
result = executeSync(event.fn(), event.payload(), event.requestId(), null);
} catch (RuntimeException e) {
LOG.warnv("Error in async Lambda execution for {0}: {1}", event.fn().getFunctionName(), e.getMessage());
// Stops retrying, as upstream did: routes the previous attempt's result, or UnknownError when
@@ -312,7 +317,8 @@ public class LambdaExecutorService implements Resettable {
event.invokedQualifier());
}
private InvokeResult executeSync(LambdaFunction fn, byte[] payload, String requestId) {
private InvokeResult executeSync(LambdaFunction fn, byte[] payload, String requestId,
String clientContext) {
ContainerHandle handle;
try {
handle = warmPool.acquire(fn);
@@ -327,6 +333,7 @@ public class LambdaExecutorService implements Resettable {
PendingInvocation invocation = new PendingInvocation(
requestId, payload, deadlineMs, fn.getFunctionArn(),
new CompletableFuture<>());
invocation.setClientContext(clientContext);
handle.getRuntimeApiServer().enqueue(invocation);
@@ -1165,6 +1165,15 @@ public class LambdaService implements ResourceProvider {
*/
public InvokeResult invoke(String region, String functionName, String queryQualifier, byte[] payload,
InvocationType type) {
return invoke(region, functionName, queryQualifier, payload, type, null);
}
/**
* Invokes a function, handing {@code clientContext} (the decoded {@code X-Amz-Client-Context} JSON) to a
* synchronous invocation. AWS passes it to synchronous invocations only.
*/
public InvokeResult invoke(String region, String functionName, String queryQualifier, byte[] payload,
InvocationType type, String clientContext) {
validateInvokeQualifier(queryQualifier);
LambdaArnUtils.ResolvedFunctionRef ref = resolveWithRegion(region, functionName, queryQualifier);
String name = ref.name();
@@ -1178,7 +1187,7 @@ public class LambdaService implements ResourceProvider {
}
reportCustomResourceLiveness(payload);
InvokeResult result = executorService.invoke(fn, payload, type,
LambdaInvocationChain.currentDepth(), qualifier);
LambdaInvocationChain.currentDepth(), qualifier, clientContext);
result.setExecutedVersion(fn.getVersion());
return result;
}
@@ -12,6 +12,7 @@ public class PendingInvocation {
private final String functionArn;
private final CompletableFuture<InvokeResult> resultFuture;
private final CompletableFuture<Void> dispatchedFuture = new CompletableFuture<>();
private volatile String clientContext;
public PendingInvocation(String requestId, byte[] payload, long deadlineMs,
String functionArn, CompletableFuture<InvokeResult> resultFuture) {
@@ -36,6 +37,8 @@ public class PendingInvocation {
public byte[] getPayload() { return payload; }
public long getDeadlineMs() { return deadlineMs; }
public String getFunctionArn() { return functionArn; }
public String getClientContext() { return clientContext; }
public void setClientContext(String clientContext) { this.clientContext = clientContext; }
public CompletableFuture<InvokeResult> getResultFuture() { return resultFuture; }
public CompletableFuture<Void> getDispatchedFuture() { return dispatchedFuture; }
}
@@ -8,6 +8,7 @@ import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
@@ -947,13 +948,16 @@ public class RuntimeApiServer {
// failed Future if the socket dies mid-flush. Funnel both into one handler.
Future<Void> write;
try {
write = ctx.response()
HttpServerResponse response = ctx.response()
.setStatusCode(200)
.putHeader("Content-Type", "application/json")
.putHeader("Lambda-Runtime-Aws-Request-Id", invocation.getRequestId())
.putHeader("Lambda-Runtime-Invoked-Function-Arn", invocation.getFunctionArn())
.putHeader("Lambda-Runtime-Deadline-Ms", String.valueOf(invocation.getDeadlineMs()))
.end(body);
.putHeader("Lambda-Runtime-Deadline-Ms", String.valueOf(invocation.getDeadlineMs()));
if (invocation.getClientContext() != null) {
response.putHeader("Lambda-Runtime-Client-Context", invocation.getClientContext());
}
write = response.end(body);
} catch (IllegalStateException alreadyEnded) {
write = Future.failedFuture(alreadyEnded);
}