-
Notifications
You must be signed in to change notification settings - Fork 2.9k
AWS: KeyManagementClient implementation that works with AWS KMS #13136
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 |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 org.apache.iceberg.aws; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
| import java.util.Map; | ||
| import org.apache.iceberg.encryption.KeyManagementClient; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; | ||
| import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariables; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.EnumSource; | ||
| import org.junit.jupiter.params.provider.NullSource; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import software.amazon.awssdk.services.kms.KmsClient; | ||
| import software.amazon.awssdk.services.kms.model.CreateKeyRequest; | ||
| import software.amazon.awssdk.services.kms.model.CreateKeyResponse; | ||
| import software.amazon.awssdk.services.kms.model.DataKeySpec; | ||
| import software.amazon.awssdk.services.kms.model.KeySpec; | ||
| import software.amazon.awssdk.services.kms.model.ScheduleKeyDeletionRequest; | ||
| import software.amazon.awssdk.services.kms.model.ScheduleKeyDeletionResponse; | ||
|
|
||
| @EnabledIfEnvironmentVariables({ | ||
| @EnabledIfEnvironmentVariable(named = AwsIntegTestUtil.AWS_ACCESS_KEY_ID, matches = ".*"), | ||
| @EnabledIfEnvironmentVariable(named = AwsIntegTestUtil.AWS_SECRET_ACCESS_KEY, matches = ".*"), | ||
| @EnabledIfEnvironmentVariable(named = AwsIntegTestUtil.AWS_SESSION_TOKEN, matches = ".*"), | ||
| @EnabledIfEnvironmentVariable(named = AwsIntegTestUtil.AWS_TEST_ACCOUNT_ID, matches = "\\d{12}") | ||
| }) | ||
| public class TestKeyManagementClient { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(TestKeyManagementClient.class); | ||
|
|
||
| private static KmsClient kmsClient; | ||
| private static String keyId; | ||
|
|
||
| @BeforeAll | ||
| public static void beforeClass() { | ||
| kmsClient = AwsClientFactories.defaultFactory().kms(); | ||
| CreateKeyRequest createKeyRequest = | ||
| CreateKeyRequest.builder() | ||
| .keySpec(KeySpec.SYMMETRIC_DEFAULT) | ||
| .description( | ||
| "Iceberg integration test key for " + TestKeyManagementClient.class.getName()) | ||
| .build(); | ||
| CreateKeyResponse response = kmsClient.createKey(createKeyRequest); | ||
| keyId = response.keyMetadata().keyId(); | ||
| } | ||
|
|
||
| @AfterAll | ||
| public static void afterClass() { | ||
| // AWS KMS doesn't allow instant deletion. Keys can be put to pendingDeletion state instead, | ||
| // with a minimum of 7 days until final removal. | ||
| ScheduleKeyDeletionRequest deletionRequest = | ||
| ScheduleKeyDeletionRequest.builder().keyId(keyId).pendingWindowInDays(7).build(); | ||
|
|
||
| ScheduleKeyDeletionResponse deletionResponse = kmsClient.scheduleKeyDeletion(deletionRequest); | ||
| LOG.info( | ||
| "Deletion of test key {} will be finalized at {}", keyId, deletionResponse.deletionDate()); | ||
|
|
||
| try { | ||
| kmsClient.close(); | ||
| } catch (Exception e) { | ||
| LOG.error("Error closing KMS client", e); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testKeyWrapping() { | ||
| try (AwsKeyManagementClient keyManagementClient = new AwsKeyManagementClient()) { | ||
| keyManagementClient.initialize(ImmutableMap.of()); | ||
|
|
||
| ByteBuffer key = ByteBuffer.wrap(new String("super-secret-table-master-key").getBytes()); | ||
| ByteBuffer encryptedKey = keyManagementClient.wrapKey(key, keyId); | ||
|
|
||
| assertThat(keyManagementClient.unwrapKey(encryptedKey, keyId)).isEqualTo(key); | ||
| } | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @NullSource | ||
| @EnumSource( | ||
| value = DataKeySpec.class, | ||
| names = {"AES_128", "AES_256"}) | ||
| public void testKeyGeneration(DataKeySpec dataKeySpec) { | ||
| try (AwsKeyManagementClient keyManagementClient = new AwsKeyManagementClient()) { | ||
| Map<String, String> properties = | ||
| dataKeySpec == null | ||
| ? ImmutableMap.of() | ||
| : ImmutableMap.of(AwsProperties.KMS_DATA_KEY_SPEC, dataKeySpec.name()); | ||
| keyManagementClient.initialize(properties); | ||
| KeyManagementClient.KeyGenerationResult result = keyManagementClient.generateKey(keyId); | ||
|
|
||
| assertThat(keyManagementClient.unwrapKey(result.wrappedKey(), keyId)).isEqualTo(result.key()); | ||
| assertThat(result.key().limit()).isEqualTo(expectedLength(dataKeySpec)); | ||
| } | ||
| } | ||
|
|
||
| private static int expectedLength(DataKeySpec spec) { | ||
| if (DataKeySpec.AES_128.equals(spec)) { | ||
| return 128 / 8; | ||
| } else { | ||
| return 256 / 8; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 org.apache.iceberg.aws; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
| import java.util.Map; | ||
| import org.apache.iceberg.encryption.KeyManagementClient; | ||
| import software.amazon.awssdk.core.SdkBytes; | ||
| import software.amazon.awssdk.services.kms.KmsClient; | ||
| import software.amazon.awssdk.services.kms.model.DataKeySpec; | ||
| import software.amazon.awssdk.services.kms.model.DecryptRequest; | ||
| import software.amazon.awssdk.services.kms.model.DecryptResponse; | ||
| import software.amazon.awssdk.services.kms.model.EncryptRequest; | ||
| import software.amazon.awssdk.services.kms.model.EncryptResponse; | ||
| import software.amazon.awssdk.services.kms.model.EncryptionAlgorithmSpec; | ||
| import software.amazon.awssdk.services.kms.model.GenerateDataKeyRequest; | ||
| import software.amazon.awssdk.services.kms.model.GenerateDataKeyResponse; | ||
|
|
||
| /** | ||
| * Key management client implementation that uses AWS Key Management Service. To be used for | ||
| * encrypting/decrypting keys with a KMS-managed master key, (by referencing its key ID), and for | ||
| * the generation of new encryption keys. | ||
| */ | ||
| public class AwsKeyManagementClient implements KeyManagementClient { | ||
|
|
||
| private KmsClient kmsClient; | ||
| private EncryptionAlgorithmSpec encryptionAlgorithmSpec; | ||
| private DataKeySpec dataKeySpec; | ||
|
|
||
| @Override | ||
| public void initialize(Map<String, String> properties) { | ||
| AwsClientFactory clientFactory = AwsClientFactories.from(properties); | ||
| this.kmsClient = clientFactory.kms(); | ||
|
|
||
| AwsProperties awsProperties = new AwsProperties(properties); | ||
| this.encryptionAlgorithmSpec = awsProperties.kmsEncryptionAlgorithmSpec(); | ||
| this.dataKeySpec = awsProperties.kmsDataKeySpec(); | ||
| } | ||
|
|
||
| @Override | ||
| public ByteBuffer wrapKey(ByteBuffer key, String wrappingKeyId) { | ||
| EncryptRequest request = | ||
| EncryptRequest.builder() | ||
| .keyId(wrappingKeyId) | ||
| .encryptionAlgorithm(encryptionAlgorithmSpec) | ||
| .plaintext(SdkBytes.fromByteBuffer(key)) | ||
| .build(); | ||
|
|
||
| EncryptResponse result = kmsClient.encrypt(request); | ||
| return result.ciphertextBlob().asByteBuffer(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean supportsKeyGeneration() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public KeyGenerationResult generateKey(String wrappingKeyId) { | ||
| GenerateDataKeyRequest request = | ||
| GenerateDataKeyRequest.builder().keyId(wrappingKeyId).keySpec(dataKeySpec).build(); | ||
|
|
||
| GenerateDataKeyResponse response = kmsClient.generateDataKey(request); | ||
| KeyGenerationResult result = | ||
| new KeyGenerationResult( | ||
| response.plaintext().asByteBuffer(), response.ciphertextBlob().asByteBuffer()); | ||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public ByteBuffer unwrapKey(ByteBuffer wrappedKey, String wrappingKeyId) { | ||
| DecryptRequest request = | ||
| DecryptRequest.builder() | ||
| .keyId(wrappingKeyId) | ||
| .encryptionAlgorithm(encryptionAlgorithmSpec) | ||
| .ciphertextBlob(SdkBytes.fromByteBuffer(wrappedKey)) | ||
| .build(); | ||
|
|
||
| DecryptResponse result = kmsClient.decrypt(request); | ||
| return result.plaintext().asByteBuffer(); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| if (kmsClient != null) { | ||
| kmsClient.close(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,7 +24,7 @@ | |
| import java.util.Map; | ||
|
|
||
| /** A minimum client interface to connect to a key management service (KMS). */ | ||
| interface KeyManagementClient extends Serializable, Closeable { | ||
| public interface KeyManagementClient extends Serializable, Closeable { | ||
|
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. I think this will necessarily need to be public so that implementations can reside in other modules, so I do agree with this change. It'd be good if @rdblue can confirm that or if some other implementation pattern was intended here.
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.
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. Definitely looks like something which needs to be public |
||
|
|
||
| /** | ||
| * Wrap a secret key, using a wrapping/master key which is stored in KMS and referenced by an ID. | ||
|
|
@@ -94,7 +94,7 @@ class KeyGenerationResult { | |
| private final ByteBuffer key; | ||
| private final ByteBuffer wrappedKey; | ||
|
|
||
| KeyGenerationResult(ByteBuffer key, ByteBuffer wrappedKey) { | ||
| public KeyGenerationResult(ByteBuffer key, ByteBuffer wrappedKey) { | ||
| this.key = key; | ||
| this.wrappedKey = wrappedKey; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will need to be serializable so that it can be used in a distributed setting (since an
EncryptionManagerinstance can also be serialized, and generally we'd expect anEncryptionManagerto also contain an instance ofKeyManagementClientThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah
KeyManagementClientdoes implementSerializableas is (though looking at the implementation, wonder how necessary that is - it's transient in theStandardEncryptionmanagerboth now and after #7770, cc @ggershinsky)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As @smaheshwar-pltr points it out the class implements
Serializableby interface but I also have doubts whether that's actually needed. KMS interactions for table encryption only concerns the master key which I'd expect to take place on driver / coordinator nodes, encrypting/decrypting the metadata json or manifest lists. Everything else down to the data files will not require a KMS.