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!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since we check transaction.table_metadata, which includes the staged updates, we can get around this within a single transaction:

with table.transaction() as tx:
    tx.remove_properties("encryption.key-id")
    tx.append(df)

Java rejects both removing and changing the key ID at commit time (RESTTableOperations.java#L255-L273). Should we port that check, either here or in a follow-up? Right now this check only runs when the producer is constructed, not again when _commit() is re-run on a retry against refreshed metadata. Moving it next to where the manifests are written would cover that case too. What do you think?

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
Comment on lines +2406 to +2407

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: AlwaysTrue is already imported at the top of this file.

Suggested change
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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: this one also passes on main, since set_properties never goes through a snapshot producer. I'm fine keeping it as a guard against over-blocking though.

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