fix(s3): keep Object Lock buckets versioning-enabled (#6322)

A bucket with Object Lock is versioned for good. PutBucketVersioning
with Suspended on such a bucket now answers 409 InvalidBucketState and
leaves it Enabled, and PutObjectLockConfiguration on a bucket whose
versioning is unset or suspended answers 409 InvalidBucketState and
enables nothing, matching the AWS-recorded messages. CloudFormation goes
through the same PutBucketVersioning path, so an update that suspends a
locked bucket rolls back.

Both checks run with the write under the bucket monitor, and CreateBucket
with Object Lock now enables versioning and the lock in one step, so no
concurrent request can leave a locked bucket suspended.

Buckets stored in either state by an earlier release load as they were;
the null-version protection for them is unchanged, and its tests now set
that state on the stored bucket.
This commit is contained in:
TiM authored and GitHub committed 2026-10-23 00:00:01 -04:00
1 parent 510a1cbbb8
commit 212a424ed2
7 files changed
+229 -20

No files matched your search

+8
View File
@@ -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
@@ -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)
@@ -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);
}
@@ -204,6 +204,33 @@ class CloudFormationS3BucketPropertiesIntegrationTest {
bucketGet(bucket, "tagging").body(containsString("<Value>before</Value>"));
}
/**
* 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("<ObjectLockConfiguration><ObjectLockEnabled>Enabled</ObjectLockEnabled></ObjectLockConfiguration>")
.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("<Status>Enabled</Status>"));
}
@Test
void theBucketCarriesWhatItsTemplateDeclares() throws InterruptedException {
String suffix = Long.toString(System.nanoTime(), 36);
@@ -36,6 +36,9 @@ class S3NullVersionReviewTest {
@TempDir
Path tempDir;
/** The bucket store of the service {@link #service} last built. */
private InMemoryStorage<String, Bucket> 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<String, S3Object> 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<Object, String> reader)
throws Exception {
Method method = explicitNull
@@ -572,6 +572,80 @@ class S3ObjectLockIntegrationTest {
.body(containsString("<Code>NoSuchObjectLockConfiguration</Code>"));
}
// --- 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("<Code>InvalidBucketState</Code>"))
.body(containsString("<Message>An Object Lock configuration is present on this bucket, "
+ "so the versioning state cannot be changed.</Message>"));
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("<ObjectLockConfiguration><ObjectLockEnabled>Enabled</ObjectLockEnabled></ObjectLockConfiguration>")
.when()
.put("/" + bucket + "?object-lock")
.then()
.statusCode(200);
given().body(versioning("Suspended")).when().put("/" + bucket + "?versioning").then().statusCode(409)
.body(containsString("<Code>InvalidBucketState</Code>"));
assertVersioning(bucket, "Enabled");
}
private static String versioning(String status) {
return "<VersioningConfiguration><Status>" + status + "</Status></VersioningConfiguration>";
}
private static void assertVersioning(String bucket, String status) {
given()
.when()
.get("/" + bucket + "?versioning")
.then()
.statusCode(200)
.body(containsString("<Status>" + status + "</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("<ObjectLockConfiguration><ObjectLockEnabled>Enabled</ObjectLockEnabled></ObjectLockConfiguration>")
.when()
.put("/" + bucket + "?object-lock")
.then()
.statusCode(409)
.body(containsString("<Code>InvalidBucketState</Code>"))
.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) {
@@ -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",