Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

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

Aniket Shukla4fb52e0b0722
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsRegions.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsRegions.java
index 4306bce0b8..3714292d21 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/AwsRegions.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsRegions.java
@@ -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() {
}
}
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServer.java b/src/main/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServer.java
index 995809c3fa..91c933cd84 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServer.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServer.java
@@ -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++) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManager.java
index ab2d2fc6fd..a48abf2b63 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManager.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManager.java
@@ -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) {
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 8008419b7f..2b0bbe5acb 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
@@ -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);
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/ecs/EcsTaskMetadataController.java b/src/main/java/io/github/hectorvent/floci/services/ecs/EcsTaskMetadataController.java
index 2f05ce3142..20bb00fca1 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ecs/EcsTaskMetadataController.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ecs/EcsTaskMetadataController.java
@@ -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) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java
index 248757111f..7256ff44b1 100644
--- a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java
+++ b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java
@@ -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) {
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/AwsRegionsTest.java b/src/test/java/io/github/hectorvent/floci/core/common/AwsRegionsTest.java
index 3e0b1d9ecf..1f05c2969e 100644
--- a/src/test/java/io/github/hectorvent/floci/core/common/AwsRegionsTest.java
+++ b/src/test/java/io/github/hectorvent/floci/core/common/AwsRegionsTest.java
@@ -14,6 +14,17 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
class AwsRegionsTest {
+ @ParameterizedTest
+ @CsvSource({
+ "us-east-1, ec2.internal",
+ "us-west-2, us-west-2.compute.internal",
+ "eu-central-1, eu-central-1.compute.internal"})
+ void ec2PrivateDnsNamesUseTheRegionalDomain(String region, String domain) {
+ assertEquals(domain, AwsRegions.ec2PrivateDnsDomain(region));
+ assertEquals("ip-10-24-34-0." + domain,
+ AwsRegions.ec2PrivateIpDnsName("10.24.34.0", region));
+ }
+
/**
* {@code ALL} is what the emulator advertises; {@code KNOWN_IDS} is what it recognises. The
* second must contain the first, or DescribeRegions could name a region that hostname parsing
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServerTest.java b/src/test/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServerTest.java
index ce75dd1713..d862b4d8be 100644
--- a/src/test/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServerTest.java
@@ -87,6 +87,22 @@ class EmbeddedDnsServerTest {
dns.resolveEc2PrivateDnsName("ip-172-16-128-9.ec2.internal").orElseThrow());
}
+ @Test
+ void resolveEc2PrivateDnsName_decodesRegionalIpName() {
+ assertEquals("172.16.128.9", dns.resolveEc2PrivateDnsName(
+ "ip-172-16-128-9.us-west-2.compute.internal").orElseThrow());
+ assertEquals("10.42.32.17", dns.resolveEc2PrivateDnsName(
+ "ip-10-42-32-17.eu-central-1.compute.internal").orElseThrow());
+ assertTrue(dns.resolveEc2PrivateDnsName("ip-172-16-128-9.invalid.compute.internal").isEmpty());
+ assertTrue(dns.resolveEc2PrivateDnsName("ip-172-16-128-9.my-cd-1.compute.internal").isEmpty());
+ }
+
+ @Test
+ void resolveEc2PrivateDnsName_decodesUnpublishedRegionIpName() {
+ assertEquals("10.0.0.5", dns.resolveEc2PrivateDnsName(
+ "ip-10-0-0-5.us-new-1.compute.internal").orElseThrow());
+ }
+
@Test
void resolveEc2PrivateDnsName_isCaseInsensitive() {
assertEquals(
@@ -106,6 +122,12 @@ class EmbeddedDnsServerTest {
dns.resolveARecord("ip-172-16-128-9.ec2.internal", "172.16.128.5").getFirst());
}
+ @Test
+ void resolveARecord_answersRegionalEc2PrivateDnsName() {
+ assertEquals("172.16.128.9", dns.resolveARecord(
+ "ip-172-16-128-9.us-west-2.compute.internal", "172.16.128.5").getFirst());
+ }
+
// ── resolveARecord: DnsRecordSource ───────────────────────────────────────
@Test
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManagerTest.java
index 53aaa785c5..5155e690e1 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManagerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManagerTest.java
@@ -124,6 +124,19 @@ class Ec2ContainerManagerTest {
assertEquals("ip-192-168-215-21.ec2.internal", networkInterface.getPrivateDnsName());
}
+ @Test
+ void exposeReachablePrivateAddressUsesInstanceRegion() {
+ Instance instance = new Instance();
+ instance.setRegion("us-west-2");
+ InstanceNetworkInterface networkInterface = new InstanceNetworkInterface();
+ instance.setNetworkInterfaces(List.of(networkInterface));
+
+ Ec2ContainerManager.exposeReachablePrivateAddress(instance, "192.168.215.21");
+
+ assertEquals("ip-192-168-215-21.us-west-2.compute.internal", instance.getPrivateDnsName());
+ assertEquals(instance.getPrivateDnsName(), networkInterface.getPrivateDnsName());
+ }
+
@Test
void exposeReachablePrivateAddressPreservesAllocatedIpWhenAwsFaithful() {
// #1983: with awsFaithfulPrivateIp=true, the CFN/subnet-allocated private
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2PrivateDnsRegionIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2PrivateDnsRegionIntegrationTest.java
new file mode 100644
index 0000000000..ac4ddafde9
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2PrivateDnsRegionIntegrationTest.java
@@ -0,0 +1,59 @@
+package io.github.hectorvent.floci.services.ec2;
+
+import io.github.hectorvent.floci.core.common.AwsRegions;
+import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.response.ExtractableResponse;
+import io.restassured.response.Response;
+import org.junit.jupiter.api.Test;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+@QuarkusTest
+class Ec2PrivateDnsRegionIntegrationTest {
+
+ @Test
+ void instanceAndNetworkInterfaceNamesUseTheRequestRegion() {
+ for (String region : new String[]{"us-east-1", "us-west-2"}) {
+ String auth = "AWS4-HMAC-SHA256 Credential=test/20261004/" + region + "/ec2/aws4_request";
+ String subnetId = given().formParam("Action", "DescribeSubnets")
+ .header("Authorization", auth)
+ .when().post("/").then().statusCode(200)
+ .extract().path("DescribeSubnetsResponse.subnetSet.item[0].subnetId");
+
+ ExtractableResponse<Response> instanceResponse = given().formParam("Action", "RunInstances")
+ .formParam("ImageId", "ami-0abcdef1234567890")
+ .formParam("InstanceType", "t3.micro")
+ .formParam("MinCount", "1")
+ .formParam("MaxCount", "1")
+ .formParam("SubnetId", subnetId)
+ .header("Authorization", auth)
+ .when().post("/").then().statusCode(200).extract();
+ String instanceId = instanceResponse.path("RunInstancesResponse.instancesSet.item.instanceId");
+ String privateIp = instanceResponse.path("RunInstancesResponse.instancesSet.item.privateIpAddress");
+ String expectedDnsName = AwsRegions.ec2PrivateIpDnsName(privateIp, region);
+ assertEquals(expectedDnsName,
+ instanceResponse.path("RunInstancesResponse.instancesSet.item.privateDnsName"));
+
+ given().formParam("Action", "DescribeInstances")
+ .formParam("InstanceId.1", instanceId)
+ .header("Authorization", auth)
+ .when().post("/").then().statusCode(200)
+ .body("DescribeInstancesResponse.reservationSet.item.instancesSet.item.privateDnsName",
+ equalTo(expectedDnsName));
+
+ ExtractableResponse<Response> interfaceResponse = given().formParam("Action", "CreateNetworkInterface")
+ .formParam("SubnetId", subnetId)
+ .header("Authorization", auth)
+ .when().post("/").then().statusCode(200).extract();
+ String interfaceIp = interfaceResponse.path(
+ "CreateNetworkInterfaceResponse.networkInterface.privateIpAddress");
+ String expectedInterfaceDns = AwsRegions.ec2PrivateIpDnsName(interfaceIp, region);
+ assertEquals(expectedInterfaceDns, interfaceResponse.path(
+ "CreateNetworkInterfaceResponse.networkInterface.privateDnsName"));
+ assertEquals(expectedInterfaceDns, interfaceResponse.path(
+ "CreateNetworkInterfaceResponse.networkInterface.privateIpAddressesSet.item.privateDnsName"));
+ }
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java
index 8bb3282296..f8d6276df1 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java
@@ -2105,6 +2105,25 @@ class Ec2ServiceTest {
assertTrue(addresses.contains("10.70.1.10"), "the counter must carry into the next /24 octet");
}
+ @Test
+ void standaloneNetworkInterfaceNamesAllPrivateIpsForTheRegion() {
+ Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
+ mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
+ new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory());
+ String vpcId = service.createVpc("us-west-2", "10.72.0.0/16", false).getVpcId();
+ String subnetId = service.createSubnet("us-west-2", vpcId, "10.72.0.0/24", "us-west-2a")
+ .getSubnetId();
+
+ NetworkInterface eni = service.createNetworkInterface("us-west-2", subnetId, null,
+ "10.72.0.10", List.of("10.72.0.11"), List.of(), List.of());
+
+ assertEquals("ip-10-72-0-10.us-west-2.compute.internal", eni.getPrivateDnsName());
+ assertEquals("ip-10-72-0-10.us-west-2.compute.internal",
+ eni.getPrivateIpAddresses().get(0).getPrivateDnsName());
+ assertEquals("ip-10-72-0-11.us-west-2.compute.internal",
+ eni.getPrivateIpAddresses().get(1).getPrivateDnsName());
+ }
+
@Test
void synthesisedAddressesSkipReservedOnesAndReportExhaustion() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),