Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(s3): derive S3 host suffix matching from DNS config (#1843)

TiM188588b4eb13
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.java
index 56438e0556..881d652062 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
@@ -66,7 +66,7 @@ public class EmbeddedDnsServer {
// NOT cover "*.X" (e.g. "localhost.floci.io" does NOT resolve bare "*.floci.io").
// localhost.localstack.cloud → localhost.localstack.cloud, *.localhost.localstack.cloud
// localhost.floci.io → localhost.floci.io, *.localhost.floci.io
- static final List<String> BUILTIN_SUFFIXES = List.of(DEFAULT_SUFFIX, LOCALSTACK_SUFFIX);
+ public static final List<String> BUILTIN_SUFFIXES = List.of(DEFAULT_SUFFIX, LOCALSTACK_SUFFIX);
private volatile String serverIp;
private final SequencedSet<String> suffixes = new LinkedHashSet<>();
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilter.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilter.java
index 6b6e2b5502..80498920d7 100644
--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilter.java
+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilter.java
@@ -13,6 +13,10 @@ import jakarta.ws.rs.core.UriBuilder;
import jakarta.ws.rs.ext.Provider;
import java.net.URI;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
@Provider
@PreMatching
@@ -21,16 +25,45 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
private final String baseHostname;
+ /**
+ * Hostname suffixes for which a bare {@code s3.<suffix>} Host header is Floci's own
+ * S3 service endpoint (bucketless) rather than a bucket literally named {@code s3}.
+ * Derived from the same source of truth the embedded DNS server uses: the always-on
+ * builtins ({@code localhost.floci.io}, {@code localhost.localstack.cloud}) plus the
+ * configured {@code floci.hostname} and any {@code floci.dns.extra-suffixes},
+ * alongside plain {@code localhost}. Stored lowercase for case-insensitive matching.
+ */
+ private final Set<String> serviceHostSuffixes;
+
@Inject
public S3VirtualHostFilter(EmulatorConfig config, ContainerDetector containerDetector) {
this.baseHostname = config.hostname()
.orElseGet(() -> containerDetector.isRunningInContainer()
? EmbeddedDnsServer.DEFAULT_SUFFIX
: extractHostnameFromUrl(config.baseUrl()));
+ this.serviceHostSuffixes = buildServiceHostSuffixes(config.hostname(), config.dns().extraSuffixes());
}
S3VirtualHostFilter() {
this.baseHostname = "localhost";
+ this.serviceHostSuffixes = buildServiceHostSuffixes(Optional.empty(), Optional.empty());
+ }
+
+ /**
+ * Builds the service-host suffix set from the DNS source of truth rather than
+ * re-hardcoding it: {@code {"localhost"}} plus the {@link EmbeddedDnsServer} builtins
+ * plus the configured hostname and any extra suffixes. The three configured inputs
+ * mirror what {@code EmbeddedDnsServer} makes resolvable, so a host that reaches Floci
+ * by wildcard DNS is routed by the same rules it resolved under. Package-private so
+ * tests reuse the same derivation instead of duplicating the builtin list.
+ */
+ static Set<String> buildServiceHostSuffixes(Optional<String> hostname, Optional<List<String>> extraSuffixes) {
+ Set<String> suffixes = new HashSet<>();
+ suffixes.add("localhost");
+ EmbeddedDnsServer.BUILTIN_SUFFIXES.forEach(s -> suffixes.add(s.toLowerCase()));
+ hostname.ifPresent(h -> suffixes.add(h.toLowerCase()));
+ extraSuffixes.ifPresent(list -> list.forEach(s -> suffixes.add(s.toLowerCase())));
+ return Set.copyOf(suffixes);
}
@Override
@@ -59,7 +92,7 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
return;
}
- String bucket = extractBucket(host, baseHostname);
+ String bucket = extractBucket(host, baseHostname, serviceHostSuffixes);
if (bucket == null) return;
String path = uri.getRawPath();
@@ -125,7 +158,7 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
*
* Returns null if the host does not match a virtual-hosted pattern.
*/
- static String extractBucket(String host, String baseHostname) {
+ static String extractBucket(String host, String baseHostname, Set<String> serviceHostSuffixes) {
if (host == null) {
return null;
}
@@ -147,7 +180,7 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
String firstLabel = hostname.substring(0, firstDot);
String remainder = hostname.substring(firstDot + 1);
- if (isS3ServiceEndpointHost(firstLabel, remainder, baseHostname)) {
+ if (isS3ServiceEndpointHost(firstLabel, remainder, baseHostname, serviceHostSuffixes)) {
return null;
}
@@ -162,20 +195,45 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
return firstLabel;
}
+ // Configured Floci DNS suffixes (builtins + floci.dns.extra-suffixes): route
+ // bucket.<suffix>, bucket.s3.<suffix> and bucket.s3.<region>.<suffix> the same
+ // way the always-on wildcard-DNS builtins resolve.
+ if (matchesConfiguredSuffixHost(remainder, serviceHostSuffixes)) {
+ return firstLabel;
+ }
+
return null;
}
- private static boolean isS3ServiceEndpointHost(String firstLabel, String remainder, String baseHostname) {
+ /**
+ * Matches the virtual-hosted bucket forms for a configured DNS suffix {@code S}
+ * (a builtin, {@code floci.hostname}, or a {@code floci.dns.extra-suffix}): {@code bucket.S},
+ * {@code bucket.s3.S}, and the region-qualified {@code bucket.s3.<region>.S}.
+ * Plain {@code localhost} is skipped here — it keeps its dedicated
+ * {@link #matchesEndpointHost} handling via {@link #isAwsS3Domain}.
+ */
+ private static boolean matchesConfiguredSuffixHost(String remainder, Set<String> serviceHostSuffixes) {
+ String lower = remainder.toLowerCase();
+ for (String suffix : serviceHostSuffixes) {
+ if ("localhost".equals(suffix)) {
+ continue;
+ }
+ if (lower.equals(suffix) || (lower.startsWith("s3.") && lower.endsWith("." + suffix))) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ static boolean isS3ServiceEndpointHost(String firstLabel, String remainder, String baseHostname,
+ Set<String> serviceHostSuffixes) {
if (!"s3".equalsIgnoreCase(firstLabel)) {
return false;
}
if (baseHostname != null && matchesEndpointHost(remainder, baseHostname)) {
return true;
}
- String lowerRemainder = remainder.toLowerCase();
- return "localhost".equals(lowerRemainder)
- || "localhost.localstack.cloud".equals(lowerRemainder)
- || "localhost.floci.io".equals(lowerRemainder);
+ return serviceHostSuffixes.contains(remainder.toLowerCase());
}
/**
@@ -230,7 +288,13 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
return true;
}
- /** Returns true for *.s3.amazonaws.com and other well-known S3 domains. */
+ /**
+ * Returns true for well-known AWS S3 domains and the always-on {@code localhost}
+ * endpoint. The Floci wildcard-DNS builtins ({@code localhost.floci.io},
+ * {@code localhost.localstack.cloud}) and any configured {@code floci.dns.extra-suffixes}
+ * are handled by {@link #matchesConfiguredSuffixHost} instead, so they stay derived
+ * from the DNS source of truth rather than hardcoded here.
+ */
private static boolean isAwsS3Domain(String remainder) {
if ("s3.amazonaws.com".equals(remainder)) {
return true;
@@ -246,14 +310,6 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
if (matchesEndpointHost(remainder, "localhost")) {
return true;
}
- // LocalStack public wildcard DNS: bucket.s3.localhost.localstack.cloud and bucket.localhost.localstack.cloud
- if (remainder.endsWith(".localstack.cloud")) {
- return remainder.startsWith("s3.") || "localhost.localstack.cloud".equals(remainder);
- }
- // Floci public wildcard DNS: bucket.s3.localhost.floci.io and bucket.localhost.floci.io → 127.0.0.1
- if (remainder.endsWith(".localhost.floci.io") || "localhost.floci.io".equals(remainder)) {
- return remainder.startsWith("s3.") || "localhost.floci.io".equals(remainder);
- }
// S3 website endpoints: bucket.s3-website-<region>.amazonaws.com, bucket.s3-website-<region>.localhost, etc.
if (remainder.startsWith("s3-website")) {
return true;
diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilterTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilterTest.java
index 3dcf43e25c..c916ed8fca 100644
--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilterTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilterTest.java
@@ -10,14 +10,27 @@ import org.mockito.ArgumentCaptor;
import java.net.URI;
+import java.util.Optional;
+import java.util.Set;
+
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class S3VirtualHostFilterTest {
+ /**
+ * Always-on service-host suffix set with no configured extra suffixes. Derived from the
+ * production factory so it stays in sync if a builtin suffix is ever added, rather than
+ * hardcoding a copy of {@code EmbeddedDnsServer.BUILTIN_SUFFIXES}.
+ */
+ private static final Set<String> DEFAULT_SUFFIXES =
+ S3VirtualHostFilter.buildServiceHostSuffixes(Optional.empty(), Optional.empty());
+
// --- extractBucket with baseHostname ---
@ParameterizedTest
@@ -62,7 +75,7 @@ class S3VirtualHostFilterTest {
"my-bucket.s3.us-east-1.localhost.floci.io, localhost, my-bucket",
})
void extractsBucketFromVirtualHostedStyle(String host, String baseHostname, String expectedBucket) {
- assertEquals(expectedBucket, S3VirtualHostFilter.extractBucket(host, baseHostname));
+ assertEquals(expectedBucket, S3VirtualHostFilter.extractBucket(host, baseHostname, DEFAULT_SUFFIXES));
}
// --- Path-style: service hostname alone — must NOT extract a bucket ---
@@ -94,7 +107,7 @@ class S3VirtualHostFilterTest {
"my-bucket.emulator.local, localhost",
})
void returnsNullForPathStyleOrMismatchedRemainder(String host, String baseHostname) {
- assertNull(S3VirtualHostFilter.extractBucket(host, baseHostname));
+ assertNull(S3VirtualHostFilter.extractBucket(host, baseHostname, DEFAULT_SUFFIXES));
}
@ParameterizedTest
@@ -105,31 +118,116 @@ class S3VirtualHostFilterTest {
"10.0.0.1:9000, localhost",
})
void returnsNullForIpAddresses(String host, String baseHostname) {
- assertNull(S3VirtualHostFilter.extractBucket(host, baseHostname));
+ assertNull(S3VirtualHostFilter.extractBucket(host, baseHostname, DEFAULT_SUFFIXES));
}
@ParameterizedTest
@NullSource
void returnsNullForNullHost(String host) {
- assertNull(S3VirtualHostFilter.extractBucket(host, "localhost"));
+ assertNull(S3VirtualHostFilter.extractBucket(host, "localhost", DEFAULT_SUFFIXES));
}
@Test
void returnsNullForNullBaseHostname() {
// path-style bare hostname (no subdomain) — must return null
- assertNull(S3VirtualHostFilter.extractBucket("localhost:4566", null));
+ assertNull(S3VirtualHostFilter.extractBucket("localhost:4566", null, DEFAULT_SUFFIXES));
// well-known domains match regardless of baseHostname
- assertEquals("my-bucket", S3VirtualHostFilter.extractBucket("my-bucket.localhost", null));
- assertEquals("my-bucket", S3VirtualHostFilter.extractBucket("my-bucket.localhost:4566", null));
- assertEquals("my-bucket", S3VirtualHostFilter.extractBucket("my-bucket.s3.amazonaws.com", null));
- assertEquals("my-bucket", S3VirtualHostFilter.extractBucket("my-bucket.s3.localhost.localstack.cloud", null));
- assertEquals("my-bucket", S3VirtualHostFilter.extractBucket("my-bucket.localhost.localstack.cloud", null));
- assertEquals("my-bucket", S3VirtualHostFilter.extractBucket("my-bucket.s3.localhost.floci.io", null));
- assertEquals("my-bucket", S3VirtualHostFilter.extractBucket("my-bucket.s3.us-east-1.localhost.floci.io", null));
- assertEquals("my-bucket", S3VirtualHostFilter.extractBucket("my-bucket.localhost.floci.io", null));
+ assertEquals("my-bucket", S3VirtualHostFilter.extractBucket("my-bucket.localhost", null, DEFAULT_SUFFIXES));
+ assertEquals("my-bucket", S3VirtualHostFilter.extractBucket("my-bucket.localhost:4566", null, DEFAULT_SUFFIXES));
+ assertEquals("my-bucket", S3VirtualHostFilter.extractBucket("my-bucket.s3.amazonaws.com", null, DEFAULT_SUFFIXES));
+ assertEquals("my-bucket", S3VirtualHostFilter.extractBucket("my-bucket.s3.localhost.localstack.cloud", null, DEFAULT_SUFFIXES));
+ assertEquals("my-bucket", S3VirtualHostFilter.extractBucket("my-bucket.localhost.localstack.cloud", null, DEFAULT_SUFFIXES));
+ assertEquals("my-bucket", S3VirtualHostFilter.extractBucket("my-bucket.s3.localhost.floci.io", null, DEFAULT_SUFFIXES));
+ assertEquals("my-bucket", S3VirtualHostFilter.extractBucket("my-bucket.s3.us-east-1.localhost.floci.io", null, DEFAULT_SUFFIXES));
+ assertEquals("my-bucket", S3VirtualHostFilter.extractBucket("my-bucket.localhost.floci.io", null, DEFAULT_SUFFIXES));
// Region-qualified vhost against localhost fallback works without baseHostname
- assertEquals("my-bucket", S3VirtualHostFilter.extractBucket("my-bucket.s3.us-east-1.localhost", null));
- assertEquals("my-bucket", S3VirtualHostFilter.extractBucket("my-bucket.s3.us-east-1.localhost:4566", null));
+ assertEquals("my-bucket", S3VirtualHostFilter.extractBucket("my-bucket.s3.us-east-1.localhost", null, DEFAULT_SUFFIXES));
+ assertEquals("my-bucket", S3VirtualHostFilter.extractBucket("my-bucket.s3.us-east-1.localhost:4566", null, DEFAULT_SUFFIXES));
+ }
+
+ // --- Service-host classification derives from the configured suffix set ---
+
+ @Test
+ void classifiesServiceHostFromDerivedSuffixSet() {
+ // Always-on builtins: bare s3.<builtin> is the service endpoint, not a bucket named "s3"
+ assertTrue(S3VirtualHostFilter.isS3ServiceEndpointHost("s3", "localhost", "localhost", DEFAULT_SUFFIXES));
+ assertTrue(S3VirtualHostFilter.isS3ServiceEndpointHost("s3", "localhost.localstack.cloud", "localhost", DEFAULT_SUFFIXES));
+ assertTrue(S3VirtualHostFilter.isS3ServiceEndpointHost("s3", "localhost.floci.io", "localhost", DEFAULT_SUFFIXES));
+
+ // A configured extra suffix is newly recognised as a service host...
+ Set<String> withExtra = Set.of(
+ "localhost", "localhost.floci.io", "localhost.localstack.cloud", "emulator.internal");
+ assertTrue(S3VirtualHostFilter.isS3ServiceEndpointHost("s3", "emulator.internal", "localhost", withExtra));
+ // ...but is not, with only the builtins configured (proves it comes from config, not a literal)
+ assertFalse(S3VirtualHostFilter.isS3ServiceEndpointHost("s3", "emulator.internal", "localhost", DEFAULT_SUFFIXES));
+
+ // The configured base hostname still wins regardless of the suffix set
+ assertTrue(S3VirtualHostFilter.isS3ServiceEndpointHost("s3", "floci.internal", "floci.internal", DEFAULT_SUFFIXES));
+
+ // Only the "s3" service label is a service host; any other first label is a bucket
+ assertFalse(S3VirtualHostFilter.isS3ServiceEndpointHost("my-bucket", "localhost", "localhost", DEFAULT_SUFFIXES));
+ }
+
+ // --- Virtual-hosted buckets route on a configured extra suffix (like the builtins) ---
+
+ @Test
+ void routesVirtualHostedBucketsForConfiguredExtraSuffix() {
+ Set<String> withExtra = Set.of(
+ "localhost", "localhost.floci.io", "localhost.localstack.cloud", "localhost.example.internal");
+
+ // All three virtual-hosted forms route to the bucket, exactly as the builtins do
+ assertEquals("my-bucket", S3VirtualHostFilter.extractBucket(
+ "my-bucket.localhost.example.internal", "localhost", withExtra));
+ assertEquals("my-bucket", S3VirtualHostFilter.extractBucket(
+ "my-bucket.localhost.example.internal:4566", "localhost", withExtra));
+ assertEquals("my-bucket", S3VirtualHostFilter.extractBucket(
+ "my-bucket.s3.localhost.example.internal", "localhost", withExtra));
+ assertEquals("my-bucket", S3VirtualHostFilter.extractBucket(
+ "my-bucket.s3.us-east-1.localhost.example.internal", "localhost", withExtra));
+
+ // The bare s3.<extra> service host stays bucketless
+ assertNull(S3VirtualHostFilter.extractBucket("s3.localhost.example.internal", "localhost", withExtra));
+
+ // Without the suffix configured, the same forms do NOT route (no accidental match)
+ assertNull(S3VirtualHostFilter.extractBucket(
+ "my-bucket.localhost.example.internal", "localhost", DEFAULT_SUFFIXES));
+ assertNull(S3VirtualHostFilter.extractBucket(
+ "my-bucket.s3.localhost.example.internal", "localhost", DEFAULT_SUFFIXES));
+ }
+
+ // --- The configured hostname is a DNS suffix too, and routes the same forms ---
+
+ @Test
+ void routesVirtualHostedBucketsForConfiguredHostname() {
+ String hostname = "aws.mycorp.test";
+ Set<String> withHostname =
+ S3VirtualHostFilter.buildServiceHostSuffixes(Optional.of(hostname), Optional.empty());
+
+ // EmbeddedDnsServer makes *.<hostname> resolvable, so the derived set must carry it too,
+ // lowercased like the rest of the set because matching is case-insensitive
+ assertTrue(withHostname.contains(hostname));
+ assertTrue(S3VirtualHostFilter.buildServiceHostSuffixes(Optional.of("AWS.MyCorp.Test"), Optional.empty())
+ .contains(hostname));
+
+ // bucket.s3.<hostname> resolves via wildcard DNS, so it must route as a bucket rather
+ // than fall through to path-style with "s3" read as the bucket name
+ assertEquals("my-bucket", S3VirtualHostFilter.extractBucket(
+ "my-bucket.s3." + hostname, hostname, withHostname));
+ assertEquals("my-bucket", S3VirtualHostFilter.extractBucket(
+ "my-bucket.s3." + hostname + ":4566", hostname, withHostname));
+
+ // The forms already covered by baseHostname matching keep working
+ assertEquals("my-bucket", S3VirtualHostFilter.extractBucket(
+ "my-bucket." + hostname, hostname, withHostname));
+ assertEquals("my-bucket", S3VirtualHostFilter.extractBucket(
+ "my-bucket.s3.us-east-1." + hostname, hostname, withHostname));
+
+ // The bare s3.<hostname> service host stays bucketless
+ assertNull(S3VirtualHostFilter.extractBucket("s3." + hostname, hostname, withHostname));
+
+ // Without the hostname configured, bucket.s3.<hostname> does not route (no accidental match)
+ assertNull(S3VirtualHostFilter.extractBucket(
+ "my-bucket.s3." + hostname, "localhost", DEFAULT_SUFFIXES));
}
// --- Hostname extraction from URL ---
@@ -178,7 +276,7 @@ class S3VirtualHostFilterTest {
// be recovered from the URI authority instead of falling through to path-style.
URI uri = URI.create("https://my-bucket.s3.us-east-1.localhost:4566/key.txt");
String host = S3VirtualHostFilter.resolveHost(null, uri);
- assertEquals("my-bucket", S3VirtualHostFilter.extractBucket(host, "localhost"));
+ assertEquals("my-bucket", S3VirtualHostFilter.extractBucket(host, "localhost", DEFAULT_SUFFIXES));
}
// --- HTTP/2 website request: the path rewrite must preserve the s3-website authority (#1954) ---