mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(ecs): inject the AWS SDK baseline env into launched task containers (#1697)
* test(stepfunctions): pass ecs:runTask ctor args in AslExecutorPathIntrinsicsTest Same one-line fix as #1696: AslExecutor gained EcsService/EcsJsonHandler ctor params in #1564 but this test (from #1558) was not updated, breaking test compilation on main. Included here so this PR's CI is green independently of #1696; resolves cleanly once #1696 merges (identical change). * feat(ecs): inject the AWS SDK baseline env into launched task containers Launched ECS task containers previously received only their task-definition and override environment. An AWS SDK running inside such a container therefore had no AWS_ENDPOINT_URL (so it targeted real AWS instead of the emulator), no region, and an empty credential-provider chain. Lambda containers already avoid this because ContainerLauncher injects those variables. Extract that baseline into a shared LaunchedContainerAwsEnv helper and apply it to ECS tasks too: - LaunchedContainerAwsEnv (core/common/docker): builds the KEY=value baseline — AWS_DEFAULT_REGION/AWS_REGION, credentials (a mounted ~/.aws dir when configured, otherwise placeholder credentials from Floci's own env falling back to test/test/test), and FLOCI_HOSTNAME/FLOCI_ENDPOINT/AWS_ENDPOINT_URL from ContainerReachableEndpoint. - ContainerLauncher (Lambda): replace the inline region/credentials/endpoint block with the shared helper. The resulting environment is byte-for-byte identical to before; only the source of the entries moved. - EcsContainerManager (ECS): prepend the baseline to each task container's environment as overridable defaults (task-def and RunTask containerOverride values still win on key conflict), and enable withHostDockerInternalOnLinux() + withEmbeddedDns() so the endpoint hostname resolves to Floci from inside the task container, the same way Lambda containers do. Credentials are injected as env placeholders (as ContainerLauncher already does for Lambda), not via the real ECS task-role container-credentials metadata endpoint; emulating that endpoint would be a reasonable follow-up. Adds LaunchedContainerAwsEnvTest and EcsContainerManagerAwsBaselineTest, and updates the existing launcher/manager constructor call-sites. * refactor(ecs): address review feedback on LaunchedContainerAwsEnv - Drop the unused single-argument constructor that stored a null ContainerReachableEndpoint and would NPE on any sdkBaselineEnv call (Greptile P1), along with the vestigial EmulatorConfig field/param it carried — the helper only needs ContainerReachableEndpoint. - Remove the now-dead null guard in EcsContainerManager.buildEnvVars (the real sdkBaselineEnv never returns null) and instead stub the mocked helper with List.of() in the ECS unit tests, making the contract explicit (Greptile P2). * docs: regenerate action tables
This commit is contained in:
1 parent
54b378ed6c
commit
291752c36e
10 files changed
+363
-37
No files matched your search
+67
@@ -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;
|
||||
}
|
||||
}
|
||||
+23
-4
@@ -9,6 +9,7 @@ import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.C
|
||||
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.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;
|
||||
|
||||
@Inject
|
||||
public 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 spec
|
||||
ContainerBuilder.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());
|
||||
|
||||
+9
-28
@@ -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));
|
||||
|
||||
Reference in new issue
Block a user