Skip to content

Commit 640b66c

Browse files
author
Sreesh Maheshwar
committed
Address review comments
1 parent 0959c7d commit 640b66c

2 files changed

Lines changed: 111 additions & 80 deletions

File tree

‎python/pyarrow/_parquet_encryption.pyx‎

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -774,36 +774,38 @@ def create_decryption_properties(
774774
>>> table = pq.read_table('encrypted.parquet', decryption_properties=props)
775775
"""
776776
cdef:
777+
c_string c_footer_key_str
777778
CSecureString c_footer_key
778-
c_string c_aad_prefix
779-
CFileDecryptionPropertiesBuilder* builder
779+
CFileDecryptionPropertiesBuilder builder
780780
shared_ptr[CFileDecryptionProperties] props
781781

782-
footer_key_bytes = tobytes(footer_key)
783-
if len(footer_key_bytes) not in (16, 24, 32):
782+
if not isinstance(footer_key, bytes):
783+
raise TypeError(
784+
f"footer_key must be bytes, not {type(footer_key).__name__}"
785+
)
786+
if len(footer_key) not in (16, 24, 32):
784787
raise ValueError(
785-
f"footer_key must be 16, 24, or 32 bytes, got {len(footer_key_bytes)}"
788+
f"footer_key must be 16, 24, or 32 bytes, got {len(footer_key)}"
786789
)
787790

788-
c_footer_key = CSecureString(<c_string>footer_key_bytes)
789-
builder = new CFileDecryptionPropertiesBuilder()
790-
791-
try:
792-
builder.footer_key(c_footer_key)
791+
c_footer_key_str = <c_string>footer_key
792+
c_footer_key = CSecureString(move(c_footer_key_str))
793+
builder.footer_key(c_footer_key)
793794

794-
if aad_prefix is not None:
795-
c_aad_prefix = tobytes(aad_prefix)
796-
builder.aad_prefix(c_aad_prefix)
795+
if aad_prefix is not None:
796+
if not isinstance(aad_prefix, bytes):
797+
raise TypeError(
798+
f"aad_prefix must be bytes, not {type(aad_prefix).__name__}"
799+
)
800+
builder.aad_prefix(<c_string>aad_prefix)
797801

798-
if not check_footer_integrity:
799-
builder.disable_footer_signature_verification()
802+
if not check_footer_integrity:
803+
builder.disable_footer_signature_verification()
800804

801-
if allow_plaintext_files:
802-
builder.plaintext_files_allowed()
805+
if allow_plaintext_files:
806+
builder.plaintext_files_allowed()
803807

804-
props = builder.build()
805-
finally:
806-
del builder
808+
props = builder.build()
807809

808810
return FileDecryptionProperties.wrap(props)
809811

@@ -879,36 +881,40 @@ def create_encryption_properties(
879881
>>> pq.write_table(table, 'encrypted.parquet', encryption_properties=props)
880882
"""
881883
cdef:
884+
c_string c_footer_key_str
882885
CSecureString c_footer_key
883-
c_string c_aad_prefix
884-
CFileEncryptionPropertiesBuilder* builder
885886
shared_ptr[CFileEncryptionProperties] props
886887
ParquetCipher cipher
887888

888-
footer_key_bytes = tobytes(footer_key)
889-
if len(footer_key_bytes) not in (16, 24, 32):
889+
if not isinstance(footer_key, bytes):
890+
raise TypeError(
891+
f"footer_key must be bytes, not {type(footer_key).__name__}"
892+
)
893+
if len(footer_key) not in (16, 24, 32):
890894
raise ValueError(
891-
f"footer_key must be 16, 24, or 32 bytes, got {len(footer_key_bytes)}"
895+
f"footer_key must be 16, 24, or 32 bytes, got {len(footer_key)}"
892896
)
893897

894898
cipher = cipher_from_name(encryption_algorithm)
895-
c_footer_key = CSecureString(<c_string>footer_key_bytes)
896-
builder = new CFileEncryptionPropertiesBuilder(c_footer_key)
897-
898-
try:
899-
builder.algorithm(cipher)
900-
901-
if aad_prefix is not None:
902-
c_aad_prefix = tobytes(aad_prefix)
903-
builder.aad_prefix(c_aad_prefix)
904-
if not store_aad_prefix:
905-
builder.disable_aad_prefix_storage()
906-
907-
if plaintext_footer:
908-
builder.set_plaintext_footer()
909-
910-
props = builder.build()
911-
finally:
912-
del builder
899+
c_footer_key_str = <c_string>footer_key
900+
c_footer_key = CSecureString(move(c_footer_key_str))
901+
cdef CFileEncryptionPropertiesBuilder builder = \
902+
CFileEncryptionPropertiesBuilder(c_footer_key)
903+
904+
builder.algorithm(cipher)
905+
906+
if aad_prefix is not None:
907+
if not isinstance(aad_prefix, bytes):
908+
raise TypeError(
909+
f"aad_prefix must be bytes, not {type(aad_prefix).__name__}"
910+
)
911+
builder.aad_prefix(<c_string>aad_prefix)
912+
if not store_aad_prefix:
913+
builder.disable_aad_prefix_storage()
914+
915+
if plaintext_footer:
916+
builder.set_plaintext_footer()
917+
918+
props = builder.build()
913919

914920
return FileEncryptionProperties.wrap(props)

‎python/pyarrow/tests/parquet/test_encryption.py‎

Lines changed: 62 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
COL_KEY = b"1234567890123450"
3838
COL_KEY_NAME = "col_key"
3939

40+
DIRECT_KEY_128 = b"0123456789abcdef"
41+
DIRECT_KEY_192 = b"0123456789abcdef01234567"
42+
DIRECT_KEY_256 = b"0123456789abcdef0123456789abcdef"
43+
DIRECT_AAD_PREFIX = b"test_aad_prefix"
44+
4045

4146
# Marks all of the tests in this module
4247
# Ignore these with pytest ... -m 'not parquet_encryption'
@@ -727,15 +732,8 @@ def test_encrypted_parquet_read_table(tempdir, data_table, basic_encryption_conf
727732
class TestDirectKeyEncryption:
728733
"""Tests for create_encryption_properties / create_decryption_properties."""
729734

730-
KEY_128 = b"0123456789abcdef"
731-
KEY_192 = b"0123456789abcdef01234567"
732-
KEY_256 = b"0123456789abcdef0123456789abcdef"
733-
AAD_PREFIX = b"test_aad_prefix"
734-
735735
@pytest.mark.parametrize("key", [
736-
b"0123456789abcdef",
737-
b"0123456789abcdef01234567",
738-
b"0123456789abcdef0123456789abcdef",
736+
DIRECT_KEY_128, DIRECT_KEY_192, DIRECT_KEY_256,
739737
], ids=["aes128", "aes192", "aes256"])
740738
def test_roundtrip_key_sizes(self, tempdir, data_table, key):
741739
path = tempdir / f"direct_{len(key) * 8}.parquet"
@@ -751,14 +749,14 @@ def test_roundtrip_with_aad_prefix(self, tempdir, data_table):
751749
path = tempdir / "direct_aad.parquet"
752750

753751
enc_props = pe.create_encryption_properties(
754-
footer_key=self.KEY_128,
755-
aad_prefix=self.AAD_PREFIX,
752+
footer_key=DIRECT_KEY_128,
753+
aad_prefix=DIRECT_AAD_PREFIX,
756754
)
757755
pq.write_table(data_table, path, encryption_properties=enc_props)
758756

759757
dec_props = pe.create_decryption_properties(
760-
footer_key=self.KEY_128,
761-
aad_prefix=self.AAD_PREFIX,
758+
footer_key=DIRECT_KEY_128,
759+
aad_prefix=DIRECT_AAD_PREFIX,
762760
)
763761
result = pq.read_table(path, decryption_properties=dec_props)
764762
assert data_table.equals(result)
@@ -768,23 +766,23 @@ def test_roundtrip_aad_prefix_not_stored(self, tempdir, data_table):
768766
path = tempdir / "direct_aad_not_stored.parquet"
769767

770768
enc_props = pe.create_encryption_properties(
771-
footer_key=self.KEY_128,
772-
aad_prefix=self.AAD_PREFIX,
769+
footer_key=DIRECT_KEY_128,
770+
aad_prefix=DIRECT_AAD_PREFIX,
773771
store_aad_prefix=False,
774772
)
775773
pq.write_table(data_table, path, encryption_properties=enc_props)
776774

777775
# Reading without aad_prefix should fail
778776
dec_props_no_aad = pe.create_decryption_properties(
779-
footer_key=self.KEY_128,
777+
footer_key=DIRECT_KEY_128,
780778
)
781-
with pytest.raises(IOError):
779+
with pytest.raises(IOError, match="AAD"):
782780
pq.read_table(path, decryption_properties=dec_props_no_aad)
783781

784782
# Reading with correct aad_prefix should succeed
785783
dec_props = pe.create_decryption_properties(
786-
footer_key=self.KEY_128,
787-
aad_prefix=self.AAD_PREFIX,
784+
footer_key=DIRECT_KEY_128,
785+
aad_prefix=DIRECT_AAD_PREFIX,
788786
)
789787
result = pq.read_table(path, decryption_properties=dec_props)
790788
assert data_table.equals(result)
@@ -793,22 +791,23 @@ def test_wrong_aad_prefix_fails(self, tempdir, data_table):
793791
path = tempdir / "direct_wrong_aad.parquet"
794792

795793
enc_props = pe.create_encryption_properties(
796-
footer_key=self.KEY_128,
797-
aad_prefix=self.AAD_PREFIX,
794+
footer_key=DIRECT_KEY_128,
795+
aad_prefix=DIRECT_AAD_PREFIX,
798796
)
799797
pq.write_table(data_table, path, encryption_properties=enc_props)
800798

801799
dec_props = pe.create_decryption_properties(
802-
footer_key=self.KEY_128,
800+
footer_key=DIRECT_KEY_128,
803801
aad_prefix=b"wrong_prefix",
804802
)
805-
with pytest.raises(IOError):
803+
with pytest.raises(IOError, match="AAD"):
806804
pq.read_table(path, decryption_properties=dec_props)
807805

808806
def test_encrypted_file_has_pare_magic(self, tempdir, data_table):
809807
path = tempdir / "direct_magic.parquet"
810808

811-
enc_props = pe.create_encryption_properties(footer_key=self.KEY_128)
809+
enc_props = pe.create_encryption_properties(
810+
footer_key=DIRECT_KEY_128)
812811
pq.write_table(data_table, path, encryption_properties=enc_props)
813812

814813
with open(path, "rb") as f:
@@ -819,43 +818,47 @@ def test_plaintext_footer(self, tempdir, data_table):
819818
path = tempdir / "direct_plaintext_footer.parquet"
820819

821820
enc_props = pe.create_encryption_properties(
822-
footer_key=self.KEY_128,
821+
footer_key=DIRECT_KEY_128,
823822
plaintext_footer=True,
824823
)
825824
pq.write_table(data_table, path, encryption_properties=enc_props)
826825

827-
dec_props = pe.create_decryption_properties(footer_key=self.KEY_128)
826+
dec_props = pe.create_decryption_properties(
827+
footer_key=DIRECT_KEY_128)
828828
result = pq.read_table(path, decryption_properties=dec_props)
829829
assert data_table.equals(result)
830830

831831
def test_aes_gcm_ctr_v1_algorithm(self, tempdir, data_table):
832832
path = tempdir / "direct_ctr.parquet"
833833

834834
enc_props = pe.create_encryption_properties(
835-
footer_key=self.KEY_128,
835+
footer_key=DIRECT_KEY_128,
836836
encryption_algorithm="AES_GCM_CTR_V1",
837837
)
838838
pq.write_table(data_table, path, encryption_properties=enc_props)
839839

840-
dec_props = pe.create_decryption_properties(footer_key=self.KEY_128)
840+
dec_props = pe.create_decryption_properties(
841+
footer_key=DIRECT_KEY_128)
841842
result = pq.read_table(path, decryption_properties=dec_props)
842843
assert data_table.equals(result)
843844

844845
def test_wrong_key_fails(self, tempdir, data_table):
845846
path = tempdir / "direct_wrong_key.parquet"
846847

847-
enc_props = pe.create_encryption_properties(footer_key=self.KEY_128)
848+
enc_props = pe.create_encryption_properties(
849+
footer_key=DIRECT_KEY_128)
848850
pq.write_table(data_table, path, encryption_properties=enc_props)
849851

850852
wrong_key = b"fedcba9876543210"
851853
dec_props = pe.create_decryption_properties(footer_key=wrong_key)
852-
with pytest.raises(IOError):
854+
with pytest.raises(IOError, match="decrypt"):
853855
pq.read_table(path, decryption_properties=dec_props)
854856

855857
def test_reading_without_decryption_fails(self, tempdir, data_table):
856858
path = tempdir / "direct_no_decrypt.parquet"
857859

858-
enc_props = pe.create_encryption_properties(footer_key=self.KEY_128)
860+
enc_props = pe.create_encryption_properties(
861+
footer_key=DIRECT_KEY_128)
859862
pq.write_table(data_table, path, encryption_properties=enc_props)
860863

861864
with pytest.raises(IOError):
@@ -867,7 +870,7 @@ def test_allow_plaintext_files(self, tempdir, data_table):
867870
pq.write_table(data_table, path)
868871

869872
dec_props = pe.create_decryption_properties(
870-
footer_key=self.KEY_128,
873+
footer_key=DIRECT_KEY_128,
871874
allow_plaintext_files=True,
872875
)
873876
result = pq.read_table(path, decryption_properties=dec_props)
@@ -878,19 +881,21 @@ def test_plaintext_file_rejected_by_default(self, tempdir, data_table):
878881
path = tempdir / "plaintext_rejected.parquet"
879882
pq.write_table(data_table, path)
880883

881-
dec_props = pe.create_decryption_properties(footer_key=self.KEY_128)
882-
with pytest.raises(IOError):
884+
dec_props = pe.create_decryption_properties(
885+
footer_key=DIRECT_KEY_128)
886+
with pytest.raises(IOError, match="plaintext"):
883887
pq.read_table(path, decryption_properties=dec_props)
884888

885889
def test_check_footer_integrity_false(self, tempdir, data_table):
886890
"""check_footer_integrity=False should still allow decryption."""
887891
path = tempdir / "direct_no_footer_check.parquet"
888892

889-
enc_props = pe.create_encryption_properties(footer_key=self.KEY_128)
893+
enc_props = pe.create_encryption_properties(
894+
footer_key=DIRECT_KEY_128)
890895
pq.write_table(data_table, path, encryption_properties=enc_props)
891896

892897
dec_props = pe.create_decryption_properties(
893-
footer_key=self.KEY_128,
898+
footer_key=DIRECT_KEY_128,
894899
check_footer_integrity=False,
895900
)
896901
result = pq.read_table(path, decryption_properties=dec_props)
@@ -901,7 +906,7 @@ def test_plaintext_footer_has_par1_magic(self, tempdir, data_table):
901906
path = tempdir / "direct_plaintext_magic.parquet"
902907

903908
enc_props = pe.create_encryption_properties(
904-
footer_key=self.KEY_128,
909+
footer_key=DIRECT_KEY_128,
905910
plaintext_footer=True,
906911
)
907912
pq.write_table(data_table, path, encryption_properties=enc_props)
@@ -914,12 +919,32 @@ def test_invalid_key_length_raises(self):
914919
with pytest.raises(ValueError, match="16, 24, or 32 bytes"):
915920
pe.create_encryption_properties(footer_key=b"short")
916921

922+
with pytest.raises(ValueError, match="16, 24, or 32 bytes"):
923+
pe.create_encryption_properties(footer_key=b"")
924+
917925
with pytest.raises(ValueError, match="16, 24, or 32 bytes"):
918926
pe.create_decryption_properties(footer_key=b"short")
919927

920928
def test_invalid_algorithm_raises(self):
921929
with pytest.raises(ValueError):
922930
pe.create_encryption_properties(
923-
footer_key=self.KEY_128,
931+
footer_key=DIRECT_KEY_128,
924932
encryption_algorithm="INVALID",
925933
)
934+
935+
def test_footer_key_rejects_non_bytes(self):
936+
with pytest.raises(TypeError, match="footer_key must be bytes"):
937+
pe.create_encryption_properties(footer_key="0123456789abcdef")
938+
939+
with pytest.raises(TypeError, match="footer_key must be bytes"):
940+
pe.create_decryption_properties(footer_key="0123456789abcdef")
941+
942+
with pytest.raises(TypeError, match="footer_key must be bytes"):
943+
pe.create_encryption_properties(footer_key=None)
944+
945+
def test_aad_prefix_rejects_str(self, tempdir, data_table):
946+
with pytest.raises(TypeError, match="aad_prefix must be bytes"):
947+
pe.create_encryption_properties(
948+
footer_key=DIRECT_KEY_128,
949+
aad_prefix="not_bytes",
950+
)

0 commit comments

Comments
 (0)