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 58a595d754..d61b07bca5 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 @@ -28,6 +28,7 @@ import io.github.hectorvent.floci.services.s3.model.FilterRule; import io.github.hectorvent.floci.services.s3.model.NotificationConfiguration; import io.github.hectorvent.floci.services.s3.model.ObjectAttributeName; import io.github.hectorvent.floci.services.s3.model.CopyObjectOptions; +import io.github.hectorvent.floci.services.s3.model.CopySourceConditions; import io.github.hectorvent.floci.services.s3.model.QueueNotification; import io.github.hectorvent.floci.services.s3.model.ObjectLockRetention; import io.github.hectorvent.floci.services.s3.model.Part; @@ -2793,7 +2794,10 @@ public class S3Controller { .withGrantWrite(httpHeaders.getHeaderString("x-amz-grant-write")) .withGrantFullControl(httpHeaders.getHeaderString("x-amz-grant-full-control")) .withGrantReadAcp(httpHeaders.getHeaderString("x-amz-grant-read-acp")) - .withGrantWriteAcp(httpHeaders.getHeaderString("x-amz-grant-write-acp"))); + .withGrantWriteAcp(httpHeaders.getHeaderString("x-amz-grant-write-acp")) + .withIfMatch(httpHeaders.getHeaderString("If-Match")) + .withIfNoneMatch(httpHeaders.getHeaderString("If-None-Match")) + .withCopySourceConditions(copySourceConditions(httpHeaders))); XmlBuilder xmlBuilder = new XmlBuilder() .raw("") .start("CopyObjectResult", AwsNamespaces.S3) @@ -2830,7 +2834,8 @@ public class S3Controller { String eTag = s3Service.uploadPartCopy(destBucket, destKey, uploadId, partNumber, sourceBucket, sourceObject.objectKey(), sourceObject.versionId(), copySourceRange, copySourceSseCustomerHeaders(httpHeaders), - sseCustomerHeaders(httpHeaders)); + sseCustomerHeaders(httpHeaders), + copySourceConditions(httpHeaders)); // The destination multipart upload's own SSE settings (captured at // CreateMultipartUpload), not anything from this request's headers. // UploadPartCopy doesn't take server-side-encryption headers itself, @@ -2856,6 +2861,16 @@ public class S3Controller { return response.build(); } + private CopySourceConditions copySourceConditions(HttpHeaders httpHeaders) { + String ifModifiedSince = httpHeaders.getHeaderString("x-amz-copy-source-if-modified-since"); + String ifUnmodifiedSince = httpHeaders.getHeaderString("x-amz-copy-source-if-unmodified-since"); + return new CopySourceConditions( + httpHeaders.getHeaderString("x-amz-copy-source-if-match"), + httpHeaders.getHeaderString("x-amz-copy-source-if-none-match"), + ifModifiedSince != null ? parseHttpDate(ifModifiedSince) : null, + ifUnmodifiedSince != null ? parseHttpDate(ifUnmodifiedSince) : null); + } + private S3Service.SseCustomerHeaders copySourceSseCustomerHeaders(HttpHeaders httpHeaders) { return new S3Service.SseCustomerHeaders( httpHeaders.getHeaderString("x-amz-copy-source-server-side-encryption-customer-algorithm"), 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 4416914031..1f311aefd6 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 @@ -54,6 +54,7 @@ import java.nio.file.StandardOpenOption; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.time.Instant; +import java.time.temporal.ChronoUnit; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Arrays; @@ -728,6 +729,36 @@ public class S3Service implements Resettable, ResourceProvider { return false; } + /** + * Applies the {@code x-amz-copy-source-if-*} preconditions to the source of a copy. The pairing + * rules are the ones S3 documents for CopyObject: a matching {@code if-match} makes + * {@code if-unmodified-since} irrelevant, and a matching {@code if-none-match} fails whatever + * {@code if-modified-since} says. Unlike a conditional GET, every failure is a 412, never a 304. + * Dates compare at second precision, the resolution of an HTTP date and of Last-Modified. S3 + * documents no {@code } value for these, so the error carries none, as a failed + * conditional GET does here. + */ + private void checkCopySourcePreconditions(S3Object source, CopySourceConditions conditions) { + if (conditions == null) { + return; + } + Instant lastModified = source.getLastModified().truncatedTo(ChronoUnit.SECONDS); + if (conditions.ifMatch() != null && !eTagMatches(conditions.ifMatch(), source.getETag())) { + throw new S3PreconditionFailedException(null); + } + if (conditions.ifUnmodifiedSince() != null && conditions.ifMatch() == null + && lastModified.isAfter(conditions.ifUnmodifiedSince())) { + throw new S3PreconditionFailedException(null); + } + if (conditions.ifNoneMatch() != null && eTagMatches(conditions.ifNoneMatch(), source.getETag())) { + throw new S3PreconditionFailedException(null); + } + if (conditions.ifModifiedSince() != null && conditions.ifNoneMatch() == null + && !lastModified.isAfter(conditions.ifModifiedSince())) { + throw new S3PreconditionFailedException(null); + } + } + private String normalizeEntityTag(String value) { String normalized = value == null ? "" : value.trim(); if (normalized.length() >= 2 && normalized.startsWith("\"") && normalized.endsWith("\"")) { @@ -1948,6 +1979,7 @@ public class S3Service implements Resettable, ResourceProvider { { CopyObjectOptions effectiveOptions = options != null ? options : new CopyObjectOptions(); S3Object source = getObject(sourceBucket, sourceKey, versionId); + checkCopySourcePreconditions(source, effectiveOptions.getCopySourceConditions()); validateSseCustomerAccess(source, effectiveOptions.getCopySourceSseCustomerAlgorithm(), effectiveOptions.getCopySourceSseCustomerKey(), @@ -1960,6 +1992,7 @@ public class S3Service implements Resettable, ResourceProvider { String destBucket, String destKey, CopyObjectOptions options) { CopyObjectOptions effectiveOptions = options != null ? options : new CopyObjectOptions(); S3Object source = getObject(sourceBucket, sourceKey); + checkCopySourcePreconditions(source, effectiveOptions.getCopySourceConditions()); validateSseCustomerAccess(source, effectiveOptions.getCopySourceSseCustomerAlgorithm(), effectiveOptions.getCopySourceSseCustomerKey(), @@ -3420,7 +3453,19 @@ public class S3Service implements Resettable, ResourceProvider { String copySourceRange, SseCustomerHeaders copySourceSseCustomerHeaders, SseCustomerHeaders sseCustomerHeaders) { + return uploadPartCopy(destBucket, destKey, uploadId, partNumber, sourceBucket, sourceKey, + sourceVersionId, copySourceRange, copySourceSseCustomerHeaders, sseCustomerHeaders, + CopySourceConditions.NONE); + } + + public String uploadPartCopy(String destBucket, String destKey, String uploadId, int partNumber, + String sourceBucket, String sourceKey, String sourceVersionId, + String copySourceRange, + SseCustomerHeaders copySourceSseCustomerHeaders, + SseCustomerHeaders sseCustomerHeaders, + CopySourceConditions copySourceConditions) { S3Object source = getObject(sourceBucket, sourceKey, sourceVersionId); + checkCopySourcePreconditions(source, copySourceConditions); validateSseCustomerAccess(source, copySourceSseCustomerHeaders.algorithm(), copySourceSseCustomerHeaders.key(), @@ -5674,7 +5719,9 @@ public class S3Service implements Resettable, ResourceProvider { .withGrantReadAcp(effectiveOptions.getGrantReadAcp()) .withGrantWriteAcp(effectiveOptions.getGrantWriteAcp()) .withChecksumAlgorithm(copyChecksumAlgorithm != null ? copyChecksumAlgorithm.name() : null) - .withTagging(effectiveTags)); + .withTagging(effectiveTags) + .withIfMatch(effectiveOptions.getIfMatch()) + .withIfNoneMatch(effectiveOptions.getIfNoneMatch())); } private record AnnotationSnapshot(ObjectAnnotation metadata, byte[] payload) {} diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/model/CopyObjectOptions.java b/src/main/java/io/github/hectorvent/floci/services/s3/model/CopyObjectOptions.java index aa70325e87..d8b4af53ad 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/model/CopyObjectOptions.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/model/CopyObjectOptions.java @@ -29,6 +29,10 @@ public class CopyObjectOptions { private String checksumAlgorithm; // Whether annotations travel with the copy: COPY (the default) or EXCLUDE. private String annotationDirective; + // Destination preconditions (If-Match / If-None-Match), checked atomically with the write. + private String ifMatch; + private String ifNoneMatch; + private CopySourceConditions copySourceConditions = CopySourceConditions.NONE; public String getMetadataDirective() { return metadataDirective; } public CopyObjectOptions withMetadataDirective(String metadataDirective) { this.metadataDirective = metadataDirective; return this; } @@ -104,4 +108,16 @@ public class CopyObjectOptions { public String getAnnotationDirective() { return annotationDirective; } public CopyObjectOptions withAnnotationDirective(String annotationDirective) { this.annotationDirective = annotationDirective; return this; } + + public String getIfMatch() { return ifMatch; } + public CopyObjectOptions withIfMatch(String ifMatch) { this.ifMatch = ifMatch; return this; } + + public String getIfNoneMatch() { return ifNoneMatch; } + public CopyObjectOptions withIfNoneMatch(String ifNoneMatch) { this.ifNoneMatch = ifNoneMatch; return this; } + + public CopySourceConditions getCopySourceConditions() { return copySourceConditions; } + public CopyObjectOptions withCopySourceConditions(CopySourceConditions copySourceConditions) { + this.copySourceConditions = copySourceConditions != null ? copySourceConditions : CopySourceConditions.NONE; + return this; + } } diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/model/CopySourceConditions.java b/src/main/java/io/github/hectorvent/floci/services/s3/model/CopySourceConditions.java new file mode 100644 index 0000000000..098a3cbc93 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/s3/model/CopySourceConditions.java @@ -0,0 +1,14 @@ +package io.github.hectorvent.floci.services.s3.model; + +import java.time.Instant; + +/** + * The {@code x-amz-copy-source-if-*} preconditions of a CopyObject or UploadPartCopy request, + * evaluated against the source object. Each field is null when its header was absent (or, for the + * dates, unparseable, which S3 treats the same way). + */ +public record CopySourceConditions(String ifMatch, String ifNoneMatch, + Instant ifModifiedSince, Instant ifUnmodifiedSince) { + + public static final CopySourceConditions NONE = new CopySourceConditions(null, null, null, null); +} diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ConditionalCopyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ConditionalCopyIntegrationTest.java new file mode 100644 index 0000000000..d0422b9fe8 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ConditionalCopyIntegrationTest.java @@ -0,0 +1,252 @@ +package io.github.hectorvent.floci.services.s3; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.specification.RequestSpecification; +import org.junit.jupiter.api.Test; + +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Locale; +import java.util.UUID; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; + +@QuarkusTest +class S3ConditionalCopyIntegrationTest { + private static final String STALE_ETAG = "\"00000000000000000000000000000000\""; + private static final String LONG_AGO = "Sat, 01 Jan 2000 00:00:00 GMT"; + // IMF-fixdate, two-digit day: RFC_1123_DATE_TIME writes "Sat, 3 Oct", which the S3 date parser rejects. + private static final String FAR_FUTURE = DateTimeFormatter + .ofPattern("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US) + .format(ZonedDateTime.now(ZoneOffset.UTC).plusDays(1)); + + // --- destination preconditions (If-Match / If-None-Match) --- + + @Test + void copyObject_ifNoneMatchStar_412WhenDestinationExistsAndDoesNotOverwrite() { + String bucket = createBucket("copy-dest-if-none"); + putObject(bucket, "src.txt", "source"); + putObject(bucket, "dst.txt", "destination"); + + copy(bucket, "src.txt", "dst.txt").header("If-None-Match", "*") + .when().put("/" + bucket + "/dst.txt") + .then().statusCode(412) + .body("Error.Code", equalTo("PreconditionFailed")) + .body("Error.Condition", equalTo("If-None-Match")); + + assertObjectBody(bucket, "dst.txt", "destination"); + } + + @Test + void copyObject_ifNoneMatchStar_copiesWhenDestinationMissing() { + String bucket = createBucket("copy-dest-if-none-missing"); + putObject(bucket, "src.txt", "source"); + + copy(bucket, "src.txt", "dst.txt").header("If-None-Match", "*") + .when().put("/" + bucket + "/dst.txt") + .then().statusCode(200); + + assertObjectBody(bucket, "dst.txt", "source"); + } + + @Test + void copyObject_ifMatch_412OnStaleDestinationAndDoesNotOverwrite() { + String bucket = createBucket("copy-dest-if-match"); + putObject(bucket, "src.txt", "source"); + String destinationETag = putObject(bucket, "dst.txt", "destination"); + + copy(bucket, "src.txt", "dst.txt").header("If-Match", STALE_ETAG) + .when().put("/" + bucket + "/dst.txt") + .then().statusCode(412) + .body("Error.Code", equalTo("PreconditionFailed")) + .body("Error.Condition", equalTo("If-Match")); + assertObjectBody(bucket, "dst.txt", "destination"); + + copy(bucket, "src.txt", "dst.txt").header("If-Match", destinationETag) + .when().put("/" + bucket + "/dst.txt") + .then().statusCode(200); + assertObjectBody(bucket, "dst.txt", "source"); + } + + @Test + void copyObject_ifMatch_404WhenDestinationMissing() { + String bucket = createBucket("copy-dest-if-match-missing"); + putObject(bucket, "src.txt", "source"); + + copy(bucket, "src.txt", "dst.txt").header("If-Match", STALE_ETAG) + .when().put("/" + bucket + "/dst.txt") + .then().statusCode(404).body("Error.Code", equalTo("NoSuchKey")); + + given().when().get("/" + bucket + "/dst.txt").then().statusCode(404); + } + + @Test + void copyObject_selfCopyHonoursDestinationIfMatch() { + String bucket = createBucket("copy-self-if-match"); + String eTag = putObject(bucket, "obj.txt", "body"); + + copy(bucket, "obj.txt", "obj.txt") + .header("x-amz-metadata-directive", "REPLACE") + .header("If-Match", STALE_ETAG) + .when().put("/" + bucket + "/obj.txt") + .then().statusCode(412); + + copy(bucket, "obj.txt", "obj.txt") + .header("x-amz-metadata-directive", "REPLACE") + .header("If-Match", eTag) + .when().put("/" + bucket + "/obj.txt") + .then().statusCode(200); + } + + // --- copy-source preconditions (x-amz-copy-source-if-*) --- + + @Test + void copyObject_copySourceIfMatch() { + String bucket = createBucket("copy-src-if-match"); + String sourceETag = putObject(bucket, "src.txt", "source"); + + assertCopySource(bucket, "x-amz-copy-source-if-match", STALE_ETAG, 412); + assertCopySource(bucket, "x-amz-copy-source-if-match", sourceETag, 200); + } + + @Test + void copyObject_copySourceIfNoneMatch() { + String bucket = createBucket("copy-src-if-none-match"); + String sourceETag = putObject(bucket, "src.txt", "source"); + + assertCopySource(bucket, "x-amz-copy-source-if-none-match", sourceETag, 412); + assertCopySource(bucket, "x-amz-copy-source-if-none-match", STALE_ETAG, 200); + } + + @Test + void copyObject_copySourceIfModifiedSince() { + String bucket = createBucket("copy-src-if-modified"); + putObject(bucket, "src.txt", "source"); + String lastModified = lastModified(bucket, "src.txt"); + + assertCopySource(bucket, "x-amz-copy-source-if-modified-since", FAR_FUTURE, 412); + assertCopySource(bucket, "x-amz-copy-source-if-modified-since", LONG_AGO, 200); + // Last-Modified has second precision, so the source has not been modified since itself. + assertCopySource(bucket, "x-amz-copy-source-if-modified-since", lastModified, 412); + } + + @Test + void copyObject_copySourceIfUnmodifiedSince() { + String bucket = createBucket("copy-src-if-unmodified"); + putObject(bucket, "src.txt", "source"); + String lastModified = lastModified(bucket, "src.txt"); + + assertCopySource(bucket, "x-amz-copy-source-if-unmodified-since", LONG_AGO, 412); + assertCopySource(bucket, "x-amz-copy-source-if-unmodified-since", FAR_FUTURE, 200); + assertCopySource(bucket, "x-amz-copy-source-if-unmodified-since", lastModified, 200); + } + + @Test + void copyObject_copySourceIfMatchTrueOverridesIfUnmodifiedSinceFalse() { + String bucket = createBucket("copy-src-combo-match"); + String sourceETag = putObject(bucket, "src.txt", "source"); + + // Alone, the date condition fails; paired with a matching if-match it is not consulted. + assertCopySource(bucket, "x-amz-copy-source-if-unmodified-since", LONG_AGO, 412); + copy(bucket, "src.txt", "dst.txt") + .header("x-amz-copy-source-if-match", sourceETag) + .header("x-amz-copy-source-if-unmodified-since", LONG_AGO) + .when().put("/" + bucket + "/dst.txt") + .then().statusCode(200); + } + + @Test + void copyObject_copySourceIfNoneMatchFalseFailsDespiteIfModifiedSinceTrue() { + String bucket = createBucket("copy-src-combo-none"); + String sourceETag = putObject(bucket, "src.txt", "source"); + + // Alone, the date condition passes; a failing if-none-match still fails the pair. + assertCopySource(bucket, "x-amz-copy-source-if-modified-since", LONG_AGO, 200); + copy(bucket, "src.txt", "dst.txt") + .header("x-amz-copy-source-if-none-match", sourceETag) + .header("x-amz-copy-source-if-modified-since", LONG_AGO) + .when().put("/" + bucket + "/dst.txt") + .then().statusCode(412); + + given().when().get("/" + bucket + "/dst.txt").then().statusCode(404); + } + + @Test + void uploadPartCopy_rejectedCopyLeavesExistingPartUnchanged() { + String bucket = createBucket("upload-part-copy-keep"); + putObject(bucket, "src.txt", "source"); + String uploadId = given() + .when() + .post("/" + bucket + "/dst.txt?uploads") + .then() + .statusCode(200) + .extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId"); + String partETag = given().body("original") + .when().put("/" + bucket + "/dst.txt?partNumber=1&uploadId=" + uploadId) + .then().statusCode(200).extract().header("ETag"); + + copy(bucket, "src.txt", "dst.txt").header("x-amz-copy-source-if-match", STALE_ETAG) + .when().put("/" + bucket + "/dst.txt?partNumber=1&uploadId=" + uploadId) + .then().statusCode(412); + + given().body("1" + partETag + + "") + .when().post("/" + bucket + "/dst.txt?uploadId=" + uploadId) + .then().statusCode(200); + assertObjectBody(bucket, "dst.txt", "original"); + } + + @Test + void uploadPartCopy_copySourceIfMatch() { + String bucket = createBucket("upload-part-copy-src"); + String sourceETag = putObject(bucket, "src.txt", "source"); + String uploadId = given() + .when() + .post("/" + bucket + "/dst.txt?uploads") + .then() + .statusCode(200) + .extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId"); + + copy(bucket, "src.txt", "dst.txt").header("x-amz-copy-source-if-match", STALE_ETAG) + .when().put("/" + bucket + "/dst.txt?partNumber=1&uploadId=" + uploadId) + .then().statusCode(412).body("Error.Code", equalTo("PreconditionFailed")); + + copy(bucket, "src.txt", "dst.txt").header("x-amz-copy-source-if-match", sourceETag) + .when().put("/" + bucket + "/dst.txt?partNumber=1&uploadId=" + uploadId) + .then().statusCode(200); + } + + private static void assertCopySource(String bucket, String header, String value, int status) { + String destination = "dst-" + UUID.randomUUID() + ".txt"; + copy(bucket, "src.txt", destination).header(header, value) + .when().put("/" + bucket + "/" + destination) + .then().statusCode(status); + given().when().get("/" + bucket + "/" + destination).then().statusCode(status == 200 ? 200 : 404); + } + + private static RequestSpecification copy(String bucket, String sourceKey, String destinationKey) { + return given().header("x-amz-copy-source", "/" + bucket + "/" + sourceKey); + } + + private static String createBucket(String label) { + String bucket = 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 String lastModified(String bucket, String key) { + return given().when().head("/" + bucket + "/" + key) + .then().statusCode(200).extract().header("Last-Modified"); + } + + private static void assertObjectBody(String bucket, String key, String body) { + given().when().get("/" + bucket + "/" + key).then().statusCode(200).body(equalTo(body)); + } +}