1919
2020#include < stdio.h>
2121
22+ #include < fstream>
23+ #include < optional>
2224#include < stdexcept>
2325#include < tuple>
2426#include < vector>
2527
2628#include < arrow/io/file.h>
2729
30+ #include " arrow/util/config.h"
2831#include " arrow/util/endian.h"
32+ #include " arrow/util/io_util.h"
2933#include " arrow/util/ubsan.h"
3034
3135#include " parquet/column_reader.h"
4347 * The files are saved in temporary folder and will be deleted after reading
4448 * them in read_configurations_test.cc test.
4549 *
50+ * To regenerate the corresponding files in the parquet-testing repository, set the
51+ * PARQUET_ENCRYPTION_TEST_OUTPUT_DIR environment variable to a directory. The files
52+ * are then written with the same layout as the parquet-testing files, and a copy of
53+ * each file (without the "tmp_" prefix) is written to that directory.
54+ *
4655 * A detailed description of the Parquet Modular Encryption specification can be found
4756 * here:
4857 * https://github.com/apache/parquet-format/blob/encryption/Encryption.md
6675 * identity storage in file metadata.
6776 * - Encryption configuration 6: Encrypt two columns and the footer, with different
6877 * keys. Use the alternative (AES_GCM_CTR_V1) algorithm.
78+ * - Encryption configuration 7: Encrypt two columns, with different keys.
79+ * Don't encrypt the footer.
80+ * Use the alternative (AES_GCM_CTR_V1) algorithm.
6981 */
7082
7183namespace parquet ::encryption::test {
@@ -78,8 +90,19 @@ class TestEncryptionConfiguration : public ::testing::Test {
7890 public:
7991 static void SetUpTestCase ();
8092
93+ void SetUp () override {
94+ #ifndef ARROW_WITH_SNAPPY
95+ if (output_dir_.has_value ()) {
96+ FAIL () << " Generating parquet-testing files requires Snappy compression" ;
97+ }
98+ #endif
99+ }
100+
81101 protected:
82- FileEncryptor encryptor_;
102+ // Set when generating files for the parquet-testing repository
103+ const std::optional<std::string> output_dir_ = GetOutputDir();
104+ FileEncryptor encryptor_ =
105+ output_dir_.has_value() ? FileEncryptor::ForParquetTestingFiles() : FileEncryptor();
83106
84107 std::string path_to_double_field_ = kDoubleFieldName ;
85108 std::string path_to_float_field_ = kFloatFieldName ;
@@ -93,6 +116,35 @@ class TestEncryptionConfiguration : public ::testing::Test {
93116 std::string file_name) {
94117 std::string file = temp_dir->path ().ToString () + file_name;
95118 encryptor_.EncryptFile (file, encryption_configurations);
119+ CopyToOutputDir (file, file_name);
120+ }
121+
122+ static std::optional<std::string> GetOutputDir () {
123+ auto maybe_output_dir =
124+ ::arrow::internal::GetEnvVar (" PARQUET_ENCRYPTION_TEST_OUTPUT_DIR" );
125+ if (!maybe_output_dir.ok ()) {
126+ return std::nullopt ;
127+ }
128+ return maybe_output_dir.MoveValueUnsafe ();
129+ }
130+
131+ // Copy the file to the output directory if it is set, so the parquet-testing
132+ // files can be regenerated.
133+ void CopyToOutputDir (const std::string& file, const std::string& file_name) const {
134+ if (!output_dir_.has_value ()) {
135+ return ;
136+ }
137+ std::string output_name = file_name;
138+ const std::string tmp_prefix = " tmp_" ;
139+ if (output_name.rfind (tmp_prefix, 0 ) == 0 ) {
140+ output_name = output_name.substr (tmp_prefix.size ());
141+ }
142+ std::ifstream src (file, std::ios::binary);
143+ std::ofstream dst (*output_dir_ + " /" + output_name, std::ios::binary);
144+ ASSERT_TRUE (src.good () && dst.good ())
145+ << " Failed to copy " << file << " to " << *output_dir_;
146+ dst << src.rdbuf ();
147+ ASSERT_TRUE (dst.good ()) << " Failed to write " << output_name;
96148 }
97149};
98150
@@ -203,6 +255,25 @@ TEST_F(TestEncryptionConfiguration, EncryptTwoColumnsAndFooterUseAES_GCM_CTR) {
203255 " tmp_encrypt_columns_and_footer_ctr.parquet.encrypted" ));
204256}
205257
258+ TEST_F (TestEncryptionConfiguration, EncryptTwoColumnsWithPlaintextFooterUseAES_GCM_CTR) {
259+ std::map<std::string, std::shared_ptr<ColumnEncryptionProperties>> encryption_cols7;
260+ encryption_cols7[path_to_double_field_] =
261+ ColumnEncryptionProperties::WithColumnKey (kColumnEncryptionKey1_ , " kc1" );
262+ encryption_cols7[path_to_float_field_] =
263+ ColumnEncryptionProperties::WithColumnKey (kColumnEncryptionKey2_ , " kc2" );
264+ encryption_cols7[kBooleanFieldName ] = ColumnEncryptionProperties::Unencrypted ();
265+
266+ FileEncryptionProperties::Builder file_encryption_builder_7 (kFooterEncryptionKey_ );
267+
268+ EXPECT_NO_THROW (
269+ this ->EncryptFile (file_encryption_builder_7.footer_key_metadata (" kf" )
270+ ->encrypted_columns (std::move (encryption_cols7))
271+ ->set_plaintext_footer ()
272+ ->algorithm (parquet::ParquetCipher::AES_GCM_CTR_V1 )
273+ ->build (),
274+ " tmp_encrypt_columns_plaintext_footer_ctr.parquet.encrypted" ));
275+ }
276+
206277enum class AadPrefixMode { None, Stored, External };
207278
208279class TestGeneratedEncryptionConfiguration
0 commit comments