Skip to content

feat(encryption): [15/N] Reject writes to encrypted tables - #4478

Open
xanderbailey wants to merge 5 commits into
apache:mainfrom
xanderbailey:xb/encryption-block-writes
Open

xanderbailey wants to merge 5 commits into
apache:mainfrom
xanderbailey:xb/encryption-block-writes

Conversation

@xanderbailey

@xanderbailey xanderbailey commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Working towards #3222

Rationale for this change

Writes to a table with encryption.key-id now raise NotImplementedError until encrypted manifests and manifest lists are wired into commits. Otherwise, once v3 manifest writing (#3624) lands, a commit would write plaintext manifests, holding the data file keys, to an encrypted table.

The check is in the snapshot producer, which writes the manifests, so it runs before any are written. Metadata-only updates are still allowed.

This will allow us to hookup the KMS service through the catalog without worry of people writing unencrypted files to an encrypted table.

Are these changes tested?

Yes, append, overwrite and delete raise on an encrypted table without writing any files, and a property update still works.

Are there any user-facing changes?

Writes to encrypted tables raise NotImplementedError.

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.
Comment thread pyiceberg/table/update/snapshot.py Outdated
Comment on lines +167 to +169
from pyiceberg.encryption.manager import _reject_encrypted_writes

_reject_encrypted_writes(transaction.table_metadata)

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!

@xanderbailey
xanderbailey marked this pull request as ready for review November 24, 2026 18:00

@Fokko Fokko left a comment

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.

Thanks @xanderbailey for adding this guard, the snapshot producer looks like the right place to me since every write that produces manifests goes through it, and it fails before any data files are written 👍 Left one comment about removing encryption.key-id in the same transaction, which gets around the check (Java rejects that at commit). I'm fine handling it in a follow-up if you prefer. One more small thing: dynamic_partition_overwrite writes the data files before the inner delete() creates a producer, so on an encrypted partitioned table it leaves orphan files before raising. Calling _reject_encrypted_writes at the top of dynamic_partition_overwrite would cover that.

Comment thread pyiceberg/table/update/snapshot.py Outdated
super().__init__(transaction)
from pyiceberg.encryption.manager import _reject_encrypted_writes

_reject_encrypted_writes(transaction.table_metadata)

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?

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.

Good catch, have changed to _reject_encrypted_writes(transaction._table.metadata, transaction.table_metadata) to check both are not encrypted which I think closes this gap

Comment thread tests/table/test_init.py Resolved
Comment thread tests/table/test_init.py
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.

Removing encryption.key-id earlier in the same transaction no longer gets
around the check.
A retry reruns _commit against refreshed metadata, so a table that became
encrypted in the meantime is still rejected before manifests are written.
It writes data files before its delete creates a snapshot producer, so
checking first avoids leaving orphan files behind.
@xanderbailey

Copy link
Copy Markdown
Contributor Author

Have also added a check above dynamic_partition_overwrite with a new test b3bc5fa

@xanderbailey

Copy link
Copy Markdown
Contributor Author

Thanks for the review @Fokko. This also made me realize that we're missing some validation in iceberg-rust for dropping key-id https://github.com/apache/iceberg-rust/pull/3824/changes

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants