mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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:
1 parent
e7ca2ece22
commit
4fb52e0b07
11 files changed
+150
-12
No files matched your search
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
+59
@@ -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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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),
|
||||
|
||||
Reference in new issue
Block a user