xanderbailey commented on code in PR #2042: URL: https://github.com/apache/iceberg-rust/pull/2042#discussion_r2701684416
########## crates/iceberg/src/encryption/parquet_key_retriever.rs: ########## @@ -0,0 +1,132 @@ +// 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. + +//! Key retriever implementation for Parquet file decryption. +//! +//! This module provides integration between Iceberg's encryption manager +//! and Parquet's key retrieval API. + +use std::sync::Arc; + +use parquet::encryption::decrypt::KeyRetriever; +use parquet::errors::{ParquetError, Result as ParquetResult}; + +use crate::encryption::EncryptionManager; + +/// Key retriever for Parquet files that integrates with Iceberg's EncryptionManager. +/// +/// This retriever unwraps DEKs from the KMS using the EncryptionManager when +/// Parquet requests decryption keys during file reading. +pub struct IcebergKeyRetriever { + encryption_manager: Arc<EncryptionManager>, + /// Runtime handle for executing async operations in sync context + runtime: tokio::runtime::Handle, +} + +impl IcebergKeyRetriever { + /// Creates a new Iceberg key retriever. + /// + /// # Arguments + /// * `encryption_manager` - The encryption manager to use for unwrapping keys + /// * `runtime` - Tokio runtime handle for async operations + pub fn new( + encryption_manager: Arc<EncryptionManager>, + runtime: tokio::runtime::Handle, + ) -> Self { + Self { + encryption_manager, + runtime, + } + } +} + +impl KeyRetriever for IcebergKeyRetriever { + fn retrieve_key(&self, key_metadata: &[u8]) -> ParquetResult<Vec<u8>> { + // The key_metadata contains Iceberg's StandardKeyMetadata in serialized form + // We need to unwrap the DEK from the KMS using our EncryptionManager + + // Clone what we need for the async block + let encryption_manager = self.encryption_manager.clone(); + let key_metadata = key_metadata.to_vec(); + let handle = self.runtime.clone(); + + // Use spawn_blocking to avoid "cannot block within async" panics + // This is necessary because Parquet calls this sync method from various contexts + let result = std::thread::scope(|s| { + s.spawn(|| { + handle.block_on(async move { + encryption_manager.prepare_decryption(&key_metadata).await Review Comment: prepare_decryption is async so we need the tokio `Handle` here to call the async function in a sync context... -- 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]
