fix(s3): route the replication subresource instead of falling through (#2519)

PUT /{bucket}?replication fell through to CreateBucket: in the default
region the idempotent-create path answered 200 with a Location header
and stored no replication configuration, and elsewhere it failed with
BucketAlreadyOwnedByYou. GET fell through to ListObjects and returned
a ListBucketResult the SDK silently parsed as an empty replication
configuration. Only DELETE was routed, as a no-op.

Route PUT and GET alongside the existing DELETE case, per the issue's
suggested fix: the configuration is stored verbatim on the bucket and
echoed back — Floci does not model replication behavior, and neither
the SDK nor the Terraform provider needs it to. The PUT body's
ReplicationConfiguration root is validated (MalformedXML otherwise);
GET on a bucket with no stored configuration answers
ReplicationConfigurationNotFoundError (404), which is what the SDK and
aws_s3_bucket_replication_configuration treat as "not configured".

Now that PUT stores a configuration, DeleteBucketReplication clears it
instead of no-opping (still an unconditional 204, matching AWS), and a
DELETE on a missing bucket answers NoSuchBucket instead of the no-op's
204.

IamActionRegistry maps the three verbs to the documented actions:
s3:PutReplicationConfiguration for PUT and DELETE (AWS authorizes
DeleteBucketReplication with the put action) and
s3:GetReplicationConfiguration for GET. The mapping mirrors the
controller's per-method dispatch order: on PUT and GET accelerate is
dispatched ahead of replication, while the DELETE chain routes
replication and never routes accelerate to an operation.

Closes #2296
This commit is contained in:
soreavis authored and GitHub committed 2026-08-25 22:51:56 -05:00
1 parent 60dd795703
commit ce4f6d1c48
7 files changed
+507 -14

No files matched your search

@@ -170,6 +170,12 @@ public class IamActionRegistry {
"uploads", "notification", "versioning", "versions", "location", "object-lock",
"website", "logging", "policy", "cors", "lifecycle", "encryption",
"publicAccessBlock", "ownershipControls", "requestPayment");
// S3Controller's DELETE chain dispatches these ahead of replication (tagging is
// resolved above). Accelerate is NOT here: on DELETE it is dispatched after
// replication. Mirrors the controller's dispatch order — extend together.
private static final List<String> DELETE_SUBRESOURCES_BEFORE_REPLICATION = List.of(
"website", "policy", "cors", "lifecycle", "encryption",
"publicAccessBlock", "ownershipControls");
/**
* Resolves S3 sub-resource ops (ACL, tagging, retention, etc.) that
@@ -182,7 +188,8 @@ public class IamActionRegistry {
boolean acl = params.containsKey("acl");
boolean tagging = params.containsKey("tagging");
boolean accelerate = params.containsKey("accelerate");
if (!acl && !tagging && !accelerate) {
boolean replication = params.containsKey("replication");
if (!acl && !tagging && !accelerate && !replication) {
return null;
}
// /{bucket}?acl → bucket-level; /{bucket}/{key}?acl → object-level
@@ -223,24 +230,49 @@ public class IamActionRegistry {
default -> null;
};
}
// Accelerate is a bucket-only subresource, and ?accelerate on an object path is
// inert — the object routes ignore it — so only a bucket-level request maps here;
// everything else falls through to the standard rule table.
// Accelerate and replication are bucket-only subresources; on an object path
// both are inert — the object routes ignore them — so only a bucket-level
// request maps here; everything else falls through to the standard rule table.
if (!isBucketLevel) {
return null;
}
List<String> dispatchedFirst = "PUT".equals(method)
? PUT_SUBRESOURCES_BEFORE_ACCELERATE
: GET_SUBRESOURCES_BEFORE_ACCELERATE;
// S3Controller's PUT and GET chains dispatch accelerate ahead of replication,
// so accelerate resolves when both ride along. Its DELETE chain routes
// replication and never routes accelerate to an operation, so on DELETE
// replication resolves even when accelerate is also present.
if (accelerate && !(replication && "DELETE".equals(method))) {
List<String> dispatchedFirst = "PUT".equals(method)
? PUT_SUBRESOURCES_BEFORE_ACCELERATE
: GET_SUBRESOURCES_BEFORE_ACCELERATE;
for (String subresource : dispatchedFirst) {
if (params.containsKey(subresource)) {
return null;
}
}
return switch (method) {
case "GET" -> "s3:GetAccelerateConfiguration";
case "PUT" -> "s3:PutAccelerateConfiguration";
// AWS defines no DELETE for the subresource.
default -> null;
};
}
// Replication. On PUT and GET this is only reached without ?accelerate, so the
// accelerate before-lists cover everything dispatched ahead of replication too.
List<String> dispatchedFirst = switch (method) {
case "PUT" -> PUT_SUBRESOURCES_BEFORE_ACCELERATE;
case "GET" -> GET_SUBRESOURCES_BEFORE_ACCELERATE;
default -> DELETE_SUBRESOURCES_BEFORE_REPLICATION;
};
for (String subresource : dispatchedFirst) {
if (params.containsKey(subresource)) {
return null;
}
}
return switch (method) {
case "GET" -> "s3:GetAccelerateConfiguration";
case "PUT" -> "s3:PutAccelerateConfiguration";
// AWS defines no DELETE for the subresource.
case "GET" -> "s3:GetReplicationConfiguration";
case "PUT" -> "s3:PutReplicationConfiguration";
// AWS authorizes DeleteBucketReplication with the put action.
case "DELETE" -> "s3:PutReplicationConfiguration";
default -> null;
};
}
@@ -324,6 +324,10 @@ public class S3Controller {
s3Service.putBucketAccelerateConfiguration(bucket, new String(body, StandardCharsets.UTF_8));
return Response.ok().build();
}
if (hasQueryParam(uriInfo, "replication")) {
s3Service.putBucketReplication(bucket, new String(body, StandardCharsets.UTF_8));
return Response.ok().build();
}
// Must be handled here: an unmatched subresource falls through to CreateBucket below,
// which answers a metrics call with BucketAlreadyOwnedByYou.
if (hasQueryParam(uriInfo, "metrics")) {
@@ -407,9 +411,7 @@ public class S3Controller {
return Response.noContent().build();
}
if (hasQueryParam(uriInfo, "replication")) {
// Floci does not model bucket replication; DeleteBucketReplication is a
// no-op that always returns 204, matching real S3. Crucially it must be
// handled here so it does NOT fall through to deleting the whole bucket.
s3Service.deleteBucketReplication(bucket);
return Response.noContent().build();
}
if (hasQueryParam(uriInfo, "metrics")) {
@@ -528,6 +530,10 @@ public class S3Controller {
s3Service.authorizeBucketRead(bucket, "s3:GetAccelerateConfiguration", authorization);
return Response.ok(s3Service.getBucketAccelerateConfiguration(bucket)).build();
}
if (hasQueryParam(uriInfo, "replication")) {
s3Service.authorizeBucketRead(bucket, "s3:GetReplicationConfiguration", authorization);
return Response.ok(s3Service.getBucketReplication(bucket)).build();
}
// GetBucketMetricsConfiguration and ListBucketMetricsConfigurations share ?metrics and
// are told apart by the id, which only the single-configuration read carries.
if (hasQueryParam(uriInfo, "metrics")) {
@@ -2497,6 +2497,42 @@ public class S3Service implements Resettable, ResourceProvider {
.build();
}
/**
* Stores the bucket replication configuration verbatim. Floci does not model
* replication behavior — the configuration is only stored and echoed back,
* which is what the SDK and the Terraform provider need. The
* ReplicationConfiguration root is required, so a body that does not parse
* to one is rejected with {@code MalformedXML}.
*/
public void putBucketReplication(String bucketName, String replicationXml) {
Bucket bucket = bucketStore.get(bucketName)
.orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404));
if (!"ReplicationConfiguration".equals(XmlParser.rootElementName(replicationXml))) {
throw new AwsException("MalformedXML",
"The XML you provided was not well-formed or did not validate against our published schema.",
400);
}
bucket.setReplicationConfiguration(replicationXml);
bucketStore.put(bucketName, bucket);
}
public String getBucketReplication(String bucketName) {
Bucket bucket = bucketStore.get(bucketName)
.orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404));
if (bucket.getReplicationConfiguration() == null) {
throw new AwsException("ReplicationConfigurationNotFoundError",
"The replication configuration was not found", 404);
}
return bucket.getReplicationConfiguration();
}
public void deleteBucketReplication(String bucketName) {
Bucket bucket = bucketStore.get(bucketName)
.orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404));
bucket.setReplicationConfiguration(null);
bucketStore.put(bucketName, bucket);
}
public void restoreObject(String bucketName, String key, String versionId, String restoreXml) {
// Validation only - stub implementation
getObject(bucketName, key, versionId);
@@ -27,6 +27,7 @@ public class Bucket {
private String encryptionConfiguration; // XML string
private String publicAccessBlockConfiguration; // XML string
private String ownershipControlsConfiguration; // XML string
private String replicationConfiguration; // XML string; null until first PUT
private String requestPaymentPayer; // "BucketOwner" (default) or "Requester"; null until first PUT
private String accelerateStatus; // "Enabled" or "Suspended"; null until first PUT
private String region;
@@ -108,6 +109,11 @@ public class Bucket {
this.ownershipControlsConfiguration = ownershipControlsConfiguration;
}
public String getReplicationConfiguration() { return replicationConfiguration; }
public void setReplicationConfiguration(String replicationConfiguration) {
this.replicationConfiguration = replicationConfiguration;
}
public String getRequestPaymentPayer() { return requestPaymentPayer; }
public void setRequestPaymentPayer(String requestPaymentPayer) { this.requestPaymentPayer = requestPaymentPayer; }