Light, fluffy, and always free - The AWS Local Emulator alternative
feat(dns): transparent AWS endpoint interception for spawned containers (#4610)
Sam Fakhreddine49501acb5ac3
diff --git a/docs/configuration/advanced/application-yml.md b/docs/configuration/advanced/application-yml.mdindex bd2f0eae32..03bd53a24a 100644--- a/docs/configuration/advanced/application-yml.md+++ b/docs/configuration/advanced/application-yml.md@@ -92,6 +92,16 @@ floci:# extra-suffixes:# - localhost.localstack.cloud+ # Transparent endpoint injection: resolve every AWS partition's DNS and dual-stack+ # suffix (amazonaws.com, api.aws, amazonaws.eu, amazonaws.com.cn and the rest; see+ # environment-variables.md for the full list) and every subdomain to Floci's+ # container IP inside spawned containers, so SDK clients built with explicit+ # real-AWS endpoints (which override AWS_ENDPOINT_URL) land on the emulator.+ # Live suffixes like api.aws and amazonaws.eu resolve to Floci while this is on.+ # Combine with tls.enabled for clients that hardcode https://.+ # Via env var: FLOCI_DNS_SPOOF_AWS_ENDPOINTS=true+ spoof-aws-endpoints: false+auth:validate-signatures: false # Set to true to verify S3 presigned URL signaturespresign-secret: local-emulator-secret # HMAC secret for S3 pre-signed URL verificationdiff --git a/docs/configuration/environment-variables.md b/docs/configuration/environment-variables.mdindex fbcb7c215d..a1edd2ec9a 100644--- a/docs/configuration/environment-variables.md+++ b/docs/configuration/environment-variables.md@@ -142,6 +142,28 @@ Floci's embedded DNS server always resolves the following wildcard suffixes to F| Variable | Default | Description ||---|---|---|| `FLOCI_DNS_EXTRA_SUFFIXES` | _(none)_ | Comma-separated list of additional hostname suffixes to resolve to Floci's container IP. Use this for custom domains beyond the built-in ones above (e.g. a private internal suffix). |+| `FLOCI_DNS_SPOOF_AWS_ENDPOINTS` | `false` | Resolve every AWS partition's DNS and dual-stack suffix (`amazonaws.com`, `api.aws`, `amazonaws.eu`, and more) and every subdomain to Floci's container IP inside spawned containers (transparent endpoint injection). See below. |++### Transparent endpoints++Some tools construct SDK clients with explicit real-AWS endpoints (e.g. `https://sts.us-east-1.amazonaws.com`), which override `AWS_ENDPOINT_URL`, so those calls escape the emulator and fail against real AWS. With `FLOCI_DNS_SPOOF_AWS_ENDPOINTS=true`, the embedded DNS server answers A queries for every AWS partition's DNS and dual-stack suffix and every subdomain at any depth (`sts.amazonaws.com`, `organizations.us-east-1.amazonaws.com`, virtual-hosted S3 like `my-bucket.s3.us-east-1.amazonaws.com`) with Floci's container IP, matching LocalStack's transparent endpoint injection. The intercepted suffixes are:++- `amazonaws.com` and `api.aws` (aws, aws-us-gov)+- `amazonaws.com.cn` and `api.amazonwebservices.com.cn` (aws-cn)+- `amazonaws.eu` and `api.amazonwebservices.eu` (aws-eusc)+- `c2s.ic.gov` and `api.aws.ic.gov` (aws-iso)+- `sc2s.sgov.gov` and `api.aws.scloud` (aws-iso-b)+- `cloud.adc-e.uk` and `api.cloud-aws.adc-e.uk` (aws-iso-e)+- `csp.hci.ic.gov` and `api.aws.hci.ic.gov` (aws-iso-f)++Some of these are live public names, not sandbox-only ones: with the flag on, `api.aws` and `amazonaws.eu` resolve to Floci too, so real endpoints under them are unreachable from spawned containers. All other queries keep the normal forward/fallback behavior.++Combine it with `FLOCI_TLS_ENABLED=true` so hardcoded `https://` endpoints work end to end:++- the TLS proxy already serves HTTPS on port 443, where those clients connect;+- the generated self-signed certificate additionally covers each of those suffixes and `*.<region>.<suffix>` for every published region, plus the multi-label and S3 endpoint forms (flipping the flag regenerates the certificate).++With TLS, set `FLOCI_DNS_SPOOF_AWS_ENDPOINTS` as an environment variable (or `-Dfloci.dns.spoof-aws-endpoints=true`), because the certificate generator does not read `application.yml`; Floci logs a warning at startup if the flag is only set there.---diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.javaindex c08024429f..3593ef73db 100644--- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java+++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java@@ -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 {diff --git a/src/main/java/io/github/hectorvent/floci/config/TlsConfigSource.java b/src/main/java/io/github/hectorvent/floci/config/TlsConfigSource.javaindex 4ba7735d0e..bfeae560cb 100644--- a/src/main/java/io/github/hectorvent/floci/config/TlsConfigSource.java+++ b/src/main/java/io/github/hectorvent/floci/config/TlsConfigSource.java@@ -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) {diff --git a/src/main/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServer.java b/src/main/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServer.javaindex 6f9a24a2c1..995809c3fa 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServer.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServer.java@@ -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 -> {diff --git a/src/main/resources/application.yml b/src/main/resources/application.ymlindex d95acfcc97..9a61471988 100644--- a/src/main/resources/application.yml+++ b/src/main/resources/application.yml@@ -252,6 +252,18 @@ floci:- 8.8.8.8- 8.8.4.4+ # Transparent endpoint injection: answer A queries for every AWS partition's DNS and+ # dual-stack suffix (and every subdomain) with Floci's container IP inside spawned+ # containers, so SDK clients constructed with explicit real-AWS endpoints, which override+ # AWS_ENDPOINT_URL, land on the emulator. Suffixes: 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. Live suffixes like api.aws and+ # amazonaws.eu resolve to Floci while this is on.+ # Combine with tls.enabled so clients that hardcode https:// are served on port 443+ # with a certificate covering those suffixes and their regional wildcards.+ spoof-aws-endpoints: false # FLOCI_DNS_SPOOF_AWS_ENDPOINTS+auth:validate-signatures: falsepresign-secret: local-emulator-secretdiff --git a/src/test/java/io/github/hectorvent/floci/config/TlsConfigSourceCertificateGenerationTest.java b/src/test/java/io/github/hectorvent/floci/config/TlsConfigSourceCertificateGenerationTest.javaindex 0c381d2b63..62c1a419f6 100644--- a/src/test/java/io/github/hectorvent/floci/config/TlsConfigSourceCertificateGenerationTest.java+++ b/src/test/java/io/github/hectorvent/floci/config/TlsConfigSourceCertificateGenerationTest.java@@ -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.diff --git a/src/test/java/io/github/hectorvent/floci/config/TlsConfigSourceCertificateReuseTest.java b/src/test/java/io/github/hectorvent/floci/config/TlsConfigSourceCertificateReuseTest.javaindex a7ddc2bd5a..a32c3cff15 100644--- a/src/test/java/io/github/hectorvent/floci/config/TlsConfigSourceCertificateReuseTest.java+++ b/src/test/java/io/github/hectorvent/floci/config/TlsConfigSourceCertificateReuseTest.java@@ -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*/diff --git a/src/test/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServerSpoofWarningTest.java b/src/test/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServerSpoofWarningTest.javanew file mode 100644index 0000000000..43b4a3106c--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServerSpoofWarningTest.java@@ -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));+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServerTest.java b/src/test/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServerTest.javaindex e67d855d34..ce75dd1713 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServerTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServerTest.java@@ -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 ─────────────────────────────@Testdiff --git a/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeSchedulerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeSchedulerIntegrationTest.javaindex 884ec5d405..13c5c14b53 100644--- a/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeSchedulerIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeSchedulerIntegrationTest.java@@ -319,6 +319,8 @@ class EventBridgeSchedulerIntegrationTest {public boolean containerFallbackEnabled() { return true; }@Overridepublic List<String> containerFallbackServers() { return List.of("8.8.8.8", "8.8.4.4"); }+ @Override+ public boolean spoofAwsEndpoints() { return false; }};}@Override