dramaticlly commented on code in PR #18210: URL: https://github.com/apache/iceberg/pull/18210#discussion_r4098744342
########## aliyun/src/main/java/org/apache/iceberg/aliyun/AliyunKeyManagementClient.java: ########## @@ -0,0 +1,212 @@ +/* + * 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.aliyun; + +import com.aliyun.kms20160120.Client; +import com.aliyun.kms20160120.models.DecryptRequest; +import com.aliyun.kms20160120.models.DecryptResponse; +import com.aliyun.kms20160120.models.EncryptRequest; +import com.aliyun.kms20160120.models.EncryptResponse; +import com.aliyun.kms20160120.models.GenerateDataKeyRequest; +import com.aliyun.kms20160120.models.GenerateDataKeyResponse; +import com.aliyun.kms20160120.models.GenerateDataKeyResponseBody; +import com.aliyun.teautil.models.RuntimeOptions; +import java.nio.ByteBuffer; +import java.util.Base64; +import java.util.Map; +import org.apache.iceberg.encryption.KeyManagementClient; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.util.ByteBuffers; +import org.apache.iceberg.util.PropertyUtil; +import org.apache.iceberg.util.SerializableMap; + +/** + * Key management client implementation that uses Alibaba Cloud KMS. Encrypts/decrypts keys with a + * KMS-managed master key (referenced by its key id) and generates new data keys. + */ +public class AliyunKeyManagementClient implements KeyManagementClient { + + /** + * Enables server-side data key generation. When enabled (the default), Iceberg calls {@link + * #generateKey(String)}; set to {@code false} to have Iceberg generate keys locally and {@link Review Comment: iceberg does not seem to call generateKey in existing code base today ########## aliyun/src/main/java/org/apache/iceberg/aliyun/AliyunKeyManagementClient.java: ########## @@ -0,0 +1,212 @@ +/* + * 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.aliyun; + +import com.aliyun.kms20160120.Client; +import com.aliyun.kms20160120.models.DecryptRequest; +import com.aliyun.kms20160120.models.DecryptResponse; +import com.aliyun.kms20160120.models.EncryptRequest; +import com.aliyun.kms20160120.models.EncryptResponse; +import com.aliyun.kms20160120.models.GenerateDataKeyRequest; +import com.aliyun.kms20160120.models.GenerateDataKeyResponse; +import com.aliyun.kms20160120.models.GenerateDataKeyResponseBody; +import com.aliyun.teautil.models.RuntimeOptions; +import java.nio.ByteBuffer; +import java.util.Base64; +import java.util.Map; +import org.apache.iceberg.encryption.KeyManagementClient; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.util.ByteBuffers; +import org.apache.iceberg.util.PropertyUtil; +import org.apache.iceberg.util.SerializableMap; + +/** + * Key management client implementation that uses Alibaba Cloud KMS. Encrypts/decrypts keys with a + * KMS-managed master key (referenced by its key id) and generates new data keys. + */ +public class AliyunKeyManagementClient implements KeyManagementClient { + + /** + * Enables server-side data key generation. When enabled (the default), Iceberg calls {@link + * #generateKey(String)}; set to {@code false} to have Iceberg generate keys locally and {@link + * #wrapKey(ByteBuffer, String)} them via KMS. + */ + public static final String ENABLE_KEY_GENERATION = "kms.client.aliyun.key.generation.enabled"; + + /** Maximum number of attempts (including the first) for each KMS call. */ + public static final String CLIENT_MAX_ATTEMPTS = "kms.client.aliyun.max.attempts"; + + /** Connect timeout in milliseconds for KMS calls. */ + public static final String CLIENT_CONNECT_TIMEOUT_MS = "kms.client.aliyun.connect.timeout.ms"; + + /** Read timeout in milliseconds for KMS calls. */ + public static final String CLIENT_READ_TIMEOUT_MS = "kms.client.aliyun.read.timeout.ms"; + + private static final boolean DEFAULT_ENABLE_KEY_GENERATION = true; + private static final int DEFAULT_MAX_ATTEMPTS = 3; + private static final int DEFAULT_CONNECT_TIMEOUT_MS = 2_000; + private static final int DEFAULT_READ_TIMEOUT_MS = 30_000; + private static final int BACKOFF_PERIOD_MS = 100; + private static final String ALIAS_PREFIX = "alias/"; + private static final String ARN_KEY_SEPARATOR = "key/"; + + private Map<String, String> allProperties; + private String dataKeySpec; + private boolean enableKeyGeneration = DEFAULT_ENABLE_KEY_GENERATION; + private int maxAttempts = DEFAULT_MAX_ATTEMPTS; + private int connectTimeoutMs = DEFAULT_CONNECT_TIMEOUT_MS; + private int readTimeoutMs = DEFAULT_READ_TIMEOUT_MS; + + private transient volatile ClientState state; + + @Override + public void initialize(Map<String, String> properties) { + this.allProperties = SerializableMap.copyOf(properties); + this.dataKeySpec = new AliyunProperties(properties).kmsDataKeySpec(); + this.enableKeyGeneration = + PropertyUtil.propertyAsBoolean( + properties, ENABLE_KEY_GENERATION, DEFAULT_ENABLE_KEY_GENERATION); + this.maxAttempts = + PropertyUtil.propertyAsInt(properties, CLIENT_MAX_ATTEMPTS, DEFAULT_MAX_ATTEMPTS); + this.connectTimeoutMs = + PropertyUtil.propertyAsInt( + properties, CLIENT_CONNECT_TIMEOUT_MS, DEFAULT_CONNECT_TIMEOUT_MS); + this.readTimeoutMs = + PropertyUtil.propertyAsInt(properties, CLIENT_READ_TIMEOUT_MS, DEFAULT_READ_TIMEOUT_MS); + } + + @Override + public boolean supportsKeyGeneration() { + return enableKeyGeneration; + } + + @Override + public KeyGenerationResult generateKey(String wrappingKeyId) { + GenerateDataKeyRequest request = + new GenerateDataKeyRequest().setKeyId(wrappingKeyId).setKeySpec(dataKeySpec); + try { + GenerateDataKeyResponse response = + client().generateDataKeyWithOptions(request, runtimeOptions()); + GenerateDataKeyResponseBody body = response.getBody(); + return new KeyGenerationResult( + base64ToBuffer(body.getPlaintext()), base64ToBuffer(body.getCiphertextBlob())); + } catch (Exception e) { + throw new RuntimeException("Failed to generate data key with Aliyun KMS", e); + } + } + + @Override + public ByteBuffer wrapKey(ByteBuffer key, String wrappingKeyId) { + if (enableKeyGeneration) { Review Comment: today the generateKey is not exercised in the iceberg code base, if enableKeyGeneration is default to true , the wrapKey called by StandardEncryptionManager.keyEncryptionKeyID() will always throw exception ########## gradle/libs.versions.toml: ########## @@ -22,6 +22,7 @@ [versions] activation = "1.1.1" aliyun-credentials-java = "0.3.12" Review Comment: I think we might need to bump this one as well to 1.0.3? ########## aliyun/src/main/java/org/apache/iceberg/aliyun/AliyunKeyManagementClient.java: ########## @@ -0,0 +1,212 @@ +/* + * 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.aliyun; + +import com.aliyun.kms20160120.Client; +import com.aliyun.kms20160120.models.DecryptRequest; +import com.aliyun.kms20160120.models.DecryptResponse; +import com.aliyun.kms20160120.models.EncryptRequest; +import com.aliyun.kms20160120.models.EncryptResponse; +import com.aliyun.kms20160120.models.GenerateDataKeyRequest; +import com.aliyun.kms20160120.models.GenerateDataKeyResponse; +import com.aliyun.kms20160120.models.GenerateDataKeyResponseBody; +import com.aliyun.teautil.models.RuntimeOptions; +import java.nio.ByteBuffer; +import java.util.Base64; +import java.util.Map; +import org.apache.iceberg.encryption.KeyManagementClient; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.util.ByteBuffers; +import org.apache.iceberg.util.PropertyUtil; +import org.apache.iceberg.util.SerializableMap; + +/** + * Key management client implementation that uses Alibaba Cloud KMS. Encrypts/decrypts keys with a + * KMS-managed master key (referenced by its key id) and generates new data keys. + */ +public class AliyunKeyManagementClient implements KeyManagementClient { + + /** + * Enables server-side data key generation. When enabled (the default), Iceberg calls {@link + * #generateKey(String)}; set to {@code false} to have Iceberg generate keys locally and {@link + * #wrapKey(ByteBuffer, String)} them via KMS. + */ + public static final String ENABLE_KEY_GENERATION = "kms.client.aliyun.key.generation.enabled"; + + /** Maximum number of attempts (including the first) for each KMS call. */ + public static final String CLIENT_MAX_ATTEMPTS = "kms.client.aliyun.max.attempts"; + + /** Connect timeout in milliseconds for KMS calls. */ + public static final String CLIENT_CONNECT_TIMEOUT_MS = "kms.client.aliyun.connect.timeout.ms"; + + /** Read timeout in milliseconds for KMS calls. */ + public static final String CLIENT_READ_TIMEOUT_MS = "kms.client.aliyun.read.timeout.ms"; Review Comment: wondering if those shall go into AliyunProperties like data key spec? ########## aliyun/src/main/java/org/apache/iceberg/aliyun/AliyunProperties.java: ########## @@ -76,11 +76,31 @@ public class AliyunProperties implements Serializable { */ public static final String OSS_STAGING_DIRECTORY = "oss.staging-dir"; + /** + * The region id used to derive a service endpoint, for example {@code cn-hangzhou}. Used by the + * Aliyun KMS client to resolve {@code kms.<region>.aliyuncs.com}. + */ + public static final String CLIENT_REGION = "client.region"; + + /** + * Overrides the KMS endpoint. Defaults to the region-derived {@code kms.<region>.aliyuncs.com}. + * Set to a KMS Instance (DKMS) endpoint for keys that live in a KMS Instance. + */ + public static final String CLIENT_KMS_ENDPOINT = "client.kms-endpoint"; + + /** The data key spec used when generating data keys with Aliyun KMS: AES_256 or AES_128. */ + public static final String KMS_DATA_KEY_SPEC = "kms.client.aliyun.generation.data_key_spec"; Review Comment: for the value we probably want consistent hypes instead of underscore like all existing -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
