Skip to content

Commit cedea96

Browse files
committed
Move get_footer_encryption_algorithm into MetadataObjectWriter
1 parent 5aa3880 commit cedea96

File tree

2 files changed

+31
-51
lines changed

2 files changed

+31
-51
lines changed

parquet/src/encryption/encrypt.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use crate::encryption::ciphers::{
2222
};
2323
use crate::errors::{ParquetError, Result};
2424
use crate::file::column_crypto_metadata::{ColumnCryptoMetaData, EncryptionWithColumnKey};
25-
use crate::format::{AesGcmV1, EncryptionAlgorithm};
2625
use crate::schema::types::{ColumnDescPtr, SchemaDescriptor};
2726
use crate::thrift::TSerializable;
2827
use ring::rand::{SecureRandom, SystemRandom};
@@ -363,27 +362,6 @@ impl FileEncryptor {
363362
Some(column_key) => Ok(Box::new(RingGcmBlockEncryptor::new(column_key.key())?)),
364363
}
365364
}
366-
367-
/// Get encryption algorithm for the plaintext footer
368-
pub(crate) fn get_footer_encryptor_for_plaintext(&self) -> Result<Option<EncryptionAlgorithm>> {
369-
if !self.properties.encrypt_footer() {
370-
let supply_aad_prefix = self
371-
.properties
372-
.aad_prefix()
373-
.map(|_| !self.properties.store_aad_prefix());
374-
let encryption_algorithm = Some(EncryptionAlgorithm::AESGCMV1(AesGcmV1 {
375-
aad_prefix: if self.properties.store_aad_prefix() {
376-
self.properties.aad_prefix().cloned()
377-
} else {
378-
None
379-
},
380-
aad_file_unique: Some(self.aad_file_unique().clone()),
381-
supply_aad_prefix,
382-
}));
383-
return Ok(encryption_algorithm);
384-
}
385-
Ok(None)
386-
}
387365
}
388366

389367
/// Write an encrypted Thrift serializable object

parquet/src/file/metadata/writer.rs

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ use crate::errors::Result;
2828
use crate::file::metadata::{KeyValue, ParquetMetaData};
2929
use crate::file::page_index::index::Index;
3030
use crate::file::writer::{get_file_magic, TrackedWrite};
31+
use crate::format::EncryptionAlgorithm;
3132
#[cfg(feature = "encryption")]
32-
use crate::format::{AesGcmV1, ColumnCryptoMetaData, EncryptionAlgorithm};
33+
use crate::format::{AesGcmV1, ColumnCryptoMetaData};
3334
use crate::format::{ColumnChunk, ColumnIndex, FileMetaData, OffsetIndex, RowGroup};
3435
use crate::schema::types;
3536
use crate::schema::types::{SchemaDescPtr, SchemaDescriptor, TypePtr};
@@ -144,15 +145,6 @@ impl<'a, W: Write> ThriftMetadataWriter<'a, W> {
144145
.object_writer
145146
.apply_row_group_encryption(self.row_groups)?;
146147

147-
#[cfg(not(feature = "encryption"))]
148-
let encryption_algorithm = None;
149-
150-
#[cfg(feature = "encryption")]
151-
let encryption_algorithm = match &self.object_writer.file_encryptor {
152-
Some(file_encryptor) => file_encryptor.get_footer_encryptor_for_plaintext()?,
153-
_ => None,
154-
};
155-
156148
let mut file_metadata = FileMetaData {
157149
num_rows,
158150
row_groups,
@@ -161,7 +153,7 @@ impl<'a, W: Write> ThriftMetadataWriter<'a, W> {
161153
schema: types::to_thrift(self.schema.as_ref())?,
162154
created_by: self.created_by.clone(),
163155
column_orders,
164-
encryption_algorithm,
156+
encryption_algorithm: self.object_writer.get_footer_encryption_algorithm(),
165157
footer_signing_key_metadata: None,
166158
};
167159

@@ -486,6 +478,10 @@ impl MetadataObjectWriter {
486478
pub fn get_file_magic(&self) -> &[u8; 4] {
487479
get_file_magic()
488480
}
481+
482+
fn get_footer_encryption_algorithm(&self) -> Option<EncryptionAlgorithm> {
483+
None
484+
}
489485
}
490486

491487
/// Implementations of [`MetadataObjectWriter`] methods that rely on encryption being enabled
@@ -506,7 +502,7 @@ impl MetadataObjectWriter {
506502
match self.file_encryptor.as_ref() {
507503
Some(file_encryptor) if file_encryptor.properties().encrypt_footer() => {
508504
// First write FileCryptoMetadata
509-
let crypto_metadata = Self::file_crypto_metadata(file_encryptor)?;
505+
let crypto_metadata = self.file_crypto_metadata()?;
510506
let mut protocol = TCompactOutputProtocol::new(&mut sink);
511507
crypto_metadata.write_to_out_protocol(&mut protocol)?;
512508

@@ -639,25 +635,31 @@ impl MetadataObjectWriter {
639635
}
640636
}
641637

642-
fn file_crypto_metadata(
643-
file_encryptor: &FileEncryptor,
644-
) -> Result<crate::format::FileCryptoMetaData> {
645-
let properties = file_encryptor.properties();
646-
let supply_aad_prefix = properties
647-
.aad_prefix()
648-
.map(|_| !properties.store_aad_prefix());
649-
let encryption_algorithm = AesGcmV1 {
650-
aad_prefix: if properties.store_aad_prefix() {
651-
properties.aad_prefix().cloned()
652-
} else {
653-
None
654-
},
655-
aad_file_unique: Some(file_encryptor.aad_file_unique().clone()),
656-
supply_aad_prefix,
657-
};
638+
fn get_footer_encryption_algorithm(&self) -> Option<EncryptionAlgorithm> {
639+
let file_encryptor = self.file_encryptor.as_ref().unwrap();
640+
if !file_encryptor.properties().encrypt_footer() {
641+
let supply_aad_prefix = file_encryptor
642+
.properties()
643+
.aad_prefix()
644+
.map(|_| !file_encryptor.properties().store_aad_prefix());
645+
let encryption_algorithm = Some(EncryptionAlgorithm::AESGCMV1(AesGcmV1 {
646+
aad_prefix: if file_encryptor.properties().store_aad_prefix() {
647+
file_encryptor.properties().aad_prefix().cloned()
648+
} else {
649+
None
650+
},
651+
aad_file_unique: Some(file_encryptor.aad_file_unique().clone()),
652+
supply_aad_prefix,
653+
}));
654+
return encryption_algorithm;
655+
}
656+
None
657+
}
658658

659+
fn file_crypto_metadata(&self) -> Result<crate::format::FileCryptoMetaData> {
660+
let properties = self.file_encryptor.as_ref().unwrap().properties();
659661
Ok(crate::format::FileCryptoMetaData {
660-
encryption_algorithm: EncryptionAlgorithm::AESGCMV1(encryption_algorithm),
662+
encryption_algorithm: self.get_footer_encryption_algorithm().unwrap(),
661663
key_metadata: properties.footer_key_metadata().cloned(),
662664
})
663665
}

0 commit comments

Comments
 (0)