Skip to content

Commit 3d95b9a

Browse files
roee88andersonm-ibm
authored andcommitted
fix: expose concrete Python exceptions
Signed-off-by: roee88 <[email protected]> Signed-off-by: Maya Anderson <[email protected]>
1 parent f2a45d2 commit 3d95b9a

6 files changed

Lines changed: 91 additions & 36 deletions

File tree

‎cpp/src/arrow/python/CMakeLists.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ if(WIN32)
6969
list(APPEND ARROW_PYTHON_SHARED_LINK_LIBS ${PYTHON_LIBRARIES} ${PYTHON_OTHER_LIBS})
7070
endif()
7171

72+
if(PARQUET_REQUIRE_ENCRYPTION)
73+
list(APPEND ARROW_PYTHON_SHARED_LINK_LIBS parquet_shared)
74+
endif()
75+
7276
set(ARROW_PYTHON_INCLUDES ${NUMPY_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS})
7377

7478
add_arrow_lib(arrow_python

‎cpp/src/arrow/python/parquet_encryption.cc‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717

1818
#include "arrow/python/parquet_encryption.h"
19+
#include "parquet/exception.h"
1920

2021
namespace arrow {
2122
namespace py {
@@ -75,6 +76,22 @@ std::shared_ptr<::parquet::encryption::KmsClient> PyKmsClientFactory::CreateKmsC
7576
return kms_client;
7677
}
7778

79+
arrow::Result<std::shared_ptr<::parquet::FileEncryptionProperties>>
80+
PyCryptoFactory::SafeGetFileEncryptionProperties(
81+
const ::parquet::encryption::KmsConnectionConfig& kms_connection_config,
82+
const ::parquet::encryption::EncryptionConfiguration& encryption_config) {
83+
PARQUET_CATCH_AND_RETURN(this->GetFileEncryptionProperties(
84+
kms_connection_config, encryption_config));
85+
}
86+
87+
arrow::Result<std::shared_ptr<::parquet::FileDecryptionProperties>>
88+
PyCryptoFactory::SafeGetFileDecryptionProperties(
89+
const ::parquet::encryption::KmsConnectionConfig& kms_connection_config,
90+
const ::parquet::encryption::DecryptionConfiguration& decryption_config) {
91+
PARQUET_CATCH_AND_RETURN(this->GetFileDecryptionProperties(
92+
kms_connection_config, decryption_config));
93+
}
94+
7895
} // namespace encryption
7996
} // namespace parquet
8097
} // namespace py

‎cpp/src/arrow/python/parquet_encryption.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "arrow/python/common.h"
2323
#include "arrow/python/visibility.h"
2424
#include "arrow/util/macros.h"
25+
#include "parquet/encryption/crypto_factory.h"
2526
#include "parquet/encryption/kms_client.h"
2627
#include "parquet/encryption/kms_client_factory.h"
2728

@@ -84,6 +85,22 @@ class ARROW_PYTHON_EXPORT PyKmsClientFactory
8485
PyKmsClientFactoryVtable vtable_;
8586
};
8687

88+
/// \brief A CryptoFactory that returns Results instead of throwing exceptions.
89+
class ARROW_PYTHON_EXPORT PyCryptoFactory : public ::parquet::encryption::CryptoFactory {
90+
public:
91+
arrow::Result<std::shared_ptr<::parquet::FileEncryptionProperties>> SafeGetFileEncryptionProperties(
92+
const ::parquet::encryption::KmsConnectionConfig& kms_connection_config,
93+
const ::parquet::encryption::EncryptionConfiguration& encryption_config);
94+
95+
/// The returned FileDecryptionProperties object will use the cache inside this
96+
/// CryptoFactory object, so please keep this
97+
/// CryptoFactory object alive along with the returned
98+
/// FileDecryptionProperties object.
99+
arrow::Result<std::shared_ptr<::parquet::FileDecryptionProperties>> SafeGetFileDecryptionProperties(
100+
const ::parquet::encryption::KmsConnectionConfig& kms_connection_config,
101+
const ::parquet::encryption::DecryptionConfiguration& decryption_config);
102+
};
103+
87104
} // namespace encryption
88105
} // namespace parquet
89106
} // namespace py

‎python/pyarrow/_parquet.pxd‎

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -651,28 +651,6 @@ ctypedef void CallbackCreateKmsClient(
651651
object,
652652
const CKmsConnectionConfig&, shared_ptr[CKmsClient]*)
653653

