fix(s3): handle PutBucketRequestPayment instead of falling through to CreateBucket (#994)

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.
This commit is contained in:
ram-sarf authored and GitHub committed 2026-05-22 17:51:15 -05:00
1 parent 9286dd7602
commit f4a534c943
4 files changed
+171

No files matched your search

@@ -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"))) {
@@ -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);
@@ -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; }