fix(s3): return account owner IDs in bucket and parts listings (#4884)

* fix(s3): return account owner IDs in bucket and parts listings

* fix(s3): bind multipart uploads to bucket ownership

* fix(s3): coordinate multipart uploads with bucket deletion

* fix(s3): preserve parallel multipart operations under deletion guard

* refactor(s3): reuse default owner display name in responses

* fix(s3): serialize operations on the same multipart upload

* style(s3): order project imports before Java imports
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-10-03 14:14:33 -04:00
1 parent fa30cd7834
commit d8a76d1376
5 files changed
+663 -176

No files matched your search

@@ -0,0 +1,283 @@
package io.github.hectorvent.floci.services.s3;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.s3.model.MultipartUpload;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
class S3MultipartBucketDeletionRaceTest {
@TempDir
Path tempDir;
@ParameterizedTest
@ValueSource(booleans = {true, false})
void deletionWaitsForInitiationAndRemovesTheNewUpload(boolean inMemory) throws Exception {
BlockingService service = new BlockingService(tempDir.resolve("initiation"), inMemory);
service.createBucket("race-bucket", "us-east-1");
service.blockOwnerLookup = true;
AtomicReference<MultipartUpload> upload = new AtomicReference<>();
AtomicReference<Throwable> failure = new AtomicReference<>();
Thread initiator = new Thread(() -> {
try {
upload.set(service.initiateMultipartUpload("race-bucket", "object", "application/octet-stream"));
} catch (Throwable error) {
failure.set(error);
}
}, "multipart-initiator");
initiator.start();
assertTrue(service.entered.await(5, TimeUnit.SECONDS));
Thread deleter = new Thread(() -> {
try {
service.deleteBucket("race-bucket");
} catch (Throwable error) {
failure.set(error);
}
}, "bucket-deleter");
deleter.start();
try {
awaitWaiting(deleter);
} finally {
service.release.countDown();
}
initiator.join(5_000);
deleter.join(5_000);
assertFalse(initiator.isAlive());
assertFalse(deleter.isAlive());
assertNull(failure.get());
assertFalse(service.bucketExists("race-bucket"));
service.createBucket("race-bucket", "us-east-1");
AwsException missing = assertThrows(AwsException.class,
() -> service.getMultipartUpload("race-bucket", "object", upload.get().getUploadId()));
assertEquals("NoSuchUpload", missing.getErrorCode());
}
@ParameterizedTest
@ValueSource(booleans = {true, false})
void deletionWaitsForPartWriteBeforeCleaningItsBackingStore(boolean inMemory) throws Exception {
BlockingService service = new BlockingService(tempDir.resolve("parts"), inMemory);
service.createBucket("race-bucket", "us-east-1");
String uploadId = service.initiateMultipartUpload("race-bucket", "object", "application/octet-stream")
.getUploadId();
service.blockUploadLookup = true;
AtomicReference<Throwable> failure = new AtomicReference<>();
Thread writer = new Thread(() -> {
try {
service.uploadPart("race-bucket", "object", uploadId, 1, new byte[]{1, 2, 3});
} catch (Throwable error) {
failure.set(error);
}
}, "multipart-writer");
writer.start();
assertTrue(service.entered.await(5, TimeUnit.SECONDS));
Thread deleter = new Thread(() -> {
try {
service.deleteBucket("race-bucket");
} catch (Throwable error) {
failure.set(error);
}
}, "bucket-deleter");
deleter.start();
try {
awaitWaiting(deleter);
} finally {
service.release.countDown();
}
writer.join(5_000);
deleter.join(5_000);
assertFalse(writer.isAlive());
assertFalse(deleter.isAlive());
assertNull(failure.get());
assertFalse(service.bucketExists("race-bucket"));
}
@Test
void multipartInitiationsInTheSameBucketCanProceedConcurrently() throws Exception {
BlockingService service = new BlockingService(tempDir.resolve("parallel"), true);
service.createBucket("race-bucket", "us-east-1");
service.blockOwnerLookup = true;
AtomicReference<Throwable> failure = new AtomicReference<>();
Thread first = new Thread(() -> {
try {
service.initiateMultipartUpload("race-bucket", "first", "application/octet-stream");
} catch (Throwable error) {
failure.set(error);
}
}, "multipart-initiator");
first.start();
assertTrue(service.entered.await(5, TimeUnit.SECONDS));
AtomicReference<MultipartUpload> secondUpload = new AtomicReference<>();
Thread second = new Thread(() -> {
try {
secondUpload.set(service.initiateMultipartUpload("race-bucket", "second", "application/octet-stream"));
} catch (Throwable error) {
failure.set(error);
}
}, "parallel-initiator");
second.start();
try {
second.join(5_000);
assertFalse(second.isAlive(), "a multipart initiation should not block another on the same bucket");
assertNotNull(secondUpload.get());
} finally {
service.release.countDown();
}
first.join(5_000);
assertFalse(first.isAlive());
assertNull(failure.get());
}
@ParameterizedTest
@ValueSource(booleans = {true, false})
void abortWaitsForPartWriteOnTheSameUpload(boolean inMemory) throws Exception {
BlockingService service = new BlockingService(tempDir.resolve("abort"), inMemory);
service.createBucket("race-bucket", "us-east-1");
String uploadId = service.initiateMultipartUpload("race-bucket", "object", "application/octet-stream")
.getUploadId();
service.blockUploadLookup = true;
AtomicReference<Throwable> failure = new AtomicReference<>();
Thread writer = new Thread(() -> {
try {
service.uploadPart("race-bucket", "object", uploadId, 1, new byte[]{1, 2, 3});
} catch (Throwable error) {
failure.set(error);
}
}, "multipart-writer");
writer.start();
assertTrue(service.entered.await(5, TimeUnit.SECONDS));
Thread aborter = new Thread(() -> {
try {
service.abortMultipartUpload("race-bucket", "object", uploadId);
} catch (Throwable error) {
failure.set(error);
}
}, "multipart-aborter");
aborter.start();
try {
awaitWaiting(aborter);
} finally {
service.release.countDown();
}
writer.join(5_000);
aborter.join(5_000);
assertFalse(writer.isAlive());
assertFalse(aborter.isAlive());
assertNull(failure.get());
AwsException missing = assertThrows(AwsException.class,
() -> service.getMultipartUpload("race-bucket", "object", uploadId));
assertEquals("NoSuchUpload", missing.getErrorCode());
}
@ParameterizedTest
@ValueSource(booleans = {true, false})
void completionWaitsForPartWriteOnTheSameUpload(boolean inMemory) throws Exception {
BlockingService service = new BlockingService(tempDir.resolve("completion"), inMemory);
service.createBucket("race-bucket", "us-east-1");
String uploadId = service.initiateMultipartUpload("race-bucket", "object", "application/octet-stream")
.getUploadId();
service.blockUploadLookup = true;
AtomicReference<Throwable> failure = new AtomicReference<>();
Thread writer = new Thread(() -> {
try {
service.uploadPart("race-bucket", "object", uploadId, 1, new byte[]{1, 2, 3});
} catch (Throwable error) {
failure.set(error);
}
}, "multipart-writer");
writer.start();
assertTrue(service.entered.await(5, TimeUnit.SECONDS));
Thread completer = new Thread(() -> {
try {
service.completeMultipartUpload("race-bucket", "object", uploadId, List.of(1), null, null);
} catch (Throwable error) {
failure.set(error);
}
}, "multipart-completer");
completer.start();
try {
awaitWaiting(completer);
} finally {
service.release.countDown();
}
writer.join(5_000);
completer.join(5_000);
assertFalse(writer.isAlive());
assertFalse(completer.isAlive());
assertNull(failure.get());
assertNotNull(service.getObject("race-bucket", "object"));
}
private static void awaitWaiting(Thread thread) throws InterruptedException {
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
while (System.nanoTime() < deadline && thread.getState() != Thread.State.WAITING) {
if (!thread.isAlive()) {
break;
}
Thread.sleep(10);
}
assertEquals(Thread.State.WAITING, thread.getState(), "operation should wait for the multipart lock");
}
private static final class BlockingService extends S3Service {
private final CountDownLatch entered = new CountDownLatch(1);
private final CountDownLatch release = new CountDownLatch(1);
private volatile boolean blockOwnerLookup;
private volatile boolean blockUploadLookup;
private BlockingService(Path dataRoot, boolean inMemory) {
super(new InMemoryStorage<>(), new InMemoryStorage<>(), dataRoot, inMemory);
}
@Override
public String getBucketOwnerAccountId(String bucketName) {
String owner = super.getBucketOwnerAccountId(bucketName);
if (blockOwnerLookup && "multipart-initiator".equals(Thread.currentThread().getName())) {
pause();
}
return owner;
}
@Override
public MultipartUpload getMultipartUpload(String bucket, String key, String uploadId) {
MultipartUpload upload = super.getMultipartUpload(bucket, key, uploadId);
if (blockUploadLookup && "multipart-writer".equals(Thread.currentThread().getName())) {
pause();
}
return upload;
}
private void pause() {
entered.countDown();
try {
if (!release.await(5, TimeUnit.SECONDS)) {
throw new AssertionError("timed out waiting for the race test to release the multipart operation");
}
} catch (InterruptedException error) {
Thread.currentThread().interrupt();
throw new AssertionError("multipart operation interrupted", error);
}
}
}
}
@@ -0,0 +1,111 @@
package io.github.hectorvent.floci.services.s3;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
@QuarkusTest
class S3OwnerIdentityIntegrationTest {
private static final String ACCOUNT_ONE = "000000000011";
private static final String ACCOUNT_TWO = "000000000012";
@Test
void listBucketsOwnerMatchesTheCallerAccountAndBucketAcl() {
String bucket = "owner-identity-list-bucket";
createBucket(ACCOUNT_ONE, bucket);
given().header("Authorization", authorization(ACCOUNT_ONE))
.when().get("/")
.then().statusCode(200)
.body(containsString("<Owner><ID>" + ACCOUNT_ONE + "</ID><DisplayName>floci</DisplayName></Owner>"))
.body(containsString("<Name>" + bucket + "</Name>"));
given().header("Authorization", authorization(ACCOUNT_ONE))
.when().get("/" + bucket + "?acl")
.then().statusCode(200)
.body(containsString("<Owner><ID>" + ACCOUNT_ONE + "</ID><DisplayName>floci</DisplayName></Owner>"));
given().header("Authorization", authorization(ACCOUNT_TWO))
.when().get("/")
.then().statusCode(200)
.body(containsString("<Owner><ID>" + ACCOUNT_TWO + "</ID><DisplayName>floci</DisplayName></Owner>"))
.body(not(containsString("<Name>" + bucket + "</Name>")));
}
@Test
void listPartsOwnerAndInitiatorUseTheUploadAccount() {
String bucket = "owner-identity-parts-bucket";
createBucket(ACCOUNT_TWO, bucket);
String uploadId = given().header("Authorization", authorization(ACCOUNT_TWO))
.when().post("/" + bucket + "/part.bin?uploads")
.then().statusCode(200)
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
given().header("Authorization", authorization(ACCOUNT_TWO))
.when().get("/" + bucket + "/part.bin?uploadId=" + uploadId)
.then().statusCode(200)
.body(containsString("<Initiator><ID>" + ACCOUNT_TWO
+ "</ID><DisplayName>floci</DisplayName></Initiator>"))
.body(containsString("<Owner><ID>" + ACCOUNT_TWO
+ "</ID><DisplayName>floci</DisplayName></Owner>"));
}
@Test
void anUploadCannotBeAttributedToAnotherAccountOrSurviveBucketDeletion() {
String bucket = "owner-identity-reused-bucket";
createBucket(ACCOUNT_ONE, bucket);
createBucket(ACCOUNT_TWO, bucket);
String firstUploadId = given().header("Authorization", authorization(ACCOUNT_ONE))
.when().post("/" + bucket + "/part.bin?uploads")
.then().statusCode(200)
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
given().header("Authorization", authorization(ACCOUNT_TWO))
.when().get("/" + bucket + "/part.bin?uploadId=" + firstUploadId)
.then().statusCode(404)
.body(containsString("<Code>NoSuchUpload</Code>"));
given().header("Authorization", authorization(ACCOUNT_ONE))
.when().delete("/" + bucket)
.then().statusCode(204);
createBucket(ACCOUNT_ONE, bucket);
given().header("Authorization", authorization(ACCOUNT_ONE))
.when().get("/" + bucket + "/part.bin?uploadId=" + firstUploadId)
.then().statusCode(404)
.body(containsString("<Code>NoSuchUpload</Code>"));
given().header("Authorization", authorization(ACCOUNT_TWO))
.when().get("/" + bucket + "/part.bin?uploadId=" + firstUploadId)
.then().statusCode(404)
.body(containsString("<Code>NoSuchUpload</Code>"));
String secondUploadId = given().header("Authorization", authorization(ACCOUNT_TWO))
.when().post("/" + bucket + "/part.bin?uploads")
.then().statusCode(200)
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
given().header("Authorization", authorization(ACCOUNT_TWO))
.when().get("/" + bucket + "/part.bin?uploadId=" + secondUploadId)
.then().statusCode(200)
.body(containsString("<Owner><ID>" + ACCOUNT_TWO
+ "</ID><DisplayName>floci</DisplayName></Owner>"));
}
private static void createBucket(String account, String bucket) {
given().header("Authorization", authorization(account))
.when().put("/" + bucket)
.then().statusCode(200);
}
private static String authorization(String account) {
return "AWS4-HMAC-SHA256 Credential=" + account
+ "/20261002/us-east-1/s3/aws4_request, SignedHeaders=host, Signature=abc";
}
}