mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(s3): name delete markers in GET and HEAD errors and the key in NoSuchKey (#7149)
* fix(s3): answer a GET or HEAD that reaches a delete marker as S3 does A read of a key whose current version is a delete marker sends the marker's headers with the 404, and a versionId that names a delete marker is 405 MethodNotAllowed, as on S3. * fix(s3): name the key in NoSuchKey errors Every NoSuchKey error names the key in a Key element, and GetObjectTagging names it with its bucket, as on S3.
This commit is contained in:
1 parent
f77d032818
commit
0f94440442
6 files changed
+233
-15
No files matched your search
@@ -1137,7 +1137,8 @@ public class S3Controller {
|
||||
closeQuietly(read);
|
||||
}
|
||||
}
|
||||
} catch (AwsException e) {
|
||||
} catch (AwsException caught) {
|
||||
AwsException e = deleteMarkerRefusal(caught, versionId, "GET");
|
||||
emitCloudTrailEvent("GetObject", bucket, key, 0L, 0L, e.getErrorCode(), e.getMessage());
|
||||
if (S3Service.isWebsiteErrorDocumentTrigger(e) && isWebsiteRequest(httpHeaders, uriInfo)) {
|
||||
Response websiteError = serveWebsiteErrorResponse(bucket, authorization, e);
|
||||
@@ -1145,7 +1146,7 @@ public class S3Controller {
|
||||
return websiteError;
|
||||
}
|
||||
}
|
||||
return xmlErrorResponse(e, bucket);
|
||||
return withDeleteMarkerHeaders(xmlErrorResponse(e, bucket), e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1442,7 +1443,8 @@ public class S3Controller {
|
||||
checksumDescribesRange && checksumMode);
|
||||
emitCloudTrailEvent("HeadObject", bucket, key, 0L, obj.getSize(), null, null);
|
||||
return resp.build();
|
||||
} catch (AwsException e) {
|
||||
} catch (AwsException caught) {
|
||||
AwsException e = deleteMarkerRefusal(caught, versionId, "HEAD");
|
||||
emitCloudTrailEvent("HeadObject", bucket, key, 0L, 0L, e.getErrorCode(), e.getMessage());
|
||||
if (S3Service.isWebsiteErrorDocumentTrigger(e) && isWebsiteRequest(httpHeaders, uriInfo)) {
|
||||
Response websiteError = serveWebsiteErrorResponse(bucket, authorization, e);
|
||||
@@ -1450,7 +1452,7 @@ public class S3Controller {
|
||||
return headOnlyResponse(websiteError);
|
||||
}
|
||||
}
|
||||
return xmlErrorResponse(e, bucket);
|
||||
return withDeleteMarkerHeaders(xmlErrorResponse(e, bucket), e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3749,11 +3751,11 @@ 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, and MethodNotAllowed
|
||||
// the method and the kind of resource that refused it.
|
||||
// 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.
|
||||
// They travel through AwsException.extendedData.
|
||||
if (e.getExtendedData() != null) {
|
||||
for (String field : List.of("Method", "ResourceType", "ProposedSize", "MinSizeAllowed",
|
||||
for (String field : List.of("Key", "Method", "ResourceType", "ProposedSize", "MinSizeAllowed",
|
||||
"MaxSizeAllowed", "PartNumber", "ETag")) {
|
||||
Object value = e.getExtendedData().get(field);
|
||||
if (value != null) {
|
||||
@@ -3799,6 +3801,27 @@ public class S3Controller {
|
||||
return response.build();
|
||||
}
|
||||
|
||||
/** A GET or HEAD that names a delete marker by its version id is refused instead of answered as a missing key. */
|
||||
private static AwsException deleteMarkerRefusal(AwsException e, String versionId, String method) {
|
||||
return e instanceof S3DeleteMarkerException marker && versionId != null ? marker.named(method) : e;
|
||||
}
|
||||
|
||||
/** S3 sends the id of the delete marker a GET or HEAD reached, and for one the request named, its time. */
|
||||
private static Response withDeleteMarkerHeaders(Response response, AwsException e) {
|
||||
if (!(e instanceof S3DeleteMarkerException marker)) {
|
||||
return response;
|
||||
}
|
||||
Response.ResponseBuilder builder = Response.fromResponse(response)
|
||||
.header("x-amz-version-id", marker.versionId());
|
||||
if (e.getHttpStatus() == 405) {
|
||||
// xmlErrorResponse already sends x-amz-delete-marker for this refusal.
|
||||
return builder.header("Last-Modified", RFC_822.format(marker.lastModified()))
|
||||
.header("Allow", "DELETE")
|
||||
.build();
|
||||
}
|
||||
return builder.header("x-amz-delete-marker", "true").build();
|
||||
}
|
||||
|
||||
private Response checkPreconditions(S3Object obj, String ifMatch, String ifNoneMatch,
|
||||
String ifModifiedSince, String ifUnmodifiedSince) {
|
||||
String eTag = obj.getETag();
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.s3.model.S3Object;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Map;
|
||||
|
||||
/** A read that reached a delete marker, answered as a missing key unless the request named the marker. */
|
||||
final class S3DeleteMarkerException extends AwsException {
|
||||
|
||||
private final String versionId;
|
||||
private final Instant lastModified;
|
||||
|
||||
S3DeleteMarkerException(S3Object marker) {
|
||||
super("NoSuchKey", "The specified key does not exist.", 404, Map.of("Key", marker.getKey()));
|
||||
this.versionId = marker.getVersionId() != null ? marker.getVersionId() : "null";
|
||||
this.lastModified = marker.getLastModified();
|
||||
}
|
||||
|
||||
private S3DeleteMarkerException(S3DeleteMarkerException current, String method) {
|
||||
super("MethodNotAllowed", "The specified method is not allowed against this resource.", 405,
|
||||
Map.of("Method", method, "ResourceType", "DeleteMarker"));
|
||||
this.versionId = current.versionId;
|
||||
this.lastModified = current.lastModified;
|
||||
}
|
||||
|
||||
/** The refusal S3 gives a {@code method} request that names this delete marker by its version id. */
|
||||
S3DeleteMarkerException named(String method) {
|
||||
return new S3DeleteMarkerException(this, method);
|
||||
}
|
||||
|
||||
String versionId() {
|
||||
return versionId;
|
||||
}
|
||||
|
||||
Instant lastModified() {
|
||||
return lastModified;
|
||||
}
|
||||
}
|
||||
@@ -1926,7 +1926,7 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
.orElseThrow(() -> new AwsException(
|
||||
"NoSuchVersion", "The specified version does not exist.", 404));
|
||||
if (stored.object().isDeleteMarker()) {
|
||||
throw new AwsException("NoSuchKey", "The specified key does not exist.", 404);
|
||||
throw new S3DeleteMarkerException(stored.object());
|
||||
}
|
||||
S3Object object = copyObject(stored.object());
|
||||
boolean archived = stored.storeKey().equals(versionedKey(bucketName, key, "null"));
|
||||
@@ -2223,13 +2223,17 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
S3Object object = findStoredObject(ownedBucket, bucketName, key, versionId)
|
||||
.orElseThrow(() -> versionId != null
|
||||
? new AwsException("NoSuchVersion", "The specified version does not exist.", 404)
|
||||
: new AwsException("NoSuchKey", "The specified key does not exist.", 404));
|
||||
: noSuchKey(key));
|
||||
if (object.isDeleteMarker()) {
|
||||
throw new AwsException("NoSuchKey", "The specified key does not exist.", 404);
|
||||
throw new S3DeleteMarkerException(object);
|
||||
}
|
||||
return new AccountAwareStorageBackend.OwnedEntry<>(ownedBucket.account(), object);
|
||||
}
|
||||
|
||||
private static AwsException noSuchKey(String key) {
|
||||
return new AwsException("NoSuchKey", "The specified key does not exist.", 404, Map.of("Key", key));
|
||||
}
|
||||
|
||||
/** The stored version {@code versionId} names, the current one when it is null, delete markers included. */
|
||||
private Optional<S3Object> findStoredObject(AccountAwareStorageBackend.OwnedEntry<Bucket> ownedBucket,
|
||||
String bucketName, String key, String versionId) {
|
||||
@@ -2398,7 +2402,7 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
}
|
||||
S3Object current = objectStore.get(objectKey(bucketName, key)).orElse(null);
|
||||
if (current == null || current.isDeleteMarker()) {
|
||||
throw new AwsException("NoSuchKey", "The specified key does not exist.", 404);
|
||||
throw noSuchKey(key);
|
||||
}
|
||||
if (!eTagMatches(ifMatch, current.getETag())) {
|
||||
throw new S3PreconditionFailedException("If-Match");
|
||||
@@ -3372,7 +3376,8 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
.map(StoredVersion::object)
|
||||
.orElseThrow(() -> versionId != null
|
||||
? new AwsException("NoSuchVersion", "The specified version does not exist.", 404)
|
||||
: new AwsException("NoSuchKey", "The specified key does not exist.", 404));
|
||||
// GetObjectTagging names the key with its bucket.
|
||||
: noSuchKey("GET".equals(method) ? bucketName + "/" + key : key));
|
||||
if (object.isDeleteMarker()) {
|
||||
throw new AwsException("MethodNotAllowed",
|
||||
"The specified method is not allowed against this resource.", 405,
|
||||
@@ -3624,10 +3629,9 @@ 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(() -> new AwsException("NoSuchKey",
|
||||
"The specified key does not exist.", 404));
|
||||
.orElseThrow(() -> noSuchKey(key));
|
||||
if (object.isDeleteMarker()) {
|
||||
throw new AwsException("NoSuchKey", "The specified key does not exist.", 404);
|
||||
throw noSuchKey(key);
|
||||
}
|
||||
return object;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user