Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(s3): allow delete markers and new versions over object-locked versions (#4390)

Luke Gordonb0ad799c5c6f
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 f4f7a42d87..014117f127 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
@@ -601,7 +601,7 @@ public class S3Service implements Resettable, ResourceProvider {
// until the replacement body is on disk, so a failed write does not drop them.
boolean[] dropPreVersioningAnnotations = {false};
resolveObjectForAccount(bucketOwnerAccount, latestKey).ifPresent(prev -> {
- if (prev.isLatest() && !prev.isDeleteMarker() && bucket.isObjectLockEnabled()) {
+ if (prev.isLatest() && !prev.isDeleteMarker() && bucket.isObjectLockEnabled() && prev.getVersionId() == null) {
checkLockProtection(prev, false);
}
if (prev.getVersionId() != null) {
@@ -1519,7 +1519,7 @@ public class S3Service implements Resettable, ResourceProvider {
if (bucket.isVersioningEnabled() && versionId == null) {
// Check lock on current latest before placing a delete marker
objectStore.get(objectKey(bucketName, key)).ifPresent(prev -> {
- if (!prev.isDeleteMarker()) {
+ if (!prev.isDeleteMarker() && prev.getVersionId() == null) {
checkLockProtection(prev, bypassGovernance);
}
});
diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectLockIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectLockIntegrationTest.java
index 8444fe6c3a..84693c2da0 100644
--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectLockIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectLockIntegrationTest.java
@@ -309,4 +309,85 @@ class S3ObjectLockIntegrationTest {
.statusCode(404)
.body(containsString("NoSuchBucket"));
}
+
+ // --- locks protect versions, not keys ---
+
+ @Test
+ @Order(20)
+ void simpleDeleteOfLegalHoldObjectCreatesDeleteMarker() {
+ String versionId = given()
+ .header("x-amz-object-lock-legal-hold", "ON")
+ .body("held")
+ .when()
+ .put("/" + LOCK_BUCKET + "/held-object.txt")
+ .then()
+ .statusCode(200)
+ .extract().header("x-amz-version-id");
+
+ given()
+ .when()
+ .delete("/" + LOCK_BUCKET + "/held-object.txt")
+ .then()
+ .statusCode(204)
+ .header("x-amz-delete-marker", "true");
+
+ given()
+ .when()
+ .get("/" + LOCK_BUCKET + "/held-object.txt?legal-hold&versionId=" + versionId)
+ .then()
+ .statusCode(200)
+ .body(containsString("<Status>ON</Status>"));
+
+ given()
+ .header("x-amz-bypass-governance-retention", "true")
+ .when()
+ .delete("/" + LOCK_BUCKET + "/held-object.txt?versionId=" + versionId)
+ .then()
+ .statusCode(403)
+ .body(containsString("AccessDenied"));
+ }
+
+ @Test
+ @Order(21)
+ void putAndCopyOverGovernanceObjectCreateNewVersions() {
+ String versionId = given()
+ .header("x-amz-object-lock-mode", "GOVERNANCE")
+ .header("x-amz-object-lock-retain-until-date", "2099-01-01T00:00:00Z")
+ .body("v1")
+ .when()
+ .put("/" + LOCK_BUCKET + "/governed-object.txt")
+ .then()
+ .statusCode(200)
+ .extract().header("x-amz-version-id");
+
+ given()
+ .body("v2")
+ .when()
+ .put("/" + LOCK_BUCKET + "/governed-object.txt")
+ .then()
+ .statusCode(200)
+ .header("x-amz-version-id", not(equalTo(versionId)));
+
+ given()
+ .header("x-amz-copy-source", "/" + LOCK_BUCKET + "/governed-object.txt?versionId=" + versionId)
+ .when()
+ .put("/" + LOCK_BUCKET + "/governed-object.txt")
+ .then()
+ .statusCode(200)
+ .header("x-amz-version-id", not(equalTo(versionId)));
+
+ given()
+ .when()
+ .get("/" + LOCK_BUCKET + "/governed-object.txt?retention&versionId=" + versionId)
+ .then()
+ .statusCode(200)
+ .body(containsString("<Mode>GOVERNANCE</Mode>"));
+
+ given()
+ .when()
+ .delete("/" + LOCK_BUCKET + "/governed-object.txt?versionId=" + versionId)
+ .then()
+ .statusCode(403)
+ .body(containsString("AccessDenied"));
+ }
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectLockVersionedWriteTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectLockVersionedWriteTest.java
new file mode 100644
index 0000000000..7cc57847c5
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectLockVersionedWriteTest.java
@@ -0,0 +1,163 @@
+package io.github.hectorvent.floci.services.s3;
+
+import io.github.hectorvent.floci.core.common.AwsException;
+import io.github.hectorvent.floci.core.storage.InMemoryStorage;
+import io.github.hectorvent.floci.services.s3.model.MultipartUpload;
+import io.github.hectorvent.floci.services.s3.model.PutObjectOptions;
+import io.github.hectorvent.floci.services.s3.model.S3Object;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Path;
+import java.time.Instant;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Object Lock protects versions, not keys: writes that add a new current version (delete marker, put, copy,
+ * multipart complete) must succeed over a locked version and leave that version intact and still locked.
+ */
+class S3ObjectLockVersionedWriteTest {
+
+ private static final String BUCKET = "lock-bucket";
+ private static final byte[] BODY = "body".getBytes(StandardCharsets.UTF_8);
+
+ private S3Service s3Service;
+
+ @TempDir
+ Path tempDir;
+
+ @BeforeEach
+ void setUp() {
+ s3Service = new S3Service(new InMemoryStorage<>(), new InMemoryStorage<>(), tempDir, true);
+ s3Service.createBucket(BUCKET, "us-east-1");
+ s3Service.setBucketObjectLockEnabled(BUCKET);
+ s3Service.putBucketVersioning(BUCKET, "Enabled");
+ s3Service.putObject(BUCKET, "source.txt", BODY, "text/plain", null);
+ }
+
+ @Test
+ void simpleDeleteOverLegalHoldCreatesDeleteMarker() {
+ S3Object held = putLegalHold("held.txt");
+
+ S3Object marker = s3Service.deleteObject(BUCKET, "held.txt", null, false);
+
+ assertTrue(marker.isDeleteMarker());
+ assertLockedVersionSurvives("held.txt", held.getVersionId());
+ }
+
+ @Test
+ void simpleDeleteOverGovernanceRetentionCreatesDeleteMarkerWithoutBypass() {
+ S3Object retained = putRetention("governed.txt", "GOVERNANCE");
+
+ S3Object marker = s3Service.deleteObject(BUCKET, "governed.txt", null, false);
+
+ assertTrue(marker.isDeleteMarker());
+ assertLockedVersionSurvives("governed.txt", retained.getVersionId());
+ }
+
+ @Test
+ void simpleDeleteOverComplianceRetentionCreatesDeleteMarker() {
+ S3Object retained = putRetention("compliance.txt", "COMPLIANCE");
+
+ S3Object marker = s3Service.deleteObject(BUCKET, "compliance.txt", null, false);
+
+ assertTrue(marker.isDeleteMarker());
+ assertLockedVersionSurvives("compliance.txt", retained.getVersionId());
+ }
+
+ @Test
+ void putOverLockedVersionCreatesNewVersion() {
+ S3Object held = putLegalHold("held.txt");
+ S3Object retained = putRetention("governed.txt", "GOVERNANCE");
+
+ S3Object heldV2 = s3Service.putObject(BUCKET, "held.txt", BODY, "text/plain", null);
+ S3Object retainedV2 = s3Service.putObject(BUCKET, "governed.txt", BODY, "text/plain", null);
+
+ assertNotEquals(held.getVersionId(), heldV2.getVersionId());
+ assertNotEquals(retained.getVersionId(), retainedV2.getVersionId());
+ assertLockedVersionSurvives("held.txt", held.getVersionId());
+ assertLockedVersionSurvives("governed.txt", retained.getVersionId());
+ }
+
+ @Test
+ void copyOverLockedVersionCreatesNewVersion() {
+ S3Object held = putLegalHold("held.txt");
+
+ S3Object copy = s3Service.copyObject(BUCKET, "source.txt", BUCKET, "held.txt");
+
+ assertNotEquals(held.getVersionId(), copy.getVersionId());
+ assertLockedVersionSurvives("held.txt", held.getVersionId());
+ }
+
+ @Test
+ void completeMultipartUploadOverLockedVersionCreatesNewVersion() {
+ S3Object held = putLegalHold("held.txt");
+
+ MultipartUpload upload = s3Service.initiateMultipartUpload(BUCKET, "held.txt", null);
+ s3Service.uploadPart(BUCKET, "held.txt", upload.getUploadId(), 1, BODY);
+ S3Object completed = s3Service.completeMultipartUpload(BUCKET, "held.txt", upload.getUploadId(),
+ List.of(1), null, null);
+
+ assertNotEquals(held.getVersionId(), completed.getVersionId());
+ assertLockedVersionSurvives("held.txt", held.getVersionId());
+ }
+
+ @Test
+ void permanentDeleteOfLockedVersionIsStillDenied() {
+ S3Object held = putLegalHold("held.txt");
+ S3Object governed = putRetention("governed.txt", "GOVERNANCE");
+ S3Object compliance = putRetention("compliance.txt", "COMPLIANCE");
+ s3Service.deleteObject(BUCKET, "held.txt", null, false);
+ s3Service.deleteObject(BUCKET, "governed.txt", null, false);
+ s3Service.deleteObject(BUCKET, "compliance.txt", null, false);
+
+ assertAccessDenied(() -> s3Service.deleteObject(BUCKET, "held.txt", held.getVersionId(), true));
+ assertAccessDenied(() -> s3Service.deleteObject(BUCKET, "governed.txt", governed.getVersionId(), false));
+ assertAccessDenied(() -> s3Service.deleteObject(BUCKET, "compliance.txt", compliance.getVersionId(), true));
+ }
+
+ @Test
+ void writesOverLockedPreVersioningObjectAreStillDenied() {
+ s3Service.createBucket("pre-versioning-bucket", "us-east-1");
+ s3Service.setBucketObjectLockEnabled("pre-versioning-bucket");
+ s3Service.putObject("pre-versioning-bucket", "held.txt", BODY, "text/plain", null,
+ new PutObjectOptions().withLegalHoldStatus("ON"));
+ s3Service.putBucketVersioning("pre-versioning-bucket", "Enabled");
+
+ assertAccessDenied(() -> s3Service.putObject("pre-versioning-bucket", "held.txt", BODY, "text/plain", null));
+ assertAccessDenied(() -> s3Service.copyObject(BUCKET, "source.txt", "pre-versioning-bucket", "held.txt"));
+ assertAccessDenied(() -> s3Service.deleteObject("pre-versioning-bucket", "held.txt", null, false));
+ assertEquals("ON", s3Service.getObject("pre-versioning-bucket", "held.txt").getLegalHoldStatus());
+ }
+
+ private S3Object putLegalHold(String key) {
+ return s3Service.putObject(BUCKET, key, BODY, "text/plain", null,
+ new PutObjectOptions().withLegalHoldStatus("ON"));
+ }
+
+ private S3Object putRetention(String key, String mode) {
+ return s3Service.putObject(BUCKET, key, BODY, "text/plain", null,
+ new PutObjectOptions()
+ .withObjectLockMode(mode)
+ .withRetainUntilDate(Instant.now().plusSeconds(3600)));
+ }
+
+ private void assertLockedVersionSurvives(String key, String versionId) {
+ S3Object version = s3Service.getObject(BUCKET, key, versionId);
+ assertNotNull(version);
+ assertTrue("ON".equals(version.getLegalHoldStatus()) || version.getObjectLockMode() != null);
+ }
+
+ private static void assertAccessDenied(Runnable action) {
+ AwsException exception = assertThrows(AwsException.class, action::run);
+ assertEquals("AccessDenied", exception.getErrorCode());
+ }
+}