fix(ec2): use regional private DNS names (#5033)

* fix(ec2): use regional private DNS names

* fix(ec2): resolve private DNS for pattern regions
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-10-05 14:16:58 -04:00
1 parent e7ca2ece22
commit 4fb52e0b07
11 files changed
+150 -12

No files matched your search

@@ -72,6 +72,18 @@ public final class AwsRegions {
return AwsPartitions.forRegionOrCommercial(region).dnsSuffix();
}
/** The private EC2 DNS domain shared by instances, network interfaces, ECS and EKS. */
public static String ec2PrivateDnsDomain(String region) {
String effectiveRegion = region == null || region.isBlank() ? "us-east-1" : region; // partition-literal: legacy private DNS domain
return "us-east-1".equals(effectiveRegion) // partition-literal: ec2.internal is specific to us-east-1
? "ec2.internal" : effectiveRegion + ".compute.internal";
}
/** The private DNS name for an IPv4 EC2 address in the request's region. */
public static String ec2PrivateIpDnsName(String address, String region) {
return "ip-" + address.replace('.', '-') + "." + ec2PrivateDnsDomain(region);
}
private AwsRegions() {
}
}
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.core.common.dns;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.config.TlsConfigSource;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.docker.ContainerDetector;
import io.quarkus.runtime.Startup;
import io.vertx.core.Vertx;
@@ -74,7 +75,8 @@ public class EmbeddedDnsServer {
public static final String DEFAULT_SUFFIX = "localhost.floci.io";
public static final String LOCALSTACK_SUFFIX = "localhost.localstack.cloud";
private static final Pattern EC2_PRIVATE_DNS_NAME =
Pattern.compile("^ip-(\\d{1,3})-(\\d{1,3})-(\\d{1,3})-(\\d{1,3})\\.ec2\\.internal$", Pattern.CASE_INSENSITIVE);
Pattern.compile("^ip-(\\d{1,3})-(\\d{1,3})-(\\d{1,3})-(\\d{1,3})\\.(?:ec2|((?:[a-z0-9]+-)+\\d+)\\.compute)\\.internal$",
Pattern.CASE_INSENSITIVE);
// Well-known emulator wildcard DNS domains that always resolve to Floci's IP.
// The suffix "localhost.X" covers "localhost.X" itself and "*.localhost.X"; it does
@@ -425,6 +427,9 @@ public class EmbeddedDnsServer {
if (!matcher.matches()) {
return Optional.empty();
}
if (matcher.group(5) != null && !RegionResolver.isKnownRegion(matcher.group(5))) {
return Optional.empty();
}
StringBuilder address = new StringBuilder();
for (int i = 1; i <= 4; i++) {
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.ec2;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsRegions;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
import io.github.hectorvent.floci.core.common.docker.ContainerDetector;
@@ -1283,7 +1284,7 @@ public class Ec2ContainerManager {
return;
}
String privateDnsName = "ip-" + privateIp.replace('.', '-') + ".ec2.internal";
String privateDnsName = AwsRegions.ec2PrivateIpDnsName(privateIp, instance.getRegion());
instance.setPrivateIpAddress(privateIp);
instance.setPrivateDnsName(privateDnsName);
if (instance.getNetworkInterfaces() != null) {
@@ -3132,7 +3132,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
? associatePublicIp
: subnet != null && subnet.isMapPublicIpOnLaunch());
inst.setPrivateIpAddress(privateIp);
inst.setPrivateDnsName("ip-" + privateIp.replace('.', '-') + ".ec2.internal");
inst.setPrivateDnsName(AwsRegions.ec2PrivateIpDnsName(privateIp, region));
inst.setKeyName(keyName);
inst.setSecurityGroups(new ArrayList<>(sgIdentifiers));
inst.setArchitecture(architecture);
@@ -9936,7 +9936,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
String eniId = "eni-" + randomHex(17);
String primaryIp = (privateIpAddress != null && !privateIpAddress.isBlank())
? privateIpAddress : assignPrivateIp(region, subnetId);
String primaryDns = "ip-" + primaryIp.replace('.', '-') + ".ec2.internal";
String primaryDns = AwsRegions.ec2PrivateIpDnsName(primaryIp, region);
NetworkInterface ni = new NetworkInterface();
ni.setNetworkInterfaceId(eniId);
@@ -9967,7 +9967,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
}
NetworkInterfacePrivateIpAddress secondary = new NetworkInterfacePrivateIpAddress();
secondary.setPrivateIpAddress(extra);
secondary.setPrivateDnsName("ip-" + extra.replace('.', '-') + ".ec2.internal");
secondary.setPrivateDnsName(AwsRegions.ec2PrivateIpDnsName(extra, region));
secondary.setPrimary(false);
ipList.add(secondary);
}
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.ecs;
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.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.ec2.model.Subnet;
import io.github.hectorvent.floci.services.ec2.model.Vpc;
@@ -59,8 +60,6 @@ public class EcsTaskMetadataController {
private static final DateTimeFormatter TIMESTAMP = DateTimeFormatter.ISO_INSTANT;
private static final String SERVICE_GROUP_PREFIX = "service:";
/** The search domain an instance gets, which us-east-1 spells differently from every other region. */
private static final String LEGACY_SEARCH_DOMAIN_REGION = "us-east-1"; // partition-literal: ec2.internal is us-east-1's own search domain
/**
* Docker's stats document, written back out the way the daemon sent it. docker-java's model
@@ -506,7 +505,7 @@ public class EcsTaskMetadataController {
}
private static String searchDomain(String region) {
return LEGACY_SEARCH_DOMAIN_REGION.equals(region) ? "ec2.internal" : region + ".compute.internal";
return AwsRegions.ec2PrivateDnsDomain(region);
}
private static ContainerDefinition definitionOf(TaskDefinition taskDef, String containerName) {
@@ -2732,10 +2732,7 @@ public class EksClusterManager
}
String deriveClusterNodePrivateDnsDomain(String region) {
String safeRegion = (region != null && !region.isBlank()) ? region : "us-east-1"; // partition-literal: fallback for domain derivation
return "us-east-1".equals(safeRegion) // partition-literal: ec2.internal is us-east-1's own search domain
? "ec2.internal"
: safeRegion + ".compute.internal";
return AwsRegions.ec2PrivateDnsDomain(region);
}
String deriveClusterNodePrivateDnsName(Cluster cluster, String region, String accountId) {