Repository navigation
feat(encryption): add EncryptionManager and StandardEncryptionManager #1071
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shangxinli
wants to merge
3
commits into
apache:main
Choose a base branch
from
shangxinli:pr-enc06-encryption-manager
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+936
−6
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #include "iceberg/encryption/encryption_manager.h" | ||
|
|
||
| #include <mutex> | ||
|
|
||
| #include "iceberg/util/macros.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| namespace { | ||
|
|
||
| struct RegistryState { | ||
| std::mutex mutex; | ||
| EncryptionManagerRegistry::Factory factory; | ||
| }; | ||
|
|
||
| RegistryState& State() { | ||
| static RegistryState state; | ||
| return state; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| void EncryptionManagerRegistry::Register(Factory factory) { | ||
| auto& state = State(); | ||
| std::lock_guard lock(state.mutex); | ||
| state.factory = std::move(factory); | ||
| } | ||
|
|
||
| Result<std::shared_ptr<EncryptionManager>> EncryptionManagerRegistry::Make( | ||
| Options options) { | ||
| ICEBERG_PRECHECK(!options.table_key_id.empty(), "Invalid encryption key ID: empty"); | ||
| ICEBERG_PRECHECK(options.data_key_length == 16 || options.data_key_length == 24 || | ||
| options.data_key_length == 32, | ||
| "Invalid data key length: {} (must be 16, 24, or 32)", | ||
| options.data_key_length); | ||
| ICEBERG_PRECHECK(options.kms != nullptr, | ||
| "Cannot create an encryption manager without a KMS client; set the " | ||
| "encryption.kms-type or encryption.kms-impl catalog property"); | ||
| Factory factory; | ||
| { | ||
| auto& state = State(); | ||
| std::lock_guard lock(state.mutex); | ||
| factory = state.factory; | ||
| } | ||
| if (!factory) { | ||
| return NotSupported( | ||
| "Table encryption is not available: link iceberg_bundle built with " | ||
| "ICEBERG_ENCRYPTION=ON and call iceberg::encryption::RegisterAll()"); | ||
| } | ||
| return factory(std::move(options)); | ||
| } | ||
|
|
||
| } // namespace iceberg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| /// \file iceberg/encryption/encryption_manager.h | ||
| /// Table encryption manager interface and the registry that creates one. | ||
|
|
||
| #include <cstdint> | ||
| #include <functional> | ||
| #include <memory> | ||
| #include <span> | ||
| #include <string> | ||
| #include <string_view> | ||
| #include <vector> | ||
|
|
||
| #include "iceberg/encryption/encrypted_key.h" | ||
| #include "iceberg/encryption/key_management_client.h" | ||
| #include "iceberg/encryption/standard_key_metadata.h" | ||
| #include "iceberg/file_io.h" | ||
| #include "iceberg/iceberg_export.h" | ||
| #include "iceberg/result.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| /// \brief Keys created when the key metadata of a new manifest list is registered. | ||
| /// | ||
| /// Both must be added to the table metadata `encryption-keys` in the same commit as | ||
| /// the snapshot whose `key-id` is `file_key.key_id`. | ||
| struct ICEBERG_EXPORT ManifestListEncryptionKeys { | ||
| /// The key encryption key, wrapped by the table master key in the KMS. | ||
| EncryptedKey key_encryption_key; | ||
| /// The manifest list key metadata, encrypted by the key encryption key. | ||
| EncryptedKey file_key; | ||
| }; | ||
|
|
||
| /// \brief Encrypts and decrypts the files of one table. | ||
| /// | ||
| /// The standard implementation (StandardEncryptionManager) needs a crypto provider | ||
| /// and lives in the bundle library; the core library reaches it through | ||
| /// EncryptionManagerRegistry. | ||
| class ICEBERG_EXPORT EncryptionManager { | ||
| public: | ||
| virtual ~EncryptionManager() = default; | ||
|
|
||
| /// \brief Generate key metadata (random data key and AAD prefix) for a new file. | ||
| virtual Result<StandardKeyMetadata> NewKeyMetadata() = 0; | ||
|
|
||
| /// \brief Wrap an AES GCM Stream (AGS1) file in a decrypting InputFile. | ||
| /// | ||
| /// \param encrypted the encrypted file | ||
| /// \param key_metadata serialized key metadata, including the file length | ||
| virtual Result<std::unique_ptr<InputFile>> NewDecryptingInputFile( | ||
| std::unique_ptr<InputFile> encrypted, std::span<const uint8_t> key_metadata) = 0; | ||
|
|
||
| /// \brief Wrap a file in an AES GCM Stream (AGS1) encrypting OutputFile. | ||
| virtual Result<std::unique_ptr<OutputFile>> NewEncryptingOutputFile( | ||
| std::unique_ptr<OutputFile> plain, const StandardKeyMetadata& key_metadata) = 0; | ||
|
|
||
| /// \brief Decrypt the key metadata of a manifest list. | ||
| /// | ||
| /// \param manifest_list_key_id the snapshot `key-id` | ||
| /// \return the serialized StandardKeyMetadata of the manifest list file | ||
| virtual Result<std::vector<uint8_t>> DecryptManifestListKeyMetadata( | ||
| std::string_view manifest_list_key_id) = 0; | ||
|
|
||
| /// \brief Encrypt the key metadata of a new manifest list with a key encryption key. | ||
| /// | ||
| /// Creates a key encryption key (wrapped by the table master key) if there is no | ||
| /// unexpired one. | ||
| virtual Result<ManifestListEncryptionKeys> RegisterManifestListKeyMetadata( | ||
| const StandardKeyMetadata& key_metadata) = 0; | ||
| }; | ||
|
|
||
| /// \brief Creates the encryption manager of encrypted tables. | ||
| /// | ||
| /// The bundle library registers the standard implementation (see | ||
| /// iceberg::encryption::RegisterAll()). Without it, encrypted tables cannot be read or | ||
| /// written. | ||
| class ICEBERG_EXPORT EncryptionManagerRegistry { | ||
| public: | ||
| struct Options { | ||
| /// The table master key ID in the KMS (table property `encryption.key-id`), taken | ||
| /// from a trusted source. | ||
| std::string table_key_id; | ||
| /// Length of generated data keys: 16, 24 or 32 bytes. | ||
| int32_t data_key_length = 16; | ||
| /// The table metadata `encryption-keys`. | ||
| std::vector<EncryptedKey> encryption_keys; | ||
| /// The catalog's KMS client. | ||
| std::shared_ptr<KeyManagementClient> kms; | ||
| }; | ||
|
|
||
| using Factory = | ||
| std::function<Result<std::shared_ptr<EncryptionManager>>(Options options)>; | ||
|
|
||
| /// \brief Register the encryption manager implementation. | ||
| static void Register(Factory factory); | ||
|
|
||
| /// \brief Create an encryption manager. | ||
| static Result<std::shared_ptr<EncryptionManager>> Make(Options options); | ||
| }; | ||
|
|
||
| } // namespace iceberg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #include "iceberg/encryption/encryption_register.h" | ||
|
|
||
| #if ICEBERG_ENCRYPTION_ENABLED | ||
| # include "iceberg/encryption/standard_encryption_manager.h" | ||
| #endif | ||
|
|
||
| namespace iceberg::encryption { | ||
|
|
||
| void RegisterAll() { | ||
| #if ICEBERG_ENCRYPTION_ENABLED | ||
| RegisterStandardEncryptionManager(); | ||
| #endif | ||
| } | ||
|
|
||
| bool IsEncryptionAvailable() { return ICEBERG_ENCRYPTION_ENABLED; } | ||
|
|
||
| } // namespace iceberg::encryption |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| /// \file iceberg/encryption/encryption_register.h | ||
| /// Registration of the table encryption implementation of the bundle library. | ||
|
|
||
| #include "iceberg/iceberg_bundle_export.h" | ||
|
|
||
| namespace iceberg::encryption { | ||
|
|
||
| /// \brief Register the standard encryption manager, if the bundle was built with | ||
| /// ICEBERG_ENCRYPTION=ON. Otherwise a no-op, and encrypted tables are not supported. | ||
| ICEBERG_BUNDLE_EXPORT void RegisterAll(); | ||
|
|
||
| /// \brief Whether the bundle was built with table encryption support. | ||
| ICEBERG_BUNDLE_EXPORT bool IsEncryptionAvailable(); | ||
|
|
||
| } // namespace iceberg::encryption |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new public headers are missing from the explicit install lists:
encryption_manager.h,encryption_register.handstandard_encryption_manager.h. Please add them to the core, bundle and encryption groups respectively and regenerate the public-header coverage. Installed consumers currently cannot include these APIs; the registration header should remain available when the bundle is built with encryption disabled.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the three missing headers to the install lists — encryption_manager.h as core, encryption_register.h tagged ICEBERG_PUBLIC_HEADERS_BUNDLE so it stays available with encryption disabled, and standard_encryption_manager.h in the existing encryption group — and regenerated cpp20_compatibility_internal.h.