adamreeve commented on code in PR #7387: URL: https://github.com/apache/arrow-rs/pull/7387#discussion_r2032602525
########## parquet/src/encryption/key_management/key_wrapper.rs: ########## @@ -0,0 +1,147 @@ +// 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. + +use crate::encryption::key_management::crypto_factory::EncryptionConfiguration; +use crate::encryption::key_management::key_encryption::encrypt_encryption_key; +use crate::encryption::key_management::key_material::KeyMaterialBuilder; +use crate::encryption::key_management::kms::KmsConnectionConfig; +use crate::encryption::key_management::kms_manager::KmsManager; +use crate::errors::Result; +use base64::prelude::BASE64_STANDARD; +use base64::Engine; +use ring::rand::{SecureRandom, SystemRandom}; +use std::collections::hash_map::Entry; +use std::collections::HashMap; +use std::sync::Arc; +use std::time::Duration; + +/// Creates key material for data encryption keys +pub(crate) struct KeyWrapper<'a> { + kms_manager: &'a Arc<KmsManager>, + kms_connection_config: Arc<KmsConnectionConfig>, + encryption_configuration: &'a EncryptionConfiguration, + master_key_to_kek: HashMap<String, KeyEncryptionKey>, +} + +impl<'a> KeyWrapper<'a> { + pub fn new( + kms_manager: &'a Arc<KmsManager>, + kms_connection_config: Arc<KmsConnectionConfig>, + encryption_configuration: &'a EncryptionConfiguration, + ) -> Self { + Self { + kms_manager, + kms_connection_config, + encryption_configuration, + master_key_to_kek: Default::default(), + } + } + + pub fn get_key_metadata( + &mut self, + key: &[u8], + master_key_id: &str, + is_footer_key: bool, + ) -> Result<Vec<u8>> { + let key_material_builder = if is_footer_key { + let kms_config = &self.kms_connection_config; + // If instance ID or URL weren't provided, set them to non-empty default values + let mut kms_instance_id = kms_config.kms_instance_id().to_owned(); + if kms_instance_id.is_empty() { + kms_instance_id.push_str("DEFAULT"); + } + let mut kms_instance_url = kms_config.kms_instance_url().to_owned(); + if kms_instance_url.is_empty() { + kms_instance_url.push_str("DEFAULT"); + } + KeyMaterialBuilder::for_footer_key(kms_instance_id, kms_instance_url) + } else { + KeyMaterialBuilder::for_column_key() + }; + + let key_material = if self.encryption_configuration.double_wrapping() { + let kek = match self.master_key_to_kek.entry(master_key_id.to_owned()) { + Entry::Occupied(kek) => kek.into_mut(), + Entry::Vacant(entry) => entry.insert(generate_key_encryption_key( + master_key_id, + self.kms_manager, + &self.kms_connection_config, + self.encryption_configuration.cache_lifetime(), + )?), + }; + + let wrapped_dek = encrypt_encryption_key(key, &kek.key_id, &kek.key)?; + + key_material_builder + .with_double_wrapped_key( + master_key_id.to_owned(), + kek.encoded_key_id.clone(), + kek.wrapped_key.clone(), + wrapped_dek, + ) + .build()? + } else { + let kms_client = self.kms_manager.get_client( + &self.kms_connection_config, + self.encryption_configuration.cache_lifetime(), + )?; + let wrapped = kms_client.wrap_key(key, master_key_id)?; + key_material_builder + .with_single_wrapped_key(master_key_id.to_owned(), wrapped) + .build()? + }; + + let serialized_material = key_material.serialize()?; + + Ok(serialized_material.into_bytes()) + } +} + +fn generate_key_encryption_key( + master_key_id: &str, + kms_manager: &Arc<KmsManager>, + kms_connection_config: &Arc<KmsConnectionConfig>, + cache_lifetime: Option<Duration>, +) -> Result<KeyEncryptionKey> { + let rng = SystemRandom::new(); Review Comment: Yes, this is the `SystemRandom` type from Ring: https://docs.rs/ring/latest/ring/rand/struct.SystemRandom.html It's documented as being "A secure random number generator where the random values come directly from the operating system." It's implemented using the [getrandom](https://crates.io/crates/getrandom) crate, which on Linux uses the `getrandom` system call. The man page for that says that "These bytes can be used to seed user-space random number generators or for cryptographic purposes." -- 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]
