diff --git a/docs/services/s3.md b/docs/services/s3.md index 9f834c2c13..0c9d9fa9c9 100644 --- a/docs/services/s3.md +++ b/docs/services/s3.md @@ -102,6 +102,14 @@ A key with no null version answers as for any missing version: `404 NoSuchVersio the key already has a null version, a later versioned write or delete marker keeps it as a noncurrent version. Deleting newer versions can make that null version 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 +bucket, so the versioning state cannot be changed.", and `PutObjectLockConfiguration` on a bucket +whose versioning is unset or suspended answers `409 InvalidBucketState`, "Versioning must be +'Enabled' on the bucket to apply a Object Lock configuration". The same refusal fails a +CloudFormation update that suspends versioning on such a bucket. A bucket an earlier Floci release +stored in either state loads in that state, and its null versions keep their Object Lock protection. + ## Lifecycle Expiration A bucket's lifecycle configuration is stored as sent and its expiration actions are applied by a 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 9074809966..156bd4e65e 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 @@ -482,8 +482,7 @@ public class S3Controller { } String lockEnabled = httpHeaders.getHeaderString("x-amz-bucket-object-lock-enabled"); if ("true".equalsIgnoreCase(lockEnabled)) { - s3Service.putBucketVersioning(bucket, "Enabled"); - s3Service.setBucketObjectLockEnabled(bucket); + s3Service.enableObjectLockOnCreate(bucket); } return Response.ok() .header("Location", "/" + bucket) 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 3fd7dd6531..2affbb6b8a 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 @@ -2915,16 +2915,28 @@ public class S3Service implements Resettable, ResourceProvider { // --- Versioning Operations --- + /** + * Sets the bucket's versioning status. Versioning cannot be suspended on a bucket with Object + * Lock, and S3 was recorded refusing it with {@code 409 InvalidBucketState}; enabling it again + * is accepted. The check and the write take the bucket monitor that + * {@link #putObjectLockConfiguration} takes, so a concurrent suspend and lock cannot both pass. + */ public void putBucketVersioning(String bucketName, String status) { - Bucket bucket = bucketStore.get(bucketName) - .orElseThrow(() -> new AwsException("NoSuchBucket", - "The specified bucket does not exist.", 404)); + Bucket bucket = requireBucket(bucketName); if (!"Enabled".equals(status) && !"Suspended".equals(status)) { throw new AwsException("MalformedXML", "Versioning status must be 'Enabled' or 'Suspended'.", 400); } - bucket.setVersioningStatus(status); - bucketStore.put(bucketName, bucket); + synchronized (bucket) { + requireSameRecord(bucketName, bucket); + if ("Suspended".equals(status) && bucket.isObjectLockEnabled()) { + throw new AwsException("InvalidBucketState", + "An Object Lock configuration is present on this bucket, so the versioning state " + + "cannot be changed.", 409); + } + bucket.setVersioningStatus(status); + bucketStore.put(bucketName, bucket); + } LOG.infov("Set versioning for bucket {0}: {1}", bucketName, status); } @@ -4284,17 +4296,42 @@ public class S3Service implements Resettable, ResourceProvider { LOG.infov("Enabled Object Lock for bucket: {0}", bucketName); } - public void putObjectLockConfiguration(String bucketName, String mode, String unit, int value) { - Bucket bucket = bucketStore.get(bucketName) - .orElseThrow(() -> new AwsException("NoSuchBucket", - "The specified bucket does not exist.", 404)); - bucket.setBucketObjectLockEnabled(); - if (mode != null && unit != null && value > 0) { - bucket.setDefaultRetention(new ObjectLockRetention(mode, unit, value)); - } else { - bucket.setDefaultRetention(null); + /** + * CreateBucket with Object Lock: enables versioning and Object Lock in one step under the bucket + * monitor, so no versioning request can suspend the new bucket between the two. + */ + public void enableObjectLockOnCreate(String bucketName) { + Bucket bucket = requireBucket(bucketName); + synchronized (bucket) { + requireSameRecord(bucketName, bucket); + bucket.setVersioningStatus("Enabled"); + bucket.setBucketObjectLockEnabled(); + bucketStore.put(bucketName, bucket); + } + LOG.infov("Enabled versioning and Object Lock for bucket: {0}", bucketName); + } + + /** + * Object Lock works only with versioned buckets, and S3 was recorded refusing a configuration on a + * bucket whose versioning is unset or suspended with {@code 409 InvalidBucketState}. Takes the + * bucket monitor {@link #putBucketVersioning} takes. + */ + public void putObjectLockConfiguration(String bucketName, String mode, String unit, int value) { + Bucket bucket = requireBucket(bucketName); + synchronized (bucket) { + requireSameRecord(bucketName, bucket); + if (!bucket.isVersioningEnabled()) { + throw new AwsException("InvalidBucketState", + "Versioning must be 'Enabled' on the bucket to apply a Object Lock configuration", 409); + } + bucket.setBucketObjectLockEnabled(); + if (mode != null && unit != null && value > 0) { + bucket.setDefaultRetention(new ObjectLockRetention(mode, unit, value)); + } else { + bucket.setDefaultRetention(null); + } + bucketStore.put(bucketName, bucket); } - bucketStore.put(bucketName, bucket); LOG.infov("Set Object Lock configuration for bucket: {0}, mode={1}, unit={2}, value={3}", bucketName, mode, unit, value); } diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationS3BucketPropertiesIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationS3BucketPropertiesIntegrationTest.java index 2de78f7b4b..0d0319f84b 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationS3BucketPropertiesIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationS3BucketPropertiesIntegrationTest.java @@ -204,6 +204,33 @@ class CloudFormationS3BucketPropertiesIntegrationTest { bucketGet(bucket, "tagging").body(containsString("before")); } + /** + * A stack update cannot suspend versioning on a bucket that has since had Object Lock enabled: S3 + * refuses it, the update rolls back, and the bucket stays versioned. + */ + @Test + void anUpdateSuspendingVersioningOnALockedBucketRollsBack() throws InterruptedException { + String suffix = Long.toString(System.nanoTime(), 36); + String bucket = "cfn-locked-versioning-" + suffix; + String stackName = "cfn-locked-versioning-stack-" + suffix; + String bucketTemplate = """ + { "Resources": { "Bucket": { "Type": "AWS::S3::Bucket", "Properties": { + "BucketName": "%s", "VersioningConfiguration": { "Status": "%s" } } } } } + """; + String stackId = stack("CreateStack", stackName, bucketTemplate.formatted(bucket, "Enabled")); + await(stackId, "CREATE_COMPLETE"); + given().body("Enabled") + .when().put("/" + bucket + "?object-lock").then().statusCode(200); + + stack("UpdateStack", stackName, bucketTemplate.formatted(bucket, "Suspended")); + await(stackId, "UPDATE_ROLLBACK_COMPLETE"); + + String events = stackEvents(stackId); + assertTrue(events.contains("An Object Lock configuration is present on this bucket"), + "the update failed on the suspend: " + events); + bucketGet(bucket, "versioning").body(containsString("Enabled")); + } + @Test void theBucketCarriesWhatItsTemplateDeclares() throws InterruptedException { String suffix = Long.toString(System.nanoTime(), 36); diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3NullVersionReviewTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3NullVersionReviewTest.java index e23cec6cf8..38b9f47720 100644 --- a/src/test/java/io/github/hectorvent/floci/services/s3/S3NullVersionReviewTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3NullVersionReviewTest.java @@ -36,6 +36,9 @@ class S3NullVersionReviewTest { @TempDir Path tempDir; + /** The bucket store of the service {@link #service} last built. */ + private InMemoryStorage buckets; + @ParameterizedTest @ValueSource(booleans = {true, false}) void rollbackThenPutListsTheReplacementNull(boolean memory) { @@ -159,7 +162,7 @@ class S3NullVersionReviewTest { } else { service.putObjectRetention("bucket", "key", "null", lock, Instant.now().plusSeconds(3600), false); } - service.putBucketVersioning("bucket", "Suspended"); + suspendLockedBucket(); assertDenied(() -> service.putObject("bucket", "key", bytes("replacement"), "text/plain", Map.of())); assertPreservedBodies(service); @@ -455,7 +458,7 @@ class S3NullVersionReviewTest { service.putBucketVersioning("bucket", "Enabled"); assertFalse(service.deleteOverridesGovernanceRetention("bucket", "key", null)); service.putObject("bucket", "key", bytes("numbered"), "text/plain", Map.of()); - service.putBucketVersioning("bucket", "Suspended"); + suspendLockedBucket(); boolean overrides = service.deleteOverridesGovernanceRetention("bucket", "key", null); assertFalse(overrides); for (boolean s3Enforced : List.of(false, true)) { @@ -486,11 +489,22 @@ class S3NullVersionReviewTest { } private S3Service service(boolean memory, InMemoryStorage objects) { - S3Service service = new S3Service(new InMemoryStorage<>(), objects, tempDir.resolve("s3"), memory); + buckets = new InMemoryStorage<>(); + S3Service service = new S3Service(buckets, objects, tempDir.resolve("s3"), memory); service.createBucket("bucket", "us-east-1"); return service; } + /** + * Suspends versioning on a bucket that already has Object Lock. PutBucketVersioning refuses + * this, but a bucket stored before it did can still be in this state. + */ + private void suspendLockedBucket() { + Bucket bucket = buckets.get("bucket").orElseThrow(); + bucket.setVersioningStatus("Suspended"); + buckets.put("bucket", bucket); + } + private static Object readSnapshot(S3Service service, boolean explicitNull, Function reader) throws Exception { Method method = explicitNull 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 ec350fa2f1..786923dc5b 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 @@ -572,6 +572,80 @@ class S3ObjectLockIntegrationTest { .body(containsString("NoSuchObjectLockConfiguration")); } + // --- versioning on a bucket with Object Lock --- + + @Test + @Order(30) + void versioningCannotBeSuspendedOnABucketWithObjectLock() { + given() + .body(versioning("Suspended")) + .when() + .put("/" + LOCK_BUCKET + "?versioning") + .then() + .statusCode(409) + .body(containsString("InvalidBucketState")) + .body(containsString("An Object Lock configuration is present on this bucket, " + + "so the versioning state cannot be changed.")); + assertVersioning(LOCK_BUCKET, "Enabled"); + + given().body(versioning("Enabled")).when().put("/" + LOCK_BUCKET + "?versioning").then().statusCode(200); + assertVersioning(LOCK_BUCKET, "Enabled"); + } + + @Test + @Order(31) + void objectLockConfigurationNeedsVersioningEnabled() { + String bucket = "object-lock-needs-versioning"; + given().when().put("/" + bucket).then().statusCode(200); + assertObjectLockConfigurationRefused(bucket); + + given().body(versioning("Suspended")).when().put("/" + bucket + "?versioning").then().statusCode(200); + assertObjectLockConfigurationRefused(bucket); + + given().body(versioning("Enabled")).when().put("/" + bucket + "?versioning").then().statusCode(200); + given() + .body("Enabled") + .when() + .put("/" + bucket + "?object-lock") + .then() + .statusCode(200); + given().body(versioning("Suspended")).when().put("/" + bucket + "?versioning").then().statusCode(409) + .body(containsString("InvalidBucketState")); + assertVersioning(bucket, "Enabled"); + } + + private static String versioning(String status) { + return "" + status + ""; + } + + private static void assertVersioning(String bucket, String status) { + given() + .when() + .get("/" + bucket + "?versioning") + .then() + .statusCode(200) + .body(containsString("" + status + "")); + } + + /** S3 was recorded refusing this on a bucket whose versioning is unset and on one where it is suspended. */ + private static void assertObjectLockConfigurationRefused(String bucket) { + given() + .body("Enabled") + .when() + .put("/" + bucket + "?object-lock") + .then() + .statusCode(409) + .body(containsString("InvalidBucketState")) + .body("Error.Message", equalTo( + "Versioning must be 'Enabled' on the bucket to apply a Object Lock configuration")); + given() + .when() + .get("/" + bucket + "?object-lock") + .then() + .statusCode(404) + .body(containsString("ObjectLockConfigurationNotFoundError")); + } + /** A PutObject, a CopyObject of {@code copySource} or a CreateMultipartUpload of {@code path}. */ private static Response createObject(String operation, String path, String copySource, RequestSpecification request) { 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 4ba3021458..0dac653fc2 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 @@ -47,6 +47,56 @@ class S3VersioningServiceTest { assertEquals("Suspended", s3Service.getBucketVersioning("versioned-bucket")); } + @Test + void versioningCannotBeSuspendedOnABucketWithObjectLock() { + s3Service.enableObjectLockOnCreate("versioned-bucket"); + + AwsException refused = assertThrows(AwsException.class, + () -> s3Service.putBucketVersioning("versioned-bucket", "Suspended")); + assertEquals("InvalidBucketState", refused.getErrorCode()); + assertEquals(409, refused.getHttpStatus()); + assertEquals("An Object Lock configuration is present on this bucket, so the versioning state " + + "cannot be changed.", refused.getMessage()); + assertEquals("Enabled", s3Service.getBucketVersioning("versioned-bucket")); + + s3Service.putBucketVersioning("versioned-bucket", "Enabled"); + assertEquals("Enabled", s3Service.getBucketVersioning("versioned-bucket")); + } + + @Test + void objectLockConfigurationNeedsVersioningEnabled() { + assertObjectLockConfigurationRefused(); + s3Service.putBucketVersioning("versioned-bucket", "Enabled"); + s3Service.putBucketVersioning("versioned-bucket", "Suspended"); + assertObjectLockConfigurationRefused(); + + s3Service.putBucketVersioning("versioned-bucket", "Enabled"); + s3Service.putObjectLockConfiguration("versioned-bucket", "GOVERNANCE", "Days", 1); + assertEquals("GOVERNANCE", s3Service.getObjectLockConfiguration("versioned-bucket").mode()); + } + + @Test + void aMissingBucketAndAnUnknownStatusAreReportedBeforeObjectLock() { + assertEquals("NoSuchBucket", assertThrows(AwsException.class, + () -> s3Service.putBucketVersioning("missing-bucket", "Bogus")).getErrorCode()); + assertEquals("NoSuchBucket", assertThrows(AwsException.class, + () -> s3Service.putObjectLockConfiguration("missing-bucket", null, null, 0)).getErrorCode()); + s3Service.enableObjectLockOnCreate("versioned-bucket"); + assertEquals("MalformedXML", assertThrows(AwsException.class, + () -> s3Service.putBucketVersioning("versioned-bucket", "Bogus")).getErrorCode()); + } + + private void assertObjectLockConfigurationRefused() { + AwsException refused = assertThrows(AwsException.class, + () -> s3Service.putObjectLockConfiguration("versioned-bucket", "GOVERNANCE", "Days", 1)); + assertEquals("InvalidBucketState", refused.getErrorCode()); + assertEquals(409, refused.getHttpStatus()); + assertEquals("Versioning must be 'Enabled' on the bucket to apply a Object Lock configuration", + refused.getMessage()); + assertEquals("ObjectLockConfigurationNotFoundError", assertThrows(AwsException.class, + () -> s3Service.getObjectLockConfiguration("versioned-bucket")).getErrorCode()); + } + @Test void suspendedPutReplacesAnArchivedNullVersionWithoutLeavingADuplicate() { s3Service.putObject("versioned-bucket", "key",