Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(eks): stop mirroring cluster audit logs to the Floci console (#4589)

Simone Primarosab2d668d9ae48
diff --git a/docs/services/eks.md b/docs/services/eks.md
index d8599f924e..1ee01c158a 100644
--- a/docs/services/eks.md
+++ b/docs/services/eks.md
@@ -229,7 +229,7 @@ EKS clusters support configuring KMS envelope encryption for secrets and control
- **Backfill**: Existing persisted clusters created before this feature was introduced are automatically backfilled on startup with default disabled logging.
- **CloudWatch Logs delivery**:
- When the `api` log type is enabled on a cluster, Floci creates the CloudWatch Logs log group `/aws/eks/<cluster-name>/cluster` and streams control plane container output to a log stream named `kube-apiserver-<hash>`, where `<hash>` is the first 32 characters of the container ID.
- - When the `audit` log type is enabled on a cluster, Floci configures k3s with the Amazon EKS control plane audit policy, writes audit logs to `/var/log/audit.log` inside the container, and streams audit records to a log stream named `kube-apiserver-audit-<hash>`.
+ - When the `audit` log type is enabled on a cluster, Floci configures k3s with the Amazon EKS control plane audit policy, writes audit logs to `/var/log/audit.log` inside the container, and streams audit records to a log stream named `kube-apiserver-audit-<hash>`. Audit records are available in CloudWatch Logs and are not echoed to the Floci console.
- When both `api` and `audit` are enabled, both streams are created and populated under `/aws/eks/<cluster-name>/cluster`. When neither is enabled, no log group or streams are created.
- **Component streams deviation**: AWS EKS provisions separate log streams for each component (`kube-apiserver-*`, `kube-apiserver-audit-*`, `kube-controller-manager-*`, `kube-scheduler-*`, `authenticator-*`). Because Floci runs clusters on k3s, which embeds the Kubernetes API server, controller manager, and scheduler within a single process, control plane container logs are delivered to the single `kube-apiserver-<hash>` stream when `api` is enabled, and API server audit logs are delivered to `kube-apiserver-audit-<hash>` when `audit` is enabled. Other control plane log types (`authenticator`, `controllerManager`, `scheduler`) do not provision separate streams.
- **Authenticator logs**: Authenticator webhook authentication events are logged directly by Floci.
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLogStreamer.java b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLogStreamer.java
index 64d1e2f303..c0df117dc3 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLogStreamer.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLogStreamer.java
@@ -35,7 +35,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Consumer;
/**
- * Streams Docker container logs to both the Floci console logger and CloudWatch Logs.
+ * Streams Docker container logs to CloudWatch Logs and, by default, the Floci console logger.
* Consolidates the log streaming pattern used across container managers.
*/
@ApplicationScoped
@@ -155,17 +155,28 @@ public class ContainerLogStreamer {
public ResultCallback.Adapter<Frame> execLogCallbackForAccount(
String accountId, String logGroup, String logStream, String region, String logPrefix) {
- return frameCallback(accountId, logGroup, logStream, region, logPrefix);
+ return execLogCallbackForAccount(accountId, logGroup, logStream, region, logPrefix, true);
}
/**
- * Shared frame handling for both the container log stream and exec streams.
+ * Returns an exec callback that always forwards to CloudWatch Logs.
+ *
+ * @param logToConsole whether to also write each frame to the Floci console at INFO level
+ */
+ public ResultCallback.Adapter<Frame> execLogCallbackForAccount(
+ String accountId, String logGroup, String logStream, String region, String logPrefix,
+ boolean logToConsole) {
+ return frameCallback(accountId, logGroup, logStream, region, logPrefix, logToConsole);
+ }
+
+ /**
+ * Handles frames from Docker exec streams.
*
* @param accountId account that owns the destination log stream, or {@code null} for the
* default account
*/
private ResultCallback.Adapter<Frame> frameCallback(String accountId, String logGroup, String logStream,
- String region, String logPrefix) {
+ String region, String logPrefix, boolean logToConsole) {
return new ResultCallback.Adapter<>() {
@Override
public void onNext(Frame frame) {
@@ -174,7 +185,9 @@ public class ContainerLogStreamer {
}
String line = new String(frame.getPayload(), StandardCharsets.UTF_8).stripTrailing();
if (!line.isEmpty()) {
- LOG.infov("[{0}] {1}", logPrefix, line);
+ if (logToConsole) {
+ LOG.infov("[{0}] {1}", logPrefix, line);
+ }
forwardToCloudWatchLogs(accountId, logGroup, logStream, region, line);
}
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java
index 23a543cc0d..ce01c31e38 100644
--- a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java
+++ b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java
@@ -1040,7 +1040,7 @@ public class EksClusterManager
return dockerClient
.execStartCmd(execCreate.getId())
.exec(logStreamer.execLogCallbackForAccount(
- accountId, logGroup, logStream, region, "eks-audit:" + clusterName));
+ accountId, logGroup, logStream, region, "eks-audit:" + clusterName, false));
}
Closeable getLogHandle(Cluster cluster) {
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerLogStreamerTest.java b/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerLogStreamerTest.java
index 7369a61263..6c0fc1157c 100644
--- a/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerLogStreamerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerLogStreamerTest.java
@@ -6,7 +6,10 @@ import com.github.dockerjava.api.command.LogContainerCmd;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.StreamType;
import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsService;
+import io.github.hectorvent.floci.testutil.LogCapture;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import java.io.Closeable;
@@ -17,6 +20,7 @@ import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
+import java.util.logging.LogRecord;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -24,6 +28,7 @@ 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.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.RETURNS_SELF;
import static org.mockito.Mockito.mock;
@@ -76,6 +81,53 @@ class ContainerLogStreamerTest {
eq("555555555555"), eq("/aws/lambda/example"), eq("stream"), anyList(), eq("us-east-1"));
}
+ @Test
+ void attachedStreamStillReachesCloudWatchAndConsole() {
+ DockerClient dockerClient = mock(DockerClient.class);
+ LogContainerCmd command = mock(LogContainerCmd.class, RETURNS_SELF);
+ when(dockerClient.logContainerCmd("container-1")).thenReturn(command);
+ CloudWatchLogsService cloudWatchLogsService = mock(CloudWatchLogsService.class);
+ ContainerLogStreamer streamer = new ContainerLogStreamer(dockerClient, cloudWatchLogsService);
+
+ streamer.attachForAccount("555555555555", "container-1", "/aws/eks/cluster/cluster",
+ "stream", "us-east-1", "eks:cluster");
+ ArgumentCaptor<ContainerLogStreamer.LogReassemblyCallback> callback =
+ ArgumentCaptor.forClass(ContainerLogStreamer.LogReassemblyCallback.class);
+ verify(command).exec(callback.capture());
+
+ List<LogRecord> records = LogCapture.capture(ContainerLogStreamer.class, () ->
+ callback.getValue().onNext(new Frame(StreamType.STDOUT, utf8("container output\n"))));
+
+ assertEquals(1, records.size());
+ verify(cloudWatchLogsService).putLogEventsForAccount(
+ eq("555555555555"), eq("/aws/eks/cluster/cluster"), eq("stream"),
+ argThat(events ->
+ events.size() == 1 && "container output".equals(events.getFirst().get("message"))),
+ eq("us-east-1"));
+ }
+
+ @ParameterizedTest
+ @ValueSource(booleans = {true, false})
+ void execStreamAlwaysReachesCloudWatchAndConsoleIsOptional(boolean logToConsole) {
+ CloudWatchLogsService cloudWatchLogsService = mock(CloudWatchLogsService.class);
+ ContainerLogStreamer streamer = new ContainerLogStreamer(null, cloudWatchLogsService);
+ ResultCallback.Adapter<Frame> callback = logToConsole
+ ? streamer.execLogCallbackForAccount("555555555555", "/aws/eks/cluster/cluster",
+ "stream", "us-east-1", "eks-audit:cluster")
+ : streamer.execLogCallbackForAccount("555555555555", "/aws/eks/cluster/cluster",
+ "stream", "us-east-1", "eks-audit:cluster", false);
+
+ List<LogRecord> records = LogCapture.capture(ContainerLogStreamer.class, () ->
+ callback.onNext(new Frame(StreamType.STDOUT, utf8("audit event\n"))));
+
+ assertEquals(logToConsole ? 1 : 0, records.size());
+ verify(cloudWatchLogsService).putLogEventsForAccount(
+ eq("555555555555"), eq("/aws/eks/cluster/cluster"), eq("stream"),
+ argThat(events ->
+ events.size() == 1 && "audit event".equals(events.getFirst().get("message"))),
+ eq("us-east-1"));
+ }
+
@Test
void reassemblesSingleLineSplitAcrossFrames() {
// A ~1.5 KB stdout line that Docker delivers as multiple frames must arrive
diff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java
index af8387b3db..797b7e64e4 100644
--- a/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java
@@ -2655,6 +2655,10 @@ class EksClusterManagerTest {
manager.startCluster(cluster);
assertEquals(mockAuditHandle, manager.getLogHandle(cluster));
+ verify(logStreamer).execLogCallbackForAccount(
+ "000000000000", "/aws/eks/audit-cluster/cluster",
+ "kube-apiserver-audit-container-id-1234567890123456789",
+ "us-east-1", "eks-audit:audit-cluster", false);
manager.stopCluster(cluster);
verify(lifecycleManager).stopAndRemove("container-id-123456789012345678901234567890", mockAuditHandle);