Skip to content

Commit 0c73652

Browse files
committed
Refactor DecryptionPropertiesBuilder
1 parent 7905545 commit 0c73652

File tree

1 file changed

+48
-38
lines changed

1 file changed

+48
-38
lines changed

parquet/src/encryption/decrypt.rs

Lines changed: 48 additions & 38 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
@@ -342,8 +342,8 @@ impl FileDecryptionProperties {
342342

343343
/// Returns a new [`FileDecryptionProperties`] builder that uses a [`KeyRetriever`]
344344
/// to get decryption keys based on key metadata.
345-
pub fn with_key_retriever(key_retriever: Arc<dyn KeyRetriever>) -> DecryptionPropertiesBuilder {
346-
DecryptionPropertiesBuilder::new_with_key_retriever(key_retriever)
345+
pub fn with_key_retriever(key_retriever: Arc<dyn KeyRetriever>) -> DecryptionPropertiesBuilderWithRetriever {
346+
DecryptionPropertiesBuilderWithRetriever::new(key_retriever)
347347
}
348348

349349
/// AAD prefix string uniquely identifies the file and prevents file swapping
@@ -411,8 +411,7 @@ impl std::fmt::Debug for FileDecryptionProperties {
411411
///
412412
/// See [`FileDecryptionProperties`] for example usage.
413413
pub struct DecryptionPropertiesBuilder {
414-
footer_key: Option<Vec<u8>>,
415-
key_retriever: Option<Arc<dyn KeyRetriever>>,
414+
footer_key: Vec<u8>,
416415
column_keys: HashMap<String, Vec<u8>>,
417416
aad_prefix: Option<Vec<u8>>,
418417
}
@@ -422,45 +421,18 @@ impl DecryptionPropertiesBuilder {
422421
/// decrypt footer metadata.
423422
pub fn new(footer_key: Vec<u8>) -> DecryptionPropertiesBuilder {
424423
Self {
425-
footer_key: Some(footer_key),
426-
key_retriever: None,
427-
column_keys: HashMap::default(),
428-
aad_prefix: None,
429-
}
430-
}
431-
432-
/// Create a new [`DecryptionPropertiesBuilder`] by providing a [`KeyRetriever`] that
433-
/// can be used to get decryption keys based on key metadata.
434-
pub fn new_with_key_retriever(
435-
key_retriever: Arc<dyn KeyRetriever>,
436-
) -> DecryptionPropertiesBuilder {
437-
Self {
438-
footer_key: None,
439-
key_retriever: Some(key_retriever),
424+
footer_key,
440425
column_keys: HashMap::default(),
441426
aad_prefix: None,
442427
}
443428
}
444429

445430
/// Finalize the builder and return created [`FileDecryptionProperties`]
446431
pub fn build(self) -> Result<FileDecryptionProperties> {
447-
let keys = match (self.footer_key, self.key_retriever) {
448-
(Some(footer_key), None) => DecryptionKeys::Explicit(ExplicitDecryptionKeys {
449-
footer_key,
450-
column_keys: self.column_keys,
451-
}),
452-
(None, Some(key_retriever)) => {
453-
if !self.column_keys.is_empty() {
454-
return Err(general_err!(
455-
"Cannot specify column keys directly when using a key retriever"
456-
));
457-
}
458-
DecryptionKeys::ViaRetriever(key_retriever)
459-
}
460-
_ => {
461-
unreachable!()
462-
}
463-
};
432+
let keys = DecryptionKeys::Explicit(ExplicitDecryptionKeys {
433+
footer_key: self.footer_key,
434+
column_keys: self.column_keys,
435+
});
464436
Ok(FileDecryptionProperties {
465437
keys,
466438
aad_prefix: self.aad_prefix,
@@ -498,6 +470,44 @@ impl DecryptionPropertiesBuilder {
498470
}
499471
}
500472

473+
/// Builder for [`FileDecryptionProperties`] that uses a [`KeyRetriever`]
474+
///
475+
/// See the [`KeyRetriever`] documentation for example usage.
476+
pub struct DecryptionPropertiesBuilderWithRetriever {
477+
key_retriever: Arc<dyn KeyRetriever>,
478+
aad_prefix: Option<Vec<u8>>,
479+
}
480+
481+
impl DecryptionPropertiesBuilderWithRetriever {
482+
/// Create a new [`DecryptionPropertiesBuilderWithRetriever`] by providing a [`KeyRetriever`] that
483+
/// can be used to get decryption keys based on key metadata.
484+
pub fn new(
485+
key_retriever: Arc<dyn KeyRetriever>,
486+
) -> DecryptionPropertiesBuilderWithRetriever {
487+
Self {
488+
key_retriever,
489+
aad_prefix: None,
490+
}
491+
}
492+
493+
/// Finalize the builder and return created [`FileDecryptionProperties`]
494+
pub fn build(self) -> Result<FileDecryptionProperties> {
495+
let keys = DecryptionKeys::ViaRetriever(self.key_retriever);
496+
Ok(FileDecryptionProperties {
497+
keys,
498+
aad_prefix: self.aad_prefix,
499+
})
500+
}
501+
502+
/// Specify the expected AAD prefix to be used for decryption.
503+
/// This must be set if the file was written with an AAD prefix and the
504+
/// prefix is not stored in the file metadata.
505+
pub fn with_aad_prefix(mut self, value: Vec<u8>) -> Self {
506+
self.aad_prefix = Some(value);
507+
self
508+
}
509+
}
510+
501511
#[derive(Clone, Debug)]
502512
pub(crate) struct FileDecryptor {
503513
decryption_properties: FileDecryptionProperties,

0 commit comments

Comments
 (0)