mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(lambda): inject default AWS credentials into Lambda containers (#623)
SDK calls inside Lambda containers fell through to IMDS and timed out because AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY / AWS_SESSION_TOKEN were absent from the runtime environment. Inject placeholder values before the user-supplied --environment so the SDK credential chain resolves without IMDS, while still honouring user overrides via Docker's last-wins duplicate-env semantics.
This commit is contained in:
1 parent
d39bdf1c22
commit
33ce936d44
2 files changed
+67
No files matched your search
+64
@@ -24,9 +24,11 @@ import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
@@ -152,6 +154,68 @@ class ContainerLauncherTest {
|
||||
verify(lifecycleManager, never()).createAndStart(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void launchFunction_injectsDefaultAwsCredentials() throws Exception {
|
||||
Path codePath = Files.createDirectory(tempDir.resolve("creds-defaults"));
|
||||
|
||||
LambdaFunction fn = new LambdaFunction();
|
||||
fn.setFunctionName("creds-fn");
|
||||
fn.setRuntime("nodejs20.x");
|
||||
fn.setHandler("index.handler");
|
||||
fn.setCodeLocalPath(codePath.toString());
|
||||
|
||||
launcher.launch(fn);
|
||||
|
||||
ArgumentCaptor<ContainerSpec> specCaptor = ArgumentCaptor.forClass(ContainerSpec.class);
|
||||
verify(lifecycleManager).create(specCaptor.capture());
|
||||
|
||||
List<String> env = specCaptor.getValue().env();
|
||||
assertTrue(env.contains("AWS_ACCESS_KEY_ID=test"),
|
||||
"default AWS_ACCESS_KEY_ID should be injected");
|
||||
assertTrue(env.contains("AWS_SECRET_ACCESS_KEY=test"),
|
||||
"default AWS_SECRET_ACCESS_KEY should be injected");
|
||||
assertTrue(env.contains("AWS_SESSION_TOKEN=test"),
|
||||
"default AWS_SESSION_TOKEN should be injected");
|
||||
}
|
||||
|
||||
@Test
|
||||
void launchFunction_userEnvironmentOverridesDefaultCredentials() throws Exception {
|
||||
Path codePath = Files.createDirectory(tempDir.resolve("creds-override"));
|
||||
|
||||
LambdaFunction fn = new LambdaFunction();
|
||||
fn.setFunctionName("override-fn");
|
||||
fn.setRuntime("nodejs20.x");
|
||||
fn.setHandler("index.handler");
|
||||
fn.setCodeLocalPath(codePath.toString());
|
||||
fn.setEnvironment(Map.of(
|
||||
"AWS_ACCESS_KEY_ID", "user-key",
|
||||
"AWS_SECRET_ACCESS_KEY", "user-secret"));
|
||||
|
||||
launcher.launch(fn);
|
||||
|
||||
ArgumentCaptor<ContainerSpec> specCaptor = ArgumentCaptor.forClass(ContainerSpec.class);
|
||||
verify(lifecycleManager).create(specCaptor.capture());
|
||||
|
||||
List<String> env = specCaptor.getValue().env();
|
||||
// Docker honours the last occurrence of a duplicate Env entry, so user
|
||||
// overrides must appear after the Floci defaults.
|
||||
int defaultKeyIdx = env.indexOf("AWS_ACCESS_KEY_ID=test");
|
||||
int userKeyIdx = env.indexOf("AWS_ACCESS_KEY_ID=user-key");
|
||||
assertTrue(defaultKeyIdx >= 0, "default AWS_ACCESS_KEY_ID still present");
|
||||
assertTrue(userKeyIdx > defaultKeyIdx,
|
||||
"user AWS_ACCESS_KEY_ID must appear after the default");
|
||||
|
||||
int defaultSecretIdx = env.indexOf("AWS_SECRET_ACCESS_KEY=test");
|
||||
int userSecretIdx = env.indexOf("AWS_SECRET_ACCESS_KEY=user-secret");
|
||||
assertTrue(defaultSecretIdx >= 0, "default AWS_SECRET_ACCESS_KEY still present");
|
||||
assertTrue(userSecretIdx > defaultSecretIdx,
|
||||
"user AWS_SECRET_ACCESS_KEY must appear after the default");
|
||||
|
||||
// AWS_SESSION_TOKEN was not overridden so the default remains.
|
||||
assertEquals(1, env.stream().filter(e -> e.startsWith("AWS_SESSION_TOKEN=")).count(),
|
||||
"AWS_SESSION_TOKEN should retain its default exactly once");
|
||||
}
|
||||
|
||||
@Test
|
||||
void launchProvidedRuntime_copiesBootstrapBeforeStart() throws Exception {
|
||||
Path codePath = Files.createDirectory(tempDir.resolve("provided-code"));
|
||||
|
||||
Reference in new issue
Block a user