From b5f29c8434bd5a77f7a3e16bdadfc5c10e55bd92 Mon Sep 17 00:00:00 2001 From: Simone <138866055+SimoneMarconi@users.noreply.github.com> Date: Thu, 19 Nov 2026 02:27:15 +0100 Subject: [PATCH] fix(ec2): Returning origin-facing prefix list for ec2 (#8652) DescribeManagedPrefixLists and GetManagedPrefixListEntries now include the AWS-managed com.amazonaws.global.cloudfront.origin-facing list (pl-3b927c52) with the 49 CLOUDFRONT_ORIGIN_FACING IPv4 ranges of ip-ranges.json, in commercial-partition regions only; China, GovCloud and the other partitions keep only the S3 and DynamoDB lists, and an accepted unknown region takes the deployment's partition. Like those lists, its MaxEntries is its entry count; the security-group rule quota is tracked in #8790. A prefix-list catalog that already declares the list keeps loading, the built-in list serving it. Closes #8634 --- docs/services/ec2.md | 9 +- .../io/floci/aws/services/ec2/Ec2Service.java | 69 +++++++++-- .../PartitionRegionGatesIntegrationTest.java | 4 +- ...beRegionsUnknownRegionIntegrationTest.java | 19 ++- .../Ec2ManagedPrefixListIntegrationTest.java | 29 ++++- .../aws/services/ec2/Ec2ServiceTest.java | 112 ++++++++++++++++-- 6 files changed, 216 insertions(+), 26 deletions(-) diff --git a/docs/services/ec2.md b/docs/services/ec2.md index 7cbb6fbef3..28c2b385c6 100644 --- a/docs/services/ec2.md +++ b/docs/services/ec2.md @@ -682,9 +682,12 @@ matching how AWS itself responds. Two AWS-managed prefix lists exist in every region without being created — `com.amazonaws..s3` (`pl-63a5400a`) and `com.amazonaws..dynamodb` -(`pl-02cd2c6b`) — matching the gateway endpoint services on AWS. They are owned by `AWS`, -read-only, and served by both `DescribePrefixLists` and `DescribeManagedPrefixLists`; -modifying or deleting one returns `UnsupportedOperation`. +(`pl-02cd2c6b`) — matching the gateway endpoint services on AWS. Every region of the +commercial `aws` partition also has `com.amazonaws.global.cloudfront.origin-facing` +(`pl-3b927c52`), holding the CloudFront origin-facing ranges published in +`ip-ranges.json`; like on AWS, it is absent from the China and GovCloud partitions. These +lists are owned by `AWS`, read-only, and served by both `DescribePrefixLists` and +`DescribeManagedPrefixLists`; modifying or deleting one returns `UnsupportedOperation`. Additional regional AWS-managed lists can be supplied through `FLOCI_SERVICES_EC2_MANAGED_PREFIX_LIST_CATALOG_PATH`. Set it to a readable JSON diff --git a/src/main/java/io/floci/aws/services/ec2/Ec2Service.java b/src/main/java/io/floci/aws/services/ec2/Ec2Service.java index 5ce3f430e2..abb5ef2818 100644 --- a/src/main/java/io/floci/aws/services/ec2/Ec2Service.java +++ b/src/main/java/io/floci/aws/services/ec2/Ec2Service.java @@ -16,6 +16,7 @@ import io.floci.aws.core.common.ContainerTeardown; import io.floci.aws.core.common.Digests; import io.floci.aws.core.common.PaginatedResult; import io.floci.aws.core.common.Pagination; +import io.floci.aws.core.common.RegionResolver; import io.floci.aws.core.common.RequestContext; import io.floci.aws.core.common.RequestScopes; import io.floci.aws.core.common.Resettable; @@ -345,6 +346,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab private final Map> capacityGroupInstanceIds = new HashMap<>(); // CDI Instance clashes with the EC2 Instance model imported above. jakarta.enterprise.inject.Instance resourceGroupsServices; + // Null in the test-shaped constructors, which fall back to the region's own partition. + RegionResolver regionResolver; private final StorageBackend hosts; private final StorageBackend egressOnlyInternetGateways; // Serialises a launch onto a host against ModifyHosts/ReleaseHosts, so a host cannot be @@ -616,7 +619,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab Ec2EbsEncryptionService ebsEncryptionService, jakarta.enterprise.inject.Instance vpcDependencyProviders, jakarta.enterprise.inject.Instance resourceGroupsServices, - jakarta.enterprise.inject.Instance launchTemplateSelections) { + jakarta.enterprise.inject.Instance launchTemplateSelections, + RegionResolver regionResolver) { this(config, containerManager, portForwardManager, amiImageResolver, imageCatalog, instanceTypeCatalog, storageFactory, requestContextInstance, iamService, volumeBlockDeviceManager); this.vpcNetworkManager = vpcNetworkManager; @@ -627,6 +631,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab this.vpcDependencyProviders = vpcDependencyProviders; this.resourceGroupsServices = resourceGroupsServices; this.launchTemplateSelections = launchTemplateSelections; + this.regionResolver = regionResolver; } public Ec2Service(EmulatorConfig config, Ec2ContainerManager containerManager, @@ -2039,9 +2044,10 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab networkAcls.delete(key(region, networkAclId)); } - // AWS-managed prefix lists for the gateway-endpoint services (S3, DynamoDB). These are - // not user-created, so they're returned as static managed data per region. Querying any - // other service name (e.g. an interface endpoint) correctly yields no match. + // AWS-managed prefix lists for the gateway-endpoint services (S3, DynamoDB) and the CloudFront + // origin-facing servers. These are not user-created, so they're returned as static managed + // data per region. Querying any other service name (e.g. an interface endpoint) correctly + // yields no match. // // The legacy DescribePrefixLists surface projects the same objects that // DescribeManagedPrefixLists serves, so the two APIs cannot report different CIDRs for the @@ -2076,6 +2082,21 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab private static final List RESERVED_PREFIX_LIST_NAME_PREFIXES = List.of("com.amazonaws.", "com.amazon.", "com.aws."); + private static final String CLOUDFRONT_ORIGIN_FACING_PREFIX_LIST_ID = "pl-3b927c52"; + private static final String CLOUDFRONT_ORIGIN_FACING_PREFIX_LIST_NAME = "com.amazonaws.global.cloudfront.origin-facing"; + /** The {@code CLOUDFRONT_ORIGIN_FACING} IPv4 ranges of ip-ranges.json (syncToken 1794843425). */ + private static final List CLOUDFRONT_ORIGIN_FACING_CIDRS = List.of( + "3.29.57.0/26", "3.172.0.0/18", "3.172.64.0/18", "13.124.199.0/24", "15.158.0.0/16", + "18.68.0.0/16", "24.110.128.0/17", "51.74.20.0/24", "51.74.21.0/24", "51.74.22.0/24", + "52.46.0.0/18", "52.82.128.0/23", "52.82.134.0/23", "54.182.128.0/20", "54.182.144.0/21", + "54.182.154.0/23", "54.182.156.0/22", "54.182.160.0/21", "54.182.172.0/22", "54.182.176.0/21", + "54.182.184.0/22", "54.182.188.0/23", "54.182.224.0/21", "54.182.240.0/21", "54.182.248.0/22", + "54.239.134.0/23", "54.239.170.0/23", "54.239.204.0/22", "54.239.208.0/21", "64.252.64.0/18", + "64.252.128.0/18", "70.132.0.0/18", "130.176.0.0/18", "130.176.64.0/21", "130.176.72.0/22", + "130.176.76.0/24", "130.176.78.0/23", "130.176.80.0/22", "130.176.86.0/23", "130.176.88.0/21", + "130.176.96.0/19", "130.176.128.0/21", "130.176.136.0/23", "130.176.140.0/22", "130.176.144.0/20", + "130.176.160.0/19", "130.176.192.0/19", "204.246.166.0/24", "205.251.218.0/24"); + /** AWS applies the reserved-name rule to a rename as well as a create. */ private void requireUnreservedPrefixListName(String prefixListName) { for (String reserved : RESERVED_PREFIX_LIST_NAME_PREFIXES) { @@ -2087,9 +2108,10 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab } /** - * The managed lists belong to the S3 and DynamoDB gateway endpoints, which are named + * The S3 and DynamoDB lists belong to the gateway endpoints, which are named * {@code com.amazonaws..} in every partition (the CDK's * {@code GatewayVpcEndpointAwsService}); the reversed-suffix names apply to interface endpoints only. + * The CloudFront origin-facing list exists in the commercial partition only. */ private List awsManagedPrefixLists(String region) { List lists = new ArrayList<>(defaultAwsManagedPrefixLists(region)); @@ -2113,6 +2135,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab if (catalog == null) { throw new IllegalArgumentException("Expected a region-to-prefix-lists object"); } + Map> loaded = new HashMap<>(); for (Map.Entry> region : catalog.entrySet()) { if (region.getKey().isBlank() || region.getValue() == null) { throw new IllegalArgumentException("Expected a nonblank region and a list"); @@ -2123,7 +2146,16 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab ids.add(list.getPrefixListId()); names.add(list.getPrefixListName()); } + // Reserved in every partition: this runs before the deployment partition is injected. + ids.add(CLOUDFRONT_ORIGIN_FACING_PREFIX_LIST_ID); + names.add(CLOUDFRONT_ORIGIN_FACING_PREFIX_LIST_NAME); + List retained = new ArrayList<>(); for (PrefixList list : region.getValue()) { + if (isLegacyCloudFrontCatalogEntry(list)) { + LOG.infov("Ignoring catalog entry {0} in {1}: the CloudFront origin-facing list is built in", + CLOUDFRONT_ORIGIN_FACING_PREFIX_LIST_ID, region.getKey()); + continue; + } if (list == null || list.getPrefixListId() == null || !list.getPrefixListId().matches("pl-(?:[0-9a-f]{8}|[0-9a-f]{17})") || list.getPrefixListName() == null || list.getPrefixListName().isBlank() @@ -2143,20 +2175,38 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab } } list.setCidrs(List.copyOf(canonicalCidrs)); + retained.add(list); } + loaded.put(region.getKey(), List.copyOf(retained)); } - return Map.copyOf(catalog); + return Map.copyOf(loaded); } catch (IOException | IllegalArgumentException error) { throw new IllegalStateException("Invalid EC2 managed prefix list catalog: " + path.get(), error); } } + /** Catalogs written before the list was built in declared it themselves; the built-in now serves it. */ + private static boolean isLegacyCloudFrontCatalogEntry(PrefixList list) { + return list != null && CLOUDFRONT_ORIGIN_FACING_PREFIX_LIST_ID.equals(list.getPrefixListId()) + && CLOUDFRONT_ORIGIN_FACING_PREFIX_LIST_NAME.equals(list.getPrefixListName()); + } + private List defaultAwsManagedPrefixLists(String region) { - return List.of( + List lists = new ArrayList<>(List.of( awsManagedPrefixList(region, "pl-63a5400a", "com.amazonaws." + region + ".s3", List.of("52.216.0.0/15", "54.231.0.0/16")), awsManagedPrefixList(region, "pl-02cd2c6b", "com.amazonaws." + region + ".dynamodb", - List.of("3.218.182.0/24", "52.94.0.0/22"))); + List.of("3.218.182.0/24", "52.94.0.0/22")))); + if (AwsRegions.DEFAULT_PARTITION.equals(partitionForRegion(region))) { + lists.add(awsManagedPrefixList(region, CLOUDFRONT_ORIGIN_FACING_PREFIX_LIST_ID, + CLOUDFRONT_ORIGIN_FACING_PREFIX_LIST_NAME, CLOUDFRONT_ORIGIN_FACING_CIDRS)); + } + return lists; + } + + /** The partition a request in {@code region} is scoped to, an accepted unknown region taking the deployment's. */ + private String partitionForRegion(String region) { + return regionResolver != null ? regionResolver.partitionForRegion(region) : AwsRegions.partitionFor(region); } private ManagedPrefixList awsManagedPrefixList(String region, String id, String name, List cidrs) { @@ -2165,7 +2215,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab list.setPrefixListName(name); // AWS-managed lists are owned by AWS itself, not by the calling account. list.setOwnerId("AWS"); - list.setPrefixListArn(AwsArnUtils.Arn.of("ec2", region, "aws", "prefix-list/" + id).toString()); + list.setPrefixListArn(new AwsArnUtils.Arn(partitionForRegion(region), "ec2", region, "aws", + "prefix-list/" + id).toString()); list.setAddressFamily("IPv4"); list.setState("create-complete"); list.setMaxEntries(cidrs.size()); diff --git a/src/test/java/io/floci/aws/core/common/PartitionRegionGatesIntegrationTest.java b/src/test/java/io/floci/aws/core/common/PartitionRegionGatesIntegrationTest.java index 638d1451bf..af746ad407 100644 --- a/src/test/java/io/floci/aws/core/common/PartitionRegionGatesIntegrationTest.java +++ b/src/test/java/io/floci/aws/core/common/PartitionRegionGatesIntegrationTest.java @@ -17,6 +17,7 @@ import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.not; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; /** @@ -252,7 +253,7 @@ class PartitionRegionGatesIntegrationTest { /** * The AWS-managed prefix lists belong to the S3 and DynamoDB gateway endpoints, which keep * {@code com.amazonaws} in every partition; only interface endpoint names take the reversed - * suffix China lists. + * suffix China lists. The CloudFront origin-facing list is commercial-only. */ @Test void gatewayPrefixListsKeepComAmazonawsWhileChinaInterfaceNamesReverseTheSuffix() { @@ -264,6 +265,7 @@ class PartitionRegionGatesIntegrationTest { .extract().xmlPath().getList("DescribeManagedPrefixListsResponse.prefixListSet.item.prefixListName"); assertTrue(names.contains("com.amazonaws.cn-north-1.s3"), names.toString()); assertTrue(names.contains("com.amazonaws.cn-north-1.dynamodb"), names.toString()); + assertFalse(names.contains("com.amazonaws.global.cloudfront.origin-facing"), names.toString()); XmlPath endpointServices = given() .header("Authorization", PartitionMatrix.sigV4Auth("cn-north-1", "ec2")) diff --git a/src/test/java/io/floci/aws/services/ec2/Ec2DescribeRegionsUnknownRegionIntegrationTest.java b/src/test/java/io/floci/aws/services/ec2/Ec2DescribeRegionsUnknownRegionIntegrationTest.java index f9ff82d3b7..553fd47f63 100644 --- a/src/test/java/io/floci/aws/services/ec2/Ec2DescribeRegionsUnknownRegionIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/ec2/Ec2DescribeRegionsUnknownRegionIntegrationTest.java @@ -16,7 +16,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue; /** * A China deployment that allows unknown regions serves a region no partition publishes in its - * own partition, so DescribeRegions lists the China regions for it, not the commercial ones. + * own partition, so DescribeRegions lists the China regions for it, not the commercial ones, and + * the AWS-managed prefix lists are China's. */ @QuarkusTest @TestProfile(Ec2DescribeRegionsUnknownRegionIntegrationTest.ChinaAllowUnknownRegionsProfile.class) @@ -53,6 +54,22 @@ class Ec2DescribeRegionsUnknownRegionIntegrationTest { assertFalse(names.contains("cn-north-1"), names.toString()); } + @Test + void anUnknownRegionServesTheDeploymentPartitionsManagedPrefixLists() { + Response response = given() + .header("Authorization", PartitionMatrix.sigV4Auth(UNKNOWN, "ec2")) + .formParam("Action", "DescribeManagedPrefixLists") + .formParam("Version", "2016-11-15") + .when().post("/").then().statusCode(200).contentType("application/xml") + .extract().response(); + List names = response.xmlPath().getList("DescribeManagedPrefixListsResponse.prefixListSet.item.prefixListName"); + List arns = response.xmlPath().getList("DescribeManagedPrefixListsResponse.prefixListSet.item.prefixListArn"); + + assertTrue(names.contains("com.amazonaws." + UNKNOWN + ".s3"), names.toString()); + assertFalse(names.contains("com.amazonaws.global.cloudfront.origin-facing"), names.toString()); + assertTrue(arns.contains("arn:aws-cn:ec2:" + UNKNOWN + ":aws:prefix-list/pl-63a5400a"), arns.toString()); + } + public static final class ChinaAllowUnknownRegionsProfile implements QuarkusTestProfile { @Override public Map getConfigOverrides() { diff --git a/src/test/java/io/floci/aws/services/ec2/Ec2ManagedPrefixListIntegrationTest.java b/src/test/java/io/floci/aws/services/ec2/Ec2ManagedPrefixListIntegrationTest.java index 856541b923..93e72a5322 100644 --- a/src/test/java/io/floci/aws/services/ec2/Ec2ManagedPrefixListIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/ec2/Ec2ManagedPrefixListIntegrationTest.java @@ -6,6 +6,8 @@ import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.*; @@ -73,22 +75,41 @@ class Ec2ManagedPrefixListIntegrationTest { .body("DescribeManagedPrefixListsResponse.prefixListSet.item.prefixListName", equalTo(LIST_NAME)); } - @Test + @ParameterizedTest @Order(3) - void describeIncludesAwsManagedGatewayLists() { + @CsvSource({ + "com.amazonaws.us-east-1.s3, pl-63a5400a", + "com.amazonaws.global.cloudfront.origin-facing, pl-3b927c52" + }) + void describeIncludesAwsManagedLists(String prefixListName, String expectedId) { given() .formParam("Action", "DescribeManagedPrefixLists") .formParam("Filter.1.Name", "prefix-list-name") - .formParam("Filter.1.Value.1", "com.amazonaws.us-east-1.s3") + .formParam("Filter.1.Value.1", prefixListName) .header("Authorization", AUTH_HEADER) .when() .post("/") .then() .statusCode(200) - .body("DescribeManagedPrefixListsResponse.prefixListSet.item.prefixListId", equalTo("pl-63a5400a")) + .body("DescribeManagedPrefixListsResponse.prefixListSet.item.prefixListId", equalTo(expectedId)) .body("DescribeManagedPrefixListsResponse.prefixListSet.item.ownerId", equalTo("AWS")); } + @Test + @Order(3) + void getEntriesReturnsTheCloudFrontOriginFacingRanges() { + given() + .formParam("Action", "GetManagedPrefixListEntries") + .formParam("PrefixListId", "pl-3b927c52") + .header("Authorization", AUTH_HEADER) + .when() + .post("/") + .then() + .statusCode(200) + .body("GetManagedPrefixListEntriesResponse.entrySet.item.cidr", + hasItems("130.176.0.0/18", "15.158.0.0/16")); + } + @Test @Order(4) void getEntriesReturnsTheInitialEntry() { diff --git a/src/test/java/io/floci/aws/services/ec2/Ec2ServiceTest.java b/src/test/java/io/floci/aws/services/ec2/Ec2ServiceTest.java index eda2d6610a..750d8c6fa3 100644 --- a/src/test/java/io/floci/aws/services/ec2/Ec2ServiceTest.java +++ b/src/test/java/io/floci/aws/services/ec2/Ec2ServiceTest.java @@ -5,6 +5,7 @@ import com.github.dockerjava.api.DockerClient; import com.github.dockerjava.api.command.ListNetworksCmd; import io.floci.aws.config.EmulatorConfig; import io.floci.aws.core.common.AwsException; +import io.floci.aws.core.common.AwsPartitions; import io.floci.aws.core.common.PaginatedResult; import io.floci.aws.core.common.RegionResolver; import io.floci.aws.core.common.RequestContext; @@ -7717,7 +7718,7 @@ class Ec2ServiceTest { assertEquals(List.of("192.0.2.0/24"), service.describePrefixLists("eu-west-1", List.of(id), Map.of()) .getFirst().getCidrs()); assertEquals("192.0.2.0/24", service.getManagedPrefixListEntries("eu-west-1", id, null).getFirst().getCidr()); - assertEquals(3, service.describeManagedPrefixLists("eu-west-1", List.of(), Map.of()).size()); + assertEquals(4, service.describeManagedPrefixLists("eu-west-1", List.of(), Map.of()).size()); assertEquals("InvalidPrefixListID.NotFound", assertThrows(AwsException.class, () -> service.describeManagedPrefixLists("us-east-1", List.of(id), Map.of())).getErrorCode()); assertEquals("UnsupportedOperation", assertThrows(AwsException.class, @@ -7745,6 +7746,11 @@ class Ec2ServiceTest { "{\"eu-west-1\": [{\"prefixListId\":\"pl-12345678\",\"prefixListName\":\"example\",\"cidrs\":[\"invalid\"]}]}", "{\"eu-west-1\": [{\"prefixListId\":\"pl-63a5400a\",\"prefixListName\":\"example\",\"cidrs\":[\"192.0.2.0/24\"]}]}", "{\"eu-west-1\": [{\"prefixListId\":\"pl-12345678\",\"prefixListName\":\"com.amazonaws.eu-west-1.s3\"," + + "\"cidrs\":[\"192.0.2.0/24\"]}]}", + "{\"eu-west-1\": [{\"prefixListId\":\"pl-3b927c52\",\"prefixListName\":\"example\"," + + "\"cidrs\":[\"192.0.2.0/24\"]}]}", + "{\"eu-west-1\": [{\"prefixListId\":\"pl-12345678\"," + + "\"prefixListName\":\"com.amazonaws.global.cloudfront.origin-facing\"," + "\"cidrs\":[\"192.0.2.0/24\"]}]}" }) void invalidManagedPrefixListCatalogFailsStartup(String json, @TempDir Path temp) throws IOException { @@ -7758,6 +7764,36 @@ class Ec2ServiceTest { assertTrue(error.getMessage().contains(catalog.toString())); } + @Test + void legacyCloudFrontCatalogEntryGivesWayToTheBuiltIn(@TempDir Path temp) throws IOException { + Path catalog = temp.resolve("prefix-lists.json"); + Files.writeString(catalog, """ + {"eu-west-1": [ + {"prefixListId": "pl-3b927c52", "prefixListName": "com.amazonaws.global.cloudfront.origin-facing", + "cidrs": ["192.0.2.0/24"]}, + {"prefixListId": "pl-0123456789abcdef0", "prefixListName": "example-managed-service", + "cidrs": ["198.51.100.0/24"]}]} + """); + EmulatorConfig config = mockConfig(true); + when(config.services().ec2().enabled()).thenReturn(true); + when(config.services().ec2().managedPrefixListCatalogPath()).thenReturn(Optional.of(catalog.toString())); + + Ec2Service service = assertDoesNotThrow(() -> prefixListService(config)); + + List cloudFront = service.describeManagedPrefixLists("eu-west-1", + List.of("pl-3b927c52"), Map.of()); + assertEquals(1, cloudFront.size()); + assertEquals(49, cloudFront.getFirst().getMaxEntries()); + List cidrs = service.getManagedPrefixListEntries("eu-west-1", "pl-3b927c52", null).stream() + .map(PrefixListEntry::getCidr) + .toList(); + assertFalse(cidrs.contains("192.0.2.0/24"), cidrs.toString()); + assertTrue(cidrs.contains("130.176.0.0/18"), cidrs.toString()); + assertEquals(List.of("198.51.100.0/24"), service.describePrefixLists("eu-west-1", + List.of("pl-0123456789abcdef0"), Map.of()).getFirst().getCidrs()); + assertEquals(4, service.describeManagedPrefixLists("eu-west-1", List.of(), Map.of()).size()); + } + @Test void missingManagedPrefixListCatalogFailsStartup(@TempDir Path temp) { EmulatorConfig config = mockConfig(true); @@ -7951,13 +7987,13 @@ class Ec2ServiceTest { service.createManagedPrefixList("us-east-1", "corp", "IPv4", 5, List.of(), List.of()); List east = service.describeManagedPrefixLists("us-east-1", List.of(), Map.of()); - assertEquals(3, east.size()); + assertEquals(4, east.size()); assertTrue(east.stream().anyMatch(l -> "com.amazonaws.us-east-1.s3".equals(l.getPrefixListName()))); assertTrue(east.stream().anyMatch(l -> "corp".equals(l.getPrefixListName()))); - // The customer list belongs to us-east-1; only the AWS-managed pair shows up elsewhere. + // The customer list belongs to us-east-1; only the AWS-managed lists show up elsewhere. List west = service.describeManagedPrefixLists("us-west-2", List.of(), Map.of()); - assertEquals(2, west.size()); + assertEquals(3, west.size()); assertTrue(west.stream().allMatch(ManagedPrefixList::isAwsManaged)); assertTrue(west.stream().anyMatch(l -> "com.amazonaws.us-west-2.s3".equals(l.getPrefixListName()))); } @@ -8120,20 +8156,80 @@ class Ec2ServiceTest { assertEquals("PrefixListVersionMismatch", error.getErrorCode()); } - @Test - void awsManagedListsCannotBeModifiedOrDeleted() { + @ParameterizedTest + @ValueSource(strings = {"pl-63a5400a", "pl-3b927c52"}) + void awsManagedListsCannotBeModifiedOrDeleted(String prefixListId) { Ec2Service service = prefixListService(); AwsException modifyError = assertThrows(AwsException.class, () -> - service.modifyManagedPrefixList("us-east-1", "pl-63a5400a", null, "hijacked", null, + service.modifyManagedPrefixList("us-east-1", prefixListId, null, "hijacked", null, List.of(), List.of())); assertEquals("UnsupportedOperation", modifyError.getErrorCode()); AwsException deleteError = assertThrows(AwsException.class, () -> - service.deleteManagedPrefixList("us-east-1", "pl-63a5400a")); + service.deleteManagedPrefixList("us-east-1", prefixListId)); assertEquals("UnsupportedOperation", deleteError.getErrorCode()); } + @Test + void cloudFrontOriginFacingListExistsInEveryCommercialRegion() { + Ec2Service service = prefixListService(); + Map> byName = Map.of("prefix-list-name", + List.of("com.amazonaws.global.cloudfront.origin-facing")); + + for (String region : List.of("us-east-1", "eu-west-1", "ap-southeast-2")) { + List found = service.describeManagedPrefixLists(region, List.of(), byName); + assertEquals(1, found.size(), region); + ManagedPrefixList list = found.getFirst(); + assertEquals("pl-3b927c52", list.getPrefixListId()); + assertEquals("AWS", list.getOwnerId()); + assertTrue(list.isAwsManaged()); + assertEquals("IPv4", list.getAddressFamily()); + assertEquals(49, list.getMaxEntries()); + assertEquals("arn:aws:ec2:" + region + ":aws:prefix-list/pl-3b927c52", list.getPrefixListArn()); + } + + List cidrs = service.getManagedPrefixListEntries("us-east-1", "pl-3b927c52", null).stream() + .map(PrefixListEntry::getCidr) + .toList(); + assertTrue(cidrs.containsAll(List.of("130.176.0.0/18", "15.158.0.0/16", "64.252.64.0/18")), cidrs.toString()); + assertEquals(49, cidrs.size(), cidrs.toString()); + } + + @Test + void cloudFrontOriginFacingListIsAbsentOutsideTheCommercialPartition() { + Ec2Service service = prefixListService(); + + for (String region : List.of("cn-north-1", "us-gov-west-1")) { + assertTrue(service.describeManagedPrefixLists(region, List.of(), Map.of()).stream() + .noneMatch(l -> "pl-3b927c52".equals(l.getPrefixListId())), region); + assertEquals("InvalidPrefixListID.NotFound", assertThrows(AwsException.class, + () -> service.getManagedPrefixListEntries(region, "pl-3b927c52", null)).getErrorCode()); + } + } + + @Test + void anUnknownRegionTakesTheDeploymentPartitionForTheCloudFrontList() { + String unknown = "polygondwanaland-west-1"; + assertTrue(AwsPartitions.forRegion(unknown).isEmpty()); + Map expectedPartitions = Map.of( + "us-gov-west-1", "aws-us-gov", + "cn-north-1", "aws-cn", + "us-east-1", "aws"); + + for (Map.Entry deployment : expectedPartitions.entrySet()) { + Ec2Service service = prefixListService(); + service.regionResolver = new RegionResolver(deployment.getKey(), "000000000000"); + List lists = service.describeManagedPrefixLists(unknown, List.of(), Map.of()); + + boolean commercial = "aws".equals(deployment.getValue()); + assertEquals(commercial, lists.stream().anyMatch(l -> "pl-3b927c52".equals(l.getPrefixListId())), + deployment.getKey()); + assertTrue(lists.stream().allMatch(l -> l.getPrefixListArn().startsWith( + "arn:" + deployment.getValue() + ":ec2:" + unknown + ":aws:prefix-list/")), lists.toString()); + } + } + @Test void deleteRemovesTheListFromDescribe() { Ec2Service service = prefixListService();