fix(s3): use ConfigProvider for runtime config lookup in S3VirtualHostFilter (#288)

Replace @ConfigProperty constructor injection with programmatic
ConfigProvider.getConfig() lookup to fix native image crash when
FLOCI_HOSTNAME is set at runtime. @PreMatching JAX-RS filters run
before CDI beans are fully initialized, so neither @ConfigProperty
nor @ConfigMapping beans are available during filter construction.

Also make bucket extraction hostname-aware: only treat the first label
as a bucket name when the remainder matches the configured base hostname
(or a well-known AWS S3 domain). This prevents false positives when
Floci sits behind a multi-label hostname like floci.svc.cluster.local.
This commit is contained in:
Matej Šnuderl authored and GitHub committed 2026-04-08 10:23:52 -05:00
1 parent 17841d2d5a
commit 944fddf5ac
2 files changed
+43 -20

No files matched your search

@@ -1,12 +1,11 @@
package io.github.hectorvent.floci.services.s3;
import jakarta.inject.Inject;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerRequestFilter;
import jakarta.ws.rs.container.PreMatching;
import jakarta.ws.rs.core.UriBuilder;
import jakarta.ws.rs.ext.Provider;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.config.ConfigProvider;
import java.net.URI;
import java.util.Optional;
@@ -17,10 +16,13 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
private final String baseHostname;
@Inject
public S3VirtualHostFilter(
@ConfigProperty(name = "floci.base-url", defaultValue = "http://localhost:4566") String baseUrl,
@ConfigProperty(name = "floci.hostname") Optional<String> hostname) {
public S3VirtualHostFilter() {
var config = ConfigProvider.getConfig();
String baseUrl = config
.getOptionalValue("floci.base-url", String.class)
.orElse("http://localhost:4566");
Optional<String> hostname = config
.getOptionalValue("floci.hostname", String.class);
String effectiveUrl = hostname
.map(h -> baseUrl.replaceFirst("://[^:/]+(:\\d+)?", "://" + h + "$1"))
.orElse(baseUrl);
@@ -71,19 +73,21 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
* matches a well-known AWS S3 domain pattern (for DNS-redirect setups).
*
* Examples with baseHostname="localhost":
* my-bucket.localhost:4566 → "my-bucket"
* my-bucket.localhost → "my-bucket"
* floci.svc.cluster.local → null (no bucket prefix, path-style)
* my-svc.floci.svc.cluster.local → null (remainder doesn't match "localhost")
* my-bucket.localhost:4566 -> "my-bucket"
* my-bucket.localhost -> "my-bucket"
* floci.svc.cluster.local -> null (no bucket prefix, path-style)
* my-svc.floci.svc.cluster.local -> null (remainder doesn't match "localhost")
*
* Examples with baseHostname="floci.svc.cluster.local":
* my-bucket.floci.svc.cluster.local → "my-bucket"
* floci.svc.cluster.local → null (no bucket prefix, path-style)
* my-bucket.floci.svc.cluster.local -> "my-bucket"
* floci.svc.cluster.local -> null (no bucket prefix, path-style)
*
* Returns null if the host does not match a virtual-hosted pattern.
*/
static String extractBucket(String host, String baseHostname) {
if (host == null) return null;
if (host == null) {
return null;
}
// Strip port if present
String hostname = stripPort(host);
@@ -117,7 +121,9 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
/** Extracts the hostname (without scheme or port) from a URL string. */
static String extractHostnameFromUrl(String url) {
if (url == null) return null;
if (url == null) {
return null;
}
try {
URI uri = URI.create(url);
return uri.getHost();
@@ -147,11 +153,15 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
return true;
}
/** Returns true for *.s3.amazonaws.com and *.s3.<region>.amazonaws.com domains. */
/** Returns true for *.s3.amazonaws.com and *.s3.region.amazonaws.com domains. */
private static boolean isAwsS3Domain(String remainder) {
if ("s3.amazonaws.com".equals(remainder)) return true;
if ("s3.amazonaws.com".equals(remainder)) {
return true;
}
// s3.<region>.amazonaws.com
if (remainder.startsWith("s3.") && remainder.endsWith(".amazonaws.com")) return true;
if (remainder.startsWith("s3.") && remainder.endsWith(".amazonaws.com")) {
return true;
}
return false;
}
}