Light, fluffy, and always free - The AWS Local Emulator alternative
fix(s3): enforce conditional writes (#566)
Rasheed2fb5a1f8aa3c
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.javaindex 5090ac7cd5..3edc0810f9 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@@ -410,6 +410,8 @@ public class S3Controller {@HeaderParam("Content-Encoding") String contentEncoding,@HeaderParam("x-amz-content-sha256") String contentSha256,@HeaderParam("x-amz-copy-source") String copySource,+ @HeaderParam("If-Match") String ifMatch,+ @HeaderParam("If-None-Match") String ifNoneMatch,@QueryParam("uploadId") String uploadId,@QueryParam("partNumber") Integer partNumber,@Context UriInfo uriInfo,@@ -449,6 +451,11 @@ public class S3Controller {return handleCopyObject(copySource, bucket, key, contentType, httpHeaders);}+ Response preconditionResponse = checkWritePreconditions(bucket, key, ifMatch, ifNoneMatch);+ if (preconditionResponse != null) {+ return preconditionResponse;+ }+String lockMode = httpHeaders.getHeaderString("x-amz-object-lock-mode");String retainUntilStr = httpHeaders.getHeaderString("x-amz-object-lock-retain-until-date");String legalHold = httpHeaders.getHeaderString("x-amz-object-lock-legal-hold");@@ -788,6 +795,8 @@ public class S3Controller {@QueryParam("uploadId") String uploadId,@QueryParam("versionId") String versionId,@HeaderParam("Content-Type") String contentType,+ @HeaderParam("If-Match") String ifMatch,+ @HeaderParam("If-None-Match") String ifNoneMatch,@Context HttpHeaders httpHeaders,@Context UriInfo uriInfo,byte[] body) {@@ -827,6 +836,10 @@ public class S3Controller {if (uploadId != null) {List<Integer> partNumbers = parseCompleteMultipartBody(new String(body));+ Response preconditionResponse = checkWritePreconditions(bucket, key, ifMatch, ifNoneMatch);+ if (preconditionResponse != null) {+ return preconditionResponse;+ }S3Object obj = s3Service.completeMultipartUpload(bucket, key, uploadId, partNumbers);String baseUrl = uriInfo.getBaseUri().toString();if (baseUrl.endsWith("/")) {@@ -1700,6 +1713,30 @@ public class S3Controller {return null;}+ private Response checkWritePreconditions(String bucket, String key, String ifMatch, String ifNoneMatch) {+ if (ifMatch == null && ifNoneMatch == null) {+ return null;+ }++ S3Object existing;+ try {+ existing = s3Service.headObject(bucket, key);+ } catch (AwsException e) {+ if ("NoSuchKey".equals(e.getErrorCode()) && ifMatch == null) {+ return null;+ }+ throw e;+ }++ if (ifMatch != null && !eTagMatches(ifMatch, existing.getETag())) {+ return preconditionFailedResponse();+ }+ if (ifNoneMatch != null && eTagMatches(ifNoneMatch, existing.getETag())) {+ return preconditionFailedResponse();+ }+ return null;+ }+private boolean hasPreconditions(String ifMatch, String ifNoneMatch,String ifModifiedSince, String ifUnmodifiedSince) {return ifMatch != null || ifNoneMatch != null || ifModifiedSince != null || ifUnmodifiedSince != null;@@ -1718,17 +1755,30 @@ public class S3Controller {}private boolean eTagMatches(String headerValue, String eTag) {- if ("*".equals(headerValue.trim())) {- return true;- }+ String normalizedETag = normalizeEntityTag(eTag);for (String candidate : headerValue.split(",")) {- if (candidate.trim().equals(eTag)) {+ String normalizedCandidate = normalizeEntityTag(candidate);+ if ("*".equals(normalizedCandidate) || normalizedCandidate.equals(normalizedETag)) {return true;}}return false;}+ private static String normalizeEntityTag(String value) {+ if (value == null) {+ return "";+ }+ String normalized = value.trim();+ if (normalized.startsWith("W/")) {+ normalized = normalized.substring(2).trim();+ }+ if (normalized.length() >= 2 && normalized.startsWith("\"") && normalized.endsWith("\"")) {+ normalized = normalized.substring(1, normalized.length() - 1);+ }+ return normalized;+ }+private Instant parseHttpDate(String dateStr) {try {return RFC_822.parse(dateStr.trim(), Instant::from);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.javanew file mode 100644index 0000000000..b8ae55255b--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ConditionalWriteIntegrationTest.java@@ -0,0 +1,275 @@+package io.github.hectorvent.floci.services.s3;++import io.quarkus.test.junit.QuarkusTest;+import org.junit.jupiter.api.Test;++import java.util.UUID;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.containsString;+import static org.hamcrest.Matchers.equalTo;+import static org.hamcrest.Matchers.notNullValue;++@QuarkusTest+class S3ConditionalWriteIntegrationTest {++ @Test+ void putObject_ifNoneMatchStar_succeedsWhenKeyMissing() {+ String bucket = createBucket("put-if-none-missing");++ given()+ .header("If-None-Match", "*")+ .body("first")+ .when()+ .put("/" + bucket + "/object.txt")+ .then()+ .statusCode(200)+ .header("ETag", notNullValue());++ assertObjectBody(bucket, "object.txt", "first");+ }++ @Test+ void putObject_ifNoneMatchStar_412WhenKeyExistsAndDoesNotOverwrite() {+ String bucket = createBucket("put-if-none-existing");+ putObject(bucket, "object.txt", "first");++ given()+ .header("If-None-Match", "*")+ .body("second")+ .when()+ .put("/" + bucket + "/object.txt")+ .then()+ .statusCode(412)+ .body(containsString("PreconditionFailed"));++ assertObjectBody(bucket, "object.txt", "first");+ }++ @Test+ void putObject_ifNoneMatchEtag_succeedsWhenEtagDiffers() {+ String bucket = createBucket("put-if-none-different");+ putObject(bucket, "object.txt", "first");++ given()+ .header("If-None-Match", "\"not-the-current-etag\"")+ .body("second")+ .when()+ .put("/" + bucket + "/object.txt")+ .then()+ .statusCode(200);++ assertObjectBody(bucket, "object.txt", "second");+ }++ @Test+ void putObject_ifNoneMatchEtag_412WhenEtagMatches() {+ String bucket = createBucket("put-if-none-match");+ String eTag = putObject(bucket, "object.txt", "first");++ given()+ .header("If-None-Match", eTag)+ .body("second")+ .when()+ .put("/" + bucket + "/object.txt")+ .then()+ .statusCode(412)+ .body(containsString("PreconditionFailed"));++ assertObjectBody(bucket, "object.txt", "first");+ }++ @Test+ void putObject_ifMatch_succeedsOnMatch() {+ String bucket = createBucket("put-if-match");+ String eTag = putObject(bucket, "object.txt", "first");++ given()+ .header("If-Match", eTag)+ .body("second")+ .when()+ .put("/" + bucket + "/object.txt")+ .then()+ .statusCode(200);++ assertObjectBody(bucket, "object.txt", "second");+ }++ @Test+ void putObject_ifMatch_412OnMismatch() {+ String bucket = createBucket("put-if-match-wrong");+ putObject(bucket, "object.txt", "first");++ given()+ .header("If-Match", "\"not-the-current-etag\"")+ .body("second")+ .when()+ .put("/" + bucket + "/object.txt")+ .then()+ .statusCode(412)+ .body(containsString("PreconditionFailed"));++ assertObjectBody(bucket, "object.txt", "first");+ }++ @Test+ void putObject_headerValueWithAndWithoutQuotes_bothHonoured() {+ String bucket = createBucket("put-quotes");+ String eTag = putObject(bucket, "object.txt", "first");++ given()+ .header("If-Match", stripQuotes(eTag))+ .body("second")+ .when()+ .put("/" + bucket + "/object.txt")+ .then()+ .statusCode(200);++ String currentETag = given()+ .when()+ .head("/" + bucket + "/object.txt")+ .then()+ .statusCode(200)+ .extract().header("ETag");++ given()+ .header("If-None-Match", stripQuotes(currentETag))+ .body("third")+ .when()+ .put("/" + bucket + "/object.txt")+ .then()+ .statusCode(412)+ .body(containsString("PreconditionFailed"));++ given()+ .header("If-None-Match", "\"*\"")+ .body("third")+ .when()+ .put("/" + bucket + "/object.txt")+ .then()+ .statusCode(412)+ .body(containsString("PreconditionFailed"));++ assertObjectBody(bucket, "object.txt", "second");+ }++ @Test+ void completeMultipartUpload_ifNoneMatchStar_412WhenKeyExists() {+ String bucket = createBucket("mpu-if-none-existing");+ putObject(bucket, "object.txt", "first");+ String uploadId = initiateMultipartUpload(bucket, "object.txt");+ uploadPart(bucket, "object.txt", uploadId, 1, "second");++ given()+ .contentType("application/xml")+ .header("If-None-Match", "*")+ .body(completeMultipartXml(1))+ .when()+ .post("/" + bucket + "/object.txt?uploadId=" + uploadId)+ .then()+ .statusCode(412)+ .body(containsString("PreconditionFailed"));++ assertObjectBody(bucket, "object.txt", "first");+ }++ @Test+ void completeMultipartUpload_ifMatch_succeedsOnMatch() {+ String bucket = createBucket("mpu-if-match");+ String eTag = putObject(bucket, "object.txt", "first");+ String uploadId = initiateMultipartUpload(bucket, "object.txt");+ uploadPart(bucket, "object.txt", uploadId, 1, "second");++ given()+ .contentType("application/xml")+ .header("If-Match", eTag)+ .body(completeMultipartXml(1))+ .when()+ .post("/" + bucket + "/object.txt?uploadId=" + uploadId)+ .then()+ .statusCode(200)+ .body(containsString("<CompleteMultipartUploadResult"));++ assertObjectBody(bucket, "object.txt", "second");+ }++ @Test+ void completeMultipartUpload_ifMatch_412OnMismatchAndDoesNotOverwrite() {+ String bucket = createBucket("mpu-if-match-wrong");+ putObject(bucket, "object.txt", "first");+ String uploadId = initiateMultipartUpload(bucket, "object.txt");+ uploadPart(bucket, "object.txt", uploadId, 1, "second");++ given()+ .contentType("application/xml")+ .header("If-Match", "\"not-the-current-etag\"")+ .body(completeMultipartXml(1))+ .when()+ .post("/" + bucket + "/object.txt?uploadId=" + uploadId)+ .then()+ .statusCode(412)+ .body(containsString("PreconditionFailed"));++ assertObjectBody(bucket, "object.txt", "first");+ }++ private static String createBucket(String label) {+ String bucket = "cond-" + label + "-" + UUID.randomUUID().toString().substring(0, 8);+ given()+ .when()+ .put("/" + bucket)+ .then()+ .statusCode(200);+ return bucket;+ }++ private static String putObject(String bucket, String key, String body) {+ return given()+ .body(body)+ .when()+ .put("/" + bucket + "/" + key)+ .then()+ .statusCode(200)+ .extract().header("ETag");+ }++ private static void assertObjectBody(String bucket, String key, String body) {+ given()+ .when()+ .get("/" + bucket + "/" + key)+ .then()+ .statusCode(200)+ .body(equalTo(body));+ }++ private static String initiateMultipartUpload(String bucket, String key) {+ return given()+ .contentType("application/octet-stream")+ .when()+ .post("/" + bucket + "/" + key + "?uploads")+ .then()+ .statusCode(200)+ .extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");+ }++ private static void uploadPart(String bucket, String key, String uploadId, int partNumber, String body) {+ given()+ .body(body)+ .when()+ .put("/" + bucket + "/" + key + "?uploadId=" + uploadId + "&partNumber=" + partNumber)+ .then()+ .statusCode(200)+ .header("ETag", notNullValue());+ }++ private static String completeMultipartXml(int partNumber) {+ return """+ <CompleteMultipartUpload>+ <Part><PartNumber>%d</PartNumber><ETag>etag</ETag></Part>+ </CompleteMultipartUpload>""".formatted(partNumber);+ }++ private static String stripQuotes(String eTag) {+ return eTag.replace("\"", "");+ }+}