|
| 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 | +#include "arrow/python/parquet_encryption.h" |
| 19 | +#include "parquet/exception.h" |
| 20 | + |
| 21 | +namespace arrow { |
| 22 | +namespace py { |
| 23 | +namespace parquet { |
| 24 | +namespace encryption { |
| 25 | + |
| 26 | +PyKmsClient::PyKmsClient(PyObject* handler, PyKmsClientVtable vtable) |
| 27 | + : handler_(handler), vtable_(std::move(vtable)) { |
| 28 | + Py_INCREF(handler); |
| 29 | +} |
| 30 | + |
| 31 | +PyKmsClient::~PyKmsClient() {} |
| 32 | + |
| 33 | +std::string PyKmsClient::WrapKey(const std::string& key_bytes, |
| 34 | + const std::string& master_key_identifier) { |
| 35 | + std::string wrapped; |
| 36 | + auto st = SafeCallIntoPython([&]() -> Status { |
| 37 | + vtable_.wrap_key(handler_.obj(), key_bytes, master_key_identifier, &wrapped); |
| 38 | + return CheckPyError(); |
| 39 | + }); |
| 40 | + if (!st.ok()) { |
| 41 | + throw ::parquet::ParquetStatusException(st); |
| 42 | + } |
| 43 | + return wrapped; |
| 44 | +} |
| 45 | + |
| 46 | +std::string PyKmsClient::UnwrapKey(const std::string& wrapped_key, |
| 47 | + const std::string& master_key_identifier) { |
| 48 | + std::string unwrapped; |
| 49 | + auto st = SafeCallIntoPython([&]() -> Status { |
| 50 | + vtable_.unwrap_key(handler_.obj(), wrapped_key, master_key_identifier, &unwrapped); |
| 51 | + return CheckPyError(); |
| 52 | + }); |
| 53 | + if (!st.ok()) { |
| 54 | + throw ::parquet::ParquetStatusException(st); |
| 55 | + } |
| 56 | + return unwrapped; |
| 57 | +} |
| 58 | + |
| 59 | +PyKmsClientFactory::PyKmsClientFactory(PyObject* handler, PyKmsClientFactoryVtable vtable) |
| 60 | + : handler_(handler), vtable_(std::move(vtable)) { |
| 61 | + Py_INCREF(handler); |
| 62 | +} |
| 63 | + |
| 64 | +PyKmsClientFactory::~PyKmsClientFactory() {} |
| 65 | + |
| 66 | +std::shared_ptr<::parquet::encryption::KmsClient> PyKmsClientFactory::CreateKmsClient( |
| 67 | + const ::parquet::encryption::KmsConnectionConfig& kms_connection_config) { |
| 68 | + std::shared_ptr<::parquet::encryption::KmsClient> kms_client; |
| 69 | + auto st = SafeCallIntoPython([&]() -> Status { |
| 70 | + vtable_.create_kms_client(handler_.obj(), kms_connection_config, &kms_client); |
| 71 | + return CheckPyError(); |
| 72 | + }); |
| 73 | + if (!st.ok()) { |
| 74 | + throw ::parquet::ParquetStatusException(st); |
| 75 | + } |
| 76 | + return kms_client; |
| 77 | +} |
| 78 | + |
| 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( |
| 84 | + this->GetFileEncryptionProperties(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( |
| 92 | + this->GetFileDecryptionProperties(kms_connection_config, decryption_config)); |
| 93 | +} |
| 94 | + |
| 95 | +} // namespace encryption |
| 96 | +} // namespace parquet |
| 97 | +} // namespace py |
| 98 | +} // namespace arrow |
0 commit comments