|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +import io |
| 19 | + |
| 20 | +import pytest |
| 21 | + |
| 22 | +from pyiceberg.avro.encoder import BinaryEncoder |
| 23 | +from pyiceberg.encryption.key_metadata import KEY_METADATA_V1, StandardKeyMetadata |
| 24 | + |
| 25 | +AES128_KEY = b"0123456789012345" |
| 26 | + |
| 27 | +# Version byte, then the Avro-encoded encryption_key: zigzag length 16 followed by the key |
| 28 | +ENCODED_PREFIX = b"\x01\x20" + AES128_KEY |
| 29 | + |
| 30 | + |
| 31 | +def encode_key_metadata(encryption_key: bytes) -> bytes: |
| 32 | + """Encode key metadata directly, bypassing StandardKeyMetadata's validation.""" |
| 33 | + output = io.BytesIO() |
| 34 | + encoder = BinaryEncoder(output) |
| 35 | + encoder.write(bytes([KEY_METADATA_V1])) |
| 36 | + encoder.write_bytes(encryption_key) |
| 37 | + encoder.write_int(0) |
| 38 | + encoder.write_int(0) |
| 39 | + return output.getvalue() |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.parametrize( |
| 43 | + "key_metadata, encoded", |
| 44 | + [ |
| 45 | + (StandardKeyMetadata(encryption_key=AES128_KEY), ENCODED_PREFIX + b"\x00\x00"), |
| 46 | + (StandardKeyMetadata(encryption_key=AES128_KEY, aad_prefix=b"ad"), ENCODED_PREFIX + b"\x02\x04ad\x00"), |
| 47 | + ( |
| 48 | + StandardKeyMetadata(encryption_key=AES128_KEY, aad_prefix=b"ad", file_length=1024), |
| 49 | + ENCODED_PREFIX + b"\x02\x04ad\x02\x80\x10", |
| 50 | + ), |
| 51 | + (StandardKeyMetadata(encryption_key=AES128_KEY, aad_prefix=b""), ENCODED_PREFIX + b"\x02\x00\x00"), |
| 52 | + ], |
| 53 | +) |
| 54 | +def test_key_metadata_serialization(key_metadata: StandardKeyMetadata, encoded: bytes) -> None: |
| 55 | + assert key_metadata.to_bytes() == encoded |
| 56 | + assert StandardKeyMetadata.from_bytes(encoded) == key_metadata |
| 57 | + |
| 58 | + |
| 59 | +def test_key_metadata_defaults() -> None: |
| 60 | + key_metadata = StandardKeyMetadata(encryption_key=AES128_KEY) |
| 61 | + |
| 62 | + assert key_metadata.aad_prefix is None |
| 63 | + assert key_metadata.file_length is None |
| 64 | + |
| 65 | + |
| 66 | +def test_key_metadata_repr_redacts_encryption_key() -> None: |
| 67 | + key_metadata = StandardKeyMetadata(encryption_key=AES128_KEY) |
| 68 | + |
| 69 | + assert "encryption_key" not in repr(key_metadata) |
| 70 | + assert repr(AES128_KEY) not in repr(key_metadata) |
| 71 | + |
| 72 | + |
| 73 | +def test_key_metadata_empty_buffer() -> None: |
| 74 | + with pytest.raises(ValueError, match="Empty key metadata"): |
| 75 | + StandardKeyMetadata.from_bytes(b"") |
| 76 | + |
| 77 | + |
| 78 | +@pytest.mark.parametrize("data", [b"\x02", b"\x02" + ENCODED_PREFIX[1:] + b"\x00\x00"]) |
| 79 | +def test_key_metadata_unsupported_version(data: bytes) -> None: |
| 80 | + with pytest.raises(ValueError, match="Unsupported key metadata version: 2"): |
| 81 | + StandardKeyMetadata.from_bytes(data) |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.parametrize("key_length", [16, 24, 32]) |
| 85 | +def test_key_metadata_accepts_aes_key_lengths(key_length: int) -> None: |
| 86 | + key_metadata = StandardKeyMetadata(encryption_key=bytes(key_length)) |
| 87 | + |
| 88 | + assert StandardKeyMetadata.from_bytes(key_metadata.to_bytes()) == key_metadata |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.parametrize("key_length", [0, 4, 15, 20, 33]) |
| 92 | +def test_key_metadata_rejects_invalid_key_length(key_length: int) -> None: |
| 93 | + with pytest.raises(ValueError, match="Invalid encryption key in key metadata"): |
| 94 | + StandardKeyMetadata(encryption_key=bytes(key_length)) |
| 95 | + |
| 96 | + |
| 97 | +@pytest.mark.parametrize("key_length", [0, 4, 15, 20, 33]) |
| 98 | +def test_key_metadata_decode_rejects_invalid_key_length(key_length: int) -> None: |
| 99 | + with pytest.raises(ValueError, match="Invalid encryption key in key metadata"): |
| 100 | + StandardKeyMetadata.from_bytes(encode_key_metadata(bytes(key_length))) |
0 commit comments