From eeb0d70078a6aa0d805a999193ca065e19ab0edc Mon Sep 17 00:00:00 2001 From: Xander Date: Tue, 24 Nov 2026 17:54:45 +0000 Subject: [PATCH 1/5] Reject writes to encrypted tables Snapshot producers raise for tables with encryption.key-id, so nothing commits plaintext manifests to an encrypted table before encrypted manifest and manifest list writes are wired up. Metadata-only updates are still allowed. --- pyiceberg/encryption/manager.py | 8 ++++++++ pyiceberg/table/update/snapshot.py | 3 +++ tests/table/test_init.py | 25 +++++++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/pyiceberg/encryption/manager.py b/pyiceberg/encryption/manager.py index bb0fd23e20..7287a73d69 100644 --- a/pyiceberg/encryption/manager.py +++ b/pyiceberg/encryption/manager.py @@ -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 diff --git a/pyiceberg/table/update/snapshot.py b/pyiceberg/table/update/snapshot.py index 7a37b76287..368fa2be4b 100644 --- a/pyiceberg/table/update/snapshot.py +++ b/pyiceberg/table/update/snapshot.py @@ -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) self.commit_uuid = commit_uuid or uuid.uuid4() self._io = io self._operation = operation diff --git a/tests/table/test_init.py b/tests/table/test_init.py index 547ca94fc9..1b7dbd2368 100644 --- a/tests/table/test_init.py +++ b/tests/table/test_init.py @@ -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 From 6e1705358298275f0e3d1e893e60d10485b454ff Mon Sep 17 00:00:00 2001 From: Xander Date: Fri, 4 Dec 2026 07:10:41 -0800 Subject: [PATCH 2/5] Use the module-level AlwaysTrue import in the encrypted write test --- tests/table/test_init.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/table/test_init.py b/tests/table/test_init.py index 1b7dbd2368..2ddcb10eac 100644 --- a/tests/table/test_init.py +++ b/tests/table/test_init.py @@ -2404,8 +2404,6 @@ def test_scan_encrypted_table(table_v3_encrypted: Table) -> None: 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)])) From 702e256f3213ba7651df5ad6ad8239713f421bd5 Mon Sep 17 00:00:00 2001 From: Xander Date: Fri, 4 Dec 2026 07:13:35 -0800 Subject: [PATCH 3/5] Check committed metadata when rejecting encrypted writes Removing encryption.key-id earlier in the same transaction no longer gets around the check. --- pyiceberg/encryption/manager.py | 6 +++--- pyiceberg/table/update/snapshot.py | 3 ++- tests/table/test_init.py | 2 ++ 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/pyiceberg/encryption/manager.py b/pyiceberg/encryption/manager.py index 7287a73d69..bfc0ab5fac 100644 --- a/pyiceberg/encryption/manager.py +++ b/pyiceberg/encryption/manager.py @@ -182,11 +182,11 @@ 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.""" +def _reject_encrypted_writes(*metadata: TableMetadata) -> None: + """Raise if any of the metadata is encrypted, until encrypted manifests and manifest lists can be written.""" from pyiceberg.table import TableProperties - if TableProperties.ENCRYPTION_KEY_ID in metadata.properties: + if any(TableProperties.ENCRYPTION_KEY_ID in m.properties for m in metadata): raise NotImplementedError("Writing to encrypted tables is not yet supported") diff --git a/pyiceberg/table/update/snapshot.py b/pyiceberg/table/update/snapshot.py index 368fa2be4b..1c1d1b373c 100644 --- a/pyiceberg/table/update/snapshot.py +++ b/pyiceberg/table/update/snapshot.py @@ -166,7 +166,8 @@ def __init__( super().__init__(transaction) from pyiceberg.encryption.manager import _reject_encrypted_writes - _reject_encrypted_writes(transaction.table_metadata) + # Committed metadata too, so removing the key in the same transaction can't bypass this + _reject_encrypted_writes(transaction._table.metadata, transaction.table_metadata) self.commit_uuid = commit_uuid or uuid.uuid4() self._io = io self._operation = operation diff --git a/tests/table/test_init.py b/tests/table/test_init.py index 2ddcb10eac..bfecde6d32 100644 --- a/tests/table/test_init.py +++ b/tests/table/test_init.py @@ -47,6 +47,7 @@ StaticTable, Table, TableIdentifier, + TableProperties, ) from pyiceberg.table.labels import Labels from pyiceberg.table.metadata import EncryptedKey, TableMetadataUtil, TableMetadataV1, TableMetadataV2, _generate_snapshot_id @@ -2411,6 +2412,7 @@ def test_writes_to_encrypted_table_are_rejected(table_v3_encrypted: Table, tmp_p lambda: table_v3_encrypted.append(df), lambda: table_v3_encrypted.overwrite(df), lambda: table_v3_encrypted.delete(AlwaysTrue()), + lambda: table_v3_encrypted.transaction().remove_properties(TableProperties.ENCRYPTION_KEY_ID).append(df), ]: with pytest.raises(NotImplementedError, match="Writing to encrypted tables is not yet supported"): write() From ec158452ad3f98c4f14f631265e3d2e85fe8f1c2 Mon Sep 17 00:00:00 2001 From: Xander Date: Fri, 4 Dec 2026 07:19:12 -0800 Subject: [PATCH 4/5] Check for encryption again when a snapshot producer commits A retry reruns _commit against refreshed metadata, so a table that became encrypted in the meantime is still rejected before manifests are written. --- pyiceberg/table/update/snapshot.py | 13 +++++++++---- tests/table/test_init.py | 12 ++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/pyiceberg/table/update/snapshot.py b/pyiceberg/table/update/snapshot.py index 1c1d1b373c..296c519110 100644 --- a/pyiceberg/table/update/snapshot.py +++ b/pyiceberg/table/update/snapshot.py @@ -164,10 +164,7 @@ def __init__( branch: str | None = MAIN_BRANCH, ) -> None: super().__init__(transaction) - from pyiceberg.encryption.manager import _reject_encrypted_writes - - # Committed metadata too, so removing the key in the same transaction can't bypass this - _reject_encrypted_writes(transaction._table.metadata, transaction.table_metadata) + self._reject_encrypted_writes() self.commit_uuid = commit_uuid or uuid.uuid4() self._io = io self._operation = operation @@ -338,7 +335,15 @@ def _summary(self, snapshot_properties: dict[str, str] = EMPTY_DICT) -> Summary: previous_summary=previous_snapshot.summary if previous_snapshot is not None else None, ) + def _reject_encrypted_writes(self) -> None: + from pyiceberg.encryption.manager import _reject_encrypted_writes + + # Committed metadata too, so removing the key in the same transaction can't bypass this + _reject_encrypted_writes(self._transaction._table.metadata, self._transaction.table_metadata) + def _commit(self) -> UpdatesAndRequirements: + # Again here, since a retry reruns this against refreshed metadata + self._reject_encrypted_writes() new_manifests = self._manifests() next_sequence_number = self._transaction.table_metadata.next_sequence_number() diff --git a/tests/table/test_init.py b/tests/table/test_init.py index bfecde6d32..b076b66014 100644 --- a/tests/table/test_init.py +++ b/tests/table/test_init.py @@ -2420,6 +2420,18 @@ def test_writes_to_encrypted_table_are_rejected(table_v3_encrypted: Table, tmp_p assert list(tmp_path.iterdir()) == [] +def test_write_is_rejected_when_table_is_encrypted_before_a_retry(table_v3: Table) -> None: + producer = table_v3.transaction().update_snapshot().fast_append() + + # A retry refreshes the table metadata, then reruns _commit + table_v3.metadata = table_v3.metadata.model_copy( + update={"properties": {**table_v3.metadata.properties, TableProperties.ENCRYPTION_KEY_ID: "master-1"}} + ) + + with pytest.raises(NotImplementedError, match="Writing to encrypted tables is not yet supported"): + producer._commit() + + def test_metadata_only_updates_to_encrypted_table_are_allowed(table_v3_encrypted: Table) -> None: transaction = table_v3_encrypted.transaction().set_properties(owner="me") From b3bc5fa64a77e6e2dd36a4e6432acf6caafe39be Mon Sep 17 00:00:00 2001 From: Xander Date: Fri, 4 Dec 2026 07:22:04 -0800 Subject: [PATCH 5/5] Reject dynamic partition overwrites of encrypted tables up front It writes data files before its delete creates a snapshot producer, so checking first avoids leaving orphan files behind. --- pyiceberg/table/__init__.py | 4 ++++ tests/table/test_init.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/pyiceberg/table/__init__.py b/pyiceberg/table/__init__.py index a8fcdb16e6..62cd13a203 100644 --- a/pyiceberg/table/__init__.py +++ b/pyiceberg/table/__init__.py @@ -617,6 +617,10 @@ def dynamic_partition_overwrite( snapshot_properties: Custom properties to be added to the snapshot summary branch: Branch Reference to run the dynamic partition overwrite operation """ + from pyiceberg.encryption.manager import _reject_encrypted_writes + + # Data files are written before the delete creates a snapshot producer + _reject_encrypted_writes(self._table.metadata, self.table_metadata) try: import pyarrow as pa except ModuleNotFoundError as e: diff --git a/tests/table/test_init.py b/tests/table/test_init.py index b076b66014..82255f8cf2 100644 --- a/tests/table/test_init.py +++ b/tests/table/test_init.py @@ -2420,6 +2420,21 @@ def test_writes_to_encrypted_table_are_rejected(table_v3_encrypted: Table, tmp_p assert list(tmp_path.iterdir()) == [] +def test_dynamic_partition_overwrite_to_encrypted_table_writes_nothing(table_v3_encrypted: Table, tmp_path: Path) -> None: + import pyarrow as pa + + spec = PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="x"), spec_id=0) + table_v3_encrypted.metadata = table_v3_encrypted.metadata.model_copy( + update={"location": f"file://{tmp_path}", "partition_specs": [spec]} + ) + df = pa.table({"x": [1, 2, 3]}, schema=pa.schema([pa.field("x", pa.int64(), nullable=False)])) + + with pytest.raises(NotImplementedError, match="Writing to encrypted tables is not yet supported"): + table_v3_encrypted.dynamic_partition_overwrite(df) + + assert list(tmp_path.iterdir()) == [] + + def test_write_is_rejected_when_table_is_encrypted_before_a_retry(table_v3: Table) -> None: producer = table_v3.transaction().update_snapshot().fast_append()