mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(lambda): preserve owner account for execution logs (#3787)
* fix(lambda): preserve owner account for execution logs * test(lambda): cover account-aware log streaming
This commit is contained in:
1 parent
008aa9ba35
commit
5cb1453f4e
7 files changed
+91
-23
No files matched your search
+11
-1
@@ -141,7 +141,12 @@ public class ContainerLogStreamer {
|
||||
*/
|
||||
public ResultCallback.Adapter<Frame> execLogCallback(String logGroup, String logStream,
|
||||
String region, String logPrefix) {
|
||||
return frameCallback(null, logGroup, logStream, region, logPrefix);
|
||||
return execLogCallbackForAccount(null, logGroup, logStream, region, logPrefix);
|
||||
}
|
||||
|
||||
public ResultCallback.Adapter<Frame> execLogCallbackForAccount(
|
||||
String accountId, String logGroup, String logStream, String region, String logPrefix) {
|
||||
return frameCallback(accountId, logGroup, logStream, region, logPrefix);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -209,6 +214,11 @@ public class ContainerLogStreamer {
|
||||
forwardToCloudWatchLogs(null, logGroup, logStream, region, line);
|
||||
}
|
||||
|
||||
public void streamToCloudWatchLogsForAccount(
|
||||
String accountId, String logGroup, String logStream, String region, String line) {
|
||||
forwardToCloudWatchLogs(accountId, logGroup, logStream, region, line);
|
||||
}
|
||||
|
||||
private void forwardToCloudWatchLogs(
|
||||
String accountId, String logGroup, String logStream, String region, String line) {
|
||||
try {
|
||||
|
||||
+11
-7
@@ -414,8 +414,10 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
// Extensions can log as soon as they start, which is before the container's own log stream
|
||||
// is attached below. Create the group/stream up front so those early lines are not dropped
|
||||
// by CloudWatch; the call is idempotent, so attach() repeating it is harmless.
|
||||
LogDestination logDestination = new LogDestination(cwLogGroup, cwLogStream, lambdaRegion);
|
||||
logStreamer.ensureLogGroupAndStream(cwLogGroup, cwLogStream, lambdaRegion);
|
||||
LogDestination logDestination = new LogDestination(
|
||||
lambdaAccountId, cwLogGroup, cwLogStream, lambdaRegion);
|
||||
logStreamer.ensureLogGroupAndStreamForAccount(
|
||||
lambdaAccountId, cwLogGroup, cwLogStream, lambdaRegion);
|
||||
|
||||
// Real AWS's runtime interface client discovers and launches every binary under
|
||||
// /opt/extensions/ as a sibling process to the main entrypoint before the runtime is
|
||||
@@ -440,8 +442,9 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
LambdaExecutionRoleCredentials.sessionAccountId(fn));
|
||||
|
||||
// Attach log streaming
|
||||
Closeable logHandle = logStreamer.attach(
|
||||
containerId, cwLogGroup, cwLogStream, lambdaRegion, "lambda:" + fn.getFunctionName());
|
||||
Closeable logHandle = logStreamer.attachForAccount(
|
||||
lambdaAccountId, containerId, cwLogGroup, cwLogStream,
|
||||
lambdaRegion, "lambda:" + fn.getFunctionName());
|
||||
handle.setLogStream(logHandle);
|
||||
|
||||
return handle;
|
||||
@@ -1304,7 +1307,7 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
* (or other extensions) from running.
|
||||
*/
|
||||
/** Where a container's output is sent: the CloudWatch log group/stream and its region. */
|
||||
private record LogDestination(String logGroup, String logStream, String region) { }
|
||||
private record LogDestination(String accountId, String logGroup, String logStream, String region) { }
|
||||
|
||||
/**
|
||||
* Arms an async watch (via Docker's own wait-for-exit API, not polling) that notices when
|
||||
@@ -1360,8 +1363,9 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
// the container log and an observability extension's output would vanish entirely.
|
||||
// Draining is also required in its own right — an unread exec pipe fills up and
|
||||
// stalls the extension process.
|
||||
dockerClient.execStartCmd(execId).exec(logStreamer.execLogCallback(
|
||||
logDestination.logGroup(), logDestination.logStream(), logDestination.region(),
|
||||
dockerClient.execStartCmd(execId).exec(logStreamer.execLogCallbackForAccount(
|
||||
logDestination.accountId(), logDestination.logGroup(), logDestination.logStream(),
|
||||
logDestination.region(),
|
||||
"lambda:" + functionName + ":" + name));
|
||||
LOG.infov("Launched extension {0} for function {1} (container {2})",
|
||||
name, functionName, containerId);
|
||||
|
||||
+3
-2
@@ -164,6 +164,7 @@ public class KubernetesPodLauncher implements LambdaRuntimeLauncher {
|
||||
var shortId = UUID.randomUUID().toString().replace("-", "").substring(0, 8);
|
||||
podName = LambdaPodSpecFactory.podName(fn.getFunctionName(), shortId);
|
||||
var region = AwsArnUtils.regionOrDefault(fn.getFunctionArn(), config.defaultRegion());
|
||||
String accountId = AwsArnUtils.accountOrDefault(fn.getFunctionArn(), config.defaultAccountId());
|
||||
var cwLogGroup = "/aws/lambda/" + fn.getFunctionName();
|
||||
var cwLogStream = logStreamer.logStreamName(shortId);
|
||||
|
||||
@@ -222,8 +223,8 @@ public class KubernetesPodLauncher implements LambdaRuntimeLauncher {
|
||||
// so a failure here (e.g. a missing pods/log RBAC verb) must not tear down a
|
||||
// healthy execution environment.
|
||||
try {
|
||||
var logHandle = logStreamer.attach(namespace, podName, cwLogGroup, cwLogStream,
|
||||
region, "lambda:" + fn.getFunctionName());
|
||||
var logHandle = logStreamer.attachForAccount(accountId, namespace, podName,
|
||||
cwLogGroup, cwLogStream, region, "lambda:" + fn.getFunctionName());
|
||||
handle.setLogStream(logHandle);
|
||||
} catch (Exception logFailure) {
|
||||
LOG.warnv("Log streaming for pod {0} could not start; the environment still "
|
||||
|
||||
+8
-2
@@ -39,7 +39,12 @@ public class KubernetesPodLogStreamer {
|
||||
|
||||
public Closeable attach(String namespace, String podName, String logGroup,
|
||||
String logStream, String region, String logPrefix) {
|
||||
cloudWatchWriter.ensureLogGroupAndStream(logGroup, logStream, region);
|
||||
return attachForAccount(null, namespace, podName, logGroup, logStream, region, logPrefix);
|
||||
}
|
||||
|
||||
public Closeable attachForAccount(String accountId, String namespace, String podName,
|
||||
String logGroup, String logStream, String region, String logPrefix) {
|
||||
cloudWatchWriter.ensureLogGroupAndStreamForAccount(accountId, logGroup, logStream, region);
|
||||
|
||||
InputStream logStreamBody = client.openPodLogStream(namespace, podName, "runtime");
|
||||
|
||||
@@ -55,7 +60,8 @@ public class KubernetesPodLogStreamer {
|
||||
continue;
|
||||
}
|
||||
LOG.infov("[{0}] {1}", logPrefix, trimmed);
|
||||
cloudWatchWriter.streamToCloudWatchLogs(logGroup, logStream, region, trimmed);
|
||||
cloudWatchWriter.streamToCloudWatchLogsForAccount(
|
||||
accountId, logGroup, logStream, region, trimmed);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// Normal on pod deletion: the log stream just ends.
|
||||
|
||||
+42
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.core.common.docker;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.async.ResultCallback;
|
||||
import com.github.dockerjava.api.command.LogContainerCmd;
|
||||
import com.github.dockerjava.api.model.Frame;
|
||||
import com.github.dockerjava.api.model.StreamType;
|
||||
@@ -22,6 +23,8 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyList;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.RETURNS_SELF;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
@@ -34,6 +37,45 @@ class ContainerLogStreamerTest {
|
||||
return s.getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
@Test
|
||||
void ensureLogGroupAndStreamUsesOwningAccount() {
|
||||
CloudWatchLogsService cloudWatchLogsService = mock(CloudWatchLogsService.class);
|
||||
ContainerLogStreamer streamer = new ContainerLogStreamer(null, cloudWatchLogsService);
|
||||
|
||||
streamer.ensureLogGroupAndStreamForAccount(
|
||||
"555555555555", "/aws/lambda/example", "2026/09/17/[$LATEST]stream", "us-east-1");
|
||||
|
||||
verify(cloudWatchLogsService).createLogGroupForAccount(
|
||||
"555555555555", "/aws/lambda/example", null, null, "us-east-1");
|
||||
verify(cloudWatchLogsService).createLogStreamForAccount(
|
||||
"555555555555", "/aws/lambda/example", "2026/09/17/[$LATEST]stream", "us-east-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void streamToCloudWatchLogsForAccountUsesOwningAccount() {
|
||||
CloudWatchLogsService cloudWatchLogsService = mock(CloudWatchLogsService.class);
|
||||
ContainerLogStreamer streamer = new ContainerLogStreamer(null, cloudWatchLogsService);
|
||||
|
||||
streamer.streamToCloudWatchLogsForAccount(
|
||||
"555555555555", "/aws/lambda/example", "stream", "us-east-1", "function output");
|
||||
|
||||
verify(cloudWatchLogsService).putLogEventsForAccount(
|
||||
eq("555555555555"), eq("/aws/lambda/example"), eq("stream"), anyList(), eq("us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void execLogCallbackForAccountForwardsExtensionOutputToOwningAccount() {
|
||||
CloudWatchLogsService cloudWatchLogsService = mock(CloudWatchLogsService.class);
|
||||
ContainerLogStreamer streamer = new ContainerLogStreamer(null, cloudWatchLogsService);
|
||||
ResultCallback.Adapter<Frame> callback = streamer.execLogCallbackForAccount(
|
||||
"555555555555", "/aws/lambda/example", "stream", "us-east-1", "lambda:example:extension");
|
||||
|
||||
callback.onNext(new Frame(StreamType.STDOUT, utf8("extension output\n")));
|
||||
|
||||
verify(cloudWatchLogsService).putLogEventsForAccount(
|
||||
eq("555555555555"), eq("/aws/lambda/example"), eq("stream"), anyList(), eq("us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void reassemblesSingleLineSplitAcrossFrames() {
|
||||
// A ~1.5 KB stdout line that Docker delivers as multiple frames must arrive
|
||||
|
||||
+10
-9
@@ -65,7 +65,6 @@ import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@@ -628,8 +627,9 @@ class ContainerLauncherTest {
|
||||
List<String> env = captureRealContainerSpec().env();
|
||||
assertTrue(env.contains("AWS_DEFAULT_REGION=eu-west-2"));
|
||||
assertTrue(env.contains("AWS_REGION=eu-west-2"));
|
||||
verify(logStreamer).attach(
|
||||
eq("container-123"), any(), any(), eq("eu-west-2"), eq("lambda:region-arn-fn"));
|
||||
verify(logStreamer).attachForAccount(
|
||||
eq("000000000000"), eq("container-123"), any(), any(),
|
||||
eq("eu-west-2"), eq("lambda:region-arn-fn"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -1620,16 +1620,17 @@ class ContainerLauncherTest {
|
||||
LambdaFunction fn = new LambdaFunction();
|
||||
fn.setFunctionName("observability-fn");
|
||||
fn.setPackageType("Image");
|
||||
fn.setFunctionArn("arn:aws:lambda:us-east-1:555555555555:function:observability-fn");
|
||||
fn.setImageUri("123456789012.dkr.ecr.us-east-1.amazonaws.com/repo:latest");
|
||||
|
||||
launcherWithRealStreamer.launch(fn);
|
||||
|
||||
// The frame became a CloudWatch log event in the function's own log group. Forwarding goes
|
||||
// through the account-aware overload with a null account id: exec streams have no owning
|
||||
// account of their own, so they land in the default account's copy of the log group.
|
||||
// The frame became a CloudWatch log event in the function owner's log group even though
|
||||
// the launcher is not running inside an HTTP request scope.
|
||||
ArgumentCaptor<List<Map<String, Object>>> events = ArgumentCaptor.forClass(List.class);
|
||||
verify(cloudWatchLogs, atLeastOnce()).putLogEventsForAccount(
|
||||
isNull(), eq("/aws/lambda/observability-fn"), anyString(), events.capture(), anyString());
|
||||
eq("555555555555"), eq("/aws/lambda/observability-fn"), anyString(),
|
||||
events.capture(), anyString());
|
||||
assertTrue(events.getAllValues().stream()
|
||||
.flatMap(List::stream)
|
||||
.anyMatch(e -> "extension started on :8080".equals(e.get("message"))),
|
||||
@@ -1653,8 +1654,8 @@ class ContainerLauncherTest {
|
||||
launcher.launch(fn);
|
||||
|
||||
InOrder inOrder = inOrder(logStreamer, dockerClient);
|
||||
inOrder.verify(logStreamer).ensureLogGroupAndStream(
|
||||
eq("/aws/lambda/ordering-fn"), anyString(), anyString());
|
||||
inOrder.verify(logStreamer).ensureLogGroupAndStreamForAccount(
|
||||
eq("000000000000"), eq("/aws/lambda/ordering-fn"), anyString(), anyString());
|
||||
inOrder.verify(dockerClient, atLeastOnce()).execCreateCmd("container-123");
|
||||
}
|
||||
|
||||
|
||||
+6
-2
@@ -119,8 +119,8 @@ class KubernetesPodLauncherTest {
|
||||
|
||||
layerService = mock(LambdaLayerService.class);
|
||||
logStreamer = mock(KubernetesPodLogStreamer.class);
|
||||
lenient().when(logStreamer.attach(anyString(), anyString(), anyString(), anyString(),
|
||||
anyString(), anyString())).thenReturn(() -> { });
|
||||
lenient().when(logStreamer.attachForAccount(anyString(), anyString(), anyString(), anyString(),
|
||||
anyString(), anyString(), anyString())).thenReturn(() -> { });
|
||||
lenient().when(logStreamer.logStreamName(anyString())).thenReturn("2026/07/25/[$LATEST]test");
|
||||
s3Service = mock(S3Service.class);
|
||||
|
||||
@@ -237,6 +237,10 @@ class KubernetesPodLauncherTest {
|
||||
.contains(tuple("AWS_LAMBDA_RUNTIME_API", "10.0.0.5:9200"));
|
||||
assertThat(pod.getSpec().getInitContainers().getFirst().getCommand().get(2))
|
||||
.contains("http://10.0.0.5:4566/awslambda-us-east-1-tasks/snapshots/000000000000/my-fn");
|
||||
verify(logStreamer).attachForAccount(
|
||||
eq("000000000000"), eq("default"), eq(handle.getContainerId()),
|
||||
eq("/aws/lambda/my-fn"), eq("2026/07/25/[$LATEST]test"),
|
||||
eq("us-east-1"), eq("lambda:my-fn"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user