654-
cdef extern from "arrow/python/parquet_encryption.h" \
655-
namespace "arrow::py::parquet::encryption" nogil:
656-
cdef cppclass CPyKmsClientVtable \
657-
" arrow::py::parquet::encryption::PyKmsClientVtable":
658-
CPyKmsClientVtable()
659-
function[CallbackWrapKey] wrap_key
660-
function[CallbackUnwrapKey] unwrap_key
661-
662-
cdef cppclass CPyKmsClient\
663-
" arrow::py::parquet::encryption::PyKmsClient"(CKmsClient):
664-
CPyKmsClient(object handler, CPyKmsClientVtable vtable)
665-
666-
cdef cppclass CPyKmsClientFactoryVtable\
667-
" arrow::py::parquet::encryption::PyKmsClientFactoryVtable":
668-
CPyKmsClientFactoryVtable()
669-
function[CallbackCreateKmsClient] create_kms_client
670-
671-
cdef cppclass CPyKmsClientFactory\
672-
" arrow::py::parquet::encryption::PyKmsClientFactory"(
673-
CKmsClientFactory):
674-
CPyKmsClientFactory(object handler, CPyKmsClientFactoryVtable vtable)
675-
676654
cdef extern from "parquet/encryption/crypto_factory.h" \
677655
namespace "parquet::encryption" nogil:
678656
cdef cppclass CEncryptionConfiguration\
@@ -704,3 +682,33 @@ cdef extern from "parquet/encryption/crypto_factory.h" \
704682
const CDecryptionConfiguration& decryption_config) except +*
705683
void RemoveCacheEntriesForToken(const c_string& access_token) except +
706684
void RemoveCacheEntriesForAllTokens() except +
685+
686+
cdef extern from "arrow/python/parquet_encryption.h" \
687+
namespace "arrow::py::parquet::encryption" nogil:
688+
cdef cppclass CPyKmsClientVtable \
689+
" arrow::py::parquet::encryption::PyKmsClientVtable":
690+
CPyKmsClientVtable()
691+
function[CallbackWrapKey] wrap_key
692+
function[CallbackUnwrapKey] unwrap_key
693+
694+
cdef cppclass CPyKmsClient\
695+
" arrow::py::parquet::encryption::PyKmsClient"(CKmsClient):
696+
CPyKmsClient(object handler, CPyKmsClientVtable vtable)
697+
698+
cdef cppclass CPyKmsClientFactoryVtable\
699+
" arrow::py::parquet::encryption::PyKmsClientFactoryVtable":
700+
CPyKmsClientFactoryVtable()
701+
function[CallbackCreateKmsClient] create_kms_client
702+
703+
cdef cppclass CPyKmsClientFactory\
704+
" arrow::py::parquet::encryption::PyKmsClientFactory"(
705+
CKmsClientFactory):
706+
CPyKmsClientFactory(object handler, CPyKmsClientFactoryVtable vtable)
707+
708+
cdef cppclass CPyCryptoFactory" arrow::py::parquet::encryption::PyCryptoFactory"(CCryptoFactory):
709+
CResult[shared_ptr[CFileEncryptionProperties]] SafeGetFileEncryptionProperties(
710+
const CKmsConnectionConfig& kms_connection_config,
711+
const CEncryptionConfiguration& encryption_config) except +*
712+
CResult[shared_ptr[CFileDecryptionProperties]] SafeGetFileDecryptionProperties(
713+
const CKmsConnectionConfig& kms_connection_config,
714+
const CDecryptionConfiguration& decryption_config) except +*

‎python/pyarrow/_parquet.pyx‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,7 +1822,7 @@ cdef class CryptoFactory(_Weakrefable):
18221822
""" A factory that produces the low-level FileEncryptionProperties and
18231823
FileDecryptionProperties objects, from the high-level parameters."""
18241824
cdef:
1825-
unique_ptr[CCryptoFactory] factory
1825+
unique_ptr[CPyCryptoFactory] factory
18261826

