|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.iceberg.aws; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | + |
| 23 | +import java.nio.ByteBuffer; |
| 24 | +import java.util.Map; |
| 25 | +import org.apache.iceberg.encryption.KeyManagementClient; |
| 26 | +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; |
| 27 | +import org.junit.jupiter.api.AfterAll; |
| 28 | +import org.junit.jupiter.api.BeforeAll; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; |
| 31 | +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariables; |
| 32 | +import org.junit.jupiter.params.ParameterizedTest; |
| 33 | +import org.junit.jupiter.params.provider.EnumSource; |
| 34 | +import org.junit.jupiter.params.provider.NullSource; |
| 35 | +import org.slf4j.Logger; |
| 36 | +import org.slf4j.LoggerFactory; |
| 37 | +import software.amazon.awssdk.services.kms.KmsClient; |
| 38 | +import software.amazon.awssdk.services.kms.model.CreateKeyRequest; |
| 39 | +import software.amazon.awssdk.services.kms.model.CreateKeyResponse; |
| 40 | +import software.amazon.awssdk.services.kms.model.DataKeySpec; |
| 41 | +import software.amazon.awssdk.services.kms.model.KeySpec; |
| 42 | +import software.amazon.awssdk.services.kms.model.ScheduleKeyDeletionRequest; |
| 43 | +import software.amazon.awssdk.services.kms.model.ScheduleKeyDeletionResponse; |
| 44 | + |
| 45 | +@EnabledIfEnvironmentVariables({ |
| 46 | + @EnabledIfEnvironmentVariable(named = AwsIntegTestUtil.AWS_ACCESS_KEY_ID, matches = ".*"), |
| 47 | + @EnabledIfEnvironmentVariable(named = AwsIntegTestUtil.AWS_SECRET_ACCESS_KEY, matches = ".*"), |
| 48 | + @EnabledIfEnvironmentVariable(named = AwsIntegTestUtil.AWS_SESSION_TOKEN, matches = ".*"), |
| 49 | + @EnabledIfEnvironmentVariable(named = AwsIntegTestUtil.AWS_TEST_ACCOUNT_ID, matches = "\\d{12}") |
| 50 | +}) |
| 51 | +public class TestKeyManagementClient { |
| 52 | + |
| 53 | + private static final Logger LOG = LoggerFactory.getLogger(TestKeyManagementClient.class); |
| 54 | + |
| 55 | + private static KmsClient kmsClient; |
| 56 | + private static String keyId; |
| 57 | + |
| 58 | + @BeforeAll |
| 59 | + public static void beforeClass() { |
| 60 | + kmsClient = AwsClientFactories.defaultFactory().kms(); |
| 61 | + CreateKeyRequest createKeyRequest = |
| 62 | + CreateKeyRequest.builder() |
| 63 | + .keySpec(KeySpec.SYMMETRIC_DEFAULT) |
| 64 | + .description( |
| 65 | + "Iceberg integration test key for " + TestKeyManagementClient.class.getName()) |
| 66 | + .build(); |
| 67 | + CreateKeyResponse response = kmsClient.createKey(createKeyRequest); |
| 68 | + keyId = response.keyMetadata().keyId(); |
| 69 | + } |
| 70 | + |
| 71 | + @AfterAll |
| 72 | + public static void afterClass() { |
| 73 | + // AWS KMS doesn't allow instant deletion. Keys can be put to pendingDeletion state instead, |
| 74 | + // with a minimum of 7 days until final removal. |
| 75 | + ScheduleKeyDeletionRequest deletionRequest = |
| 76 | + ScheduleKeyDeletionRequest.builder().keyId(keyId).pendingWindowInDays(7).build(); |
| 77 | + |
| 78 | + ScheduleKeyDeletionResponse deletionResponse = kmsClient.scheduleKeyDeletion(deletionRequest); |
| 79 | + LOG.info( |
| 80 | + "Deletion of test key {} will be finalized at {}", keyId, deletionResponse.deletionDate()); |
| 81 | + |
| 82 | + try { |
| 83 | + kmsClient.close(); |
| 84 | + } catch (Exception e) { |
| 85 | + LOG.error("Error closing KMS client", e); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testKeyWrapping() { |
| 91 | + try (AwsKeyManagementClient keyManagementClient = new AwsKeyManagementClient()) { |
| 92 | + keyManagementClient.initialize(ImmutableMap.of()); |
| 93 | + |
| 94 | + ByteBuffer key = ByteBuffer.wrap(new String("super-secret-table-master-key").getBytes()); |
| 95 | + ByteBuffer encryptedKey = keyManagementClient.wrapKey(key, keyId); |
| 96 | + |
| 97 | + assertThat(keyManagementClient.unwrapKey(encryptedKey, keyId)).isEqualTo(key); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @ParameterizedTest |
| 102 | + @NullSource |
| 103 | + @EnumSource( |
| 104 | + value = DataKeySpec.class, |
| 105 | + names = {"AES_128", "AES_256"}) |
| 106 | + public void testKeyGeneration(DataKeySpec dataKeySpec) { |
| 107 | + try (AwsKeyManagementClient keyManagementClient = new AwsKeyManagementClient()) { |
| 108 | + Map<String, String> properties = |
| 109 | + dataKeySpec == null |
| 110 | + ? ImmutableMap.of() |
| 111 | + : ImmutableMap.of(AwsProperties.KMS_DATA_KEY_SPEC, dataKeySpec.name()); |
| 112 | + keyManagementClient.initialize(properties); |
| 113 | + KeyManagementClient.KeyGenerationResult result = keyManagementClient.generateKey(keyId); |
| 114 | + |
| 115 | + assertThat(keyManagementClient.unwrapKey(result.wrappedKey(), keyId)).isEqualTo(result.key()); |
| 116 | + assertThat(result.key().limit()).isEqualTo(expectedLength(dataKeySpec)); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private static int expectedLength(DataKeySpec spec) { |
| 121 | + if (DataKeySpec.AES_128.equals(spec)) { |
| 122 | + return 128 / 8; |
| 123 | + } else { |
| 124 | + return 256 / 8; |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments