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:
Mihai BlidaruandMihai Blidaru authored and GitHub committed 2026-09-05 15:12:13 -04:00
1 parent ebdf092cce
commit 724be0a6a7
28 files changed
+2678 -129

No files matched your search

@@ -0,0 +1,142 @@
package io.github.hectorvent.floci.services.s3;
import io.github.hectorvent.floci.core.common.AwsException;
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.S3Checksum;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
/**
* Composite vectors were captured from real Amazon S3 (eu-west-1, September 2026): the part checksums are
* what UploadPart returned and the composite values are what CompleteMultipartUpload and HeadObject reported.
*/
class S3ChecksumTest {
@Test
void compositeSha256MatchesAws() {
assertEquals("rVWtNbadtsaKXJAcgWPWjifHzJaXde/iV4jaJAOdivc=-3", ChecksumAlgorithm.SHA256.composite(List.of(
"lcKa6Boe7a167tUCN5C4OaFygGSYD8MBnBARTrdL+1w=",
"gHjhbUT4oo2tSCtSZJdlKi+jeNXJT+VImqdPXsXvkOc=",
"4l9PQ3zOPZ0oGFvknFX+J4zZwE4WMzO+l3r8XrbWOeM=")));
}
@Test
void compositeOfSinglePartStillCarriesPartCount() {
assertEquals("D0xEU2q/FgypQljU/eaDWTSRcnDG3KQGOtevJWcmMRY=-1",
ChecksumAlgorithm.SHA256.composite(List.of("n7gWa0Gp88JMie9iljKcv731WbPhWtFVibhxsVB8FAw=")));
}
@Test
void compositeSha1MatchesAws() {
assertEquals("CBB1y8xhBCot1HoCMQCx18NR2tU=-2", ChecksumAlgorithm.SHA1.composite(List.of(
"1/0x5kkt8+9IA/j+B6LiWtFTdrY=", "H2o2aM+t5OzS5CuqgWtQ90pWCpo=")));
}
@Test
void compositeCrc32MatchesAws() {
assertEquals("R/nORQ==-2", ChecksumAlgorithm.CRC32.composite(List.of("W0QpDQ==", "anCi7Q==")));
}
@Test
void compositeCrc32cMatchesAws() {
assertEquals("ROC8NA==-2", ChecksumAlgorithm.CRC32C.composite(List.of("wMKxiw==", "myWdrQ==")));
}
@Test
void withoutPartCountStripsOnlyTheSuffix() {
assertEquals("rVWtNbadtsaKXJAcgWPWjifHzJaXde/iV4jaJAOdivc=",
S3Checksum.withoutPartCount("rVWtNbadtsaKXJAcgWPWjifHzJaXde/iV4jaJAOdivc=-3"));
assertEquals("+SDIDA==", S3Checksum.withoutPartCount("+SDIDA=="));
assertNull(S3Checksum.withoutPartCount(null));
}
@Test
void computeDispatchesOnAlgorithm() {
byte[] data = "hello".getBytes(StandardCharsets.UTF_8);
assertEquals(S3Checksum.sha256Base64(data), ChecksumAlgorithm.SHA256.compute(data));
assertEquals(S3Checksum.sha1Base64(data), ChecksumAlgorithm.SHA1.compute(data));
assertEquals(S3Checksum.crc32Base64(data), ChecksumAlgorithm.CRC32.compute(data));
assertEquals(S3Checksum.crc32cBase64(data), ChecksumAlgorithm.CRC32C.compute(data));
assertEquals(S3Checksum.crc64NvmeBase64(data), ChecksumAlgorithm.CRC64NVME.compute(data));
}
@Test
void objectWithoutDeclaredAlgorithmGetsCrc64Nvme() {
byte[] data = "hello".getBytes(StandardCharsets.UTF_8);
S3Checksum checksum = S3Checksum.fullObject(null, data);
assertEquals(ChecksumAlgorithm.CRC64NVME, checksum.algorithm());
assertEquals(S3Checksum.crc64NvmeBase64(data), checksum.getChecksumCRC64NVME());
assertEquals(ChecksumType.FULL_OBJECT, checksum.getChecksumType());
}
@Test
void objectAttributesViewDropsTheSuffixOfCompositeValuesOnly() {
S3Checksum composite = S3Checksum.composite(ChecksumAlgorithm.CRC32, List.of("W0QpDQ==", "anCi7Q=="));
assertEquals("R/nORQ==-2", composite.getChecksumCRC32());
assertEquals("R/nORQ==", composite.forObjectAttributes().getChecksumCRC32());
assertEquals("R/nORQ==-2", composite.getChecksumCRC32(), "the stored checksum is left untouched");
S3Checksum fullObject = S3Checksum.fullObject(ChecksumAlgorithm.CRC32, new byte[0]);
assertEquals(fullObject.getChecksumCRC32(), fullObject.forObjectAttributes().getChecksumCRC32());
}
@Test
void valueAccessorsFollowTheAlgorithm() {
S3Checksum checksum = new S3Checksum();
assertNull(checksum.algorithm());
checksum.setValueFor(ChecksumAlgorithm.CRC32C, "wMKxiw==");
assertEquals(ChecksumAlgorithm.CRC32C, checksum.algorithm());
assertEquals("wMKxiw==", checksum.getChecksumCRC32C());
assertEquals("wMKxiw==", checksum.valueFor(ChecksumAlgorithm.CRC32C));
assertNull(checksum.valueFor(ChecksumAlgorithm.SHA256));
}
@Test
void algorithmHeaderParsingIsCaseInsensitiveAndRejectsUnknownValues() {
assertEquals(ChecksumAlgorithm.SHA256, ChecksumAlgorithm.fromWireValue("sha256"));
assertEquals(ChecksumAlgorithm.CRC64NVME, ChecksumAlgorithm.fromWireValue(" CRC64NVME "));
assertNull(ChecksumAlgorithm.fromWireValue(null));
assertNull(ChecksumAlgorithm.fromWireValue(" "));
AwsException knownButUnsupported = assertThrows(AwsException.class, () -> ChecksumAlgorithm.fromWireValue("SHA512"));
assertEquals("InvalidRequest", knownButUnsupported.getErrorCode());
AwsException unknown = assertThrows(AwsException.class, () -> ChecksumAlgorithm.fromWireValue("MURMUR3"));
assertEquals("InvalidArgument", unknown.getErrorCode());
}
@Test
void multipartTypeFollowsTheAwsAlgorithmTable() {
assertEquals(ChecksumType.COMPOSITE, ChecksumAlgorithm.SHA256.multipartType(null));
assertEquals(ChecksumType.COMPOSITE, ChecksumAlgorithm.SHA1.multipartType(null));
assertEquals(ChecksumType.COMPOSITE, ChecksumAlgorithm.CRC32.multipartType(null));
assertEquals(ChecksumType.COMPOSITE, ChecksumAlgorithm.CRC32C.multipartType(null));
assertEquals(ChecksumType.FULL_OBJECT, ChecksumAlgorithm.CRC64NVME.multipartType(null));
assertEquals(ChecksumType.FULL_OBJECT, ChecksumAlgorithm.CRC32.multipartType(ChecksumType.FULL_OBJECT));
assertEquals(ChecksumType.COMPOSITE, ChecksumAlgorithm.CRC32C.multipartType(ChecksumType.COMPOSITE));
AwsException shaFullObject = assertThrows(AwsException.class,
() -> ChecksumAlgorithm.SHA256.multipartType(ChecksumType.FULL_OBJECT));
assertEquals("The FULL_OBJECT checksum type cannot be used with the sha256 checksum algorithm.",
shaFullObject.getMessage());
AwsException crc64Composite = assertThrows(AwsException.class,
() -> ChecksumAlgorithm.CRC64NVME.multipartType(ChecksumType.COMPOSITE));
assertEquals("The COMPOSITE checksum type cannot be used with the crc64nvme checksum algorithm.",
crc64Composite.getMessage());
}
@Test
void typeHeaderParsingIsCaseInsensitiveAndRejectsUnknownValues() {
assertEquals(ChecksumType.FULL_OBJECT, ChecksumAlgorithm.CRC32.multipartType(ChecksumType.fromWireValue("full_object")));
assertEquals(ChecksumType.COMPOSITE, ChecksumType.fromWireValue("COMPOSITE"));
assertNull(ChecksumType.fromWireValue(null));
AwsException unknown = assertThrows(AwsException.class, () -> ChecksumType.fromWireValue("PARTIAL"));
assertEquals("InvalidRequest", unknown.getErrorCode());
assertEquals("Value for x-amz-checksum-type header is invalid.", unknown.getMessage());
}
}
@@ -106,6 +106,8 @@ class S3IntegrationTest {
.then()
.statusCode(200)
.body(containsString("<GetObjectAttributesResponse"))
// S3 returns this ETag without the quotes HeadObject carries
.body(matchesPattern("(?s).*<ETag>[0-9a-f]{32}</ETag>.*"))
.body(containsString("<StorageClass>STANDARD_IA</StorageClass>"))
.body(containsString("<ObjectSize>20</ObjectSize>"))
.body(containsString("<ChecksumCRC64NVME>"));
@@ -217,6 +219,7 @@ class S3IntegrationTest {
.header("x-amz-meta-owner", equalTo("team-b"))
.header("x-amz-storage-class", equalTo("GLACIER"))
.header("x-amz-checksum-sha256", notNullValue())
.header("x-amz-checksum-type", equalTo("FULL_OBJECT"))
.body(equalTo("Hello World from S3!"));
// Verify GetObjectAttributes returns the overridden checksum algorithm
@@ -25,6 +25,9 @@ class S3MultipartIntegrationTest {
private static final String SSE_CUSTOMER_KEY_MD5 = customerKeyMd5(SSE_CUSTOMER_KEY);
private static final String WRONG_SSE_CUSTOMER_KEY = Base64.getEncoder().encodeToString("abcdef0123456789abcdef0123456789".getBytes(StandardCharsets.UTF_8));
private static final String WRONG_SSE_CUSTOMER_KEY_MD5 = customerKeyMd5(WRONG_SSE_CUSTOMER_KEY);
private static final String PART_1 = "Part1Data-Hello";
private static final String PART_2 = "Part2Data-World";
private static final String COMPOSITE_SHA256 = compositeSha256(PART_1, PART_2);
private static String uploadId;
@Test
@@ -60,24 +63,26 @@ class S3MultipartIntegrationTest {
@Order(3)
void uploadPart1() {
given()
.body("Part1Data-Hello")
.body(PART_1)
.when()
.put("/" + BUCKET + "/" + KEY + "?uploadId=" + uploadId + "&partNumber=1")
.then()
.statusCode(200)
.header("ETag", notNullValue());
.header("ETag", notNullValue())
.header("x-amz-checksum-sha256", equalTo(S3Checksum.sha256Base64(PART_1.getBytes(StandardCharsets.UTF_8))));
}
@Test
@Order(4)
void uploadPart2() {
given()
.body("Part2Data-World")
.body(PART_2)
.when()
.put("/" + BUCKET + "/" + KEY + "?uploadId=" + uploadId + "&partNumber=2")
.then()
.statusCode(200)
.header("ETag", notNullValue());
.header("ETag", notNullValue())
.header("x-amz-checksum-sha256", equalTo(S3Checksum.sha256Base64(PART_2.getBytes(StandardCharsets.UTF_8))));
}
@Test
@@ -114,9 +119,9 @@ class S3MultipartIntegrationTest {
void completeMultipartUpload() {
String completeXml = """
<CompleteMultipartUpload>
<Part><PartNumber>1</PartNumber><ETag>etag1</ETag></Part>
<Part><PartNumber>2</PartNumber><ETag>etag2</ETag></Part>
</CompleteMultipartUpload>""";
<Part><PartNumber>1</PartNumber><ETag>etag1</ETag><ChecksumSHA256>%s</ChecksumSHA256></Part>
<Part><PartNumber>2</PartNumber><ETag>etag2</ETag><ChecksumSHA256>%s</ChecksumSHA256></Part>
</CompleteMultipartUpload>""".formatted(sha256Base64(PART_1), sha256Base64(PART_2));
given()
.contentType("application/xml")
@@ -127,7 +132,9 @@ class S3MultipartIntegrationTest {
.statusCode(200)
.body(containsString("<CompleteMultipartUploadResult"))
.body(containsString("<ETag>"))
.body(containsString("-2")); // Composite ETag ends with -2
.body(containsString("-2")) // Composite ETag ends with -2
.body(containsString("<ChecksumSHA256>" + COMPOSITE_SHA256 + "</ChecksumSHA256>"))
.body(containsString("<ChecksumType>COMPOSITE</ChecksumType>"));
}
@Test
@@ -142,24 +149,38 @@ class S3MultipartIntegrationTest {
.header("x-amz-server-side-encryption", equalTo("AES256"))
.header("x-amz-meta-owner", equalTo("team-a"))
.header("x-amz-storage-class", equalTo("STANDARD_IA"))
.body(equalTo("Part1Data-HelloPart2Data-World"));
.body(equalTo(PART_1 + PART_2));
given()
.header("x-amz-checksum-mode", "ENABLED")
.when()
.head("/" + BUCKET + "/" + KEY)
.then()
.statusCode(200)
.header("x-amz-checksum-sha256", equalTo(COMPOSITE_SHA256))
.header("x-amz-checksum-type", equalTo("COMPOSITE"));
}
@Test
@Order(10)
void getMultipartObjectAttributes() {
given()
.header("x-amz-object-attributes", "ObjectParts,Checksum,StorageClass")
.header("x-amz-object-attributes", "ETag,ObjectParts,Checksum,StorageClass")
.header("x-amz-max-parts", 1)
.when()
.get("/" + BUCKET + "/" + KEY + "?attributes")
.then()
.statusCode(200)
.body(containsString("<GetObjectAttributesResponse"))
.body(matchesPattern("(?s).*<ETag>[0-9a-f]{32}-2</ETag>.*"))
.body(containsString("<StorageClass>STANDARD_IA</StorageClass>"))
.body(containsString("<ObjectParts>"))
.body(containsString("<PartsCount>2</PartsCount>"))
.body(containsString("<ChecksumSHA256>"));
// GetObjectAttributes reports the composite value without the "-N" suffix
.body(containsString("<ChecksumSHA256>" + S3Checksum.withoutPartCount(COMPOSITE_SHA256) + "</ChecksumSHA256>"))
.body(containsString("<ChecksumType>COMPOSITE</ChecksumType>"))
.body(containsString("<ChecksumSHA256>" + S3Checksum.sha256Base64(PART_1.getBytes(StandardCharsets.UTF_8))
+ "</ChecksumSHA256>"));
}
@Test
@@ -564,6 +585,7 @@ class S3MultipartIntegrationTest {
String key = "checksum-mismatch-multipart.bin";
String newUploadId = given()
.header("x-amz-checksum-algorithm", "CRC32")
.header("x-amz-checksum-type", "FULL_OBJECT")
.when()
.post("/" + BUCKET + "/" + key + "?uploads")
.then()
@@ -610,10 +632,13 @@ class S3MultipartIntegrationTest {
String newUploadId = given()
.header("x-amz-checksum-algorithm", "CRC32")
.header("x-amz-checksum-type", "FULL_OBJECT")
.when()
.post("/" + BUCKET + "/" + key + "?uploads")
.then()
.statusCode(200)
.header("x-amz-checksum-algorithm", equalTo("CRC32"))
.header("x-amz-checksum-type", equalTo("FULL_OBJECT"))
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
given()
@@ -659,16 +684,11 @@ class S3MultipartIntegrationTest {
.then()
.statusCode(200);
String completeXml = """
<CompleteMultipartUpload>
<Part><PartNumber>1</PartNumber><ETag>etag1</ETag></Part>
</CompleteMultipartUpload>""";
given()
.contentType("application/xml")
.header("x-amz-checksum-type", "FULL_OBJECT")
.header("x-amz-checksum-sha256", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")
.body(completeXml)
.body(singlePartCompleteXml("<ChecksumSHA256>" + sha256Base64("Part1Data-Hello") + "</ChecksumSHA256>"))
.when()
.post("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId)
.then()
@@ -691,10 +711,13 @@ class S3MultipartIntegrationTest {
String newUploadId = given()
.header("x-amz-checksum-algorithm", "CRC32")
.header("x-amz-checksum-type", "FULL_OBJECT")
.when()
.post("/" + BUCKET + "/" + key + "?uploads")
.then()
.statusCode(200)
.header("x-amz-checksum-algorithm", equalTo("CRC32"))
.header("x-amz-checksum-type", equalTo("FULL_OBJECT"))
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
given()
@@ -730,8 +753,417 @@ class S3MultipartIntegrationTest {
@Test
@Order(24)
void initiateMultipartUploadResolvesChecksumType() {
String newUploadId = given()
.header("x-amz-checksum-algorithm", "SHA256")
.when()
.post("/" + BUCKET + "/checksum-type-default.bin?uploads")
.then()
.statusCode(200)
.header("x-amz-checksum-algorithm", equalTo("SHA256"))
.header("x-amz-checksum-type", equalTo("COMPOSITE"))
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
given().when().delete("/" + BUCKET + "/checksum-type-default.bin?uploadId=" + newUploadId).then().statusCode(204);
given()
.header("x-amz-checksum-algorithm", "SHA256")
.header("x-amz-checksum-type", "FULL_OBJECT")
.when()
.post("/" + BUCKET + "/checksum-type-invalid.bin?uploads")
.then()
.statusCode(400)
.body(containsString("InvalidRequest"))
.body(containsString("The FULL_OBJECT checksum type cannot be used with the sha256 checksum algorithm."));
given()
.header("x-amz-checksum-algorithm", "CRC64NVME")
.header("x-amz-checksum-type", "COMPOSITE")
.when()
.post("/" + BUCKET + "/checksum-type-invalid.bin?uploads")
.then()
.statusCode(400)
.body(containsString("InvalidRequest"));
// S3 validates the type header on its own, and only accepts it next to an algorithm
given()
.header("x-amz-checksum-type", "PARTIAL")
.when()
.post("/" + BUCKET + "/checksum-type-invalid.bin?uploads")
.then()
.statusCode(400)
.body(containsString("Value for x-amz-checksum-type header is invalid."));
given()
.header("x-amz-checksum-type", "FULL_OBJECT")
.when()
.post("/" + BUCKET + "/checksum-type-invalid.bin?uploads")
.then()
.statusCode(400)
.body(containsString("The x-amz-checksum-type header can only be used with the x-amz-checksum-algorithm header."));
}
@Test
@Order(25)
void completeMultipartUploadRejectsFullObjectTypeOnCompositeUpload() {
String key = "checksum-mode-mismatch.bin";
String data = "Part1Data-Hello";
String newUploadId = given()
.header("x-amz-checksum-algorithm", "CRC32")
.when()
.post("/" + BUCKET + "/" + key + "?uploads")
.then()
.statusCode(200)
.header("x-amz-checksum-type", equalTo("COMPOSITE"))
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
given()
.body(data)
.when()
.put("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId + "&partNumber=1")
.then()
.statusCode(200);
given()
.contentType("application/xml")
.header("x-amz-checksum-type", "FULL_OBJECT")
.header("x-amz-checksum-crc32", S3Checksum.crc32Base64(data.getBytes(StandardCharsets.UTF_8)))
.body(singlePartCompleteXml("<ChecksumCRC32>" + S3Checksum.crc32Base64(data.getBytes(StandardCharsets.UTF_8)) + "</ChecksumCRC32>"))
.when()
.post("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId)
.then()
.statusCode(400)
.body(containsString("InvalidRequest"))
.body(containsString("The upload was created using the COMPOSITE checksum mode."));
given().when().delete("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId).then().statusCode(204);
}
@Test
@Order(26)
void completeMultipartUploadRejectsMismatchedCompositeChecksum() {
String key = "composite-mismatch-multipart.bin";
String newUploadId = given()
.header("x-amz-checksum-algorithm", "SHA256")
.when()
.post("/" + BUCKET + "/" + key + "?uploads")
.then()
.statusCode(200)
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
given()
.body("Part1Data-Hello")
.when()
.put("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId + "&partNumber=1")
.then()
.statusCode(200);
given()
.contentType("application/xml")
.header("x-amz-checksum-type", "COMPOSITE")
.header("x-amz-checksum-sha256", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=-1")
.body(singlePartCompleteXml("<ChecksumSHA256>" + sha256Base64("Part1Data-Hello") + "</ChecksumSHA256>"))
.when()
.post("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId)
.then()
.statusCode(400)
.body(containsString("BadDigest"))
.body(containsString("The sha256 you specified did not match the calculated checksum."));
given().when().delete("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId).then().statusCode(204);
}
@Test
@Order(27)
void completeMultipartUploadAcceptsMatchingCompositeChecksumAndPartChecksums() {
String key = "composite-match-multipart.bin";
String newUploadId = given()
.header("x-amz-checksum-algorithm", "SHA256")
.when()
.post("/" + BUCKET + "/" + key + "?uploads")
.then()
.statusCode(200)
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
String part1Sha256 = S3Checksum.sha256Base64(PART_1.getBytes(StandardCharsets.UTF_8));
String part2Sha256 = S3Checksum.sha256Base64(PART_2.getBytes(StandardCharsets.UTF_8));
given()
.header("x-amz-checksum-sha256", part1Sha256)
.body(PART_1)
.when()
.put("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId + "&partNumber=1")
.then()
.statusCode(200);
given()
.header("x-amz-checksum-sha256", part2Sha256)
.body(PART_2)
.when()
.put("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId + "&partNumber=2")
.then()
.statusCode(200);
String completeXml = """
<CompleteMultipartUpload xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Part><ChecksumSHA256>%s</ChecksumSHA256><ETag>etag1</ETag><PartNumber>1</PartNumber></Part>
<Part><ChecksumSHA256>%s</ChecksumSHA256><ETag>etag2</ETag><PartNumber>2</PartNumber></Part>
</CompleteMultipartUpload>""".formatted(part1Sha256, part2Sha256);
given()
.contentType("application/xml")
.header("x-amz-checksum-type", "COMPOSITE")
.header("x-amz-checksum-sha256", COMPOSITE_SHA256)
.body(completeXml)
.when()
.post("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId)
.then()
.statusCode(200)
.body(containsString("<ChecksumSHA256>" + COMPOSITE_SHA256 + "</ChecksumSHA256>"))
.body(containsString("<ChecksumType>COMPOSITE</ChecksumType>"));
given()
.header("x-amz-checksum-mode", "ENABLED")
.when()
.get("/" + BUCKET + "/" + key)
.then()
.statusCode(200)
.header("x-amz-checksum-sha256", equalTo(COMPOSITE_SHA256))
.header("x-amz-checksum-type", equalTo("COMPOSITE"));
}
@Test
@Order(28)
void completeMultipartUploadRejectsMismatchedPartChecksum() {
String key = "bad-part-checksum-multipart.bin";
String newUploadId = given()
.header("x-amz-checksum-algorithm", "SHA256")
.when()
.post("/" + BUCKET + "/" + key + "?uploads")
.then()
.statusCode(200)
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
given()
.body("Part1Data-Hello")
.when()
.put("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId + "&partNumber=1")
.then()
.statusCode(200);
String completeXml = """
<CompleteMultipartUpload>
<Part><PartNumber>1</PartNumber><ETag>etag1</ETag>
<ChecksumSHA256>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=</ChecksumSHA256></Part>
</CompleteMultipartUpload>""";
given()
.contentType("application/xml")
.body(completeXml)
.when()
.post("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId)
.then()
.statusCode(400)
.body(containsString("InvalidPart"));
given().when().delete("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId).then().statusCode(204);
}
@Test
@Order(29)
void completeMultipartUploadWithCrc64NvmeReportsFullObjectChecksum() {
String key = "crc64-multipart.bin";
String newUploadId = given()
.header("x-amz-checksum-algorithm", "CRC64NVME")
.when()
.post("/" + BUCKET + "/" + key + "?uploads")
.then()
.statusCode(200)
.header("x-amz-checksum-type", equalTo("FULL_OBJECT"))
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
given().body(PART_1).when()
.put("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId + "&partNumber=1")
.then().statusCode(200);
given().body(PART_2).when()
.put("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId + "&partNumber=2")
.then().statusCode(200);
String fullObjectCrc64 = S3Checksum.crc64NvmeBase64((PART_1 + PART_2).getBytes(StandardCharsets.UTF_8));
given()
.contentType("application/xml")
.body("""
<CompleteMultipartUpload>
<Part><PartNumber>1</PartNumber><ETag>etag1</ETag></Part>
<Part><PartNumber>2</PartNumber><ETag>etag2</ETag></Part>
</CompleteMultipartUpload>""")
.when()
.post("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId)
.then()
.statusCode(200)
.body(containsString("<ChecksumCRC64NVME>" + fullObjectCrc64 + "</ChecksumCRC64NVME>"))
.body(containsString("<ChecksumType>FULL_OBJECT</ChecksumType>"));
given()
.header("x-amz-checksum-mode", "ENABLED")
.when()
.head("/" + BUCKET + "/" + key)
.then()
.statusCode(200)
.header("x-amz-checksum-crc64nvme", equalTo(fullObjectCrc64))
.header("x-amz-checksum-type", equalTo("FULL_OBJECT"));
}
@Test
@Order(30)
void copyOfMultipartObjectReportsFullObjectChecksum() {
given()
.header("x-amz-copy-source", "/" + BUCKET + "/" + KEY)
.when()
.put("/" + BUCKET + "/composite-copy.bin")
.then()
.statusCode(200)
.body(containsString("CopyObjectResult"))
.body(containsString("<ChecksumSHA256>" + S3Checksum.sha256Base64((PART_1 + PART_2).getBytes(StandardCharsets.UTF_8))
+ "</ChecksumSHA256>"))
.body(containsString("<ChecksumType>FULL_OBJECT</ChecksumType>"));
given()
.header("x-amz-checksum-mode", "ENABLED")
.when()
.head("/" + BUCKET + "/composite-copy.bin")
.then()
.statusCode(200)
.header("x-amz-checksum-sha256", equalTo(S3Checksum.sha256Base64((PART_1 + PART_2).getBytes(StandardCharsets.UTF_8))))
.header("x-amz-checksum-type", equalTo("FULL_OBJECT"));
given()
.header("x-amz-object-attributes", "ObjectParts,Checksum")
.when()
.get("/" + BUCKET + "/composite-copy.bin?attributes")
.then()
.statusCode(200)
.body(containsString("<ChecksumType>FULL_OBJECT</ChecksumType>"))
.body(not(containsString("<ObjectParts>")));
}
@Test
@Order(31)
void completeMultipartUploadRejectsNonNumericPartNumber() {
String key = "malformed-complete.bin";
String newUploadId = given()
.when()
.post("/" + BUCKET + "/" + key + "?uploads")
.then()
.statusCode(200)
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
given()
.contentType("application/xml")
.body("<CompleteMultipartUpload><Part><PartNumber>one</PartNumber><ETag>etag1</ETag></Part></CompleteMultipartUpload>")
.when()
.post("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId)
.then()
.statusCode(400)
.body(containsString("MalformedXML"));
given().when().delete("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId).then().statusCode(204);
}
@Test
@Order(32)
void uploadPartEchoesNoChecksumWhenTheUploadDeclaredNoAlgorithm() {
String key = "no-algorithm-part.bin";
String newUploadId = given()
.when()
.post("/" + BUCKET + "/" + key + "?uploads")
.then()
.statusCode(200)
.header("x-amz-checksum-algorithm", nullValue())
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
given()
.body(PART_1)
.when()
.put("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId + "&partNumber=1")
.then()
.statusCode(200)
.header("ETag", notNullValue())
.header("x-amz-checksum-crc64nvme", nullValue())
.header("x-amz-checksum-sha256", nullValue());
given().when().delete("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId).then().statusCode(204);
}
@Test
@Order(33)
void completeMultipartUploadRequiresPartChecksumsOnCompositeUploads() {
String key = "missing-part-checksums.bin";
String newUploadId = given()
.header("x-amz-checksum-algorithm", "SHA256")
.when()
.post("/" + BUCKET + "/" + key + "?uploads")
.then()
.statusCode(200)
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
given()
.body(PART_1)
.when()
.put("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId + "&partNumber=1")
.then()
.statusCode(200);
given()
.contentType("application/xml")
.body(singlePartCompleteXml(""))
.when()
.post("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId)
.then()
.statusCode(400)
.body(containsString("<Code>InvalidRequest</Code>"))
.body(containsString("The upload was created using a sha256 checksum. The complete request must include "
+ "the checksum for each part. It was missing for part 1 in the request."));
given()
.contentType("application/xml")
.body(singlePartCompleteXml("<ChecksumCRC32>" + S3Checksum.crc32Base64(PART_1.getBytes(StandardCharsets.UTF_8)) + "</ChecksumCRC32>"))
.when()
.post("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId)
.then()
.statusCode(400)
.body(containsString("<Code>BadDigest</Code>"))
.body(containsString("The crc32 you specified for part 1 did not match what we received."));
given().when().delete("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId).then().statusCode(204);
}
@Test
@Order(34)
void copyOfMultipartObjectGetsItsOwnEtag() {
given()
.header("x-amz-copy-source", "/" + BUCKET + "/" + KEY)
.when()
.put("/" + BUCKET + "/copy-of-multipart.bin")
.then()
.statusCode(200)
.contentType("application/xml")
// the copy is a single object: a plain MD5 ETag, not the "-2" of the multipart source
.body(matchesPattern("(?s).*<ETag>&quot;[0-9a-f]{32}&quot;</ETag>.*"));
given()
.when()
.head("/" + BUCKET + "/copy-of-multipart.bin")
.then()
.statusCode(200)
.header("ETag", matchesPattern("\"[0-9a-f]{32}\""));
}
@Test
@Order(40)
void cleanUp() {
given().when().delete("/" + BUCKET + "/copy-of-multipart.bin").then().statusCode(204);
given().when().delete("/" + BUCKET + "/" + KEY).then().statusCode(204);
given().when().delete("/" + BUCKET + "/composite-match-multipart.bin").then().statusCode(204);
given().when().delete("/" + BUCKET + "/crc64-multipart.bin").then().statusCode(204);
given().when().delete("/" + BUCKET + "/composite-copy.bin").then().statusCode(204);
given().when().delete("/" + BUCKET + "/tagged-multipart.bin").then().statusCode(204);
given().when().delete("/" + BUCKET + "/source-for-copy.bin").then().statusCode(204);
given().when().delete("/" + BUCKET + "/copy-dest.bin").then().statusCode(204);
@@ -742,6 +1174,30 @@ class S3MultipartIntegrationTest {
given().when().delete("/" + BUCKET).then().statusCode(204);
}
private static String singlePartCompleteXml(String checksumElement) {
return """
<CompleteMultipartUpload>
<Part><PartNumber>1</PartNumber><ETag>etag1</ETag>%s</Part>
</CompleteMultipartUpload>""".formatted(checksumElement);
}
private static String sha256Base64(String data) {
return S3Checksum.sha256Base64(data.getBytes(StandardCharsets.UTF_8));
}
private static String compositeSha256(String... parts) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
MessageDigest composite = MessageDigest.getInstance("SHA-256");
for (String part : parts) {
composite.update(digest.digest(part.getBytes(StandardCharsets.UTF_8)));
}
return Base64.getEncoder().encodeToString(composite.digest()) + "-" + parts.length;
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("SHA-256 is not available", e);
}
}
private static String customerKeyMd5(String customerKey) {
try {
byte[] md5 = MessageDigest.getInstance("MD5").digest(Base64.getDecoder().decode(customerKey));
@@ -2,9 +2,12 @@ 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.ChecksumAlgorithm;
import io.github.hectorvent.floci.services.s3.model.ChecksumType;
import io.github.hectorvent.floci.services.s3.model.GetObjectAttributesResult;
import io.github.hectorvent.floci.services.s3.model.MultipartUpload;
import io.github.hectorvent.floci.services.s3.model.ObjectAttributeName;
import io.github.hectorvent.floci.services.s3.model.S3Checksum;
import io.github.hectorvent.floci.services.s3.model.S3Object;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -12,6 +15,10 @@ import org.junit.jupiter.api.io.TempDir;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -91,7 +98,10 @@ class S3MultipartServiceTest {
assertEquals("part1part2", new String(fetched.getData()));
// Composite ETag should end with -2 (number of parts)
assertTrue(result.getETag().endsWith("-2\""), "ETag should be composite: " + result.getETag());
assertEquals("COMPOSITE", result.getChecksum().getChecksumType());
// Without a checksum algorithm AWS attaches the default CRC64NVME full-object checksum
assertEquals(ChecksumType.FULL_OBJECT, result.getChecksum().getChecksumType());
assertEquals(S3Checksum.crc64NvmeBase64("part1part2".getBytes(StandardCharsets.UTF_8)),
result.getChecksum().getChecksumCRC64NVME());
}
@Test
@@ -181,22 +191,284 @@ class S3MultipartServiceTest {
@Test
void getObjectAttributesReturnsMultipartParts() {
MultipartUpload upload = s3Service.initiateMultipartUpload("test-bucket", "parts.bin", "application/octet-stream");
MultipartUpload upload = initiate("parts.bin", "SHA256", null);
s3Service.uploadPart("test-bucket", "parts.bin", upload.getUploadId(), 1, "abc".getBytes(StandardCharsets.UTF_8));
s3Service.uploadPart("test-bucket", "parts.bin", upload.getUploadId(), 2, "def".getBytes(StandardCharsets.UTF_8));
s3Service.completeMultipartUpload("test-bucket", "parts.bin", upload.getUploadId(), List.of(1, 2), null, null);
s3Service.completeMultipartUpload("test-bucket", "parts.bin", upload.getUploadId(), List.of(1, 2),
partChecksums(ChecksumAlgorithm.SHA256, "abc".getBytes(StandardCharsets.UTF_8), "def".getBytes(StandardCharsets.UTF_8)), null, null);
GetObjectAttributesResult attributes = s3Service.getObjectAttributes("test-bucket", "parts.bin", null,
Set.of(ObjectAttributeName.OBJECT_PARTS, ObjectAttributeName.CHECKSUM),
1, 0);
assertNotNull(attributes.getChecksum());
assertEquals("COMPOSITE", attributes.getChecksum().getChecksumType());
assertEquals(ChecksumType.COMPOSITE, attributes.getChecksum().getChecksumType());
assertNotNull(attributes.getObjectParts());
assertTrue(attributes.getObjectParts().isPartChecksumsAvailable());
assertEquals(2, attributes.getObjectParts().getPartsCount());
assertEquals(1, attributes.getObjectParts().getParts().size());
assertTrue(attributes.getObjectParts().isTruncated());
assertEquals(1, attributes.getObjectParts().getNextPartNumberMarker());
assertNotNull(attributes.getObjectParts().getParts().get(0).getChecksum().getChecksumCRC64NVME());
assertEquals(S3Checksum.sha256Base64("abc".getBytes(StandardCharsets.UTF_8)),
attributes.getObjectParts().getParts().get(0).getChecksum().getChecksumSHA256());
}
@Test
void getObjectAttributesReportsOnlyThePartCountForFullObjectChecksums() {
MultipartUpload upload = s3Service.initiateMultipartUpload("test-bucket", "full-parts.bin", "application/octet-stream");
s3Service.uploadPart("test-bucket", "full-parts.bin", upload.getUploadId(), 1, "abc".getBytes(StandardCharsets.UTF_8));
s3Service.uploadPart("test-bucket", "full-parts.bin", upload.getUploadId(), 2, "def".getBytes(StandardCharsets.UTF_8));
s3Service.completeMultipartUpload("test-bucket", "full-parts.bin", upload.getUploadId(), List.of(1, 2), null, null);
GetObjectAttributesResult attributes = s3Service.getObjectAttributes("test-bucket", "full-parts.bin", null,
Set.of(ObjectAttributeName.OBJECT_PARTS, ObjectAttributeName.CHECKSUM), null, null);
assertEquals(ChecksumType.FULL_OBJECT, attributes.getChecksum().getChecksumType());
assertEquals(2, attributes.getObjectParts().getPartsCount());
assertFalse(attributes.getObjectParts().isPartChecksumsAvailable());
assertTrue(attributes.getObjectParts().getParts().isEmpty());
}
@Test
void getObjectAttributesOmitsObjectPartsForSinglePartObjects() {
s3Service.putObject("test-bucket", "single.txt", "hello".getBytes(StandardCharsets.UTF_8), "text/plain", null);
GetObjectAttributesResult attributes = s3Service.getObjectAttributes("test-bucket", "single.txt", null,
Set.of(ObjectAttributeName.OBJECT_PARTS, ObjectAttributeName.CHECKSUM), null, null);
assertEquals(ChecksumType.FULL_OBJECT, attributes.getChecksum().getChecksumType());
assertNull(attributes.getObjectParts());
}
@Test
void completeMultipartUploadReturnsCompositeSha256Checksum() {
byte[] part1 = "Part1Data-Hello".getBytes(StandardCharsets.UTF_8);
byte[] part2 = "Part2Data-World".getBytes(StandardCharsets.UTF_8);
MultipartUpload upload = initiate("sha256.bin", "SHA256", null);
s3Service.uploadPart("test-bucket", "sha256.bin", upload.getUploadId(), 1, part1);
s3Service.uploadPart("test-bucket", "sha256.bin", upload.getUploadId(), 2, part2);
S3Object result = s3Service.completeMultipartUpload("test-bucket", "sha256.bin",
upload.getUploadId(), List.of(1, 2), partChecksums(ChecksumAlgorithm.SHA256, part1, part2), null, null);
String expected = compositeSha256(part1, part2);
assertTrue(expected.endsWith("-2"));
assertEquals(expected, result.getChecksum().getChecksumSHA256());
assertEquals(ChecksumType.COMPOSITE, result.getChecksum().getChecksumType());
assertEquals(S3Checksum.sha256Base64(part1), result.getParts().get(0).getChecksum().getChecksumSHA256());
assertEquals(S3Checksum.sha256Base64(part2), result.getParts().get(1).getChecksum().getChecksumSHA256());
S3Object head = s3Service.getObjectMetadata("test-bucket", "sha256.bin", null);
assertEquals(expected, head.getChecksum().getChecksumSHA256());
GetObjectAttributesResult attributes = s3Service.getObjectAttributes("test-bucket", "sha256.bin", null,
Set.of(ObjectAttributeName.CHECKSUM), null, null);
assertEquals(S3Checksum.withoutPartCount(expected), attributes.getChecksum().getChecksumSHA256());
assertEquals(ChecksumType.COMPOSITE, attributes.getChecksum().getChecksumType());
}
@Test
void singlePartMultipartUploadStillReturnsCompositeChecksum() {
byte[] data = "only-part".getBytes(StandardCharsets.UTF_8);
MultipartUpload upload = initiate("single.bin", "SHA256", null);
s3Service.uploadPart("test-bucket", "single.bin", upload.getUploadId(), 1, data);
S3Object result = s3Service.completeMultipartUpload("test-bucket", "single.bin",
upload.getUploadId(), List.of(1), partChecksums(ChecksumAlgorithm.SHA256, data), null, null);
assertEquals(compositeSha256(data), result.getChecksum().getChecksumSHA256());
assertTrue(result.getChecksum().getChecksumSHA256().endsWith("-1"));
assertEquals(ChecksumType.COMPOSITE, result.getChecksum().getChecksumType());
}
@Test
void completeMultipartUploadReturnsCompositeCrc32Checksum() {
byte[] part1 = "crc-part-1".getBytes(StandardCharsets.UTF_8);
byte[] part2 = "crc-part-2".getBytes(StandardCharsets.UTF_8);
MultipartUpload upload = initiate("crc32.bin", "CRC32", null);
s3Service.uploadPart("test-bucket", "crc32.bin", upload.getUploadId(), 1, part1);
s3Service.uploadPart("test-bucket", "crc32.bin", upload.getUploadId(), 2, part2);
S3Object result = s3Service.completeMultipartUpload("test-bucket", "crc32.bin",
upload.getUploadId(), List.of(1, 2), partChecksums(ChecksumAlgorithm.CRC32, part1, part2), null, null);
byte[] partChecksums = concat(Base64.getDecoder().decode(S3Checksum.crc32Base64(part1)),
Base64.getDecoder().decode(S3Checksum.crc32Base64(part2)));
assertEquals(S3Checksum.crc32Base64(partChecksums) + "-2", result.getChecksum().getChecksumCRC32());
assertEquals(ChecksumType.COMPOSITE, result.getChecksum().getChecksumType());
}
@Test
void completeMultipartUploadKeepsCrc64NvmeAsFullObjectChecksum() {
byte[] part1 = "crc64-part-1".getBytes(StandardCharsets.UTF_8);
byte[] part2 = "crc64-part-2".getBytes(StandardCharsets.UTF_8);
MultipartUpload upload = initiate("crc64.bin", "CRC64NVME", null);
assertEquals(ChecksumType.FULL_OBJECT, upload.getChecksumType());
s3Service.uploadPart("test-bucket", "crc64.bin", upload.getUploadId(), 1, part1);
s3Service.uploadPart("test-bucket", "crc64.bin", upload.getUploadId(), 2, part2);
S3Object result = s3Service.completeMultipartUpload("test-bucket", "crc64.bin",
upload.getUploadId(), List.of(1, 2), null, null);
assertEquals(S3Checksum.crc64NvmeBase64(concat(part1, part2)), result.getChecksum().getChecksumCRC64NVME());
assertEquals(ChecksumType.FULL_OBJECT, result.getChecksum().getChecksumType());
}
@Test
void completeMultipartUploadHonorsFullObjectTypeRequestedAtInitiation() {
byte[] part1 = "full-part-1".getBytes(StandardCharsets.UTF_8);
byte[] part2 = "full-part-2".getBytes(StandardCharsets.UTF_8);
MultipartUpload upload = initiate("crc32-full.bin", "CRC32", "FULL_OBJECT");
s3Service.uploadPart("test-bucket", "crc32-full.bin", upload.getUploadId(), 1, part1);
s3Service.uploadPart("test-bucket", "crc32-full.bin", upload.getUploadId(), 2, part2);
S3Object result = s3Service.completeMultipartUpload("test-bucket", "crc32-full.bin",
upload.getUploadId(), List.of(1, 2), null, null);
assertEquals(S3Checksum.crc32Base64(concat(part1, part2)), result.getChecksum().getChecksumCRC32());
assertEquals(ChecksumType.FULL_OBJECT, result.getChecksum().getChecksumType());
}
@Test
void initiateMultipartUploadRejectsIncompatibleChecksumType() {
AwsException sha = assertThrows(AwsException.class, () -> initiate("bad-sha.bin", "SHA256", "FULL_OBJECT"));
assertEquals("InvalidRequest", sha.getErrorCode());
AwsException crc64 = assertThrows(AwsException.class, () -> initiate("bad-crc64.bin", "CRC64NVME", "COMPOSITE"));
assertEquals("InvalidRequest", crc64.getErrorCode());
assertEquals(ChecksumType.COMPOSITE, initiate("crc32c.bin", "CRC32C", null).getChecksumType());
assertNull(initiate("no-algorithm.bin", null, null).getChecksumType());
AwsException typeAlone = assertThrows(AwsException.class, () -> initiate("type-alone.bin", null, "FULL_OBJECT"));
assertEquals("InvalidRequest", typeAlone.getErrorCode());
assertEquals("The x-amz-checksum-type header can only be used with the x-amz-checksum-algorithm header.",
typeAlone.getMessage());
AwsException unknownType = assertThrows(AwsException.class, () -> initiate("unknown-type.bin", null, "PARTIAL"));
assertEquals("InvalidRequest", unknownType.getErrorCode());
assertEquals("Value for x-amz-checksum-type header is invalid.", unknownType.getMessage());
}
@Test
void completeMultipartUploadValidatesClientCompositeChecksum() {
byte[] part1 = "Part1Data-Hello".getBytes(StandardCharsets.UTF_8);
byte[] part2 = "Part2Data-World".getBytes(StandardCharsets.UTF_8);
MultipartUpload upload = initiate("validated.bin", "SHA256", null);
s3Service.uploadPart("test-bucket", "validated.bin", upload.getUploadId(), 1, part1);
s3Service.uploadPart("test-bucket", "validated.bin", upload.getUploadId(), 2, part2);
String composite = compositeSha256(part1, part2);
S3Checksum wrong = new S3Checksum();
wrong.setChecksumSHA256("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=-2");
AwsException mismatch = assertThrows(AwsException.class, () -> s3Service.completeMultipartUpload(
"test-bucket", "validated.bin", upload.getUploadId(), List.of(1, 2), partChecksums(ChecksumAlgorithm.SHA256, part1, part2), "COMPOSITE", wrong));
assertEquals("BadDigest", mismatch.getErrorCode());
S3Checksum wrongPartCount = new S3Checksum();
wrongPartCount.setChecksumSHA256(S3Checksum.withoutPartCount(composite) + "-5");
assertEquals("BadDigest", assertThrows(AwsException.class, () -> s3Service.completeMultipartUpload(
"test-bucket", "validated.bin", upload.getUploadId(), List.of(1, 2), partChecksums(ChecksumAlgorithm.SHA256, part1, part2), null, wrongPartCount)).getErrorCode());
S3Checksum withoutSuffix = new S3Checksum();
withoutSuffix.setChecksumSHA256(S3Checksum.withoutPartCount(composite));
S3Object result = s3Service.completeMultipartUpload("test-bucket", "validated.bin",
upload.getUploadId(), List.of(1, 2), partChecksums(ChecksumAlgorithm.SHA256, part1, part2), "COMPOSITE", withoutSuffix);
assertEquals(composite, result.getChecksum().getChecksumSHA256());
}
@Test
void completeMultipartUploadRejectsChecksumTypeDifferentFromInitiation() {
byte[] data = "crc-data".getBytes(StandardCharsets.UTF_8);
MultipartUpload upload = initiate("crc32-mode.bin", "CRC32", null);
s3Service.uploadPart("test-bucket", "crc32-mode.bin", upload.getUploadId(), 1, data);
S3Checksum fullObject = new S3Checksum();
fullObject.setChecksumCRC32(S3Checksum.crc32Base64(data));
AwsException error = assertThrows(AwsException.class, () -> s3Service.completeMultipartUpload(
"test-bucket", "crc32-mode.bin", upload.getUploadId(), List.of(1), partChecksums(ChecksumAlgorithm.CRC32, data), "FULL_OBJECT", fullObject));
assertEquals("InvalidRequest", error.getErrorCode());
assertTrue(error.getMessage().contains("COMPOSITE checksum mode"), error.getMessage());
}
@Test
void completeMultipartUploadRejectsMismatchedPartChecksum() {
byte[] data = "part-data".getBytes(StandardCharsets.UTF_8);
MultipartUpload upload = initiate("bad-part.bin", "SHA256", null);
s3Service.uploadPart("test-bucket", "bad-part.bin", upload.getUploadId(), 1, data);
S3Checksum wrongPart = new S3Checksum();
wrongPart.setChecksumSHA256("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=");
AwsException error = assertThrows(AwsException.class, () -> s3Service.completeMultipartUpload(
"test-bucket", "bad-part.bin", upload.getUploadId(), List.of(1), Map.of(1, wrongPart), null, null));
assertEquals("InvalidPart", error.getErrorCode());
S3Checksum rightPart = new S3Checksum();
rightPart.setChecksumSHA256(S3Checksum.sha256Base64(data));
S3Object result = s3Service.completeMultipartUpload("test-bucket", "bad-part.bin",
upload.getUploadId(), List.of(1), Map.of(1, rightPart), null, null);
assertEquals(compositeSha256(data), result.getChecksum().getChecksumSHA256());
}
@Test
void completeMultipartUploadRequiresEveryPartChecksumOnCompositeUploads() {
byte[] part1 = "Part1Data-Hello".getBytes(StandardCharsets.UTF_8);
byte[] part2 = "Part2Data-World".getBytes(StandardCharsets.UTF_8);
MultipartUpload upload = initiate("strict.bin", "SHA256", null);
s3Service.uploadPart("test-bucket", "strict.bin", upload.getUploadId(), 1, part1);
s3Service.uploadPart("test-bucket", "strict.bin", upload.getUploadId(), 2, part2);
AwsException none = assertThrows(AwsException.class, () -> s3Service.completeMultipartUpload(
"test-bucket", "strict.bin", upload.getUploadId(), List.of(1, 2), null, null));
assertEquals("InvalidRequest", none.getErrorCode());
assertEquals("The upload was created using a sha256 checksum. The complete request must include the checksum "
+ "for each part. It was missing for part 1 in the request.", none.getMessage());
AwsException second = assertThrows(AwsException.class, () -> s3Service.completeMultipartUpload(
"test-bucket", "strict.bin", upload.getUploadId(), List.of(1, 2), partChecksums(ChecksumAlgorithm.SHA256, part1), null, null));
assertTrue(second.getMessage().endsWith("It was missing for part 2 in the request."), second.getMessage());
S3Checksum otherAlgorithm = new S3Checksum();
otherAlgorithm.setChecksumCRC32(S3Checksum.crc32Base64(part1));
AwsException other = assertThrows(AwsException.class, () -> s3Service.completeMultipartUpload(
"test-bucket", "strict.bin", upload.getUploadId(), List.of(1, 2),
Map.of(1, otherAlgorithm, 2, S3Checksum.of(ChecksumAlgorithm.SHA256, part2)), null, null));
assertEquals("BadDigest", other.getErrorCode());
assertEquals("The crc32 you specified for part 1 did not match what we received.", other.getMessage());
S3Object result = s3Service.completeMultipartUpload("test-bucket", "strict.bin", upload.getUploadId(),
List.of(1, 2), partChecksums(ChecksumAlgorithm.SHA256, part1, part2), null, null);
assertEquals(compositeSha256(part1, part2), result.getChecksum().getChecksumSHA256());
}
private static Map<Integer, S3Checksum> partChecksums(ChecksumAlgorithm algorithm, byte[]... parts) {
Map<Integer, S3Checksum> checksums = new HashMap<>();
for (int i = 0; i < parts.length; i++) {
checksums.put(i + 1, S3Checksum.of(algorithm, parts[i]));
}
return checksums;
}
private MultipartUpload initiate(String key, String checksumAlgorithm, String checksumType) {
return s3Service.initiateMultipartUpload("test-bucket", key, null, null, null, null, null, null,
null, null, null, checksumAlgorithm, checksumType, null);
}
private static String compositeSha256(byte[]... parts) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
MessageDigest composite = MessageDigest.getInstance("SHA-256");
for (byte[] part : parts) {
composite.update(digest.digest(part));
}
return Base64.getEncoder().encodeToString(composite.digest()) + "-" + parts.length;
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(e);
}
}
private static byte[] concat(byte[] first, byte[] second) {
byte[] result = new byte[first.length + second.length];
System.arraycopy(first, 0, result, 0, first.length);
System.arraycopy(second, 0, result, first.length, second.length);
return result;
}
}
@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
import io.github.hectorvent.floci.services.s3.model.ChecksumType;
import io.github.hectorvent.floci.services.s3.model.FilterRule;
import io.github.hectorvent.floci.services.s3.model.GetObjectAttributesResult;
import io.github.hectorvent.floci.services.s3.model.LambdaNotification;
@@ -19,6 +20,9 @@ import org.junit.jupiter.api.io.TempDir;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HexFormat;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
@@ -396,7 +400,7 @@ class S3ServiceTest {
assertEquals("team-a", head.getMetadata().get("owner"));
assertNotNull(head.getChecksum());
assertNotNull(head.getChecksum().getChecksumCRC64NVME());
assertEquals("FULL_OBJECT", head.getChecksum().getChecksumType());
assertEquals(ChecksumType.FULL_OBJECT, head.getChecksum().getChecksumType());
assertEquals(stored.getETag(), head.getETag());
}
@@ -480,6 +484,29 @@ class S3ServiceTest {
assertEquals("dest", copy.getMetadata().get("owner"));
}
@Test
void copyOfMultipartObjectGetsTheEtagOfTheWholeContent() throws NoSuchAlgorithmException {
s3Service.createBucket("test-bucket", "us-east-1");
byte[] part1 = "Part1Data-Hello".getBytes(StandardCharsets.UTF_8);
byte[] part2 = "Part2Data-World".getBytes(StandardCharsets.UTF_8);
var upload = s3Service.initiateMultipartUpload("test-bucket", "multipart.bin", "application/octet-stream");
s3Service.uploadPart("test-bucket", "multipart.bin", upload.getUploadId(), 1, part1);
s3Service.uploadPart("test-bucket", "multipart.bin", upload.getUploadId(), 2, part2);
S3Object source = s3Service.completeMultipartUpload("test-bucket", "multipart.bin", upload.getUploadId(),
List.of(1, 2), null, null);
assertTrue(source.getETag().endsWith("-2\""), source.getETag());
S3Object copy = s3Service.copyObject("test-bucket", "multipart.bin", "test-bucket", "multipart-copy.bin");
byte[] whole = new byte[part1.length + part2.length];
System.arraycopy(part1, 0, whole, 0, part1.length);
System.arraycopy(part2, 0, whole, part1.length, part2.length);
String expected = "\"" + HexFormat.of().formatHex(MessageDigest.getInstance("MD5").digest(whole)) + "\"";
assertEquals(expected, copy.getETag());
assertNotEquals(source.getETag(), copy.getETag());
assertEquals(expected, s3Service.getObject("test-bucket", "multipart-copy.bin").getETag());
}
@Test
void copyObjectWithNonASCIIKey() {
s3Service.createBucket("test-bucket", "us-east-1");