mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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:
8 files changed
+487
-9
No files matched your search
@@ -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 = """
|
||||
|
||||
+79
@@ -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";
|
||||
}
|
||||
}
|
||||
+74
@@ -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);
|
||||
}
|
||||
|
||||
+218
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user