Skip to content

Commit 387e1f2

Browse files
committed
Hold a shared pointer to the KeyToolkit in the FileKeyUnwrapper
1 parent e1197ce commit 387e1f2

3 files changed

Lines changed: 25 additions & 29 deletions

File tree

‎cpp/src/parquet/encryption/crypto_factory.cc‎

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,6 @@
2828

2929
namespace parquet::encryption {
3030

31-
namespace {
32-
33-
/// Holds a FileKeyUnwrapper and shared pointer to a KeyToolkit to allow
34-
/// keeping the KeyToolkit alive alongside the FileKeyUnwrapper
35-
class CryptoFactoryFileKeyRetriever : public DecryptionKeyRetriever {
36-
public:
37-
CryptoFactoryFileKeyRetriever(
38-
std::shared_ptr<KeyToolkit> key_toolkit,
39-
const KmsConnectionConfig& kms_connection_config, double cache_lifetime_seconds,
40-
const std::string& file_path,
41-
const std::shared_ptr<::arrow::fs::FileSystem>& file_system)
42-
: file_key_unwrapper_(key_toolkit.get(), kms_connection_config,
43-
cache_lifetime_seconds, file_path, file_system) {
44-
key_toolkit_ = std::move(key_toolkit);
45-
}
46-
47-
std::string GetKey(const std::string& key_metadata) override {
48-
return file_key_unwrapper_.GetKey(key_metadata);
49-
}
50-
51-
private:
52-
FileKeyUnwrapper file_key_unwrapper_;
53-
std::shared_ptr<KeyToolkit> key_toolkit_;
54-
};
55-
56-
} // namespace
57-
5831
void CryptoFactory::RegisterKmsClientFactory(
5932
std::shared_ptr<KmsClientFactory> kms_client_factory) {
6033
key_toolkit_->RegisterKmsClientFactory(kms_client_factory);
@@ -199,7 +172,7 @@ std::shared_ptr<FileDecryptionProperties> CryptoFactory::GetFileDecryptionProper
199172
const KmsConnectionConfig& kms_connection_config,
200173
const DecryptionConfiguration& decryption_config, const std::string& file_path,
201174
const std::shared_ptr<::arrow::fs::FileSystem>& file_system) {
202-
auto key_retriever = std::make_shared<CryptoFactoryFileKeyRetriever>(
175+
auto key_retriever = std::make_shared<FileKeyUnwrapper>(
203176
key_toolkit_, kms_connection_config, decryption_config.cache_lifetime_seconds,
204177
file_path, file_system);
205178

‎cpp/src/parquet/encryption/file_key_unwrapper.cc‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@
2424

2525
namespace parquet::encryption {
2626

27+
FileKeyUnwrapper::FileKeyUnwrapper(
28+
std::shared_ptr<KeyToolkit> key_toolkit,
29+
const KmsConnectionConfig& kms_connection_config, double cache_lifetime_seconds,
30+
const std::string& file_path,
31+
const std::shared_ptr<::arrow::fs::FileSystem>& file_system)
32+
: key_toolkit_owner_(std::move(key_toolkit)),
33+
key_toolkit_(key_toolkit_owner_.get()),
34+
kms_connection_config_(kms_connection_config),
35+
cache_entry_lifetime_seconds_(cache_lifetime_seconds),
36+
file_path_(file_path),
37+
file_system_(file_system) {
38+
kek_per_kek_id_ = key_toolkit_->kek_read_cache_per_token().GetOrCreateInternalCache(
39+
kms_connection_config.key_access_token(), cache_entry_lifetime_seconds_);
40+
}
41+
2742
FileKeyUnwrapper::FileKeyUnwrapper(
2843
KeyToolkit* key_toolkit, const KmsConnectionConfig& kms_connection_config,
2944
double cache_lifetime_seconds, const std::string& file_path,

‎cpp/src/parquet/encryption/file_key_unwrapper.h‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,19 @@ class PARQUET_EXPORT FileKeyUnwrapper : public DecryptionKeyRetriever {
4444
/// KmsClient in the cache.
4545
/// If the file uses external key material then the Parquet file path and file
4646
/// system must be specified.
47+
FileKeyUnwrapper(std::shared_ptr<KeyToolkit> key_toolkit,
48+
const KmsConnectionConfig& kms_connection_config,
49+
double cache_lifetime_seconds, const std::string& file_path = "",
50+
const std::shared_ptr<::arrow::fs::FileSystem>& file_system = NULLPTR);
51+
52+
/// Constructor overload that takes a raw pointer to the KeyToolkit
4753
FileKeyUnwrapper(KeyToolkit* key_toolkit,
4854
const KmsConnectionConfig& kms_connection_config,
4955
double cache_lifetime_seconds, const std::string& file_path = "",
5056
const std::shared_ptr<::arrow::fs::FileSystem>& file_system = NULLPTR);
5157

52-
/// Constructor overload that accepts an existing key_material_store rather than using
58+
/// Constructor overload that takes a raw pointer to the KeyToolkit and
59+
/// accepts an existing key_material_store rather than using
5360
/// the file path and file system to create one when needed. This is useful for key
5461
/// rotation to allow accessing the key material store after it is used.
5562
FileKeyUnwrapper(KeyToolkit* key_toolkit,
@@ -69,6 +76,7 @@ class PARQUET_EXPORT FileKeyUnwrapper : public DecryptionKeyRetriever {
6976

7077
/// A map of Key Encryption Key (KEK) ID -> KEK bytes, for the current token
7178
std::shared_ptr<::arrow::util::ConcurrentMap<std::string, std::string>> kek_per_kek_id_;
79+
std::shared_ptr<KeyToolkit> key_toolkit_owner_ = NULLPTR;
7280
KeyToolkit* key_toolkit_;
7381
KmsConnectionConfig kms_connection_config_;
7482
const double cache_entry_lifetime_seconds_;

0 commit comments

Comments
 (0)