mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -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
+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