Skip to content

Commit 43b1242

Browse files
committed
Add encryption algorithm to file_metadata before writing
1 parent 5120ad4 commit 43b1242

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

parquet/src/encryption/encrypt.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -385,16 +385,19 @@ pub(crate) fn sign_and_write_object<T: TSerializable, W: Write>(
385385
let mut protocol = TCompactOutputProtocol::new(&mut buffer);
386386
object.write_to_out_protocol(&mut protocol)?;
387387
}
388-
let plaintext_len = buffer.len();
389388
sink.write_all(&buffer)?;
389+
buffer = encryptor.encrypt(buffer.as_ref(), module_aad)?;
390+
391+
let ciphertext_length : u32 = buffer.len()
392+
.try_into()
393+
.map_err(|err| general_err!("Plaintext data too long. {:?}", err))?;
390394

391-
let encrypted_buffer = encryptor.encrypt(buffer.as_ref(), module_aad)?;
392-
393395
// Format is: [ciphertext size, nonce, ciphertext, authentication tag]
394-
let nonce = encrypted_buffer[SIZE_LEN..SIZE_LEN + NONCE_LEN].to_vec();
395-
let tag = encrypted_buffer[SIZE_LEN + NONCE_LEN + plaintext_len..SIZE_LEN + NONCE_LEN + plaintext_len + TAG_LEN].to_vec();
396+
let nonce = buffer[SIZE_LEN..SIZE_LEN + NONCE_LEN].to_vec();
397+
let tag = buffer[(ciphertext_length - TAG_LEN as u32) as usize..].to_vec();
396398
sink.write_all(&nonce)?;
397399
sink.write_all(&tag)?;
400+
398401
Ok(())
399402
}
400403

parquet/src/file/metadata/writer.rs

+25-6
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
// under the License.
1717

1818
#[cfg(feature = "encryption")]
19-
use crate::encryption::encrypt::{encrypt_object, encrypt_object_to_vec, sign_and_write_object, FileEncryptor};
20-
#[cfg(feature = "encryption")]
21-
use crate::encryption::modules::{create_footer_aad, create_module_aad, ModuleType};
19+
use crate::encryption::{
20+
encrypt::{encrypt_object, encrypt_object_to_vec, sign_and_write_object, FileEncryptor},
21+
modules::{create_footer_aad, create_module_aad, ModuleType},
22+
};
2223
#[cfg(feature = "encryption")]
2324
use crate::errors::ParquetError;
2425
use crate::errors::Result;
@@ -141,6 +142,25 @@ impl<'a, W: Write> ThriftMetadataWriter<'a, W> {
141142
.object_writer
142143
.apply_row_group_encryption(self.row_groups)?;
143144

145+
let mut encryption_algorithm = None;
146+
if let Some(file_encryptor) = self.object_writer.file_encryptor.clone() {
147+
let properties = file_encryptor.properties();
148+
if !properties.encrypt_footer() {
149+
let supply_aad_prefix = properties
150+
.aad_prefix()
151+
.map(|_| !properties.store_aad_prefix());
152+
encryption_algorithm = Some(EncryptionAlgorithm::AESGCMV1(AesGcmV1 {
153+
aad_prefix: if properties.store_aad_prefix() {
154+
properties.aad_prefix().cloned()
155+
} else {
156+
None
157+
},
158+
aad_file_unique: Some(file_encryptor.aad_file_unique().clone()),
159+
supply_aad_prefix,
160+
}));
161+
}
162+
};
163+
144164
let mut file_metadata = FileMetaData {
145165
num_rows,
146166
row_groups,
@@ -149,7 +169,7 @@ impl<'a, W: Write> ThriftMetadataWriter<'a, W> {
149169
schema: types::to_thrift(self.schema.as_ref())?,
150170
created_by: self.created_by.clone(),
151171
column_orders,
152-
encryption_algorithm: None,
172+
encryption_algorithm,
153173
footer_signing_key_metadata: None,
154174
};
155175

@@ -503,8 +523,7 @@ impl MetadataObjectWriter {
503523
let mut encryptor = file_encryptor.get_footer_encryptor()?;
504524
encrypt_object(file_metadata, &mut encryptor, &mut sink, &aad)
505525
}
506-
Some(file_encryptor) if !file_encryptor.properties().encrypt_footer() => {
507-
// todo: should we also check for file_metadata.encryption_algorithm.is_some() ?
526+
Some(file_encryptor) if file_metadata.encryption_algorithm.is_some() => {
508527
let aad = create_footer_aad(file_encryptor.file_aad())?;
509528
let mut encryptor = file_encryptor.get_footer_encryptor()?;
510529
sign_and_write_object(file_metadata, &mut encryptor, &mut sink, &aad)

parquet/src/file/writer.rs

-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,6 @@ impl<W: Write + Send> SerializedFileWriter<W> {
349349
);
350350

351351
#[cfg(feature = "encryption")]
352-
// todo
353352
{
354353
encoder = encoder.with_file_encryptor(self.file_encryptor.clone());
355354
}

0 commit comments

Comments
 (0)