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 <string>
0021 #include <variant>
0022 
0023 #include "parquet/encryption/key_material.h"
0024 #include "parquet/exception.h"
0025 #include "parquet/platform.h"
0026 
0027 namespace parquet::encryption {
0028 
0029 // Parquet encryption specification defines "key metadata" as an arbitrary byte array,
0030 // generated by file writers for each encryption key, and passed to the low level API for
0031 // storage in the file footer. The "key metadata" field is made available to file readers
0032 // to enable recovery of the key. This interface can be utilized for implementation
0033 // of any key management scheme.
0034 //
0035 // The keytools package (PARQUET-1373) implements one approach, of many possible, to key
0036 // management and to generation of the "key metadata" fields. This approach, based on the
0037 // "envelope encryption" pattern, allows integration with KMS servers. It keeps the actual
0038 // material, required to recover a key, in a "key material" object (see the KeyMaterial
0039 // class for details). This class is implemented to support version 1 of the parquet key
0040 // management tools specification.
0041 //
0042 // KeyMetadata writes (and reads) the "key metadata" field as a flat json object,
0043 // with the following fields:
0044 // 1. "keyMaterialType" - a String, with the type of  key material.
0045 // 2. "internalStorage" - a boolean. If true, means that "key material" is kept inside the
0046 // "key metadata" field. If false, "key material" is kept externally (outside Parquet
0047 // files) - in this case, "key metadata" keeps a reference to the external "key material".
0048 // 3. "keyReference" - a String, with the reference to the external "key material".
0049 // Written only if internalStorage is false.
0050 //
0051 // If internalStorage is true, "key material" is a part of "key metadata", and the json
0052 // keeps additional fields, described in the KeyMaterial class.
0053 class PARQUET_EXPORT KeyMetadata {
0054  public:
0055   static constexpr const char kKeyMaterialInternalStorageField[] = "internalStorage";
0056   static constexpr const char kKeyReferenceField[] = "keyReference";
0057 
0058   /// key_metadata_bytes is the key metadata field stored in the parquet file,
0059   /// in the serialized json object format.
0060   static KeyMetadata Parse(const std::string& key_metadata_bytes);
0061 
0062   static std::string CreateSerializedForExternalMaterial(
0063       const std::string& key_reference);
0064 
0065   bool key_material_stored_internally() const { return is_internal_storage_; }
0066 
0067   const KeyMaterial& key_material() const {
0068     if (!is_internal_storage_) {
0069       throw ParquetException("key material is stored externally.");
0070     }
0071     return ::std::get<KeyMaterial>(key_material_or_reference_);
0072   }
0073 
0074   const std::string& key_reference() const {
0075     if (is_internal_storage_) {
0076       throw ParquetException("key material is stored internally.");
0077     }
0078     return ::std::get<std::string>(key_material_or_reference_);
0079   }
0080 
0081  private:
0082   explicit KeyMetadata(const KeyMaterial& key_material);
0083   explicit KeyMetadata(const std::string& key_reference);
0084 
0085   bool is_internal_storage_;
0086   /// If is_internal_storage_ is true, KeyMaterial is set,
0087   /// else a string referencing to an outside "key material" is set.
0088   ::std::variant<KeyMaterial, std::string> key_material_or_reference_;
0089 };
0090 
0091 }  // namespace parquet::encryption