mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(lambda): resolve background layers in their function account (#6162)
Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
1 parent
52c54834b0
commit
2ecc01b3e5
8 files changed
+250
-4
No files matched your search
+74
@@ -0,0 +1,74 @@
|
||||
package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaLayerVersion;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
|
||||
class LambdaLayerAsyncAccountTest {
|
||||
private static final String DEFAULT_ACCOUNT = "000000000000";
|
||||
private static final String OWNER = "123456789012";
|
||||
|
||||
@Test
|
||||
void backgroundLookupUsesTheStoredOwnerAndDoesNotSubstituteTheDefaultAccountsLayer() throws Exception {
|
||||
AccountAwareStorageBackend<LambdaLayerVersion> storage = AccountAwareStorageBackend.inMemory(DEFAULT_ACCOUNT);
|
||||
LambdaLayerService service = service(storage);
|
||||
LambdaLayerVersion defaultLayer = layer("arn:aws:lambda:eu-west-1:" + DEFAULT_ACCOUNT + ":layer:shared:1");
|
||||
LambdaLayerVersion ownerLayer = layer("arn:aws:lambda:eu-west-1:" + OWNER + ":layer:shared:1");
|
||||
storage.putForAccount(DEFAULT_ACCOUNT, "layer::eu-west-1::shared::1", defaultLayer);
|
||||
storage.putForAccount(OWNER, "layer::eu-west-1::shared::1", ownerLayer);
|
||||
|
||||
assertNull(CompletableFuture.supplyAsync(
|
||||
() -> service.resolveLayerByArn(ownerLayer.getLayerVersionArn())).get(5, TimeUnit.SECONDS));
|
||||
assertSame(ownerLayer, CompletableFuture.supplyAsync(() -> service.resolveLayerByArnForAccount(
|
||||
ownerLayer.getLayerVersionArn(), OWNER, "eu-west-1")).get(5, TimeUnit.SECONDS));
|
||||
assertSame(defaultLayer, service.resolveLayerByArn(defaultLayer.getLayerVersionArn()));
|
||||
assertNull(service.resolveLayerByArnForAccount(defaultLayer.getLayerVersionArn(), OWNER, "eu-west-1"));
|
||||
assertNull(service.resolveLayerByArnForAccount(ownerLayer.getLayerVersionArn(), "222222222222", "eu-west-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void backgroundLookupUsesTheFunctionsPartitionAndChecksTheStoredArn() {
|
||||
AccountAwareStorageBackend<LambdaLayerVersion> storage = AccountAwareStorageBackend.inMemory(DEFAULT_ACCOUNT);
|
||||
LambdaLayerService service = service(storage);
|
||||
LambdaLayerVersion ownerLayer = layer("arn:aws-cn:lambda:cn-north-1:" + OWNER + ":layer:shared:1");
|
||||
storage.putForAccount(OWNER, "layer::cn-north-1::shared::1", ownerLayer);
|
||||
assertSame(ownerLayer, service.resolveLayerByArnForAccount(ownerLayer.getLayerVersionArn(), OWNER, "cn-north-1"));
|
||||
assertNull(service.resolveLayerByArnForAccount(ownerLayer.getLayerVersionArn(), OWNER, "eu-west-1"));
|
||||
assertNull(service.resolveLayerByArnForAccount(
|
||||
"arn:aws:lambda:cn-north-1:" + OWNER + ":layer:shared:1", OWNER, "cn-north-1"));
|
||||
storage.putForAccount(OWNER, "layer::cn-north-1::shared::1",
|
||||
layer("arn:aws-cn:lambda:cn-north-1:222222222222:layer:shared:1"));
|
||||
assertNull(service.resolveLayerByArnForAccount(ownerLayer.getLayerVersionArn(), OWNER, "cn-north-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void absentLayerOrInvalidOwnerNeverFallsBackToAmbientStorage() {
|
||||
AccountAwareStorageBackend<LambdaLayerVersion> storage = AccountAwareStorageBackend.inMemory(DEFAULT_ACCOUNT);
|
||||
LambdaLayerService service = service(storage);
|
||||
String arn = "arn:aws:lambda:eu-west-1:" + OWNER + ":layer:shared:1";
|
||||
assertNull(service.resolveLayerByArnForAccount(arn, OWNER, "eu-west-1"));
|
||||
assertNull(service.resolveLayerByArnForAccount(arn, null, "eu-west-1"));
|
||||
assertNull(service.resolveLayerByArnForAccount(arn, "", "eu-west-1"));
|
||||
assertNull(service.resolveLayerByArnForAccount(arn, OWNER, ""));
|
||||
}
|
||||
|
||||
private static LambdaLayerService service(AccountAwareStorageBackend<LambdaLayerVersion> storage) {
|
||||
return new LambdaLayerService(new LambdaLayerStore(storage), null, null,
|
||||
new RegionResolver("us-east-1", DEFAULT_ACCOUNT), null);
|
||||
}
|
||||
|
||||
private static LambdaLayerVersion layer(String arn) {
|
||||
LambdaLayerVersion layer = new LambdaLayerVersion();
|
||||
layer.setLayerName("shared");
|
||||
layer.setVersion(1);
|
||||
layer.setLayerVersionArn(arn);
|
||||
return layer;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaLayerVersion;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public final class LambdaLayerFixtures {
|
||||
private LambdaLayerFixtures() {}
|
||||
|
||||
public static LambdaLayerService accountScopedService(
|
||||
String defaultAccount, String region, Map<String, LambdaLayerVersion> layersByAccount) {
|
||||
AccountAwareStorageBackend<LambdaLayerVersion> storage = AccountAwareStorageBackend.inMemory(defaultAccount);
|
||||
layersByAccount.forEach((account, layer) -> storage.putForAccount(account,
|
||||
"layer::" + region + "::" + layer.getLayerName() + "::" + layer.getVersion(), layer));
|
||||
return new LambdaLayerService(new LambdaLayerStore(storage), null, null,
|
||||
new RegionResolver("us-east-1", defaultAccount), null);
|
||||
}
|
||||
|
||||
public static LambdaLayerVersion layer(String account, String region, String name, long version) {
|
||||
LambdaLayerVersion layer = new LambdaLayerVersion();
|
||||
layer.setLayerName(name);
|
||||
layer.setVersion(version);
|
||||
layer.setLayerVersionArn("arn:aws:lambda:" + region + ":" + account + ":layer:" + name + ":" + version);
|
||||
return layer;
|
||||
}
|
||||
}
|
||||
+58
@@ -25,8 +25,11 @@ import io.github.hectorvent.floci.core.common.docker.LaunchedContainerAwsEnv;
|
||||
import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsService;
|
||||
import io.github.hectorvent.floci.services.ecr.registry.EcrRegistryManager;
|
||||
import io.github.hectorvent.floci.services.iam.model.SessionCreds;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaLayerFixtures;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaLayerService;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaFileSystemConfig;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaLayerVersion;
|
||||
import io.github.hectorvent.floci.services.lambda.runtime.RuntimeApiServer;
|
||||
import io.github.hectorvent.floci.services.lambda.runtime.RuntimeApiServerFactory;
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
||||
@@ -55,6 +58,7 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalInt;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@@ -93,6 +97,7 @@ class ContainerLauncherTest {
|
||||
/** Collects remote paths passed to withRemotePath across all copy mocks. */
|
||||
final java.util.List<String> capturedRemotePaths = new java.util.ArrayList<>();
|
||||
String capturedRuntimeBootstrap;
|
||||
final Map<String, String> capturedLayerFiles = new LinkedHashMap<>();
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
@@ -166,6 +171,7 @@ class ContainerLauncherTest {
|
||||
// when the pipe buffer fills.
|
||||
capturedRemotePaths.clear();
|
||||
capturedRuntimeBootstrap = null;
|
||||
capturedLayerFiles.clear();
|
||||
lenient().when(dockerClient.copyArchiveToContainerCmd(any())).thenAnswer(inv -> {
|
||||
CopyArchiveToContainerCmd cmd = mock(CopyArchiveToContainerCmd.class);
|
||||
final java.io.InputStream[] captured = {null};
|
||||
@@ -188,6 +194,18 @@ class ContainerLauncherTest {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
if ("/opt".equals(remote[0])) {
|
||||
try (TarArchiveInputStream tar = new TarArchiveInputStream(captured[0])) {
|
||||
TarArchiveEntry entry;
|
||||
while ((entry = tar.getNextEntry()) != null) {
|
||||
if (!entry.isDirectory()) {
|
||||
capturedLayerFiles.put(entry.getName(),
|
||||
new String(tar.readAllBytes(), StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
try { captured[0].transferTo(java.io.OutputStream.nullOutputStream()); }
|
||||
catch (Exception ignored) {}
|
||||
}
|
||||
@@ -197,6 +215,46 @@ class ContainerLauncherTest {
|
||||
});
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"000000000000", "123456789012"})
|
||||
void backgroundColdStartCopiesOnlyTheFunctionsOwnedLayer(String account) throws Exception {
|
||||
String defaultAccount = "000000000000";
|
||||
String owner = "123456789012";
|
||||
String region = "eu-west-1";
|
||||
LambdaLayerVersion defaultLayer = LambdaLayerFixtures.layer(defaultAccount, region, "shared", 1);
|
||||
LambdaLayerVersion ownerLayer = LambdaLayerFixtures.layer(owner, region, "shared", 1);
|
||||
Map<String, LambdaLayerVersion> ownedLayers = Map.of(defaultAccount, defaultLayer, owner, ownerLayer);
|
||||
for (Map.Entry<String, LambdaLayerVersion> entry : ownedLayers.entrySet()) {
|
||||
Path layerPath = Files.createDirectory(tempDir.resolve(entry.getKey()));
|
||||
Files.writeString(layerPath.resolve("owner.txt"), entry.getKey());
|
||||
entry.getValue().setCodeLocalPath(layerPath.toString());
|
||||
}
|
||||
LambdaLayerService layers = LambdaLayerFixtures.accountScopedService(
|
||||
defaultAccount, region, Map.of(defaultAccount, defaultLayer, owner, ownerLayer));
|
||||
ContainerReachableEndpoint endpoint =
|
||||
new ContainerReachableEndpoint(config, dockerHostResolver, embeddedDnsServer, 4566);
|
||||
ContainerLauncher backgroundLauncher = new ContainerLauncher(
|
||||
new ContainerBuilder(config, dockerHostResolver, embeddedDnsServer),
|
||||
lifecycleManager, logStreamer, imageResolver, runtimeApiServerFactory,
|
||||
dockerHostResolver, config, ecrRegistryManager, layers,
|
||||
new LaunchedContainerAwsEnv(endpoint), executionRoleCredentials);
|
||||
LambdaFunction fn = new LambdaFunction();
|
||||
fn.setFunctionName("background-layer");
|
||||
fn.setFunctionArn("arn:aws:lambda:" + region + ":" + account + ":function:background-layer");
|
||||
fn.setRuntime("nodejs20.x");
|
||||
fn.setHandler("index.handler");
|
||||
fn.setHotReloadHostPath(tempDir.toString());
|
||||
fn.setLayers(List.of((defaultAccount.equals(account) ? defaultLayer : ownerLayer).getLayerVersionArn()));
|
||||
|
||||
ContainerHandle handle = CompletableFuture.supplyAsync(() -> backgroundLauncher.launch(fn))
|
||||
.get(10, TimeUnit.SECONDS);
|
||||
try {
|
||||
assertEquals(Map.of("owner.txt", account), capturedLayerFiles);
|
||||
} finally {
|
||||
backgroundLauncher.stop(handle);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Captures every {@link ContainerSpec} passed to {@code lifecycleManager.create(...)} and
|
||||
* returns the REAL Lambda container's spec.
|
||||
|
||||
+42
@@ -11,11 +11,13 @@ import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.config.TlsConfigSource;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.docker.LaunchedContainerAwsEnv;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaLayerFixtures;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaLayerService;
|
||||
import io.github.hectorvent.floci.services.lambda.launcher.ContainerHandle;
|
||||
import io.github.hectorvent.floci.services.lambda.launcher.ImageResolver;
|
||||
import io.github.hectorvent.floci.services.lambda.model.ContainerState;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaLayerVersion;
|
||||
import io.github.hectorvent.floci.services.lambda.runtime.RuntimeApiServer;
|
||||
import io.github.hectorvent.floci.services.lambda.runtime.RuntimeApiServerFactory;
|
||||
import io.github.hectorvent.floci.services.s3.PreSignedUrlGenerator;
|
||||
@@ -26,6 +28,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManager;
|
||||
@@ -41,6 +44,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -142,6 +146,44 @@ class KubernetesPodLauncherTest {
|
||||
logStreamer, s3Service, presignGenerator);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"000000000000", "123456789012"})
|
||||
void backgroundColdStartDownloadsOnlyTheFunctionsOwnedLayer(String account) throws Exception {
|
||||
String defaultAccount = "000000000000";
|
||||
String owner = "123456789012";
|
||||
String region = "eu-west-1";
|
||||
LambdaLayerVersion defaultLayer = LambdaLayerFixtures.layer(defaultAccount, region, "shared", 1);
|
||||
LambdaLayerVersion ownerLayer = LambdaLayerFixtures.layer(owner, region, "shared", 1);
|
||||
LambdaLayerService layers = LambdaLayerFixtures.accountScopedService(
|
||||
defaultAccount, region, Map.of(defaultAccount, defaultLayer, owner, ownerLayer));
|
||||
KubernetesPodLauncher backgroundLauncher = new KubernetesPodLauncher(
|
||||
apiClient, config, runtimeApiServerFactory, imageResolver, addressResolver, awsEnv,
|
||||
layers, new LambdaPodSpecFactory(config), logStreamer, s3Service, presignGenerator);
|
||||
LambdaFunction fn = function();
|
||||
fn.setAccountId(account);
|
||||
fn.setFunctionArn("arn:aws:lambda:" + region + ":" + account + ":function:my-fn");
|
||||
fn.setLayers(List.of((defaultAccount.equals(account) ? defaultLayer : ownerLayer).getLayerVersionArn()));
|
||||
markPodPhaseInBackground("floci-lambda-my-fn-", "Running");
|
||||
|
||||
ContainerHandle handle = CompletableFuture.supplyAsync(() -> backgroundLauncher.launch(fn))
|
||||
.get(10, TimeUnit.SECONDS);
|
||||
try {
|
||||
Pod pod = client.pods().inNamespace("default").withName(handle.getContainerId()).get();
|
||||
String initScript = pod.getSpec().getInitContainers().getFirst().getCommand().get(2);
|
||||
assertThat(initScript).contains("/awslambda-" + region + "-tasks/layers/"
|
||||
+ account + "/shared/1?X-Amz-Algorithm=AWS4-HMAC-SHA256")
|
||||
.contains("unzip -oq /tmp/layer0.zip -d /opt");
|
||||
String otherAccount = defaultAccount.equals(account) ? owner : defaultAccount;
|
||||
assertThat(initScript).doesNotContain("/layers/" + otherAccount + "/");
|
||||
verify(presignGenerator).generatePresignedUrl(
|
||||
eq("http://10.0.0.5:4566"), eq("awslambda-" + region + "-tasks"),
|
||||
eq("layers/" + account + "/shared/1"), eq("GET"), eq(0), eq(region), eq(account));
|
||||
} finally {
|
||||
backgroundLauncher.stop(handle);
|
||||
}
|
||||
assertThat(client.pods().inNamespace("default").withName(handle.getContainerId()).get()).isNull();
|
||||
}
|
||||
|
||||
/** The mock server's cert is self-signed and generated per run; trust it, not the JVM default. */
|
||||
private static HttpClient trustAllHttpClient() {
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user