kevinjqliu commented on code in PR #4149: URL: https://github.com/apache/iceberg-python/pull/4149#discussion_r4371707613
########## 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: I think we should raise here instead of returning `None`. Encryption is a v3 feature, and java rejects `encryption.key-id` on pre-v3 tables at creation. Once this is wired into writes, returning `None` would mean writes silently go out as plaintext even though the user asked for encryption. We should make sure the raise doesn't block loading the table or metadata-only commits, otherwise users can't `remove_properties("encryption.key-id")` to fix it. Pre-v3 tables have no `encryption-keys`, so nothing in them can be encrypted and reads don't need the manager. When we wire this up, the error should only surface when producing a snapshot (write side). Follow-up: we should reject `encryption.key-id` / `encryption.data-key-length` on pre-v3 tables at create / property update, like java's [`checkCompatibility`](https://github.com/apache/iceberg/blob/fb58513787756dedab6550d6695df36a27acb64e/core/src/main/java/org/apache/iceberg/encryption/EncryptionUtil.java#L207-L219). -- 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]
