fix(lambda): resolve partition for layer lookup and principals (#6947)

This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-10-29 16:32:20 -04:00
1 parent b73f643d3f
commit b063f7ff8c
4 files changed
+35 -5

No files matched your search

@@ -3,7 +3,6 @@ package io.github.hectorvent.floci.services.lambda;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.AwsRegions;
import io.github.hectorvent.floci.core.common.FileTrees;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.RegionResolver;
@@ -422,7 +421,7 @@ public class LambdaLayerService {
if (arn == null || accountId == null || accountId.isBlank()
|| functionRegion == null || functionRegion.isBlank()
|| !"lambda".equals(arn.service())
|| AwsArnUtils.isForeignPartition(arn, AwsRegions.partitionFor(functionRegion))
|| AwsArnUtils.isForeignPartition(arn, regionResolver.partitionForRegion(functionRegion))
|| AwsArnUtils.isForeignAccount(arn, accountId)) {
return null;
}
@@ -9,7 +9,6 @@ import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.AwsPartition;
import io.github.hectorvent.floci.core.common.AwsPartitions;
import io.github.hectorvent.floci.core.common.AwsRegions;
import io.github.hectorvent.floci.core.common.CustomResourceLiveness;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.Pagination;
@@ -3882,7 +3881,7 @@ public class LambdaService implements ResourceProvider {
* (such as {@code *}) is kept as given. ARNs are checked first because an IAM role, user or
* session name may contain a dot.
*/
private static Object permissionPrincipal(String principal, String region) {
private Object permissionPrincipal(String principal, String region) {
if (principal == null) {
return null;
}
@@ -3890,7 +3889,7 @@ public class LambdaService implements ResourceProvider {
return Map.of("AWS", principal);
}
if (ACCOUNT_ID_PATTERN.matcher(principal).matches()) {
String partition = AwsRegions.partitionFor(region);
String partition = regionResolver.partitionForRegion(region);
return Map.of("AWS", AwsArnUtils.Arn.global(partition, "iam", principal, "root").toString());
}
if (principal.contains(".")) {
@@ -48,6 +48,20 @@ class LambdaLayerAsyncAccountTest {
assertNull(service.resolveLayerByArnForAccount(ownerLayer.getLayerVersionArn(), OWNER, "cn-north-1"));
}
@Test
void backgroundLookupInUnpublishedRegionUsesDeploymentPartition() {
String region = "xx-nowhere-9";
AccountAwareStorageBackend<LambdaLayerVersion> storage = AccountAwareStorageBackend.inMemory(DEFAULT_ACCOUNT);
LambdaLayerService service = new LambdaLayerService(new LambdaLayerStore(storage), null, null,
new RegionResolver("cn-north-1", DEFAULT_ACCOUNT), null);
LambdaLayerVersion ownerLayer = layer("arn:aws-cn:lambda:" + region + ":" + OWNER + ":layer:shared:1");
storage.putForAccount(OWNER, "layer::" + region + "::shared::1", ownerLayer);
assertSame(ownerLayer, service.resolveLayerByArnForAccount(ownerLayer.getLayerVersionArn(), OWNER, region));
assertNull(service.resolveLayerByArnForAccount(
"arn:aws:lambda:" + region + ":" + OWNER + ":layer:shared:1", OWNER, region));
}
@Test
void absentLayerOrInvalidOwnerNeverFallsBackToAmbientStorage() {
AccountAwareStorageBackend<LambdaLayerVersion> storage = AccountAwareStorageBackend.inMemory(DEFAULT_ACCOUNT);
@@ -3281,6 +3281,24 @@ class LambdaServiceTest {
assertEquals(Map.of("AWS", "arn:aws-cn:iam::111122223333:root"), statement.get("Principal"));
}
@Test
void addPermission_accountIdInUnpublishedRegion_usesDeploymentPartition() {
String region = "xx-nowhere-9";
LambdaFunctionStore store = new LambdaFunctionStore(new InMemoryStorage<String, LambdaFunction>());
service = new LambdaService(store, new WarmPool(), new CodeStore(Path.of("target/test-data/lambda-code")),
new ZipExtractor(), new RegionResolver("cn-north-1", "000000000000"));
Map<String, Object> request = baseRequest("perm-unpublished-principal-fn");
request.put("Role", "arn:aws-cn:iam::000000000000:role/test-role");
service.createFunction(region, request);
Map<String, Object> statement = service.addPermission(region, "perm-unpublished-principal-fn", null, Map.of(
"StatementId", "cross-account",
"Action", "lambda:InvokeFunction",
"Principal", "111122223333"));
assertEquals(Map.of("AWS", "arn:aws-cn:iam::111122223333:root"), statement.get("Principal"));
}
@Test
void addPermission_functionUrlAuthTypeOutsideTheEnum_isRejectedAndLeavesNoStatement() {
// Catches: an unknown FunctionUrlAuthType stored as a condition no function URL can ever match