ggershinsky commented on a change in pull request #3471:
URL: https://github.com/apache/iceberg/pull/3471#discussion_r831961463



##########
File path: 
core/src/main/java/org/apache/iceberg/encryption/EnvelopeEncryptionManager.java
##########
@@ -0,0 +1,139 @@
+/*
+ * 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.encryption;
+
+import java.nio.ByteBuffer;
+import java.security.SecureRandom;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.io.OutputFile;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+
+/**
+ * Encryption manager which in conjunction with a KMS can encrypt {@link 
OutputFile} and decrypt
+ * {@link InputFile}. Envelope encryption uses a key wrapping strategy, where 
a Key Encryption Key (KEK) is used to
+ * wrap or unwrap a Data Encryption Key DEK which is used to encrypt the 
underlying files.
+ *
+ * When generating new DEKs for OutputFiles, this class will first attempt to 
have the KMS generate a new key. If the
+ * KMS does not support key generation a new key will be produced by pulling 
bytes from a {@link SecureRandom} on the
+ * JVM writing the file.
+ */
+public class EnvelopeEncryptionManager implements EncryptionManager {
+  private final EnvelopeConfiguration dataEncryptionConfig;
+  private final boolean nativeFormatEncryption;
+  private final KmsClient kmsClient;
+  private final int dataKeyLength;
+
+  private transient volatile SecureRandom workerRNG = null;
+
+  /**
+   * @param nativeFormatEncryption true for native encryption inside file 
formats (Parquet or ORC), false for generic
+   *                               encryption of file streams
+   * @param dataEncryptionConfig configuration for encryption of data files. 
Future versions will add configuration for
+   *                             encryption of metadata files
+   * @param kmsClient Client of KMS used to wrap/unwrap keys in envelope 
encryption
+   * @param dataKeyLength If DEKs are not generated by KMS, they will be 
randomly generated by Iceberg using this length
+   */
+  public EnvelopeEncryptionManager(boolean nativeFormatEncryption,  
EnvelopeConfiguration dataEncryptionConfig,
+      KmsClient kmsClient, int dataKeyLength) {
+    this.nativeFormatEncryption = nativeFormatEncryption;
+    if (!nativeFormatEncryption) {
+      throw new UnsupportedOperationException("EnvelopeEncryptionManager 
currently only supports encryption " +
+          "provided by the underlying file format.");
+    }
+    Preconditions.checkNotNull(dataEncryptionConfig,
+        "Cannot create EnvelopeEncryptionManager because data encryption 
config is not passed");
+    this.dataEncryptionConfig = dataEncryptionConfig;
+    Preconditions.checkNotNull(dataEncryptionConfig.kekId(),
+        "Cannot create EnvelopeEncryptionManager because table key encryption 
key ID is not specified");
+    Preconditions.checkNotNull(kmsClient,
+        "Cannot create EnvelopeEncryptionManager because KmsClient is null");
+    this.kmsClient = kmsClient;
+    this.dataKeyLength = dataKeyLength;
+  }
+
+  @Override
+  public EncryptedOutputFile encrypt(OutputFile rawOutput) {
+    EnvelopeMetadata metadata = generateEnvelopeMetadata(dataEncryptionConfig);
+
+    if (nativeFormatEncryption) {
+      NativeFileCryptoParameters nativeEncryptParams = 
NativeFileCryptoParameters.create(metadata.dek())
+          .encryptionAlgorithm(metadata.algorithm())
+          .build();
+
+      Preconditions.checkArgument(rawOutput instanceof NativelyEncryptedFile,

Review comment:
       a good point. This is called per data file, so not very often; but 
still, I think, a reason enough to revert this back to an if block.




-- 
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]

Reply via email to