|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/encryption/in_memory_kms.h" |
| 21 | + |
| 22 | +#include "iceberg/encryption/aes_gcm_internal.h" |
| 23 | +#include "iceberg/util/base64.h" |
| 24 | +#include "iceberg/util/macros.h" |
| 25 | + |
| 26 | +namespace iceberg { |
| 27 | + |
| 28 | +void InMemoryKms::Register() { |
| 29 | + KmsRegistry::Register(std::string(kName), |
| 30 | + []() -> Result<std::unique_ptr<KeyManagementClient>> { |
| 31 | + return std::make_unique<InMemoryKms>(); |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +Status InMemoryKms::Initialize( |
| 36 | + const std::unordered_map<std::string, std::string>& properties) { |
| 37 | + // Decode into a staging map so a bad property leaves master_keys_ untouched; |
| 38 | + // properties is unordered, so a partial update would be non-deterministic. |
| 39 | + std::unordered_map<std::string, std::vector<uint8_t>> decoded; |
| 40 | + for (const auto& [name, value] : properties) { |
| 41 | + if (!name.starts_with(kKeyPropertyPrefix)) { |
| 42 | + continue; |
| 43 | + } |
| 44 | + auto id = name.substr(kKeyPropertyPrefix.size()); |
| 45 | + ICEBERG_PRECHECK(!id.empty(), "Master key property {} has an empty key id", name); |
| 46 | + ICEBERG_ASSIGN_OR_RAISE(auto key, Base64::Decode(value)); |
| 47 | + ICEBERG_PRECHECK(key.size() == 16 || key.size() == 24 || key.size() == 32, |
| 48 | + "Master key {} must be 16, 24 or 32 bytes, got {}", name, |
| 49 | + key.size()); |
| 50 | + decoded.insert_or_assign(std::move(id), std::vector<uint8_t>(key.begin(), key.end())); |
| 51 | + } |
| 52 | + for (auto& [id, key] : decoded) { |
| 53 | + master_keys_.insert_or_assign(id, std::move(key)); |
| 54 | + } |
| 55 | + return {}; |
| 56 | +} |
| 57 | + |
| 58 | +Result<const std::vector<uint8_t>*> InMemoryKms::MasterKey(std::string_view id) const { |
| 59 | + auto it = master_keys_.find(std::string(id)); |
| 60 | + if (it == master_keys_.end()) { |
| 61 | + return NotFound("Master key {} is not found", id); |
| 62 | + } |
| 63 | + return &it->second; |
| 64 | +} |
| 65 | + |
| 66 | +Result<std::vector<uint8_t>> InMemoryKms::WrapKey(std::span<const uint8_t> key, |
| 67 | + std::string_view wrapping_key_id) { |
| 68 | + ICEBERG_ASSIGN_OR_RAISE(auto master_key, MasterKey(wrapping_key_id)); |
| 69 | + ICEBERG_ASSIGN_OR_RAISE(auto cipher, encryption::AesGcmCipher::Make(*master_key)); |
| 70 | + return cipher->Encrypt(key, {}); |
| 71 | +} |
| 72 | + |
| 73 | +Result<std::vector<uint8_t>> InMemoryKms::UnwrapKey(std::span<const uint8_t> wrapped_key, |
| 74 | + std::string_view wrapping_key_id) { |
| 75 | + ICEBERG_ASSIGN_OR_RAISE(auto master_key, MasterKey(wrapping_key_id)); |
| 76 | + ICEBERG_ASSIGN_OR_RAISE(auto cipher, encryption::AesGcmCipher::Make(*master_key)); |
| 77 | + return cipher->Decrypt(wrapped_key, {}); |
| 78 | +} |
| 79 | + |
| 80 | +} // namespace iceberg |
0 commit comments