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 38b3200cfc..6bc80198b8 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 @@ -1859,9 +1859,19 @@ public class S3Service implements Resettable, ResourceProvider { .orElseThrow(() -> new AwsException("NoSuchKey", "The specified key does not exist.", 404)); - // COMPLIANCE mode: retainUntil cannot be shortened - if ("COMPLIANCE".equals(obj.getObjectLockMode()) + boolean activeComplianceRetention = "COMPLIANCE".equals(obj.getObjectLockMode()) && obj.getRetainUntilDate() != null + && Instant.now().isBefore(obj.getRetainUntilDate()); + + // Active COMPLIANCE mode cannot be changed or removed, even when the + // retention date is unchanged or extended. + if (activeComplianceRetention && !"COMPLIANCE".equals(mode)) { + throw new AwsException("AccessDenied", + "COMPLIANCE retention mode cannot be changed", 403); + } + + // Active COMPLIANCE mode: retainUntil cannot be shortened. + if (activeComplianceRetention && retainUntil != null && retainUntil.isBefore(obj.getRetainUntilDate())) { throw new AwsException("AccessDenied", 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 c41a1e35db..8444fe6c3a 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 @@ -143,7 +143,7 @@ class S3ObjectLockIntegrationTest { String body = """ - GOVERNANCE + COMPLIANCE 2030-01-01T00:00:00Z """; @@ -199,6 +199,108 @@ class S3ObjectLockIntegrationTest { @Test @Order(14) + void putGovernanceObjectForRetentionUpgrade() { + given() + .header("x-amz-object-lock-mode", "GOVERNANCE") + .header("x-amz-object-lock-retain-until-date", "2030-01-01T00:00:00Z") + .body("governance object") + .when() + .put("/" + LOCK_BUCKET + "/governance-object.txt") + .then() + .statusCode(200); + } + + @Test + @Order(15) + void governanceRetentionCanBeUpgradedToCompliance() { + String body = """ + + + COMPLIANCE + 2030-01-01T00:00:00Z + + """; + given() + .body(body) + .when() + .put("/" + LOCK_BUCKET + "/governance-object.txt?retention") + .then() + .statusCode(200); + + given() + .when() + .get("/" + LOCK_BUCKET + "/governance-object.txt?retention") + .then() + .statusCode(200) + .body(containsString("COMPLIANCE")); + } + + @Test + @Order(16) + void complianceRetentionCannotBeDowngradedToGovernance() { + String body = """ + + + GOVERNANCE + 2030-01-01T00:00:00Z + + """; + given() + .body(body) + .when() + .put("/" + LOCK_BUCKET + "/" + RETENTION_KEY + "?retention") + .then() + .statusCode(403) + .body(containsString("AccessDenied")); + + given() + .when() + .get("/" + LOCK_BUCKET + "/" + RETENTION_KEY + "?retention") + .then() + .statusCode(200) + .body(containsString("COMPLIANCE")); + } + + @Test + @Order(17) + void putObjectWithExpiredComplianceRetention() { + given() + .header("x-amz-object-lock-mode", "COMPLIANCE") + .header("x-amz-object-lock-retain-until-date", "2020-01-01T00:00:00Z") + .body("expired compliance object") + .when() + .put("/" + LOCK_BUCKET + "/expired-compliance-object.txt") + .then() + .statusCode(200); + } + + @Test + @Order(18) + void expiredComplianceRetentionCanChangeMode() { + String body = """ + + + GOVERNANCE + 2030-01-01T00:00:00Z + + """; + given() + .body(body) + .when() + .put("/" + LOCK_BUCKET + "/expired-compliance-object.txt?retention") + .then() + .statusCode(200); + + given() + .when() + .get("/" + LOCK_BUCKET + "/expired-compliance-object.txt?retention") + .then() + .statusCode(200) + .body(containsString("GOVERNANCE")); + } + + @Test + @Order(19) void nonExistentBucketReturnsNoSuchBucket() { given() .when()