mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(s3): derive S3 host suffix matching from DNS config (#1843)
* fix(s3): derive S3 host suffix matching from DNS config
S3VirtualHostFilter hardcoded the wildcard-DNS suffixes (localhost.floci.io,
localhost.localstack.cloud) in two places: the bare service-host check
(isS3ServiceEndpointHost) and the virtual-hosted bucket fallback (isAwsS3Domain).
Both now derive their suffix set from the embedded DNS source of truth plus any
configured floci.dns.extra-suffixes, so a custom DNS suffix is recognised the same
way the builtins are.
Why: the two literal lists duplicated what the DNS server already knows and could
drift from it silently. Deriving from EmbeddedDnsServer.BUILTIN_SUFFIXES keeps them
in lockstep, and folding in extra-suffixes means a user migrating from LocalStack
via a custom suffix gets consistent S3 host handling (bucket routing and service
endpoint detection), not just the DNS resolution they get today.
How:
- buildServiceHostSuffixes() computes {localhost} + BUILTIN_SUFFIXES + extra-suffixes
once per instance; isS3ServiceEndpointHost() checks membership instead of literals.
- matchesConfiguredSuffixHost() routes the virtual-hosted forms bucket.<suffix>,
bucket.s3.<suffix> and bucket.s3.<region>.<suffix> for every configured suffix,
replacing the hardcoded localstack/floci branches in isAwsS3Domain.
- EmbeddedDnsServer.BUILTIN_SUFFIXES widened to public so the filter reuses the list.
The virtual-host match now requires the full builtin suffix (.localhost.localstack.cloud)
rather than the bare parent domain (.localstack.cloud), matching LocalStack's actual
wildcard DNS (*.localhost.localstack.cloud).
* test(s3): derive DEFAULT_SUFFIXES from the production factory
The test constant hardcoded a copy of the builtin suffix set, which would
silently drift if a new builtin were added to EmbeddedDnsServer.BUILTIN_SUFFIXES.
Widen buildServiceHostSuffixes() to package-private and reuse it in the test so
the two stay in sync automatically.
* fix(s3): include the configured hostname in the derived suffix set
EmbeddedDnsServer resolves builtins + floci.hostname + floci.dns.extra-suffixes,
but the derived service-host set carried only two of the three. The configured
hostname reaches the filter as baseHostname, and matchesEndpointHost excludes the
bare s3.<host> remainder by design, so the bucket form one level down
(bucket.s3.<hostname>) matched nothing. DNS still resolves that host, so the
request reached Floci and was then handled path-style, binding the first path
segment as the bucket, rather than being refused.
Carry the hostname into buildServiceHostSuffixes so all three DNS inputs are
mirrored and a host that resolves by wildcard DNS routes under the same rules.
This commit is contained in:
3 files changed
+188
-34
No files matched your search
+114
-16
@@ -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) ---
|
||||
|
||||
Reference in new issue
Block a user