-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-34990][SQL][TESTS] Add ParquetEncryptionSuite #32146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3f05a6e
[SPARK-34990] Add a test for Parquet Modular Encryption in Spark.
andersonm-ibm a8b6191
scalastyle
andersonm-ibm 876ec70
Address Gidon's comments - reference to VaultClient and remove AAD.
andersonm-ibm cafe027
Remove extra spaces in pom.xml
andersonm-ibm 8ecee1a
Address review comments - mainly rename test, remove prints and compa…
andersonm-ibm 0d9c98a
Fix style
andersonm-ibm a83d0ec
Move parquet encryption test to hive module to remove the old jackson…
andersonm-ibm 48e8a7a
Fixed import order
andersonm-ibm 20df92b
Use withSQLConf to set hadoop configuration for the test
andersonm-ibm 5e3f76c
Use InMemoryKms from parquet-hadoop test jar, instead of re-implement…
andersonm-ibm 4fe0dcb
Read magic string using RandomAccessFile instead of an external process.
andersonm-ibm 3bb9f60
Change test to use TestHiveSingleton like other tests in Hive module …
andersonm-ibm aa507a4
Use Charset
andersonm-ibm 599c71d
Add a comment, verify the first magic string instead of the last one,…
andersonm-ibm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
sql/hive/src/test/scala/org/apache/spark/sql/hive/ParquetEncryptionSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.hive | ||
|
|
||
| import java.io.File | ||
| import java.io.RandomAccessFile | ||
| import java.nio.charset.StandardCharsets | ||
| import java.util.Base64 | ||
|
|
||
| import org.apache.spark.sql.QueryTest | ||
| import org.apache.spark.sql.hive.test.TestHiveSingleton | ||
|
|
||
| /** | ||
| * A test suite that tests parquet modular encryption usage. | ||
| */ | ||
| class ParquetEncryptionSuite extends QueryTest with TestHiveSingleton { | ||
| import spark.implicits._ | ||
|
|
||
| private val encoder = Base64.getEncoder | ||
| private val footerKey = | ||
| encoder.encodeToString("0123456789012345".getBytes(StandardCharsets.UTF_8)) | ||
| private val key1 = encoder.encodeToString("1234567890123450".getBytes(StandardCharsets.UTF_8)) | ||
| private val key2 = encoder.encodeToString("1234567890123451".getBytes(StandardCharsets.UTF_8)) | ||
|
|
||
| test("SPARK-34990: Write and read an encrypted parquet") { | ||
| withTempDir { dir => | ||
| withSQLConf( | ||
| "parquet.crypto.factory.class" -> | ||
| "org.apache.parquet.crypto.keytools.PropertiesDrivenCryptoFactory", | ||
| "parquet.encryption.kms.client.class" -> | ||
| "org.apache.parquet.crypto.keytools.mocks.InMemoryKMS", | ||
| "parquet.encryption.key.list" -> | ||
| s"footerKey: ${footerKey}, key1: ${key1}, key2: ${key2}") { | ||
|
|
||
| val inputDF = Seq((1, 22, 333)).toDF("a", "b", "c") | ||
| val parquetDir = new File(dir, "parquet").getCanonicalPath | ||
| inputDF.write | ||
| .option("parquet.encryption.column.keys", "key1: a, b; key2: c") | ||
| .option("parquet.encryption.footer.key", "footerKey") | ||
| .parquet(parquetDir) | ||
|
|
||
| verifyParquetEncrypted(parquetDir) | ||
|
|
||
| val parquetDF = spark.read.parquet(parquetDir) | ||
| assert(parquetDF.inputFiles.nonEmpty) | ||
| val readDataset = parquetDF.select("a", "b", "c") | ||
| checkAnswer(readDataset, inputDF) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Verify that the directory contains an encrypted parquet in | ||
| * encrypted footer mode by means of checking for all the parquet part files | ||
| * in the parquet directory that their magic string is PARE, as defined in the spec: | ||
| * https://github.com/apache/parquet-format/blob/master/Encryption.md#54-encrypted-footer-mode | ||
| */ | ||
| private def verifyParquetEncrypted(parquetDir: String): Unit = { | ||
| val parquetPartitionFiles = getListOfParquetFiles(new File(parquetDir)) | ||
| assert(parquetPartitionFiles.size >= 1) | ||
| parquetPartitionFiles.foreach { parquetFile => | ||
| val magicString = "PARE" | ||
| val magicStringLength = magicString.length() | ||
| val byteArray = new Array[Byte](magicStringLength) | ||
| val randomAccessFile = new RandomAccessFile(parquetFile, "r") | ||
| try { | ||
| randomAccessFile.read(byteArray, 0, magicStringLength) | ||
| } finally { | ||
| randomAccessFile.close() | ||
| } | ||
| val stringRead = new String(byteArray, StandardCharsets.UTF_8) | ||
| assert(magicString == stringRead) | ||
| } | ||
| } | ||
|
|
||
| private def getListOfParquetFiles(dir: File): List[File] = { | ||
| dir.listFiles.filter(_.isFile).toList.filter { file => | ||
| file.getName.endsWith("parquet") | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.