mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(route53): resolve private hosted zone records from cluster containers (#4353)
Implement DnsRecordSource SPI via Route53DnsRecordSource to decouple Route 53 resolution from EmbeddedDnsServer. Add DnsLookupHelper in core.common.dns for external IPv4 resolution via upstream forwarders with system fallback, shared by Route53DnsRecordSource for CNAME and ALIAS chasing. Discover private hosted zones via Route53Service and support in-zone chasing, built-in Floci suffixes, and EC2 internal names. Authoritatively resolve A records within private zones to prevent upstream query leakage. Wire EKS cluster containers to embedded DNS via specBuilder.withEmbeddedDns().
This commit is contained in:
1 parent
84363f9570
commit
4032efd9c7
15 files changed
+1401
-52
No files matched your search
@@ -0,0 +1,141 @@
|
||||
package io.github.hectorvent.floci.core.common.dns;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class DnsLookupHelperTest {
|
||||
|
||||
@Test
|
||||
void parseARecordsRejectsMismatchedTxId() {
|
||||
byte[] resp = buildSyntheticDnsResponse((short) 100, "example.com", "93.184.216.34");
|
||||
List<String> result = DnsLookupHelper.parseARecordsFromDnsResponse(
|
||||
resp, resp.length, (short) 200, "example.com");
|
||||
assertTrue(result.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseARecordsRejectsMismatchedQuestionName() {
|
||||
byte[] resp = buildSyntheticDnsResponse((short) 100, "other.com", "93.184.216.34");
|
||||
List<String> result = DnsLookupHelper.parseARecordsFromDnsResponse(
|
||||
resp, resp.length, (short) 100, "example.com");
|
||||
assertTrue(result.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseARecordsAcceptsValidMatchingResponse() {
|
||||
byte[] resp = buildSyntheticDnsResponse((short) 100, "example.com", "93.184.216.34");
|
||||
List<String> result = DnsLookupHelper.parseARecordsFromDnsResponse(
|
||||
resp, resp.length, (short) 100, "example.com");
|
||||
assertEquals(List.of("93.184.216.34"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseARecordsAcceptsCnameTargetBeforeCnameRecord() {
|
||||
ByteBuffer buf = ByteBuffer.allocate(512);
|
||||
short txId = 100;
|
||||
buf.putShort(txId);
|
||||
buf.putShort((short) 0x8180);
|
||||
buf.putShort((short) 1);
|
||||
buf.putShort((short) 2);
|
||||
buf.putShort((short) 0);
|
||||
buf.putShort((short) 0);
|
||||
|
||||
writeDnsName(buf, "alias.com");
|
||||
buf.putShort((short) 1);
|
||||
buf.putShort((short) 1);
|
||||
|
||||
writeDnsName(buf, "target.com");
|
||||
buf.putShort((short) 1);
|
||||
buf.putShort((short) 1);
|
||||
buf.putInt(300);
|
||||
buf.putShort((short) 4);
|
||||
buf.put((byte) 93);
|
||||
buf.put((byte) 184);
|
||||
buf.put((byte) 216);
|
||||
buf.put((byte) 34);
|
||||
|
||||
writeDnsName(buf, "alias.com");
|
||||
buf.putShort((short) 5);
|
||||
buf.putShort((short) 1);
|
||||
buf.putInt(300);
|
||||
int rdLengthPos = buf.position();
|
||||
buf.putShort((short) 0);
|
||||
int rdataStart = buf.position();
|
||||
writeDnsName(buf, "target.com");
|
||||
int rdataEnd = buf.position();
|
||||
buf.putShort(rdLengthPos, (short) (rdataEnd - rdataStart));
|
||||
|
||||
byte[] resp = new byte[buf.position()];
|
||||
System.arraycopy(buf.array(), 0, resp, 0, resp.length);
|
||||
|
||||
List<String> result = DnsLookupHelper.parseARecordsFromDnsResponse(
|
||||
resp, resp.length, txId, "alias.com");
|
||||
assertEquals(List.of("93.184.216.34"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseARecordsHandlesCyclicCompressionPointersWithoutStackOverflow() {
|
||||
ByteBuffer buf = ByteBuffer.allocate(512);
|
||||
short txId = 100;
|
||||
buf.putShort(txId);
|
||||
buf.putShort((short) 0x8180);
|
||||
buf.putShort((short) 1);
|
||||
buf.putShort((short) 0);
|
||||
buf.putShort((short) 0);
|
||||
buf.putShort((short) 0);
|
||||
|
||||
int cycleOffset = buf.position();
|
||||
buf.putShort((short) (0xC000 | cycleOffset));
|
||||
buf.putShort((short) 1);
|
||||
buf.putShort((short) 1);
|
||||
|
||||
byte[] resp = new byte[buf.position()];
|
||||
System.arraycopy(buf.array(), 0, resp, 0, resp.length);
|
||||
|
||||
List<String> result = DnsLookupHelper.parseARecordsFromDnsResponse(
|
||||
resp, resp.length, txId, "example.com");
|
||||
assertTrue(result.isEmpty());
|
||||
}
|
||||
|
||||
private void writeDnsName(ByteBuffer buf, String name) {
|
||||
for (String label : name.split("\\.")) {
|
||||
byte[] bytes = label.getBytes(StandardCharsets.US_ASCII);
|
||||
buf.put((byte) bytes.length);
|
||||
buf.put(bytes);
|
||||
}
|
||||
buf.put((byte) 0);
|
||||
}
|
||||
|
||||
private byte[] buildSyntheticDnsResponse(short txId, String questionName, String ip) {
|
||||
ByteBuffer buf = ByteBuffer.allocate(512);
|
||||
buf.putShort(txId);
|
||||
buf.putShort((short) 0x8180);
|
||||
buf.putShort((short) 1);
|
||||
buf.putShort((short) 1);
|
||||
buf.putShort((short) 0);
|
||||
buf.putShort((short) 0);
|
||||
|
||||
writeDnsName(buf, questionName);
|
||||
buf.putShort((short) 1);
|
||||
buf.putShort((short) 1);
|
||||
|
||||
writeDnsName(buf, questionName);
|
||||
buf.putShort((short) 1);
|
||||
buf.putShort((short) 1);
|
||||
buf.putInt(300);
|
||||
buf.putShort((short) 4);
|
||||
for (String octet : ip.split("\\.")) {
|
||||
buf.put((byte) Integer.parseInt(octet));
|
||||
}
|
||||
|
||||
byte[] result = new byte[buf.position()];
|
||||
System.arraycopy(buf.array(), 0, result, 0, result.length);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,8 @@ import io.github.hectorvent.floci.services.ec2.model.Instance;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mockito;
|
||||
@@ -316,6 +318,38 @@ class EksClusterManagerTest {
|
||||
"io.floci.region", "us-east-1"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {true, false})
|
||||
void startClusterEmbeddedDnsFlag(boolean embeddedDns) {
|
||||
EmulatorConfig config = Mockito.mock(EmulatorConfig.class, Mockito.RETURNS_DEEP_STUBS);
|
||||
when(config.services().eks().defaultImage()).thenReturn("rancher/k3s:v1.30.0-k3s1");
|
||||
when(config.services().eks().embeddedDns()).thenReturn(embeddedDns);
|
||||
|
||||
ContainerLifecycleManager lifecycleManager = Mockito.mock(ContainerLifecycleManager.class, Mockito.RETURNS_DEEP_STUBS);
|
||||
when(lifecycleManager.create(any())).thenReturn("container-id");
|
||||
|
||||
ContainerBuilder containerBuilder = Mockito.mock(ContainerBuilder.class);
|
||||
ContainerBuilder.Builder builder = Mockito.mock(ContainerBuilder.Builder.class, Mockito.RETURNS_SELF);
|
||||
when(containerBuilder.newContainer(anyString())).thenReturn(builder);
|
||||
when(builder.build()).thenReturn(Mockito.mock(ContainerSpec.class));
|
||||
|
||||
EksClusterManager manager = new EksClusterManager(containerBuilder, lifecycleManager,
|
||||
Mockito.mock(ContainerDetector.class), Mockito.mock(PortAllocator.class),
|
||||
Mockito.mock(DockerHostResolver.class), Mockito.mock(EcrRegistryManager.class),
|
||||
config, new RegionResolver("us-east-1", "000000000000"));
|
||||
|
||||
Cluster cluster = new Cluster();
|
||||
cluster.setName("my-cluster");
|
||||
|
||||
manager.startCluster(cluster);
|
||||
|
||||
if (embeddedDns) {
|
||||
verify(builder).withEmbeddedDns();
|
||||
} else {
|
||||
verify(builder, never()).withEmbeddedDns();
|
||||
}
|
||||
}
|
||||
|
||||
/** Re-latching persisted clusters after a Floci/Docker restart (#2609), without a Docker daemon. */
|
||||
@Nested
|
||||
class RestoreCluster {
|
||||
@@ -601,42 +635,6 @@ class EksClusterManagerTest {
|
||||
assertEquals("floci-aws-eks-999999999999.demo", cluster.getDockerName());
|
||||
verify(lifecycleManager).removeIfExists("floci-aws-eks-999999999999.demo");
|
||||
}
|
||||
|
||||
@Test
|
||||
void startClusterRegistersNodeInstanceAndNotifiesListener() {
|
||||
stubFreshStart("cid-new", 6440);
|
||||
List<Instance> registered = new ArrayList<>();
|
||||
manager.addNodeRegistrationListener(registered::add);
|
||||
|
||||
Cluster cluster = cluster();
|
||||
manager.startCluster(cluster);
|
||||
|
||||
Instance inst = manager.getRegisteredClusterNodeInstance(cluster);
|
||||
assertNotNull(inst);
|
||||
assertEquals("cid-new", inst.getDockerContainerId());
|
||||
assertEquals(1, registered.size());
|
||||
assertEquals(inst.getInstanceId(), registered.getFirst().getInstanceId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoreClusterRegistersNodeInstanceAndNotifiesListener() {
|
||||
when(lifecycleManager.findByName("floci-eks-demo"))
|
||||
.thenReturn(Optional.of(survivingContainer("cid-1")));
|
||||
when(lifecycleManager.adopt("cid-1", List.of(6443)))
|
||||
.thenReturn(new ContainerInfo("cid-1", Map.of(), Map.of(6443, 6512)));
|
||||
|
||||
List<Instance> registered = new ArrayList<>();
|
||||
manager.addNodeRegistrationListener(registered::add);
|
||||
|
||||
Cluster cluster = cluster();
|
||||
manager.restoreCluster(cluster);
|
||||
|
||||
Instance inst = manager.getRegisteredClusterNodeInstance(cluster);
|
||||
assertNotNull(inst);
|
||||
assertEquals("cid-1", inst.getDockerContainerId());
|
||||
assertEquals(1, registered.size());
|
||||
assertEquals(inst.getInstanceId(), registered.getFirst().getInstanceId());
|
||||
}
|
||||
}
|
||||
|
||||
/** Mirror-injection guard behavior, without a Docker daemon. */
|
||||
|
||||
+186
@@ -0,0 +1,186 @@
|
||||
package io.github.hectorvent.floci.services.eks;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.async.ResultCallback;
|
||||
import com.github.dockerjava.api.command.ExecCreateCmdResponse;
|
||||
import com.github.dockerjava.api.model.Frame;
|
||||
import com.github.dockerjava.api.model.StreamType;
|
||||
import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerSpec;
|
||||
import io.github.hectorvent.floci.services.route53.Route53Service;
|
||||
import io.github.hectorvent.floci.services.route53.model.ResourceRecord;
|
||||
import io.github.hectorvent.floci.services.route53.model.ResourceRecordSet;
|
||||
import io.github.hectorvent.floci.services.route53.model.VpcAssociation;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.QuarkusTestProfile;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Assumptions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(EksRoute53DnsDockerIntegrationTest.Profile.class)
|
||||
class EksRoute53DnsDockerIntegrationTest {
|
||||
|
||||
public static final class Profile implements QuarkusTestProfile {
|
||||
@Override
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
return Map.of(
|
||||
"floci.services.eks.embedded-dns", "true");
|
||||
}
|
||||
}
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(EksRoute53DnsDockerIntegrationTest.class);
|
||||
private static final String TEST_IMAGE = "alpine:3.21";
|
||||
|
||||
@Inject
|
||||
DockerClient dockerClient;
|
||||
|
||||
@Inject
|
||||
ContainerBuilder containerBuilder;
|
||||
|
||||
@Inject
|
||||
ContainerLifecycleManager lifecycleManager;
|
||||
|
||||
@Inject
|
||||
EmbeddedDnsServer embeddedDnsServer;
|
||||
|
||||
@Inject
|
||||
Route53Service route53Service;
|
||||
|
||||
private String containerId;
|
||||
private String zoneId;
|
||||
|
||||
@BeforeEach
|
||||
void requireDockerAndEmbeddedDns() {
|
||||
boolean dockerAvailable = isDockerAvailable();
|
||||
if (!dockerAvailable) {
|
||||
LOG.warn("Docker daemon is not available; skipping EksRoute53DnsDockerIntegrationTest");
|
||||
}
|
||||
Assumptions.assumeTrue(dockerAvailable, "Docker daemon must be available for EKS Route 53 DNS integration test");
|
||||
|
||||
boolean dnsAvailable = embeddedDnsServer.getServerIp().isPresent();
|
||||
if (!dnsAvailable) {
|
||||
LOG.warn("Embedded DNS server is not running or has no bound IP; skipping EksRoute53DnsDockerIntegrationTest");
|
||||
}
|
||||
Assumptions.assumeTrue(dnsAvailable, "Embedded DNS server must have a bound IP for Route 53 DNS integration test");
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
if (containerId != null) {
|
||||
try {
|
||||
lifecycleManager.stopAndRemove(containerId, null);
|
||||
} catch (Exception ignored) {
|
||||
// Best-effort cleanup of test container
|
||||
}
|
||||
}
|
||||
if (zoneId != null) {
|
||||
try {
|
||||
List<Map<String, Object>> deletes = route53Service.listResourceRecordSets(zoneId, null, null, 100).stream()
|
||||
.filter(rrs -> !"NS".equals(rrs.getType()) && !"SOA".equals(rrs.getType()))
|
||||
.map(rrs -> Map.<String, Object>of("action", "DELETE", "rrs", rrs))
|
||||
.toList();
|
||||
if (!deletes.isEmpty()) {
|
||||
route53Service.changeResourceRecordSets(zoneId, deletes, null);
|
||||
}
|
||||
route53Service.deleteHostedZone(zoneId);
|
||||
} catch (Exception ignored) {
|
||||
// Best-effort cleanup of test hosted zone
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void clusterContainerResolvesRoute53PrivateHostedZoneRecords() throws Exception {
|
||||
String testDomain = "corp-it-" + UUID.randomUUID().toString().substring(0, 8) + ".internal.";
|
||||
zoneId = route53Service.createHostedZone(
|
||||
testDomain,
|
||||
UUID.randomUUID().toString(),
|
||||
"EKS IT private zone",
|
||||
new VpcAssociation("vpc-12345", "us-east-1")).zone().getId();
|
||||
|
||||
ResourceRecordSet aRrs = new ResourceRecordSet();
|
||||
aRrs.setName("db." + testDomain);
|
||||
aRrs.setType("A");
|
||||
aRrs.setTtl(60L);
|
||||
aRrs.setRecords(List.of(new ResourceRecord("10.0.1.200")));
|
||||
route53Service.changeResourceRecordSets(zoneId, List.of(Map.of("action", "CREATE", "rrs", aRrs)), null);
|
||||
|
||||
ContainerSpec spec = containerBuilder.newContainer(TEST_IMAGE)
|
||||
.withName("floci-eks-dns-test-" + UUID.randomUUID().toString().substring(0, 8))
|
||||
.withEmbeddedDns()
|
||||
.withCmd(List.of("sh", "-c", "trap 'exit 0' TERM; sleep 300 & wait"))
|
||||
.build();
|
||||
|
||||
containerId = lifecycleManager.createAndStart(spec).containerId();
|
||||
assertNotNull(containerId, "Container ID must not be null");
|
||||
|
||||
String dnsServerIp = embeddedDnsServer.getServerIp().orElseThrow();
|
||||
|
||||
// Verify that /etc/resolv.conf inside the container has the embedded DNS server IP injected
|
||||
String resolvConf = execInContainer(containerId, new String[]{"cat", "/etc/resolv.conf"});
|
||||
assertTrue(resolvConf.contains(dnsServerIp), "Container /etc/resolv.conf must include embedded DNS server IP: " + resolvConf);
|
||||
|
||||
// Verify that standard resolver queries inside the container resolve the Route 53 private record
|
||||
String lookupCmd = "nslookup db." + testDomain.substring(0, testDomain.length() - 1);
|
||||
String output = execInContainer(containerId, new String[]{"sh", "-c", lookupCmd});
|
||||
assertNotNull(output, "Lookup output must not be null");
|
||||
assertTrue(output.contains("10.0.1.200"), "Lookup output must contain record IP: " + output);
|
||||
}
|
||||
|
||||
private boolean isDockerAvailable() {
|
||||
try {
|
||||
dockerClient.pingCmd().exec();
|
||||
return true;
|
||||
} catch (Exception expected) {
|
||||
// Docker daemon is not running or unreachable in this test environment
|
||||
LOG.debugv("Docker ping failed: {0}", expected.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private String execInContainer(String containerId, String[] cmd) throws Exception {
|
||||
ExecCreateCmdResponse exec = dockerClient.execCreateCmd(containerId)
|
||||
.withCmd(cmd).withAttachStdout(true).withAttachStderr(true).exec();
|
||||
|
||||
StringBuilder stdout = new StringBuilder();
|
||||
StringBuilder stderr = new StringBuilder();
|
||||
boolean completed = dockerClient.execStartCmd(exec.getId())
|
||||
.exec(new ResultCallback.Adapter<Frame>() {
|
||||
@Override
|
||||
public void onNext(Frame frame) {
|
||||
if (frame != null && frame.getPayload() != null) {
|
||||
String text = new String(frame.getPayload(), StandardCharsets.UTF_8);
|
||||
(frame.getStreamType() == StreamType.STDERR ? stderr : stdout).append(text);
|
||||
}
|
||||
}
|
||||
})
|
||||
.awaitCompletion(30, TimeUnit.SECONDS);
|
||||
|
||||
if (!completed) {
|
||||
throw new RuntimeException("exec timed out in container " + containerId);
|
||||
}
|
||||
Long exitCode = dockerClient.inspectExecCmd(exec.getId()).exec().getExitCodeLong();
|
||||
if (exitCode == null || exitCode != 0) {
|
||||
throw new RuntimeException("exec failed with code " + exitCode + ": " + stderr);
|
||||
}
|
||||
return stdout.toString();
|
||||
}
|
||||
}
|
||||
+356
@@ -0,0 +1,356 @@
|
||||
package io.github.hectorvent.floci.services.route53;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.dns.DnsLookupHelper;
|
||||
import io.github.hectorvent.floci.services.route53.model.AliasTarget;
|
||||
import io.github.hectorvent.floci.services.route53.model.ResourceRecord;
|
||||
import io.github.hectorvent.floci.services.route53.model.ResourceRecordSet;
|
||||
import io.github.hectorvent.floci.services.route53.model.VpcAssociation;
|
||||
import io.quarkus.test.InjectMock;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
class Route53DnsResolutionTest {
|
||||
|
||||
@Inject
|
||||
Route53Service route53Service;
|
||||
|
||||
@InjectMock
|
||||
DnsLookupHelper dnsLookupHelper;
|
||||
|
||||
@Inject
|
||||
Route53DnsRecordSource dnsRecordSource;
|
||||
|
||||
private final List<String> createdZoneIds = new ArrayList<>();
|
||||
|
||||
@AfterEach
|
||||
void cleanupZones() {
|
||||
for (String zoneId : createdZoneIds) {
|
||||
try {
|
||||
List<Map<String, Object>> deletes = route53Service.listResourceRecordSets(zoneId, null, null, 100).stream()
|
||||
.filter(rrs -> !"NS".equals(rrs.getType()) && !"SOA".equals(rrs.getType()))
|
||||
.map(rrs -> Map.<String, Object>of("action", "DELETE", "rrs", rrs))
|
||||
.toList();
|
||||
if (!deletes.isEmpty()) {
|
||||
route53Service.changeResourceRecordSets(zoneId, deletes, null);
|
||||
}
|
||||
route53Service.deleteHostedZone(zoneId);
|
||||
} catch (Exception ignored) {
|
||||
// Best-effort cleanup
|
||||
}
|
||||
}
|
||||
createdZoneIds.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolvesDirectARecordInPrivateHostedZone() {
|
||||
String zone = uniqueZone();
|
||||
String zoneId = createPrivateZone(zone);
|
||||
addRecord(zoneId, "api." + zone, "A", "10.0.1.100");
|
||||
|
||||
Optional<List<String>> result = dnsRecordSource.resolveIpv4("api." + zone);
|
||||
assertTrue(result.isPresent());
|
||||
assertEquals(List.of("10.0.1.100"), result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolvesMultipleARecords() {
|
||||
String zone = uniqueZone();
|
||||
String zoneId = createPrivateZone(zone);
|
||||
addRecords(zoneId, "api." + zone, "A", List.of("10.0.1.101", "10.0.1.102"));
|
||||
|
||||
Optional<List<String>> result = dnsRecordSource.resolveIpv4("api." + zone);
|
||||
assertTrue(result.isPresent());
|
||||
assertEquals(2, result.get().size());
|
||||
assertTrue(result.get().contains("10.0.1.101"));
|
||||
assertTrue(result.get().contains("10.0.1.102"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void answersWithAtMostEightRecords() {
|
||||
String zone = uniqueZone();
|
||||
String zoneId = createPrivateZone(zone);
|
||||
List<String> ips = new ArrayList<>();
|
||||
for (int i = 1; i <= 12; i++) {
|
||||
ips.add("10.0.1." + i);
|
||||
}
|
||||
addRecords(zoneId, "fleet." + zone, "A", ips);
|
||||
|
||||
Optional<List<String>> result = dnsRecordSource.resolveIpv4("fleet." + zone);
|
||||
assertTrue(result.isPresent());
|
||||
assertEquals(8, result.get().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolutionIsCaseInsensitiveAndToleratesATrailingDot() {
|
||||
String zone = uniqueZone();
|
||||
String zoneId = createPrivateZone(zone);
|
||||
addRecord(zoneId, "api." + zone, "A", "10.0.1.200");
|
||||
|
||||
Optional<List<String>> result = dnsRecordSource.resolveIpv4("API." + zone.toUpperCase() + ".");
|
||||
assertTrue(result.isPresent());
|
||||
assertEquals(List.of("10.0.1.200"), result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void doesNotResolvePublicHostedZone() {
|
||||
String zone = uniqueZone();
|
||||
String zoneId = route53Service.createHostedZone(
|
||||
zone + ".", UUID.randomUUID().toString(), "public zone", null).zone().getId();
|
||||
createdZoneIds.add(zoneId);
|
||||
addRecord(zoneId, "api." + zone, "A", "1.2.3.4");
|
||||
|
||||
Optional<List<String>> result = dnsRecordSource.resolveIpv4("api." + zone);
|
||||
assertTrue(result.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void doesNotResolveNameOutsideAnyPrivateZone() {
|
||||
assertTrue(dnsRecordSource.resolveIpv4("example.com").isEmpty());
|
||||
assertTrue(dnsRecordSource.resolveIpv4("").isEmpty());
|
||||
assertTrue(dnsRecordSource.resolveIpv4(null).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolvesWildcardRecordInPrivateZone() {
|
||||
String zone = uniqueZone();
|
||||
String zoneId = createPrivateZone(zone);
|
||||
addRecord(zoneId, "*." + zone, "A", "10.0.2.1");
|
||||
|
||||
Optional<List<String>> result = dnsRecordSource.resolveIpv4("app." + zone);
|
||||
assertTrue(result.isPresent());
|
||||
assertEquals(List.of("10.0.2.1"), result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void wildcardDoesNotMatchBeneathCloserExistingName() {
|
||||
String zone = uniqueZone();
|
||||
String zoneId = createPrivateZone(zone);
|
||||
addRecord(zoneId, "*." + zone, "A", "10.0.2.1");
|
||||
addRecord(zoneId, "b." + zone, "TXT", "\"text-record\"");
|
||||
|
||||
// Wildcard should not match a.b.zone because b.zone exists
|
||||
Optional<List<String>> result = dnsRecordSource.resolveIpv4("a.b." + zone);
|
||||
assertTrue(result.isPresent());
|
||||
assertEquals(List.of(), result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolvesPrivateCnameChainToARecord() {
|
||||
String zone = uniqueZone();
|
||||
String zoneId = createPrivateZone(zone);
|
||||
addRecord(zoneId, "api." + zone, "A", "10.0.3.50");
|
||||
addRecord(zoneId, "service." + zone, "CNAME", "api." + zone + ".");
|
||||
|
||||
Optional<List<String>> result = dnsRecordSource.resolveIpv4("service." + zone);
|
||||
assertTrue(result.isPresent());
|
||||
assertEquals(List.of("10.0.3.50"), result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void handlesCnameCycleGracefullyWithoutInfiniteLoop() {
|
||||
String zone = uniqueZone();
|
||||
String zoneId = createPrivateZone(zone);
|
||||
addRecord(zoneId, "cname1." + zone, "CNAME", "cname2." + zone);
|
||||
addRecord(zoneId, "cname2." + zone, "CNAME", "cname1." + zone);
|
||||
|
||||
Optional<List<String>> result = dnsRecordSource.resolveIpv4("cname1." + zone);
|
||||
assertTrue(result.isPresent());
|
||||
assertEquals(List.of(), result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolvesPrivateCnameToEc2PrivateDnsName() {
|
||||
String zone = uniqueZone();
|
||||
String zoneId = createPrivateZone(zone);
|
||||
addRecord(zoneId, "ec2host." + zone, "CNAME", "ip-10-0-5-88.ec2.internal.");
|
||||
|
||||
Optional<List<String>> result = dnsRecordSource.resolveIpv4("ec2host." + zone);
|
||||
assertTrue(result.isPresent());
|
||||
assertEquals(List.of("10.0.5.88"), result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolvesPrivateCnameToExternalHostname() {
|
||||
Mockito.when(dnsLookupHelper.resolveIpv4("external.example.com"))
|
||||
.thenReturn(List.of("93.184.216.34"));
|
||||
|
||||
String zone = uniqueZone();
|
||||
String zoneId = createPrivateZone(zone);
|
||||
addRecord(zoneId, "ext." + zone, "CNAME", "external.example.com.");
|
||||
|
||||
Optional<List<String>> result = dnsRecordSource.resolveIpv4("ext." + zone);
|
||||
assertTrue(result.isPresent());
|
||||
assertEquals(List.of("93.184.216.34"), result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolvesAliasRecordInPrivateZone() {
|
||||
String zone = uniqueZone();
|
||||
String zoneId = createPrivateZone(zone);
|
||||
addRecord(zoneId, "target." + zone, "A", "10.0.4.99");
|
||||
addAlias(zoneId, "alias." + zone, "A", "target." + zone + ".");
|
||||
|
||||
Optional<List<String>> result = dnsRecordSource.resolveIpv4("alias." + zone);
|
||||
assertTrue(result.isPresent());
|
||||
assertEquals(List.of("10.0.4.99"), result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void returnsEmptyListForNonExistentNameInPrivateZone() {
|
||||
String zone = uniqueZone();
|
||||
createPrivateZone(zone);
|
||||
|
||||
Optional<List<String>> result = dnsRecordSource.resolveIpv4("nonexistent." + zone);
|
||||
assertTrue(result.isPresent());
|
||||
assertEquals(List.of(), result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void returnsEmptyListForExistingNameWithoutARecord() {
|
||||
String zone = uniqueZone();
|
||||
String zoneId = createPrivateZone(zone);
|
||||
addRecord(zoneId, "txtonly." + zone, "TXT", "\"only text here\"");
|
||||
|
||||
Optional<List<String>> result = dnsRecordSource.resolveIpv4("txtonly." + zone);
|
||||
assertTrue(result.isPresent());
|
||||
assertEquals(List.of(), result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parentZoneDoesNotAnswerChildNamesIfChildZoneExists() {
|
||||
String parentZone = uniqueZone();
|
||||
String childZone = "sub." + parentZone;
|
||||
String parentId = createPrivateZone(parentZone);
|
||||
String childId = createPrivateZone(childZone);
|
||||
|
||||
addRecord(parentId, "api.sub." + parentZone, "A", "10.0.10.1");
|
||||
addRecord(childId, "api." + childZone, "A", "10.0.20.1");
|
||||
|
||||
Optional<List<String>> result = dnsRecordSource.resolveIpv4("api.sub." + parentZone);
|
||||
assertTrue(result.isPresent());
|
||||
assertEquals(List.of("10.0.20.1"), result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void cnameTargetToFlociBuiltinSuffixResolvesLocalAddress() throws Exception {
|
||||
String zone = uniqueZone();
|
||||
String zoneId = createPrivateZone(zone);
|
||||
addRecord(zoneId, "floci-cname." + zone, "CNAME", "s3.localhost.localstack.cloud");
|
||||
|
||||
Optional<List<String>> result = dnsRecordSource.resolveIpv4("floci-cname." + zone);
|
||||
assertTrue(result.isPresent());
|
||||
String expectedIp = InetAddress.getLocalHost().getHostAddress();
|
||||
assertEquals(List.of(expectedIp), result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void cnameTargetToDefaultFlociSuffixResolvesLocalAddress() throws Exception {
|
||||
String zone = uniqueZone();
|
||||
String zoneId = createPrivateZone(zone);
|
||||
addRecord(zoneId, "floci-cname2." + zone, "CNAME", "sqs.localhost.floci.io");
|
||||
|
||||
Optional<List<String>> result = dnsRecordSource.resolveIpv4("floci-cname2." + zone);
|
||||
assertTrue(result.isPresent());
|
||||
String expectedIp = InetAddress.getLocalHost().getHostAddress();
|
||||
assertEquals(List.of(expectedIp), result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aliasTargetToFlociBuiltinSuffixResolvesLocalAddress() throws Exception {
|
||||
String zone = uniqueZone();
|
||||
String zoneId = createPrivateZone(zone);
|
||||
addAlias(zoneId, "floci-alias." + zone, "A", "s3.localhost.localstack.cloud");
|
||||
|
||||
Optional<List<String>> result = dnsRecordSource.resolveIpv4("floci-alias." + zone);
|
||||
assertTrue(result.isPresent());
|
||||
String expectedIp = InetAddress.getLocalHost().getHostAddress();
|
||||
assertEquals(List.of(expectedIp), result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aliasTargetToBareLocalstackCloudDoesNotResolveLocalAddress() throws Exception {
|
||||
Mockito.when(dnsLookupHelper.resolveIpv4("s3.localstack.cloud"))
|
||||
.thenReturn(List.of("192.0.2.1"));
|
||||
|
||||
String zone = uniqueZone();
|
||||
String zoneId = createPrivateZone(zone);
|
||||
addAlias(zoneId, "bare-alias." + zone, "A", "s3.localstack.cloud");
|
||||
|
||||
Optional<List<String>> result = dnsRecordSource.resolveIpv4("bare-alias." + zone);
|
||||
assertTrue(result.isPresent());
|
||||
String localIp = InetAddress.getLocalHost().getHostAddress();
|
||||
assertFalse(result.get().contains(localIp));
|
||||
assertEquals(List.of("192.0.2.1"), result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aliasTargetToEc2PrivateDnsResolves() {
|
||||
String zone = uniqueZone();
|
||||
String zoneId = createPrivateZone(zone);
|
||||
addAlias(zoneId, "ec2-alias." + zone, "A", "ip-10-0-0-5.ec2.internal");
|
||||
|
||||
Optional<List<String>> result = dnsRecordSource.resolveIpv4("ec2-alias." + zone);
|
||||
assertTrue(result.isPresent());
|
||||
assertEquals(List.of("10.0.0.5"), result.get());
|
||||
}
|
||||
|
||||
private String createPrivateZone(String name) {
|
||||
String zoneName = name.endsWith(".") ? name : name + ".";
|
||||
String id = route53Service.createHostedZone(
|
||||
zoneName,
|
||||
UUID.randomUUID().toString(),
|
||||
"test private zone",
|
||||
new VpcAssociation("vpc-12345", "us-east-1")).zone().getId();
|
||||
createdZoneIds.add(id);
|
||||
return id;
|
||||
}
|
||||
|
||||
private void addRecord(String zoneId, String name, String type, String value) {
|
||||
ResourceRecordSet rrs = new ResourceRecordSet();
|
||||
rrs.setName(name);
|
||||
rrs.setType(type);
|
||||
rrs.setTtl(300L);
|
||||
rrs.setRecords(List.of(new ResourceRecord(value)));
|
||||
route53Service.changeResourceRecordSets(zoneId, List.of(Map.of("action", "CREATE", "rrs", rrs)), null);
|
||||
}
|
||||
|
||||
private void addRecords(String zoneId, String name, String type, List<String> values) {
|
||||
ResourceRecordSet rrs = new ResourceRecordSet();
|
||||
rrs.setName(name);
|
||||
rrs.setType(type);
|
||||
rrs.setTtl(300L);
|
||||
rrs.setRecords(values.stream().map(ResourceRecord::new).toList());
|
||||
route53Service.changeResourceRecordSets(zoneId, List.of(Map.of("action", "CREATE", "rrs", rrs)), null);
|
||||
}
|
||||
|
||||
private void addAlias(String zoneId, String name, String type, String targetDnsName) {
|
||||
ResourceRecordSet rrs = new ResourceRecordSet();
|
||||
rrs.setName(name);
|
||||
rrs.setType(type);
|
||||
AliasTarget alias = new AliasTarget();
|
||||
alias.setDnsName(targetDnsName);
|
||||
alias.setHostedZoneId(zoneId);
|
||||
alias.setEvaluateTargetHealth(false);
|
||||
rrs.setAliasTarget(alias);
|
||||
route53Service.changeResourceRecordSets(zoneId, List.of(Map.of("action", "CREATE", "rrs", rrs)), null);
|
||||
}
|
||||
|
||||
private static String uniqueZone() {
|
||||
return "r53dns" + UUID.randomUUID().toString().substring(0, 8) + ".internal";
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import io.github.hectorvent.floci.services.route53.model.AliasTarget;
|
||||
import io.github.hectorvent.floci.services.route53.model.ResourceRecord;
|
||||
import io.github.hectorvent.floci.services.route53.model.ResourceRecordSet;
|
||||
import io.github.hectorvent.floci.services.route53.model.VpcAssociation;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
@@ -22,7 +23,9 @@ import static org.hamcrest.Matchers.empty;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class Route53ServiceTest {
|
||||
|
||||
@@ -263,4 +266,37 @@ class Route53ServiceTest {
|
||||
assertThat(records, hasSize(1));
|
||||
assertEquals("5.6.7.8", records.get(0).getRecords().get(0).getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void clear_resetsPrivateZoneNameCounts() {
|
||||
Route53Service service = newService();
|
||||
String zoneId = service.createHostedZone("corp.internal.", "ref-clear", null,
|
||||
new VpcAssociation("vpc-123", "us-east-1")).zone().getId();
|
||||
|
||||
service.changeResourceRecordSets(zoneId,
|
||||
List.of(change("CREATE", aRecord("app.corp.internal.", 300, "10.0.0.1"))), null);
|
||||
|
||||
List<ResourceRecordSet> beforeClear = service.findPrivateRecordsForName("app.corp.internal.");
|
||||
assertThat(beforeClear, hasSize(1));
|
||||
|
||||
service.clear();
|
||||
|
||||
List<ResourceRecordSet> afterClear = service.findPrivateRecordsForName("app.corp.internal.");
|
||||
assertThat(afterClear, empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void isCoveredByPrivateZone_matchesPrivateZoneDomainsOnly() {
|
||||
Route53Service service = newService();
|
||||
service.createHostedZone("corp.internal.", "ref-private", null,
|
||||
new VpcAssociation("vpc-123", "us-east-1"));
|
||||
service.createHostedZone("public.com.", "ref-public", null, null);
|
||||
|
||||
assertTrue(service.isCoveredByPrivateZone("corp.internal"));
|
||||
assertTrue(service.isCoveredByPrivateZone("app.corp.internal."));
|
||||
assertTrue(service.isCoveredByPrivateZone("sub.service.corp.internal"));
|
||||
assertFalse(service.isCoveredByPrivateZone("public.com"));
|
||||
assertFalse(service.isCoveredByPrivateZone("othercorp.internal"));
|
||||
assertFalse(service.isCoveredByPrivateZone("example.org"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user