Skip to content

Commit a52607e

Browse files
committed
Add FileEncryptionProperties to WriterProperties
1 parent 897313c commit a52607e

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

parquet/src/column/writer/mod.rs

+26
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,7 @@ mod tests {
15351535
page::PageReader,
15361536
reader::{get_column_reader, get_typed_column_reader, ColumnReaderImpl},
15371537
};
1538+
use crate::encryption::encryption::FileEncryptionProperties;
15381539
use crate::file::writer::TrackedWrite;
15391540
use crate::file::{
15401541
properties::ReaderProperties, reader::SerializedPageReader, writer::SerializedPageWriter,
@@ -3379,6 +3380,31 @@ mod tests {
33793380
);
33803381
}
33813382

3383+
#[cfg(feature = "encryption")]
3384+
#[test]
3385+
fn test_encryption_writer() {
3386+
let message_type = "
3387+
message test_schema {
3388+
OPTIONAL BYTE_ARRAY a (UTF8);
3389+
}
3390+
";
3391+
let schema = Arc::new(parse_message_type(message_type).unwrap());
3392+
let file: File = tempfile::tempfile().unwrap();
3393+
3394+
let builder = WriterProperties::builder();
3395+
let key_code: &[u8] = "0123456789012345".as_bytes();
3396+
let file_encryption_properties = FileEncryptionProperties::builder(key_code.to_vec())
3397+
.build()
3398+
.unwrap();
3399+
3400+
let props = Arc::new(
3401+
builder
3402+
.set_file_encryption_properties(file_encryption_properties)
3403+
.build(),
3404+
);
3405+
let mut writer = SerializedFileWriter::new(&file, schema, props).unwrap();
3406+
}
3407+
33823408
#[test]
33833409
fn test_increment_max_binary_chars() {
33843410
let r = increment(vec![0xFF, 0xFE, 0xFD, 0xFF, 0xFF]);

parquet/src/encryption/encryption.rs

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
use std::collections::HashMap;
19+
20+
#[derive(Debug, Clone)]
21+
pub struct FileEncryptionProperties {
22+
encrypt_footer: bool,
23+
footer_key: Vec<u8>,
24+
column_keys: Option<HashMap<Vec<u8>, Vec<u8>>>,
25+
aad_prefix: Option<Vec<u8>>,
26+
}
27+
28+
impl FileEncryptionProperties {
29+
pub fn builder(footer_key: Vec<u8>) -> EncryptionPropertiesBuilder {
30+
EncryptionPropertiesBuilder::new(footer_key)
31+
}
32+
}
33+
34+
pub struct EncryptionPropertiesBuilder {
35+
footer_key: Vec<u8>,
36+
column_keys: Option<HashMap<Vec<u8>, Vec<u8>>>,
37+
aad_prefix: Option<Vec<u8>>,
38+
}
39+
40+
impl EncryptionPropertiesBuilder {
41+
pub fn new(footer_key: Vec<u8>) -> EncryptionPropertiesBuilder {
42+
Self {
43+
footer_key,
44+
column_keys: None,
45+
aad_prefix: None,
46+
}
47+
}
48+
49+
pub fn build(self) -> crate::errors::Result<FileEncryptionProperties> {
50+
Ok(FileEncryptionProperties {
51+
encrypt_footer: true,
52+
footer_key: self.footer_key,
53+
column_keys: self.column_keys,
54+
aad_prefix: self.aad_prefix,
55+
})
56+
}
57+
}

parquet/src/encryption/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@
2020
2121
pub mod ciphers;
2222
pub mod decryption;
23+
pub mod encryption;
2324
pub mod modules;

parquet/src/file/properties.rs

+15
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
//! Configuration via [`WriterProperties`] and [`ReaderProperties`]
1919
use crate::basic::{Compression, Encoding};
2020
use crate::compression::{CodecOptions, CodecOptionsBuilder};
21+
use crate::encryption::encryption::FileEncryptionProperties;
2122
use crate::file::metadata::KeyValue;
2223
use crate::format::SortingColumn;
2324
use crate::schema::types::ColumnPath;
@@ -169,6 +170,7 @@ pub struct WriterProperties {
169170
column_index_truncate_length: Option<usize>,
170171
statistics_truncate_length: Option<usize>,
171172
coerce_types: bool,
173+
file_encryption_properties: Option<FileEncryptionProperties>,
172174
}
173175

174176
impl Default for WriterProperties {
@@ -392,6 +394,7 @@ pub struct WriterPropertiesBuilder {
392394
column_index_truncate_length: Option<usize>,
393395
statistics_truncate_length: Option<usize>,
394396
coerce_types: bool,
397+
file_encryption_properties: Option<FileEncryptionProperties>,
395398
}
396399

397400
impl WriterPropertiesBuilder {
@@ -414,6 +417,7 @@ impl WriterPropertiesBuilder {
414417
column_index_truncate_length: DEFAULT_COLUMN_INDEX_TRUNCATE_LENGTH,
415418
statistics_truncate_length: DEFAULT_STATISTICS_TRUNCATE_LENGTH,
416419
coerce_types: DEFAULT_COERCE_TYPES,
420+
file_encryption_properties: None,
417421
}
418422
}
419423

@@ -436,6 +440,7 @@ impl WriterPropertiesBuilder {
436440
column_index_truncate_length: self.column_index_truncate_length,
437441
statistics_truncate_length: self.statistics_truncate_length,
438442
coerce_types: self.coerce_types,
443+
file_encryption_properties: self.file_encryption_properties,
439444
}
440445
}
441446

@@ -808,6 +813,16 @@ impl WriterPropertiesBuilder {
808813
self.coerce_types = coerce_types;
809814
self
810815
}
816+
817+
/// Sets FileEncryptionProperties.
818+
/// Only applicable if file encryption is enabled.
819+
pub fn set_file_encryption_properties(
820+
mut self,
821+
file_encryption_properties: FileEncryptionProperties,
822+
) -> Self {
823+
self.file_encryption_properties = Some(file_encryption_properties);
824+
self
825+
}
811826
}
812827

813828
/// Controls the level of statistics to be computed by the writer and stored in

0 commit comments

Comments
 (0)