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.<region>.kinesis-streams answers on
kinesis.<region>.amazonaws.com and kinesis-firehose on firehose, the ECR
registry name is the wildcard *.dkr.ecr.<region>.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.<region>.amazonaws.com,
so the endpoint's private name is the wildcard *.execute-api.<region>.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.
This commit is contained in:
Matthew Ingle authored and GitHub committed 2026-09-19 15:45:58 -04:00
1 parent b9fab02849
commit b10b81d253
8 files changed
+468 -4

No files matched your search

@@ -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());
}
@@ -5489,6 +5489,17 @@ public class Ec2QueryHandler {
xml.start("item").elem("groupId", securityGroupId).end("item");
}
xml.end("groupSet");
List<VpcEndpointDnsEntry> 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();
}
@@ -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<String> 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<String, String> 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<VpcEndpointDnsEntry> 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<VpcEndpointDnsEntry> 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.<region>} prefix and
* reverses what is left, so {@code com.amazonaws.us-east-1.ecr.api} is reached at
* {@code api.ecr.<region>.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<String> 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<String> 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,
@@ -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 <a href="https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DnsEntry.html">AWS EC2 DnsEntry</a>
*/
public record VpcEndpointDnsEntry(
String dnsName,
String hostedZoneId
) {}