diff --git a/docs/services/s3.md b/docs/services/s3.md index 02f7bc0f61..3f381e0455 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 keeps the encryption of a source that has one, a known deviation from AWS: the [`CopyObject` reference](https://docs.aws.amazon.com/AmazonS3/latest/API/API_CopyObject.html) says that "if you don't specify encryption information in your copy request, the encryption setting of the target object is set to the default encryption configuration of the destination bucket". A source with no encryption stored (an SSE-C object, or one stored before the default was applied) takes the destination bucket's default -- 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` (inherited from the source object when the request doesn't specify new SSE headers; 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 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 Current limitations: 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 7f88aca067..913db40922 100644 --- a/src/main/java/io/floci/aws/services/s3/S3Service.java +++ b/src/main/java/io/floci/aws/services/s3/S3Service.java @@ -8707,14 +8707,7 @@ public class S3Service implements Resettable, ResourceProvider { String effectiveCacheControl = replaceMetadata && effectiveOptions.getCacheControl() != null ? effectiveOptions.getCacheControl() : source.getCacheControl(); - String effectiveServerSideEncryption = destinationCustomerKey != null - ? null - : (normalizedServerSideEncryption != null ? normalizedServerSideEncryption : source.getServerSideEncryption()); - String effectiveSseKmsKeyId = "aws:kms".equals(effectiveServerSideEncryption) - ? (normalizedServerSideEncryption != null - ? effectiveOptions.getSseKmsKeyId() - : source.getSseKmsKeyId()) - : null; + String effectiveSseKmsKeyId = effectiveOptions.getSseKmsKeyId(); boolean replaceTags = "REPLACE".equalsIgnoreCase(effectiveOptions.getTaggingDirective()); Map effectiveTags = replaceTags ? effectiveOptions.getReplacementTagging() @@ -8769,7 +8762,7 @@ public class S3Service implements Resettable, ResourceProvider { } result[0] = storeObjectCopy(destBucket, destKey, body, metadata, effectiveContentType, effectiveStorageClass, effectiveContentEncoding, - effectiveContentDisposition, effectiveCacheControl, effectiveServerSideEncryption, + effectiveContentDisposition, effectiveCacheControl, normalizedServerSideEncryption, effectiveSseKmsKeyId, effectiveOptions, copyChecksumAlgorithm, effectiveTags); if (copyAnnotations) { restoreAnnotations(sourceAnnotations, destBucket, destKey, result[0]); @@ -8793,7 +8786,7 @@ public class S3Service implements Resettable, ResourceProvider { .orElseThrow(() -> noSuchBucket())) { result[0] = storeObjectCopy(destBucket, destKey, body, metadata, effectiveContentType, effectiveStorageClass, effectiveContentEncoding, - effectiveContentDisposition, effectiveCacheControl, effectiveServerSideEncryption, + effectiveContentDisposition, effectiveCacheControl, normalizedServerSideEncryption, effectiveSseKmsKeyId, effectiveOptions, copyChecksumAlgorithm, effectiveTags); if (copyAnnotations) { restoreAnnotations(sourceAnnotations, destBucket, destKey, result[0]); 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 997f6f8166..55ff5b0b9f 100644 --- a/src/test/java/io/floci/aws/services/s3/S3DefaultEncryptionIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/s3/S3DefaultEncryptionIntegrationTest.java @@ -9,14 +9,18 @@ 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.CompletedPart; +import software.amazon.awssdk.services.s3.model.CopyObjectResponse; 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 java.util.ArrayList; +import java.util.List; import java.util.UUID; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; /** * S3 encrypts every new object: one uploaded without encryption headers gets the bucket's default @@ -28,6 +32,7 @@ class S3DefaultEncryptionIntegrationTest { private static final String KMS_KEY = "arn:aws:kms:us-east-1:000000000000:key/11111111-2222-3333-4444-555555555555"; + private final List otherBuckets = new ArrayList<>(); private S3Client s3; private String bucket; @@ -39,8 +44,9 @@ class S3DefaultEncryptionIntegrationTest { } @AfterEach - void deleteBucketAndCloseClient() { + void deleteBucketsAndCloseClient() { S3TestClients.deleteBucket(s3, bucket); + otherBuckets.forEach(other -> S3TestClients.deleteBucket(s3, other)); s3.close(); } @@ -83,12 +89,53 @@ class S3DefaultEncryptionIntegrationTest { assertEquals(KMS_KEY, head.ssekmsKeyId()); } + @Test + void aCopyIntoAKmsDefaultBucketTakesItsKey() { + s3.putObject(r -> r.bucket(bucket).key("source"), RequestBody.fromString("source")); + String kmsBucket = otherBucket(); + s3.putBucketEncryption(r -> r.bucket(kmsBucket).serverSideEncryptionConfiguration(c -> c.rules(rule -> + rule.applyServerSideEncryptionByDefault(d -> d.sseAlgorithm(ServerSideEncryption.AWS_KMS) + .kmsMasterKeyID(KMS_KEY))))); + + CopyObjectResponse copy = s3.copyObject(r -> r.sourceBucket(bucket).sourceKey("source") + .destinationBucket(kmsBucket).destinationKey("copy")); + + assertEquals(ServerSideEncryption.AWS_KMS, copy.serverSideEncryption()); + assertEquals(KMS_KEY, copy.ssekmsKeyId()); + HeadObjectResponse head = s3.headObject(r -> r.bucket(kmsBucket).key("copy")); + assertEquals(ServerSideEncryption.AWS_KMS, head.serverSideEncryption()); + assertEquals(KMS_KEY, head.ssekmsKeyId()); + } + + @Test + void aCopyWithAnExplicitHeaderKeepsIt() { + useKmsDefault(); + String sourceBucket = otherBucket(); + s3.putObject(r -> r.bucket(sourceBucket).key("source"), RequestBody.fromString("source")); + + CopyObjectResponse copy = s3.copyObject(r -> r.sourceBucket(sourceBucket).sourceKey("source") + .destinationBucket(bucket).destinationKey("copy").serverSideEncryption(ServerSideEncryption.AES256)); + + assertEquals(ServerSideEncryption.AES256, copy.serverSideEncryption()); + assertNull(copy.ssekmsKeyId()); + HeadObjectResponse head = head("copy"); + assertEquals(ServerSideEncryption.AES256, head.serverSideEncryption()); + assertNull(head.ssekmsKeyId()); + } + private void useKmsDefault() { s3.putBucketEncryption(r -> r.bucket(bucket).serverSideEncryptionConfiguration(c -> c.rules(rule -> rule.applyServerSideEncryptionByDefault(d -> d.sseAlgorithm(ServerSideEncryption.AWS_KMS) .kmsMasterKeyID(KMS_KEY))))); } + private String otherBucket() { + String other = "default-encryption-other-" + UUID.randomUUID(); + s3.createBucket(r -> r.bucket(other)); + otherBuckets.add(other); + return other; + } + private HeadObjectResponse head(String key) { return s3.headObject(r -> r.bucket(bucket).key(key)); } 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 81fc0de051..34e70cf8dc 100644 --- a/src/test/java/io/floci/aws/services/s3/S3DefaultEncryptionServiceTest.java +++ b/src/test/java/io/floci/aws/services/s3/S3DefaultEncryptionServiceTest.java @@ -30,6 +30,7 @@ class S3DefaultEncryptionServiceTest { private static final String BUCKET = "default-encryption-bucket"; private static final String SOURCE = "copy-source-bucket"; private static final String KMS_KEY = "arn:aws:kms:us-east-1:000000000000:key/11111111-2222-3333-4444-555555555555"; + private static final String OTHER_KMS_KEY = "arn:aws:kms:us-east-1:000000000000:key/99999999-8888-7777-6666-555555555555"; private static final byte[] DATA = "data".getBytes(StandardCharsets.UTF_8); @TempDir @@ -205,6 +206,67 @@ class S3DefaultEncryptionServiceTest { } } + @Test + void aCopyTakesTheDestinationBucketsDefaultNotTheSourcesEncryption() { + s3Service.putBucketEncryption(SOURCE, configuration("aws:kms", KMS_KEY)); + s3Service.putObject(SOURCE, "kms.txt", DATA, "text/plain", null, new PutObjectOptions()); + put("sse-s3.txt", new PutObjectOptions()); + + S3Object intoPlainBucket = s3Service.copyObject(SOURCE, "kms.txt", BUCKET, "plain-copy.txt"); + + assertEquals("AES256", intoPlainBucket.getServerSideEncryption()); + assertNull(intoPlainBucket.getSseKmsKeyId()); + + s3Service.putBucketEncryption(BUCKET, configuration("aws:kms", OTHER_KMS_KEY)); + S3Object fromKmsSource = s3Service.copyObject(SOURCE, "kms.txt", BUCKET, "kms-copy.txt"); + S3Object fromSseS3Source = s3Service.copyObject(BUCKET, "sse-s3.txt", BUCKET, "sse-s3-copy.txt"); + + for (S3Object copy : List.of(fromKmsSource, fromSseS3Source)) { + assertEquals("aws:kms", copy.getServerSideEncryption()); + assertEquals(OTHER_KMS_KEY, copy.getSseKmsKeyId()); + } + assertEquals(OTHER_KMS_KEY, s3Service.getObject(BUCKET, "kms-copy.txt").getSseKmsKeyId()); + } + + @Test + void aCopyWithAnExplicitHeaderKeepsIt() { + s3Service.putBucketEncryption(BUCKET, configuration("aws:kms", KMS_KEY)); + put("source.txt", new PutObjectOptions()); + + S3Object aes = s3Service.copyObject(BUCKET, "source.txt", BUCKET, "aes-copy.txt", null, + new CopyObjectOptions().withServerSideEncryption("AES256")); + S3Object kms = s3Service.copyObject(BUCKET, "source.txt", BUCKET, "kms-copy.txt", null, + new CopyObjectOptions().withServerSideEncryption("aws:kms").withSseKmsKeyId(OTHER_KMS_KEY)); + S3Object kmsWithoutKey = s3Service.copyObject(BUCKET, "source.txt", BUCKET, "kms-no-key-copy.txt", null, + new CopyObjectOptions().withServerSideEncryption("aws:kms")); + + assertEquals("AES256", aes.getServerSideEncryption()); + assertNull(aes.getSseKmsKeyId()); + assertEquals("aws:kms", kms.getServerSideEncryption()); + assertEquals(OTHER_KMS_KEY, kms.getSseKmsKeyId()); + assertEquals("aws:kms", kmsWithoutKey.getServerSideEncryption()); + assertNotEquals(KMS_KEY, kmsWithoutKey.getSseKmsKeyId()); + S3Object dsse = s3Service.copyObject(BUCKET, "source.txt", BUCKET, "dsse-copy.txt", null, + new CopyObjectOptions().withServerSideEncryption("aws:kms:dsse").withSseKmsKeyId(OTHER_KMS_KEY)); + assertEquals(OTHER_KMS_KEY, dsse.getSseKmsKeyId()); + } + + @Test + void aCopyEncryptedWithACustomerKeyGetsNoDefault() throws Exception { + s3Service.putBucketEncryption(BUCKET, configuration("aws:kms", KMS_KEY)); + put("source.txt", new PutObjectOptions()); + PutObjectOptions sseC = customerKey(new PutObjectOptions()); + + S3Object copy = s3Service.copyObject(BUCKET, "source.txt", BUCKET, "sse-c-copy.txt", null, + new CopyObjectOptions() + .withSseCustomerAlgorithm(sseC.getSseCustomerAlgorithm()) + .withSseCustomerKey(sseC.getSseCustomerKey()) + .withSseCustomerKeyMd5(sseC.getSseCustomerKeyMd5())); + + assertNull(copy.getServerSideEncryption()); + assertEquals("AES256", copy.getSseCustomerAlgorithm()); + } + private S3Object put(String key, PutObjectOptions options) { return s3Service.putObject(BUCKET, key, DATA, "text/plain", null, options); } diff --git a/src/test/java/io/floci/aws/services/s3/S3IntegrationTest.java b/src/test/java/io/floci/aws/services/s3/S3IntegrationTest.java index 7972775a4c..ce730b0de9 100644 --- a/src/test/java/io/floci/aws/services/s3/S3IntegrationTest.java +++ b/src/test/java/io/floci/aws/services/s3/S3IntegrationTest.java @@ -2058,7 +2058,7 @@ class S3IntegrationTest { @Test @Order(79) - void copyObjectPreservesServerSideEncryption() { + void copyObjectTakesTheDestinationBucketDefaultEncryptionUnlessTheRequestNamesOne() { given() .header("x-amz-copy-source", "/sse-bucket/encrypted.txt") .when() @@ -2078,7 +2078,7 @@ class S3IntegrationTest { .statusCode(200) .header("x-amz-server-side-encryption", equalTo("AES256")); - String kmsKeyId = "arn:aws:kms:us-east-1:000000000000:key/test-key"; + // The source is aws:kms, but sse-bucket has no default encryption configuration: the copy is SSE-S3. given() .header("x-amz-copy-source", "/sse-bucket/kms-encrypted.txt") .when() @@ -2086,12 +2086,32 @@ class S3IntegrationTest { .then() .statusCode(200) .body(containsString("CopyObjectResult")) + .header("x-amz-server-side-encryption", equalTo("AES256")) + .header("x-amz-server-side-encryption-aws-kms-key-id", nullValue()); + + given() + .when() + .head("/sse-bucket/kms-encrypted-copy.txt") + .then() + .statusCode(200) + .header("x-amz-server-side-encryption", equalTo("AES256")) + .header("x-amz-server-side-encryption-aws-kms-key-id", nullValue()); + + String kmsKeyId = "arn:aws:kms:us-east-1:000000000000:key/test-key"; + given() + .header("x-amz-copy-source", "/sse-bucket/encrypted.txt") + .header("x-amz-server-side-encryption", "aws:kms") + .header("x-amz-server-side-encryption-aws-kms-key-id", kmsKeyId) + .when() + .put("/sse-bucket/kms-requested-copy.txt") + .then() + .statusCode(200) .header("x-amz-server-side-encryption", equalTo("aws:kms")) .header("x-amz-server-side-encryption-aws-kms-key-id", equalTo(kmsKeyId)); given() .when() - .head("/sse-bucket/kms-encrypted-copy.txt") + .head("/sse-bucket/kms-requested-copy.txt") .then() .statusCode(200) .header("x-amz-server-side-encryption", equalTo("aws:kms")) @@ -2332,6 +2352,7 @@ class S3IntegrationTest { given().delete("/sse-bucket/aes-with-kms-id.txt"); given().delete("/sse-bucket/kms-encrypted.txt"); given().delete("/sse-bucket/kms-encrypted-copy.txt"); + given().delete("/sse-bucket/kms-requested-copy.txt"); given().delete("/sse-bucket/sse-c.txt"); given().delete("/sse-bucket/sse-c-copy.txt"); given().delete("/sse-bucket");