diff --git a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java index fec161005e..2cb25f7ab9 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java @@ -423,6 +423,33 @@ public class IamEnforcementFilter implements ContainerRequestFilter { abortIfDenied(ctx, caller, "s3:GetObject", credentialScope, resources, withoutObjectTags(targetContexts), region, accountId, akid); } + // A DeleteObject conditioned on an ETag reveals whether the object still has it, and the + // conditional-deletes guide requires s3:GetObject for it; If-Match: * (existence only) + // needs just s3:DeleteObject. The read is checked the way the PutObject case above is. + if ("s3:DeleteObject".equals(action) && isPlainDeleteObject(ctx) + && isETagCondition(ctx.getHeaderString("If-Match"))) { + abortIfDenied(ctx, caller, "s3:GetObject", credentialScope, resources, + withoutObjectTags(targetContexts), region, accountId, akid); + } + } + + // The DELETE subresources S3Controller.deleteObject routes away from DeleteObject (?uploadId= is + // AbortMultipartUpload, which the action registry also files under s3:DeleteObject). Everything + // else, including the x-id and X-Amz-* parameters SDKs and presigned URLs add, is a DeleteObject. + private static final Set NON_DELETE_OBJECT_SUBRESOURCES = Set.of("uploadId", "tagging", "annotation"); + + private static boolean isPlainDeleteObject(ContainerRequestContext ctx) { + return "DELETE".equalsIgnoreCase(ctx.getMethod()) + && ctx.getUriInfo().getQueryParameters().keySet().stream() + .noneMatch(NON_DELETE_OBJECT_SUBRESOURCES::contains); + } + + private static boolean isETagCondition(String ifMatch) { + if (ifMatch == null) { + return false; + } + String value = ifMatch.trim(); + return !value.equals("*") && !value.equals("\"*\""); } /** diff --git a/src/main/java/io/github/hectorvent/floci/core/common/XmlParser.java b/src/main/java/io/github/hectorvent/floci/core/common/XmlParser.java index 1453138374..a8f23de491 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/XmlParser.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/XmlParser.java @@ -187,11 +187,18 @@ public final class XmlParser { return extractAll(xml, elementName).stream().anyMatch(value::equals); } - /** A key paired with an optional version id, extracted from a {@code Delete} request's {@code } block. */ - public record KeyVersion(String key, String versionId) {} + /** + * A key paired with an optional version id and an optional {@code If-Match} ETag, extracted from a + * {@code Delete} request's {@code } block. + */ + public record KeyVersion(String key, String versionId, String eTag) { + public KeyVersion(String key, String versionId) { + this(key, versionId, null); + } + } /** - * Extracts {@code (Key, VersionId)} pairs from an S3 {@code DeleteObjects} request body, + * Extracts {@code (Key, VersionId, ETag)} entries from an S3 {@code DeleteObjects} request body, * one entry per {@code } block. * *

