diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaLayerService.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaLayerService.java index 15fa668f89..c0ac679c2f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaLayerService.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaLayerService.java @@ -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; } diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java index 8bde8e3789..45695e3ecb 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java @@ -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(".")) { diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaLayerAsyncAccountTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaLayerAsyncAccountTest.java index 605a6b4012..2adfd75383 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaLayerAsyncAccountTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaLayerAsyncAccountTest.java @@ -48,6 +48,20 @@ class LambdaLayerAsyncAccountTest { assertNull(service.resolveLayerByArnForAccount(ownerLayer.getLayerVersionArn(), OWNER, "cn-north-1")); } + @Test + void backgroundLookupInUnpublishedRegionUsesDeploymentPartition() { + String region = "xx-nowhere-9"; + AccountAwareStorageBackend 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 storage = AccountAwareStorageBackend.inMemory(DEFAULT_ACCOUNT); diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java index e5e1e17039..0b698d2373 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java @@ -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()); + service = new LambdaService(store, new WarmPool(), new CodeStore(Path.of("target/test-data/lambda-code")), + new ZipExtractor(), new RegionResolver("cn-north-1", "000000000000")); + Map request = baseRequest("perm-unpublished-principal-fn"); + request.put("Role", "arn:aws-cn:iam::000000000000:role/test-role"); + service.createFunction(region, request); + + Map 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