File indexing completed on 2026-04-17 08:28:53
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #pragma once
0019
0020 #include <memory>
0021 #include <string>
0022
0023 #include "parquet/encryption/key_encryption_key.h"
0024 #include "parquet/encryption/kms_client.h"
0025 #include "parquet/encryption/kms_client_factory.h"
0026 #include "parquet/encryption/two_level_cache_with_expiration.h"
0027 #include "parquet/platform.h"
0028
0029 namespace parquet::encryption {
0030
0031 static constexpr uint64_t kCacheCleanPeriodForKeyRotation = 60 * 60;
0032
0033
0034
0035
0036 class PARQUET_EXPORT KeyToolkit {
0037 public:
0038 KeyToolkit() { last_cache_clean_for_key_rotation_time_ = {}; }
0039
0040
0041 TwoLevelCacheWithExpiration<std::shared_ptr<KmsClient>>& kms_client_cache_per_token() {
0042 return kms_client_cache_;
0043 }
0044
0045
0046 TwoLevelCacheWithExpiration<KeyEncryptionKey>& kek_write_cache_per_token() {
0047 return key_encryption_key_write_cache_;
0048 }
0049
0050
0051
0052 TwoLevelCacheWithExpiration<::arrow::util::SecureString>& kek_read_cache_per_token() {
0053 return key_encryption_key_read_cache_;
0054 }
0055
0056 std::shared_ptr<KmsClient> GetKmsClient(
0057 const KmsConnectionConfig& kms_connection_config, double cache_entry_lifetime_ms);
0058
0059
0060 void RemoveCacheEntriesForToken(const std::string& access_token);
0061
0062 void RemoveCacheEntriesForAllTokens();
0063
0064 void RegisterKmsClientFactory(std::shared_ptr<KmsClientFactory> kms_client_factory) {
0065 if (kms_client_factory_ != NULLPTR) {
0066 throw ParquetException("KMS client factory has already been registered.");
0067 }
0068 kms_client_factory_ = std::move(kms_client_factory);
0069 }
0070
0071
0072
0073
0074
0075
0076
0077 void RotateMasterKeys(const KmsConnectionConfig& kms_connection_config,
0078 const std::string& parquet_file_path,
0079 const std::shared_ptr<::arrow::fs::FileSystem>& file_system,
0080 bool double_wrapping, double cache_lifetime_seconds);
0081
0082 private:
0083 TwoLevelCacheWithExpiration<std::shared_ptr<KmsClient>> kms_client_cache_;
0084 TwoLevelCacheWithExpiration<KeyEncryptionKey> key_encryption_key_write_cache_;
0085 TwoLevelCacheWithExpiration<::arrow::util::SecureString> key_encryption_key_read_cache_;
0086 std::shared_ptr<KmsClientFactory> kms_client_factory_;
0087 mutable ::arrow::util::Mutex last_cache_clean_for_key_rotation_time_mutex_;
0088 internal::TimePoint last_cache_clean_for_key_rotation_time_;
0089 };
0090
0091
0092
0093 class PARQUET_EXPORT KeyWithMasterId {
0094 public:
0095 KeyWithMasterId(::arrow::util::SecureString key_bytes, std::string master_id)
0096 : key_bytes_(std::move(key_bytes)), master_id_(std::move(master_id)) {}
0097
0098 const ::arrow::util::SecureString& data_key() const { return key_bytes_; }
0099 const std::string& master_id() const { return master_id_; }
0100
0101 private:
0102 ::arrow::util::SecureString key_bytes_;
0103 std::string master_id_;
0104 };
0105
0106 }