xanderbailey commented on code in PR #4149:
URL: https://github.com/apache/iceberg-python/pull/4149#discussion_r4371626270


##########
pyiceberg/encryption/manager.py:
##########
@@ -0,0 +1,190 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Envelope key management for table encryption.
+
+A master key held by the key management service wraps key encryption keys 
(KEKs), which are
+stored in table metadata. A KEK wraps the key metadata of each snapshot's 
manifest list.
+"""
+
+from __future__ import annotations
+
+import time
+import uuid
+from collections.abc import Iterable
+
+from pyiceberg.encryption.ciphers import AesGcmCipher, AesKeySize, SecureKey
+from pyiceberg.encryption.key_metadata import StandardKeyMetadata
+from pyiceberg.encryption.kms import KeyManagementClient
+from pyiceberg.table import TableProperties
+from pyiceberg.table.metadata import EncryptedKey, TableMetadata, 
TableMetadataV3
+from pyiceberg.utils.properties import property_as_int
+
+KEK_CREATED_AT_PROPERTY = "KEY_TIMESTAMP"
+
+KEK_LIFESPAN_MS = 730 * 24 * 60 * 60 * 1000
+
+KEK_CACHE_TTL_SECONDS = 3600
+
+
+class EncryptionManager:
+    """Generates file keys and wraps manifest list key metadata for an 
encrypted table."""
+
+    def __init__(
+        self,
+        kms_client: KeyManagementClient,
+        table_key_id: str,
+        encryption_keys: Iterable[EncryptedKey] = (),
+        key_size: AesKeySize = AesKeySize.BITS_128,
+    ) -> None:
+        self._kms_client = kms_client
+        self._table_key_id = table_key_id
+        self._key_size = key_size
+        self._encryption_keys = {key.key_id: key for key in encryption_keys}
+        self._kek_cache: dict[str, tuple[SecureKey, float]] = {}
+
+    @classmethod
+    def from_table_metadata(cls, kms_client: KeyManagementClient | None, 
metadata: TableMetadata) -> EncryptionManager | None:
+        """Create a manager for the table, or return None if the table is not 
encrypted.
+
+        Args:
+            kms_client (KeyManagementClient | None): Wraps and unwraps KEKs 
with the table's master key.
+            metadata (TableMetadata): The table's metadata, holding 
`encryption.key-id` and its encryption keys.
+        """
+        if not isinstance(metadata, TableMetadataV3):

Review Comment:
   This came up https://github.com/apache/iceberg-rust/issues/3402 and was 
discussed here 
https://github.com/apache/iceberg-rust/pull/2453#discussion_r3328741702. Happy 
to change this in python and follow-up in rust



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to