Skip to content
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

[SPARK-34990][SQL][TESTS] Add ParquetEncryptionSuite #32146

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions sql/hive/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.parquet</groupId>
<artifactId>parquet-hadoop</artifactId>
<version>${parquet.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<!--
<dependency>
<groupId>com.google.guava</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* 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)
}
}
}

private def verifyParquetEncrypted(parquetDir: String) = {
attilapiros marked this conversation as resolved.
Show resolved Hide resolved
val parquetPartitionFiles = getListOfParquetFiles(new File(parquetDir))
assert(parquetPartitionFiles.size >= 1)
attilapiros marked this conversation as resolved.
Show resolved Hide resolved
val parquetFile = parquetPartitionFiles.maxBy(_.lastModified)

val magicString = "PARE"
val magicStringLength = magicString.length()
val byteArray = new Array[Byte](magicStringLength)
val randomAccessFile = new RandomAccessFile(parquetFile, "r")
try {
randomAccessFile.seek(randomAccessFile.length() - magicStringLength);
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")
}
}
}