diff --git a/docs/services/s3.md b/docs/services/s3.md index c65601473c..9351c73408 100644 --- a/docs/services/s3.md +++ b/docs/services/s3.md @@ -105,7 +105,10 @@ noncurrent version. Deleting newer versions can make that null version current a `DeleteObject` without a version id in a versioning-suspended bucket makes a `null` delete marker the current version, as on S3. It replaces the null version or an earlier `null` delete marker, and the other versions stay as noncurrent versions. Deleting `versionId=null` removes that marker and -makes the newest remaining version current again. +makes the newest remaining version current again. A write in such a bucket, by PutObject, CopyObject +or CompleteMultipartUpload, makes its `null` version current in the same way. The other versions and +delete markers stay as noncurrent versions, and deleting `versionId=null` makes the newest of them +current again. Object Lock works only with versioning enabled. `PutBucketVersioning` with `Suspended` on a bucket with Object Lock answers `409 InvalidBucketState`, "An Object Lock configuration is present on this 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 a508f2905e..bd64bfa6c4 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 @@ -1089,6 +1089,11 @@ public class S3Service implements Resettable, ResourceProvider { } throw e; } + // In a suspended bucket the version or delete marker this write replaces stays as noncurrent. + if (prev != null && prev.getVersionId() != null) { + prev.setLatest(false); + putObjectForAccount(bucketOwnerAccount, versionedKey(bucketName, key, prev.getVersionId()), prev); + } // Retain archived null history and annotations until publication succeeds. // Annotation readers share this bucket monitor, so they cannot observe the // replacement with the old annotations while cleanup is pending. @@ -2476,29 +2481,14 @@ public class S3Service implements Resettable, ResourceProvider { .filter(existing -> isNamedVersion(existing, versionId)) .orElse(null); if (toDelete == null && "null".equals(versionId)) { - // A current null delete marker exists only at the plain latest key. + // A current null version or null delete marker exists only at the plain latest key. toDelete = objectStore.get(objectKey(bucketName, key)) .filter(candidate -> key.equals(candidate.getKey())) - .filter(candidate -> candidate.isDeleteMarker() && candidate.getVersionId() == null) - .orElse(null); - } - if (toDelete == null && "null".equals(versionId)) { - // Before the null version is archived it exists only at the plain latest key. - S3Object existing = objectStore.get(objectKey(bucketName, key)) - .filter(candidate -> key.equals(candidate.getKey())) - .filter(candidate -> !candidate.isDeleteMarker()) .filter(candidate -> candidate.getVersionId() == null) .orElse(null); - if (existing == null) { + if (toDelete == null) { return null; } - checkLockProtection(existing, bypassGovernance); - objectStore.delete(objectKey(bucketName, key)); - deleteFile(bucketName, key); - deleteAllAnnotationsFor(annotationParentKey(bucketName, key, null)); - LOG.debugv("Permanently deleted null version: {0}/{1}", bucketName, key); - fireNotifications(bucketName, key, removalEvent + ":Delete", existing); - return existing; } if (toDelete != null && !toDelete.isDeleteMarker()) { checkLockProtection(toDelete, bypassGovernance); diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningIntegrationTest.java index c16e3a146f..096cc8a794 100644 --- a/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningIntegrationTest.java @@ -832,4 +832,47 @@ class S3VersioningIntegrationTest { .body(equalTo("older")); } + @Test + @Order(30) + void aSuspendedWriteMakesTheVersionItReplacesNoncurrent() { + String bucket = "suspended-null-write"; + given().when().put("/" + bucket).then().statusCode(200); + given().body("Enabled") + .when().put("/" + bucket + "?versioning").then().statusCode(200); + List keys = List.of("put", "copy", "upload"); + List older = keys.stream() + .map(key -> given().body("older").when().put("/" + bucket + "/" + key) + .then().statusCode(200).extract().header("x-amz-version-id")) + .toList(); + given().body("Suspended") + .when().put("/" + bucket + "?versioning").then().statusCode(200); + + given().body("null").when().put("/" + bucket + "/put").then().statusCode(200); + given().header("x-amz-copy-source", "/" + bucket + "/put") + .when().put("/" + bucket + "/copy").then().statusCode(200); + String uploadId = given().when().post("/" + bucket + "/upload?uploads").then().statusCode(200) + .extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId"); + String partETag = given().body("null").when().put("/" + bucket + "/upload?uploadId=" + uploadId + + "&partNumber=1").then().statusCode(200).extract().header("ETag"); + given().contentType("application/xml") + .body(""" + + 1%s + """.formatted(partETag)) + .when().post("/" + bucket + "/upload?uploadId=" + uploadId).then().statusCode(200); + + for (int i = 0; i < keys.size(); i++) { + given().queryParam("versions", "").queryParam("prefix", keys.get(i)).when().get("/" + bucket) + .then().statusCode(200) + .body("ListVersionsResult.Version.VersionId", contains("null", older.get(i))) + .body("ListVersionsResult.Version.IsLatest", contains("true", "false")); + } + + given().queryParam("versionId", "null").when().delete("/" + bucket + "/put").then().statusCode(204) + .header("x-amz-version-id", "null"); + given().when().get("/" + bucket + "/put").then().statusCode(200) + .header("x-amz-version-id", older.get(0)) + .body(equalTo("older")); + } + } diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningServiceTest.java index 88634c576f..1c5ddc4e89 100644 --- a/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningServiceTest.java @@ -154,6 +154,31 @@ class S3VersioningServiceTest { assertEquals(List.of(current.getVersionId() + " latest"), versionLines("put")); } + @Test + void aSuspendedPutMakesTheVersionOrDeleteMarkerItReplacesNoncurrent() { + s3Service.putBucketVersioning("versioned-bucket", "Enabled"); + S3Object first = s3Service.putObject("versioned-bucket", "put", + "v1".getBytes(StandardCharsets.UTF_8), "text/plain", null); + S3Object second = s3Service.putObject("versioned-bucket", "put", + "v2".getBytes(StandardCharsets.UTF_8), "text/plain", null); + S3Object hidden = s3Service.putObject("versioned-bucket", "marker", + "v1".getBytes(StandardCharsets.UTF_8), "text/plain", null); + S3Object marker = s3Service.deleteObject("versioned-bucket", "marker"); + s3Service.putBucketVersioning("versioned-bucket", "Suspended"); + s3Service.putObject("versioned-bucket", "put", "null".getBytes(StandardCharsets.UTF_8), "text/plain", null); + s3Service.putObject("versioned-bucket", "marker", "null".getBytes(StandardCharsets.UTF_8), "text/plain", null); + + assertEquals(List.of("null latest", second.getVersionId(), first.getVersionId()), versionLines("put")); + assertEquals(List.of("null latest", marker.getVersionId() + " marker", hidden.getVersionId()), + versionLines("marker")); + + s3Service.deleteObject("versioned-bucket", "put", "null"); + s3Service.deleteObject("versioned-bucket", "marker", "null"); + assertEquals(List.of(second.getVersionId() + " latest", first.getVersionId()), versionLines("put")); + assertEquals(List.of(marker.getVersionId() + " marker latest", hidden.getVersionId()), + versionLines("marker")); + } + private List versionLines(String key) { return s3Service.listObjectVersions("versioned-bucket", key, 100, null).versions().stream() .map(version -> (version.getVersionId() != null ? version.getVersionId() : "null")