mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(s3): return composite multipart checksums and match S3 on UploadPart, GetObjectAttributes and CopyObject responses (#3093)
* fix(s3): return composite checksums for multipart uploads CompleteMultipartUpload stored the checksum of the whole object but labelled it COMPOSITE, so clients that recompute the composite value from the part checksums, as the AWS documentation describes, never matched it. Follow the AWS checksum type rules, verified against real S3: - SHA1/SHA256 are always COMPOSITE, CRC32/CRC32C default to COMPOSITE and CRC64NVME is always FULL_OBJECT. x-amz-checksum-type on CreateMultipartUpload selects FULL_OBJECT for CRC32/CRC32C; incompatible combinations fail with InvalidRequest, and the response echoes x-amz-checksum-algorithm/-type. - The composite value is the algorithm applied to the concatenated binary part checksums, Base64 encoded, with a "-<part count>" suffix. It is returned by CompleteMultipartUpload, HeadObject and GetObject (with x-amz-checksum-type), while GetObjectAttributes omits the suffix, as AWS does. - A checksum sent on CompleteMultipartUpload is validated against the stored type: a mismatching value is a BadDigest (the composite is accepted with or without its suffix), a checksum type different from the one the upload was created with is an InvalidRequest, per-part checksums in the request body that do not match the uploaded parts are an InvalidPart, and a COMPOSITE upload completed without the checksum of every part is an InvalidRequest (FULL_OBJECT uploads need none, as on S3). A non-numeric PartNumber is a MalformedXML instead of a 500. - UploadPart echoes the part checksum (x-amz-checksum-<algorithm>) when the upload declared an algorithm, as S3 does; without one no header is returned. - CopyObject writes a single object without a part manifest; a composite source gets a full-object checksum recomputed with its algorithm, and CopyObjectResult carries the checksum elements, matching AWS. - GetObjectAttributes lists part-level checksums only for COMPOSITE objects: a FULL_OBJECT multipart object reports its PartsCount alone and a single-part object has no ObjectParts element. The algorithm and type rules live in two new enums, ChecksumAlgorithm and ChecksumType, parsed once from the request headers; S3Checksum builds the full-object, composite and GetObjectAttributes views of a checksum. The boto3 compatibility suite gains multipart upload_file cases for SHA256 and for the SDK's default CRC32, asserting the composite value and the part list. The S3 docs drop the stale note that checksum responses focus on SHA-1 and SHA-256 (CRC32, CRC32C and CRC64NVME have been supported since #877) and list the AWS algorithms Floci rejects. * fix(s3): return the GetObjectAttributes ETag without quotes S3 returns the ETag element of GetObjectAttributes without the surrounding quotes that HeadObject, ListParts and CompleteMultipartUpload keep. Verified against real S3 with the same object: head_object gives "af3c...-3" while get_object_attributes gives af3c...-3, and the AWS CLI reference shows the same in its get-object-attributes example. Floci echoed the quoted value, so a client comparing both saw different strings on Floci and equal ones nowhere. * fix(s3): give a copied object its own ETag CopyObject kept the ETag of the source, so the copy of a multipart object carried the "<md5-of-md5s>-N" value of its parts. S3 writes a copy as a single object and returns the MD5 of the whole content as its ETag, in CopyObjectResult and HeadObject alike (verified against real S3: source "af3c...-3", copy "a5f04f..."). The copy now keeps the ETag storeObject computes. * fix(s3): send CopyObjectResult as application/xml The CopyObject response carried Content-Type: text/plain;charset=UTF-8 for an XML body. S3 labels it application/xml like every other XML response. * test(s3): cover multipart checksums in every SDK compatibility suite Each SDK implements flexible checksums differently, so the multipart flows are exercised per client rather than once: the Go transfer manager (what terraform-provider-aws uses), @aws-sdk/lib-storage for Node, the Java SDK v2 both low-level and through S3TransferManager on the multipart-enabled async client, the AWS CLI (s3api, aws s3 cp --checksum-algorithm and a plain aws s3 cp that goes multipart on its own) and boto3. Every case asserts the composite or full-object value computed in the test itself, the UploadPart echo, the suffix-less GetObjectAttributes checksum and unquoted ETag, the rejected Complete without part checksums, and the copy of a multipart object as a single full object with its own ETag. The default algorithm cases accept whatever the SDK version declares (CRC32 composite or full object, or none) and check the stored value against it. * test(terraform): upload a large aws_s3_object with Terraform and OpenTofu The IaC suites created buckets but never an object, so the code path that surfaced the composite checksum bug, an aws_s3_object above the provider's part size uploaded through the SDK transfer manager, was not covered. Both suites now upload a 12 MiB file generated by the test setup with checksum_algorithm SHA256 and check that HeadObject reports a COMPOSITE checksum equal to the composite computed locally over the part sizes GetObjectAttributes lists, that the state attribute holds the same value, and that re-planning the object shows no drift. compat-opentofu moves to the same aws-cli image as compat-terraform (2.34.32): 2.15 predates the S3 checksum type and cannot show ChecksumType in its output. * fix(s3): reject x-amz-checksum-type without a checksum algorithm CreateMultipartUpload only parsed x-amz-checksum-type when an algorithm was declared, so a type sent on its own, valid or not, was silently ignored. S3 validates the header regardless: an unknown value is InvalidRequest "Value for x-amz-checksum-type header is invalid." and a valid one without an algorithm is InvalidRequest "The x-amz-checksum-type header can only be used with the x-amz-checksum-algorithm header." (both verified against real S3). * test(s3): log cleanup failures in the multipart checksum compat tests The Java, Node and Go cases swallowed errors while deleting their objects and bucket. They now report what could not be deleted and why, so a leftover is diagnosable instead of silent. --------- Co-authored-by: Mihai Blidaru <mihai.blidaru@lynxfctech.com>
This commit is contained in:
1 parent
ebdf092cce
commit
724be0a6a7
28 files changed
+2678
-129
No files matched your search
@@ -13,6 +13,8 @@ import io.github.hectorvent.floci.core.common.RequestContext;
|
||||
import io.github.hectorvent.floci.services.cloudtrail.CloudTrailService;
|
||||
import io.github.hectorvent.floci.services.sns.SnsQueryHandler;
|
||||
import io.github.hectorvent.floci.services.s3.model.Bucket;
|
||||
import io.github.hectorvent.floci.services.s3.model.ChecksumAlgorithm;
|
||||
import io.github.hectorvent.floci.services.s3.model.ChecksumType;
|
||||
import io.github.hectorvent.floci.services.s3.model.GetObjectAttributesParts;
|
||||
import io.github.hectorvent.floci.services.s3.model.GetObjectAttributesResult;
|
||||
import io.github.hectorvent.floci.services.s3.model.LambdaNotification;
|
||||
@@ -48,6 +50,7 @@ import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -711,11 +714,16 @@ public class S3Controller {
|
||||
}
|
||||
byte[] partData = decodeAwsChunked(body, contentEncoding, contentSha256);
|
||||
validateChecksumHeaders(httpHeaders, partData, getChecksumAlgorithm(httpHeaders));
|
||||
String eTag = s3Service.uploadPart(bucket, key, uploadId, partNumber, partData,
|
||||
Part part = s3Service.storePart(bucket, key, uploadId, partNumber, partData,
|
||||
httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-algorithm"),
|
||||
httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-key"),
|
||||
httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-key-MD5"));
|
||||
Response.ResponseBuilder response = Response.ok().header("ETag", eTag);
|
||||
Response.ResponseBuilder response = Response.ok().header("ETag", part.getETag());
|
||||
// S3 echoes the part checksum only when the upload declared an algorithm.
|
||||
ChecksumAlgorithm declared = s3Service.getMultipartUpload(bucket, key, uploadId).getChecksumAlgorithm();
|
||||
if (declared != null) {
|
||||
response.header("x-amz-checksum-" + declared.wireValue(), part.getChecksum().valueFor(declared));
|
||||
}
|
||||
appendSseCustomerHeaders(response,
|
||||
httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-algorithm"),
|
||||
httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-key-MD5"));
|
||||
@@ -1266,6 +1274,7 @@ public class S3Controller {
|
||||
httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-key"),
|
||||
httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-key-MD5"),
|
||||
getChecksumAlgorithm(httpHeaders),
|
||||
httpHeaders.getHeaderString("x-amz-checksum-type"),
|
||||
parseInlineTaggingHeader(httpHeaders.getHeaderString("x-amz-tagging")));
|
||||
String xml = new XmlBuilder()
|
||||
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
|
||||
@@ -1276,6 +1285,10 @@ public class S3Controller {
|
||||
.end("InitiateMultipartUploadResult")
|
||||
.build();
|
||||
Response.ResponseBuilder response = Response.ok(xml);
|
||||
if (upload.getChecksumAlgorithm() != null) {
|
||||
response.header("x-amz-checksum-algorithm", upload.getChecksumAlgorithm().name());
|
||||
response.header("x-amz-checksum-type", upload.getChecksumType().name());
|
||||
}
|
||||
appendSseCustomerHeaders(response, upload);
|
||||
return response.build();
|
||||
}
|
||||
@@ -1297,7 +1310,8 @@ public class S3Controller {
|
||||
|
||||
if (uploadId != null) {
|
||||
s3Service.authorizeObjectWrite(bucket, key, "s3:PutObject", authorization);
|
||||
List<Integer> partNumbers = parseCompleteMultipartBody(new String(body));
|
||||
List<CompletedPart> completedParts = parseCompleteMultipartBody(new String(body, StandardCharsets.UTF_8));
|
||||
List<Integer> partNumbers = completedParts.stream().map(CompletedPart::partNumber).toList();
|
||||
Response preconditionResponse = checkWritePreconditions(bucket, key, ifMatch, ifNoneMatch);
|
||||
if (preconditionResponse != null) {
|
||||
return preconditionResponse;
|
||||
@@ -1305,7 +1319,7 @@ public class S3Controller {
|
||||
String checksumType = httpHeaders.getHeaderString("x-amz-checksum-type");
|
||||
S3Checksum expectedChecksum = extractChecksumFromHeaders(httpHeaders);
|
||||
S3Object obj = s3Service.completeMultipartUpload(bucket, key, uploadId, partNumbers,
|
||||
checksumType, expectedChecksum);
|
||||
completedPartChecksums(completedParts), checksumType, expectedChecksum);
|
||||
String baseUrl = uriInfo.getBaseUri().toString();
|
||||
if (baseUrl.endsWith("/")) {
|
||||
baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
|
||||
@@ -1317,6 +1331,7 @@ public class S3Controller {
|
||||
.elem("Bucket", bucket)
|
||||
.elem("Key", key)
|
||||
.elem("ETag", obj.getETag());
|
||||
appendChecksumElements(xmlBuilder, obj.getChecksum());
|
||||
if (obj.getVersionId() != null) {
|
||||
xmlBuilder.elem("VersionId", obj.getVersionId());
|
||||
}
|
||||
@@ -1468,13 +1483,42 @@ public class S3Controller {
|
||||
return Response.ok(xml.build()).build();
|
||||
}
|
||||
|
||||
private List<Integer> parseCompleteMultipartBody(String xml) {
|
||||
List<String> parts = XmlParser.extractAll(xml, "PartNumber");
|
||||
private record CompletedPart(int partNumber, S3Checksum checksum) {}
|
||||
|
||||
private List<CompletedPart> parseCompleteMultipartBody(String xml) {
|
||||
List<Map<String, String>> parts = XmlParser.extractGroups(xml, "Part");
|
||||
if (parts.isEmpty()) {
|
||||
throw new AwsException("MalformedXML",
|
||||
"The XML you provided was not well-formed.", 400);
|
||||
throw malformedCompleteMultipartXml();
|
||||
}
|
||||
return parts.stream().map(Integer::parseInt).toList();
|
||||
List<CompletedPart> completedParts = new ArrayList<>();
|
||||
for (Map<String, String> part : parts) {
|
||||
int partNumber;
|
||||
try {
|
||||
partNumber = Integer.parseInt(part.getOrDefault("PartNumber", "").trim());
|
||||
} catch (NumberFormatException e) {
|
||||
throw malformedCompleteMultipartXml();
|
||||
}
|
||||
S3Checksum checksum = new S3Checksum();
|
||||
for (ChecksumAlgorithm algorithm : ChecksumAlgorithm.values()) {
|
||||
checksum.setValueFor(algorithm, part.get("Checksum" + algorithm.name()));
|
||||
}
|
||||
completedParts.add(new CompletedPart(partNumber, checksum.hasAnyValue() ? checksum : null));
|
||||
}
|
||||
return completedParts;
|
||||
}
|
||||
|
||||
private static AwsException malformedCompleteMultipartXml() {
|
||||
return new AwsException("MalformedXML", "The XML you provided was not well-formed.", 400);
|
||||
}
|
||||
|
||||
private Map<Integer, S3Checksum> completedPartChecksums(List<CompletedPart> parts) {
|
||||
Map<Integer, S3Checksum> checksums = new HashMap<>();
|
||||
for (CompletedPart part : parts) {
|
||||
if (part.checksum() != null) {
|
||||
checksums.put(part.partNumber(), part.checksum());
|
||||
}
|
||||
}
|
||||
return checksums;
|
||||
}
|
||||
|
||||
// --- Versioning Operations ---
|
||||
@@ -2183,18 +2227,23 @@ public class S3Controller {
|
||||
.withGrantFullControl(httpHeaders.getHeaderString("x-amz-grant-full-control"))
|
||||
.withGrantReadAcp(httpHeaders.getHeaderString("x-amz-grant-read-acp"))
|
||||
.withGrantWriteAcp(httpHeaders.getHeaderString("x-amz-grant-write-acp")));
|
||||
String xml = new XmlBuilder()
|
||||
XmlBuilder xmlBuilder = new XmlBuilder()
|
||||
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
|
||||
.start("CopyObjectResult", AwsNamespaces.S3)
|
||||
.elem("LastModified", ISO_FORMAT.format(copy.getLastModified()))
|
||||
.elem("ETag", copy.getETag())
|
||||
.end("CopyObjectResult")
|
||||
.build();
|
||||
Response.ResponseBuilder response = Response.ok(xml);
|
||||
.elem("ETag", copy.getETag());
|
||||
appendChecksumElements(xmlBuilder, copy.getChecksum());
|
||||
String xml = xmlBuilder.end("CopyObjectResult").build();
|
||||
Response.ResponseBuilder response = Response.ok(xml).type(MediaType.APPLICATION_XML);
|
||||
appendSseCustomerHeaders(response, copy);
|
||||
return response.build();
|
||||
}
|
||||
|
||||
// GetObjectAttributes is the one S3 response whose ETag comes without the surrounding quotes.
|
||||
private static String unquoted(String eTag) {
|
||||
return eTag == null ? null : eTag.replace("\"", "");
|
||||
}
|
||||
|
||||
private Response handleUploadPartCopy(String copySource, String destBucket, String destKey,
|
||||
String uploadId, int partNumber, HttpHeaders httpHeaders) {
|
||||
CopySourceRef sourceObject = parseCopySource(copySource);
|
||||
@@ -2242,7 +2291,7 @@ public class S3Controller {
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
|
||||
.start("GetObjectAttributesResponse", AwsNamespaces.S3)
|
||||
.elem("ETag", result.getETag());
|
||||
.elem("ETag", unquoted(result.getETag()));
|
||||
appendChecksum(xml, result.getChecksum());
|
||||
appendObjectParts(xml, result.getObjectParts());
|
||||
if (result.getStorageClass() != null) {
|
||||
@@ -2267,22 +2316,33 @@ public class S3Controller {
|
||||
if (checksum == null || !checksum.hasAnyValue()) {
|
||||
return;
|
||||
}
|
||||
xml.start("Checksum")
|
||||
.elem("ChecksumCRC32", checksum.getChecksumCRC32())
|
||||
xml.start("Checksum");
|
||||
appendChecksumElements(xml, checksum);
|
||||
xml.end("Checksum");
|
||||
}
|
||||
|
||||
private void appendChecksumElements(XmlBuilder xml, S3Checksum checksum) {
|
||||
if (checksum == null || !checksum.hasAnyValue()) {
|
||||
return;
|
||||
}
|
||||
xml.elem("ChecksumCRC32", checksum.getChecksumCRC32())
|
||||
.elem("ChecksumCRC32C", checksum.getChecksumCRC32C())
|
||||
.elem("ChecksumCRC64NVME", checksum.getChecksumCRC64NVME())
|
||||
.elem("ChecksumSHA1", checksum.getChecksumSHA1())
|
||||
.elem("ChecksumSHA256", checksum.getChecksumSHA256())
|
||||
.elem("ChecksumType", checksum.getChecksumType())
|
||||
.end("Checksum");
|
||||
.elem("ChecksumType", checksum.getChecksumType() != null ? checksum.getChecksumType().name() : null);
|
||||
}
|
||||
|
||||
private void appendObjectParts(XmlBuilder xml, GetObjectAttributesParts objectParts) {
|
||||
if (objectParts == null) {
|
||||
return;
|
||||
}
|
||||
xml.start("ObjectParts")
|
||||
.elem("IsTruncated", objectParts.isTruncated())
|
||||
xml.start("ObjectParts");
|
||||
if (!objectParts.isPartChecksumsAvailable()) {
|
||||
xml.elem("PartsCount", objectParts.getPartsCount()).end("ObjectParts");
|
||||
return;
|
||||
}
|
||||
xml.elem("IsTruncated", objectParts.isTruncated())
|
||||
.elem("MaxParts", objectParts.getMaxParts())
|
||||
.elem("NextPartNumberMarker", objectParts.getNextPartNumberMarker())
|
||||
.elem("PartNumberMarker", objectParts.getPartNumberMarker());
|
||||
@@ -2320,6 +2380,9 @@ public class S3Controller {
|
||||
if (checksum.getChecksumSHA256() != null) {
|
||||
resp.header("x-amz-checksum-sha256", checksum.getChecksumSHA256());
|
||||
}
|
||||
if (checksum.getChecksumType() != null && checksum.hasAnyValue()) {
|
||||
resp.header("x-amz-checksum-type", checksum.getChecksumType().name());
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> extractUserMetadata(HttpHeaders httpHeaders) {
|
||||
@@ -2352,7 +2415,7 @@ public class S3Controller {
|
||||
checksum.setChecksumCRC64NVME(crc64nvme);
|
||||
checksum.setChecksumSHA1(sha1);
|
||||
checksum.setChecksumSHA256(sha256);
|
||||
checksum.setChecksumType("FULL_OBJECT");
|
||||
checksum.setChecksumType(ChecksumType.FULL_OBJECT);
|
||||
return checksum;
|
||||
}
|
||||
|
||||
|
||||
@@ -457,10 +457,10 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
object.getMetadata().putAll(metadata);
|
||||
}
|
||||
object.setStorageClass(ObjectAttributeName.normalizeStorageClass(effectiveOptions.getStorageClass()));
|
||||
String validatedChecksumAlgorithm = validateAndNormalizeChecksumAlgorithm(effectiveOptions.getChecksumAlgorithm());
|
||||
ChecksumAlgorithm validatedChecksumAlgorithm = ChecksumAlgorithm.fromWireValue(effectiveOptions.getChecksumAlgorithm());
|
||||
S3Checksum resolvedChecksum = checksum != null ? copyChecksum(checksum)
|
||||
: effectiveOptions.getClientChecksum() != null ? copyChecksum(effectiveOptions.getClientChecksum())
|
||||
: buildChecksum(data, parts, false, validatedChecksumAlgorithm);
|
||||
: S3Checksum.fullObject(validatedChecksumAlgorithm, data);
|
||||
object.setChecksum(resolvedChecksum);
|
||||
object.setParts(copyParts(parts));
|
||||
object.setContentEncoding(effectiveOptions.getContentEncoding());
|
||||
@@ -1058,9 +1058,9 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
result.setObjectSize(object.getSize());
|
||||
}
|
||||
if (attributes.contains(ObjectAttributeName.CHECKSUM)) {
|
||||
result.setChecksum(copyChecksum(object.getChecksum()));
|
||||
result.setChecksum(object.getChecksum() == null ? null : object.getChecksum().forObjectAttributes());
|
||||
}
|
||||
if (attributes.contains(ObjectAttributeName.OBJECT_PARTS)) {
|
||||
if (attributes.contains(ObjectAttributeName.OBJECT_PARTS) && object.getParts() != null && !object.getParts().isEmpty()) {
|
||||
result.setObjectParts(buildObjectParts(object, maxParts, partNumberMarker));
|
||||
}
|
||||
|
||||
@@ -1081,9 +1081,16 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
return object;
|
||||
}
|
||||
|
||||
// AWS lists part-level checksums only for composite objects; a full-object multipart object
|
||||
// reports its part count alone.
|
||||
private GetObjectAttributesParts buildObjectParts(S3Object object, Integer maxParts, Integer partNumberMarker) {
|
||||
List<Part> sortedParts = new ArrayList<>(copyParts(object.getParts()));
|
||||
sortedParts.sort(Comparator.comparingInt(Part::getPartNumber));
|
||||
if (object.getChecksum() == null || object.getChecksum().getChecksumType() != ChecksumType.COMPOSITE) {
|
||||
GetObjectAttributesParts countOnly = new GetObjectAttributesParts();
|
||||
countOnly.setPartsCount(sortedParts.size());
|
||||
return countOnly;
|
||||
}
|
||||
|
||||
int max = (maxParts == null || maxParts <= 0) ? 1000 : maxParts;
|
||||
int marker = Math.max(partNumberMarker != null ? partNumberMarker : 0, 0);
|
||||
@@ -1094,6 +1101,7 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
List<Part> returnedParts = visibleParts.stream().limit(max).toList();
|
||||
|
||||
GetObjectAttributesParts result = new GetObjectAttributesParts();
|
||||
result.setPartChecksumsAvailable(true);
|
||||
result.setMaxParts(max);
|
||||
result.setPartNumberMarker(marker);
|
||||
result.setParts(returnedParts);
|
||||
@@ -2139,6 +2147,17 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
String contentDisposition, String serverSideEncryption, String acl,
|
||||
String sseCustomerAlgorithm, String sseCustomerKey, String sseCustomerKeyMd5,
|
||||
String checksumAlgorithm, Map<String, String> tagging) {
|
||||
return initiateMultipartUpload(bucket, key, contentType, metadata, storageClass, contentDisposition,
|
||||
serverSideEncryption, acl, sseCustomerAlgorithm, sseCustomerKey, sseCustomerKeyMd5,
|
||||
checksumAlgorithm, null, tagging);
|
||||
}
|
||||
|
||||
public MultipartUpload initiateMultipartUpload(String bucket, String key, String contentType,
|
||||
Map<String, String> metadata, String storageClass,
|
||||
String contentDisposition, String serverSideEncryption, String acl,
|
||||
String sseCustomerAlgorithm, String sseCustomerKey, String sseCustomerKeyMd5,
|
||||
String checksumAlgorithm, String checksumType,
|
||||
Map<String, String> tagging) {
|
||||
ensureBucketExists(bucket);
|
||||
if (acl != null && !acl.isBlank()) {
|
||||
cannedObjectAclXml(acl);
|
||||
@@ -2158,7 +2177,14 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
upload.setSseCustomerKeyMd5(customerKey.keyMd5());
|
||||
}
|
||||
upload.setAcl(acl);
|
||||
upload.setChecksumAlgorithm(validateAndNormalizeChecksumAlgorithm(checksumAlgorithm));
|
||||
ChecksumAlgorithm algorithm = ChecksumAlgorithm.fromWireValue(checksumAlgorithm);
|
||||
ChecksumType requestedChecksumType = ChecksumType.fromWireValue(checksumType);
|
||||
if (requestedChecksumType != null && algorithm == null) {
|
||||
throw new AwsException("InvalidRequest",
|
||||
"The x-amz-checksum-type header can only be used with the x-amz-checksum-algorithm header.", 400);
|
||||
}
|
||||
upload.setChecksumAlgorithm(algorithm);
|
||||
upload.setChecksumType(algorithm == null ? null : algorithm.multipartType(requestedChecksumType));
|
||||
if (tagging != null && !tagging.isEmpty()) {
|
||||
upload.setTagging(new HashMap<>(tagging));
|
||||
}
|
||||
@@ -2184,6 +2210,13 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
|
||||
public String uploadPart(String bucket, String key, String uploadId, int partNumber, byte[] data,
|
||||
String sseCustomerAlgorithm, String sseCustomerKey, String sseCustomerKeyMd5) {
|
||||
return storePart(bucket, key, uploadId, partNumber, data, sseCustomerAlgorithm, sseCustomerKey, sseCustomerKeyMd5)
|
||||
.getETag();
|
||||
}
|
||||
|
||||
/** Stores one part and returns it, with the ETag and the checksum the upload's algorithm gives it. */
|
||||
public Part storePart(String bucket, String key, String uploadId, int partNumber, byte[] data,
|
||||
String sseCustomerAlgorithm, String sseCustomerKey, String sseCustomerKeyMd5) {
|
||||
MultipartUpload upload = multipartUploads.get(uploadId);
|
||||
if (upload == null || !upload.getBucket().equals(bucket) || !upload.getKey().equals(key)) {
|
||||
throw new AwsException("NoSuchUpload",
|
||||
@@ -2208,10 +2241,10 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
|
||||
String eTag = computeETag(data);
|
||||
Part part = new Part(partNumber, eTag, data.length);
|
||||
part.setChecksum(buildChecksum(data, List.of(part), true, upload.getChecksumAlgorithm()));
|
||||
part.setChecksum(S3Checksum.of(upload.getChecksumAlgorithm(), data));
|
||||
upload.getParts().put(partNumber, part);
|
||||
LOG.debugv("Uploaded part {0} for upload {1} ({2} bytes)", partNumber, uploadId, data.length);
|
||||
return eTag;
|
||||
return part;
|
||||
}
|
||||
|
||||
public String uploadPartCopy(String destBucket, String destKey, String uploadId, int partNumber,
|
||||
@@ -2251,20 +2284,33 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
|
||||
public S3Object completeMultipartUpload(String bucket, String key, String uploadId, List<Integer> partNumbers,
|
||||
String checksumType, S3Checksum expectedChecksum) {
|
||||
return completeMultipartUpload(bucket, key, uploadId, partNumbers, Map.of(), checksumType, expectedChecksum);
|
||||
}
|
||||
|
||||
public S3Object completeMultipartUpload(String bucket, String key, String uploadId, List<Integer> partNumbers,
|
||||
Map<Integer, S3Checksum> partChecksums,
|
||||
String checksumType, S3Checksum expectedChecksum) {
|
||||
MultipartUpload upload = multipartUploads.get(uploadId);
|
||||
if (upload == null || !upload.getBucket().equals(bucket) || !upload.getKey().equals(key)) {
|
||||
throw new AwsException("NoSuchUpload",
|
||||
"The specified multipart upload does not exist.", 404);
|
||||
}
|
||||
|
||||
// Verify all requested parts exist
|
||||
ChecksumAlgorithm algorithm = upload.getChecksumAlgorithm() != null ? upload.getChecksumAlgorithm() : ChecksumAlgorithm.CRC64NVME;
|
||||
ChecksumType storedChecksumType = upload.getChecksumType() != null ? upload.getChecksumType() : ChecksumType.FULL_OBJECT;
|
||||
|
||||
// Verify all requested parts exist and carry the checksums the upload requires
|
||||
for (int num : partNumbers) {
|
||||
if (!upload.getParts().containsKey(num)) {
|
||||
Part part = upload.getParts().get(num);
|
||||
if (part == null) {
|
||||
throw new AwsException("InvalidPart",
|
||||
"One or more of the specified parts could not be found. Part " + num + " is missing.", 400);
|
||||
}
|
||||
validatePartChecksum(upload.getChecksumAlgorithm(), storedChecksumType, num, part, partChecksums.get(num));
|
||||
}
|
||||
|
||||
validateCompleteChecksumType(algorithm, storedChecksumType, ChecksumType.fromWireValue(checksumType));
|
||||
|
||||
// Concatenate parts in order
|
||||
try {
|
||||
ByteArrayOutputStream combined = new ByteArrayOutputStream();
|
||||
@@ -2281,21 +2327,18 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
|
||||
byte[] allData = combined.toByteArray();
|
||||
|
||||
boolean fullObjectChecksumRequested = "FULL_OBJECT".equalsIgnoreCase(checksumType)
|
||||
&& expectedChecksum != null && expectedChecksum.hasAnyValue();
|
||||
if (fullObjectChecksumRequested) {
|
||||
validateFullObjectChecksum(allData, expectedChecksum);
|
||||
}
|
||||
|
||||
// Composite ETag: MD5 of concatenated part MD5s, suffixed with part count
|
||||
String compositeETag = "\"" + bytesToHex(md.digest()) + "-" + partNumbers.size() + "\"";
|
||||
|
||||
List<Part> completedParts = partNumbers.stream()
|
||||
.map(num -> copyPart(upload.getParts().get(num)))
|
||||
.toList();
|
||||
S3Checksum checksum = buildChecksum(allData, completedParts, true, upload.getChecksumAlgorithm());
|
||||
if (fullObjectChecksumRequested) {
|
||||
checksum.setChecksumType("FULL_OBJECT");
|
||||
S3Checksum checksum = storedChecksumType == ChecksumType.COMPOSITE
|
||||
? S3Checksum.composite(algorithm, completedParts.stream()
|
||||
.map(part -> part.getChecksum().valueFor(algorithm)).toList())
|
||||
: S3Checksum.fullObject(algorithm, allData);
|
||||
if (expectedChecksum != null && expectedChecksum.hasAnyValue()) {
|
||||
validateExpectedChecksum(checksum, expectedChecksum);
|
||||
}
|
||||
S3Object object = storeObject(bucket, key, allData, upload.getContentType(), upload.getMetadata(),
|
||||
checksum, completedParts,
|
||||
@@ -2344,6 +2387,10 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
}
|
||||
|
||||
public MultipartUpload listParts(String bucket, String key, String uploadId) {
|
||||
return getMultipartUpload(bucket, key, uploadId);
|
||||
}
|
||||
|
||||
public MultipartUpload getMultipartUpload(String bucket, String key, String uploadId) {
|
||||
MultipartUpload upload = multipartUploads.get(uploadId);
|
||||
if (upload == null || !upload.getBucket().equals(bucket) || !upload.getKey().equals(key)) {
|
||||
throw new AwsException("NoSuchUpload",
|
||||
@@ -3350,57 +3397,62 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
public static String validateAndNormalizeChecksumAlgorithm(String algorithm) {
|
||||
if (algorithm == null || algorithm.isBlank()) {
|
||||
return null;
|
||||
private static void validateCompleteChecksumType(ChecksumAlgorithm algorithm, ChecksumType storedType,
|
||||
ChecksumType requestedType) {
|
||||
if (requestedType == null) {
|
||||
return;
|
||||
}
|
||||
String normalized = algorithm.trim().toUpperCase(java.util.Locale.ROOT);
|
||||
if (normalized.equals("CRC32") || normalized.equals("CRC32C") || normalized.equals("SHA1") || normalized.equals("SHA256") || normalized.equals("CRC64NVME")) {
|
||||
return normalized;
|
||||
}
|
||||
if (normalized.equals("SHA512") || normalized.equals("MD5") || normalized.equals("XXHASH3") || normalized.equals("XXHASH64") || normalized.equals("XXHASH128")) {
|
||||
throw new AwsException("InvalidRequest", "The checksum algorithm you specified is a valid AWS checksum algorithm, but is not currently supported by Floci (supported: CRC32, CRC32C, CRC64NVME, SHA1, SHA256).", 400);
|
||||
}
|
||||
throw new AwsException("InvalidArgument", "The checksum algorithm you specified is not supported.", 400);
|
||||
}
|
||||
|
||||
private static void validateFullObjectChecksum(byte[] data, S3Checksum expected) {
|
||||
if (expected.getChecksumSHA1() != null || expected.getChecksumSHA256() != null) {
|
||||
if (requestedType == ChecksumType.FULL_OBJECT && !algorithm.supports(ChecksumType.FULL_OBJECT)) {
|
||||
throw new AwsException("InvalidRequest",
|
||||
"The FULL_OBJECT checksum type is not supported with the SHA1 or SHA256 checksum algorithm. "
|
||||
+ "Full object checksums are only supported with the CRC32, CRC32C, and CRC64NVME checksum algorithms.",
|
||||
400);
|
||||
"The algorithm type you specified in x-amz-checksum- header is invalid.", 400);
|
||||
}
|
||||
if (expected.getChecksumCRC32() != null && !expected.getChecksumCRC32().equals(S3Checksum.crc32Base64(data))) {
|
||||
throw new AwsException("BadDigest", "The CRC32 checksum you specified did not match the payload.", 400);
|
||||
}
|
||||
if (expected.getChecksumCRC32C() != null && !expected.getChecksumCRC32C().equals(S3Checksum.crc32cBase64(data))) {
|
||||
throw new AwsException("BadDigest", "The CRC32C checksum you specified did not match the payload.", 400);
|
||||
}
|
||||
if (expected.getChecksumCRC64NVME() != null
|
||||
&& !expected.getChecksumCRC64NVME().equals(S3Checksum.crc64NvmeBase64(data))) {
|
||||
throw new AwsException("BadDigest", "The CRC64NVME checksum you specified did not match the payload.", 400);
|
||||
if (requestedType != storedType) {
|
||||
throw new AwsException("InvalidRequest",
|
||||
"The upload was created using the " + storedType + " checksum mode. "
|
||||
+ "The complete request must use the same checksum mode.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static S3Checksum buildChecksum(byte[] data, List<Part> parts, boolean multipartUpload) {
|
||||
return buildChecksum(data, parts, multipartUpload, null);
|
||||
// As on S3: a COMPOSITE upload needs the checksum of every part in the request body, a checksum for
|
||||
// another algorithm is a BadDigest, a wrong value an InvalidPart. FULL_OBJECT uploads need none.
|
||||
private static void validatePartChecksum(ChecksumAlgorithm declared, ChecksumType storedType, int partNumber,
|
||||
Part uploaded, S3Checksum expected) {
|
||||
if (expected == null || !expected.hasAnyValue()) {
|
||||
if (declared != null && storedType == ChecksumType.COMPOSITE) {
|
||||
throw new AwsException("InvalidRequest", "The upload was created using a " + declared.wireValue()
|
||||
+ " checksum. The complete request must include the checksum for each part. It was missing for part "
|
||||
+ partNumber + " in the request.", 400);
|
||||
}
|
||||
return;
|
||||
}
|
||||
ChecksumAlgorithm algorithm = declared != null && expected.valueFor(declared) != null ? declared : expected.algorithm();
|
||||
if (declared != null && algorithm != declared) {
|
||||
throw new AwsException("BadDigest", "The " + algorithm.wireValue() + " you specified for part " + partNumber
|
||||
+ " did not match what we received.", 400);
|
||||
}
|
||||
if (!expected.valueFor(algorithm).equals(uploaded.getChecksum().valueFor(algorithm))) {
|
||||
throw new AwsException("InvalidPart",
|
||||
"One or more of the specified parts could not be found. The part may not have been uploaded, "
|
||||
+ "or the specified entity tag may not match the part's entity tag.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static S3Checksum buildChecksum(byte[] data, List<Part> parts, boolean multipartUpload, String algorithm) {
|
||||
S3Checksum checksum = new S3Checksum();
|
||||
String algo = (algorithm != null) ? algorithm.toUpperCase() : "CRC64NVME";
|
||||
switch (algo) {
|
||||
case "CRC32" -> checksum.setChecksumCRC32(S3Checksum.crc32Base64(data));
|
||||
case "CRC32C" -> checksum.setChecksumCRC32C(S3Checksum.crc32cBase64(data));
|
||||
case "SHA1" -> checksum.setChecksumSHA1(S3Checksum.sha1Base64(data));
|
||||
case "SHA256" -> checksum.setChecksumSHA256(S3Checksum.sha256Base64(data));
|
||||
default -> checksum.setChecksumCRC64NVME(S3Checksum.crc64NvmeBase64(data));
|
||||
// AWS accepts a composite value with or without its "-N" suffix, but a wrong suffix is a BadDigest.
|
||||
private static void validateExpectedChecksum(S3Checksum computed, S3Checksum expected) {
|
||||
for (ChecksumAlgorithm algorithm : ChecksumAlgorithm.values()) {
|
||||
String expectedValue = expected.valueFor(algorithm);
|
||||
if (expectedValue == null) {
|
||||
continue;
|
||||
}
|
||||
String computedValue = computed.valueFor(algorithm);
|
||||
boolean matches = expectedValue.equals(computedValue)
|
||||
|| (computed.getChecksumType() == ChecksumType.COMPOSITE
|
||||
&& expectedValue.equals(S3Checksum.withoutPartCount(computedValue)));
|
||||
if (!matches) {
|
||||
throw new AwsException("BadDigest", "The " + algorithm.wireValue()
|
||||
+ " you specified did not match the calculated checksum.", 400);
|
||||
}
|
||||
}
|
||||
checksum.setChecksumType(multipartUpload || (parts != null && parts.size() > 1)
|
||||
? "COMPOSITE"
|
||||
: "FULL_OBJECT");
|
||||
return checksum;
|
||||
}
|
||||
|
||||
private static S3Object copyObject(S3Object source) {
|
||||
@@ -3435,17 +3487,7 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
}
|
||||
|
||||
private static S3Checksum copyChecksum(S3Checksum source) {
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
S3Checksum copy = new S3Checksum();
|
||||
copy.setChecksumCRC32(source.getChecksumCRC32());
|
||||
copy.setChecksumCRC32C(source.getChecksumCRC32C());
|
||||
copy.setChecksumCRC64NVME(source.getChecksumCRC64NVME());
|
||||
copy.setChecksumSHA1(source.getChecksumSHA1());
|
||||
copy.setChecksumSHA256(source.getChecksumSHA256());
|
||||
copy.setChecksumType(source.getChecksumType());
|
||||
return copy;
|
||||
return source == null ? null : source.copy();
|
||||
}
|
||||
|
||||
private static List<Part> copyParts(List<Part> sourceParts) {
|
||||
@@ -3860,14 +3902,20 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
? effectiveOptions.getReplacementTagging()
|
||||
: source.getTags();
|
||||
|
||||
// A copy is written as one object without a part manifest; a composite source gets a
|
||||
// full-object checksum recomputed with its algorithm, as AWS does.
|
||||
S3Checksum effectiveChecksum = source.getChecksum();
|
||||
String copyChecksumAlgorithm = validateAndNormalizeChecksumAlgorithm(effectiveOptions.getChecksumAlgorithm());
|
||||
ChecksumAlgorithm copyChecksumAlgorithm = ChecksumAlgorithm.fromWireValue(effectiveOptions.getChecksumAlgorithm());
|
||||
if (copyChecksumAlgorithm == null && effectiveChecksum != null
|
||||
&& effectiveChecksum.getChecksumType() == ChecksumType.COMPOSITE) {
|
||||
copyChecksumAlgorithm = effectiveChecksum.algorithm();
|
||||
}
|
||||
if (copyChecksumAlgorithm != null) {
|
||||
effectiveChecksum = null;
|
||||
}
|
||||
|
||||
S3Object copy = storeObject(destBucket, destKey, source.getData(), effectiveContentType, metadata,
|
||||
effectiveChecksum, copyChecksumAlgorithm != null ? null : source.getParts(),
|
||||
effectiveChecksum, null,
|
||||
new PutObjectOptions()
|
||||
.withStorageClass(effectiveStorageClass)
|
||||
.withContentEncoding(effectiveContentEncoding)
|
||||
@@ -3883,9 +3931,10 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
.withGrantFullControl(effectiveOptions.getGrantFullControl())
|
||||
.withGrantReadAcp(effectiveOptions.getGrantReadAcp())
|
||||
.withGrantWriteAcp(effectiveOptions.getGrantWriteAcp())
|
||||
.withChecksumAlgorithm(copyChecksumAlgorithm)
|
||||
.withChecksumAlgorithm(copyChecksumAlgorithm != null ? copyChecksumAlgorithm.name() : null)
|
||||
.withTagging(effectiveTags));
|
||||
copy.setETag(source.getETag());
|
||||
// A copy is written as one object, so it keeps the ETag storeObject computed (the MD5 of the
|
||||
// whole content) instead of the source's, which for a multipart source ends in "-N". As on S3.
|
||||
LOG.debugv("Copied object: {0}/{1} -> {2}/{3}", sourceBucket, sourceKey, destBucket, destKey);
|
||||
fireNotifications(destBucket, destKey, "ObjectCreated:Copy", copy);
|
||||
return copy;
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
package io.github.hectorvent.floci.services.s3.model;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Checksum algorithms Floci supports, with the multipart checksum types AWS allows for each one:
|
||||
* SHA1 and SHA256 are always {@code COMPOSITE}, CRC64NVME is always {@code FULL_OBJECT}, and
|
||||
* CRC32 and CRC32C accept both, defaulting to {@code COMPOSITE}.
|
||||
*/
|
||||
@RegisterForReflection
|
||||
public enum ChecksumAlgorithm {
|
||||
CRC32(Set.of(ChecksumType.COMPOSITE, ChecksumType.FULL_OBJECT)),
|
||||
CRC32C(Set.of(ChecksumType.COMPOSITE, ChecksumType.FULL_OBJECT)),
|
||||
CRC64NVME(Set.of(ChecksumType.FULL_OBJECT)),
|
||||
SHA1(Set.of(ChecksumType.COMPOSITE)),
|
||||
SHA256(Set.of(ChecksumType.COMPOSITE));
|
||||
|
||||
/** Algorithms AWS accepts but Floci does not implement yet. */
|
||||
private static final Set<String> KNOWN_UNSUPPORTED = Set.of("SHA512", "MD5", "XXHASH3", "XXHASH64", "XXHASH128");
|
||||
|
||||
private final Set<ChecksumType> multipartTypes;
|
||||
|
||||
ChecksumAlgorithm(Set<ChecksumType> multipartTypes) {
|
||||
this.multipartTypes = multipartTypes;
|
||||
}
|
||||
|
||||
/** Lowercase name, as AWS spells the algorithm in error messages. */
|
||||
public String wireValue() {
|
||||
return name().toLowerCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
public boolean supports(ChecksumType type) {
|
||||
return multipartTypes.contains(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* The checksum type a multipart upload with this algorithm ends up with: the requested one when
|
||||
* the algorithm allows it, otherwise the default (COMPOSITE where supported, else FULL_OBJECT).
|
||||
*/
|
||||
public ChecksumType multipartType(ChecksumType requested) {
|
||||
if (requested == null) {
|
||||
return supports(ChecksumType.COMPOSITE) ? ChecksumType.COMPOSITE : ChecksumType.FULL_OBJECT;
|
||||
}
|
||||
if (!supports(requested)) {
|
||||
throw new AwsException("InvalidRequest", "The " + requested + " checksum type cannot be used with the "
|
||||
+ wireValue() + " checksum algorithm.", 400);
|
||||
}
|
||||
return requested;
|
||||
}
|
||||
|
||||
/** Base64 checksum of {@code data} with this algorithm. */
|
||||
public String compute(byte[] data) {
|
||||
return switch (this) {
|
||||
case CRC32 -> S3Checksum.crc32Base64(data);
|
||||
case CRC32C -> S3Checksum.crc32cBase64(data);
|
||||
case CRC64NVME -> S3Checksum.crc64NvmeBase64(data);
|
||||
case SHA1 -> S3Checksum.sha1Base64(data);
|
||||
case SHA256 -> S3Checksum.sha256Base64(data);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Composite checksum of a multipart object: this algorithm applied to the concatenated binary
|
||||
* part checksums, Base64 encoded and suffixed with the part count (for example {@code ...=-3}).
|
||||
*/
|
||||
public String composite(List<String> partChecksums) {
|
||||
ByteArrayOutputStream concatenated = new ByteArrayOutputStream();
|
||||
for (String partChecksum : partChecksums) {
|
||||
concatenated.writeBytes(Base64.getDecoder().decode(partChecksum));
|
||||
}
|
||||
return compute(concatenated.toByteArray()) + "-" + partChecksums.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an {@code x-amz-checksum-algorithm} value. Returns {@code null} when the header is absent.
|
||||
*/
|
||||
public static ChecksumAlgorithm fromWireValue(String value) {
|
||||
if (value == null || value.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
String normalized = value.trim().toUpperCase(Locale.ROOT);
|
||||
for (ChecksumAlgorithm algorithm : values()) {
|
||||
if (algorithm.name().equals(normalized)) {
|
||||
return algorithm;
|
||||
}
|
||||
}
|
||||
if (KNOWN_UNSUPPORTED.contains(normalized)) {
|
||||
throw new AwsException("InvalidRequest", "The checksum algorithm you specified is a valid AWS checksum "
|
||||
+ "algorithm, but is not currently supported by Floci (supported: CRC32, CRC32C, CRC64NVME, SHA1, SHA256).", 400);
|
||||
}
|
||||
throw new AwsException("InvalidArgument", "The checksum algorithm you specified is not supported.", 400);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package io.github.hectorvent.floci.services.s3.model;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
/** The two ways S3 derives an object checksum from a multipart upload. */
|
||||
@RegisterForReflection
|
||||
public enum ChecksumType {
|
||||
/** The algorithm applied to the concatenated part checksums, suffixed with the part count. */
|
||||
COMPOSITE,
|
||||
/** The algorithm applied to the whole object content. */
|
||||
FULL_OBJECT;
|
||||
|
||||
/**
|
||||
* Parses an {@code x-amz-checksum-type} value, case-insensitively like S3. Returns {@code null} when the
|
||||
* header is absent; an unknown value is the {@code InvalidRequest} S3 returns for it.
|
||||
*/
|
||||
public static ChecksumType fromWireValue(String value) {
|
||||
if (value == null || value.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
String normalized = value.trim().toUpperCase(Locale.ROOT);
|
||||
for (ChecksumType type : values()) {
|
||||
if (type.name().equals(normalized)) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
throw new AwsException("InvalidRequest", "Value for x-amz-checksum-type header is invalid.", 400);
|
||||
}
|
||||
}
|
||||
+4
@@ -13,6 +13,7 @@ public class GetObjectAttributesParts {
|
||||
private int nextPartNumberMarker;
|
||||
private int partNumberMarker;
|
||||
private int partsCount;
|
||||
private boolean partChecksumsAvailable;
|
||||
private List<Part> parts = new ArrayList<>();
|
||||
|
||||
public boolean isTruncated() { return isTruncated; }
|
||||
@@ -30,6 +31,9 @@ public class GetObjectAttributesParts {
|
||||
public int getPartsCount() { return partsCount; }
|
||||
public void setPartsCount(int partsCount) { this.partsCount = partsCount; }
|
||||
|
||||
public boolean isPartChecksumsAvailable() { return partChecksumsAvailable; }
|
||||
public void setPartChecksumsAvailable(boolean partChecksumsAvailable) { this.partChecksumsAvailable = partChecksumsAvailable; }
|
||||
|
||||
public List<Part> getParts() { return parts; }
|
||||
public void setParts(List<Part> parts) { this.parts = parts; }
|
||||
}
|
||||
@@ -21,7 +21,8 @@ public class MultipartUpload {
|
||||
private String sseCustomerAlgorithm;
|
||||
private String sseCustomerKeyMd5;
|
||||
private String acl;
|
||||
private String checksumAlgorithm;
|
||||
private ChecksumAlgorithm checksumAlgorithm;
|
||||
private ChecksumType checksumType;
|
||||
private Map<String, String> metadata;
|
||||
private Map<String, String> tagging;
|
||||
private Instant initiated;
|
||||
@@ -72,8 +73,11 @@ public class MultipartUpload {
|
||||
public String getAcl() { return acl; }
|
||||
public void setAcl(String acl) { this.acl = acl; }
|
||||
|
||||
public String getChecksumAlgorithm() { return checksumAlgorithm; }
|
||||
public void setChecksumAlgorithm(String checksumAlgorithm) { this.checksumAlgorithm = checksumAlgorithm; }
|
||||
public ChecksumAlgorithm getChecksumAlgorithm() { return checksumAlgorithm; }
|
||||
public void setChecksumAlgorithm(ChecksumAlgorithm checksumAlgorithm) { this.checksumAlgorithm = checksumAlgorithm; }
|
||||
|
||||
public ChecksumType getChecksumType() { return checksumType; }
|
||||
public void setChecksumType(ChecksumType checksumType) { this.checksumType = checksumType; }
|
||||
|
||||
public Map<String, String> getMetadata() { return metadata; }
|
||||
public void setMetadata(Map<String, String> metadata) { this.metadata = metadata; }
|
||||
|
||||
@@ -5,12 +5,16 @@ import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.zip.CRC32;
|
||||
import java.util.zip.CRC32C;
|
||||
|
||||
@RegisterForReflection
|
||||
public class S3Checksum {
|
||||
|
||||
private static final Pattern PART_COUNT_SUFFIX = Pattern.compile("-\\d+$");
|
||||
|
||||
private static final long CRC64_NVME_POLY = 0x9a6c9329ac4bc9b5L;
|
||||
private static final long[] CRC64_TABLE = buildCrc64Table();
|
||||
|
||||
@@ -19,7 +23,7 @@ public class S3Checksum {
|
||||
private String checksumCRC64NVME;
|
||||
private String checksumSHA1;
|
||||
private String checksumSHA256;
|
||||
private String checksumType;
|
||||
private ChecksumType checksumType;
|
||||
|
||||
public String getChecksumCRC32() { return checksumCRC32; }
|
||||
public void setChecksumCRC32(String checksumCRC32) { this.checksumCRC32 = checksumCRC32; }
|
||||
@@ -36,14 +40,90 @@ public class S3Checksum {
|
||||
public String getChecksumSHA256() { return checksumSHA256; }
|
||||
public void setChecksumSHA256(String checksumSHA256) { this.checksumSHA256 = checksumSHA256; }
|
||||
|
||||
public String getChecksumType() { return checksumType; }
|
||||
public void setChecksumType(String checksumType) { this.checksumType = checksumType; }
|
||||
public ChecksumType getChecksumType() { return checksumType; }
|
||||
public void setChecksumType(ChecksumType checksumType) { this.checksumType = checksumType; }
|
||||
|
||||
public boolean hasAnyValue() {
|
||||
return checksumCRC32 != null || checksumCRC32C != null || checksumCRC64NVME != null
|
||||
|| checksumSHA1 != null || checksumSHA256 != null;
|
||||
}
|
||||
|
||||
public String valueFor(ChecksumAlgorithm algorithm) {
|
||||
return switch (algorithm) {
|
||||
case CRC32 -> checksumCRC32;
|
||||
case CRC32C -> checksumCRC32C;
|
||||
case CRC64NVME -> checksumCRC64NVME;
|
||||
case SHA1 -> checksumSHA1;
|
||||
case SHA256 -> checksumSHA256;
|
||||
};
|
||||
}
|
||||
|
||||
public void setValueFor(ChecksumAlgorithm algorithm, String value) {
|
||||
switch (algorithm) {
|
||||
case CRC32 -> checksumCRC32 = value;
|
||||
case CRC32C -> checksumCRC32C = value;
|
||||
case CRC64NVME -> checksumCRC64NVME = value;
|
||||
case SHA1 -> checksumSHA1 = value;
|
||||
case SHA256 -> checksumSHA256 = value;
|
||||
}
|
||||
}
|
||||
|
||||
/** The algorithm whose value is set, or {@code null} when the checksum is empty. */
|
||||
public ChecksumAlgorithm algorithm() {
|
||||
for (ChecksumAlgorithm algorithm : ChecksumAlgorithm.values()) {
|
||||
if (valueFor(algorithm) != null) {
|
||||
return algorithm;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Checksum of {@code data} with no type, as stored for a part. S3 uses CRC64NVME when no algorithm was declared. */
|
||||
public static S3Checksum of(ChecksumAlgorithm algorithm, byte[] data) {
|
||||
ChecksumAlgorithm effective = algorithm != null ? algorithm : ChecksumAlgorithm.CRC64NVME;
|
||||
S3Checksum checksum = new S3Checksum();
|
||||
checksum.setValueFor(effective, effective.compute(data));
|
||||
return checksum;
|
||||
}
|
||||
|
||||
public static S3Checksum fullObject(ChecksumAlgorithm algorithm, byte[] data) {
|
||||
S3Checksum checksum = of(algorithm, data);
|
||||
checksum.setChecksumType(ChecksumType.FULL_OBJECT);
|
||||
return checksum;
|
||||
}
|
||||
|
||||
public static S3Checksum composite(ChecksumAlgorithm algorithm, List<String> partChecksums) {
|
||||
S3Checksum checksum = new S3Checksum();
|
||||
checksum.setValueFor(algorithm, algorithm.composite(partChecksums));
|
||||
checksum.setChecksumType(ChecksumType.COMPOSITE);
|
||||
return checksum;
|
||||
}
|
||||
|
||||
public S3Checksum copy() {
|
||||
S3Checksum copy = new S3Checksum();
|
||||
copy.checksumCRC32 = checksumCRC32;
|
||||
copy.checksumCRC32C = checksumCRC32C;
|
||||
copy.checksumCRC64NVME = checksumCRC64NVME;
|
||||
copy.checksumSHA1 = checksumSHA1;
|
||||
copy.checksumSHA256 = checksumSHA256;
|
||||
copy.checksumType = checksumType;
|
||||
return copy;
|
||||
}
|
||||
|
||||
/** The checksum as GetObjectAttributes reports it: a composite value loses the {@code -N} suffix HeadObject carries. */
|
||||
public S3Checksum forObjectAttributes() {
|
||||
S3Checksum copy = copy();
|
||||
if (copy.checksumType == ChecksumType.COMPOSITE) {
|
||||
ChecksumAlgorithm algorithm = copy.algorithm();
|
||||
copy.setValueFor(algorithm, withoutPartCount(copy.valueFor(algorithm)));
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
|
||||
public static String withoutPartCount(String checksum) {
|
||||
return checksum == null ? null : PART_COUNT_SUFFIX.matcher(checksum).replaceFirst("");
|
||||
}
|
||||
|
||||
public static String crc32Base64(byte[] data) {
|
||||
CRC32 crc = new CRC32();
|
||||
crc.update(data);
|
||||
|
||||
@@ -71,7 +71,7 @@ public class S3Object {
|
||||
this.storageClass = "STANDARD";
|
||||
this.checksum = new S3Checksum();
|
||||
this.checksum.setChecksumSHA256(S3Checksum.sha256Base64(data));
|
||||
this.checksum.setChecksumType("FULL_OBJECT");
|
||||
this.checksum.setChecksumType(ChecksumType.FULL_OBJECT);
|
||||
this.parts = new ArrayList<>();
|
||||
this.tags = new HashMap<>();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user