xanderbailey commented on code in PR #2339: URL: https://github.com/apache/iceberg-rust/pull/2339#discussion_r3123739408
########## crates/iceberg/src/encryption/kms/memory.rs: ########## @@ -0,0 +1,287 @@ +// 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. + +//! In-memory KMS implementation for testing and development. +//! +//! **WARNING**: This implementation is NOT suitable for production use. +//! Keys are stored in memory only and will be lost when the process exits. + +use std::collections::HashMap; +use std::fmt; +use std::sync::{Arc, PoisonError, RwLock}; + +use async_trait::async_trait; + +use super::KeyManagementClient; +use crate::encryption::{AesGcmCipher, AesKeySize, SecureKey, SensitiveBytes}; +use crate::{Error, ErrorKind, Result}; + +fn lock_error<T>(e: PoisonError<T>) -> Error { + Error::new(ErrorKind::Unexpected, format!("Lock poisoned: {e}")) +} + +/// In-memory KMS for testing. Not suitable for production use. +/// +/// ``` +/// use iceberg::encryption::KeyManagementClient; +/// use iceberg::encryption::kms::InMemoryKeyManagementClient; +/// +/// # async fn example() -> iceberg::Result<()> { +/// let kms = InMemoryKeyManagementClient::new(); +/// kms.add_master_key("my-master-key")?; +/// +/// let dek = vec![0u8; 16]; +/// let wrapped = kms.wrap_key(&dek, "my-master-key").await?; +/// let unwrapped = kms.unwrap_key(&wrapped, "my-master-key").await?; +/// assert_eq!(dek.as_slice(), unwrapped.as_bytes()); +/// # Ok(()) +/// # } +/// ``` +#[derive(Clone)] +pub struct InMemoryKeyManagementClient { + master_keys: Arc<RwLock<HashMap<String, SensitiveBytes>>>, + master_key_size: AesKeySize, +} + +impl fmt::Debug for InMemoryKeyManagementClient { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("InMemoryKeyManagementClient") + .field("master_key_size", &self.master_key_size) + .field("key_count", &self.key_count()) + .finish() + } +} + +impl Default for InMemoryKeyManagementClient { + fn default() -> Self { + Self::new() + } +} + +impl InMemoryKeyManagementClient { + /// Creates a new in-memory KMS with 128-bit AES keys. + pub fn new() -> Self { Review Comment: [1b2777f](https://github.com/apache/iceberg-rust/pull/2339/commits/1b2777f19f896292de2c8558d4906411e0dbb8ca) -- 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]
