-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add example demonstrating how Parquet encryption could be configured with KMS integration #16237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
adamreeve
wants to merge
2
commits into
apache:main
Choose a base branch
from
adamreeve:encryption-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
205 changes: 205 additions & 0 deletions
205
datafusion-examples/examples/parquet_encryption_with_kms.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use arrow::array::{ArrayRef, Int32Array, RecordBatch, StringArray}; | ||
use datafusion::config::TableParquetOptions; | ||
use datafusion::dataframe::DataFrameWriteOptions; | ||
use datafusion::datasource::file_format::parquet::ParquetFormat; | ||
use datafusion::datasource::listing::ListingOptions; | ||
use datafusion::datasource::physical_plan::FileSinkConfig; | ||
use datafusion::error::{DataFusionError, Result}; | ||
use datafusion::physical_plan::execute_stream; | ||
use datafusion::prelude::SessionContext; | ||
use futures::StreamExt; | ||
use parquet::encryption::decrypt::FileDecryptionProperties; | ||
use parquet::encryption::encrypt::FileEncryptionProperties; | ||
use parquet_key_management::crypto_factory::{ | ||
CryptoFactory, DecryptionConfiguration, EncryptionConfiguration, | ||
}; | ||
use parquet_key_management::kms::KmsConnectionConfig; | ||
use parquet_key_management::test_kms::TestKmsClientFactory; | ||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
use tempfile::TempDir; | ||
|
||
/// This example demonstrates reading and writing Parquet files that | ||
/// are encrypted using Parquet Modular Encryption, and uses the | ||
/// parquet-key-management crate to integrate with a Key Management Server (KMS). | ||
|
||
const ENCRYPTION_FACTORY_ID: &'static str = "example.inmem_kms_encryption"; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
let ctx = SessionContext::new(); | ||
|
||
// Register an `EncryptionFactory` implementation to be used for Parquet encryption | ||
// in the session context. | ||
// This example uses an in-memory test KMS from the `parquet_key_management` crate with | ||
// a custom `KmsEncryptionFactory` wrapper type to integrate with DataFusion. | ||
// `EncryptionFactory` instances are registered with a name to identify them so | ||
// they can be later referenced in configuration options, and it's possible to register | ||
// multiple different factories to handle different ways of encrypting Parquet. | ||
// In future it could be possible to have built-in implementations in DataFusion. | ||
let crypto_factory = CryptoFactory::new(TestKmsClientFactory::with_default_keys()); | ||
let encryption_factory = KmsEncryptionFactory { crypto_factory }; | ||
ctx.register_parquet_encryption_factory( | ||
ENCRYPTION_FACTORY_ID, | ||
Arc::new(encryption_factory), | ||
); | ||
|
||
let tmpdir = TempDir::new()?; | ||
write_encrypted(&ctx, &tmpdir).await?; | ||
read_encrypted(&ctx, &tmpdir).await?; | ||
Ok(()) | ||
} | ||
|
||
/// Write an encrypted Parquet file | ||
async fn write_encrypted(ctx: &SessionContext, tmpdir: &TempDir) -> Result<()> { | ||
let a: ArrayRef = Arc::new(StringArray::from(vec!["a", "b", "c", "d"])); | ||
let b: ArrayRef = Arc::new(Int32Array::from(vec![1, 10, 10, 100])); | ||
let batch = RecordBatch::try_from_iter(vec![("a", a), ("b", b)])?; | ||
|
||
ctx.register_batch("test_data", batch)?; | ||
let df = ctx.table("test_data").await?; | ||
|
||
let mut parquet_options = TableParquetOptions::new(); | ||
// We specify that we want to use Parquet encryption by setting the identifier of the | ||
// encryption factory to use. | ||
parquet_options.encryption.factory_id = ENCRYPTION_FACTORY_ID.to_owned(); | ||
// Our encryption factory requires specifying the master key identifier to | ||
// use for encryption. To support arbitrary configuration options for different encryption factories, | ||
// DataFusion could use a HashMap<String, String> field for encryption options. | ||
let encryption_config = EncryptionConfig::new("kf1".to_owned()); | ||
parquet_options.encryption.factory_options = encryption_config.to_config_map(); | ||
|
||
df.write_parquet( | ||
tmpdir.path().to_str().unwrap(), | ||
DataFrameWriteOptions::new(), | ||
Some(parquet_options), | ||
) | ||
.await?; | ||
|
||
println!("Encrypted Parquet written to {:?}", tmpdir.path()); | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Read from an encrypted Parquet file | ||
async fn read_encrypted(ctx: &SessionContext, tmpdir: &TempDir) -> Result<()> { | ||
let mut parquet_options = TableParquetOptions::new(); | ||
// Specify the encryption factory to use for decrypting Parquet. | ||
// In this example, we don't require any additional configuration options when reading | ||
// as key identifiers are stored in the key metadata. | ||
parquet_options.encryption.factory_id = ENCRYPTION_FACTORY_ID.to_owned(); | ||
|
||
let file_format = ParquetFormat::default().with_options(parquet_options); | ||
let listing_options = ListingOptions::new(Arc::new(file_format)); | ||
|
||
let file_name = std::fs::read_dir(tmpdir)?.next().unwrap()?; | ||
let table_path = format!("file://{}", file_name.path().as_os_str().to_str().unwrap()); | ||
|
||
let _ = ctx | ||
.register_listing_table( | ||
"encrypted_parquet_table", | ||
&table_path, | ||
listing_options.clone(), | ||
None, | ||
None, | ||
) | ||
.await?; | ||
|
||
let df = ctx.sql("SELECT * FROM encrypted_parquet_table").await?; | ||
let plan = df.create_physical_plan().await?; | ||
|
||
let mut batch_stream = execute_stream(plan.clone(), ctx.task_ctx())?; | ||
println!("Reading encrypted Parquet as a RecordBatch stream"); | ||
while let Some(batch) = batch_stream.next().await { | ||
let batch = batch?; | ||
println!("Read batch with {} rows", batch.num_rows()); | ||
} | ||
println!("Finished reading"); | ||
Ok(()) | ||
} | ||
|
||
// Options used to configure our example encryption factory | ||
struct EncryptionConfig { | ||
pub footer_key_id: String, | ||
} | ||
|
||
impl EncryptionConfig { | ||
pub fn new(footer_key_id: String) -> Self { | ||
Self { footer_key_id } | ||
} | ||
|
||
pub fn to_config_map(self) -> HashMap<String, String> { | ||
let mut config = HashMap::new(); | ||
config.insert("footer_key_id".to_string(), self.footer_key_id); | ||
config | ||
} | ||
} | ||
|
||
/// Wrapper type around `CryptoFactory` to allow implementing the `EncryptionFactory` trait | ||
struct KmsEncryptionFactory { | ||
crypto_factory: CryptoFactory, | ||
} | ||
|
||
/// `EncryptionFactory` is a trait defined by DataFusion that allows generating | ||
/// file encryption and decryption properties. | ||
impl EncryptionFactory for KmsEncryptionFactory { | ||
/// Generate file encryption properties to use when writing a Parquet file. | ||
/// The `FileSinkConfig` is provided so that the schema may be used to dynamically configure | ||
/// per-column encryption keys. | ||
/// Because `FileSinkConfig` can represent multiple output files, we also provide a | ||
/// single file path so that external key material may be used (where key metadata is | ||
/// stored in a JSON file alongside Parquet files). | ||
fn get_file_encryption_properties( | ||
&self, | ||
options: &TableParquetOptions, | ||
sink_config: &FileSinkConfig, | ||
file_path: &str, | ||
) -> Result<FileEncryptionProperties> { | ||
let config: &HashMap<String, String> = options.encryption.factory_options; | ||
let footer_key_id = config.get("footer_key_id").cloned().ok_or_else(|| { | ||
DataFusionError::Configuration( | ||
"Footer key id for encryption is not set".to_owned(), | ||
) | ||
})?; | ||
// We could configure per-column keys using the provided schema, | ||
// but for simplicity this example uses uniform encryption. | ||
let config = EncryptionConfiguration::builder(footer_key_id).build()?; | ||
// Similarly, the KMS connection could be configured from the options if needed, but this | ||
// example just uses the default options. | ||
let kms_config = Arc::new(KmsConnectionConfig::default()); | ||
Ok(self | ||
.crypto_factory | ||
.file_encryption_properties(kms_config, &config)?) | ||
} | ||
|
||
/// Generate file decryption properties to use when reading a Parquet file. | ||
/// The `file_path` needs to be known to support encryption factories that use external key material. | ||
fn get_file_decryption_properties( | ||
&self, | ||
options: &TableParquetOptions, | ||
file_path: &str, | ||
) -> Result<FileDecryptionProperties> { | ||
let config = DecryptionConfiguration::builder().build()?; | ||
let kms_config = Arc::new(KmsConnectionConfig::default()); | ||
Ok(self | ||
.crypto_factory | ||
.file_decryption_properties(kms_config, config)?) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this needs to be under
parquet_options.global...
since it affects all columns. And I guess we should figure out if we want a encryption sub-piece for all crypto related options. Maybe call it crypto?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I initially had it under
global
but then decided that didn't quite make sense as some of the options might be per-column keys, so moved it to a separateencryption
namespace.And yeah I was using
encryption
here to include decryption properties too.crypto
probably makes more sense, and being shorter to type is nice.