]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
1 | // Licensed to the Apache Software Foundation (ASF) under one |
2 | // or more contributor license agreements. See the NOTICE file | |
3 | // distributed with this work for additional information | |
4 | // regarding copyright ownership. The ASF licenses this file | |
5 | // to you under the Apache License, Version 2.0 (the | |
6 | // "License"); you may not use this file except in compliance | |
7 | // with the License. You may obtain a copy of the License at | |
8 | // | |
9 | // http://www.apache.org/licenses/LICENSE-2.0 | |
10 | // | |
11 | // Unless required by applicable law or agreed to in writing, | |
12 | // software distributed under the License is distributed on an | |
13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
14 | // KIND, either express or implied. See the License for the | |
15 | // specific language governing permissions and limitations | |
16 | // under the License. | |
17 | ||
18 | #include "arrow/json/object_parser.h" | |
19 | #include "arrow/json/object_writer.h" | |
20 | ||
21 | #include "parquet/encryption/key_metadata.h" | |
22 | #include "parquet/exception.h" | |
23 | ||
24 | using ::arrow::json::internal::ObjectParser; | |
25 | using ::arrow::json::internal::ObjectWriter; | |
26 | ||
27 | namespace parquet { | |
28 | namespace encryption { | |
29 | ||
30 | constexpr const char KeyMetadata::kKeyMaterialInternalStorageField[]; | |
31 | constexpr const char KeyMetadata::kKeyReferenceField[]; | |
32 | ||
33 | KeyMetadata::KeyMetadata(const std::string& key_reference) | |
34 | : is_internal_storage_(false), key_material_or_reference_(key_reference) {} | |
35 | ||
36 | KeyMetadata::KeyMetadata(const KeyMaterial& key_material) | |
37 | : is_internal_storage_(true), key_material_or_reference_(key_material) {} | |
38 | ||
39 | KeyMetadata KeyMetadata::Parse(const std::string& key_metadata) { | |
40 | ObjectParser json_parser; | |
41 | ::arrow::Status status = json_parser.Parse(key_metadata); | |
42 | if (!status.ok()) { | |
43 | throw ParquetException("Failed to parse key metadata " + key_metadata); | |
44 | } | |
45 | ||
46 | // 1. Extract "key material type", and make sure it is supported | |
47 | std::string key_material_type; | |
48 | PARQUET_ASSIGN_OR_THROW(key_material_type, | |
49 | json_parser.GetString(KeyMaterial::kKeyMaterialTypeField)); | |
50 | if (key_material_type != KeyMaterial::kKeyMaterialType1) { | |
51 | throw ParquetException("Wrong key material type: " + key_material_type + " vs " + | |
52 | KeyMaterial::kKeyMaterialType1); | |
53 | } | |
54 | ||
55 | // 2. Check if "key material" is stored internally in Parquet file key metadata, or is | |
56 | // stored externally | |
57 | bool is_internal_storage; | |
58 | PARQUET_ASSIGN_OR_THROW(is_internal_storage, | |
59 | json_parser.GetBool(kKeyMaterialInternalStorageField)); | |
60 | ||
61 | if (is_internal_storage) { | |
62 | // 3.1 "key material" is stored internally, inside "key metadata" - parse it | |
63 | KeyMaterial key_material = KeyMaterial::Parse(&json_parser); | |
64 | return KeyMetadata(key_material); | |
65 | } else { | |
66 | // 3.2 "key material" is stored externally. "key metadata" keeps a reference to it | |
67 | std::string key_reference; | |
68 | PARQUET_ASSIGN_OR_THROW(key_reference, json_parser.GetString(kKeyReferenceField)); | |
69 | return KeyMetadata(key_reference); | |
70 | } | |
71 | } | |
72 | ||
73 | // For external material only. For internal material, create serialized KeyMaterial | |
74 | // directly | |
75 | std::string KeyMetadata::CreateSerializedForExternalMaterial( | |
76 | const std::string& key_reference) { | |
77 | ObjectWriter json_writer; | |
78 | ||
79 | json_writer.SetString(KeyMaterial::kKeyMaterialTypeField, | |
80 | KeyMaterial::kKeyMaterialType1); | |
81 | json_writer.SetBool(kKeyMaterialInternalStorageField, false); | |
82 | ||
83 | json_writer.SetString(kKeyReferenceField, key_reference); | |
84 | ||
85 | return json_writer.Serialize(); | |
86 | } | |
87 | ||
88 | } // namespace encryption | |
89 | } // namespace parquet |