mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(route53): validate stored ALB targets for public DNS aliases (#8942)
This commit is contained in:
1 parent
b388616273
commit
d28bdf664b
10 files changed
+558
-9
No files matched your search
@@ -18,6 +18,16 @@ public interface DnsRecordSource {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/** Supplies the receiving listener's address to sources that resolve local alias targets. */
|
||||
default Optional<DnsAnswer> resolveAuthoritative(String name, int type, String listenerAddress) {
|
||||
return resolveAuthoritative(name, type);
|
||||
}
|
||||
|
||||
/** Resolves a stored resource's alias target without recursive DNS or suffix substitution. */
|
||||
default Optional<DnsAnswer> resolveAliasTarget(String name, String hostedZoneId, int type, String listenerAddress) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a query name to IPv4 addresses and their TTL. An empty optional means this
|
||||
* source does not own the name. The name arrives without a trailing dot and in the case the client sent.
|
||||
|
||||
@@ -377,8 +377,31 @@ public class EmbeddedDnsServer {
|
||||
return resolveRecordWithOwnership(name, myIp, 1);
|
||||
}
|
||||
|
||||
/** Only one stored resource may claim an alias target; failures cannot fall through to DNS. */
|
||||
public Optional<DnsAnswer> resolveAliasTarget(String name, String hostedZoneId, int type, String listenerAddress) {
|
||||
DnsAnswer resolved = null;
|
||||
if (recordSources != null) {
|
||||
for (DnsRecordSource source : recordSources) {
|
||||
try {
|
||||
Optional<DnsAnswer> candidate = source.resolveAliasTarget(name, hostedZoneId, type, listenerAddress);
|
||||
if (candidate.isPresent()) {
|
||||
if (resolved != null) {
|
||||
return Optional.of(DnsAnswer.refused());
|
||||
}
|
||||
resolved = candidate.orElseThrow();
|
||||
}
|
||||
} catch (RuntimeException failure) {
|
||||
LOG.debugv("DNS alias source {0} failed to resolve {1}: {2}",
|
||||
source.getClass().getSimpleName(), name, failure.getMessage());
|
||||
return Optional.of(DnsAnswer.refused());
|
||||
}
|
||||
}
|
||||
}
|
||||
return Optional.ofNullable(resolved);
|
||||
}
|
||||
|
||||
private Optional<DnsAnswer> resolveRecordWithOwnership(String name, String myIp, int type) {
|
||||
Optional<DnsAnswer> stored = resolveFromRecordSources(name, type, true);
|
||||
Optional<DnsAnswer> stored = resolveFromRecordSources(name, type, true, myIp);
|
||||
if (stored.isPresent()) {
|
||||
return stored;
|
||||
}
|
||||
@@ -416,7 +439,7 @@ public class EmbeddedDnsServer {
|
||||
* hosted zone that matches the same name.
|
||||
*/
|
||||
QueryPlan planQuery(String name, String clientAddress, String myIp, int type) {
|
||||
Optional<DnsAnswer> stored = resolveFromRecordSources(name, type, true);
|
||||
Optional<DnsAnswer> stored = resolveFromRecordSources(name, type, true, myIp);
|
||||
Optional<DnsAnswer> emulatorName = resolveEmulatorName(name, myIp, type);
|
||||
if (emulatorName.isPresent() && stored.isEmpty()) {
|
||||
return QueryPlan.answering(emulatorName.orElseThrow());
|
||||
@@ -509,10 +532,11 @@ public class EmbeddedDnsServer {
|
||||
* through to the upstream resolvers, which is what happened before any source existed.
|
||||
*/
|
||||
private Optional<DnsAnswer> resolveFromRecordSources(String name, int type) {
|
||||
return resolveFromRecordSources(name, type, false);
|
||||
return resolveFromRecordSources(name, type, false, null);
|
||||
}
|
||||
|
||||
private Optional<DnsAnswer> resolveFromRecordSources(String name, int type, boolean authoritative) {
|
||||
private Optional<DnsAnswer> resolveFromRecordSources(
|
||||
String name, int type, boolean authoritative, String listenerAddress) {
|
||||
if (recordSources == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
@@ -523,7 +547,7 @@ public class EmbeddedDnsServer {
|
||||
Optional<DnsAnswer> empty = Optional.empty();
|
||||
for (DnsRecordSource source : recordSources) {
|
||||
try {
|
||||
Optional<DnsAnswer> answer = authoritative ? source.resolveAuthoritative(name, type)
|
||||
Optional<DnsAnswer> answer = authoritative ? source.resolveAuthoritative(name, type, listenerAddress)
|
||||
: source.resolve(name, type);
|
||||
if (answer != null && answer.isPresent()) {
|
||||
if (answer.get().isEmpty()) {
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package io.floci.aws.services.elbv2;
|
||||
|
||||
import io.floci.aws.core.common.dns.DnsAnswer;
|
||||
import io.floci.aws.core.common.dns.DnsRecordSource;
|
||||
import io.floci.aws.services.elbv2.model.LoadBalancer;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/** Resolves an ALB alias to the emulator address serving its listeners. */
|
||||
@ApplicationScoped
|
||||
public class ElbV2DnsRecordSource implements DnsRecordSource {
|
||||
private final ElbV2Service service;
|
||||
|
||||
public ElbV2DnsRecordSource(ElbV2Service service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<DnsAnswer> resolveIpv4(String name) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<DnsAnswer> resolveAliasTarget(String name, String hostedZoneId, int type, String listenerAddress) {
|
||||
List<LoadBalancer> matches = service.findByDnsName(name);
|
||||
if (matches.isEmpty()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
if (matches.size() != 1) {
|
||||
return Optional.of(DnsAnswer.refused());
|
||||
}
|
||||
LoadBalancer target = matches.getFirst();
|
||||
if (!"application".equals(target.getType()) || hostedZoneId == null
|
||||
|| !hostedZoneId.equals(target.getCanonicalHostedZoneId()) || (type != 1 && type != 28)) {
|
||||
return Optional.of(DnsAnswer.refused());
|
||||
}
|
||||
if (type == 28 || listenerAddress == null || listenerAddress.isBlank()) {
|
||||
return Optional.of(DnsAnswer.noData());
|
||||
}
|
||||
return Optional.of(DnsAnswer.records(List.of(listenerAddress), 60));
|
||||
}
|
||||
}
|
||||
@@ -33,6 +33,7 @@ import java.net.InetAddress;
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -104,6 +105,22 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
|
||||
// tags: resource-ARN → {key → value}
|
||||
private Map<String, Map<String, String>> tags = new ConcurrentHashMap<>();
|
||||
|
||||
/** DNS selects the resource owner independently of a management request's account. */
|
||||
List<LoadBalancer> findByDnsName(String name) {
|
||||
if (name == null || name.isBlank()) {
|
||||
return List.of();
|
||||
}
|
||||
String normalized = name.endsWith(".") ? name.substring(0, name.length() - 1) : name;
|
||||
if (normalized.regionMatches(true, 0, "dualstack.", 0, "dualstack.".length())) {
|
||||
normalized = normalized.substring("dualstack.".length());
|
||||
}
|
||||
String target = normalized;
|
||||
Collection<Map<String, LoadBalancer>> regions = allAccounts(loadBalancers, loadBalancerStore);
|
||||
return regions.stream().flatMap(region -> region.values().stream())
|
||||
.filter(lb -> target.equalsIgnoreCase(lb.getDnsName()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
void initializeStorage()
|
||||
{
|
||||
|
||||
@@ -75,6 +75,11 @@ public class Route53DnsRecordSource implements DnsRecordSource {
|
||||
|
||||
@Override
|
||||
public Optional<DnsAnswer> resolveAuthoritative(String name, int type) {
|
||||
return resolveAuthoritative(name, type, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<DnsAnswer> resolveAuthoritative(String name, int type, String listenerAddress) {
|
||||
if (name == null || name.isBlank() || (type != 1 && type != 2 && type != 6 && type != 16 && type != 28)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
@@ -119,9 +124,9 @@ public class Route53DnsRecordSource implements DnsRecordSource {
|
||||
case 6 -> DnsRecord.Soa::new;
|
||||
default -> DnsRecord.Txt::new;
|
||||
};
|
||||
DnsAnswer answer = buildRecords(name, zone.matchingRecords().stream()
|
||||
DnsAnswer answer = buildPublicRecords(name, type, zone.matchingRecords().stream()
|
||||
.filter(record -> record.getType().equals(recordType)).toList(),
|
||||
recordFactory, Route53DnsRecordSource::publicTtlOf);
|
||||
recordFactory, listenerAddress);
|
||||
if (answer.responseCode() != 0) {
|
||||
return Optional.of(answer);
|
||||
}
|
||||
@@ -145,6 +150,32 @@ public class Route53DnsRecordSource implements DnsRecordSource {
|
||||
}
|
||||
}
|
||||
|
||||
private DnsAnswer buildPublicRecords(String name, int type, List<ResourceRecordSet> sets,
|
||||
Function<String, DnsRecord> recordFactory, String listenerAddress) {
|
||||
if (sets.stream().noneMatch(record -> record.getAliasTarget() != null)) {
|
||||
return buildRecords(name, sets, recordFactory, Route53DnsRecordSource::publicTtlOf);
|
||||
}
|
||||
if (sets.size() != 1 || embeddedDnsServer == null || (type != 1 && type != 28)) {
|
||||
return DnsAnswer.refused();
|
||||
}
|
||||
ResourceRecordSet record = sets.getFirst();
|
||||
AliasTarget alias = record.getAliasTarget();
|
||||
if (record.getSetIdentifier() != null || record.getWeight() != null || record.getRegion() != null
|
||||
|| record.getFailover() != null || record.getHealthCheckId() != null
|
||||
|| Boolean.TRUE.equals(record.getMultiValueAnswer())
|
||||
|| record.getTtl() != null
|
||||
|| record.getRecords() != null && !record.getRecords().isEmpty()) {
|
||||
return DnsAnswer.refused();
|
||||
}
|
||||
// A single simple alias still answers when unhealthy, since there is no alternative record.
|
||||
DnsAnswer target = embeddedDnsServer.resolveAliasTarget(
|
||||
alias.getDnsName(), alias.getHostedZoneId(), type, listenerAddress).orElseGet(DnsAnswer::refused);
|
||||
if (target.responseCode() != 0 || target.records().stream().anyMatch(value -> value.type() != type)) {
|
||||
return DnsAnswer.refused();
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<DnsAnswer> resolve(String name, int type) {
|
||||
if (type != 33) {
|
||||
|
||||
Reference in new issue
Block a user