Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-17 08:28:53

0001 // Licensed to the Apache Software Foundation (ASF) under one
0002 // or more contributor license agreements.  See the NOTICE file
0003 // distributed with this work for additional information
0004 // regarding copyright ownership.  The ASF licenses this file
0005 // to you under the Apache License, Version 2.0 (the
0006 // "License"); you may not use this file except in compliance
0007 // with the License.  You may obtain a copy of the License at
0008 //
0009 //   http://www.apache.org/licenses/LICENSE-2.0
0010 //
0011 // Unless required by applicable law or agreed to in writing,
0012 // software distributed under the License is distributed on an
0013 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0014 // KIND, either express or implied.  See the License for the
0015 // specific language governing permissions and limitations
0016 // under the License.
0017 
0018 #pragma once
0019 
0020 #include <unordered_map>
0021 #include <vector>
0022 
0023 #include "arrow/util/concurrent_map.h"
0024 
0025 #include "parquet/encryption/kms_client.h"
0026 #include "parquet/platform.h"
0027 
0028 namespace parquet::encryption {
0029 
0030 /// This class supports local wrapping mode, master keys will be fetched from the KMS
0031 /// server and used to encrypt other keys (data encryption keys or key encryption keys).
0032 class PARQUET_EXPORT LocalWrapKmsClient : public KmsClient {
0033  public:
0034   static constexpr const char kLocalWrapNoKeyVersion[] = "NO_VERSION";
0035 
0036   explicit LocalWrapKmsClient(const KmsConnectionConfig& kms_connection_config);
0037 
0038   std::string WrapKey(const ::arrow::util::SecureString& key_bytes,
0039                       const std::string& master_key_identifier) override;
0040 
0041   ::arrow::util::SecureString UnwrapKey(
0042       const std::string& wrapped_key, const std::string& master_key_identifier) override;
0043 
0044  protected:
0045   /// Get master key from the remote KMS server.
0046   /// Note: this function might be called by multiple threads
0047   virtual const ::arrow::util::SecureString& GetMasterKeyFromServer(
0048       const std::string& master_key_identifier) = 0;
0049 
0050  private:
0051   /// KMS systems wrap keys by encrypting them by master keys, and attaching additional
0052   /// information (such as the version number of the masker key) to the result of
0053   /// encryption. The master key version is required in  key rotation. Currently, the
0054   /// local wrapping mode does not support key rotation (because not all KMS systems allow
0055   /// to fetch a master key by its ID and version number). Still, the local wrapping mode
0056   /// adds a placeholder for the master key version, that will enable support for key
0057   /// rotation in this mode in the future, with appropriate KMS systems. This will also
0058   /// enable backward compatibility, where future readers will be able to extract master
0059   /// key version in the files written by the current code.
0060   ///
0061   /// LocalKeyWrap class writes (and reads) the "key wrap" as a flat json with the
0062   /// following fields:
0063   /// 1. "masterKeyVersion" - a String, with the master key version. In the current
0064   /// version, only one value is allowed - "NO_VERSION".
0065   /// 2. "encryptedKey" - a String, with the key encrypted by the master key
0066   /// (base64-encoded).
0067   class LocalKeyWrap {
0068    public:
0069     static constexpr const char kLocalWrapKeyVersionField[] = "masterKeyVersion";
0070     static constexpr const char kLocalWrapEncryptedKeyField[] = "encryptedKey";
0071 
0072     LocalKeyWrap(std::string master_key_version, std::string encrypted_encoded_key);
0073 
0074     static std::string CreateSerialized(const std::string& encrypted_encoded_key);
0075 
0076     static LocalKeyWrap Parse(const std::string& wrapped_key);
0077 
0078     const std::string& master_key_version() const { return master_key_version_; }
0079 
0080     const std::string& encrypted_encoded_key() const { return encrypted_encoded_key_; }
0081 
0082    private:
0083     std::string encrypted_encoded_key_;
0084     std::string master_key_version_;
0085   };
0086 
0087   const ::arrow::util::SecureString& GetKeyFromServer(const std::string& key_identifier);
0088 
0089  protected:
0090   KmsConnectionConfig kms_connection_config_;
0091   ::arrow::util::ConcurrentMap<std::string, ::arrow::util::SecureString>
0092       master_key_cache_;
0093 };
0094 
0095 }  // namespace parquet::encryption