fix(s3): honour If-Match on DeleteObject and the ETag element on DeleteObjects (#4526)

* fix(s3): honour If-Match on DeleteObject and the ETag element on DeleteObjects

A conditional delete was ignored: DeleteObject with a stale If-Match answered
204 and deleted the object, and a DeleteObjects entry carrying a stale <ETag>
was reported under <Deleted> and removed. S3 treats both as preconditions.

The precondition is evaluated against the current version of the key, as S3
documents, including when versionId addresses an older version. A matching
ETag (or *) proceeds; a mismatch answers 412 PreconditionFailed and leaves the
object in place; a missing key or a current delete marker answers 404 NoSuchKey.
In DeleteObjects each entry is checked on its own and a failure is reported
under <Error> while the rest of the batch proceeds.

The check runs under the bucket monitor that PutObject's conditional write
already holds, so an overwrite cannot land between the check and the delete.

* fix(s3): require s3:GetObject for a conditional delete by ETag

An ETag condition on DeleteObject, or an <ETag> element in DeleteObjects,
tells the caller whether the object still has that ETag, so S3 requires
s3:GetObject alongside s3:DeleteObject for it. The If-Match: * existence
check needs only s3:DeleteObject. DeleteObjects reports a refused entry
under <Error> as AccessDenied and deletes the rest.

* fix(s3): require s3:GetObject for an ETag-conditional delete under IAM

The bucket-policy check added earlier left the IAM identity-policy layer
alone, so an IAM user allowed s3:DeleteObject but not s3:GetObject could
still delete by ETag. IamEnforcementFilter now authorizes that read for a
DeleteObject whose If-Match is an ETag, the way it already does for a
conditional PutObject. If-Match: * still needs only s3:DeleteObject.

* fix(s3): limit the IAM conditional-delete read to DeleteObject

AbortMultipartUpload is also a DELETE on an object key filed under
s3:DeleteObject, and it has no object ETag to compare, so an If-Match on it
must not ask for s3:GetObject. The read is now required only when the DELETE
carries no subresource besides versionId.

* fix(s3): skip the IAM delete read only for routed subresources

Allowing only versionId as a query key let any other parameter bypass the
check, including the x-id=DeleteObject SDKs add and the X-Amz-* parameters of
a presigned URL, while the controller still handled the request as a
conditional DeleteObject. The read is now skipped only for the subresources
S3Controller routes elsewhere: uploadId, tagging and annotation.

* style(s3): import XmlPath in the conditional-write test

* docs(s3): record why a delete marker answers 404 on a conditional delete

The conditional-deletes guide says 412, real S3 answered 404 when measured. The comment keeps the next reader of the guide from flipping it back.
This commit is contained in:
TiM authored and GitHub committed 2026-09-29 11:44:08 -04:00
1 parent 95cbe7de34
commit 18ef1f58e9
8 files changed
+487 -9

No files matched your search

@@ -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<String> 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("\"*\"");
}
/**
@@ -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 <Object>} 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 <Object>} 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 <Object>} block.
*
* <p>Unlike {@link #extractAll(String, String)}, which flattens every {@code <Key>} across
@@ -199,8 +206,9 @@ public final class XmlParser {
* {@code <Object>} 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".
*
* <p>{@code <VersionId>} is optional per the {@code Delete} request schema; an {@code <Object>}
* block that omits it yields an entry with a {@code null} versionId.
* <p>{@code <VersionId>} and {@code <ETag>} are optional per the {@code Delete} request schema; an
* {@code <Object>} 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.
*
* <pre>{@code
* List<XmlParser.KeyVersion> 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;
}
@@ -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);
@@ -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.
*
* <p>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<DeleteError> 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 {
@@ -220,6 +220,24 @@ class XmlParserTest {
assertEquals(new XmlParser.KeyVersion("b.txt", "v2"), entries.get(1));
}
@Test
void extractDeleteObjectEntriesKeepsEachObjectsETag() {
String xml = """
<Delete>
<Object><Key>a.txt</Key><ETag>"abc"</ETag></Object>
<Object><Key>b.txt</Key><VersionId>v2</VersionId></Object>
<Object><Key>c.txt</Key><ETag>*</ETag></Object>
</Delete>
""";
List<XmlParser.KeyVersion> 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 = """
@@ -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";
}
}
@@ -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("<Delete><Object><Key>batch.txt</Key><ETag>" + eTag + "</ETag></Object></Delete>")
.when()
.post("/" + bucket + "?delete")
.then()
.statusCode(200)
.body(containsString("<Error><Key>batch.txt</Key>"))
.body(containsString("<Code>AccessDenied</Code>"));
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);
}
@@ -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("""
<Delete>
<Object><Key>matching.txt</Key><ETag>%s</ETag></Object>
<Object><Key>stale.txt</Key><ETag>%s</ETag></Object>
<Object><Key>missing.txt</Key><ETag>*</ETag></Object>
<Object><Key>unconditional.txt</Key></Object>
</Delete>""".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<Integer> delete = executor.submit(() -> {
barrier.await();
return given().header("If-Match", original)
.when().delete("/" + bucket + "/" + key)
.then().extract().statusCode();
});
Future<Integer> 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("<VersioningConfiguration><Status>Enabled</Status></VersioningConfiguration>")
.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<String> 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)