-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add support for server-side encryption with customer key to S3 #23533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| */ | ||
| package io.trino.filesystem.s3; | ||
|
|
||
| import io.trino.filesystem.encryption.EncryptionKey; | ||
| import io.trino.filesystem.s3.S3FileSystemConfig.S3SseType; | ||
| import io.trino.memory.context.AggregatedMemoryContext; | ||
| import io.trino.memory.context.LocalMemoryContext; | ||
|
|
@@ -43,7 +44,10 @@ | |
| import java.util.concurrent.Executor; | ||
| import java.util.concurrent.Future; | ||
|
|
||
| import static com.google.common.base.Verify.verify; | ||
| import static io.trino.filesystem.s3.S3FileSystemConfig.ObjectCannedAcl.getCannedAcl; | ||
| import static io.trino.filesystem.s3.S3SseCUtils.encoded; | ||
| import static io.trino.filesystem.s3.S3SseCUtils.md5Checksum; | ||
| import static java.lang.Math.clamp; | ||
| import static java.lang.Math.max; | ||
| import static java.lang.Math.min; | ||
|
|
@@ -69,6 +73,7 @@ final class S3OutputStream | |
| private final String sseKmsKeyId; | ||
| private final ObjectCannedACL cannedAcl; | ||
| private final boolean exclusiveCreate; | ||
| private final Optional<EncryptionKey> key; | ||
|
|
||
| private int currentPartNumber; | ||
| private byte[] buffer = new byte[0]; | ||
|
|
@@ -85,7 +90,7 @@ final class S3OutputStream | |
| // Visibility is ensured by calling get() on inProgressUploadFuture. | ||
| private Optional<String> uploadId = Optional.empty(); | ||
|
|
||
| public S3OutputStream(AggregatedMemoryContext memoryContext, Executor uploadExecutor, S3Client client, S3Context context, S3Location location, boolean exclusiveCreate) | ||
| public S3OutputStream(AggregatedMemoryContext memoryContext, Executor uploadExecutor, S3Client client, S3Context context, S3Location location, boolean exclusiveCreate, Optional<EncryptionKey> key) | ||
| { | ||
| this.memoryContext = memoryContext.newLocalMemoryContext(S3OutputStream.class.getSimpleName()); | ||
| this.uploadExecutor = requireNonNull(uploadExecutor, "uploadExecutor is null"); | ||
|
|
@@ -98,6 +103,9 @@ public S3OutputStream(AggregatedMemoryContext memoryContext, Executor uploadExec | |
| this.sseType = context.sseType(); | ||
| this.sseKmsKeyId = context.sseKmsKeyId(); | ||
| this.cannedAcl = getCannedAcl(context.cannedAcl()); | ||
| this.key = requireNonNull(key, "key is null"); | ||
|
|
||
| verify(key.isEmpty() || sseType == S3SseType.NONE, "Encryption key cannot be used with sse configuration"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the only one place when you touch |
||
| } | ||
|
|
||
| @SuppressWarnings("NumericCastThatLosesPrecision") | ||
|
|
@@ -216,6 +224,11 @@ private void flushBuffer(boolean finished) | |
| if (exclusiveCreate) { | ||
| builder.ifNoneMatch("*"); | ||
| } | ||
| key.ifPresent(encryption -> { | ||
| builder.sseCustomerKey(encoded(encryption)); | ||
| builder.sseCustomerAlgorithm(encryption.algorithm()); | ||
| builder.sseCustomerKeyMD5(md5Checksum(encryption)); | ||
| }); | ||
| switch (sseType) { | ||
| case NONE -> { /* ignored */ } | ||
| case S3 -> builder.serverSideEncryption(AES256); | ||
|
|
@@ -301,6 +314,11 @@ private CompletedPart uploadPage(byte[] data, int length) | |
| .bucket(location.bucket()) | ||
| .key(location.key()) | ||
| .applyMutation(builder -> { | ||
| key.ifPresent(encryption -> { | ||
| builder.sseCustomerKey(encoded(encryption)); | ||
| builder.sseCustomerAlgorithm(encryption.algorithm()); | ||
| builder.sseCustomerKeyMD5(md5Checksum(encryption)); | ||
| }); | ||
| switch (sseType) { | ||
| case NONE -> { /* ignored */ } | ||
| case S3 -> builder.serverSideEncryption(AES256); | ||
|
|
@@ -321,6 +339,11 @@ private CompletedPart uploadPage(byte[] data, int length) | |
| .contentLength((long) length) | ||
| .uploadId(uploadId.get()) | ||
| .partNumber(currentPartNumber) | ||
| .applyMutation(builder -> key.ifPresent(encryption -> { | ||
| builder.sseCustomerKey(encoded(encryption)); | ||
| builder.sseCustomerAlgorithm(encryption.algorithm()); | ||
| builder.sseCustomerKeyMD5(md5Checksum(encryption)); | ||
| })) | ||
| .build(); | ||
|
|
||
| ByteBuffer bytes = ByteBuffer.wrap(data, 0, length); | ||
|
|
@@ -346,6 +369,11 @@ private void finishUpload(String uploadId) | |
| .uploadId(uploadId) | ||
| .multipartUpload(x -> x.parts(parts)) | ||
| .applyMutation(builder -> { | ||
| key.ifPresent(encodingKey -> { | ||
| builder.sseCustomerKey(encoded(encodingKey)); | ||
| builder.sseCustomerAlgorithm(encodingKey.algorithm()); | ||
| builder.sseCustomerKeyMD5(md5Checksum(encodingKey)); | ||
| }); | ||
| if (exclusiveCreate) { | ||
| builder.ifNoneMatch("*"); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.trino.filesystem.s3; | ||
|
|
||
| import io.trino.filesystem.encryption.EncryptionKey; | ||
|
|
||
| import java.security.MessageDigest; | ||
| import java.security.NoSuchAlgorithmException; | ||
| import java.util.Base64; | ||
|
|
||
| final class S3SseCUtils | ||
|
wendigo marked this conversation as resolved.
|
||
| { | ||
| private S3SseCUtils() {} | ||
|
|
||
| public static String encoded(EncryptionKey key) | ||
| { | ||
| return Base64.getEncoder().encodeToString(key.key()); | ||
| } | ||
|
|
||
| public static String md5Checksum(EncryptionKey key) | ||
| { | ||
| try { | ||
| MessageDigest digest = MessageDigest.getInstance("MD5"); | ||
|
wendigo marked this conversation as resolved.
|
||
| digest.update(key.key()); | ||
| return Base64.getEncoder().encodeToString(digest.digest()); | ||
| } | ||
| catch (NoSuchAlgorithmException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.