Skip to content

Commit

Permalink
chore: fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
srh committed Sep 11, 2024
1 parent 19b82cb commit 04f21c3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 23 deletions.
42 changes: 26 additions & 16 deletions parquet/src/file/footer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ use crate::schema::types::{self, SchemaDescriptor};
use crate::file::{
encryption::{
decrypt_module, parquet_magic, ParquetEncryptionConfig, ParquetEncryptionKey,
ParquetEncryptionMode,
ParquetEncryptionKeyInfo, RandomFileIdentifier, AAD_FILE_UNIQUE_SIZE,
PARQUET_KEY_HASH_LENGTH,
ParquetEncryptionKeyInfo, ParquetEncryptionMode, RandomFileIdentifier,
AAD_FILE_UNIQUE_SIZE, PARQUET_KEY_HASH_LENGTH,
},
PARQUET_MAGIC_ENCRYPTED_FOOTER_CUBE, PARQUET_MAGIC_UNSUPPORTED_PARE,
};
Expand All @@ -53,20 +52,20 @@ fn select_key(
key_metadata: &Option<Vec<u8>>,
) -> Result<ParquetEncryptionKey> {
if let Some(key_id) = key_metadata {
if key_id.len() != PARQUET_KEY_HASH_LENGTH {
if key_id.len() != PARQUET_KEY_HASH_LENGTH {
return Err(general_err!(
"Unsupported Parquet file. key_metadata field length is not supported"
));
}
let mut key_id_arr = [0u8; PARQUET_KEY_HASH_LENGTH];
key_id_arr.copy_from_slice(&key_id);
let read_keys: &[ParquetEncryptionMode] = encryption_config.read_keys();
for mode in read_keys {
}
let mut key_id_arr = [0u8; PARQUET_KEY_HASH_LENGTH];
key_id_arr.copy_from_slice(&key_id);
let read_keys: &[ParquetEncryptionMode] = encryption_config.read_keys();
for mode in read_keys {
match mode {
ParquetEncryptionMode::Unencrypted => { },
ParquetEncryptionMode::Unencrypted => {}
ParquetEncryptionMode::FooterEncrypted(key_info) => {
if key_info.key.compute_key_hash() == key_id_arr {
return Ok(key_info.key)
return Ok(key_info.key);
}
}
}
Expand Down Expand Up @@ -115,16 +114,26 @@ pub fn parse_metadata<R: ChunkReader>(
let trailing_magic: &[u8] = &default_len_end_buf[default_end_len - 4..];
if trailing_magic == PARQUET_MAGIC {
if let Some(config) = encryption_config {
if !config.read_keys().iter().any(|m| matches!(m, ParquetEncryptionMode::Unencrypted)) {
if !config
.read_keys()
.iter()
.any(|m| matches!(m, ParquetEncryptionMode::Unencrypted))
{
return Err(general_err!("Invalid Parquet file in encrypted mode. File (or at least the Parquet footer) is not encrypted"));
}
}
encrypted_footer = false;
} else if trailing_magic == PARQUET_MAGIC_ENCRYPTED_FOOTER_CUBE {
let has_keys = encryption_config.as_ref().map_or(false,
|config| config.read_keys().iter().any(|m| matches!(m, ParquetEncryptionMode::FooterEncrypted(_))));
let has_keys = encryption_config.as_ref().map_or(false, |config| {
config
.read_keys()
.iter()
.any(|m| matches!(m, ParquetEncryptionMode::FooterEncrypted(_)))
});
if !has_keys {
return Err(general_err!("Invalid Parquet file in unencrypted mode. File is encrypted"));
return Err(general_err!(
"Invalid Parquet file in unencrypted mode. File is encrypted"
));
}
encrypted_footer = true;
} else if trailing_magic == PARQUET_MAGIC_UNSUPPORTED_PARE {
Expand Down Expand Up @@ -174,7 +183,8 @@ pub fn parse_metadata<R: ChunkReader>(

let random_file_identifier: Option<RandomFileIdentifier>;
if encrypted_footer {
let encryption_config: &ParquetEncryptionConfig = encryption_config.as_ref().unwrap();
let encryption_config: &ParquetEncryptionConfig =
encryption_config.as_ref().unwrap();
let file_crypto_metadata = {
let mut prot = TCompactInputProtocol::new(&mut metadata_read);
TFileCryptoMetaData::read_from_in_protocol(&mut prot).map_err(|e| {
Expand Down
24 changes: 17 additions & 7 deletions parquet/src/file/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ mod tests {
use crate::compression::{create_codec, Codec};
use crate::file::encryption::{
generate_random_file_identifier, ParquetEncryptionConfig,
ParquetEncryptionKeyInfo, ParquetEncryptionMode
ParquetEncryptionKeyInfo, ParquetEncryptionMode,
};
use crate::file::reader::Length;
use crate::file::{
Expand Down Expand Up @@ -1367,10 +1367,14 @@ mod tests {

file_writer.close().unwrap();

let encryption_config = encryption_info
.map(|(key_info, _)| ParquetEncryptionConfig::new(vec![ParquetEncryptionMode::FooterEncrypted(key_info)]).unwrap());
let encryption_config = encryption_info.map(|(key_info, _)| {
ParquetEncryptionConfig::new(vec![ParquetEncryptionMode::FooterEncrypted(
key_info,
)])
.unwrap()
});
let reader = assert_send(
SerializedFileReader::new_maybe_encrypted(file, &encryption_config).unwrap()
SerializedFileReader::new_maybe_encrypted(file, &encryption_config).unwrap(),
);
assert_eq!(reader.num_row_groups(), data.len());
assert_eq!(
Expand Down Expand Up @@ -1479,9 +1483,15 @@ mod tests {
let buffer = cursor.into_inner().unwrap();

let reading_cursor = crate::file::serialized_reader::SliceableCursor::new(buffer);
let encryption_config = encryption_info
.map(|(key_info, _)| ParquetEncryptionConfig::new(vec![ParquetEncryptionMode::FooterEncrypted(key_info)]).unwrap());
let reader = SerializedFileReader::new_maybe_encrypted(reading_cursor, &encryption_config).unwrap();
let encryption_config = encryption_info.map(|(key_info, _)| {
ParquetEncryptionConfig::new(vec![ParquetEncryptionMode::FooterEncrypted(
key_info,
)])
.unwrap()
});
let reader =
SerializedFileReader::new_maybe_encrypted(reading_cursor, &encryption_config)
.unwrap();

assert_eq!(reader.num_row_groups(), data.len());
assert_eq!(
Expand Down

0 comments on commit 04f21c3

Please sign in to comment.