fix(lambda): scope extracted function code by region (#3265)

* fix(lambda): scope extracted function code by region

* fix(lambda): isolate new paths from legacy code

* chore(ci): retry transient pull request checks
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-09-08 23:43:48 -05:00
1 parent e02accff4a
commit 290559cff0
4 files changed
+149 -61

No files matched your search

@@ -14,6 +14,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@@ -60,11 +61,65 @@ class LambdaAccountScopedCodeTest {
svcB.deleteFunction(REGION, "shared-fn");
assertTrue(sharedCodeStore.exists(ACCOUNT_A, "shared-fn"),
assertTrue(sharedCodeStore.exists(ACCOUNT_A, REGION, "shared-fn"),
"deleting B's function must not delete A's code");
assertEquals("A", Files.readString(Path.of(a.getCodeLocalPath()).resolve("index.js")));
}
@Test
void sameFunctionNameInTwoRegionsKeepsItsOwnCodeOnDisk(@TempDir Path baseDir) throws Exception {
CodeStore sharedCodeStore = new CodeStore(baseDir);
LambdaService service = serviceFor(ACCOUNT_A, sharedCodeStore);
LambdaFunction east = service.createFunction(REGION, zipRequest("regional-fn", "east"));
LambdaFunction west = service.createFunction("eu-west-1", zipRequest("regional-fn", "west"));
assertNotEquals(east.getCodeLocalPath(), west.getCodeLocalPath());
assertEquals("east", Files.readString(Path.of(east.getCodeLocalPath()).resolve("index.js")));
assertEquals("west", Files.readString(Path.of(west.getCodeLocalPath()).resolve("index.js")));
}
@Test
void updateAndDeleteInOneRegionLeaveTheOtherRegionCodeIntact(@TempDir Path baseDir) throws Exception {
CodeStore sharedCodeStore = new CodeStore(baseDir);
LambdaService service = serviceFor(ACCOUNT_A, sharedCodeStore);
service.createFunction(REGION, zipRequest("regional-fn", "east-v1"));
LambdaFunction west = service.createFunction("eu-west-1", zipRequest("regional-fn", "west-v1"));
service.updateFunctionCode(REGION, "regional-fn", Map.of("ZipFile", zipBase64("index.js", "east-v2")));
assertEquals("west-v1", Files.readString(Path.of(west.getCodeLocalPath()).resolve("index.js")));
service.deleteFunction(REGION, "regional-fn");
assertTrue(sharedCodeStore.exists(ACCOUNT_A, "eu-west-1", "regional-fn"));
assertEquals("west-v1", Files.readString(Path.of(west.getCodeLocalPath()).resolve("index.js")));
}
@Test
void regionScopedCodeSurvivesAStoreRestart(@TempDir Path baseDir) throws Exception {
LambdaService first = serviceFor(ACCOUNT_A, new CodeStore(baseDir));
LambdaFunction created = first.createFunction("ap-southeast-2", zipRequest("restart-fn", "persisted"));
CodeStore afterRestart = new CodeStore(baseDir);
assertTrue(afterRestart.exists(ACCOUNT_A, "ap-southeast-2", "restart-fn"));
assertEquals("persisted", Files.readString(Path.of(created.getCodeLocalPath()).resolve("index.js")));
}
@Test
void concurrentExtractionInTwoRegionsDoesNotOverwriteCode(@TempDir Path baseDir) throws Exception {
CodeStore sharedCodeStore = new CodeStore(baseDir);
LambdaService service = serviceFor(ACCOUNT_A, sharedCodeStore);
CompletableFuture<LambdaFunction> east = CompletableFuture.supplyAsync(() -> createUnchecked(
service, REGION, "concurrent-fn", "east"));
CompletableFuture<LambdaFunction> west = CompletableFuture.supplyAsync(() -> createUnchecked(
service, "eu-west-1", "concurrent-fn", "west"));
LambdaFunction eastFunction = east.join();
LambdaFunction westFunction = west.join();
assertEquals("east", Files.readString(Path.of(eastFunction.getCodeLocalPath()).resolve("index.js")));
assertEquals("west", Files.readString(Path.of(westFunction.getCodeLocalPath()).resolve("index.js")));
}
@Test
void publishVersionCounterKeyCarriesTheOwningAccount() {
LambdaFunction a = functionOwnedBy(ACCOUNT_A);
@@ -205,6 +260,14 @@ class LambdaAccountScopedCodeTest {
return fn;
}
private LambdaFunction createUnchecked(LambdaService service, String region, String name, String source) {
try {
return service.createFunction(region, zipRequest(name, source));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private LambdaService serviceFor(String accountId, CodeStore codeStore) {
return new LambdaService(
new LambdaFunctionStore(new InMemoryStorage<String, LambdaFunction>()),
@@ -13,37 +13,53 @@ class CodeStoreTest {
private static final String ACCOUNT_A = "111111111111";
private static final String ACCOUNT_B = "222222222222";
private static final String REGION = "us-east-1";
@Test
void sameFunctionNameInTwoAccountsResolvesToDistinctDirectories(@TempDir Path baseDir) {
CodeStore store = new CodeStore(baseDir);
Path a = store.getCodePath(ACCOUNT_A, "shared-name");
Path b = store.getCodePath(ACCOUNT_B, "shared-name");
Path a = store.getCodePath(ACCOUNT_A, REGION, "shared-name");
Path b = store.getCodePath(ACCOUNT_B, REGION, "shared-name");
assertNotEquals(a, b, "two accounts must not share one on-disk extraction directory");
assertTrue(a.startsWith(baseDir.resolve(ACCOUNT_A)));
assertTrue(b.startsWith(baseDir.resolve(ACCOUNT_B)));
}
@Test
void newRegionLayoutCannotNestInsideAlegacyRegionNamedFunction(@TempDir Path baseDir) throws IOException {
CodeStore store = new CodeStore(baseDir);
Path legacyFunction = baseDir.resolve(ACCOUNT_A).resolve(REGION);
Path newFunction = store.getCodePath(ACCOUNT_A, REGION, REGION);
writeHandler(legacyFunction, "legacy");
writeHandler(newFunction, "new");
assertFalse(newFunction.normalize().startsWith(legacyFunction.normalize()),
"new extraction paths must not be children of a legacy region-named function");
assertEquals("legacy", Files.readString(legacyFunction.resolve("index.js")));
assertEquals("new", Files.readString(newFunction.resolve("index.js")));
}
@Test
void deleteRemovesOnlyTheOwningAccountsCode(@TempDir Path baseDir) throws IOException {
CodeStore store = new CodeStore(baseDir);
writeHandler(store.getCodePath(ACCOUNT_A, "shared-name"), "a");
writeHandler(store.getCodePath(ACCOUNT_B, "shared-name"), "b");
writeHandler(store.getCodePath(ACCOUNT_A, REGION, "shared-name"), "a");
writeHandler(store.getCodePath(ACCOUNT_B, REGION, "shared-name"), "b");
store.delete(ACCOUNT_B, "shared-name");
store.delete(ACCOUNT_B, REGION, "shared-name");
assertTrue(store.exists(ACCOUNT_A, "shared-name"), "deleting B's code must not touch A's");
assertFalse(store.exists(ACCOUNT_B, "shared-name"));
assertEquals("a", Files.readString(store.getCodePath(ACCOUNT_A, "shared-name").resolve("index.js")));
assertTrue(store.exists(ACCOUNT_A, REGION, "shared-name"), "deleting B's code must not touch A's");
assertFalse(store.exists(ACCOUNT_B, REGION, "shared-name"));
assertEquals("a", Files.readString(store.getCodePath(ACCOUNT_A, REGION, "shared-name").resolve("index.js")));
}
@Test
void accountSegmentIsSanitizedLikeTheFunctionName(@TempDir Path baseDir) {
CodeStore store = new CodeStore(baseDir);
Path traversal = store.getCodePath("../../etc", "fn");
Path traversal = store.getCodePath("../../etc", REGION, "fn");
assertTrue(traversal.normalize().startsWith(baseDir.normalize()),
"a hostile account segment must not escape the base directory");
@@ -57,7 +73,7 @@ class CodeStoreTest {
// parent directory once handed to Path.resolve.
CodeStore store = new CodeStore(baseDir);
Path traversal = store.getCodePath("..", "fn");
Path traversal = store.getCodePath("..", REGION, "fn");
assertTrue(traversal.normalize().startsWith(baseDir.normalize()),
"a bare '..' account segment must not escape the base directory");
@@ -67,7 +83,7 @@ class CodeStoreTest {
void bareDotFunctionSegmentCannotResolveToTheAccountDirectoryItself(@TempDir Path baseDir) {
CodeStore store = new CodeStore(baseDir);
Path traversal = store.getCodePath(ACCOUNT_A, ".");
Path traversal = store.getCodePath(ACCOUNT_A, REGION, ".");
assertTrue(traversal.normalize().startsWith(baseDir.normalize()));
assertNotEquals(baseDir.resolve(ACCOUNT_A).normalize(), traversal.normalize(),
@@ -84,12 +100,12 @@ class CodeStoreTest {
CodeStore store = new CodeStore(baseDir);
Path legacyPath = baseDir.resolve("legacy-fn");
writeHandler(legacyPath, "legacy");
writeHandler(store.getCodePath(ACCOUNT_A, "legacy-fn"), "current");
writeHandler(store.getCodePath(ACCOUNT_A, REGION, "legacy-fn"), "current");
store.delete(ACCOUNT_A, "legacy-fn");
store.delete(ACCOUNT_A, REGION, "legacy-fn");
assertTrue(Files.exists(legacyPath), "delete() must not unilaterally remove the legacy directory");
assertFalse(store.exists(ACCOUNT_A, "legacy-fn"));
assertFalse(store.exists(ACCOUNT_A, REGION, "legacy-fn"));
}
@Test
@@ -113,44 +129,44 @@ class CodeStoreTest {
// character set sanitizeName can emit, which makes the two namespaces disjoint by
// construction rather than by a prefix match that has to guess where the name ends.
CodeStore store = new CodeStore(baseDir);
writeHandler(store.getVersionCodePath(ACCOUNT_A, "foo", "1"), "foo-v1");
writeHandler(store.getCodePath(ACCOUNT_A, "foo.v1"), "other-function");
writeHandler(store.getVersionCodePath(ACCOUNT_A, "foo.v1", "1"), "other-function-v1");
writeHandler(store.getVersionCodePath(ACCOUNT_A, REGION, "foo", "1"), "foo-v1");
writeHandler(store.getCodePath(ACCOUNT_A, REGION, "foo.v1"), "other-function");
writeHandler(store.getVersionCodePath(ACCOUNT_A, REGION, "foo.v1", "1"), "other-function-v1");
store.delete(ACCOUNT_A, "foo");
store.delete(ACCOUNT_A, REGION, "foo");
assertTrue(store.exists(ACCOUNT_A, "foo.v1"),
assertTrue(store.exists(ACCOUNT_A, REGION, "foo.v1"),
"deleting foo must not remove a function literally named foo.v1");
assertEquals("other-function",
Files.readString(store.getCodePath(ACCOUNT_A, "foo.v1").resolve("index.js")));
Files.readString(store.getCodePath(ACCOUNT_A, REGION, "foo.v1").resolve("index.js")));
assertEquals("other-function-v1",
Files.readString(store.getVersionCodePath(ACCOUNT_A, "foo.v1", "1").resolve("index.js")),
Files.readString(store.getVersionCodePath(ACCOUNT_A, REGION, "foo.v1", "1").resolve("index.js")),
"another function's published version code must survive too");
assertFalse(Files.exists(store.getVersionCodePath(ACCOUNT_A, "foo", "1")),
assertFalse(Files.exists(store.getVersionCodePath(ACCOUNT_A, REGION, "foo", "1")),
"foo's own version code must still be reclaimed");
}
@Test
void deleteVersionRemovesOneVersionAndLeavesTheRestInPlace(@TempDir Path baseDir) throws IOException {
CodeStore store = new CodeStore(baseDir);
writeHandler(store.getCodePath(ACCOUNT_A, "fn"), "latest");
writeHandler(store.getVersionCodePath(ACCOUNT_A, "fn", "1"), "one");
writeHandler(store.getVersionCodePath(ACCOUNT_A, "fn", "2"), "two");
writeHandler(store.getCodePath(ACCOUNT_A, REGION, "fn"), "latest");
writeHandler(store.getVersionCodePath(ACCOUNT_A, REGION, "fn", "1"), "one");
writeHandler(store.getVersionCodePath(ACCOUNT_A, REGION, "fn", "2"), "two");
store.deleteVersion(ACCOUNT_A, "fn", "1");
store.deleteVersion(ACCOUNT_A, REGION, "fn", "1");
assertFalse(Files.exists(store.getVersionCodePath(ACCOUNT_A, "fn", "1")));
assertFalse(Files.exists(store.getVersionCodePath(ACCOUNT_A, REGION, "fn", "1")));
assertEquals("two",
Files.readString(store.getVersionCodePath(ACCOUNT_A, "fn", "2").resolve("index.js")));
assertTrue(store.exists(ACCOUNT_A, "fn"), "$LATEST's code must be untouched");
Files.readString(store.getVersionCodePath(ACCOUNT_A, REGION, "fn", "2").resolve("index.js")));
assertTrue(store.exists(ACCOUNT_A, REGION, "fn"), "$LATEST's code must be untouched");
}
@Test
void versionCodeLivesOutsideTheDirectoryExtractionReplaces(@TempDir Path baseDir) {
CodeStore store = new CodeStore(baseDir);
Path latest = store.getCodePath(ACCOUNT_A, "fn");
Path version = store.getVersionCodePath(ACCOUNT_A, "fn", "1");
Path latest = store.getCodePath(ACCOUNT_A, REGION, "fn");
Path version = store.getVersionCodePath(ACCOUNT_A, REGION, "fn", "1");
assertFalse(version.normalize().startsWith(latest.normalize()),
"extraction replaces $LATEST's directory wholesale, taking any nested version with it");