Light, fluffy, and always free - The AWS Local Emulator alternative
fix(s3): refuse Object Lock settings on a bucket without Object Lock (#5969)
Priamo Germosen36b315d77b42
diff --git a/docs/services/s3.md b/docs/services/s3.mdindex 57e673c9d4..3322e3d813 100644--- a/docs/services/s3.md+++ b/docs/services/s3.md@@ -686,7 +686,7 @@ Floci now persists and returns the following object attribute state on S3 object- multipart checksums follow the AWS checksum type: `COMPOSITE` (`<checksum of the part checksums>-<part count>`) for SHA1, SHA256 and, by default, CRC32 and CRC32C; `FULL_OBJECT` for CRC64NVME or when `x-amz-checksum-type: FULL_OBJECT` was requested on `CreateMultipartUpload`. The `-N` suffix is returned by `CompleteMultipartUpload`, `HeadObject` and `GetObject`, and omitted by `GetObjectAttributes`, which lists part-level checksums only for `COMPOSITE` objects (a `FULL_OBJECT` multipart object reports its `PartsCount` alone, a single-part object has no `ObjectParts`), as on AWS. `UploadPart` echoes the part checksum (`x-amz-checksum-<algorithm>`) when the upload declared an algorithm, and completing a `COMPOSITE` upload requires the checksum of every part in the `CompleteMultipartUpload` body (`InvalidRequest` otherwise), while `FULL_OBJECT` uploads accept a body without them- canned object ACLs from `x-amz-acl` on `PutObject`, `CopyObject`, and multipart initiation- explicit ACL grant headers (`x-amz-grant-read`, `x-amz-grant-write`, `x-amz-grant-full-control`, `x-amz-grant-read-acp`, `x-amz-grant-write-acp`) with `id` and `uri` grantees on `PutObject`, `CopyObject`, and multipart initiation, where they apply when the upload completes-- Object Lock headers (`x-amz-object-lock-mode`, `x-amz-object-lock-retain-until-date`, `x-amz-object-lock-legal-hold`) on `PutObject`, `CopyObject`, and multipart initiation, where they apply when the upload completes. A mode without a date or a date without a mode, an unknown mode, or a date that is not an ISO 8601 timestamp is refused with `400 InvalidArgument`+- Object Lock headers (`x-amz-object-lock-mode`, `x-amz-object-lock-retain-until-date`, `x-amz-object-lock-legal-hold`) on `PutObject`, `CopyObject`, and multipart initiation, where they apply when the upload completes. A mode without a date or a date without a mode, an unknown mode, or a date that is not an ISO 8601 timestamp is refused with `400 InvalidArgument`. In a bucket without Object Lock, a mode, a date or a legal hold of `ON` is refused with `400 InvalidRequest`, "Bucket is missing Object Lock Configuration", as `PutObjectRetention`, `GetObjectRetention`, `PutObjectLegalHold` and `GetObjectLegalHold` are there. `GetObjectRetention` and `GetObjectLegalHold` of an object without a retention or a legal hold answer `404 NoSuchObjectLockConfiguration`. A `PutObjectRetention` body with a mode but no date, a date but no mode, or an unknown mode is refused with `400 MalformedXML`; one naming neither removes the retention- explicit object SSE headers from `x-amz-server-side-encryption` on `PutObject`, `CopyObject`, and multipart initiation, replayed on `GetObject` and `HeadObject`- SSE-KMS key IDs from `x-amz-server-side-encryption-aws-kms-key-id` on `PutObject` and multipart initiation, and from `CopyObject`/`UploadPartCopy` (inherited from the source object when the request doesn't specify new SSE headers), returned by `PutObject`, `CopyObject`, `UploadPartCopy`, `CompleteMultipartUpload`, `GetObject`, and `HeadObject`. A bucket's default SSE-KMS key is not simulated: `aws:kms` with no explicit key ID omits the header rather than returning a synthesized `aws/s3` key ARNdiff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.javaindex 77745eb4e1..54ea52c435 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@@ -3007,11 +3007,10 @@ public class S3Controller {private Response handleGetObjectLegalHold(String bucket, String key, String versionId) {S3Object obj = s3Service.getObjectLegalHold(bucket, key, versionId);- String status = obj.getLegalHoldStatus() != null ? obj.getLegalHoldStatus() : "OFF";String xml = new XmlBuilder().raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").start("LegalHold", AwsNamespaces.S3)- .elem("Status", status)+ .elem("Status", obj.getLegalHoldStatus()).end("LegalHold").build();return Response.ok(xml).type(MediaType.APPLICATION_XML).build();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.javaindex f365167eb6..ed1c5e4062 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@@ -695,6 +695,7 @@ public class S3Service implements Resettable, ResourceProvider {.orElseThrow(() -> new AwsException("NoSuchBucket","The specified bucket does not exist.", 404));rejectUploadAclWhenOwnershipEnforced(checkedBucket.value(), checkedBucket.account(), effectiveOptions);+ rejectObjectLockWithoutLockEnabled(checkedBucket.value(), effectiveOptions);rejectConflictingServerSideEncryption(normalizeServerSideEncryption(effectiveOptions.getServerSideEncryption()),validateSseCustomerKey(effectiveOptions.getSseCustomerAlgorithm(),@@ -877,6 +878,7 @@ public class S3Service implements Resettable, ResourceProvider {object.setSseCustomerKeyMd5(sseCustomerKey.keyMd5());}rejectUploadAclWhenOwnershipEnforced(bucket, bucketOwnerAccount, effectiveOptions);+ rejectObjectLockWithoutLockEnabled(bucket, effectiveOptions);String objectAcl = resolveObjectAclXml(bucketOwnerAccount,effectiveOptions.getAcl(), effectiveOptions.getGrantRead(),effectiveOptions.getGrantWrite(), effectiveOptions.getGrantFullControl(),@@ -4027,12 +4029,55 @@ public class S3Service implements Resettable, ResourceProvider {return bucket.getDefaultRetention();}+ /**+ * An object has Object Lock settings only in a bucket with Object Lock enabled. S3 was recorded+ * refusing retention and legal hold on any other bucket with this error.+ */+ private static void requireObjectLockEnabled(Bucket bucket) {+ if (!bucket.isObjectLockEnabled()) {+ throw new AwsException("InvalidRequest", "Bucket is missing Object Lock Configuration", 400);+ }+ }++ /**+ * Refuses the Object Lock settings an object is created with in a bucket without Object Lock, as+ * retention and legal hold are refused there: a mode, a retain-until date or a legal hold that is+ * ON. A legal hold that is OFF sets no lock.+ */+ private static void rejectObjectLockWithoutLockEnabled(Bucket bucket, PutObjectOptions options) {+ if (options.getObjectLockMode() != null || options.getRetainUntilDate() != null+ || "ON".equals(options.getLegalHoldStatus())) {+ requireObjectLockEnabled(bucket);+ }+ }++ /**+ * The version a retention or legal-hold request names. S3 was recorded refusing these requests+ * on a bucket without Object Lock before it looks for the key.+ */+ private AccountAwareStorageBackend.OwnedEntry<S3Object> getLockableObjectEntry(+ String bucketName, String key, String versionId) {+ requireObjectLockEnabled(resolveBucket(bucketName)+ .orElseThrow(() -> new AwsException("NoSuchBucket",+ "The specified bucket does not exist.", 404)));+ return getStoredObjectEntry(bucketName, key, versionId);+ }+public void putObjectRetention(String bucketName, String key, String versionId,String mode, Instant retainUntil, boolean bypassGovernance) {AccountAwareStorageBackend.OwnedEntry<S3Object> ownedObject =- getStoredObjectEntry(bucketName, key, versionId);+ getLockableObjectEntry(bucketName, key, versionId);S3Object obj = ownedObject.value();+ // A mode without a date, or a mode S3 does not know, is malformed, as S3 was recorded+ // answering; a date without a mode is refused the same way, since GetObjectRetention reads a+ // retention by its mode. A retention naming neither removes it.+ if ((mode == null) != (retainUntil == null)+ || (mode != null && !"GOVERNANCE".equals(mode) && !"COMPLIANCE".equals(mode))) {+ throw new AwsException("MalformedXML",+ "The XML you provided was not well-formed or did not validate against our published schema", 400);+ }+boolean activeComplianceRetention = "COMPLIANCE".equals(obj.getObjectLockMode())&& obj.getRetainUntilDate() != null&& Instant.now().isBefore(obj.getRetainUntilDate());@@ -4070,12 +4115,22 @@ public class S3Service implements Resettable, ResourceProvider {}public S3Object getObjectRetention(String bucketName, String key, String versionId) {- return getStoredObject(bucketName, key, versionId);+ S3Object object = getLockableObjectEntry(bucketName, key, versionId).value();+ if (object.getObjectLockMode() == null) {+ throw noSuchObjectLockConfiguration();+ }+ return object;+ }++ /** S3 was recorded answering a read of a retention or legal hold the object does not have with this error. */+ private static AwsException noSuchObjectLockConfiguration() {+ return new AwsException("NoSuchObjectLockConfiguration",+ "The specified object does not have a ObjectLock configuration", 404);}public void putObjectLegalHold(String bucketName, String key, String versionId, String status) {AccountAwareStorageBackend.OwnedEntry<S3Object> ownedObject =- getStoredObjectEntry(bucketName, key, versionId);+ getLockableObjectEntry(bucketName, key, versionId);S3Object obj = ownedObject.value();obj.setLegalHoldStatus(status);putObjectMetadataForAccount(ownedObject.account(), bucketName, key, obj);@@ -4083,7 +4138,11 @@ public class S3Service implements Resettable, ResourceProvider {}public S3Object getObjectLegalHold(String bucketName, String key, String versionId) {- return getStoredObject(bucketName, key, versionId);+ S3Object object = getLockableObjectEntry(bucketName, key, versionId).value();+ if (object.getLegalHoldStatus() == null) {+ throw noSuchObjectLockConfiguration();+ }+ return object;}// --- Multipart Upload Operations ---@@ -4181,6 +4240,7 @@ public class S3Service implements Resettable, ResourceProvider {"The specified bucket does not exist.", 404));String ownerAccount = getBucketOwnerAccountId(bucket);rejectUploadAclWhenOwnershipEnforced(destination, ownerAccount, objectOptions);+ rejectObjectLockWithoutLockEnabled(destination, objectOptions);resolveObjectAclXml(ownerAccount, acl, objectOptions.getGrantRead(), objectOptions.getGrantWrite(),objectOptions.getGrantFullControl(), objectOptions.getGrantReadAcp(),objectOptions.getGrantWriteAcp());diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.javaindex 2703f460b2..28f5eaa82f 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.java@@ -1481,17 +1481,24 @@ class S3AuthEnforcementIntegrationTest {String bucket = "auth-batch-bypass-scope-bucket";String unlockedKey = "unlocked.txt";String lockedKey = "locked.txt";- given().filter(LOCAL_SIGNER).when().put("/" + bucket).then().statusCode(200);-given()+ .filter(LOCAL_SIGNER)+ .header("x-amz-bucket-object-lock-enabled", "true")+ .when()+ .put("/" + bucket)+ .then()+ .statusCode(200);++ String unlockedVersion = given().filter(LOCAL_SIGNER).body("unlocked").when().put("/" + bucket + "/" + unlockedKey).then()- .statusCode(200);+ .statusCode(200)+ .extract().header("x-amz-version-id");- given()+ String lockedVersion = given().filter(LOCAL_SIGNER).header("x-amz-object-lock-mode", "GOVERNANCE").header("x-amz-object-lock-retain-until-date", "2030-01-01T00:00:00Z")@@ -1499,23 +1506,25 @@ class S3AuthEnforcementIntegrationTest {.when().put("/" + bucket + "/" + lockedKey).then()- .statusCode(200);+ .statusCode(200)+ .extract().header("x-amz-version-id");given().filter(LOCAL_SIGNER).contentType("application/json")- .body(publicObjectActionPolicy(bucket, "s3:DeleteObject"))+ .body(publicObjectActionPolicy(bucket, "s3:DeleteObjectVersion")).when().put("/" + bucket + "?policy").then().statusCode(200);+ // The lock protects a version, so the entries name theirs.String deleteXml = """<Delete>- <Object><Key>%s</Key></Object>- <Object><Key>%s</Key></Object>+ <Object><Key>%s</Key><VersionId>%s</VersionId></Object>+ <Object><Key>%s</Key><VersionId>%s</VersionId></Object></Delete>- """.formatted(unlockedKey, lockedKey);+ """.formatted(unlockedKey, unlockedVersion, lockedKey, lockedVersion);given().header("x-amz-bypass-governance-retention", "true")diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3DeleteObjectsIamEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3DeleteObjectsIamEnforcementIntegrationTest.javaindex 03504a21fa..e7789dd8dc 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3DeleteObjectsIamEnforcementIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3DeleteObjectsIamEnforcementIntegrationTest.java@@ -231,7 +231,7 @@ class S3DeleteObjectsIamEnforcementIntegrationTest {@Testvoid bypassingGovernanceRetentionNeedsTheBypassPermission() {String bucket = bucket("delobj-governance");- enableVersioning(bucket);+ enableObjectLock(bucket);String versionId = putGovernanceRetained(bucket, "retained.txt");String plainVersionId = put(bucket, "plain.txt");String user = uniqueName("delobj-governance");@@ -260,12 +260,17 @@ class S3DeleteObjectsIamEnforcementIntegrationTest {}// An object written before versioning was enabled is the null version, which an entry names as- // VersionId "null". Its retention protects it as any version's does, so the bypass needs the permission.+ // VersionId "null". Retention set on it once the bucket has Object Lock protects it as any version's+ // does, so the bypass needs the permission.@Testvoid bypassingGovernanceRetentionOnTheNullVersionNeedsTheBypassPermission() {String bucket = bucket("delobj-governance-null");- putGovernanceRetained(bucket, "retained.txt");- enableVersioning(bucket);+ put(bucket, "retained.txt");+ enableObjectLock(bucket);+ given().header("Authorization", auth(ADMIN, "s3")).queryParam("versionId", "null")+ .body("<Retention><Mode>GOVERNANCE</Mode>"+ + "<RetainUntilDate>2099-01-01T00:00:00Z</RetainUntilDate></Retention>")+ .when().put("/" + bucket + "/retained.txt?retention").then().statusCode(200);String user = uniqueName("delobj-governance-null");String accessKeyId = createUser(user);String objects = "arn:aws:s3:::" + bucket + "/*";@@ -289,7 +294,7 @@ class S3DeleteObjectsIamEnforcementIntegrationTest {@Testvoid aNullVersionEntryDoesNotInheritTheRetentionOfAVersionedObject() {String bucket = bucket("delobj-governance-no-null");- enableVersioning(bucket);+ enableObjectLock(bucket);String versionId = putGovernanceRetained(bucket, "retained.txt");String user = uniqueName("delobj-governance-no-null");String accessKeyId = createUser(user);@@ -455,6 +460,14 @@ class S3DeleteObjectsIamEnforcementIntegrationTest {.when().put("/" + bucket + "?versioning").then().statusCode(200);}+ /** Object Lock on an existing bucket, which needs versioning: "Object Lock works only with versioned buckets". */+ private static void enableObjectLock(String bucket) {+ enableVersioning(bucket);+ given().header("Authorization", auth(ADMIN, "s3")).contentType("application/xml")+ .body("<ObjectLockConfiguration><ObjectLockEnabled>Enabled</ObjectLockEnabled></ObjectLockConfiguration>")+ .when().put("/" + bucket + "?object-lock").then().statusCode(200);+ }+private static void assertPresent(String bucket, String key) {given().header("Authorization", auth(ADMIN, "s3")).when().head("/" + bucket + "/" + key).then().statusCode(200);diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3DeleteObjectsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3DeleteObjectsIntegrationTest.javaindex 26d61f3d88..df1fba8b4f 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3DeleteObjectsIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3DeleteObjectsIntegrationTest.java@@ -129,6 +129,12 @@ class S3DeleteObjectsIntegrationTest {void deleteObjects_governanceRetentionVersionIdHonorsBypassHeader() {String bucket = createBucket();enableVersioning(bucket);+ given()+ .body("<ObjectLockConfiguration><ObjectLockEnabled>Enabled</ObjectLockEnabled></ObjectLockConfiguration>")+ .when()+ .put("/" + bucket + "?object-lock")+ .then()+ .statusCode(200);String key = "governance.txt";String versionId = putGovernanceLockedObject(bucket, key);diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3GlobalBucketNamespaceTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3GlobalBucketNamespaceTest.javaindex e96640b21b..290c9bfa7e 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3GlobalBucketNamespaceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3GlobalBucketNamespaceTest.java@@ -252,6 +252,7 @@ class S3GlobalBucketNamespaceTest {globalNs.putBucketOwnershipControls("shared-metadata-bucket", "<OwnershipControls><Rule>"+ "<ObjectOwnership>ObjectWriter</ObjectOwnership></Rule></OwnershipControls>");globalNs.putBucketVersioning("shared-metadata-bucket", "Enabled");+ globalNs.setBucketObjectLockEnabled("shared-metadata-bucket");S3Object created = globalNs.putObject("shared-metadata-bucket", "object.txt", "payload".getBytes(UTF_8),"text/plain", Map.of());diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3LifecycleServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3LifecycleServiceTest.javaindex 2c63f2acb5..6318554725 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3LifecycleServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3LifecycleServiceTest.java@@ -129,6 +129,7 @@ class S3LifecycleServiceTest {@Testvoid aNoncurrentVersionUnderALegalHoldIsKept() {s3Service.putBucketVersioning(BUCKET, "Enabled");+ s3Service.setBucketObjectLockEnabled(BUCKET);S3Object held = s3Service.putObject(BUCKET, "a.txt", "held".getBytes(StandardCharsets.UTF_8), "text/plain",Map.of(), null, null, "ON");S3Object current = put("a.txt");diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectIndexJournalTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectIndexJournalTest.javaindex 94421f8bc3..cacc96f1cc 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectIndexJournalTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectIndexJournalTest.java@@ -144,6 +144,7 @@ class S3ObjectIndexJournalTest {S3Service s3 = newService(first, directory);s3.createBucket(VERSIONED_BUCKET, REGION);s3.putBucketVersioning(VERSIONED_BUCKET, "Enabled");+ s3.setBucketObjectLockEnabled(VERSIONED_BUCKET);String current = s3.putObject(VERSIONED_BUCKET, key(0), body(0), "text/plain", Map.of()).getVersionId();s3.putObjectLegalHold(VERSIONED_BUCKET, key(0), current, "ON");copyAsLeftByAKill(directory, afterCrash);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.javaindex 2a0f833338..ec350fa2f1 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@@ -465,6 +465,130 @@ class S3ObjectLockIntegrationTest {}}+ // --- Object Lock settings on a bucket without Object Lock ---++ @Test+ @Order(25)+ void retentionAndLegalHoldAreRefusedOnABucketWithoutObjectLock() {+ String retention = "<Retention><Mode>GOVERNANCE</Mode>"+ + "<RetainUntilDate>2099-01-01T00:00:00Z</RetainUntilDate></Retention>";+ for (String bucket : new String[] {PLAIN_BUCKET, NO_LOCK_BUCKET}) {+ String path = "/" + bucket + "/unlockable.txt";+ given().body("x").when().put(path).then().statusCode(200);++ assertMissingObjectLockConfiguration(given().body(retention).when().put(path + "?retention"));+ assertMissingObjectLockConfiguration(given().when().get(path + "?retention"));+ assertMissingObjectLockConfiguration(given().body("<LegalHold><Status>ON</Status></LegalHold>")+ .when().put(path + "?legal-hold"));+ assertMissingObjectLockConfiguration(given().when().get(path + "?legal-hold"));+ // S3 was recorded answering a legal hold without a status as malformed first.+ given()+ .body("<LegalHold></LegalHold>")+ .when()+ .put(path + "?legal-hold")+ .then()+ .statusCode(400)+ .body(containsString("<Code>MalformedXML</Code>"));+ }+ }++ @Test+ @Order(26)+ void objectLockHeadersAreRefusedOnABucketWithoutObjectLock() {+ given().body("source").when().put("/" + PLAIN_BUCKET + "/plain-source.txt").then().statusCode(200);+ String copySource = "/" + PLAIN_BUCKET + "/plain-source.txt";+ for (String operation : new String[] {"put", "copy", "upload"}) {+ String path = "/" + PLAIN_BUCKET + "/refused-lock-" + operation + ".txt";+ assertMissingObjectLockConfiguration(createObject(operation, path, copySource, given()+ .header("x-amz-object-lock-mode", "GOVERNANCE")+ .header("x-amz-object-lock-retain-until-date", "2099-01-01T00:00:00Z")));+ assertMissingObjectLockConfiguration(createObject(operation, path, copySource, given()+ .header("x-amz-object-lock-legal-hold", "ON")));+ given().when().head(path).then().statusCode(404);+ // A legal hold that is OFF sets no lock, so the object is created.+ createObject(operation, path, copySource, given().header("x-amz-object-lock-legal-hold", "OFF"))+ .then()+ .statusCode(200);+ }+ }++ @Test+ @Order(27)+ void aCopyOfALockedObjectIntoABucketWithoutObjectLockTakesNoLock() {+ given()+ .header("x-amz-copy-source", "/" + LOCK_BUCKET + "/copy-lock-target.txt")+ .when()+ .put("/" + PLAIN_BUCKET + "/copy-of-locked.txt")+ .then()+ .statusCode(200);++ given().when().delete("/" + PLAIN_BUCKET + "/copy-of-locked.txt").then().statusCode(204);+ }++ @Test+ @Order(28)+ void readingARetentionOrLegalHoldTheObjectDoesNotHaveIsNoSuchObjectLockConfiguration() {+ String bucket = "object-lock-without-rule";+ given().header("x-amz-bucket-object-lock-enabled", "true").when().put("/" + bucket).then().statusCode(200);+ String path = "/" + bucket + "/never-locked.txt";+ given().body("x").when().put(path).then().statusCode(200);++ for (String setting : new String[] {"?retention", "?legal-hold"}) {+ given()+ .when()+ .get(path + setting)+ .then()+ .statusCode(404)+ .body(containsString("<Code>NoSuchObjectLockConfiguration</Code>"))+ .body(containsString("<Message>The specified object does not have a ObjectLock configuration</Message>"));+ }+ }++ @Test+ @Order(29)+ void aRetentionMissingItsModeOrItsDateOrWithAnUnknownModeIsMalformedAndNotSaved() {+ String bucket = "object-lock-incomplete-retention";+ given().header("x-amz-bucket-object-lock-enabled", "true").when().put("/" + bucket).then().statusCode(200);+ String path = "/" + bucket + "/incomplete.txt";+ given().body("x").when().put(path).then().statusCode(200);++ for (String retention : new String[] {+ "<Mode>GOVERNANCE</Mode>",+ "<RetainUntilDate>2099-01-01T00:00:00Z</RetainUntilDate>",+ "<Mode>BAD-VALUE</Mode><RetainUntilDate>2099-01-01T00:00:00Z</RetainUntilDate>"}) {+ given()+ .body("<Retention xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" + retention + "</Retention>")+ .when()+ .put(path + "?retention")+ .then()+ .statusCode(400)+ .body(containsString("<Code>MalformedXML</Code>"));+ }+ given()+ .when()+ .get(path + "?retention")+ .then()+ .statusCode(404)+ .body(containsString("<Code>NoSuchObjectLockConfiguration</Code>"));+ }++ /** 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) {+ return switch (operation) {+ case "put" -> request.body("x").when().put(path);+ case "copy" -> request.header("x-amz-copy-source", copySource).when().put(path);+ default -> request.when().post(path + "?uploads");+ };+ }++ private static void assertMissingObjectLockConfiguration(Response response) {+ response.then()+ .statusCode(400)+ .body(containsString("<Code>InvalidRequest</Code>"))+ .body(containsString("<Message>Bucket is missing Object Lock Configuration</Message>"));+ }+/** A PutObject, a CopyObject of {@code copySource} or a CreateMultipartUpload refused for its lock headers. */private static void assertInvalidLockHeaders(String operation, String copySource, String mode, String until,String argumentName, String argumentValue, String message) {@@ -476,12 +600,7 @@ class S3ObjectLockIntegrationTest {request = request.header("x-amz-object-lock-retain-until-date", until);}String path = "/" + LOCK_BUCKET + "/invalid-lock-" + operation + ".txt";- Response response = switch (operation) {- case "put" -> request.body("x").when().put(path);- case "copy" -> request.header("x-amz-copy-source", copySource).when().put(path);- default -> request.when().post(path + "?uploads");- };- ValidatableResponse then = response.then()+ ValidatableResponse then = createObject(operation, path, copySource, request).then().statusCode(400).body(containsString("<Code>InvalidArgument</Code>")).body(containsString(message))diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ServiceAnnotationsTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ServiceAnnotationsTest.javaindex e9d76f0c7f..fb267b23dd 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3ServiceAnnotationsTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ServiceAnnotationsTest.java@@ -493,6 +493,7 @@ class S3ServiceAnnotationsTest {@Testvoid governanceRetentionBlocksAnnotationDeleteWithoutBypass() {+ s3Service.setBucketObjectLockEnabled(BUCKET);s3Service.putObject(BUCKET, "locked.txt", "body".getBytes(StandardCharsets.UTF_8),null, null, new io.github.hectorvent.floci.services.s3.model.PutObjectOptions().withObjectLockMode("GOVERNANCE")@@ -523,6 +524,7 @@ class S3ServiceAnnotationsTest {@Testvoid complianceRetentionBlocksAnnotationPutAndDelete() {+ s3Service.setBucketObjectLockEnabled(BUCKET);s3Service.putObject(BUCKET, "compliance.txt", "body".getBytes(StandardCharsets.UTF_8),null, null, new io.github.hectorvent.floci.services.s3.model.PutObjectOptions().withObjectLockMode("COMPLIANCE")