mirror of
https://github.com/floci-io/floci.git
synced 2026-12-05 19:18:01 -05:00
feat(dns): transparent AWS endpoint interception for spawned containers (#4610)
* feat(dns): resolve amazonaws.com to Floci inside spawned containers when spoof-aws-endpoints is on SDK clients built with explicit real-AWS endpoints override AWS_ENDPOINT_URL and would otherwise escape to real AWS; off by default. * fix(tls): cover every region and S3 endpoint form in the spoof SANs The default-region-only SANs failed the TLS handshake for any other region. * fix(dns): warn when spoof-aws-endpoints is set where the TLS certificate cannot see it Startup logs a WARN when the flag is only in application.yml and TLS is on. * fix(dns): derive spoofed AWS suffixes and SANs from partitions Intercept every partition DNS and dual-stack suffix and name each region's certificate SANs under its own partition suffix, so China, EUSC and ISO endpoints are covered and cn-* regions are no longer named under the commercial suffix. Add the s3-control SAN, drop the lambda-url SAN (function URLs live under on.aws, which is not intercepted), and log the startup warning with LOG.warnv. * test(dns): name the new certificate tests as sentences * docs(dns): document every AWS partition suffix spoof-aws-endpoints intercepts The flag covers each partition's DNS and dual-stack suffix, not just amazonaws.com. List them in the config comments, env-var docs and EmulatorConfig javadoc, call out that live suffixes like api.aws and amazonaws.eu resolve to Floci, and use a single literal in the TLS visibility warning. Rename the certificate regeneration test to a sentence.
This commit is contained in:
1 parent
894f20bb81
commit
49501acb5a
11 files changed
+512
-4
No files matched your search
@@ -209,6 +209,29 @@ public interface EmulatorConfig {
|
||||
*/
|
||||
@WithDefault("8.8.8.8,8.8.4.4")
|
||||
List<String> containerFallbackServers();
|
||||
|
||||
/**
|
||||
* When {@code true}, the embedded DNS server also answers A queries for every AWS
|
||||
* partition's DNS and dual-stack suffix (amazonaws.com, api.aws, amazonaws.com.cn,
|
||||
* api.amazonwebservices.com.cn, amazonaws.eu, api.amazonwebservices.eu, c2s.ic.gov,
|
||||
* api.aws.ic.gov, sc2s.sgov.gov, api.aws.scloud, cloud.adc-e.uk,
|
||||
* api.cloud-aws.adc-e.uk, csp.hci.ic.gov, api.aws.hci.ic.gov) and every subdomain
|
||||
* (any depth: {@code sts.amazonaws.com},
|
||||
* {@code organizations.us-east-1.amazonaws.com}, virtual-hosted S3 like
|
||||
* {@code bucket.s3.us-east-1.amazonaws.com}) with Floci's container IP,
|
||||
* LocalStack-style transparent endpoint injection. Tools that construct SDK
|
||||
* clients with explicit real-AWS endpoints (overriding {@code AWS_ENDPOINT_URL})
|
||||
* then land on Floci instead of escaping to real AWS.
|
||||
*
|
||||
* <p>Combine with {@code floci.tls.enabled=true} so hardcoded {@code https://}
|
||||
* endpoints are served on port 443 with a certificate covering the AWS wildcards.
|
||||
*
|
||||
* <p>Off by default: it hijacks all real-AWS traffic from spawned containers, including
|
||||
* live suffixes such as {@code api.aws} and {@code amazonaws.eu}.
|
||||
* Env: {@code FLOCI_DNS_SPOOF_AWS_ENDPOINTS}
|
||||
*/
|
||||
@WithDefault("false")
|
||||
boolean spoofAwsEndpoints();
|
||||
}
|
||||
|
||||
interface SecurityConfig {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package io.github.hectorvent.floci.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsPartition;
|
||||
import io.github.hectorvent.floci.core.common.AwsPartitions;
|
||||
import io.github.hectorvent.floci.core.common.AwsRegions;
|
||||
import io.github.hectorvent.floci.services.acm.CertificateGenerator;
|
||||
import io.github.hectorvent.floci.services.acm.model.KeyAlgorithm;
|
||||
@@ -125,8 +127,7 @@ public class TlsConfigSource implements ConfigSource {
|
||||
trustAnchor = ca.certificatePath();
|
||||
|
||||
if (Files.exists(certFile) && Files.exists(keyFile)) {
|
||||
List<String> currentHostnames = new ArrayList<>(DEFAULT_SAN_HOSTNAMES);
|
||||
currentHostnames.addAll(extractCustomHostnames());
|
||||
List<String> currentHostnames = configuredSanHostnames();
|
||||
|
||||
// Regenerate when the hostname config changed, when the existing leaf was not
|
||||
// issued by the current CA (a pre-CA self-signed cert, or the CA was regenerated),
|
||||
@@ -208,11 +209,134 @@ public class TlsConfigSource implements ConfigSource {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether {@code floci.dns.spoof-aws-endpoints} is set where this source can read it (system
|
||||
* property or environment variable). It cannot see application.yml, so a flag set only there
|
||||
* leaves the certificate without the AWS SANs.
|
||||
*/
|
||||
public static boolean spoofAwsEndpointsVisibleToTls() {
|
||||
return "true".equalsIgnoreCase(resolveProperty("floci.dns.spoof-aws-endpoints", "false"));
|
||||
}
|
||||
|
||||
/** Whether {@code floci.tls.enabled} is set where this source can read it. */
|
||||
public static boolean tlsEnabledVisibleToTls() {
|
||||
return "true".equalsIgnoreCase(resolveProperty("floci.tls.enabled", "false"));
|
||||
}
|
||||
|
||||
/**
|
||||
* The full SAN list the server certificate must cover for the current configuration:
|
||||
* defaults, custom hostnames, and the AWS endpoint wildcards when
|
||||
* {@code floci.dns.spoof-aws-endpoints} is enabled. Used both for generation and for the
|
||||
* change detection that triggers regeneration, so flipping the spoof flag regenerates the
|
||||
* certificate.
|
||||
*/
|
||||
private List<String> configuredSanHostnames() {
|
||||
List<String> sans = new ArrayList<>(DEFAULT_SAN_HOSTNAMES);
|
||||
sans.addAll(extractCustomHostnames());
|
||||
sans.addAll(awsSpoofSans());
|
||||
return sans;
|
||||
}
|
||||
|
||||
/**
|
||||
* The suffixes floci.dns.spoof-aws-endpoints intercepts, shared by the embedded DNS server and
|
||||
* the certificate SANs: every partition's DNS and dual-stack suffix. Each covers the suffix
|
||||
* itself and its subdomains at any depth, so explicit SDK endpoints like
|
||||
* {@code sts.us-east-1.<suffix>} resolve to Floci instead of real AWS.
|
||||
*/
|
||||
public static Set<String> awsEndpointSuffixes() {
|
||||
Set<String> result = new LinkedHashSet<>();
|
||||
for (AwsPartition partition : AwsPartitions.all()) {
|
||||
result.add(partition.dnsSuffix());
|
||||
result.add(partition.dualStackDnsSuffix());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Service infixes that AWS virtual-hosts with an extra resource-id label before the region. */
|
||||
private static final List<String> MULTI_LABEL_REGIONAL_SERVICE_INFIXES = List.of(
|
||||
"execute-api", "dkr.ecr", "s3-control", "s3.dualstack", "s3-website",
|
||||
"s3-fips", "s3-fips.dualstack");
|
||||
|
||||
/**
|
||||
* S3 endpoint prefixes that join the region with a hyphen instead of a dot: the legacy
|
||||
* {@code s3-<region>} form and its website counterpart {@code s3-website-<region>}, per
|
||||
* {@code S3VirtualHostFilter#isS3QualifierTail}, the authoritative list of endpoint forms
|
||||
* Floci itself recognizes as S3.
|
||||
*/
|
||||
private static final List<String> HYPHEN_JOINED_REGIONAL_S3_PREFIXES = List.of("s3", "s3-website");
|
||||
|
||||
/** Regionless S3 transfer-acceleration endpoints, with and without dualstack. */
|
||||
private static final List<String> REGIONLESS_S3_INFIXES = List.of("s3-accelerate", "s3-accelerate.dualstack");
|
||||
|
||||
/**
|
||||
* SANs covering AWS endpoint hostnames spoofed by the embedded DNS server.
|
||||
* Wildcards match a single label, so {@code *.amazonaws.com} covers global
|
||||
* endpoints ({@code sts.amazonaws.com}) but not regional ones
|
||||
* ({@code sts.us-east-1.amazonaws.com}), which need their own
|
||||
* {@code *.<region>.amazonaws.com} entry. A client can hit an explicit endpoint outside
|
||||
* {@code floci.default-region} (a cross-region call, a Lambda whose own AWS_REGION differs
|
||||
* from the emulator default, or a published region this emulator doesn't advertise via
|
||||
* DescribeRegions); DNS spoofing routes it to Floci regardless of region, so every
|
||||
* {@link AwsRegions#KNOWN_IDS published region id} gets a SAN, not just the configured default.
|
||||
*
|
||||
* <p>A single wildcard label also cannot cover multi-label endpoints, where a resource id
|
||||
* contributes an extra label before the service name:
|
||||
* {@code <api-id>.execute-api.<region>.amazonaws.com},
|
||||
* {@code <account>.dkr.ecr.<region>.amazonaws.com},
|
||||
* {@code <account>.s3-control.<region>.amazonaws.com}, and
|
||||
* {@code <bucket>.s3.dualstack.<region>.amazonaws.com}. Each needs its own
|
||||
* {@code *.<infix>.<region>.amazonaws.com} SAN.
|
||||
*
|
||||
* <p>S3 publishes several more forms: some join the region with a hyphen
|
||||
* ({@code <bucket>.s3-<region>.amazonaws.com}, {@code <bucket>.s3-website-<region>.amazonaws.com}),
|
||||
* and two are regionless transfer-acceleration endpoints
|
||||
* ({@code <bucket>.s3-accelerate.amazonaws.com},
|
||||
* {@code <bucket>.s3-accelerate.dualstack.amazonaws.com}). The set follows
|
||||
* {@code S3VirtualHostFilter#isS3QualifierTail}, which documents every S3 endpoint form Floci
|
||||
* itself routes; keep the two in sync so the SAN list does not drift from what gets spoofed.
|
||||
*
|
||||
* <p>The suffix is never fixed: global names use every partition's DNS suffix from
|
||||
* {@link AwsPartitions#all()}, and each region's names use {@link AwsRegions#dnsSuffixFor}, so
|
||||
* {@code cn-north-1} is named under the China suffix and never under the commercial one. Lambda
|
||||
* function URLs ({@code <url-id>.lambda-url.<region>.on.aws}) live outside the partition suffix
|
||||
* and are not intercepted, so they get no SAN.
|
||||
*/
|
||||
private List<String> awsSpoofSans() {
|
||||
if (!spoofAwsEndpointsVisibleToTls()) {
|
||||
return List.of();
|
||||
}
|
||||
List<String> sans = new ArrayList<>();
|
||||
for (String suffix : awsEndpointSuffixes()) {
|
||||
sans.add("*." + suffix);
|
||||
sans.add("*.s3." + suffix);
|
||||
for (String infix : REGIONLESS_S3_INFIXES) {
|
||||
sans.add("*." + infix + "." + suffix);
|
||||
}
|
||||
}
|
||||
for (String region : AwsRegions.KNOWN_IDS.stream().sorted().toList()) {
|
||||
String suffix = AwsRegions.dnsSuffixFor(region);
|
||||
// A wildcard matches exactly one label (RFC 6125 6.4.3), so the two broad
|
||||
// wildcards miss virtual-hosted addressing, where the bucket adds a label:
|
||||
// my-bucket.s3.<suffix> and my-bucket.s3.<region>.<suffix>. The DNS
|
||||
// spoof does route those, so without these the handshake fails on a hostname
|
||||
// mismatch rather than the request reaching Floci.
|
||||
sans.add("*." + region + "." + suffix);
|
||||
sans.add("*.s3." + region + "." + suffix);
|
||||
sans.add("*." + region + "." + AwsPartitions.forRegionOrCommercial(region).dualStackDnsSuffix());
|
||||
for (String infix : MULTI_LABEL_REGIONAL_SERVICE_INFIXES) {
|
||||
sans.add("*." + infix + "." + region + "." + suffix);
|
||||
}
|
||||
for (String prefix : HYPHEN_JOINED_REGIONAL_S3_PREFIXES) {
|
||||
sans.add("*." + prefix + "-" + region + "." + suffix);
|
||||
}
|
||||
}
|
||||
return sans;
|
||||
}
|
||||
|
||||
private void generateServerCert(Path tlsDir, Path certFile, Path keyFile, FlociCertificateAuthority ca) {
|
||||
try {
|
||||
Files.createDirectories(tlsDir);
|
||||
List<String> configured = new ArrayList<>(DEFAULT_SAN_HOSTNAMES);
|
||||
configured.addAll(extractCustomHostnames());
|
||||
List<String> configured = configuredSanHostnames();
|
||||
List<String> learned = readLearnedHostnames(tlsDir, certFile);
|
||||
List<String> allSans = new ArrayList<>(configured);
|
||||
for (String name : learned) {
|
||||
|
||||
@@ -1,6 +1,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.docker.ContainerDetector;
|
||||
import io.quarkus.runtime.Startup;
|
||||
import io.vertx.core.Vertx;
|
||||
@@ -38,6 +39,10 @@ import java.util.regex.Pattern;
|
||||
* Docker network IP so virtual-hosted S3 URLs (my-bucket.floci:4566) work from
|
||||
* inside Lambda containers without requiring wildcard Docker aliases.
|
||||
*
|
||||
* When floci.dns.spoof-aws-endpoints is enabled, every AWS partition suffix and all of its
|
||||
* subdomains resolve to Floci's IP as well (transparent endpoint injection), so
|
||||
* clients built with explicit real-AWS endpoints land on the emulator.
|
||||
*
|
||||
* Also answers for any name a {@link DnsRecordSource} owns, which is how a service that holds
|
||||
* a private DNS zone (Cloud Map) gets real records rather than only API responses.
|
||||
*
|
||||
@@ -91,6 +96,22 @@ public class EmbeddedDnsServer {
|
||||
this(suffixes, List.of());
|
||||
}
|
||||
|
||||
/**
|
||||
* The certificate generator cannot read application.yml, so DNS spoofing without AWS SANs on
|
||||
* the certificate only happens when the flag and TLS are on but the flag is not visible to it.
|
||||
*/
|
||||
static boolean shouldWarnSpoofInvisibleToTls(boolean spoofConfigured, boolean tlsEnabled,
|
||||
boolean spoofVisibleToTls) {
|
||||
return spoofConfigured && tlsEnabled && !spoofVisibleToTls;
|
||||
}
|
||||
|
||||
EmbeddedDnsServer(List<String> suffixes, boolean spoofAwsEndpoints) {
|
||||
this(suffixes, List.of());
|
||||
if (spoofAwsEndpoints) {
|
||||
this.suffixes.addAll(TlsConfigSource.awsEndpointSuffixes());
|
||||
}
|
||||
}
|
||||
|
||||
EmbeddedDnsServer(List<String> suffixes, Iterable<DnsRecordSource> recordSources) {
|
||||
this(suffixes, recordSources, List.of(), List.of());
|
||||
}
|
||||
@@ -124,6 +145,13 @@ public class EmbeddedDnsServer {
|
||||
suffixes.addAll(BUILTIN_SUFFIXES);
|
||||
config.hostname().ifPresent(suffixes::add);
|
||||
config.dns().extraSuffixes().ifPresent(suffixes::addAll);
|
||||
if (config.dns().spoofAwsEndpoints()) {
|
||||
suffixes.addAll(TlsConfigSource.awsEndpointSuffixes());
|
||||
if (shouldWarnSpoofInvisibleToTls(true, TlsConfigSource.tlsEnabledVisibleToTls(),
|
||||
TlsConfigSource.spoofAwsEndpointsVisibleToTls())) {
|
||||
LOG.warnv("floci.dns.spoof-aws-endpoints is set in application config but the TLS certificate only reads it from FLOCI_DNS_SPOOF_AWS_ENDPOINTS or -Dfloci.dns.spoof-aws-endpoints; https:// calls to {0} will fail the handshake until it is set there.", TlsConfigSource.awsEndpointSuffixes());
|
||||
}
|
||||
}
|
||||
|
||||
DatagramSocket socket = vertx.createDatagramSocket(new DatagramSocketOptions().setIpV6(false));
|
||||
socket.listen(DNS_PORT, "0.0.0.0", ar -> {
|
||||
|
||||
Reference in new issue
Block a user