From b10b81d253ec4041b6184fb4956073b292e287a4 Mon Sep 17 00:00:00 2001 From: Matthew Ingle Date: Sat, 19 Sep 2026 20:45:58 +0100 Subject: [PATCH] feat(ec2): report DnsEntries for interface VPC endpoints (#3895) * feat(ec2): report DnsEntries for interface VPC endpoints CreateVpcEndpoint and DescribeVpcEndpoints answered without a dnsEntrySet, so an interface endpoint reported no DNS entries at all. A caller that follows the endpoint to the name it should point a Route 53 alias at has nothing to read: Terraform's aws_vpc_endpoint exposes the list positionally as dns_entry, so dns_entry[0].dns_name fails outright. An interface endpoint now reports the regional name, one name per availability zone it has a subnet in, and the service's own public name when private DNS is on. The names and their hosted zones are derived from the endpoint and its region rather than stored, so repeated describes and a restart keep answering with what the caller first saw, the same approach the endpoint's network interfaces already use. The service segment follows AWS's rule rather than the last dot-separated segment: com.amazonaws.us-east-1.ecr.api is reached at api.ecr.us-east-1.vpce.amazonaws.com. CloudFormation publishes the matching DnsEntries attribute, which closes its row in getatt-attribute-gaps.tsv. * fix(ec2): build endpoint DNS names from the region's partition suffix The names hard-coded amazonaws.com, so an endpoint in cn-north-1 reported ...vpce.amazonaws.com where AWS serves ...vpce.amazonaws.com.cn, and the same for the private DNS name. AwsRegions.dnsSuffixFor already carries the suffix per partition, as EksTokenMinter uses when it builds an STS hostname. Also covers the CloudFormation attribute end to end: a CreateStack whose Outputs take Fn::GetAtt on DnsEntries now asserts the pairing and the entry order, which the schema-coverage ratchet cannot check because it only matches provisioner source. * fix(ec2): answer the private DNS name AWS serves, not the service token Deriving the private name from the endpoint service token is wrong for the services whose two differ. com.amazonaws..kinesis-streams answers on kinesis..amazonaws.com and kinesis-firehose on firehose, the ECR registry name is the wildcard *.dkr.ecr..amazonaws.com, and the IoT data plane has no private name at all: AWS leaves private DNS off for it. Those four are the exceptions among the services DescribeVpcEndpointServices advertises, so the map covers that list rather than standing in for AWS's DNS catalogue. Everything else keeps deriving from the token, which is what AWS serves for it. * fix(ec2): answer the wildcard private DNS name for API Gateway A private REST API is invoked at {restapi-id}.execute-api..amazonaws.com, so the endpoint's private name is the wildcard *.execute-api..amazonaws.com rather than the bare regional name the service token produces. * fix(ec2): answer the private DNS names AWS serves for the ECS endpoints An ecs-agent or ecs-telemetry endpoint answered on its own service token, but AWS serves them at ecs-a and ecs-t. Cover every advertised service in the matrix test, and fail when one is advertised without a private DNS name decided for it. --- .../Ec2VpcEndpointCfnProvisioner.java | 4 + .../floci/services/ec2/Ec2QueryHandler.java | 11 + .../floci/services/ec2/Ec2Service.java | 91 +++++++ .../ec2/model/VpcEndpointDnsEntry.java | 12 + ...udFormationVpcEndpointIntegrationTest.java | 84 +++++- .../Ec2VpcEndpointCfnProvisionerTest.java | 21 ++ ...2VpcEndpointDnsEntriesIntegrationTest.java | 248 ++++++++++++++++++ .../cloudformation/getatt-attribute-gaps.tsv | 1 - 8 files changed, 468 insertions(+), 4 deletions(-) create mode 100644 src/main/java/io/github/hectorvent/floci/services/ec2/model/VpcEndpointDnsEntry.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VpcEndpointDnsEntriesIntegrationTest.java diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2VpcEndpointCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2VpcEndpointCfnProvisioner.java index 616f389f22..f96ea84606 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2VpcEndpointCfnProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2VpcEndpointCfnProvisioner.java @@ -12,6 +12,7 @@ import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; /** * CloudFormation provisioning for {@code AWS::EC2::VPCEndpoint}. @@ -68,6 +69,9 @@ public class Ec2VpcEndpointCfnProvisioner implements CfnResourceProvisioner { if (endpoint.getCreationTimestamp() != null) { r.getAttributes().put("CreationTimestamp", ISO_FMT.format(endpoint.getCreationTimestamp())); } + r.getAttributes().put("DnsEntries", ec2Service.endpointDnsEntries(endpoint).stream() + .map(entry -> entry.hostedZoneId() + ":" + entry.dnsName()) + .collect(Collectors.joining(","))); if (previousEndpointId != null && !previousEndpointId.equals(endpoint.getVpcEndpointId())) { deleteReplacedEndpoint(previousEndpointId, ctx.region()); } diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java index 2b9c6f1043..1ab5cb2445 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java @@ -5489,6 +5489,17 @@ public class Ec2QueryHandler { xml.start("item").elem("groupId", securityGroupId).end("item"); } xml.end("groupSet"); + List dnsEntries = service.endpointDnsEntries(endpoint); + if (!dnsEntries.isEmpty()) { + xml.start("dnsEntrySet"); + for (VpcEndpointDnsEntry entry : dnsEntries) { + xml.start("item") + .elem("dnsName", entry.dnsName()) + .elem("hostedZoneId", entry.hostedZoneId()) + .end("item"); + } + xml.end("dnsEntrySet"); + } xml.raw(tagSetXml(endpoint.getTags())); return xml.build(); } diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java index febf7b4973..7e294ce65b 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java @@ -13,11 +13,13 @@ import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.Random; import java.util.Set; +import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.BiPredicate; @@ -103,6 +105,7 @@ import io.github.hectorvent.floci.services.ec2.model.Vpc; import io.github.hectorvent.floci.services.ec2.model.VpcCidrBlockAssociation; import io.github.hectorvent.floci.services.ec2.model.VpcIpv6CidrBlockAssociation; import io.github.hectorvent.floci.services.ec2.model.VpcEndpoint; +import io.github.hectorvent.floci.services.ec2.model.VpcEndpointDnsEntry; import io.github.hectorvent.floci.services.ec2.model.VpcEndpointSubnetConfiguration; import io.github.hectorvent.floci.services.ec2.model.VpcPeeringConnection; import io.github.hectorvent.floci.services.ec2.model.VpcPeeringConnectionStateReason; @@ -143,6 +146,18 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider { Pattern.compile("^tgw-attach-[0-9a-f]{8}([0-9a-f]{9})?$"); private static final Set ROUTE_TABLE_MEMBERSHIP_FILTER_NAMES = Set.of("resource-id", "resource-type", "transit-gateway-attachment-id"); + /** + * Private DNS names that are not the token in the endpoint service name, empty for the + * services AWS serves no private name for. + */ + private static final Map PRIVATE_DNS_NAMES = Map.of( + "kinesis-streams", "kinesis", + "kinesis-firehose", "firehose", + "dkr.ecr", "*.dkr.ecr", + "execute-api", "*.execute-api", + "ecs-agent", "ecs-a", + "ecs-telemetry", "ecs-t", + "data.iot", ""); // A first launch may need to pull a large AMI-backed image. Keep a finite CloudFormation // bound, but allow enough time for that legitimate cold-start path before cancellation. private static final Duration CONTAINER_LAUNCH_TIMEOUT = Duration.ofMinutes(5); @@ -4012,6 +4027,82 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider { return "eni-" + hex.substring(0, 17); } + /** + * The {@code DnsEntries} an interface endpoint reports. The names and zones are synthesized + * deterministically rather than persisted, so a restart keeps answering with what the caller + * first saw, and the private-DNS entry carries its own zone because AWS creates one per + * endpoint. One class AWS also serves is not modelled, needing per-service metadata floci + * does not hold: the several private names a service such as S3 answers to. + */ + public List endpointDnsEntries(VpcEndpoint endpoint) { + if (!"Interface".equalsIgnoreCase(endpoint.getVpcEndpointType())) { + return List.of(); + } + String region = endpoint.getRegion(); + String serviceToken = endpointServiceToken(endpoint.getServiceName(), region); + String name = endpoint.getVpcEndpointId() + "-" + endpointDnsDiscriminator(endpoint.getVpcEndpointId()); + String dnsSuffix = AwsRegions.dnsSuffixFor(region); + String domain = serviceToken + "." + region + ".vpce." + dnsSuffix; + String hostedZoneId = vpceHostedZoneId(region); + + List entries = new ArrayList<>(); + entries.add(new VpcEndpointDnsEntry(name + "." + domain, hostedZoneId)); + endpoint.getSubnetIds().stream() + .map(subnetId -> subnets.get(key(region, subnetId)).orElse(null)) + .filter(Objects::nonNull) + .map(Subnet::getAvailabilityZone) + .filter(az -> az != null && !az.isBlank()) + .distinct() + .sorted() + .forEach(az -> entries.add(new VpcEndpointDnsEntry(name + "-" + az + "." + domain, hostedZoneId))); + if (endpoint.isPrivateDnsEnabled() && !serviceToken.startsWith("vpce-svc-")) { + String privateName = PRIVATE_DNS_NAMES.getOrDefault(serviceToken, serviceToken); + if (!privateName.isEmpty()) { + entries.add(new VpcEndpointDnsEntry(privateName + "." + region + "." + dnsSuffix, + privateDnsHostedZoneId(endpoint.getVpcEndpointId()))); + } + } + return entries; + } + + private static String endpointDnsDiscriminator(String endpointId) { + String hex = UUID.nameUUIDFromBytes(("vpce-dns|" + endpointId).getBytes(StandardCharsets.UTF_8)) + .toString().replace("-", ""); + return hex.substring(0, 8); + } + + /** + * The service segment of the name. AWS drops the {@code com.amazonaws.} prefix and + * reverses what is left, so {@code com.amazonaws.us-east-1.ecr.api} is reached at + * {@code api.ecr..vpce.amazonaws.com}. An endpoint service carries its + * {@code vpce-svc-*} identifier as the one trailing segment, which the same rule returns. + */ + private static String endpointServiceToken(String serviceName, String region) { + if (serviceName == null || serviceName.isBlank()) { + return "unknown"; + } + List parts = List.of(serviceName.split("\\.")); + int regionAt = region == null ? -1 : parts.indexOf(region); + if (regionAt < 0 || regionAt == parts.size() - 1) { + return parts.get(parts.size() - 1); + } + List serviceParts = new ArrayList<>(parts.subList(regionAt + 1, parts.size())); + Collections.reverse(serviceParts); + return String.join(".", serviceParts); + } + + private static String vpceHostedZoneId(String region) { + String hex = UUID.nameUUIDFromBytes(("vpce-zone|" + region).getBytes(StandardCharsets.UTF_8)) + .toString().replace("-", "").toUpperCase(Locale.ROOT); + return "Z" + hex.substring(0, 13); + } + + private static String privateDnsHostedZoneId(String endpointId) { + String hex = UUID.nameUUIDFromBytes(("vpce-private-zone|" + endpointId).getBytes(StandardCharsets.UTF_8)) + .toString().replace("-", "").toUpperCase(Locale.ROOT); + return "Z" + hex.substring(0, 13); + } + /** * The interface address for one of the endpoint's subnets. An address the caller pinned * through {@code SubnetConfiguration} wins outright: AWS fixes that address on the interface, diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/model/VpcEndpointDnsEntry.java b/src/main/java/io/github/hectorvent/floci/services/ec2/model/VpcEndpointDnsEntry.java new file mode 100644 index 0000000000..83084f5b8b --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/model/VpcEndpointDnsEntry.java @@ -0,0 +1,12 @@ +package io.github.hectorvent.floci.services.ec2.model; + +/** + * One {@code DnsEntry} of an interface VPC endpoint: a name the endpoint answers to, and the + * zone holding that name. + * + * @see AWS EC2 DnsEntry + */ +public record VpcEndpointDnsEntry( + String dnsName, + String hostedZoneId +) {} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationVpcEndpointIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationVpcEndpointIntegrationTest.java index 5ce4776448..62f88fcae8 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationVpcEndpointIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationVpcEndpointIntegrationTest.java @@ -6,6 +6,10 @@ import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.containsString; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * End-to-end check that CloudFormation provisions AWS::EC2::VPCEndpoint for @@ -67,9 +71,9 @@ class CloudFormationVpcEndpointIntegrationTest { .then() .statusCode(200) .extract().asString(); - org.junit.jupiter.api.Assertions.assertTrue(stackDescription.contains("CREATE_COMPLETE")); + assertTrue(stackDescription.contains("CREATE_COMPLETE")); String endpointId = XmlParser.extractFirst(stackDescription, "OutputValue", null); - org.junit.jupiter.api.Assertions.assertNotNull(endpointId); + assertNotNull(endpointId); given() .contentType("application/x-www-form-urlencoded") @@ -89,6 +93,80 @@ class CloudFormationVpcEndpointIntegrationTest { .then() .statusCode(200) .extract().asString(); - org.junit.jupiter.api.Assertions.assertFalse(endpointsAfterDelete.contains(endpointId)); + assertFalse(endpointsAfterDelete.contains(endpointId)); + } + + @Test + void createStackPublishesTheDnsEntriesAttribute() { + String stackName = "cfn-vpce-dns-" + Long.toString(System.nanoTime(), 36); + + String template = """ + { + "Resources": { + "Vpc": {"Type": "AWS::EC2::VPC", "Properties": {"CidrBlock": "10.62.0.0/16"}}, + "Subnet": { + "Type": "AWS::EC2::Subnet", + "Properties": { + "VpcId": {"Ref": "Vpc"}, + "CidrBlock": "10.62.1.0/24", + "AvailabilityZone": "us-east-1a" + } + }, + "Endpoint": { + "Type": "AWS::EC2::VPCEndpoint", + "Properties": { + "VpcId": {"Ref": "Vpc"}, + "ServiceName": "com.amazonaws.us-east-1.ecr.api", + "VpcEndpointType": "Interface", + "SubnetIds": [{"Ref": "Subnet"}] + } + } + }, + "Outputs": { + "Entries": {"Value": {"Fn::GetAtt": ["Endpoint", "DnsEntries"]}} + } + } + """; + + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", CFN_AUTH) + .formParam("Action", "CreateStack") + .formParam("StackName", stackName) + .formParam("TemplateBody", template) + .when() + .post("/") + .then() + .statusCode(200); + + String stackDescription = given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", CFN_AUTH) + .formParam("Action", "DescribeStacks") + .formParam("StackName", stackName) + .when() + .post("/") + .then() + .statusCode(200) + .extract().asString(); + + String entries = XmlParser.extractFirst(stackDescription, "OutputValue", null); + assertNotNull(entries); + String[] pairs = entries.split(","); + assertEquals(3, pairs.length, "regional, one zone, then private DNS, got " + entries); + assertTrue(pairs[0].matches("Z[0-9A-Z]+:vpce-[0-9a-f]+-[0-9a-f]+\\.api\\.ecr\\.us-east-1\\.vpce\\.amazonaws\\.com"), + "first entry should be hostedZoneId:regionalName, got " + pairs[0]); + assertTrue(pairs[1].contains("-us-east-1a."), + "second entry should be the zonal name, got " + pairs[1]); + + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", CFN_AUTH) + .formParam("Action", "DeleteStack") + .formParam("StackName", stackName) + .when() + .post("/") + .then() + .statusCode(200); } } diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2VpcEndpointCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2VpcEndpointCfnProvisionerTest.java index d4688df301..3b1ee980b4 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2VpcEndpointCfnProvisionerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2VpcEndpointCfnProvisionerTest.java @@ -9,6 +9,7 @@ import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplate import io.github.hectorvent.floci.services.cloudformation.model.StackResource; import io.github.hectorvent.floci.services.ec2.Ec2Service; import io.github.hectorvent.floci.services.ec2.model.VpcEndpoint; +import io.github.hectorvent.floci.services.ec2.model.VpcEndpointDnsEntry; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; @@ -125,6 +126,26 @@ class Ec2VpcEndpointCfnProvisionerTest { assertEquals("vpce-123", r.getPhysicalId()); assertEquals("vpce-123", r.getAttributes().get("Id")); assertEquals("2026-08-13T10:15:30.000Z", r.getAttributes().get("CreationTimestamp")); + assertEquals("", r.getAttributes().get("DnsEntries")); + } + + @Test + void dnsEntriesPairTheZoneWithTheNameAndJoinOnCommas() { + when(ec2.createVpcEndpoint(anyString(), anyString(), anyString(), anyString(), + anyList(), anyList(), anyList(), any(), any(), anyList())) + .thenReturn(endpoint("vpce-entries")); + when(ec2.endpointDnsEntries(any())).thenReturn(List.of( + new VpcEndpointDnsEntry("vpce-entries-ab12cd34.s3.us-east-1.vpce.amazonaws.com", "ZREGIONAL"), + new VpcEndpointDnsEntry("s3.us-east-1.amazonaws.com", "ZPRIVATE"))); + StackResource r = resource(); + + provisioner.provision(r, mapper.createObjectNode() + .put("VpcId", "vpc-1") + .put("ServiceName", "com.amazonaws.us-east-1.s3") + .put("VpcEndpointType", "Interface"), ctx()); + + assertEquals("ZREGIONAL:vpce-entries-ab12cd34.s3.us-east-1.vpce.amazonaws.com," + + "ZPRIVATE:s3.us-east-1.amazonaws.com", r.getAttributes().get("DnsEntries")); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VpcEndpointDnsEntriesIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VpcEndpointDnsEntriesIntegrationTest.java new file mode 100644 index 0000000000..a17ebacb86 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VpcEndpointDnsEntriesIntegrationTest.java @@ -0,0 +1,248 @@ +package io.github.hectorvent.floci.services.ec2; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.path.xml.XmlPath; +import io.restassured.specification.RequestSpecification; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.List; +import java.util.Map; +import java.util.stream.Stream; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.endsWith; +import static org.hamcrest.Matchers.everyItem; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.startsWith; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * The {@code DnsEntries} an interface endpoint reports, which is how a caller finds the name to + * point a Route 53 alias at. AWS orders them by class: regional, zonal, then the private-DNS + * name. Terraform's aws_vpc_endpoint exposes the list positionally as {@code dns_entry}, so the + * first entry has to be the regional one. + * + *

The service segment is not the last segment of the service name: AWS drops + * {@code com.amazonaws.} and reverses what is left, so an {@code ecr.api} endpoint is + * reached at {@code api.ecr..vpce.amazonaws.com}. + * + * @see DescribeVpcEndpoints + */ +@QuarkusTest +class Ec2VpcEndpointDnsEntriesIntegrationTest { + + private static final String AUTH_HEADER = + "AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/ec2/aws4_request"; + + private static final String CN_AUTH_HEADER = + "AWS4-HMAC-SHA256 Credential=test/20260205/cn-north-1/ec2/aws4_request"; + + private static final String ENTRIES = + "DescribeVpcEndpointsResponse.vpcEndpointSet.item.dnsEntrySet.item"; + + private String ec2Value(String action, String element, String... formParams) { + return ec2ValueWithHeader(AUTH_HEADER, action, element, formParams); + } + + private String ec2ValueWithHeader(String authHeader, String action, String element, String... formParams) { + RequestSpecification req = given().formParam("Action", action) + .header("Authorization", authHeader); + for (int i = 0; i < formParams.length; i += 2) { + req = req.formParam(formParams[i], formParams[i + 1]); + } + return req.when().post("/").then().statusCode(200).extract().path(element); + } + + private String createVpc(String cidr) { + return ec2Value("CreateVpc", "CreateVpcResponse.vpc.vpcId", "CidrBlock", cidr); + } + + private String createSubnet(String vpcId, String cidr, String availabilityZone) { + return ec2Value("CreateSubnet", "CreateSubnetResponse.subnet.subnetId", + "VpcId", vpcId, "CidrBlock", cidr, "AvailabilityZone", availabilityZone); + } + + private XmlPath describeEndpoint(String endpointId) { + return given() + .formParam("Action", "DescribeVpcEndpoints") + .formParam("VpcEndpointId.1", endpointId) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .extract().xmlPath(); + } + + private List dnsNamesOf(String endpointId) { + return describeEndpoint(endpointId).getList(ENTRIES + ".dnsName", String.class); + } + + private List hostedZoneIdsOf(String endpointId) { + return describeEndpoint(endpointId).getList(ENTRIES + ".hostedZoneId", String.class); + } + + @Test + void anInterfaceEndpointReportsTheRegionalNameThenEachZoneThenThePrivateDnsName() { + String vpcId = createVpc("10.70.0.0/16"); + String subnetA = createSubnet(vpcId, "10.70.1.0/24", "us-east-1a"); + String subnetB = createSubnet(vpcId, "10.70.2.0/24", "us-east-1b"); + + String endpointId = ec2Value("CreateVpcEndpoint", + "CreateVpcEndpointResponse.vpcEndpoint.vpcEndpointId", + "VpcId", vpcId, "ServiceName", "com.amazonaws.us-east-1.ecr.api", + "VpcEndpointType", "Interface", "SubnetId.1", subnetA, "SubnetId.2", subnetB); + + List names = dnsNamesOf(endpointId); + assertEquals(4, names.size(), "regional, one per zone, then private DNS, got " + names); + assertThat(names.get(0), startsWith(endpointId + "-")); + assertThat(names.get(0), endsWith(".api.ecr.us-east-1.vpce.amazonaws.com")); + assertThat(names.get(1), endsWith("-us-east-1a.api.ecr.us-east-1.vpce.amazonaws.com")); + assertEquals("api.ecr.us-east-1.amazonaws.com", names.get(3)); + + List zones = hostedZoneIdsOf(endpointId); + assertEquals(zones.get(0), zones.get(1), "the regional and zonal names share one zone"); + assertThat(zones.get(3), not(zones.get(0))); + + assertEquals(names, dnsNamesOf(endpointId)); + } + + @Test + void anEndpointServiceReportsItsOwnIdentifierAndNoPrivateDnsName() { + String vpcId = createVpc("10.72.0.0/16"); + String subnetId = createSubnet(vpcId, "10.72.1.0/24", "us-east-1a"); + + String endpointId = ec2Value("CreateVpcEndpoint", + "CreateVpcEndpointResponse.vpcEndpoint.vpcEndpointId", + "VpcId", vpcId, "ServiceName", "com.amazonaws.vpce.us-east-1.vpce-svc-0123456789abcdef0", + "VpcEndpointType", "Interface", "SubnetId.1", subnetId); + + List names = dnsNamesOf(endpointId); + assertEquals(2, names.size(), "regional and one zonal name, no private DNS, got " + names); + assertThat(names.get(0), endsWith(".vpce-svc-0123456789abcdef0.us-east-1.vpce.amazonaws.com")); + assertThat(names.get(1), + endsWith("-us-east-1a.vpce-svc-0123456789abcdef0.us-east-1.vpce.amazonaws.com")); + } + + /** + * What AWS puts in front of {@code ..amazonaws.com} in each advertised service's + * private DNS name, empty for a service with no private name. S3 is absent deliberately: + * it answers to several, the one class {@code endpointDnsEntries} does not model. + */ + private static final Map PRIVATE_DNS_PREFIX_BY_SERVICE = Map.ofEntries( + Map.entry("ec2", "ec2"), + Map.entry("ec2messages", "ec2messages"), + Map.entry("ssm", "ssm"), + Map.entry("ssmmessages", "ssmmessages"), + Map.entry("logs", "logs"), + Map.entry("monitoring", "monitoring"), + Map.entry("sts", "sts"), + Map.entry("secretsmanager", "secretsmanager"), + Map.entry("kms", "kms"), + Map.entry("ecr.api", "api.ecr"), + Map.entry("ecr.dkr", "*.dkr.ecr"), + Map.entry("ecs", "ecs"), + Map.entry("ecs-agent", "ecs-a"), + Map.entry("ecs-telemetry", "ecs-t"), + Map.entry("elasticloadbalancing", "elasticloadbalancing"), + Map.entry("sns", "sns"), + Map.entry("sqs", "sqs"), + Map.entry("kinesis-streams", "kinesis"), + Map.entry("kinesis-firehose", "firehose"), + Map.entry("states", "states"), + Map.entry("events", "events"), + Map.entry("lambda", "lambda"), + Map.entry("glue", "glue"), + Map.entry("athena", "athena"), + Map.entry("iot.data", ""), + Map.entry("execute-api", "*.execute-api")); + + static Stream advertisedServicePrivateDnsNames() { + return PRIVATE_DNS_PREFIX_BY_SERVICE.entrySet().stream() + .sorted(Map.Entry.comparingByKey()) + .map(entry -> Arguments.of( + "com.amazonaws.us-east-1." + entry.getKey(), + entry.getValue().isEmpty() ? "" : entry.getValue() + ".us-east-1.amazonaws.com")); + } + + @Test + void everyAdvertisedServiceHasAPrivateDnsNameDecision() { + List advertised = given() + .formParam("Action", "DescribeVpcEndpointServices") + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .extract().xmlPath() + .getList("DescribeVpcEndpointServicesResponse.serviceNameSet.item", String.class); + + List undecided = advertised.stream() + .map(name -> name.replace("com.amazonaws.us-east-1.", "")) + .filter(name -> !name.equals("s3")) + .filter(name -> !PRIVATE_DNS_PREFIX_BY_SERVICE.containsKey(name)) + .toList(); + assertEquals(List.of(), undecided, + "add these to PRIVATE_DNS_PREFIX_BY_SERVICE with the prefix AWS answers for them"); + } + + @ParameterizedTest(name = "{0}") + @MethodSource("advertisedServicePrivateDnsNames") + void thePrivateDnsNameIsTheOneAwsAnswersFor(String serviceName, String expectedPrivateName) { + String vpcId = createVpc("10.76.0.0/16"); + String subnetId = createSubnet(vpcId, "10.76.1.0/24", "us-east-1a"); + + String endpointId = ec2Value("CreateVpcEndpoint", + "CreateVpcEndpointResponse.vpcEndpoint.vpcEndpointId", + "VpcId", vpcId, "ServiceName", serviceName, + "VpcEndpointType", "Interface", "SubnetId.1", subnetId); + + List names = dnsNamesOf(endpointId); + if (expectedPrivateName.isEmpty()) { + assertEquals(2, names.size(), "regional and one zone, no private name, got " + names); + } else { + assertEquals(3, names.size(), "regional, one zone, then private name, got " + names); + assertEquals(expectedPrivateName, names.get(2)); + } + } + + @Test + void aChinaRegionEndpointUsesItsPartitionDnsSuffix() { + String vpcId = ec2ValueWithHeader(CN_AUTH_HEADER, "CreateVpc", "CreateVpcResponse.vpc.vpcId", + "CidrBlock", "10.95.0.0/16"); + String subnetId = ec2ValueWithHeader(CN_AUTH_HEADER, "CreateSubnet", "CreateSubnetResponse.subnet.subnetId", + "VpcId", vpcId, "CidrBlock", "10.95.1.0/24", "AvailabilityZone", "cn-north-1a"); + + List names = given() + .formParam("Action", "CreateVpcEndpoint") + .formParam("VpcId", vpcId) + .formParam("ServiceName", "com.amazonaws.cn-north-1.monitoring") + .formParam("VpcEndpointType", "Interface") + .formParam("SubnetId.1", subnetId) + .header("Authorization", CN_AUTH_HEADER) + .when().post("/") + .then().statusCode(200) + .extract().xmlPath() + .getList("CreateVpcEndpointResponse.vpcEndpoint.dnsEntrySet.item.dnsName", String.class); + + assertEquals(3, names.size(), "regional, one zone, then private DNS, got " + names); + assertThat(names, everyItem(endsWith(".amazonaws.com.cn"))); + } + + @Test + void aGatewayEndpointReportsNoDnsEntries() { + String vpcId = createVpc("10.71.0.0/16"); + String endpointId = ec2Value("CreateVpcEndpoint", + "CreateVpcEndpointResponse.vpcEndpoint.vpcEndpointId", + "VpcId", vpcId, "ServiceName", "com.amazonaws.us-east-1.s3", "VpcEndpointType", "Gateway"); + + String body = given() + .formParam("Action", "DescribeVpcEndpoints") + .formParam("VpcEndpointId.1", endpointId) + .header("Authorization", AUTH_HEADER) + .when().post("/") + .then().statusCode(200).extract().asString(); + assertThat(body, not(containsString("dnsEntrySet"))); + } +} diff --git a/src/test/resources/cloudformation/getatt-attribute-gaps.tsv b/src/test/resources/cloudformation/getatt-attribute-gaps.tsv index 4d5428b8fe..3bca27a4d1 100644 --- a/src/test/resources/cloudformation/getatt-attribute-gaps.tsv +++ b/src/test/resources/cloudformation/getatt-attribute-gaps.tsv @@ -16,7 +16,6 @@ AWS::EC2::Subnet NetworkAclAssociationId the subnet's default network ACL associ AWS::EC2::VPC CidrBlockAssociations needs association ids Floci's VPC model does not track AWS::EC2::VPC DefaultNetworkAcl needs a default network ACL Floci does not create with the VPC AWS::EC2::VPC Ipv6CidrBlocks IPv6 CIDR association is not emulated -AWS::EC2::VPCEndpoint DnsEntries endpoint DNS entries are not emulated AWS::EC2::VPCEndpoint NetworkInterfaceIds endpoint ENIs are not created AWS::EC2::VPCGatewayAttachment AttachmentType the exact literal AWS returns (internet vs vpn gateway) is not given by the schema AWS::Kinesis::Stream WarmThroughputObject warm throughput is not an emulated Kinesis feature