mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
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:
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
|
||||
|
||||
+10
-3
@@ -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; }
|
||||
}
|
||||
+7
-3
@@ -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);
|
||||
}
|
||||
|
||||
+19
@@ -177,6 +177,25 @@ class LambdaExecutorServiceTest {
|
||||
assertTrue(payload.contains("1 seconds"), "payload should contain timeout value");
|
||||
}
|
||||
|
||||
@Test
|
||||
void syncInvoke_clientContext_isPlacedOnTheEnqueuedInvocation() {
|
||||
RuntimeApiServer rtas = mock(RuntimeApiServer.class);
|
||||
ContainerHandle handle = new ContainerHandle("cid-ctx", "test-fn", rtas, ContainerState.WARM);
|
||||
when(warmPool.acquire(any())).thenReturn(handle);
|
||||
AtomicReference<String> seen = new AtomicReference<>();
|
||||
doAnswer(inv -> {
|
||||
PendingInvocation pi = inv.getArgument(0);
|
||||
seen.set(pi.getClientContext());
|
||||
pi.getResultFuture().complete(new InvokeResult(200, null, "{}".getBytes(), null, "req-ctx"));
|
||||
return pi.getResultFuture();
|
||||
}).when(rtas).enqueue(any(PendingInvocation.class));
|
||||
|
||||
String clientContext = "{\"custom\":{\"traceparent\":\"00-abc-def-01\"}}";
|
||||
executor.invoke(fn, "{}".getBytes(), InvocationType.RequestResponse, 0, null, clientContext);
|
||||
|
||||
assertEquals(clientContext, seen.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void successfulInvocation_releasesHandle_doesNotDestroy() {
|
||||
RuntimeApiServer rtas = mock(RuntimeApiServer.class);
|
||||
|
||||
@@ -135,6 +135,20 @@ class LambdaIntegrationTest {
|
||||
.statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void syncInvoke_malformedClientContext_returns400() {
|
||||
given()
|
||||
.header("X-Amz-Client-Context", "not-base64-json!")
|
||||
.contentType("application/json")
|
||||
.body("{}")
|
||||
.when()
|
||||
.post(BASE_PATH + "/functions/hello-world/invocations")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidRequestContentException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void createFunctionMissingRole_returns400() {
|
||||
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
|
||||
import io.quarkus.test.InjectMock;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@QuarkusTest
|
||||
class LambdaInvokeClientContextIntegrationTest {
|
||||
|
||||
private static final String CONTEXT_JSON = "{\"custom\":{\"traceparent\":\"00-abc-def-01\"}}";
|
||||
|
||||
@InjectMock
|
||||
LambdaService lambdaService;
|
||||
|
||||
|
||||
private String encodedContext() {
|
||||
String pretty = "{ \"custom\": { \"traceparent\": \"00-abc-def-01\" } }";
|
||||
return Base64.getEncoder().encodeToString(pretty.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private void stubInvoke() {
|
||||
when(lambdaService.invoke(any(), any(), any(), any(), any(), any()))
|
||||
.thenReturn(new InvokeResult(200, null, "{}".getBytes(), null, "req"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void requestResponse_validClientContext_isDecodedAndPassedToTheService() {
|
||||
stubInvoke();
|
||||
|
||||
given()
|
||||
.header("X-Amz-Client-Context", encodedContext())
|
||||
.contentType("application/json")
|
||||
.body("{}")
|
||||
.when().post("/2015-03-31/functions/ctx-fn/invocations")
|
||||
.then().statusCode(200);
|
||||
|
||||
verify(lambdaService).invoke(any(), eq("ctx-fn"), any(), any(),
|
||||
eq(InvocationType.RequestResponse), eq(CONTEXT_JSON));
|
||||
}
|
||||
|
||||
@Test
|
||||
void event_clientContext_isNotPassedToTheService() {
|
||||
stubInvoke();
|
||||
|
||||
given()
|
||||
.header("X-Amz-Invocation-Type", "Event")
|
||||
.header("X-Amz-Client-Context", encodedContext())
|
||||
.contentType("application/json")
|
||||
.body("{}")
|
||||
.when().post("/2015-03-31/functions/ctx-fn-event/invocations")
|
||||
.then().statusCode(200);
|
||||
|
||||
verify(lambdaService).invoke(any(), eq("ctx-fn-event"), any(), any(),
|
||||
eq(InvocationType.Event), isNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
void requestResponse_nonAsciiClientContext_isEscapedForTheHeader() {
|
||||
stubInvoke();
|
||||
String raw = "{\"custom\":{\"name\":\"café\"}}";
|
||||
|
||||
given()
|
||||
.header("X-Amz-Client-Context",
|
||||
Base64.getEncoder().encodeToString(raw.getBytes(StandardCharsets.UTF_8)))
|
||||
.contentType("application/json")
|
||||
.body("{}")
|
||||
.when().post("/2015-03-31/functions/ctx-fn-utf/invocations")
|
||||
.then().statusCode(200);
|
||||
|
||||
verify(lambdaService).invoke(any(), eq("ctx-fn-utf"), any(), any(),
|
||||
eq(InvocationType.RequestResponse), eq("{\"custom\":{\"name\":\"caf\\u00E9\"}}"));
|
||||
}
|
||||
}
|
||||
+25
@@ -109,6 +109,31 @@ class RuntimeApiServerTest {
|
||||
assertTrue(response.body().contains("key"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(10)
|
||||
void nextEndpoint_emitsClientContextHeaderOnlyWhenPresent() throws Exception {
|
||||
String clientContext = "{\"custom\":{\"traceparent\":\"00-abc-def-01\"}}";
|
||||
PendingInvocation withContext = new PendingInvocation(
|
||||
"req-ctx", "{}".getBytes(), System.currentTimeMillis() + 60_000,
|
||||
"arn:aws:lambda:us-east-1:000000000000:function:test",
|
||||
new CompletableFuture<>());
|
||||
withContext.setClientContext(clientContext);
|
||||
server.enqueue(withContext);
|
||||
|
||||
HttpRequest next = HttpRequest.newBuilder()
|
||||
.uri(URI.create("http://localhost:" + port + "/2018-06-01/runtime/invocation/next"))
|
||||
.GET().build();
|
||||
HttpResponse<String> first = httpClient.send(next, HttpResponse.BodyHandlers.ofString());
|
||||
assertEquals(clientContext, first.headers().firstValue("Lambda-Runtime-Client-Context").orElse(null));
|
||||
|
||||
server.enqueue(new PendingInvocation(
|
||||
"req-noctx", "{}".getBytes(), System.currentTimeMillis() + 60_000,
|
||||
"arn:aws:lambda:us-east-1:000000000000:function:test",
|
||||
new CompletableFuture<>()));
|
||||
HttpResponse<String> second = httpClient.send(next, HttpResponse.BodyHandlers.ofString());
|
||||
assertTrue(second.headers().firstValue("Lambda-Runtime-Client-Context").isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(10)
|
||||
void nextEndpoint_startsDeadlineWhenInvocationIsDispatched() throws Exception {
|
||||
|
||||
Reference in new issue
Block a user