Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pyiceberg/encryption/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ def _data_key_size(metadata: TableMetadata) -> AesKeySize:
return AesKeySize.from_key_length(TableProperties.ENCRYPTION_DATA_KEY_LENGTH_DEFAULT if key_length is None else key_length)


def _reject_encrypted_writes(metadata: TableMetadata) -> None:
"""Raise if the table is encrypted, until encrypted manifests and manifest lists can be written."""
from pyiceberg.table import TableProperties

if TableProperties.ENCRYPTION_KEY_ID in metadata.properties:
raise NotImplementedError("Writing to encrypted tables is not yet supported")


def _now_ms() -> int:
return time.time_ns() // 1_000_000

Expand Down
3 changes: 3 additions & 0 deletions pyiceberg/table/update/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ def __init__(
branch: str | None = MAIN_BRANCH,
) -> None:
super().__init__(transaction)
from pyiceberg.encryption.manager import _reject_encrypted_writes

_reject_encrypted_writes(transaction.table_metadata)
Comment on lines +167 to +169

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this spot makes the most sense to block this, everything in the library goes through here today and hopefully we won't have to block this for long!

self.commit_uuid = commit_uuid or uuid.uuid4()
self._io = io
self._operation = operation
Expand Down
25 changes: 25 additions & 0 deletions tests/table/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -2401,6 +2401,31 @@ def test_scan_encrypted_table(table_v3_encrypted: Table) -> None:
assert table_v3_encrypted.scan().to_arrow().num_rows == 0


def test_writes_to_encrypted_table_are_rejected(table_v3_encrypted: Table, tmp_path: Path) -> None:
import pyarrow as pa

from pyiceberg.expressions import AlwaysTrue

table_v3_encrypted.metadata = table_v3_encrypted.metadata.model_copy(update={"location": f"file://{tmp_path}"})
df = pa.table({"x": [1, 2, 3]}, schema=pa.schema([pa.field("x", pa.int64(), nullable=False)]))

for write in [
lambda: table_v3_encrypted.append(df),
lambda: table_v3_encrypted.overwrite(df),
lambda: table_v3_encrypted.delete(AlwaysTrue()),
]:
with pytest.raises(NotImplementedError, match="Writing to encrypted tables is not yet supported"):
write()

assert list(tmp_path.iterdir()) == []


def test_metadata_only_updates_to_encrypted_table_are_allowed(table_v3_encrypted: Table) -> None:
transaction = table_v3_encrypted.transaction().set_properties(owner="me")

assert transaction.table_metadata.properties["owner"] == "me"


def test_inspect_encrypted_table(table_v3_encrypted: Table) -> None:
assert table_v3_encrypted.inspect.manifests().num_rows == 0
assert table_v3_encrypted.inspect.all_manifests().num_rows == 0
Expand Down
Loading