From f4a534c943fc7cbc77cf027da4bafc7984df74c2 Mon Sep 17 00:00:00 2001 From: ram-sarf Date: Sat, 23 May 2026 01:51:15 +0300 Subject: [PATCH] fix(s3): handle PutBucketRequestPayment instead of falling through to CreateBucket (#994) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PUT /{bucket}?requestPayment was not recognised as a sub-resource by S3Controller, so the request fell through to the bucket-creation handler. For any pre-existing bucket that returned BucketAlreadyOwnedByYou (HTTP 409) — which broke real workloads such as Terraform's aws_s3_bucket_request_payment_configuration on every re-apply. Add explicit handling for the requestPayment sub-resource, mirroring the existing OwnershipControls / Acl flow: - Bucket model gains a requestPaymentPayer field. - S3Service.putBucketRequestPayment validates the bucket exists (NoSuchBucket / 404) and the payer value (BucketOwner | Requester); invalid payers return MalformedXML / 400 to match real S3. - S3Service.getBucketRequestPayment returns the canonical RequestPaymentConfiguration XML, defaulting to BucketOwner when never set — real S3 never returns 404 for GetBucketRequestPayment. - Controller dispatches PUT and GET ?requestPayment to the new methods. No DELETE handler is added; AWS does not expose one. Regression test asserts the original Terraform-breaking case (a second PUT on an existing bucket must return 200, not 409) along with default GET, MalformedXML on invalid payer, and NoSuchBucket on missing bucket. --- .../floci/services/s3/S3Controller.java | 7 ++ .../floci/services/s3/S3Service.java | 41 ++++++ .../floci/services/s3/model/Bucket.java | 4 + .../s3/S3RequestPaymentIntegrationTest.java | 119 ++++++++++++++++++ 4 files changed, 171 insertions(+) create mode 100644 src/test/java/io/github/hectorvent/floci/services/s3/S3RequestPaymentIntegrationTest.java 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.java index 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.java index 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 implementation getObject(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.java index 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 string private String publicAccessBlockConfiguration; // XML string private String ownershipControlsConfiguration; // XML string + private String requestPaymentPayer; // "BucketOwner" (default) or "Requester"; null until first PUT private 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.java new file mode 100644 index 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 = """ + + Requester + + """; + private static final String BUCKET_OWNER_PAYS_XML = """ + + BucketOwner + + """; + + @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("BucketOwner")); + } + + @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("Requester")); + } + + /** + * 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(""" + + SomeoneElse + + """) + .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")); + } +}