Skip to content

Commit 1dc9760

Browse files
authored
Refactor DecryptionPropertiesBuilder (#7477)
1 parent 11158ba commit 1dc9760

File tree

1 file changed

+58
-39
lines changed

1 file changed

+58
-39
lines changed

parquet/src/encryption/decrypt.rs

Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ use std::sync::Arc;
8989
///
9090
/// // Create decryption properties for reading an encrypted file.
9191
/// // Note that we don't need to specify which columns are encrypted,
92-
/// // this is determined by the file metadata and the required keys will be retrieved
92+
/// // this is determined by the file metadata, and the required keys will be retrieved
9393
/// // dynamically using our key retriever.
9494
/// let decryption_properties = FileDecryptionProperties::with_key_retriever(key_retriever)
9595
/// .build()?;
@@ -293,7 +293,7 @@ impl PartialEq for DecryptionKeys {
293293
/// `FileDecryptionProperties` hold keys and AAD data required to decrypt a Parquet file.
294294
///
295295
/// When reading Arrow data, the `FileDecryptionProperties` should be included in the
296-
/// [`ArrowReaderOptions`](crate::arrow::arrow_reader::ArrowReaderOptions) using
296+
/// [`ArrowReaderOptions`](crate::arrow::arrow_reader::ArrowReaderOptions) using
297297
/// [`with_file_decryption_properties`](crate::arrow::arrow_reader::ArrowReaderOptions::with_file_decryption_properties).
298298
///
299299
/// # Examples
@@ -343,8 +343,10 @@ impl FileDecryptionProperties {
343343

344344
/// Returns a new [`FileDecryptionProperties`] builder that uses a [`KeyRetriever`]
345345
/// to get decryption keys based on key metadata.
346-
pub fn with_key_retriever(key_retriever: Arc<dyn KeyRetriever>) -> DecryptionPropertiesBuilder {
347-
DecryptionPropertiesBuilder::new_with_key_retriever(key_retriever)
346+
pub fn with_key_retriever(
347+
key_retriever: Arc<dyn KeyRetriever>,
348+
) -> DecryptionPropertiesBuilderWithRetriever {
349+
DecryptionPropertiesBuilderWithRetriever::new(key_retriever)
348350
}
349351

350352
/// AAD prefix string uniquely identifies the file and prevents file swapping
@@ -417,8 +419,7 @@ impl std::fmt::Debug for FileDecryptionProperties {
417419
///
418420
/// See [`FileDecryptionProperties`] for example usage.
419421
pub struct DecryptionPropertiesBuilder {
420-
footer_key: Option<Vec<u8>>,
421-
key_retriever: Option<Arc<dyn KeyRetriever>>,
422+
footer_key: Vec<u8>,
422423
column_keys: HashMap<String, Vec<u8>>,
423424
aad_prefix: Option<Vec<u8>>,
424425
footer_signature_verification: bool,
@@ -429,22 +430,7 @@ impl DecryptionPropertiesBuilder {
429430
/// decrypt footer metadata.
430431
pub fn new(footer_key: Vec<u8>) -> DecryptionPropertiesBuilder {
431432
Self {
432-
footer_key: Some(footer_key),
433-
key_retriever: None,
434-
column_keys: HashMap::default(),
435-
aad_prefix: None,
436-
footer_signature_verification: true,
437-
}
438-
}
439-
440-
/// Create a new [`DecryptionPropertiesBuilder`] by providing a [`KeyRetriever`] that
441-
/// can be used to get decryption keys based on key metadata.
442-
pub fn new_with_key_retriever(
443-
key_retriever: Arc<dyn KeyRetriever>,
444-
) -> DecryptionPropertiesBuilder {
445-
Self {
446-
footer_key: None,
447-
key_retriever: Some(key_retriever),
433+
footer_key,
448434
column_keys: HashMap::default(),
449435
aad_prefix: None,
450436
footer_signature_verification: true,
@@ -453,23 +439,10 @@ impl DecryptionPropertiesBuilder {
453439

454440
/// Finalize the builder and return created [`FileDecryptionProperties`]
455441
pub fn build(self) -> Result<FileDecryptionProperties> {
456-
let keys = match (self.footer_key, self.key_retriever) {
457-
(Some(footer_key), None) => DecryptionKeys::Explicit(ExplicitDecryptionKeys {
458-
footer_key,
459-
column_keys: self.column_keys,
460-
}),
461-
(None, Some(key_retriever)) => {
462-
if !self.column_keys.is_empty() {
463-
return Err(general_err!(
464-
"Cannot specify column keys directly when using a key retriever"
465-
));
466-
}
467-
DecryptionKeys::ViaRetriever(key_retriever)
468-
}
469-
_ => {
470-
unreachable!()
471-
}
472-
};
442+
let keys = DecryptionKeys::Explicit(ExplicitDecryptionKeys {
443+
footer_key: self.footer_key,
444+
column_keys: self.column_keys,
445+
});
473446
Ok(FileDecryptionProperties {
474447
keys,
475448
aad_prefix: self.aad_prefix,
@@ -515,6 +488,52 @@ impl DecryptionPropertiesBuilder {
515488
}
516489
}
517490

491+
/// Builder for [`FileDecryptionProperties`] that uses a [`KeyRetriever`]
492+
///
493+
/// See the [`KeyRetriever`] documentation for example usage.
494+
pub struct DecryptionPropertiesBuilderWithRetriever {
495+
key_retriever: Arc<dyn KeyRetriever>,
496+
aad_prefix: Option<Vec<u8>>,
497+
footer_signature_verification: bool,
498+
}
499+
500+
impl DecryptionPropertiesBuilderWithRetriever {
501+
/// Create a new [`DecryptionPropertiesBuilderWithRetriever`] by providing a [`KeyRetriever`] that
502+
/// can be used to get decryption keys based on key metadata.
503+
pub fn new(key_retriever: Arc<dyn KeyRetriever>) -> DecryptionPropertiesBuilderWithRetriever {
504+
Self {
505+
key_retriever,
506+
aad_prefix: None,
507+
footer_signature_verification: true,
508+
}
509+
}
510+
511+
/// Finalize the builder and return created [`FileDecryptionProperties`]
512+
pub fn build(self) -> Result<FileDecryptionProperties> {
513+
let keys = DecryptionKeys::ViaRetriever(self.key_retriever);
514+
Ok(FileDecryptionProperties {
515+
keys,
516+
aad_prefix: self.aad_prefix,
517+
footer_signature_verification: self.footer_signature_verification,
518+
})
519+
}
520+
521+
/// Specify the expected AAD prefix to be used for decryption.
522+
/// This must be set if the file was written with an AAD prefix and the
523+
/// prefix is not stored in the file metadata.
524+
pub fn with_aad_prefix(mut self, value: Vec<u8>) -> Self {
525+
self.aad_prefix = Some(value);
526+
self
527+
}
528+
529+
/// Disable verification of footer tags for files that use plaintext footers.
530+
/// Signature verification is enabled by default.
531+
pub fn disable_footer_signature_verification(mut self) -> Self {
532+
self.footer_signature_verification = false;
533+
self
534+
}
535+
}
536+
518537
#[derive(Clone, Debug)]
519538
pub(crate) struct FileDecryptor {
520539
decryption_properties: FileDecryptionProperties,

0 commit comments

Comments
 (0)