From 48b19f76f38ea03f9e74437749538f59d8bd22d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TATSUNO=20=E2=80=9CTaz=E2=80=9D=20Yasuhiro?= Date: Sun, 1 Nov 2026 09:53:31 +0900 Subject: [PATCH] fix(s3): name the key and version in NoSuchVersion errors (#7155) * fix(s3): name the key and version in NoSuchVersion errors Every NoSuchVersion error names the key and the version id, and an annotation request for a missing version answers NoSuchVersion instead of NoSuchKey, as on S3. * test(s3): check annotation put and delete and a missing key for NoSuchVersion --- .../floci/services/s3/S3Controller.java | 7 ++- .../floci/services/s3/S3Service.java | 21 +++---- .../s3/S3NoSuchVersionIntegrationTest.java | 57 +++++++++++++++++++ .../services/s3/S3ServiceAnnotationsTest.java | 2 +- .../s3/S3VersioningIntegrationTest.java | 3 +- 5 files changed, 74 insertions(+), 16 deletions(-) create mode 100644 src/test/java/io/github/hectorvent/floci/services/s3/S3NoSuchVersionIntegrationTest.java diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java index 6dcbc64c6d..4145729d6b 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 @@ -3752,11 +3752,12 @@ public class S3Controller { // S3 InvalidArgument responses carry ArgumentName and ArgumentValue so the SDK can // surface which input was rejected, and EntityTooSmall and EntityTooLarge the proposed and // allowed sizes. Multipart size errors also identify the rejected part, MethodNotAllowed - // the method and the kind of resource that refused it, and NoSuchKey the key. + // the method and the kind of resource that refused it, and NoSuchKey and NoSuchVersion + // the key and version that are missing. // They travel through AwsException.extendedData. if (e.getExtendedData() != null) { - for (String field : List.of("Key", "Method", "ResourceType", "ProposedSize", "MinSizeAllowed", - "MaxSizeAllowed", "PartNumber", "ETag")) { + for (String field : List.of("Key", "VersionId", "Method", "ResourceType", "ProposedSize", + "MinSizeAllowed", "MaxSizeAllowed", "PartNumber", "ETag")) { Object value = e.getExtendedData().get(field); if (value != null) { xmlBuilder.elem(field, value.toString()); 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 784bfcbda6..3dd28ff7a7 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 @@ -1923,8 +1923,7 @@ public class S3Service implements Resettable, ResourceProvider { String accountId = ownedBucket.account(); for (int attempt = 0; attempt < 10_000; attempt++) { StoredVersion stored = findStoredVersionForAccount(accountId, bucketName, key, "null") - .orElseThrow(() -> new AwsException( - "NoSuchVersion", "The specified version does not exist.", 404)); + .orElseThrow(() -> noSuchVersion(key, "null")); if (stored.object().isDeleteMarker()) { throw new S3DeleteMarkerException(stored.object()); } @@ -2221,9 +2220,7 @@ public class S3Service implements Resettable, ResourceProvider { "The specified bucket does not exist.", 404)); S3Object object = findStoredObject(ownedBucket, bucketName, key, versionId) - .orElseThrow(() -> versionId != null - ? new AwsException("NoSuchVersion", "The specified version does not exist.", 404) - : noSuchKey(key)); + .orElseThrow(() -> versionId != null ? noSuchVersion(key, versionId) : noSuchKey(key)); if (object.isDeleteMarker()) { throw new S3DeleteMarkerException(object); } @@ -2234,6 +2231,11 @@ public class S3Service implements Resettable, ResourceProvider { return new AwsException("NoSuchKey", "The specified key does not exist.", 404, Map.of("Key", key)); } + private static AwsException noSuchVersion(String key, String versionId) { + return new AwsException("NoSuchVersion", "The specified version does not exist.", 404, + Map.of("Key", key, "VersionId", versionId)); + } + /** The stored version {@code versionId} names, the current one when it is null, delete markers included. */ private Optional findStoredObject(AccountAwareStorageBackend.OwnedEntry ownedBucket, String bucketName, String key, String versionId) { @@ -3372,12 +3374,11 @@ public class S3Service implements Resettable, ResourceProvider { AccountAwareStorageBackend.OwnedEntry ownedBucket = resolveBucketEntry(bucketName) .orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404)); + // GetObjectTagging names the key with its bucket. + String namedKey = "GET".equals(method) ? bucketName + "/" + key : key; S3Object object = findStoredVersionForAccount(ownedBucket.account(), bucketName, key, versionId) .map(StoredVersion::object) - .orElseThrow(() -> versionId != null - ? new AwsException("NoSuchVersion", "The specified version does not exist.", 404) - // GetObjectTagging names the key with its bucket. - : noSuchKey("GET".equals(method) ? bucketName + "/" + key : key)); + .orElseThrow(() -> versionId != null ? noSuchVersion(namedKey, versionId) : noSuchKey(namedKey)); if (object.isDeleteMarker()) { throw new AwsException("MethodNotAllowed", "The specified method is not allowed against this resource.", 405, @@ -3629,7 +3630,7 @@ public class S3Service implements Resettable, ResourceProvider { private S3Object resolveParentObject(String bucketName, String key, String versionId) { S3Object object = findStoredVersion(bucketName, key, versionId) .map(StoredVersion::object) - .orElseThrow(() -> noSuchKey(key)); + .orElseThrow(() -> versionId != null ? noSuchVersion(key, versionId) : noSuchKey(key)); if (object.isDeleteMarker()) { throw noSuchKey(key); } diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3NoSuchVersionIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3NoSuchVersionIntegrationTest.java new file mode 100644 index 0000000000..7b2fed631b --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3NoSuchVersionIntegrationTest.java @@ -0,0 +1,57 @@ +package io.github.hectorvent.floci.services.s3; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.Response; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; + +@QuarkusTest +class S3NoSuchVersionIntegrationTest { + private static final String BUCKET = "no-such-version-names-it"; + private static final String KEY = "dir/k.txt"; + private static final String PATH = "/" + BUCKET + "/" + KEY; + + @Test + void noSuchVersionNamesTheKeyAndTheVersion() { + given().when().put("/" + BUCKET).then().statusCode(200); + given().body("Enabled") + .when().put("/" + BUCKET + "?versioning").then().statusCode(200); + String gone = given().body("1").when().put(PATH).then().statusCode(200).extract().header("x-amz-version-id"); + given().body("2").when().put(PATH).then().statusCode(200); + given().when().delete(PATH + "?versionId=" + gone).then().statusCode(204); + + for (String key : List.of(KEY, "missing")) { + String path = "/" + BUCKET + "/" + key; + for (String versionId : List.of(gone, "null")) { + String version = "versionId=" + versionId; + missing(given().when().get(path + "?" + version), key, versionId); + missing(given().when().get(path + "?acl&" + version), key, versionId); + missing(given().header("x-amz-object-attributes", "ETag").when().get(path + "?attributes&" + version), + key, versionId); + missing(given().body("").when().put(path + "?tagging&" + version), + key, versionId); + missing(given().when().delete(path + "?tagging&" + version), key, versionId); + missing(given().when().get(path + "?annotation&annotationName=labels&" + version), key, versionId); + missing(given().when().get(path + "?annotation&" + version), key, versionId); + missing(given().body("x").when().put(path + "?annotation&annotationName=labels&" + version), + key, versionId); + missing(given().when().delete(path + "?annotation&annotationName=labels&" + version), key, versionId); + missing(given().header("x-amz-copy-source", path + "?" + version).when().put("/" + BUCKET + "/copy"), + key, versionId); + // GetObjectTagging names the key with its bucket. + missing(given().when().get(path + "?tagging&" + version), BUCKET + "/" + key, versionId); + } + } + } + + private static void missing(Response response, String key, String versionId) { + response.then().statusCode(404) + .body("Error.Code", equalTo("NoSuchVersion")) + .body("Error.Key", equalTo(key)) + .body("Error.VersionId", equalTo(versionId)); + } +} 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.java index aa479900ad..2b65367122 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 @@ -234,7 +234,7 @@ class S3ServiceAnnotationsTest { putAnnotation("version-bound", "payload"); String versionId = s3Service.getObject(BUCKET, "docs/readme.txt", null).getVersionId(); s3Service.deleteObject(BUCKET, "docs/readme.txt", versionId); - assertThrowsAws("NoSuchKey", () -> + assertThrowsAws("NoSuchVersion", () -> s3Service.getObjectAnnotation(BUCKET, "docs/readme.txt", "version-bound", versionId)); } diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningIntegrationTest.java index 096cc8a794..572185c93a 100644 --- a/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningIntegrationTest.java @@ -627,8 +627,7 @@ class S3VersioningIntegrationTest { given().queryParam("versionId", "null").header("x-amz-acl", "private") .when().put(added + "?acl").then().statusCode(404).body(noSuchVersion); given().queryParam("versionId", "null").queryParam("annotationName", "labels").body("x") - .when().put(added + "?annotation").then().statusCode(404) - .body(containsString("NoSuchKey")); + .when().put(added + "?annotation").then().statusCode(404).body(noSuchVersion); given().header("x-amz-copy-source", "/" + bucket + "/new?versionId=null") .when().put("/" + bucket + "/copy-of-new").then().statusCode(404).body(noSuchVersion); given().when().get(added).then().statusCode(200).body(equalTo("new"));