Unlike {@link #extractAll(String, String)}, which flattens every {@code } across @@ -199,8 +206,9 @@ public final class XmlParser { * {@code } block. This is needed so a batch delete can target the exact version named, * rather than discarding it and always falling back to "no version specified". * - *

{@code } is optional per the {@code Delete} request schema; an {@code } - * block that omits it yields an entry with a {@code null} versionId. + *

{@code } and {@code } are optional per the {@code Delete} request schema; an + * {@code } block that omits one yields an entry with {@code null} in its place. The ETag is a + * conditional-delete precondition, checked like an {@code If-Match} header on DeleteObject. * *
{@code
      * List entries = XmlParser.extractDeleteObjectEntries(body);
@@ -216,6 +224,7 @@ public final class XmlParser {
             boolean inObject = false;
             String key = null;
             String versionId = null;
+            String eTag = null;
             while (r.hasNext()) {
                 int event = r.next();
                 if (event == XMLStreamConstants.START_ELEMENT) {
@@ -224,15 +233,18 @@ public final class XmlParser {
                         inObject = true;
                         key = null;
                         versionId = null;
+                        eTag = null;
                     } else if (inObject && "Key".equals(local)) {
                         key = r.getElementText();
                     } else if (inObject && "VersionId".equals(local)) {
                         versionId = r.getElementText();
+                    } else if (inObject && "ETag".equals(local)) {
+                        eTag = r.getElementText();
                     }
                 } else if (event == XMLStreamConstants.END_ELEMENT
                         && inObject && "Object".equals(r.getLocalName())) {
                     if (key != null) {
-                        result.add(new KeyVersion(key, versionId));
+                        result.add(new KeyVersion(key, versionId, eTag));
                     }
                     inObject = false;
                 }
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 60317ba7b4..84cb9a698f 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
@@ -1347,11 +1347,13 @@ public class S3Controller {
             }
             boolean bypass = "true".equalsIgnoreCase(
                     httpHeaders.getHeaderString("x-amz-bypass-governance-retention"));
+            String ifMatch = httpHeaders.getHeaderString("If-Match");
             s3Service.authorizeDeleteObject(bucket, key, versionId, authorization);
+            authorizeDeleteETagRead(bucket, key, ifMatch, authorization);
             if (bypass) {
                 s3Service.authorizeObjectWrite(bucket, key, "s3:BypassGovernanceRetention", authorization);
             }
-            S3Object result = s3Service.deleteObject(bucket, key, versionId, bypass);
+            S3Object result = s3Service.deleteObject(bucket, key, versionId, bypass, ifMatch);
             var resp = Response.noContent();
             if (result != null) {
                 if (result.isDeleteMarker()) {
@@ -1542,6 +1544,7 @@ public class S3Controller {
         for (XmlParser.KeyVersion entry : entries) {
             try {
                 s3Service.authorizeDeleteObject(bucket, entry.key(), entry.versionId(), authorization);
+                authorizeDeleteETagRead(bucket, entry.key(), entry.eTag(), authorization);
                 if (bypass && s3Service.isGovernanceRetentionActive(
                         bucket, entry.key(), entry.versionId())) {
                     s3Service.authorizeObjectWrite(bucket, entry.key(),
@@ -1591,6 +1594,20 @@ public class S3Controller {
         return Response.ok(builder.build()).type(MediaType.APPLICATION_XML).build();
     }
 
+    // A conditional delete against an ETag reveals whether the object still has it, so S3 requires
+    // s3:GetObject as well as s3:DeleteObject. The existence check (If-Match: *) needs only the delete.
+    private void authorizeDeleteETagRead(String bucket, String key, String ifMatch,
+                                         S3Service.RequestAuthorization authorization) {
+        if (ifMatch == null) {
+            return;
+        }
+        String value = ifMatch.trim();
+        if (value.equals("*") || value.equals("\"*\"")) {
+            return;
+        }
+        s3Service.authorizeGetObject(bucket, key, null, authorization);
+    }
+
     private Response handleListParts(String bucket, String key, String uploadId,
                                       Integer maxPartsParam, String partNumberMarkerParam) {
         MultipartUpload upload = s3Service.listParts(bucket, key, uploadId);
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 486dec964b..701dc0211f 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
@@ -1503,18 +1503,50 @@ public class S3Service implements Resettable, ResourceProvider {
     }
 
     public S3Object deleteObject(String bucketName, String key, String versionId, boolean bypassGovernance) {
+        return deleteObject(bucketName, key, versionId, bypassGovernance, null);
+    }
+
+    /**
+     * Deletes an object, first checking an {@code If-Match} precondition when one is given.
+     *
+     * 

As on S3, the precondition is evaluated against the current version of the key even + * when {@code versionId} addresses an older one: a missing key, or a current delete marker, + * answers {@code NoSuchKey}, and an ETag that does not match answers {@code 412}. + */ + public S3Object deleteObject(String bucketName, String key, String versionId, boolean bypassGovernance, + String ifMatch) { Bucket bucket = bucketStore.get(bucketName) .orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404)); // The bucket monitor serializes this against PutObject's annotation cleanup and the // annotation subresource writes, which hold the same monitor (storeObjectInternal - // already runs under it via storeObject). + // already runs under it via storeObject). The precondition is checked under it too, + // so a concurrent overwrite cannot land between the check and the delete. synchronized (bucket) { + checkDeletePrecondition(bucketName, key, ifMatch); return deleteObjectLocked(bucket, bucketName, key, versionId, bypassGovernance); } } + // A current delete marker answers 404 NoSuchKey, the same as a missing key. That is deliberate: + // the S3 conditional-deletes guide says "If the latest version of the object is a delete marker, + // the object doesn't exist and the DeleteObject API will fail and return a 412 Precondition + // Failed response", but real S3 (general purpose bucket, ap-southeast-2, measured 2026-09-07) + // answered 404 NoSuchKey for If-Match: * over a current delete marker. Follow the measurement. + private void checkDeletePrecondition(String bucketName, String key, String ifMatch) { + if (ifMatch == null) { + return; + } + S3Object current = objectStore.get(objectKey(bucketName, key)).orElse(null); + if (current == null || current.isDeleteMarker()) { + throw new AwsException("NoSuchKey", "The specified key does not exist.", 404); + } + if (!eTagMatches(ifMatch, current.getETag())) { + throw new S3PreconditionFailedException("If-Match"); + } + } + private S3Object deleteObjectLocked(Bucket bucket, String bucketName, String key, String versionId, boolean bypassGovernance) { if (bucket.isVersioningEnabled() && versionId == null) { @@ -2028,7 +2060,8 @@ public class S3Service implements Resettable, ResourceProvider { List errors = new ArrayList<>(); for (XmlParser.KeyVersion entry : entries) { try { - S3Object result = deleteObject(bucketName, entry.key(), entry.versionId(), bypassGovernance); + S3Object result = deleteObject(bucketName, entry.key(), entry.versionId(), bypassGovernance, + entry.eTag()); if (result != null && result.isDeleteMarker()) { deleted.add(new DeleteResult(entry.key(), entry.versionId(), true, result.getVersionId())); } else { diff --git a/src/test/java/io/github/hectorvent/floci/core/common/XmlParserTest.java b/src/test/java/io/github/hectorvent/floci/core/common/XmlParserTest.java index 99b5ce2ab9..df13be48a3 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/XmlParserTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/XmlParserTest.java @@ -220,6 +220,24 @@ class XmlParserTest { assertEquals(new XmlParser.KeyVersion("b.txt", "v2"), entries.get(1)); } + @Test + void extractDeleteObjectEntriesKeepsEachObjectsETag() { + String xml = """ + + a.txt"abc" + b.txtv2 + c.txt* + + """; + + List entries = XmlParser.extractDeleteObjectEntries(xml); + + assertEquals(List.of( + new XmlParser.KeyVersion("a.txt", null, "\"abc\""), + new XmlParser.KeyVersion("b.txt", "v2", null), + new XmlParser.KeyVersion("c.txt", null, "*")), entries); + } + @Test void extractDeleteObjectEntriesVersionIdIsOptional() { String xml = """ diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/S3ConditionalDeleteIamEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/S3ConditionalDeleteIamEnforcementIntegrationTest.java new file mode 100644 index 0000000000..0c8f5d4971 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/iam/S3ConditionalDeleteIamEnforcementIntegrationTest.java @@ -0,0 +1,79 @@ +package io.github.hectorvent.floci.services.iam; + +import io.github.hectorvent.floci.testing.IamEnforcementProfile; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * A DeleteObject conditioned on an ETag needs {@code s3:GetObject} as well as {@code s3:DeleteObject}; + * {@code If-Match: *} needs only the delete. Per the S3 conditional-deletes guide, not a measurement. + */ +@QuarkusTest +@TestProfile(IamEnforcementProfile.class) +class S3ConditionalDeleteIamEnforcementIntegrationTest { + + private static final String ADMIN = "test"; + private static final String REGION = "us-east-1"; + + @Test + void anETagConditionedDeleteAlsoNeedsGetObject() { + String suffix = Long.toString(System.nanoTime(), 36); + String bucket = "conddel-" + suffix; + String user = "conddel-" + suffix; + given().header("Authorization", auth(ADMIN, "s3")).when().put("/" + bucket).then().statusCode(200); + for (String key : new String[] {"etag.txt", "star.txt"}) { + given().header("Authorization", auth(ADMIN, "s3")).body("seed") + .when().put("/" + bucket + "/" + key).then().statusCode(200); + } + given().formParam("Action", "CreateUser").formParam("UserName", user) + .header("Authorization", auth(ADMIN, "iam")).when().post("/").then().statusCode(200); + String accessKeyId = given().formParam("Action", "CreateAccessKey").formParam("UserName", user) + .header("Authorization", auth(ADMIN, "iam")).when().post("/").then().statusCode(200) + .extract().path("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.AccessKeyId"); + String eTag = given().header("Authorization", auth(ADMIN, "s3")).when().head("/" + bucket + "/etag.txt") + .then().statusCode(200).extract().header("ETag"); + String objects = "arn:aws:s3:::" + bucket + "/*"; + + policy(user, "{\"Effect\":\"Allow\",\"Action\":\"s3:DeleteObject\",\"Resource\":\"" + objects + "\"}"); + assertEquals(403, delete(accessKeyId, bucket, "etag.txt", eTag), "an ETag condition with no GetObject"); + assertEquals(403, given().header("Authorization", auth(accessKeyId, "s3")).header("If-Match", eTag) + .when().delete("/" + bucket + "/etag.txt?x-id=DeleteObject").statusCode(), + "an SDK's x-id query parameter does not skip the check"); + assertEquals(204, delete(accessKeyId, bucket, "star.txt", "*"), "the existence check needs only DeleteObject"); + + // AbortMultipartUpload shares the DELETE verb and the s3:DeleteObject action here, but is not + // a conditional object delete, so an If-Match on it asks for no GetObject. + String uploadId = given().header("Authorization", auth(ADMIN, "s3")) + .when().post("/" + bucket + "/mpu.txt?uploads").then().statusCode(200) + .extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId"); + policy(user, "{\"Effect\":\"Allow\",\"Action\":[\"s3:DeleteObject\",\"s3:AbortMultipartUpload\"],\"Resource\":\"" + + objects + "\"}"); + assertEquals(204, given().header("Authorization", auth(accessKeyId, "s3")).header("If-Match", eTag) + .when().delete("/" + bucket + "/mpu.txt?uploadId=" + uploadId).statusCode(), + "an abort carrying If-Match is not a conditional delete"); + + policy(user, "{\"Effect\":\"Allow\",\"Action\":[\"s3:DeleteObject\",\"s3:GetObject\"],\"Resource\":\"" + + objects + "\"}"); + assertEquals(204, delete(accessKeyId, bucket, "etag.txt", eTag), "with GetObject the condition is evaluated"); + } + + private static void policy(String user, String statement) { + given().formParam("Action", "PutUserPolicy").formParam("UserName", user).formParam("PolicyName", "p") + .formParam("PolicyDocument", "{\"Version\":\"2012-10-17\",\"Statement\":[" + statement + "]}") + .header("Authorization", auth(ADMIN, "iam")).when().post("/").then().statusCode(200); + } + + private static int delete(String accessKeyId, String bucket, String key, String ifMatch) { + return given().header("Authorization", auth(accessKeyId, "s3")).header("If-Match", ifMatch) + .when().delete("/" + bucket + "/" + key).statusCode(); + } + + private static String auth(String accessKeyId, String service) { + return "AWS4-HMAC-SHA256 Credential=" + accessKeyId + "/20260629/" + REGION + "/" + service + + "/aws4_request, SignedHeaders=host, Signature=abc"; + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.java index a352661290..388ab90a60 100644 --- a/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.java @@ -2282,6 +2282,80 @@ class S3AuthEnforcementIntegrationTest { return publicObjectActionPolicy(bucket, key, "s3:GetObject"); } + @Test + @Order(56) + void conditionalDeleteByETagAlsoNeedsGetObject() { + String bucket = "auth-conditional-delete-bucket"; + given().filter(LOCAL_SIGNER).when().put("/" + bucket).then().statusCode(200); + String eTag = given().filter(LOCAL_SIGNER).body("etag-guarded") + .when().put("/" + bucket + "/etag.txt") + .then().statusCode(200).extract().header("ETag"); + given().filter(LOCAL_SIGNER).body("batch-guarded") + .when().put("/" + bucket + "/batch.txt") + .then().statusCode(200); + given().filter(LOCAL_SIGNER).body("star-guarded") + .when().put("/" + bucket + "/star.txt") + .then().statusCode(200); + given() + .filter(LOCAL_SIGNER) + .contentType("application/json") + .body(publicObjectActionPolicy(bucket, "s3:DeleteObject")) + .when() + .put("/" + bucket + "?policy") + .then() + .statusCode(200); + + // s3:DeleteObject alone: an ETag condition is refused, the existence check is allowed. + given() + .header("If-Match", eTag) + .when() + .delete("/" + bucket + "/etag.txt") + .then() + .statusCode(403) + .body(containsString("AccessDenied")); + given() + .contentType("application/xml") + .body("batch.txt" + eTag + "") + .when() + .post("/" + bucket + "?delete") + .then() + .statusCode(200) + .body(containsString("batch.txt")) + .body(containsString("AccessDenied")); + given() + .header("If-Match", "*") + .when() + .delete("/" + bucket + "/star.txt") + .then() + .statusCode(204); + + // With s3:GetObject granted as well, the ETag condition is evaluated. + given() + .filter(LOCAL_SIGNER) + .contentType("application/json") + .body(""" + { + "Version": "2012-10-17", + "Statement": { + "Effect": "Allow", + "Principal": "*", + "Action": ["s3:DeleteObject", "s3:GetObject"], + "Resource": "arn:aws:s3:::%s/*" + } + } + """.formatted(bucket)) + .when() + .put("/" + bucket + "?policy") + .then() + .statusCode(200); + given() + .header("If-Match", eTag) + .when() + .delete("/" + bucket + "/etag.txt") + .then() + .statusCode(204); + } + private static String publicObjectActionPolicy(String bucket, String action) { return publicObjectActionPolicy(bucket, "*", action); } diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ConditionalWriteIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ConditionalWriteIntegrationTest.java index 1032ba757e..f2e775f841 100644 --- a/src/test/java/io/github/hectorvent/floci/services/s3/S3ConditionalWriteIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ConditionalWriteIntegrationTest.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.services.s3; import io.quarkus.test.junit.QuarkusTest; +import io.restassured.path.xml.XmlPath; import io.restassured.response.ValidatableResponse; import org.junit.jupiter.api.Test; @@ -25,6 +26,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; class S3ConditionalWriteIntegrationTest { private static final String PRECONDITION_FAILED_MESSAGE = "At least one of the pre-conditions you specified did not hold"; + private static final String STALE_ETAG = "\"00000000000000000000000000000000\""; @Test void putObject_ifNoneMatchStar_succeedsWhenKeyMissing() { @@ -263,6 +265,192 @@ class S3ConditionalWriteIntegrationTest { assertObjectBody(bucket, "object.txt", "first"); } + @Test + void deleteObject_ifMatch_deletesOnMatch() { + String bucket = createBucket("delete-if-match"); + String eTag = putObject(bucket, "object.txt", "first"); + + given() + .header("If-Match", eTag) + .when() + .delete("/" + bucket + "/object.txt") + .then() + .statusCode(204); + + assertObjectMissing(bucket, "object.txt"); + } + + @Test + void deleteObject_ifMatch_412OnMismatchAndDoesNotDelete() { + String bucket = createBucket("delete-if-match-stale"); + putObject(bucket, "object.txt", "first"); + + ValidatableResponse response = given() + .header("If-Match", STALE_ETAG) + .when() + .delete("/" + bucket + "/object.txt") + .then(); + + assertPreconditionFailed(response, "If-Match"); + assertObjectBody(bucket, "object.txt", "first"); + } + + @Test + void deleteObject_ifMatch_404WhenKeyMissing() { + String bucket = createBucket("delete-if-match-missing"); + + for (String ifMatch : List.of(STALE_ETAG, "*")) { + given() + .header("If-Match", ifMatch) + .when() + .delete("/" + bucket + "/missing.txt") + .then() + .statusCode(404) + .body("Error.Code", equalTo("NoSuchKey")); + } + } + + @Test + void deleteObject_ifMatchStar_deletesWhenKeyExists() { + String bucket = createBucket("delete-if-match-star"); + putObject(bucket, "object.txt", "first"); + + given() + .header("If-Match", "*") + .when() + .delete("/" + bucket + "/object.txt") + .then() + .statusCode(204); + + assertObjectMissing(bucket, "object.txt"); + } + + @Test + void deleteObject_versioned_ifMatchMismatchCreatesNoDeleteMarker() { + String bucket = createVersionedBucket("delete-if-match-versioned"); + String eTag = putObject(bucket, "object.txt", "first"); + + assertPreconditionFailed(given() + .header("If-Match", STALE_ETAG) + .when() + .delete("/" + bucket + "/object.txt") + .then(), "If-Match"); + assertObjectBody(bucket, "object.txt", "first"); + + given() + .header("If-Match", eTag) + .when() + .delete("/" + bucket + "/object.txt") + .then() + .statusCode(204) + .header("x-amz-delete-marker", "true"); + assertObjectMissing(bucket, "object.txt"); + } + + @Test + void deleteObject_versioned_ifMatch404WhenCurrentVersionIsDeleteMarker() { + String bucket = createVersionedBucket("delete-if-match-marker"); + putObject(bucket, "object.txt", "first"); + given().when().delete("/" + bucket + "/object.txt").then().statusCode(204); + + given() + .header("If-Match", "*") + .when() + .delete("/" + bucket + "/object.txt") + .then() + .statusCode(404) + .body("Error.Code", equalTo("NoSuchKey")); + } + + @Test + void deleteObject_versioned_ifMatchIsEvaluatedAgainstTheCurrentVersion() { + String bucket = createVersionedBucket("delete-if-match-version-id"); + String olderETag = putObject(bucket, "object.txt", "older"); + String olderVersionId = versionIdOf(bucket, "object.txt", 1); + String currentETag = putObject(bucket, "object.txt", "current"); + + // The addressed version's own ETag is not what the precondition checks. + assertPreconditionFailed(given() + .header("If-Match", olderETag) + .when() + .delete("/" + bucket + "/object.txt?versionId=" + olderVersionId) + .then(), "If-Match"); + given().when().get("/" + bucket + "/object.txt?versionId=" + olderVersionId).then().statusCode(200); + + given() + .header("If-Match", currentETag) + .when() + .delete("/" + bucket + "/object.txt?versionId=" + olderVersionId) + .then() + .statusCode(204); + given().when().get("/" + bucket + "/object.txt?versionId=" + olderVersionId).then().statusCode(404); + assertObjectBody(bucket, "object.txt", "current"); + } + + @Test + void deleteObjects_perObjectETag_isAPreconditionPerEntry() { + String bucket = createBucket("delete-objects-etag"); + String matchingETag = putObject(bucket, "matching.txt", "matching"); + putObject(bucket, "stale.txt", "stale"); + putObject(bucket, "unconditional.txt", "unconditional"); + + String response = given() + .contentType("application/xml") + .body(""" + + matching.txt%s + stale.txt%s + missing.txt* + unconditional.txt + """.formatted(stripQuotes(matchingETag), stripQuotes(STALE_ETAG))) + .when() + .post("/" + bucket + "?delete") + .then() + .statusCode(200) + .extract().asString(); + + XmlPath xml = XmlPath.from(response); + assertEquals(List.of("matching.txt", "unconditional.txt"), xml.getList("DeleteResult.Deleted.Key")); + assertEquals(List.of("stale.txt", "missing.txt"), xml.getList("DeleteResult.Error.Key")); + assertEquals(List.of("PreconditionFailed", "NoSuchKey"), xml.getList("DeleteResult.Error.Code")); + assertObjectBody(bucket, "stale.txt", "stale"); + assertObjectMissing(bucket, "matching.txt"); + assertObjectMissing(bucket, "unconditional.txt"); + } + + @Test + void concurrentDeleteObject_ifMatch_neverDeletesAnOverwrite() throws Exception { + String bucket = createBucket("delete-if-match-concurrent"); + ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor(); + try { + for (int round = 0; round < 25; round++) { + String key = "object-" + round + ".txt"; + String original = putObject(bucket, key, "original"); + CyclicBarrier barrier = new CyclicBarrier(2); + Future delete = executor.submit(() -> { + barrier.await(); + return given().header("If-Match", original) + .when().delete("/" + bucket + "/" + key) + .then().extract().statusCode(); + }); + Future overwrite = executor.submit(() -> { + barrier.await(); + return given().body("overwrite") + .when().put("/" + bucket + "/" + key) + .then().extract().statusCode(); + }); + assertEquals(200, overwrite.get(10, SECONDS)); + int deleteStatus = delete.get(10, SECONDS); + // Delete-then-overwrite and overwrite-then-412 both leave the overwrite in place. + // Only a check that is not atomic with the delete can remove it. + assertEquals(true, deleteStatus == 204 || deleteStatus == 412, "delete status " + deleteStatus); + assertObjectBody(bucket, key, "overwrite"); + } + } finally { + executor.shutdownNow(); + } + } + private static String createBucket(String label) { String bucket = "cond-" + label + "-" + UUID.randomUUID().toString().substring(0, 8); given() @@ -273,6 +461,36 @@ class S3ConditionalWriteIntegrationTest { return bucket; } + private static String createVersionedBucket(String label) { + String bucket = createBucket(label); + given() + .body("Enabled") + .when() + .put("/" + bucket + "?versioning") + .then() + .statusCode(200); + return bucket; + } + + /** Version id of the {@code n}-th version written to {@code key} (1-based, oldest first). */ + private static String versionIdOf(String bucket, String key, int n) { + List ids = given() + .when() + .get("/" + bucket + "?versions&prefix=" + key) + .then() + .statusCode(200) + .extract().xmlPath().getList("ListVersionsResult.Version.VersionId"); + return ids.get(ids.size() - n); + } + + private static void assertObjectMissing(String bucket, String key) { + given() + .when() + .get("/" + bucket + "/" + key) + .then() + .statusCode(404); + } + private static String putObject(String bucket, String key, String body) { return given() .body(body)