Light, fluffy, and always free - The AWS Local Emulator alternative
feat(ecs): inject the AWS SDK baseline env into launched task containers (#1697)
Alex Banna291752c36e4e
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/docker/LaunchedContainerAwsEnv.java b/src/main/java/io/github/hectorvent/floci/core/common/docker/LaunchedContainerAwsEnv.javanew file mode 100644index 0000000000..9d56bf8c5f--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/core/common/docker/LaunchedContainerAwsEnv.java@@ -0,0 +1,67 @@+package io.github.hectorvent.floci.core.common.docker;++import jakarta.enterprise.context.ApplicationScoped;+import jakarta.inject.Inject;++import java.net.URI;+import java.util.ArrayList;+import java.util.List;+import java.util.Optional;++/**+ * Builds the baseline AWS SDK environment for a container Floci launches, so the workload+ * inside it can reach the emulator with working credentials — the same variables regardless of+ * whether the container is a Lambda function or an ECS task.+ *+ * <p>Emulator-style, matching how a local AWS emulator satisfies the SDK credential-provider+ * chain: the SDK is pointed at Floci via {@code AWS_ENDPOINT_URL} and given placeholder+ * credentials (Floci does not verify request signatures). This mirrors how AWS itself supplies+ * credentials to a launched workload — Lambda via its execution role, an ECS task via its task+ * role — so that in either case the container starts with usable credentials rather than an+ * empty provider chain ({@code Could not load credentials from any providers}).+ */+@ApplicationScoped+public class LaunchedContainerAwsEnv {++ private final ContainerReachableEndpoint reachableEndpoint;++ @Inject+ public LaunchedContainerAwsEnv(ContainerReachableEndpoint reachableEndpoint) {+ this.reachableEndpoint = reachableEndpoint;+ }++ /**+ * The baseline {@code "KEY=value"} AWS SDK environment entries for a launched container:+ * region, credentials, and the Floci endpoint the SDK should target.+ *+ * @param region the AWS region the container should use+ * @param awsConfigMountDir in-container directory of a mounted AWS config/credentials+ * directory (as in {@code ~/.aws}); when present the SDK discovers+ * credentials from there. Empty = inject placeholder credentials.+ */+ public List<String> sdkBaselineEnv(String region, Optional<String> awsConfigMountDir) {+ List<String> env = new ArrayList<>();+ env.add("AWS_DEFAULT_REGION=" + region);+ env.add("AWS_REGION=" + region);+ if (awsConfigMountDir.isPresent() && !awsConfigMountDir.get().isBlank()) {+ // ~/.aws is mounted — don't inject credentials, let the SDK discover them.+ // Set explicit file paths so discovery works regardless of container HOME.+ String dir = awsConfigMountDir.get();+ env.add("AWS_SHARED_CREDENTIALS_FILE=" + dir + "/credentials");+ env.add("AWS_CONFIG_FILE=" + dir + "/config");+ } else {+ // Use Floci's own env vars, falling back to placeholder credentials.+ String ak = System.getenv("AWS_ACCESS_KEY_ID");+ String sk = System.getenv("AWS_SECRET_ACCESS_KEY");+ String st = System.getenv("AWS_SESSION_TOKEN");+ env.add("AWS_ACCESS_KEY_ID=" + (ak != null ? ak : "test"));+ env.add("AWS_SECRET_ACCESS_KEY=" + (sk != null ? sk : "test"));+ env.add("AWS_SESSION_TOKEN=" + (st != null ? st : "test"));+ }+ String flociEndpoint = reachableEndpoint.baseUrl();+ env.add("FLOCI_HOSTNAME=" + URI.create(flociEndpoint).getHost());+ env.add("FLOCI_ENDPOINT=" + flociEndpoint);+ env.add("AWS_ENDPOINT_URL=" + flociEndpoint);+ return env;+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManager.javaindex 8818ffc944..71308bfee3 100644--- a/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManager.java@@ -9,6 +9,7 @@ import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.Cimport io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;import io.github.hectorvent.floci.core.common.docker.ContainerSpec;import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;+import io.github.hectorvent.floci.core.common.docker.LaunchedContainerAwsEnv;import io.github.hectorvent.floci.services.ecs.model.Container;import io.github.hectorvent.floci.services.ecs.model.ContainerDefinition;import io.github.hectorvent.floci.services.ecs.model.ContainerOverride;@@ -32,6 +33,7 @@ import java.util.ArrayList;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;+import java.util.Optional;/*** Manages Docker container lifecycle for ECS tasks.@@ -48,6 +50,7 @@ public class EcsContainerManager {private final ContainerDetector containerDetector;private final EmulatorConfig config;private final RegionResolver regionResolver;+ private final LaunchedContainerAwsEnv awsEnv;@Injectpublic EcsContainerManager(ContainerBuilder containerBuilder,@@ -55,13 +58,15 @@ public class EcsContainerManager {ContainerLogStreamer logStreamer,ContainerDetector containerDetector,EmulatorConfig config,- RegionResolver regionResolver) {+ RegionResolver regionResolver,+ LaunchedContainerAwsEnv awsEnv) {this.containerBuilder = containerBuilder;this.lifecycleManager = lifecycleManager;this.logStreamer = logStreamer;this.containerDetector = containerDetector;this.config = config;this.regionResolver = regionResolver;+ this.awsEnv = awsEnv;}/**@@ -112,8 +117,14 @@ public class EcsContainerManager {// Build container specContainerBuilder.Builder specBuilder = containerBuilder.newContainer(def.getImage()).withName(containerName)- .withEnv(buildEnvVars(def, override))+ .withEnv(buildEnvVars(def, override, region)).withDockerNetwork(config.services().ecs().dockerNetwork())+ // Resolve Floci's endpoint from inside the task container the same way Lambda+ // containers do: host.docker.internal on Linux, plus Floci's embedded DNS so the+ // reachable AWS_ENDPOINT_URL hostname resolves to Floci instead of the container's+ // own loopback.+ .withHostDockerInternalOnLinux()+ .withEmbeddedDns().withLogRotation();// Add memory limit if specified@@ -328,9 +339,17 @@ public class EcsContainerManager {}}- private List<String> buildEnvVars(ContainerDefinition def, ContainerOverride override) {- // Task-def environment first, then override environment (override wins on key conflict).+ private List<String> buildEnvVars(ContainerDefinition def, ContainerOverride override, String region) {+ // AWS SDK baseline (endpoint + region + credentials) so the task can reach the emulator,+ // then the task-def environment, then the override environment — the task def and override+ // win on key conflict, so an explicit task-def value is never clobbered.Map<String, String> envMap = new LinkedHashMap<>();+ for (String kv : awsEnv.sdkBaselineEnv(region, Optional.empty())) {+ int eq = kv.indexOf('=');+ if (eq > 0) {+ envMap.put(kv.substring(0, eq), kv.substring(eq + 1));+ }+ }if (def.getEnvironment() != null) {for (var kv : def.getEnvironment()) {envMap.put(kv.name(), kv.value());diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.javaindex 667aa20d0f..1f4e1cc99d 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java@@ -3,12 +3,12 @@ package io.github.hectorvent.floci.services.lambda.launcher;import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.AwsArnUtils;import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;-import io.github.hectorvent.floci.core.common.docker.ContainerReachableEndpoint;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;import io.github.hectorvent.floci.core.common.docker.ContainerSpec;import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;+import io.github.hectorvent.floci.core.common.docker.LaunchedContainerAwsEnv;import io.github.hectorvent.floci.services.ecr.registry.EcrRegistryManager;import io.github.hectorvent.floci.services.lambda.LambdaLayerService;import io.github.hectorvent.floci.services.lambda.model.ContainerState;@@ -78,7 +78,7 @@ public class ContainerLauncher {private final EmulatorConfig config;private final EcrRegistryManager ecrRegistryManager;private final LambdaLayerService layerService;- private final ContainerReachableEndpoint reachableEndpoint;+ private final LaunchedContainerAwsEnv awsEnv;/** Matches an AWS-shaped ECR image URI: {@code <account>.dkr.ecr.<region>.amazonaws.com/<repo>[:tag]}. */private static final java.util.regex.Pattern AWS_ECR_URI =@@ -94,7 +94,7 @@ public class ContainerLauncher {EmulatorConfig config,EcrRegistryManager ecrRegistryManager,LambdaLayerService layerService,- ContainerReachableEndpoint reachableEndpoint) {+ LaunchedContainerAwsEnv awsEnv) {this.containerBuilder = containerBuilder;this.lifecycleManager = lifecycleManager;this.logStreamer = logStreamer;@@ -104,7 +104,7 @@ public class ContainerLauncher {this.config = config;this.ecrRegistryManager = ecrRegistryManager;this.layerService = layerService;- this.reachableEndpoint = reachableEndpoint;+ this.awsEnv = awsEnv;}/**@@ -178,11 +178,6 @@ public class ContainerLauncher {String cwLogStream = LOG_STREAM_DATE_FMT.format(LocalDate.now()) + "/[$LATEST]" + shortId;String lambdaRegion = extractRegionFromArn(fn.getFunctionArn(), config.defaultRegion());- // Floci endpoint reachable from inside the container (shared with the CloudFormation- // custom-resource provisioner, which uses the same address for ResponseURL callbacks).- String flociEndpoint = reachableEndpoint.baseUrl();- String flociHostname = java.net.URI.create(flociEndpoint).getHost();-// When TLS is on, the container must trust Floci's self-signed cert so HTTPS callbacks// to Floci succeed (e.g. a CDK custom resource's cfn-response, which hardcodes https://).// Short-circuit when TLS is off so cert-path/storage config isn't read needlessly.@@ -202,27 +197,13 @@ public class ContainerLauncher {if (fn.getHandler() != null && !fn.getHandler().isBlank()) {env.add("_HANDLER=" + fn.getHandler());}- env.add("AWS_DEFAULT_REGION=" + lambdaRegion);- env.add("AWS_REGION=" + lambdaRegion);+ // Region, credentials and the Floci endpoint the SDK should target — the same baseline+ // Floci gives every launched container. When the host ~/.aws is mounted (awsConfigPath)+ // the SDK discovers credentials from /opt/aws-config instead of the placeholders.Optional<String> awsConfigPath = config.services().lambda().awsConfigPath().filter(s -> !s.isBlank());- if (awsConfigPath.isPresent()) {- // ~/.aws will be mounted — don't inject credentials, let SDK discover them.- // Set explicit file paths so discovery works regardless of container HOME.- env.add("AWS_SHARED_CREDENTIALS_FILE=/opt/aws-config/credentials");- env.add("AWS_CONFIG_FILE=/opt/aws-config/config");- } else {- // Use Floci's own env vars, fallback to test/test/test- String ak = System.getenv("AWS_ACCESS_KEY_ID");- String sk = System.getenv("AWS_SECRET_ACCESS_KEY");- String st = System.getenv("AWS_SESSION_TOKEN");- env.add("AWS_ACCESS_KEY_ID=" + (ak != null ? ak : "test"));- env.add("AWS_SECRET_ACCESS_KEY=" + (sk != null ? sk : "test"));- env.add("AWS_SESSION_TOKEN=" + (st != null ? st : "test"));- }- env.add("FLOCI_HOSTNAME=" + flociHostname);- env.add("FLOCI_ENDPOINT=" + flociEndpoint);- env.add("AWS_ENDPOINT_URL=" + flociEndpoint);+ env.addAll(awsEnv.sdkBaselineEnv(lambdaRegion,+ awsConfigPath.isPresent() ? Optional.of("/opt/aws-config") : Optional.empty()));env.addAll(flociCaEnv(flociCaCert));if (fn.getEnvironment() != null) {fn.getEnvironment().forEach((k, v) -> env.add(k + "=" + v));diff --git a/src/test/java/io/github/hectorvent/floci/core/common/docker/LaunchedContainerAwsEnvTest.java b/src/test/java/io/github/hectorvent/floci/core/common/docker/LaunchedContainerAwsEnvTest.javanew file mode 100644index 0000000000..1e9edd5894--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/core/common/docker/LaunchedContainerAwsEnvTest.java@@ -0,0 +1,95 @@+package io.github.hectorvent.floci.core.common.docker;++import org.junit.jupiter.api.Test;++import java.util.List;+import java.util.Optional;++import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertTrue;+import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.when;++/**+ * Unit tests for {@link LaunchedContainerAwsEnv#sdkBaselineEnv}: the AWS SDK baseline+ * (region, credentials and the Floci endpoint) injected into every container Floci launches.+ * The Floci endpoint is stubbed via {@link ContainerReachableEndpoint} so the test does not+ * depend on host networking.+ */+class LaunchedContainerAwsEnvTest {++ private LaunchedContainerAwsEnv awsEnvWithEndpoint(String baseUrl) {+ ContainerReachableEndpoint endpoint = mock(ContainerReachableEndpoint.class);+ when(endpoint.baseUrl()).thenReturn(baseUrl);+ return new LaunchedContainerAwsEnv(endpoint);+ }++ @Test+ void injectsRegionEndpointAndPlaceholderCredentialsWhenNoConfigDir() {+ LaunchedContainerAwsEnv awsEnv = awsEnvWithEndpoint("http://localhost:4566");++ List<String> env = awsEnv.sdkBaselineEnv("us-east-1", Optional.empty());++ assertTrue(env.contains("AWS_DEFAULT_REGION=us-east-1"));+ assertTrue(env.contains("AWS_REGION=us-east-1"));++ // Floci endpoint the SDK should target, reachable from inside the container.+ assertTrue(env.contains("FLOCI_HOSTNAME=localhost"));+ assertTrue(env.contains("FLOCI_ENDPOINT=http://localhost:4566"));+ assertTrue(env.contains("AWS_ENDPOINT_URL=http://localhost:4566"));++ // Placeholder credentials: the host env var when set, otherwise "test".+ String expectedAk = System.getenv("AWS_ACCESS_KEY_ID") != null ? System.getenv("AWS_ACCESS_KEY_ID") : "test";+ String expectedSk = System.getenv("AWS_SECRET_ACCESS_KEY") != null ? System.getenv("AWS_SECRET_ACCESS_KEY") : "test";+ String expectedSt = System.getenv("AWS_SESSION_TOKEN") != null ? System.getenv("AWS_SESSION_TOKEN") : "test";+ assertTrue(env.contains("AWS_ACCESS_KEY_ID=" + expectedAk));+ assertTrue(env.contains("AWS_SECRET_ACCESS_KEY=" + expectedSk));+ assertTrue(env.contains("AWS_SESSION_TOKEN=" + expectedSt));++ // No mounted-config file paths when credentials are injected directly.+ assertTrue(env.stream().noneMatch(e -> e.startsWith("AWS_SHARED_CREDENTIALS_FILE=")));+ assertTrue(env.stream().noneMatch(e -> e.startsWith("AWS_CONFIG_FILE=")));+ }++ @Test+ void pointsSdkAtMountedConfigDirAndSkipsPlaceholderCredentials() {+ LaunchedContainerAwsEnv awsEnv = awsEnvWithEndpoint("http://localhost:4566");++ List<String> env = awsEnv.sdkBaselineEnv("eu-west-1", Optional.of("/opt/aws-config"));++ assertTrue(env.contains("AWS_DEFAULT_REGION=eu-west-1"));+ assertTrue(env.contains("AWS_REGION=eu-west-1"));++ // A mounted ~/.aws directory: point the SDK at explicit file paths, discover credentials there.+ assertTrue(env.contains("AWS_SHARED_CREDENTIALS_FILE=/opt/aws-config/credentials"));+ assertTrue(env.contains("AWS_CONFIG_FILE=/opt/aws-config/config"));++ // Credentials must not be injected when the SDK discovers them from the mounted directory.+ assertTrue(env.stream().noneMatch(e -> e.startsWith("AWS_ACCESS_KEY_ID=")));+ assertTrue(env.stream().noneMatch(e -> e.startsWith("AWS_SECRET_ACCESS_KEY=")));+ assertTrue(env.stream().noneMatch(e -> e.startsWith("AWS_SESSION_TOKEN=")));++ assertTrue(env.contains("AWS_ENDPOINT_URL=http://localhost:4566"));+ }++ @Test+ void blankConfigDirFallsBackToPlaceholderCredentials() {+ LaunchedContainerAwsEnv awsEnv = awsEnvWithEndpoint("http://localhost:4566");++ List<String> env = awsEnv.sdkBaselineEnv("us-east-1", Optional.of(" "));++ // A blank directory is treated as "not mounted": inject placeholder credentials instead.+ assertTrue(env.stream().anyMatch(e -> e.startsWith("AWS_ACCESS_KEY_ID=")));+ assertTrue(env.stream().noneMatch(e -> e.startsWith("AWS_SHARED_CREDENTIALS_FILE=")));+ }++ @Test+ void derivesFlociHostnameFromEndpointHost() {+ LaunchedContainerAwsEnv awsEnv = awsEnvWithEndpoint("https://host.docker.internal:4566");++ List<String> env = awsEnv.sdkBaselineEnv("us-east-1", Optional.empty());++ assertEquals(1, env.stream().filter(e -> e.equals("FLOCI_HOSTNAME=host.docker.internal")).count());+ assertTrue(env.contains("AWS_ENDPOINT_URL=https://host.docker.internal:4566"));+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerAwsBaselineTest.java b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerAwsBaselineTest.javanew file mode 100644index 0000000000..93c6c97f55--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerAwsBaselineTest.java@@ -0,0 +1,151 @@+package io.github.hectorvent.floci.services.ecs.container;++import io.github.hectorvent.floci.config.EmulatorConfig;+import io.github.hectorvent.floci.core.common.RegionResolver;+import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;+import io.github.hectorvent.floci.core.common.docker.ContainerDetector;+import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;+import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo;+import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;+import io.github.hectorvent.floci.core.common.docker.LaunchedContainerAwsEnv;+import io.github.hectorvent.floci.services.ecs.model.ContainerDefinition;+import io.github.hectorvent.floci.services.ecs.model.ContainerOverride;+import io.github.hectorvent.floci.services.ecs.model.EcsTask;+import io.github.hectorvent.floci.services.ecs.model.KeyValuePair;+import io.github.hectorvent.floci.services.ecs.model.TaskDefinition;+import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.Test;+import org.mockito.ArgumentCaptor;++import java.util.List;+import java.util.Map;++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.anyString;+import static org.mockito.ArgumentMatchers.eq;+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;+import static org.mockito.Mockito.RETURNS_SELF;+import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.verify;+import static org.mockito.Mockito.when;++/**+ * Unit tests for the AWS SDK baseline environment injected into ECS task containers by+ * {@link EcsContainerManager#startTask}. The baseline (region, credentials and the Floci+ * endpoint) is added as an <em>overridable default</em>: it is present unless the task+ * definition or a RunTask {@code containerOverride} sets the same key, in which case the+ * explicit value wins so a task's own configuration is never clobbered.+ *+ * <p>The container builder, lifecycle manager and {@link LaunchedContainerAwsEnv} are mocked,+ * so the test asserts the env that <em>would</em> be handed to Docker without launching a+ * container — runnable under {@code mvn test} (CI) with no Docker daemon.+ */+class EcsContainerManagerAwsBaselineTest {++ private ContainerBuilder containerBuilder;+ private ContainerBuilder.Builder builder;+ private LaunchedContainerAwsEnv awsEnv;+ private EcsContainerManager manager;++ @BeforeEach+ void setUp() {+ builder = mock(ContainerBuilder.Builder.class, RETURNS_SELF);+ containerBuilder = mock(ContainerBuilder.class);+ when(containerBuilder.newContainer(anyString())).thenReturn(builder);++ ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);+ when(lifecycleManager.createAndStart(any()))+ .thenReturn(new ContainerInfo("docker-id", Map.of()));++ ContainerLogStreamer logStreamer = mock(ContainerLogStreamer.class);+ ContainerDetector containerDetector = mock(ContainerDetector.class);+ EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);+ RegionResolver regionResolver = mock(RegionResolver.class);++ awsEnv = mock(LaunchedContainerAwsEnv.class);+ when(awsEnv.sdkBaselineEnv(anyString(), any())).thenReturn(List.of(+ "AWS_DEFAULT_REGION=us-east-1",+ "AWS_REGION=us-east-1",+ "AWS_ACCESS_KEY_ID=test",+ "AWS_ENDPOINT_URL=http://localhost:4566"));++ manager = new EcsContainerManager(containerBuilder, lifecycleManager, logStreamer,+ containerDetector, config, regionResolver, awsEnv);+ }++ @Test+ void injectsAwsBaselineAsOverridableDefaults() {+ ContainerDefinition app = containerDef("app", "app:latest",+ // Task def overrides one baseline key (AWS_REGION) and adds its own key.+ List.of(new KeyValuePair("AWS_REGION", "eu-west-1"),+ new KeyValuePair("APP_ONLY", "1")));++ TaskDefinition taskDef = new TaskDefinition();+ taskDef.setFamily("test-family");+ taskDef.setContainerDefinitions(List.of(app));++ EcsTask task = new EcsTask();+ task.setTaskArn("arn:aws:ecs:us-east-1:000000000000:task/test-cluster/abc123");++ manager.startTask(task, taskDef, null, "us-east-1");++ // The baseline is resolved for the task's region.+ verify(awsEnv).sdkBaselineEnv(eq("us-east-1"), any());++ @SuppressWarnings("unchecked")+ ArgumentCaptor<List<String>> envCaptor = ArgumentCaptor.forClass(List.class);+ verify(builder).withEnv(envCaptor.capture());+ List<String> env = envCaptor.getValue();++ // Baseline entries are injected...+ assertTrue(env.contains("AWS_ENDPOINT_URL=http://localhost:4566"),+ "AWS endpoint baseline should be injected into the task container");+ assertTrue(env.contains("AWS_ACCESS_KEY_ID=test"),+ "credential baseline should be injected when not overridden");+ // ...but the task def wins on a key conflict (baseline is overridable).+ assertTrue(env.contains("AWS_REGION=eu-west-1"),+ "task-def value should override the baseline on key conflict");+ assertFalse(env.contains("AWS_REGION=us-east-1"),+ "overridden baseline value should not remain");+ // Task-def-only entries are preserved.+ assertTrue(env.contains("APP_ONLY=1"));+ }++ @Test+ void runTaskOverrideWinsOverBaseline() {+ ContainerDefinition app = containerDef("app", "app:latest", List.of());++ TaskDefinition taskDef = new TaskDefinition();+ taskDef.setFamily("test-family");+ taskDef.setContainerDefinitions(List.of(app));++ ContainerOverride override = new ContainerOverride();+ override.setName("app");+ override.setEnvironment(List.of(new KeyValuePair("AWS_ENDPOINT_URL", "http://override:9999")));++ EcsTask task = new EcsTask();+ task.setTaskArn("arn:aws:ecs:us-east-1:000000000000:task/test-cluster/abc123");++ manager.startTask(task, taskDef, List.of(override), "us-east-1");++ @SuppressWarnings("unchecked")+ ArgumentCaptor<List<String>> envCaptor = ArgumentCaptor.forClass(List.class);+ verify(builder).withEnv(envCaptor.capture());+ List<String> env = envCaptor.getValue();++ assertTrue(env.contains("AWS_ENDPOINT_URL=http://override:9999"),+ "RunTask containerOverride should win over the baseline");+ assertFalse(env.contains("AWS_ENDPOINT_URL=http://localhost:4566"),+ "overridden baseline endpoint should not remain");+ }++ private static ContainerDefinition containerDef(String name, String image, List<KeyValuePair> env) {+ ContainerDefinition def = new ContainerDefinition();+ def.setName(name);+ def.setImage(image);+ def.setEnvironment(env);+ return def;+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerOverridesTest.java b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerOverridesTest.javaindex 6b844a2309..9784858d17 100644--- a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerOverridesTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerOverridesTest.java@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.core.common.docker.ContainerDetector;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo;import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;+import io.github.hectorvent.floci.core.common.docker.LaunchedContainerAwsEnv;import io.github.hectorvent.floci.services.ecs.model.ContainerDefinition;import io.github.hectorvent.floci.services.ecs.model.ContainerOverride;import io.github.hectorvent.floci.services.ecs.model.EcsTask;@@ -63,9 +64,11 @@ class EcsContainerManagerOverridesTest {ContainerDetector containerDetector = mock(ContainerDetector.class);EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);RegionResolver regionResolver = mock(RegionResolver.class);+ LaunchedContainerAwsEnv awsEnv = mock(LaunchedContainerAwsEnv.class);+ when(awsEnv.sdkBaselineEnv(any(), any())).thenReturn(List.of());manager = new EcsContainerManager(containerBuilder, lifecycleManager, logStreamer,- containerDetector, config, regionResolver);+ containerDetector, config, regionResolver, awsEnv);}@Testdiff --git a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerPortMappingsTest.java b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerPortMappingsTest.javaindex 9a4d8832fd..3d6d3c9eee 100644--- a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerPortMappingsTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerPortMappingsTest.java@@ -8,6 +8,7 @@ import io.github.hectorvent.floci.core.common.docker.ContainerDetector;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo;import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;+import io.github.hectorvent.floci.core.common.docker.LaunchedContainerAwsEnv;import io.github.hectorvent.floci.services.ecs.model.ContainerDefinition;import io.github.hectorvent.floci.services.ecs.model.EcsTask;import io.github.hectorvent.floci.services.ecs.model.PortMapping;@@ -71,9 +72,11 @@ class EcsContainerManagerPortMappingsTest {containerDetector = mock(ContainerDetector.class);EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);RegionResolver regionResolver = mock(RegionResolver.class);+ LaunchedContainerAwsEnv awsEnv = mock(LaunchedContainerAwsEnv.class);+ when(awsEnv.sdkBaselineEnv(any(), any())).thenReturn(List.of());manager = new EcsContainerManager(containerBuilder, lifecycleManager, logStreamer,- containerDetector, config, regionResolver);+ containerDetector, config, regionResolver, awsEnv);}private void startWith(List<PortMapping> portMappings) {diff --git a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerVolumesTest.java b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerVolumesTest.javaindex 7bba2de20c..413e429c8c 100644--- a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerVolumesTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerVolumesTest.java@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.core.common.docker.ContainerDetector;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo;import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;+import io.github.hectorvent.floci.core.common.docker.LaunchedContainerAwsEnv;import io.github.hectorvent.floci.services.ecs.model.ContainerDefinition;import io.github.hectorvent.floci.services.ecs.model.EcsTask;import io.github.hectorvent.floci.services.ecs.model.EfsVolumeConfiguration;@@ -65,9 +66,11 @@ class EcsContainerManagerVolumesTest {ContainerDetector containerDetector = mock(ContainerDetector.class);EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);RegionResolver regionResolver = mock(RegionResolver.class);+ LaunchedContainerAwsEnv awsEnv = mock(LaunchedContainerAwsEnv.class);+ when(awsEnv.sdkBaselineEnv(any(), any())).thenReturn(List.of());manager = new EcsContainerManager(containerBuilder, lifecycleManager, logStreamer,- containerDetector, config, regionResolver);+ containerDetector, config, regionResolver, awsEnv);}@Testdiff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaImageConfigTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaImageConfigTest.javaindex a93f35fd54..2e74af061e 100644--- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaImageConfigTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaImageConfigTest.java@@ -9,6 +9,7 @@ import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;import io.github.hectorvent.floci.core.common.docker.ContainerSpec;import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;+import io.github.hectorvent.floci.core.common.docker.LaunchedContainerAwsEnv;import io.github.hectorvent.floci.core.storage.InMemoryStorage;import io.github.hectorvent.floci.services.ecr.registry.EcrRegistryManager;import io.github.hectorvent.floci.services.lambda.launcher.ContainerLauncher;@@ -247,9 +248,10 @@ class LambdaImageConfigTest {ContainerBuilder containerBuilder = new ContainerBuilder(config, dockerHostResolver, embeddedDnsServer);ContainerReachableEndpoint reachableEndpoint =new ContainerReachableEndpoint(config, dockerHostResolver, embeddedDnsServer);+ LaunchedContainerAwsEnv awsEnv = new LaunchedContainerAwsEnv(reachableEndpoint);launcher = new ContainerLauncher(containerBuilder, lifecycleManager, logStreamer, imageResolver,runtimeApiServerFactory, dockerHostResolver, config, ecrRegistryManager,- mock(io.github.hectorvent.floci.services.lambda.LambdaLayerService.class), reachableEndpoint);+ mock(io.github.hectorvent.floci.services.lambda.LambdaLayerService.class), awsEnv);when(runtimeApiServerFactory.create()).thenReturn(runtimeApiServer);when(runtimeApiServer.getPort()).thenReturn(9000);diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.javaindex 451cc44b41..ac14850c91 100644--- a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java@@ -8,6 +8,7 @@ import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;import io.github.hectorvent.floci.core.common.docker.ContainerReachableEndpoint;import io.github.hectorvent.floci.core.common.docker.ContainerSpec;import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;+import io.github.hectorvent.floci.core.common.docker.LaunchedContainerAwsEnv;import io.github.hectorvent.floci.services.ecr.registry.EcrRegistryManager;import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;import io.github.hectorvent.floci.services.lambda.runtime.RuntimeApiServer;@@ -86,9 +87,10 @@ class ContainerLauncherTest {ContainerBuilder containerBuilder = new ContainerBuilder(config, dockerHostResolver, embeddedDnsServer);ContainerReachableEndpoint reachableEndpoint =new ContainerReachableEndpoint(config, dockerHostResolver, embeddedDnsServer);+ LaunchedContainerAwsEnv awsEnv = new LaunchedContainerAwsEnv(reachableEndpoint);launcher = new ContainerLauncher(containerBuilder, lifecycleManager, logStreamer, imageResolver,runtimeApiServerFactory, dockerHostResolver, config, ecrRegistryManager,- mock(io.github.hectorvent.floci.services.lambda.LambdaLayerService.class), reachableEndpoint);+ mock(io.github.hectorvent.floci.services.lambda.LambdaLayerService.class), awsEnv);when(runtimeApiServerFactory.create()).thenReturn(runtimeApiServer);when(runtimeApiServer.getPort()).thenReturn(9000);