Repository navigation
feat(encryption): [14/N] Add EncryptedOutputFile #4380
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
Merged
kevinjqliu
merged 3 commits into
apache:main
from
xanderbailey:xb/encryption-write-manifests
Nov 19, 2026
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,15 +14,19 @@ | |
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| """Input files for metadata encrypted with AGS1 streams.""" | ||
| """Input and output files for metadata encrypted with AGS1 streams.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import io | ||
| from collections.abc import Callable | ||
| from dataclasses import replace | ||
|
|
||
| from typing_extensions import override | ||
|
|
||
| from pyiceberg.encryption.key_metadata import StandardKeyMetadata | ||
| from pyiceberg.encryption.stream import _decrypt_ags1 | ||
| from pyiceberg.io import InputFile, InputStream | ||
| from pyiceberg.encryption.stream import _decrypt_ags1, _encrypt_ags1 | ||
| from pyiceberg.io import InputFile, InputStream, OutputFile, OutputStream | ||
|
|
||
|
|
||
| class EncryptedInputFile(InputFile): | ||
|
|
@@ -48,3 +52,63 @@ def open(self, seekable: bool = True) -> InputStream: | |
| def _decrypt(self) -> bytes: | ||
| with self._input_file.open(seekable=False) as stream: | ||
| return _decrypt_ags1(stream.read(), self._key_metadata) | ||
|
|
||
|
|
||
| class EncryptedOutputFile(OutputFile): | ||
| """Encrypts a file, such as a manifest, into an AGS1 stream when its output stream is closed.""" | ||
|
|
||
| def __init__(self, output_file: OutputFile, key_metadata: StandardKeyMetadata) -> None: | ||
| super().__init__(output_file.location) | ||
| self._output_file = output_file | ||
| self._key_metadata = key_metadata | ||
| self._file_length: int | None = None | ||
|
|
||
| @property | ||
| def key_metadata(self) -> StandardKeyMetadata: | ||
| """The key metadata to record for the file, including the length of the written file.""" | ||
| if self._file_length is None: | ||
| raise ValueError(f"Cannot get key metadata before the file is written: {self.location}") | ||
| return replace(self._key_metadata, file_length=self._file_length) | ||
|
|
||
| @override | ||
| def __len__(self) -> int: | ||
| """Return the length of the encrypted file, in bytes.""" | ||
| return self._file_length if self._file_length is not None else len(self._output_file) | ||
|
|
||
| @override | ||
| def exists(self) -> bool: | ||
| """Check whether the file exists.""" | ||
| return self._output_file.exists() | ||
|
|
||
| @override | ||
| def to_input_file(self) -> InputFile: | ||
| """Return an input file that decrypts the written file.""" | ||
| return EncryptedInputFile(self._output_file.to_input_file(), self.key_metadata) | ||
|
|
||
| @override | ||
| def create(self, overwrite: bool = False) -> OutputStream: | ||
| """Return a stream that buffers the file, then encrypts and writes it when closed.""" | ||
| return _EncryptingOutputStream(self._output_file.create(overwrite=overwrite), self._key_metadata, self._set_file_length) | ||
|
|
||
| def _set_file_length(self, file_length: int) -> None: | ||
| self._file_length = file_length | ||
|
Comment on lines
+91
to
+94
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
|
|
||
| class _EncryptingOutputStream(io.BytesIO): | ||
| def __init__(self, output_stream: OutputStream, key_metadata: StandardKeyMetadata, on_close: Callable[[int], None]) -> None: | ||
| super().__init__() | ||
| self._output_stream = output_stream | ||
| self._key_metadata = key_metadata | ||
| self._on_close = on_close | ||
|
|
||
| def close(self) -> None: | ||
| if self.closed: | ||
| return | ||
| try: | ||
| encrypted = _encrypt_ags1(self.getvalue(), self._key_metadata) | ||
| with self._output_stream: | ||
| self._output_stream.write(encrypted) | ||
| self._on_close(len(encrypted)) | ||
| finally: | ||
| # Close even if the write fails, so garbage collection does not retry it. | ||
| super().close() | ||
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
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.
note that we proactively update the
_key_metadata's file_length after writing