jdockerty commented on code in PR #2026:
URL: https://github.com/apache/iceberg-rust/pull/2026#discussion_r2902427898


##########
crates/iceberg/src/encryption/crypto.rs:
##########
@@ -0,0 +1,458 @@
+// 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.
+
+//! Core cryptographic operations for Iceberg encryption.
+
+use std::fmt;
+use std::str::FromStr;
+
+use aes_gcm::aead::generic_array::typenum::Unsigned;
+use aes_gcm::aead::rand_core::RngCore;
+use aes_gcm::aead::{Aead, AeadCore, KeyInit, KeySizeUser, OsRng, Payload};
+use aes_gcm::{Aes128Gcm, Key, Nonce};
+use zeroize::Zeroizing;
+
+use crate::{Error, ErrorKind, Result};
+
+/// Wrapper for sensitive byte data (encryption keys, DEKs, etc.) that:
+/// - Zeroizes memory on drop
+/// - Redacts content in [`Debug`] output
+/// - Provides only `&[u8]` access via [`as_bytes()`](Self::as_bytes)
+/// - Uses `Box<[u8]>` (immutable boxed slice) since key bytes never grow
+///
+/// Use this type for any struct field that holds plaintext key material.
+/// Because its [`Debug`] impl always prints `[N bytes REDACTED]`, structs
+/// containing `SensitiveBytes` can safely derive or implement `Debug`
+/// without risk of leaking key material.
+#[derive(Clone, PartialEq, Eq)]
+pub struct SensitiveBytes(Zeroizing<Box<[u8]>>);

Review Comment:
   Love this 💯 



##########
crates/iceberg/src/encryption/crypto.rs:
##########
@@ -0,0 +1,383 @@
+// 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.
+
+//! Core cryptographic operations for Iceberg encryption.
+
+use std::str::FromStr;
+
+use aes_gcm::aead::generic_array::typenum::Unsigned;
+use aes_gcm::aead::{Aead, AeadCore, KeyInit, KeySizeUser, OsRng, Payload};
+use aes_gcm::{Aes128Gcm, Key, Nonce};
+use rand::RngCore;
+use zeroize::Zeroizing;
+
+use crate::{Error, ErrorKind, Result};
+
+/// Supported encryption algorithm.
+/// Currently only AES-128-GCM is supported as it's the only algorithm
+/// compatible with arrow-rs Parquet encryption.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum EncryptionAlgorithm {
+    /// AES-128 in GCM mode
+    Aes128Gcm,

Review Comment:
   >  once the arrow PR merges
   
   Not a huge deal, but I wonder whether you can save a bit of extra review 
churn by filling that part in already and having the pre-existing 
`FeatureUnsupported` error in this PR?
   
   
https://github.com/apache/iceberg-rust/blob/854f66d371a59d218498de72b02db7d3282f2643/crates/iceberg/src/error.rs#L59-L62
   
   As I say, this is also probably quite easy to do in a follow-up too once it 
does merge. So not a huge deal 👍 



##########
crates/iceberg/src/encryption/crypto.rs:
##########
@@ -0,0 +1,458 @@
+// 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.
+
+//! Core cryptographic operations for Iceberg encryption.
+
+use std::fmt;
+use std::str::FromStr;
+
+use aes_gcm::aead::generic_array::typenum::Unsigned;
+use aes_gcm::aead::rand_core::RngCore;
+use aes_gcm::aead::{Aead, AeadCore, KeyInit, KeySizeUser, OsRng, Payload};
+use aes_gcm::{Aes128Gcm, Key, Nonce};
+use zeroize::Zeroizing;
+
+use crate::{Error, ErrorKind, Result};
+
+/// Wrapper for sensitive byte data (encryption keys, DEKs, etc.) that:
+/// - Zeroizes memory on drop
+/// - Redacts content in [`Debug`] output
+/// - Provides only `&[u8]` access via [`as_bytes()`](Self::as_bytes)
+/// - Uses `Box<[u8]>` (immutable boxed slice) since key bytes never grow
+///
+/// Use this type for any struct field that holds plaintext key material.
+/// Because its [`Debug`] impl always prints `[N bytes REDACTED]`, structs
+/// containing `SensitiveBytes` can safely derive or implement `Debug`
+/// without risk of leaking key material.
+#[derive(Clone, PartialEq, Eq)]
+pub struct SensitiveBytes(Zeroizing<Box<[u8]>>);
+
+impl SensitiveBytes {
+    /// Wraps the given bytes as sensitive material.
+    pub fn new(bytes: impl Into<Box<[u8]>>) -> Self {
+        Self(Zeroizing::new(bytes.into()))
+    }
+
+    /// Returns the underlying bytes.
+    pub fn as_bytes(&self) -> &[u8] {
+        &self.0
+    }
+
+    /// Returns the number of bytes.
+    pub fn len(&self) -> usize {
+        self.0.len()
+    }
+
+    /// Returns `true` if the byte slice is empty.
+    pub fn is_empty(&self) -> bool {
+        self.0.is_empty()
+    }
+}
+
+impl fmt::Debug for SensitiveBytes {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        write!(f, "[{} bytes REDACTED]", self.0.len())
+    }
+}
+
+/// Supported encryption algorithm.
+/// Currently only AES-128-GCM is supported as it's the only algorithm
+/// compatible with arrow-rs Parquet encryption.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum EncryptionAlgorithm {
+    /// AES-128 in GCM mode
+    Aes128Gcm,
+}
+
+impl EncryptionAlgorithm {
+    /// Returns the key length in bytes for this algorithm.
+    pub fn key_length(&self) -> usize {
+        match self {
+            Self::Aes128Gcm => <Aes128Gcm as KeySizeUser>::KeySize::USIZE,
+        }
+    }
+
+    /// Returns the nonce/IV length in bytes for this algorithm.
+    pub fn nonce_length(&self) -> usize {
+        match self {
+            Self::Aes128Gcm => <Aes128Gcm as AeadCore>::NonceSize::USIZE,
+        }
+    }
+
+    /// Returns the string identifier for this algorithm.
+    pub fn as_str(&self) -> &'static str {
+        match self {
+            Self::Aes128Gcm => "AES_GCM_128",
+        }
+    }
+
+    /// Returns the algorithm for a given DEK length in bytes.
+    ///
+    /// Matches Java's `encryption.data-key-length` property semantics:
+    /// 16 → AES-128-GCM.
+    pub fn from_key_length(len: usize) -> Result<Self> {
+        match len {
+            16 => Ok(Self::Aes128Gcm),
+            _ => Err(Error::new(
+                ErrorKind::DataInvalid,
+                format!("Unsupported data key length: {len} (must be 16)"),
+            )),
+        }
+    }
+}
+
+impl FromStr for EncryptionAlgorithm {
+    type Err = Error;
+
+    fn from_str(s: &str) -> Result<Self> {
+        match s {
+            "AES_GCM_128" | "AES128_GCM" => Ok(Self::Aes128Gcm),
+            _ => Err(Error::new(
+                ErrorKind::DataInvalid,
+                format!("Unsupported encryption algorithm: {s}"),

Review Comment:
   This might be better mapped to the [`FeatureUnsupported` 
error](https://github.com/apache/iceberg-rust/blob/main/crates/iceberg/src/error.rs#L59-L62),
 considering the message is also about an unsupported algorithm. Rather than 
invalid data (although I do think this works anyway)
   
   



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