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 1f8aa97929..9d630da5c6 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 @@ -949,6 +949,10 @@ public class S3Service { Bucket bucket = bucketStore.get(bucketName) .orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404)); + if (!bucket.isObjectLockEnabled()) { + throw new AwsException("ObjectLockConfigurationNotFoundError", + "Object Lock configuration does not exist for this bucket", 404); + } return bucket.getDefaultRetention(); } 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 new file mode 100644 index 0000000000..3914981602 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectLockIntegrationTest.java @@ -0,0 +1,137 @@ +package io.github.hectorvent.floci.services.s3; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.*; + +@QuarkusTest +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class S3ObjectLockIntegrationTest { + + private static final String PLAIN_BUCKET = "object-lock-plain"; + private static final String NO_LOCK_BUCKET = "object-lock-disabled"; + private static final String LOCK_BUCKET = "object-lock-enabled"; + + // --- plain bucket (no object-lock header) --- + + @Test + @Order(1) + void createPlainBucket() { + given().when().put("/" + PLAIN_BUCKET).then().statusCode(200); + } + + @Test + @Order(2) + void plainBucketReturnsObjectLockConfigurationNotFoundError() { + given() + .when() + .get("/" + PLAIN_BUCKET + "?object-lock") + .then() + .statusCode(404) + .body(containsString("ObjectLockConfigurationNotFoundError")) + .body(containsString("Object Lock configuration does not exist for this bucket")); + } + + // --- bucket created with x-amz-bucket-object-lock-enabled: false --- + + @Test + @Order(3) + void createBucketWithObjectLockExplicitlyDisabled() { + given() + .header("x-amz-bucket-object-lock-enabled", "false") + .when() + .put("/" + NO_LOCK_BUCKET) + .then() + .statusCode(200); + } + + @Test + @Order(4) + void explicitlyDisabledBucketReturnsObjectLockConfigurationNotFoundError() { + given() + .when() + .get("/" + NO_LOCK_BUCKET + "?object-lock") + .then() + .statusCode(404) + .body(containsString("ObjectLockConfigurationNotFoundError")); + } + + // --- bucket created with x-amz-bucket-object-lock-enabled: true --- + + @Test + @Order(5) + void createBucketWithObjectLockEnabled() { + given() + .header("x-amz-bucket-object-lock-enabled", "true") + .when() + .put("/" + LOCK_BUCKET) + .then() + .statusCode(200); + } + + @Test + @Order(6) + void lockEnabledBucketReturnsConfigWithNoRule() { + given() + .when() + .get("/" + LOCK_BUCKET + "?object-lock") + .then() + .statusCode(200) + .body(containsString("Enabled")) + .body(not(containsString(""))); + } + + @Test + @Order(7) + void putObjectLockConfigurationWithRetentionRule() { + String body = """ + + + Enabled + + + COMPLIANCE + 30 + + + + """; + given() + .body(body) + .when() + .put("/" + LOCK_BUCKET + "?object-lock") + .then() + .statusCode(200); + } + + @Test + @Order(8) + void getObjectLockConfigurationReturnsRetentionRule() { + given() + .when() + .get("/" + LOCK_BUCKET + "?object-lock") + .then() + .statusCode(200) + .body(containsString("Enabled")) + .body(containsString("COMPLIANCE")) + .body(containsString("30")); + } + + // --- non-existent bucket --- + + @Test + @Order(9) + void nonExistentBucketReturnsNoSuchBucket() { + given() + .when() + .get("/does-not-exist-object-lock-test?object-lock") + .then() + .statusCode(404) + .body(containsString("NoSuchBucket")); + } +}