18271827
# Avoid mistakingly creating attributes
18281828
__slots__ = ()
@@ -1835,7 +1835,7 @@ cdef class CryptoFactory(_Weakrefable):
18351835
kms_client_factory : a callable that accepts KmsConnectionConfig
18361836
and returns a KmsClient
18371837
"""
1838-
self.factory.reset(new CCryptoFactory())
1838+
self.factory.reset(new CPyCryptoFactory())
18391839

18401840
if callable(kms_client_factory):
18411841
self.init(kms_client_factory)
@@ -1875,10 +1875,15 @@ cdef class CryptoFactory(_Weakrefable):
18751875
file_encryption_properties : FileEncryptionProperties
18761876
File encryption properties.
18771877
"""
1878-
file_encryption_properties = \
1879-
self.factory.get().GetFileEncryptionProperties(
1878+
cdef:
1879+
CResult[shared_ptr[CFileEncryptionProperties]] file_encryption_properties_result
1880+
1881+
file_encryption_properties_result = \
1882+
self.factory.get().SafeGetFileEncryptionProperties(
18801883
deref(kms_connection_config.unwrap().get()),
18811884
deref(encryption_config.unwrap().get()))
1885+
file_encryption_properties = GetResultValue(
1886+
file_encryption_properties_result)
18821887
return FileEncryptionProperties.wrap(file_encryption_properties)
18831888

18841889
def file_decryption_properties(
@@ -1901,15 +1906,19 @@ cdef class CryptoFactory(_Weakrefable):
19011906
file_decryption_properties : FileDecryptionProperties
19021907
File decryption properties.
19031908
"""
1904-
cdef CDecryptionConfiguration c_decryption_config
1909+
cdef:
1910+
CDecryptionConfiguration c_decryption_config
1911+
CResult[shared_ptr[CFileDecryptionProperties]] c_file_decryption_properties
19051912
if decryption_config is None:
19061913
c_decryption_config = CDecryptionConfiguration()
19071914
else:
19081915
c_decryption_config = deref(decryption_config.unwrap().get())
1909-
file_decryption_properties = \
1910-
self.factory.get().GetFileDecryptionProperties(
1916+
c_file_decryption_properties = \
1917+
self.factory.get().SafeGetFileDecryptionProperties(
19111918
deref(kms_connection_config.unwrap().get()),
19121919
c_decryption_config)
1920+
file_decryption_properties = GetResultValue(
1921+
c_file_decryption_properties)
19131922
return FileDecryptionProperties.wrap(file_decryption_properties)
19141923

19151924
def remove_cache_entries_for_token(self, access_token):

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def kms_factory(kms_connection_configuration):
260260
return InMemoryKmsClient(kms_connection_configuration)
261261

262262
crypto_factory = pq.CryptoFactory(kms_factory)
263-
with pytest.raises(RuntimeError, match=r"column_keys"):
263+
with pytest.raises(OSError, match=r"Either column_keys or uniform_encryption must be set"):
264264
# Write with encryption properties
265265
write_encrypted_parquet(path, data_table, encryption_config,
266266
kms_connection_config, crypto_factory)
@@ -281,7 +281,7 @@ def kms_factory(kms_connection_configuration):
281281
return InMemoryKmsClient(kms_connection_configuration)
282282

283283
crypto_factory = pq.CryptoFactory(kms_factory)
284-
with pytest.raises(RuntimeError, match="footer_key.*KeyError"):
284+
with pytest.raises(KeyError, match="footer_key"):
285285
# Write with encryption properties
286286
write_encrypted_parquet(path, data_table, encryption_config,
287287
kms_connection_config, crypto_factory)
@@ -316,8 +316,8 @@ def kms_factory(kms_connection_configuration):
316316
# Exception thrown in wrap/unwrap calls
317317
return ThrowingKmsClient(kms_connection_configuration)
318318

319-
with pytest.raises(RuntimeError, match="Cannot Wrap Key.*ValueError"):
320-
crypto_factory = pq.CryptoFactory(kms_factory)
319+
crypto_factory = pq.CryptoFactory(kms_factory)
320+
with pytest.raises(ValueError, match="Cannot Wrap Key"):
321321
# Write with encryption properties
322322
write_encrypted_parquet(path, data_table, encryption_config,
323323
kms_connection_config, crypto_factory)
@@ -336,8 +336,8 @@ def kms_factory(kms_connection_configuration):
336336
raise ValueError('Cannot create KmsClient')
337337

338338
crypto_factory = pq.CryptoFactory(kms_factory)
339-
with pytest.raises(RuntimeError,
340-
match="Cannot create KmsClient.*ValueError"):
339+
with pytest.raises(ValueError,
340+
match="Cannot create KmsClient"):
341341
# Write with encryption properties
342342
write_encrypted_parquet(path, data_table, encryption_config,
343343
kms_connection_config, crypto_factory)
@@ -370,7 +370,7 @@ def kms_factory(kms_connection_configuration):
370370
return WrongTypeKmsClient(kms_connection_configuration)
371371

372372
crypto_factory = pq.CryptoFactory(kms_factory)
373-
with pytest.raises(RuntimeError, match=r"TypeError"):
373+
with pytest.raises(TypeError):
374374
# Write with encryption properties
375375
write_encrypted_parquet(path, data_table, encryption_config,
376376
kms_connection_config, crypto_factory)

0 commit comments

Comments
 (0)