From c15e1e26ce1bfbb67510cb11b090d9ad1dd1b367 Mon Sep 17 00:00:00 2001 From: glorfindel33 <217200047+glorfindel33@users.noreply.github.com> Date: Mon, 23 Nov 2026 13:17:02 +0200 Subject: [PATCH] fix(s3): take the bucket's default encryption when a multipart upload is created (#9212) --- docs/services/s3.md | 4 +- .../floci/aws/services/s3/S3Controller.java | 21 ++++-- .../io/floci/aws/services/s3/S3Service.java | 17 +++-- .../services/s3/model/MultipartUpload.java | 22 ++++++ ...ucketPolicyEnforcementIntegrationTest.java | 25 +++++-- .../S3DefaultEncryptionIntegrationTest.java | 72 ++++++++++++++++++- .../s3/S3DefaultEncryptionServiceTest.java | 70 ++++++++++++++++++ 7 files changed, 209 insertions(+), 22 deletions(-) diff --git a/docs/services/s3.md b/docs/services/s3.md index 3f381e0455..b889c8f0e0 100644 --- a/docs/services/s3.md +++ b/docs/services/s3.md @@ -810,8 +810,8 @@ Floci now persists and returns the following object attribute state on S3 object - canned object ACLs from `x-amz-acl` on `PutObject`, `CopyObject`, and multipart initiation - explicit ACL grant headers (`x-amz-grant-read`, `x-amz-grant-write`, `x-amz-grant-full-control`, `x-amz-grant-read-acp`, `x-amz-grant-write-acp`) with `id` and `uri` grantees on `PutObject`, `CopyObject`, and multipart initiation, where they apply when the upload completes - Object Lock headers (`x-amz-object-lock-mode`, `x-amz-object-lock-retain-until-date`, `x-amz-object-lock-legal-hold`) on `PutObject`, `CopyObject`, and multipart initiation, where they apply when the upload completes. A mode without a date or a date without a mode, an unknown mode, or a date that is not an ISO 8601 timestamp is refused with `400 InvalidArgument`. In a bucket without Object Lock, a mode, a date or a legal hold of `ON` is refused with `400 InvalidRequest`, "Bucket is missing Object Lock Configuration", as `PutObjectRetention`, `GetObjectRetention`, `PutObjectLegalHold` and `GetObjectLegalHold` are there. `GetObjectRetention` and `GetObjectLegalHold` of an object without a retention or a legal hold answer `404 NoSuchObjectLockConfiguration`. A `PutObjectRetention` body with a mode but no date, a date but no mode, or an unknown mode is refused with `400 MalformedXML`; one naming neither removes the retention -- object SSE from `x-amz-server-side-encryption` on `PutObject`, `CopyObject`, and multipart initiation, and form fields on browser/presigned POST, returned by `GetObject` and `HeadObject`. An object stored with neither an SSE header nor an SSE-C key takes its bucket's default encryption (`PutBucketEncryption`), SSE-S3 (`AES256`) when the bucket has none. A multipart upload takes it when it completes, a known deviation from AWS: the [`CreateMultipartUpload` response](https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateMultipartUpload.html) carries `x-amz-server-side-encryption`, so an upload's encryption is set when it is created (that it cannot change later is inferred), and an `UploadPartCopy` response reports only the encryption the upload was created with. A `CopyObject` without encryption headers takes the destination bucket's default, not the source object's encryption, as the [`CopyObject` reference](https://docs.aws.amazon.com/AmazonS3/latest/API/API_CopyObject.html) says -- SSE-KMS key IDs from `x-amz-server-side-encryption-aws-kms-key-id` on `PutObject`, browser/presigned POST, and multipart initiation, and from `CopyObject` (only when the request names the encryption; a copy without one takes the destination bucket's default; the parts of an `UploadPartCopy` take the encryption of their upload), returned by `PutObject`, `CopyObject`, `CompleteMultipartUpload`, `GetObject`, and `HeadObject`, and by `UploadPartCopy` for an upload created with an SSE-KMS header. A bucket's default SSE-KMS key is applied as configured but not simulated: `aws:kms` with no key ID, in the request or in the bucket's default encryption, omits the header rather than returning a synthesized `aws/s3` key ARN +- object SSE from `x-amz-server-side-encryption` on `PutObject`, `CopyObject`, and multipart initiation, and form fields on browser/presigned POST, returned by `GetObject` and `HeadObject`. An object stored with neither an SSE header nor an SSE-C key takes its bucket's default encryption (`PutBucketEncryption`), SSE-S3 (`AES256`) when the bucket has none. A multipart upload takes it when it is created, as the [`CreateMultipartUpload` reference](https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateMultipartUpload.html) says of its parts ("the encryption setting of the uploaded parts is set to the default encryption configuration of the destination bucket"): a default changed afterwards does not reach an upload already created (inferred), and the responses of `CreateMultipartUpload`, `UploadPart`, `UploadPartCopy` and `CompleteMultipartUpload` report it. A `CopyObject` without encryption headers takes the destination bucket's default, not the source object's encryption, as the [`CopyObject` reference](https://docs.aws.amazon.com/AmazonS3/latest/API/API_CopyObject.html) says +- SSE-KMS key IDs from `x-amz-server-side-encryption-aws-kms-key-id` on `PutObject`, browser/presigned POST, and multipart initiation, and from `CopyObject` (only when the request names the encryption; a copy without one takes the destination bucket's default; the parts of an `UploadPartCopy` take the encryption of their upload), returned by `PutObject`, `CopyObject`, `CreateMultipartUpload`, `UploadPart`, `UploadPartCopy`, `CompleteMultipartUpload`, `GetObject`, and `HeadObject`. A bucket's default SSE-KMS key is applied as configured but not simulated: `aws:kms` with no key ID, in the request or in the bucket's default encryption, omits the header rather than returning a synthesized `aws/s3` key ARN Current limitations: diff --git a/src/main/java/io/floci/aws/services/s3/S3Controller.java b/src/main/java/io/floci/aws/services/s3/S3Controller.java index caee95f139..1ab473dceb 100644 --- a/src/main/java/io/floci/aws/services/s3/S3Controller.java +++ b/src/main/java/io/floci/aws/services/s3/S3Controller.java @@ -949,10 +949,12 @@ public class S3Controller { httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-key-MD5")); 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(); + MultipartUpload upload = s3Service.getMultipartUpload(bucket, key, uploadId); + ChecksumAlgorithm declared = upload.getChecksumAlgorithm(); if (declared != null) { response.header(declared.headerName(), part.getChecksum().valueFor(declared)); } + appendServerSideEncryptionHeaders(response, upload); appendSseCustomerHeaders(response, httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-algorithm"), httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-key-MD5")); @@ -1723,6 +1725,7 @@ public class S3Controller { response.header("x-amz-checksum-algorithm", upload.getChecksumAlgorithm().name()); response.header("x-amz-checksum-type", upload.getChecksumType().name()); } + appendServerSideEncryptionHeaders(response, upload); appendSseCustomerHeaders(response, upload); return response.build(); } @@ -3260,6 +3263,15 @@ public class S3Controller { appendSseCustomerHeaders(resp, obj.getSseCustomerAlgorithm(), obj.getSseCustomerKeyMd5()); } + private static void appendServerSideEncryptionHeaders(Response.ResponseBuilder response, MultipartUpload upload) { + if (upload.effectiveServerSideEncryption() != null) { + response.header("x-amz-server-side-encryption", upload.effectiveServerSideEncryption()); + } + if (upload.effectiveSseKmsKeyId() != null) { + response.header("x-amz-server-side-encryption-aws-kms-key-id", upload.effectiveSseKmsKeyId()); + } + } + private void appendSseCustomerHeaders(Response.ResponseBuilder resp, MultipartUpload upload) { appendSseCustomerHeaders(resp, upload.getSseCustomerAlgorithm(), upload.getSseCustomerKeyMd5()); } @@ -3411,12 +3423,7 @@ public class S3Controller { } Response.ResponseBuilder response = Response.ok(xml.end("CopyPartResult").build()) .type(MediaType.APPLICATION_XML); - if (destinationUpload.getServerSideEncryption() != null) { - response.header("x-amz-server-side-encryption", destinationUpload.getServerSideEncryption()); - } - if (destinationUpload.getSseKmsKeyId() != null) { - response.header("x-amz-server-side-encryption-aws-kms-key-id", destinationUpload.getSseKmsKeyId()); - } + appendServerSideEncryptionHeaders(response, destinationUpload); appendSseCustomerHeaders(response, httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-algorithm"), httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-key-MD5")); diff --git a/src/main/java/io/floci/aws/services/s3/S3Service.java b/src/main/java/io/floci/aws/services/s3/S3Service.java index 913db40922..04dfb6136e 100644 --- a/src/main/java/io/floci/aws/services/s3/S3Service.java +++ b/src/main/java/io/floci/aws/services/s3/S3Service.java @@ -4841,7 +4841,16 @@ public class S3Service implements Resettable, ResourceProvider { upload.setContentEncoding(objectOptions.getContentEncoding()); upload.setCacheControl(objectOptions.getCacheControl()); upload.setServerSideEncryption(normalizedServerSideEncryption); - upload.setSseKmsKeyId("aws:kms".equals(normalizedServerSideEncryption) ? sseKmsKeyId : null); + upload.setSseKmsKeyId("aws:kms".equals(normalizedServerSideEncryption) + || "aws:kms:dsse".equals(normalizedServerSideEncryption) ? sseKmsKeyId : null); + if (normalizedServerSideEncryption == null && customerKey == null) { + ObjectEncryption encryption = defaultEncryption(destination); + upload.setDefaultServerSideEncryption(encryption.algorithm()); + upload.setDefaultSseKmsKeyId("aws:kms".equals(encryption.algorithm()) + || "aws:kms:dsse".equals(encryption.algorithm()) + ? encryption.kmsKeyId() + : null); + } if (customerKey != null) { upload.setSseCustomerAlgorithm(customerKey.algorithm()); upload.setSseCustomerKeyMd5(customerKey.keyMd5()); @@ -5368,8 +5377,8 @@ public class S3Service implements Resettable, ResourceProvider { .withContentDisposition(upload.getContentDisposition()) .withContentEncoding(upload.getContentEncoding()) .withCacheControl(upload.getCacheControl()) - .withServerSideEncryption(upload.getServerSideEncryption()) - .withSseKmsKeyId(upload.getSseKmsKeyId()) + .withServerSideEncryption(upload.effectiveServerSideEncryption()) + .withSseKmsKeyId(upload.effectiveSseKmsKeyId()) .withAcl(upload.getAcl()) .withGrantRead(upload.getGrantRead()) .withGrantWrite(upload.getGrantWrite()) @@ -5634,7 +5643,7 @@ public class S3Service implements Resettable, ResourceProvider { /** The encryption condition keys of an upload's parts and completion: what it recorded when created. */ public Map> multipartEncryptionConditions(String bucket, String key, String uploadId) { MultipartUpload upload = getMultipartUpload(bucket, key, uploadId); - return S3ObjectActions.encryptionConditions(upload.getServerSideEncryption(), upload.getSseKmsKeyId()); + return S3ObjectActions.encryptionConditions(upload.effectiveServerSideEncryption(), upload.effectiveSseKmsKeyId()); } /** Checks a part or a completion against the bucket policy by the encryption its upload recorded. */ diff --git a/src/main/java/io/floci/aws/services/s3/model/MultipartUpload.java b/src/main/java/io/floci/aws/services/s3/model/MultipartUpload.java index 7aacf1192a..efb18869af 100644 --- a/src/main/java/io/floci/aws/services/s3/model/MultipartUpload.java +++ b/src/main/java/io/floci/aws/services/s3/model/MultipartUpload.java @@ -21,6 +21,11 @@ public class MultipartUpload { private String cacheControl; private String serverSideEncryption; private String sseKmsKeyId; + // What the bucket's default encryption gave an upload whose request named none, when it was created. The two + // fields above hold only what the request sent: the CreateMultipartUpload request is judged by its own headers, + // its parts and completion by the effective encryption. + private String defaultServerSideEncryption; + private String defaultSseKmsKeyId; private String sseCustomerAlgorithm; private String sseCustomerKeyMd5; private String acl; @@ -87,6 +92,23 @@ public class MultipartUpload { public String getSseKmsKeyId() { return sseKmsKeyId; } public void setSseKmsKeyId(String sseKmsKeyId) { this.sseKmsKeyId = sseKmsKeyId; } + public String getDefaultServerSideEncryption() { return defaultServerSideEncryption; } + public void setDefaultServerSideEncryption(String defaultServerSideEncryption) { + this.defaultServerSideEncryption = defaultServerSideEncryption; + } + + public String getDefaultSseKmsKeyId() { return defaultSseKmsKeyId; } + public void setDefaultSseKmsKeyId(String defaultSseKmsKeyId) { this.defaultSseKmsKeyId = defaultSseKmsKeyId; } + + /** The encryption the upload's parts and object take: what the request sent, else the bucket default taken at creation. */ + public String effectiveServerSideEncryption() { + return serverSideEncryption != null ? serverSideEncryption : defaultServerSideEncryption; + } + + public String effectiveSseKmsKeyId() { + return serverSideEncryption != null ? sseKmsKeyId : defaultSseKmsKeyId; + } + public String getSseCustomerAlgorithm() { return sseCustomerAlgorithm; } public void setSseCustomerAlgorithm(String sseCustomerAlgorithm) { this.sseCustomerAlgorithm = sseCustomerAlgorithm; } diff --git a/src/test/java/io/floci/aws/services/s3/S3BucketPolicyEnforcementIntegrationTest.java b/src/test/java/io/floci/aws/services/s3/S3BucketPolicyEnforcementIntegrationTest.java index a55ea4dc72..4fe11019ac 100644 --- a/src/test/java/io/floci/aws/services/s3/S3BucketPolicyEnforcementIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/s3/S3BucketPolicyEnforcementIntegrationTest.java @@ -936,11 +936,8 @@ class S3BucketPolicyEnforcementIntegrationTest { /** * A multipart upload under the same two statements. A part sends neither header, so it is judged - * by what its upload recorded. AWS does not document this; it was measured on S3 on 2026-10-30, - * each case here as S3 answered it. A case whose upload is opened before the policy stands uses a - * bucket with no default encryption: S3 records the bucket's default on an upload that asks for - * none, where Floci records only what the request asks for, so on a bucket defaulting to - * {@code aws:kms} such a part is admitted on S3 and refused here. + * by what its upload recorded, the bucket's default included for an upload that asked for none. + * AWS does not document this; it was measured on S3 on 2026-10-30, each case here as S3 answered it. */ @Test void anUploadOpenedWithTheBucketsKeyHasItsPartsAdmittedAndStandsUnderThatKey() { @@ -976,7 +973,7 @@ class S3BucketPolicyEnforcementIntegrationTest { } @Test - void aPartOfAnUploadThatRecordedNoEncryptionIsRefusedAndNoObjectStands() { + void aPartOfAnUploadThatRecordedTheSseS3DefaultIsRefusedAndNoObjectStands() { EncryptionFixture fixture = bucketWithoutDefaultEncryption(); String uploadId = given().filter(fixture.user().signer()).post("/" + fixture.bucket() + "/plain.bin?uploads") .then().statusCode(200) @@ -991,6 +988,22 @@ class S3BucketPolicyEnforcementIntegrationTest { .then().statusCode(204); } + @Test + void aPartOfAnUploadOpenedWithNoEncryptionIsJudgedByTheDefaultItRecorded() { + String suffix = suffix(); + EncryptionFixture fixture = encryptionBucket("kms-default-" + suffix, + encryptionUser("kms-default-user-" + suffix, ALLOW_S3_AND_KMS), suffix, true); + String uploadId = given().filter(fixture.user().signer()).post("/" + fixture.bucket() + "/default.bin?uploads") + .then().statusCode(200) + .extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId"); + putKmsOnlyPolicy(fixture); + + uploadPart(fixture.user().signer(), fixture, "default.bin", uploadId).then().statusCode(200); + + given().filter(fixture.owner()).delete("/" + fixture.bucket() + "/default.bin?uploadId=" + uploadId) + .then().statusCode(204); + } + @Test void aPartOfAnUploadThatRecordedAnotherEncryptionIsRefused() { EncryptionFixture fixture = bucketWithoutDefaultEncryption(); diff --git a/src/test/java/io/floci/aws/services/s3/S3DefaultEncryptionIntegrationTest.java b/src/test/java/io/floci/aws/services/s3/S3DefaultEncryptionIntegrationTest.java index 55ff5b0b9f..fe17d56dd7 100644 --- a/src/test/java/io/floci/aws/services/s3/S3DefaultEncryptionIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/s3/S3DefaultEncryptionIntegrationTest.java @@ -8,12 +8,16 @@ import org.junit.jupiter.api.Test; import software.amazon.awssdk.core.ResponseInputStream; import software.amazon.awssdk.core.sync.RequestBody; import software.amazon.awssdk.services.s3.S3Client; +import software.amazon.awssdk.services.s3.model.CompleteMultipartUploadResponse; import software.amazon.awssdk.services.s3.model.CompletedPart; import software.amazon.awssdk.services.s3.model.CopyObjectResponse; +import software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse; import software.amazon.awssdk.services.s3.model.GetObjectResponse; import software.amazon.awssdk.services.s3.model.HeadObjectResponse; import software.amazon.awssdk.services.s3.model.PutObjectResponse; import software.amazon.awssdk.services.s3.model.ServerSideEncryption; +import software.amazon.awssdk.services.s3.model.UploadPartCopyResponse; +import software.amazon.awssdk.services.s3.model.UploadPartResponse; import java.util.ArrayList; import java.util.List; @@ -45,9 +49,12 @@ class S3DefaultEncryptionIntegrationTest { @AfterEach void deleteBucketsAndCloseClient() { - S3TestClients.deleteBucket(s3, bucket); - otherBuckets.forEach(other -> S3TestClients.deleteBucket(s3, other)); - s3.close(); + try { + S3TestClients.deleteBucket(s3, bucket); + otherBuckets.forEach(other -> S3TestClients.deleteBucket(s3, other)); + } finally { + s3.close(); + } } @Test @@ -89,6 +96,65 @@ class S3DefaultEncryptionIntegrationTest { assertEquals(KMS_KEY, head.ssekmsKeyId()); } + @Test + void aMultipartUploadReportsTheDefaultInEveryResponse() { + useKmsDefault(); + s3.putObject(r -> r.bucket(bucket).key("source"), RequestBody.fromString("source")); + + CreateMultipartUploadResponse created = s3.createMultipartUpload(r -> r.bucket(bucket).key("multipart")); + String uploadId = created.uploadId(); + UploadPartResponse part = s3.uploadPart(r -> r.bucket(bucket).key("multipart").uploadId(uploadId) + .partNumber(1), RequestBody.fromString("part")); + UploadPartCopyResponse copied = s3.uploadPartCopy(r -> r.sourceBucket(bucket).sourceKey("source") + .destinationBucket(bucket).destinationKey("multipart").uploadId(uploadId).partNumber(2)); + + assertEquals(ServerSideEncryption.AWS_KMS, created.serverSideEncryption()); + assertEquals(KMS_KEY, created.ssekmsKeyId()); + assertEquals(ServerSideEncryption.AWS_KMS, part.serverSideEncryption()); + assertEquals(KMS_KEY, part.ssekmsKeyId()); + assertEquals(ServerSideEncryption.AWS_KMS, copied.serverSideEncryption()); + assertEquals(KMS_KEY, copied.ssekmsKeyId()); + s3.abortMultipartUpload(r -> r.bucket(bucket).key("multipart").uploadId(uploadId)); + } + + @Test + void aMultipartUploadKeepsTheDefaultItWasCreatedWith() { + useKmsDefault(); + String uploadId = s3.createMultipartUpload(r -> r.bucket(bucket).key("multipart")).uploadId(); + s3.putBucketEncryption(r -> r.bucket(bucket).serverSideEncryptionConfiguration(c -> c.rules(rule -> + rule.applyServerSideEncryptionByDefault(d -> d.sseAlgorithm(ServerSideEncryption.AES256))))); + + UploadPartResponse part = s3.uploadPart(r -> r.bucket(bucket).key("multipart").uploadId(uploadId) + .partNumber(1), RequestBody.fromString("part")); + CompleteMultipartUploadResponse completed = s3.completeMultipartUpload(r -> r.bucket(bucket) + .key("multipart").uploadId(uploadId).multipartUpload(m -> m.parts( + CompletedPart.builder().partNumber(1).eTag(part.eTag()).build()))); + + assertEquals(ServerSideEncryption.AWS_KMS, part.serverSideEncryption()); + assertEquals(ServerSideEncryption.AWS_KMS, completed.serverSideEncryption()); + assertEquals(KMS_KEY, completed.ssekmsKeyId()); + HeadObjectResponse head = head("multipart"); + assertEquals(ServerSideEncryption.AWS_KMS, head.serverSideEncryption()); + assertEquals(KMS_KEY, head.ssekmsKeyId()); + } + + @Test + void aMultipartUploadWithAnExplicitHeaderReportsIt() { + useKmsDefault(); + + CreateMultipartUploadResponse created = s3.createMultipartUpload(r -> r.bucket(bucket).key("multipart") + .serverSideEncryption(ServerSideEncryption.AES256)); + String uploadId = created.uploadId(); + UploadPartResponse part = s3.uploadPart(r -> r.bucket(bucket).key("multipart").uploadId(uploadId) + .partNumber(1), RequestBody.fromString("part")); + + assertEquals(ServerSideEncryption.AES256, created.serverSideEncryption()); + assertNull(created.ssekmsKeyId()); + assertEquals(ServerSideEncryption.AES256, part.serverSideEncryption()); + assertNull(part.ssekmsKeyId()); + s3.abortMultipartUpload(r -> r.bucket(bucket).key("multipart").uploadId(uploadId)); + } + @Test void aCopyIntoAKmsDefaultBucketTakesItsKey() { s3.putObject(r -> r.bucket(bucket).key("source"), RequestBody.fromString("source")); diff --git a/src/test/java/io/floci/aws/services/s3/S3DefaultEncryptionServiceTest.java b/src/test/java/io/floci/aws/services/s3/S3DefaultEncryptionServiceTest.java index 34e70cf8dc..73340d37c1 100644 --- a/src/test/java/io/floci/aws/services/s3/S3DefaultEncryptionServiceTest.java +++ b/src/test/java/io/floci/aws/services/s3/S3DefaultEncryptionServiceTest.java @@ -155,6 +155,76 @@ class S3DefaultEncryptionServiceTest { assertNull(s3Service.getObject(BUCKET, "multipart-sse-c.bin").getServerSideEncryption()); } + @Test + void aMultipartUploadTakesTheDefaultWhenItIsCreated() { + s3Service.putBucketEncryption(BUCKET, configuration("aws:kms", KMS_KEY)); + MultipartUpload upload = s3Service.initiateMultipartUpload(BUCKET, "created.bin", + "application/octet-stream", Map.of(), null, new PutObjectOptions()); + + assertEquals("aws:kms", upload.effectiveServerSideEncryption()); + assertEquals(KMS_KEY, upload.effectiveSseKmsKeyId()); + assertNull(upload.getServerSideEncryption()); + assertEquals(Map.of("s3:x-amz-server-side-encryption", List.of("aws:kms"), + "s3:x-amz-server-side-encryption-aws-kms-key-id", List.of(KMS_KEY)), + s3Service.multipartEncryptionConditions(BUCKET, "created.bin", upload.getUploadId())); + + s3Service.putBucketEncryption(BUCKET, configuration("AES256", null)); + s3Service.uploadPart(BUCKET, "created.bin", upload.getUploadId(), 1, DATA); + S3Object completed = s3Service.completeMultipartUpload(BUCKET, "created.bin", upload.getUploadId(), + List.of(1), null, null); + + assertEquals("aws:kms", completed.getServerSideEncryption()); + assertEquals(KMS_KEY, completed.getSseKmsKeyId()); + } + + @Test + void aDualLayerKmsDefaultReachesAMultipartUploadWithItsKey() { + s3Service.putBucketEncryption(BUCKET, configuration("aws:kms:dsse", KMS_KEY)); + MultipartUpload upload = s3Service.initiateMultipartUpload(BUCKET, "dsse.bin", + "application/octet-stream", Map.of(), null, new PutObjectOptions()); + s3Service.uploadPart(BUCKET, "dsse.bin", upload.getUploadId(), 1, DATA); + S3Object completed = s3Service.completeMultipartUpload(BUCKET, "dsse.bin", upload.getUploadId(), + List.of(1), null, null); + + assertEquals("aws:kms:dsse", upload.effectiveServerSideEncryption()); + assertEquals(KMS_KEY, upload.effectiveSseKmsKeyId()); + assertEquals("aws:kms:dsse", completed.getServerSideEncryption()); + assertEquals(KMS_KEY, completed.getSseKmsKeyId()); + } + + @Test + void anExplicitDualLayerHeaderKeepsItsKeyId() { + s3Service.putBucketEncryption(BUCKET, configuration("aws:kms", KMS_KEY)); + MultipartUpload upload = s3Service.initiateMultipartUpload(BUCKET, "explicit-dsse.bin", + "application/octet-stream", Map.of(), null, + new PutObjectOptions().withServerSideEncryption("aws:kms:dsse").withSseKmsKeyId(OTHER_KMS_KEY)); + s3Service.uploadPart(BUCKET, "explicit-dsse.bin", upload.getUploadId(), 1, DATA); + S3Object completed = s3Service.completeMultipartUpload(BUCKET, "explicit-dsse.bin", upload.getUploadId(), + List.of(1), null, null); + + assertEquals("aws:kms:dsse", upload.effectiveServerSideEncryption()); + assertEquals(OTHER_KMS_KEY, upload.effectiveSseKmsKeyId()); + assertEquals("aws:kms:dsse", completed.getServerSideEncryption()); + assertEquals(OTHER_KMS_KEY, completed.getSseKmsKeyId()); + } + + @Test + void anUploadThatSendsItsOwnEncryptionRecordsNoDefault() throws Exception { + s3Service.putBucketEncryption(BUCKET, configuration("aws:kms", KMS_KEY)); + + MultipartUpload explicit = s3Service.initiateMultipartUpload(BUCKET, "explicit.bin", + "application/octet-stream", Map.of(), null, new PutObjectOptions().withServerSideEncryption("AES256")); + MultipartUpload sseC = s3Service.initiateMultipartUpload(BUCKET, "sse-c.bin", + "application/octet-stream", Map.of(), null, customerKey(new PutObjectOptions())); + + assertEquals("AES256", explicit.effectiveServerSideEncryption()); + assertNull(explicit.effectiveSseKmsKeyId()); + assertNull(explicit.getDefaultServerSideEncryption()); + assertNull(sseC.effectiveServerSideEncryption()); + assertNull(sseC.effectiveSseKmsKeyId()); + assertNull(sseC.getDefaultServerSideEncryption()); + } + @Test void aConfigurationWithoutAnAlgorithmFallsBackToSseS3() { s3Service.putBucketEncryption(BUCKET, "");