Light, fluffy, and always free - The AWS Local Emulator alternative
fix(s3): handle PutBucketRequestPayment instead of falling through to CreateBucket (#994)
ram-sarff4a534c943fc
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 ade46985db..6da2ab9457 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@@ -196,6 +196,10 @@ public class S3Controller {s3Service.putBucketOwnershipControls(bucket, new String(body, StandardCharsets.UTF_8));return Response.ok().build();}+ if (hasQueryParam(uriInfo, "requestPayment")) {+ s3Service.putBucketRequestPayment(bucket, new String(body, StandardCharsets.UTF_8));+ return Response.ok().build();+ }String locationConstraint = null;if (body != null && body.length > 0) {@@ -334,6 +338,9 @@ public class S3Controller {if (hasQueryParam(uriInfo, "ownershipControls")) {return Response.ok(s3Service.getBucketOwnershipControls(bucket)).build();}+ if (hasQueryParam(uriInfo, "requestPayment")) {+ return Response.ok(s3Service.getBucketRequestPayment(bucket)).build();+ }// --- Website Hosting Redirection Logic ---if (uriInfo.getQueryParameters().isEmpty() || (uriInfo.getQueryParameters().size() == 1 && hasQueryParam(uriInfo, "list-type"))) {diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.javaindex 63312a3492..95ba239495 100644--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.s3;import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.AwsArnUtils;import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.core.common.AwsNamespaces;import io.github.hectorvent.floci.core.common.RegionResolver;import io.github.hectorvent.floci.core.common.XmlBuilder;import io.github.hectorvent.floci.core.common.XmlParser;@@ -1515,6 +1516,46 @@ public class S3Service {bucketStore.put(bucketName, bucket);}+ /**+ * Stores the bucket Request Payment configuration. AWS only allows the values+ * {@code BucketOwner} and {@code Requester}; we accept either and reject anything+ * else with {@code MalformedXML} to match the real S3 behavior.+ */+ public void putBucketRequestPayment(String bucketName, String requestPaymentXml) {+ Bucket bucket = bucketStore.get(bucketName)+ .orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404));+ String payer = XmlParser.extractFirst(requestPaymentXml, "Payer", null);+ if (payer == null) {+ throw new AwsException("MalformedXML",+ "The XML you provided was not well-formed or did not validate against our published schema.",+ 400);+ }+ payer = payer.trim();+ if (!"BucketOwner".equals(payer) && !"Requester".equals(payer)) {+ throw new AwsException("MalformedXML",+ "The XML you provided was not well-formed or did not validate against our published schema.",+ 400);+ }+ bucket.setRequestPaymentPayer(payer);+ bucketStore.put(bucketName, bucket);+ }++ /**+ * Returns the bucket Request Payment configuration as XML. Buckets that have+ * never been configured return the AWS default ({@code BucketOwner}); this matches+ * real S3, which never returns 404 for {@code GetBucketRequestPayment}.+ */+ public String getBucketRequestPayment(String bucketName) {+ Bucket bucket = bucketStore.get(bucketName)+ .orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404));+ String payer = bucket.getRequestPaymentPayer() != null ? bucket.getRequestPaymentPayer() : "BucketOwner";+ return new XmlBuilder()+ .start("RequestPaymentConfiguration", AwsNamespaces.S3)+ .elem("Payer", payer)+ .end("RequestPaymentConfiguration")+ .build();+ }+public void restoreObject(String bucketName, String key, String versionId, String restoreXml) {// Validation only - stub implementationgetObject(bucketName, key, versionId);diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/model/Bucket.java b/src/main/java/io/github/hectorvent/floci/services/s3/model/Bucket.javaindex 84574248ab..69e9ff8d2a 100644--- a/src/main/java/io/github/hectorvent/floci/services/s3/model/Bucket.java+++ b/src/main/java/io/github/hectorvent/floci/services/s3/model/Bucket.java@@ -26,6 +26,7 @@ public class Bucket {private String encryptionConfiguration; // XML stringprivate String publicAccessBlockConfiguration; // XML stringprivate String ownershipControlsConfiguration; // XML string+ private String requestPaymentPayer; // "BucketOwner" (default) or "Requester"; null until first PUTprivate String region;private WebsiteConfiguration websiteConfiguration;@@ -95,6 +96,9 @@ public class Bucket {this.ownershipControlsConfiguration = ownershipControlsConfiguration;}+ public String getRequestPaymentPayer() { return requestPaymentPayer; }+ public void setRequestPaymentPayer(String requestPaymentPayer) { this.requestPaymentPayer = requestPaymentPayer; }+public String getRegion() { return region; }public void setRegion(String region) { this.region = region; }diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3RequestPaymentIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3RequestPaymentIntegrationTest.javanew file mode 100644index 0000000000..1d9f9bac76--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3RequestPaymentIntegrationTest.java@@ -0,0 +1,119 @@+package io.github.hectorvent.floci.services.s3;++import io.quarkus.test.junit.QuarkusTest;+import org.junit.jupiter.api.MethodOrderer;+import org.junit.jupiter.api.Order;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.api.TestMethodOrder;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.containsString;+import static org.hamcrest.Matchers.not;++@QuarkusTest+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)+class S3RequestPaymentIntegrationTest {++ private static final String BUCKET = "request-payment-int-test";+ private static final String REQUESTER_PAYS_XML = """+ <RequestPaymentConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">+ <Payer>Requester</Payer>+ </RequestPaymentConfiguration>+ """;+ private static final String BUCKET_OWNER_PAYS_XML = """+ <RequestPaymentConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">+ <Payer>BucketOwner</Payer>+ </RequestPaymentConfiguration>+ """;++ @Test+ @Order(1)+ void createBucket() {+ given()+ .when()+ .put("/" + BUCKET)+ .then()+ .statusCode(200);+ }++ @Test+ @Order(2)+ void getRequestPaymentBeforePutReturnsBucketOwnerDefault() {+ given()+ .when()+ .get("/" + BUCKET + "?requestPayment")+ .then()+ .statusCode(200)+ .body(containsString("<RequestPaymentConfiguration"))+ .body(containsString("<Payer>BucketOwner</Payer>"));+ }++ @Test+ @Order(3)+ void putRequestPaymentReturns200() {+ given()+ .body(REQUESTER_PAYS_XML)+ .when()+ .put("/" + BUCKET + "?requestPayment")+ .then()+ .statusCode(200);+ }++ @Test+ @Order(4)+ void getRequestPaymentReturnsStoredPayer() {+ given()+ .when()+ .get("/" + BUCKET + "?requestPayment")+ .then()+ .statusCode(200)+ .body(containsString("<Payer>Requester</Payer>"));+ }++ /**+ * Regression test for the Terraform-breaking bug where {@code PUT /{bucket}?requestPayment}+ * fell through to the bucket-creation handler and returned {@code 409 BucketAlreadyOwnedByYou}.+ * Real S3 allows {@code PutBucketRequestPayment} to be called repeatedly on an existing+ * bucket — and Terraform's {@code aws_s3_bucket_request_payment_configuration} resource+ * relies on this idempotency.+ */+ @Test+ @Order(5)+ void putRequestPaymentIsIdempotentOnExistingBucket() {+ given()+ .body(BUCKET_OWNER_PAYS_XML)+ .when()+ .put("/" + BUCKET + "?requestPayment")+ .then()+ .statusCode(200)+ .body(not(containsString("BucketAlreadyOwnedByYou")));+ }++ @Test+ @Order(6)+ void putRequestPaymentRejectsInvalidPayer() {+ given()+ .body("""+ <RequestPaymentConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">+ <Payer>SomeoneElse</Payer>+ </RequestPaymentConfiguration>+ """)+ .when()+ .put("/" + BUCKET + "?requestPayment")+ .then()+ .statusCode(400)+ .body(containsString("MalformedXML"));+ }++ @Test+ @Order(7)+ void putRequestPaymentOnMissingBucketReturns404() {+ given()+ .body(BUCKET_OWNER_PAYS_XML)+ .when()+ .put("/this-bucket-does-not-exist-rp?requestPayment")+ .then()+ .statusCode(404)+ .body(containsString("NoSuchBucket"));+ }+}