rdblue commented on code in PR #6884:
URL: https://github.com/apache/iceberg/pull/6884#discussion_r1421829444


##########
core/src/main/java/org/apache/iceberg/encryption/StandardEncryptionManager.java:
##########
@@ -0,0 +1,142 @@
+/*
+ * 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.TableProperties;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.io.OutputFile;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
+import org.apache.iceberg.util.ByteBuffers;
+
+public class StandardEncryptionManager implements EncryptionManager {
+  private final transient KeyManagementClient kmsClient;
+  private final String tableKeyId;
+  private final int dataKeyLength;
+
+  private transient volatile SecureRandom lazyRNG = null;
+
+  class StandardEncryptedOutputFile implements EncryptedOutputFile {
+
+    private final OutputFile encryptingOutputFile;
+
+    private final EncryptionKeyMetadata keyMetadata;
+    private final OutputFile rawOutputFile;
+
+    StandardEncryptedOutputFile(
+        OutputFile encryptingOutputFile,
+        EncryptionKeyMetadata keyMetadata,
+        OutputFile rawOutputFile) {
+      this.encryptingOutputFile = encryptingOutputFile;
+      this.keyMetadata = keyMetadata;
+      this.rawOutputFile = rawOutputFile;
+    }
+
+    @Override
+    public OutputFile encryptingOutputFile() {
+      return encryptingOutputFile;
+    }
+
+    @Override
+    public EncryptionKeyMetadata keyMetadata() {
+      return keyMetadata;
+    }
+
+    @Override
+    public OutputFile rawOutputFile() {
+      return rawOutputFile;
+    }
+  }
+
+  /**
+   * @param tableKeyId table encryption key id
+   * @param dataKeyLength length of data encryption key (16/24/32 bytes)
+   * @param kmsClient Client of KMS used to wrap/unwrap keys in envelope 
encryption
+   */
+  public StandardEncryptionManager(
+      String tableKeyId, int dataKeyLength, KeyManagementClient kmsClient) {
+    Preconditions.checkNotNull(tableKeyId, "Invalid encryption key ID: null");
+    Preconditions.checkNotNull(kmsClient, "Invalid KMS client: null");
+    this.tableKeyId = tableKeyId;
+    this.kmsClient = kmsClient;
+    this.dataKeyLength = dataKeyLength;
+  }
+
+  @Override
+  public EncryptedOutputFile encrypt(OutputFile rawOutput) {
+    ByteBuffer fileDek = ByteBuffer.allocate(dataKeyLength);
+    workerRNG().nextBytes(fileDek.array());
+
+    ByteBuffer aadPrefix = 
ByteBuffer.allocate(TableProperties.ENCRYPTION_AAD_LENGTH_DEFAULT);
+    workerRNG().nextBytes(aadPrefix.array());
+
+    KeyMetadata encryptionMetadata = new KeyMetadata(fileDek, aadPrefix);
+
+    return new StandardEncryptedOutputFile(
+        new AesGcmOutputFile(rawOutput, fileDek.array(), aadPrefix.array()),
+        encryptionMetadata,
+        rawOutput);
+  }
+
+  @Override
+  public InputFile decrypt(EncryptedInputFile encrypted) {
+    KeyMetadata keyMetadata = KeyMetadata.castOrParse(encrypted.keyMetadata());
+
+    byte[] fileDek = ByteBuffers.toByteArray(keyMetadata.encryptionKey());
+    byte[] aadPrefix = ByteBuffers.toByteArray(keyMetadata.aadPrefix());
+
+    return new AesGcmInputFile(encrypted.encryptedInputFile(), fileDek, 
aadPrefix);
+  }
+
+  @Override
+  public Iterable<InputFile> decrypt(Iterable<EncryptedInputFile> encrypted) {
+    // Bulk decrypt is only applied to data files. Returning source input 
files for parquet.
+    return Iterables.transform(encrypted, this::getSourceFile);

Review Comment:
   This is incorrect. Based on the comment, I think the intent was to skip 
creating an `AesGcmInputFile` because you know that this is only called for 
data files. But that's a bad assumption and there is no guarantee it will be 
called that way in the future.
   
   Instead, I think this should use a decrypted `InputFile` that can return the 
underlying `encryptedInputFile` just like the `StandardEncryptedOutputFile` and 
the key metadata above.



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to