fix(s3): fail fast on unwritable data root instead of opaque 500s (#1868)

When the persistent path is not writable (read-only or root-owned bind
mounts), S3Service's constructor failed lazily and every S3 request
surfaced as an opaque plain-text 500 that REST-XML SDK parsers cannot
read.

- validate the persistent path at boot and refuse to start with an
  actionable error when any enabled service uses non-memory storage
- add class-scoped exception mappers so unhandled failures on S3 and
  S3-Control routes render the S3 InternalError XML contract instead of
  Quarkus's plain-text error page
- warn loudly from the container entrypoint when the state dir is not
  writable by the runtime user

Fixes #1664
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-07-15 00:43:11 -04:00
1 parent 1ba15f0ce7
commit fe49bee41e
10 files changed
+386 -2

No files matched your search

@@ -0,0 +1,77 @@
package io.github.hectorvent.floci.core.storage;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.ResolvedServiceCatalog;
import io.github.hectorvent.floci.core.common.ServiceDescriptor;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
/**
* Validates at boot that the persistent storage path is usable whenever any enabled service
* runs with a non-memory storage mode. Without this check the failure surfaces lazily and
* confusingly: S3 answers every request with an opaque 500 (its service bean creates its data
* directory in the constructor), while other services silently lose data at flush time.
*/
@ApplicationScoped
public class PersistentPathValidator {
private final ResolvedServiceCatalog catalog;
private final EmulatorConfig config;
@Inject
public PersistentPathValidator(ResolvedServiceCatalog catalog, EmulatorConfig config) {
this.catalog = catalog;
this.config = config;
}
/**
* @throws IllegalStateException when persistence is enabled but the path is unusable
*/
public void validateAtBoot() {
List<ServiceDescriptor> persistent = catalog.all().stream()
.filter(d -> d.enabled() && d.supportsStorage() && !"memory".equals(d.storageMode()))
.toList();
if (persistent.isEmpty()) {
return;
}
Path root = Path.of(config.storage().persistentPath());
try {
probeWritable(root);
Path s3Root = root.resolve("s3");
boolean s3Persistent = persistent.stream().anyMatch(d -> "s3".equals(d.storageKey()));
if (s3Persistent && Files.isDirectory(s3Root)) {
probeWritable(s3Root);
}
} catch (IOException | SecurityException e) {
String services = persistent.stream()
.map(d -> d.storageKey() + "=" + d.storageMode())
.distinct()
.limit(8)
.reduce((a, b) -> a + ", " + b)
.orElse("");
throw new IllegalStateException(
"Persistent storage path '" + root.toAbsolutePath()
+ "' is not writable, but non-memory storage is enabled (" + services
+ "). Fix the volume mount permissions (it may be read-only or root-owned),"
+ " or point FLOCI_STORAGE_PERSISTENT_PATH at a writable directory.", e);
}
}
static void probeWritable(Path dir) throws IOException {
Files.createDirectories(dir);
Path probe = Files.createTempFile(dir, ".floci-write-probe", null);
try {
Files.deleteIfExists(probe);
} catch (IOException e) {
// The write itself succeeded, so the path is writable; a failed cleanup
// must not abort boot as a false "not writable".
probe.toFile().deleteOnExit();
}
}
}
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.lifecycle;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.ContainerTeardown;
import io.github.hectorvent.floci.core.common.ServiceRegistry;
import io.github.hectorvent.floci.core.storage.PersistentPathValidator;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.lifecycle.inithook.InitializationHook;
import io.github.hectorvent.floci.lifecycle.inithook.InitializationHooksRunner;
@@ -85,6 +86,7 @@ public class EmulatorLifecycle {
private final InitLifecycleState initLifecycleState;
private final SchemaCreationWorker schemaCreationWorker;
private final jakarta.enterprise.inject.Instance<ContainerTeardown> containerTeardowns;
private final PersistentPathValidator persistentPathValidator;
@Inject
public EmulatorLifecycle(StorageFactory storageFactory, ServiceRegistry serviceRegistry,
@@ -111,7 +113,8 @@ public class EmulatorLifecycle {
FlociUiManager flociUiManager,
InitLifecycleState initLifecycleState,
SchemaCreationWorker schemaCreationWorker,
jakarta.enterprise.inject.Instance<ContainerTeardown> containerTeardowns) {
jakarta.enterprise.inject.Instance<ContainerTeardown> containerTeardowns,
PersistentPathValidator persistentPathValidator) {
this.storageFactory = storageFactory;
this.serviceRegistry = serviceRegistry;
this.config = config;
@@ -138,6 +141,7 @@ public class EmulatorLifecycle {
this.initLifecycleState = initLifecycleState;
this.schemaCreationWorker = schemaCreationWorker;
this.containerTeardowns = containerTeardowns;
this.persistentPathValidator = persistentPathValidator;
}
void onStart(@Observes StartupEvent ignored) {
@@ -158,6 +162,8 @@ public class EmulatorLifecycle {
}
initLifecycleState.markBootCompleted();
persistentPathValidator.validateAtBoot();
serviceRegistry.logEnabledServices();
storageFactory.loadAll();
schemaCreationWorker.recoverOrphans();
@@ -15,6 +15,8 @@ import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import org.jboss.logging.Logger;
import org.jboss.resteasy.reactive.server.ServerExceptionMapper;
import jakarta.ws.rs.core.Response;
import java.net.URLDecoder;
@@ -35,6 +37,8 @@ import java.util.UUID;
@Produces(MediaType.APPLICATION_XML)
public class S3ControlController {
private static final Logger LOG = Logger.getLogger(S3ControlController.class);
private static final String AMZ_REQUEST_ID = "x-amz-request-id";
private static final String AMZN_REQUEST_ID = "x-amzn-RequestId";
private static final String AMZ_ID_2 = "x-amz-id-2";
@@ -213,6 +217,20 @@ public class S3ControlController {
* bare {@code <Error>} collapses to "UnknownError" at the SDK layer.
* See issue #557.
*/
// Same protocol safety net as S3Controller: unhandled failures on S3 Control routes
// must render the REST-XML error contract, not Quarkus's plain-text error page.
@ServerExceptionMapper
public Response mapUnhandledThrowable(Throwable t) {
LOG.error("Unhandled exception processing S3 Control request", t);
return xmlErrorResponse(new AwsException("InternalError",
"We encountered an internal error. Please try again.", 500));
}
@ServerExceptionMapper
public Response mapEscapedAwsException(AwsException e) {
return xmlErrorResponse(e);
}
private Response xmlErrorResponse(AwsException e) {
String requestId = UUID.randomUUID().toString();
String xml = new XmlBuilder()
@@ -60,6 +60,7 @@ import javax.xml.stream.XMLStreamReader;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jboss.logging.Logger;
import org.jboss.resteasy.reactive.server.ServerExceptionMapper;
/**
* S3 controller handling REST-style S3 API requests.
@@ -2145,6 +2146,23 @@ public class S3Controller {
+ "<ul><li>Code: " + code + "</li><li>Message: " + message + "</li></ul></body></html>";
}
// Unhandled failures on S3 routes (e.g. lazy CDI bean instantiation throwing inside an
// endpoint body) must render S3's InternalError XML contract, never Quarkus's plain-text
// error page, which SDK REST-XML parsers cannot read. Class-scoped: S3 endpoints only.
@ServerExceptionMapper
public Response mapUnhandledThrowable(Throwable t) {
LOG.error("Unhandled exception processing S3 request", t);
return xmlErrorResponse(new AwsException("InternalError",
"We encountered an internal error. Please try again.", 500));
}
// An AwsException escaping an endpoint body would otherwise hit the global JSON
// AwsExceptionMapper (exact-type match wins over the class-scoped Throwable mapper).
@ServerExceptionMapper
public Response mapEscapedAwsException(AwsException e) {
return xmlErrorResponse(e);
}
private Response xmlErrorResponse(AwsException e) {
String condition = e instanceof S3PreconditionFailedException preconditionFailedException
? preconditionFailedException.condition()