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:
Sam Fakhreddine authored and GitHub committed 2026-09-30 13:42:08 -04:00
1 parent 894f20bb81
commit 49501acb5a
11 files changed
+512 -4

No files matched your search

@@ -46,6 +46,8 @@ class TlsConfigSourceCertificateGenerationTest {
System.clearProperty("floci.storage.persistent-path");
System.clearProperty("floci.hostname");
System.clearProperty("floci.base-url");
System.clearProperty("floci.dns.spoof-aws-endpoints");
System.clearProperty("floci.default-region");
System.clearProperty("floci.services.iot.endpoint-address");
}
@@ -214,6 +216,175 @@ class TlsConfigSourceCertificateGenerationTest {
"Metadata should include default 'localhost' hostname");
}
/**
* Test that the AWS endpoint wildcards are included when spoof-aws-endpoints is enabled
*/
@Test
void certificateIncludesAwsWildcardsWhenSpoofEnabled() throws Exception {
// Arrange
System.setProperty("floci.dns.spoof-aws-endpoints", "true");
// Act
new TlsConfigSource();
// Assert
Path certFile = tempDir.resolve("tls/floci-server.crt");
assertTrue(Files.exists(certFile), "Certificate file should exist");
List<String> sans = extractSansFromCertificate(certFile);
assertTrue(sans.contains("*.amazonaws.com"),
"Certificate SANs should include '*.amazonaws.com' when spoofing is enabled");
assertTrue(sans.contains("*.us-east-1.amazonaws.com"),
"Certificate SANs should include the default region wildcard '*.us-east-1.amazonaws.com'");
assertTrue(sans.contains("localhost"),
"Certificate SANs should still include default 'localhost'");
}
/**
* A TLS wildcard matches exactly one label (RFC 6125 6.4.3), so the broad
* *.amazonaws.com wildcards do not cover virtual-hosted S3 addressing, where the
* bucket contributes an extra label. The DNS spoof does route those hostnames to
* Floci, so without dedicated SANs the request dies at the handshake.
*/
@Test
void certificateCoversVirtualHostedS3WhenSpoofEnabled() throws Exception {
System.setProperty("floci.dns.spoof-aws-endpoints", "true");
new TlsConfigSource();
Path certFile = tempDir.resolve("tls/floci-server.crt");
List<String> sans = extractSansFromCertificate(certFile);
assertTrue(sans.contains("*.s3.amazonaws.com"),
"Certificate SANs should cover global virtual-hosted S3");
assertTrue(sans.contains("*.s3.us-east-1.amazonaws.com"),
"Certificate SANs should cover regional virtual-hosted S3");
}
/**
* A client can hit an explicit HTTPS AWS endpoint outside floci.default-region (a
* cross-region call, or a Lambda whose own AWS_REGION differs from the emulator default).
* DNS spoofing routes it to Floci regardless of region, so the cert must cover every
* published region, including ones the emulator does not advertise (eu-north-1), not
* just the configured default.
*/
@Test
void awsRegionalWildcardsCoverEveryKnownRegion() throws Exception {
// Catches: SANs limited to the configured default region, failing the handshake elsewhere
System.setProperty("floci.dns.spoof-aws-endpoints", "true");
System.setProperty("floci.default-region", "eu-west-1");
new TlsConfigSource();
List<String> sans = extractSansFromCertificate(tempDir.resolve("tls/floci-server.crt"));
for (String region : List.of("us-east-1", "eu-west-1", "eu-north-1", "ap-southeast-2")) {
assertTrue(sans.contains("*." + region + ".amazonaws.com"),
"Certificate SANs should include '*." + region + ".amazonaws.com' regardless of the "
+ "configured default region");
assertTrue(sans.contains("*.s3." + region + ".amazonaws.com"),
"Certificate SANs should include regional virtual-hosted S3 for " + region);
}
}
/**
* A wildcard SAN matches exactly one label (RFC 6125 6.4.3), so {@code *.<region>.amazonaws.com}
* does not cover endpoints where a resource id adds a label before the service name. DNS
* spoofing still routes them to Floci, so each needs a dedicated SAN.
*/
@Test
void certificateCoversMultiLabelRegionalEndpointsWhenSpoofEnabled() throws Exception {
// Catches: a missing SAN for execute-api, dkr.ecr, s3-control or s3.dualstack hosts
System.setProperty("floci.dns.spoof-aws-endpoints", "true");
new TlsConfigSource();
List<String> sans = extractSansFromCertificate(tempDir.resolve("tls/floci-server.crt"));
assertTrue(sans.contains("*.execute-api.us-east-1.amazonaws.com"),
"Certificate SANs should cover API Gateway execute-api endpoints");
assertTrue(sans.contains("*.dkr.ecr.us-east-1.amazonaws.com"),
"Certificate SANs should cover ECR dkr.ecr endpoints");
assertTrue(sans.contains("*.s3.dualstack.us-east-1.amazonaws.com"),
"Certificate SANs should cover dualstack virtual-hosted S3 endpoints");
assertTrue(sans.contains("*.s3-control.us-east-1.amazonaws.com"),
"Certificate SANs should cover S3 Control <account>.s3-control endpoints");
}
@Test
void certificateDoesNotClaimLambdaUrlUnderAmazonaws() throws Exception {
// Catches: a lambda-url SAN under the amazonaws suffix, where Lambda function URLs do not live
System.setProperty("floci.dns.spoof-aws-endpoints", "true");
new TlsConfigSource();
List<String> sans = extractSansFromCertificate(tempDir.resolve("tls/floci-server.crt"));
assertFalse(sans.contains("*.lambda-url.us-east-1.amazonaws.com"),
"Function URLs are <url-id>.lambda-url.<region>.on.aws, never under the partition suffix");
}
@Test
void certificateUsesEachRegionsOwnPartitionSuffix() throws Exception {
// Catches: cn-* regions named under amazonaws.com and non-commercial partition suffixes uncovered
System.setProperty("floci.dns.spoof-aws-endpoints", "true");
new TlsConfigSource();
List<String> sans = extractSansFromCertificate(tempDir.resolve("tls/floci-server.crt"));
assertTrue(sans.contains("*.amazonaws.com.cn"), "China global endpoints");
assertTrue(sans.contains("*.cn-north-1.amazonaws.com.cn"), "China regional endpoints");
assertTrue(sans.contains("*.execute-api.cn-north-1.amazonaws.com.cn"), "China multi-label endpoints");
assertTrue(sans.contains("*.s3-website-cn-north-1.amazonaws.com.cn"), "China hyphenated S3 endpoints");
assertTrue(sans.contains("*.us-iso-east-1.c2s.ic.gov"), "ISO regional endpoints");
assertTrue(sans.contains("*.amazonaws.eu"), "EUSC global endpoints");
assertFalse(sans.contains("*.cn-north-1.amazonaws.com"),
"cn-north-1 has no endpoints under the commercial suffix");
assertFalse(sans.contains("*.us-east-1.amazonaws.com.cn"),
"us-east-1 has no endpoints under the China suffix");
}
/**
* S3 publishes more endpoint forms than the dotted regional one: website (dotted and
* hyphenated), legacy hyphenated regional, FIPS and its dualstack variant, and the regionless
* transfer-acceleration endpoints. The set follows S3VirtualHostFilter#isS3QualifierTail.
*/
@Test
void certificateCoversEveryS3EndpointFormWhenSpoofEnabled() throws Exception {
// Catches: an S3 endpoint form that is routed to Floci but has no matching SAN
System.setProperty("floci.dns.spoof-aws-endpoints", "true");
new TlsConfigSource();
List<String> sans = extractSansFromCertificate(tempDir.resolve("tls/floci-server.crt"));
assertTrue(sans.contains("*.s3-website-us-east-1.amazonaws.com"),
"Certificate SANs should cover the hyphenated S3 website endpoint form");
assertTrue(sans.contains("*.s3-website.us-east-1.amazonaws.com"),
"Certificate SANs should cover the dotted S3 website endpoint form");
assertTrue(sans.contains("*.s3-us-east-1.amazonaws.com"),
"Certificate SANs should cover the legacy hyphenated S3 regional endpoint form");
assertTrue(sans.contains("*.s3-fips.us-east-1.amazonaws.com"),
"Certificate SANs should cover the S3 FIPS endpoint");
assertTrue(sans.contains("*.s3-fips.dualstack.us-east-1.amazonaws.com"),
"Certificate SANs should cover the dualstack S3 FIPS endpoint");
assertTrue(sans.contains("*.s3-accelerate.amazonaws.com"),
"Certificate SANs should cover the regionless S3 transfer-acceleration endpoint");
assertTrue(sans.contains("*.s3-accelerate.dualstack.amazonaws.com"),
"Certificate SANs should cover the dualstack S3 transfer-acceleration endpoint");
}
/**
* Test that no AWS wildcards are included when spoof-aws-endpoints is disabled
*/
@Test
void certificateExcludesAwsWildcardsWhenSpoofDisabled() throws Exception {
// Act
new TlsConfigSource();
// Assert
List<String> sans = extractSansFromCertificate(tempDir.resolve("tls/floci-server.crt"));
assertFalse(sans.contains("*.amazonaws.com"),
"Certificate SANs should not include '*.amazonaws.com' when spoofing is disabled");
assertFalse(sans.contains("*.us-east-1.amazonaws.com"),
"Certificate SANs should not include '*.us-east-1.amazonaws.com' when spoofing is disabled");
}
/**
* The IoT endpoint address is what devices verify on 8883 and 443, so the boot certificate
* covers it even when it has more labels than the wildcard SAN matches.
@@ -47,6 +47,7 @@ class TlsConfigSourceCertificateReuseTest {
System.clearProperty("floci.tls.enabled");
System.clearProperty("floci.tls.self-signed");
System.clearProperty("floci.storage.persistent-path");
System.clearProperty("floci.dns.spoof-aws-endpoints");
}
/**
@@ -257,6 +258,45 @@ class TlsConfigSourceCertificateReuseTest {
"Certificate content should be unchanged (reused, not regenerated)");
}
/**
* Test that certificate is regenerated when the spoof-aws-endpoints flag flips
*/
@Test
void certificateIsRegeneratedWhenTheSpoofFlagFlips() throws Exception {
// Arrange: Generate initial certificate without AWS endpoint spoofing
System.setProperty("floci.tls.enabled", "true");
System.setProperty("floci.tls.self-signed", "true");
System.setProperty("floci.storage.persistent-path", tempDir.toString());
// Act: Create TlsConfigSource - this generates the initial certificate
new TlsConfigSource();
Path tlsDir = tempDir.resolve("tls");
Path certFile = tlsDir.resolve("floci-server.crt");
Path metadataFile = tlsDir.resolve("floci-server.metadata.json");
assertTrue(Files.exists(certFile), "Initial certificate should be generated");
assertTrue(Files.exists(metadataFile), "Initial metadata should be generated");
assertFalse(Files.readString(metadataFile).contains("*.amazonaws.com"),
"Initial metadata should not contain the AWS wildcard");
String initialCert = Files.readString(certFile);
// Enable AWS endpoint spoofing
System.setProperty("floci.dns.spoof-aws-endpoints", "true");
// Act: Create new TlsConfigSource - should regenerate certificate
new TlsConfigSource();
// Assert: Certificate and metadata should be regenerated with the AWS wildcards.
// Comparing content (rather than mtime after a sleep) keeps this deterministic on
// filesystems with coarse timestamp resolution.
assertNotEquals(initialCert, Files.readString(certFile),
"Certificate should be regenerated when the spoof flag flips (content changed)");
assertTrue(Files.readString(metadataFile).contains("*.amazonaws.com"),
"New metadata should contain the AWS wildcard");
}
/**
* Test that certificate is regenerated when base URL hostname changes
*/
@@ -0,0 +1,33 @@
package io.github.hectorvent.floci.core.common.dns;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class EmbeddedDnsServerSpoofWarningTest {
@Test
void warnsWhenSpoofIsOnTlsIsOnAndCertificateCannotSeeTheFlag() {
// Catches: no warning when DNS spoofs amazonaws.com but the certificate has no AWS SANs
assertTrue(EmbeddedDnsServer.shouldWarnSpoofInvisibleToTls(true, true, false));
}
@Test
void staysQuietWhenCertificateSeesTheFlag() {
// Catches: warning even though the certificate already carries the AWS SANs
assertFalse(EmbeddedDnsServer.shouldWarnSpoofInvisibleToTls(true, true, true));
}
@Test
void staysQuietWhenTlsIsOff() {
// Catches: warning about certificate SANs when there is no TLS at all
assertFalse(EmbeddedDnsServer.shouldWarnSpoofInvisibleToTls(true, false, false));
}
@Test
void staysQuietWhenSpoofIsOff() {
// Catches: warning when the spoof flag is not enabled
assertFalse(EmbeddedDnsServer.shouldWarnSpoofInvisibleToTls(false, true, false));
}
}
@@ -398,6 +398,49 @@ class EmbeddedDnsServerTest {
assertEquals(15, buffer.getInt(firstTtl + 16));
}
// ── spoof-aws-endpoints: transparent endpoint injection ──────────────────
@Test
void spoofAwsEndpoints_answersAmazonawsAQueriesWithFlociIp() {
EmbeddedDnsServer spoofing = new EmbeddedDnsServer(List.of(), true);
assertEquals(List.of("172.19.0.2"), spoofing.resolveARecord("sts.amazonaws.com", "172.19.0.2"));
assertEquals(List.of("172.19.0.2"), spoofing.resolveARecord("sts.us-east-1.amazonaws.com", "172.19.0.2"));
assertEquals(List.of("172.19.0.2"), spoofing.resolveARecord("organizations.us-east-1.amazonaws.com", "172.19.0.2"));
assertEquals(List.of("172.19.0.2"), spoofing.resolveARecord("my-bucket.s3.us-east-1.amazonaws.com", "172.19.0.2"));
assertEquals(List.of("172.19.0.2"), spoofing.resolveARecord("amazonaws.com", "172.19.0.2"));
}
@Test
void spoofAwsEndpoints_coversEveryPartitionSuffix() {
// Catches: interception limited to the commercial suffix, leaving China, EUSC and ISO endpoints on real AWS
EmbeddedDnsServer spoofing = new EmbeddedDnsServer(List.of(), true);
assertEquals(List.of("172.19.0.2"), spoofing.resolveARecord("sts.cn-north-1.amazonaws.com.cn", "172.19.0.2"));
assertEquals(List.of("172.19.0.2"), spoofing.resolveARecord("sts.eusc-de-east-1.amazonaws.eu", "172.19.0.2"));
assertEquals(List.of("172.19.0.2"), spoofing.resolveARecord("sts.us-iso-east-1.c2s.ic.gov", "172.19.0.2"));
}
@Test
void spoofAwsEndpoints_offByDefault_amazonawsQueriesAreForwarded() {
assertTrue(dns.resolveARecord("sts.amazonaws.com", "172.19.0.2").isEmpty());
assertTrue(dns.resolveARecord("sts.us-east-1.amazonaws.com", "172.19.0.2").isEmpty());
assertTrue(dns.resolveARecord("my-bucket.s3.us-east-1.amazonaws.com", "172.19.0.2").isEmpty());
}
@Test
void spoofAwsEndpoints_doesNotCaptureOtherDomains() {
EmbeddedDnsServer spoofing = new EmbeddedDnsServer(List.of(), true);
assertFalse(spoofing.matchesSuffix("business-api.tiktok.com"));
assertFalse(spoofing.matchesSuffix("myamazonaws.com"));
assertFalse(spoofing.matchesSuffix("amazonaws.com.evil.example"));
}
@Test
void spoofAwsEndpoints_flociDomainsStillResolve() {
EmbeddedDnsServer spoofing = new EmbeddedDnsServer(List.of(), true);
assertTrue(spoofing.matchesSuffix("my-bucket.s3.localhost.floci.io"));
assertTrue(spoofing.matchesSuffix("my-bucket.localhost.localstack.cloud"));
}
// ── matchesSuffix — built-in emulator domains ─────────────────────────────
@Test
@@ -319,6 +319,8 @@ class EventBridgeSchedulerIntegrationTest {
public boolean containerFallbackEnabled() { return true; }
@Override
public List<String> containerFallbackServers() { return List.of("8.8.8.8", "8.8.4.4"); }
@Override
public boolean spoofAwsEndpoints() { return false; }
};
}
@Override