Light, fluffy, and always free - The AWS Local Emulator alternative
fix(s3): serve custom error document for missing keys in static websi… (#1334)
Eduardo Lugo Vela28a1427ea8b5
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.javaindex 9a864b3a34..4ebd776474 100644--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java@@ -307,7 +307,8 @@ public class S3Controller {@QueryParam("encoding-type") String encodingType,@QueryParam("key-marker") String keyMarker,@QueryParam("marker") String marker,- @Context UriInfo uriInfo) {+ @Context UriInfo uriInfo,+ @Context HttpHeaders httpHeaders) {try {validateRawUri();if (hasQueryParam(uriInfo, "uploads")) {@@ -368,7 +369,7 @@ public class S3Controller {}// --- Website Hosting Redirection Logic ---- if (uriInfo.getQueryParameters().isEmpty() || (uriInfo.getQueryParameters().size() == 1 && hasQueryParam(uriInfo, "list-type"))) {+ if (isWebsiteRequest(httpHeaders) && (uriInfo.getQueryParameters().isEmpty() || (uriInfo.getQueryParameters().size() == 1 && hasQueryParam(uriInfo, "list-type")))) {try {WebsiteConfiguration webConfig = s3Service.getBucketWebsite(bucket);if (webConfig.getIndexDocument() != null) {@@ -381,7 +382,8 @@ public class S3Controller {.header("x-amz-website-redirect-location", "index").build();} catch (AwsException e) {- // If index.html is missing, we could serve ErrorDocument, but for now we fall back to listObjects+ Response r = serveErrorDocument(bucket, webConfig);+ if (r != null) return r;}}} catch (AwsException e) {@@ -634,6 +636,14 @@ public class S3Controller {return fullObjectResponse(bucket, key, versionId, obj, overrides);} catch (AwsException e) {+ if ("NoSuchKey".equals(e.getErrorCode()) && isWebsiteRequest(httpHeaders)) {+ try {+ WebsiteConfiguration webConfig = s3Service.getBucketWebsite(bucket);+ Response r = serveErrorDocument(bucket, webConfig);+ if (r != null) return r;+ } catch (AwsException ignored) {+ }+ }return xmlErrorResponse(e);}}@@ -1988,6 +1998,34 @@ public class S3Controller {}}+ private static boolean isWebsiteRequest(HttpHeaders httpHeaders) {+ String host = httpHeaders.getHeaderString("Host");+ return host != null && host.contains("s3-website");+ }++ private Response serveErrorDocument(String bucket, WebsiteConfiguration cfg) {+ if (cfg.getErrorDocument() == null) {+ return null;+ }+ try {+ S3Object err = s3Service.getObject(bucket, cfg.getErrorDocument());+ return Response.status(404)+ .entity(err.getData())+ .type(err.getContentType())+ .header("Content-Length", err.getSize())+ .header("x-amz-error-code", "NoSuchKey")+ .header("x-amz-error-message", "The specified key does not exist.")+ .build();+ } catch (AwsException ignored) {+ return Response.status(404)+ .entity("<html><head><title>404 Not Found</title></head>\n<body><h1>404 Not Found</h1>\n<ul><li>Code: NoSuchKey</li><li>Message: The specified key does not exist.</li></ul></body></html>")+ .type(MediaType.TEXT_HTML)+ .header("x-amz-error-code", "NoSuchKey")+ .header("x-amz-error-message", "The specified key does not exist.")+ .build();+ }+ }+private Response xmlErrorResponse(AwsException e) {String condition = e instanceof S3PreconditionFailedException preconditionFailedException? preconditionFailedException.condition()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.javaindex e5714fa497..a55577b778 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@@ -205,6 +205,10 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {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;+ }return false;}}diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3WebsiteIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3WebsiteIntegrationTest.javaindex dc4d23216c..963a3e3e99 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3WebsiteIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3WebsiteIntegrationTest.java@@ -1,6 +1,7 @@package io.github.hectorvent.floci.services.s3;import io.quarkus.test.junit.QuarkusTest;+import io.restassured.RestAssured;import org.junit.jupiter.api.MethodOrderer;import org.junit.jupiter.api.Order;import org.junit.jupiter.api.Test;@@ -15,6 +16,10 @@ class S3WebsiteIntegrationTest {private static final String BUCKET = "website-test-bucket";+ private String websiteHost() {+ return BUCKET + ".s3-website-us-east-1.localhost:" + RestAssured.port;+ }+@Test@Order(1)void setupBucket() {@@ -74,19 +79,33 @@ class S3WebsiteIntegrationTest {@Test@Order(5)- void indexRedirectionNotWorkingYetWithoutIndexFile() {- // Access root - should return XML list because index.html is missing+ void uploadErrorFile() {given()+ .contentType("text/html")+ .body("<html><body>Custom Error</body></html>").when()- .get("/" + BUCKET)+ .put("/" + BUCKET + "/error.html").then()- .statusCode(200)- .contentType("application/xml")- .body(containsString("<ListBucketResult"));+ .statusCode(200);}@Test@Order(6)+ void missingIndexServesErrorDocument() {+ given()+ .header("Host", websiteHost())+ .when()+ .get("/")+ .then()+ .statusCode(404)+ .contentType("text/html")+ .header("x-amz-error-code", "NoSuchKey")+ .header("x-amz-error-message", "The specified key does not exist.")+ .body(equalTo("<html><body>Custom Error</body></html>"));+ }++ @Test+ @Order(7)void uploadIndexFile() {given().contentType("text/html")@@ -98,12 +117,12 @@ class S3WebsiteIntegrationTest {}@Test- @Order(7)+ @Order(8)void indexRedirectionWorks() {- // Access root - should now return index.html contentgiven()+ .header("Host", websiteHost()).when()- .get("/" + BUCKET)+ .get("/").then().statusCode(200).contentType("text/html")@@ -111,7 +130,59 @@ class S3WebsiteIntegrationTest {}@Test- @Order(8)+ @Order(9)+ void missingKeyServesErrorDocument() {+ given()+ .header("Host", websiteHost())+ .when()+ .get("/missing-page")+ .then()+ .statusCode(404)+ .contentType("text/html")+ .header("x-amz-error-code", "NoSuchKey")+ .header("x-amz-error-message", "The specified key does not exist.")+ .body(equalTo("<html><body>Custom Error</body></html>"));+ }++ @Test+ @Order(10)+ void missingKeyReturnsXmlForRegularApiRequest() {+ given()+ .when()+ .get("/" + BUCKET + "/missing-page")+ .then()+ .statusCode(404)+ .body(containsString("NoSuchKey"));+ }++ @Test+ @Order(11)+ void missingErrorDocumentServesHtmlFallback() {+ given().delete("/" + BUCKET + "/error.html");++ given()+ .header("Host", websiteHost())+ .when()+ .get("/missing-page")+ .then()+ .statusCode(404)+ .contentType("text/html")+ .header("x-amz-error-code", "NoSuchKey")+ .header("x-amz-error-message", "The specified key does not exist.")+ .body(containsString("404 Not Found"))+ .body(containsString("NoSuchKey"));++ given()+ .contentType("text/html")+ .body("<html><body>Custom Error</body></html>")+ .when()+ .put("/" + BUCKET + "/error.html")+ .then()+ .statusCode(200);+ }++ @Test+ @Order(12)void deleteWebsiteConfiguration() {given().queryParam("website", "")@@ -120,7 +191,6 @@ class S3WebsiteIntegrationTest {.then().statusCode(204);- // Verify it's gonegiven().queryParam("website", "").when()@@ -130,9 +200,10 @@ class S3WebsiteIntegrationTest {}@Test- @Order(9)+ @Order(13)void cleanup() {given().delete("/" + BUCKET + "/index.html");+ given().delete("/" + BUCKET + "/error.html");given().delete("/" + BUCKET);}}diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsDynamoDbIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsDynamoDbIntegrationTest.javaindex 3e39a4990e..2b515ba59c 100644--- a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsDynamoDbIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsDynamoDbIntegrationTest.java@@ -645,7 +645,10 @@ class StepFunctionsDynamoDbIntegrationTest {.post("/");String status = resp.jsonPath().getString("status");if ("SUCCEEDED".equals(status)) {- return resp.jsonPath().getString("output");+ String output = resp.jsonPath().getString("output");+ if (output != null) {+ return output;+ }}if ("FAILED".equals(status) || "ABORTED".equals(status)) {fail("Execution " + status + ": " + resp.body().asString());