Repository navigation
feat(encryption): [7/N] Add encryption key updates #4148
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,7 +29,13 @@ | |
| from pyiceberg.exceptions import CommitFailedException | ||
| from pyiceberg.partitioning import PARTITION_FIELD_ID_START, PartitionSpec | ||
| from pyiceberg.schema import Schema | ||
| from pyiceberg.table.metadata import SUPPORTED_TABLE_FORMAT_VERSION, TableMetadata, TableMetadataUtil | ||
| from pyiceberg.table.metadata import ( | ||
| SUPPORTED_TABLE_FORMAT_VERSION, | ||
| EncryptedKey, | ||
| TableMetadata, | ||
| TableMetadataUtil, | ||
| TableMetadataV3, | ||
| ) | ||
| from pyiceberg.table.refs import MAIN_BRANCH, SnapshotRef, SnapshotRefType | ||
| from pyiceberg.table.snapshots import ( | ||
| MetadataLogEntry, | ||
|
|
@@ -222,6 +228,16 @@ class RemovePartitionStatisticsUpdate(IcebergBaseModel): | |
| snapshot_id: int = Field(alias="snapshot-id") | ||
|
|
||
|
|
||
| class AddEncryptionKeyUpdate(IcebergBaseModel): | ||
| action: Literal["add-encryption-key"] = Field(default="add-encryption-key") | ||
| encryption_key: EncryptedKey = Field(alias="encryption-key") | ||
|
|
||
|
|
||
| class RemoveEncryptionKeyUpdate(IcebergBaseModel): | ||
|
Contributor
Author
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. Matches Rust's
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. |
||
| action: Literal["remove-encryption-key"] = Field(default="remove-encryption-key") | ||
| key_id: str = Field(alias="key-id") | ||
|
|
||
|
|
||
| TableUpdate = Annotated[ | ||
| AssignUUIDUpdate | ||
| | UpgradeFormatVersionUpdate | ||
|
|
@@ -243,7 +259,9 @@ class RemovePartitionStatisticsUpdate(IcebergBaseModel): | |
| | RemovePartitionSpecsUpdate | ||
| | RemoveSchemasUpdate | ||
| | SetPartitionStatisticsUpdate | ||
| | RemovePartitionStatisticsUpdate, | ||
| | RemovePartitionStatisticsUpdate | ||
| | AddEncryptionKeyUpdate | ||
| | RemoveEncryptionKeyUpdate, | ||
| Field(discriminator="action"), | ||
| ] | ||
|
|
||
|
|
@@ -680,6 +698,31 @@ def _( | |
| return base_metadata.model_copy(update={"partition_statistics": statistics}) | ||
|
|
||
|
|
||
| @_apply_table_update.register(AddEncryptionKeyUpdate) | ||
| def _(update: AddEncryptionKeyUpdate, base_metadata: TableMetadata, context: _TableMetadataUpdateContext) -> TableMetadata: | ||
| if not isinstance(base_metadata, TableMetadataV3): | ||
| raise ValueError(f"Encryption keys are not supported in format version {base_metadata.format_version}") | ||
|
Comment on lines
+703
to
+704
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. 👍 |
||
|
|
||
| if any(key.key_id == update.encryption_key.key_id for key in base_metadata.encryption_keys): | ||
|
Contributor
Author
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. Existing key id is a no-op, as in Rust's |
||
| return base_metadata | ||
|
|
||
| context.add_update(update) | ||
| return base_metadata.model_copy(update={"encryption_keys": base_metadata.encryption_keys + [update.encryption_key]}) | ||
|
|
||
|
|
||
| @_apply_table_update.register(RemoveEncryptionKeyUpdate) | ||
| def _(update: RemoveEncryptionKeyUpdate, base_metadata: TableMetadata, context: _TableMetadataUpdateContext) -> TableMetadata: | ||
| if not isinstance(base_metadata, TableMetadataV3): | ||
| raise ValueError(f"Encryption keys are not supported in format version {base_metadata.format_version}") | ||
|
|
||
| encryption_keys = [key for key in base_metadata.encryption_keys if key.key_id != update.key_id] | ||
| if len(encryption_keys) == len(base_metadata.encryption_keys): | ||
|
Contributor
Author
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. Removing a missing key is a no-op, as in Rust's |
||
| return base_metadata | ||
|
|
||
| context.add_update(update) | ||
| return base_metadata.model_copy(update={"encryption_keys": encryption_keys}) | ||
|
|
||
|
|
||
| def update_table_metadata( | ||
| base_metadata: TableMetadata, | ||
| updates: tuple[TableUpdate, ...], | ||
|
|
||
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.
Matches Rust's
TableUpdate::AddEncryptionKey.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.
and rest spec entry: https://github.com/apache/iceberg/blob/fb58513787756dedab6550d6695df36a27acb64e/open-api/rest-catalog-open-api.yaml#L3611-L3622