xanderbailey commented on code in PR #2383:
URL: https://github.com/apache/iceberg-rust/pull/2383#discussion_r3194831465


##########
crates/iceberg/src/encryption/io.rs:
##########
@@ -0,0 +1,154 @@
+// 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.
+
+//! Encrypted file wrappers for InputFile / OutputFile.
+
+use std::sync::Arc;
+
+use bytes::Bytes;
+
+use super::file_decryptor::AesGcmFileDecryptor;
+use super::file_encryptor::AesGcmFileEncryptor;
+use crate::io::{FileMetadata, FileRead, FileWrite, InputFile, OutputFile};
+
+/// An AGS1 stream-encrypted input file wrapping a plain [`InputFile`].
+///
+/// Transparently decrypts on read.
+pub struct EncryptedInputFile {
+    inner: InputFile,
+    decryptor: Arc<AesGcmFileDecryptor>,
+}
+
+impl EncryptedInputFile {
+    /// Creates a new encrypted input file.
+    pub fn new(inner: InputFile, decryptor: Arc<AesGcmFileDecryptor>) -> Self {
+        Self { inner, decryptor }
+    }
+
+    /// Absolute path of the file.
+    pub fn location(&self) -> &str {
+        self.inner.location()
+    }
+
+    /// Check if file exists.
+    pub async fn exists(&self) -> crate::Result<bool> {
+        self.inner.exists().await
+    }
+
+    /// Fetch and returns metadata of file.
+    ///
+    /// The returned size is the **plaintext** size.
+    pub async fn metadata(&self) -> crate::Result<FileMetadata> {
+        let raw_meta = self.inner.metadata().await?;
+        let plaintext_size = self.decryptor.plaintext_length(raw_meta.size)?;
+        Ok(FileMetadata {
+            size: plaintext_size,
+        })
+    }
+
+    /// Read and returns whole content of file (decrypted plaintext).
+    pub async fn read(&self) -> crate::Result<Bytes> {
+        let meta = self.metadata().await?;
+        let reader = self.reader().await?;
+        reader.read(0..meta.size).await
+    }
+
+    /// Creates a reader that transparently decrypts on each read.
+    pub async fn reader(&self) -> crate::Result<Box<dyn FileRead>> {
+        let raw_meta = self.inner.metadata().await?;
+        let raw_reader = self.inner.reader().await?;
+        self.decryptor.wrap_reader(raw_reader, raw_meta.size)
+    }
+
+    /// Consumes self and returns the underlying plain input file.
+    pub fn into_inner(self) -> InputFile {
+        self.inner
+    }
+}
+
+impl std::fmt::Debug for EncryptedInputFile {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        f.debug_struct("EncryptedInputFile")
+            .field("path", &self.inner.location())
+            .finish_non_exhaustive()
+    }
+}
+
+/// An AGS1 stream-encrypted output file wrapping a plain [`OutputFile`].
+///
+/// Transparently encrypts on write.
+pub struct EncryptedOutputFile {
+    inner: OutputFile,
+    key_metadata: Box<[u8]>,
+    encryptor: Arc<AesGcmFileEncryptor>,
+}
+
+impl EncryptedOutputFile {
+    /// Creates a new encrypted output file.
+    pub fn new(
+        inner: OutputFile,
+        key_metadata: Box<[u8]>,
+        encryptor: Arc<AesGcmFileEncryptor>,

Review Comment:
   This would push dek generation into the `EncryptedOutputFile` which feels 
like we're mixing concerns between io and the EM. i.e. the EM should be 
responsible for generating dek / aad etc and `EncryptedOutputFile` / 
`EncryptedInputFile` should be responsible for reading and writing data?



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