Github user vanzin commented on a diff in the pull request:
https://github.com/apache/spark/pull/8880#discussion_r45415002
--- Diff:
core/src/main/scala/org/apache/spark/crypto/CryptoStreamUtils.scala ---
@@ -0,0 +1,107 @@
+/*
+ * 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.crypto
+
+import java.io.{IOException, InputStream, OutputStream}
+import java.nio.ByteBuffer
+import java.util.Properties
+
+import com.intel.chimera.ConfigurationKeys._
+import com.intel.chimera.codec.CipherSuite
+import com.intel.chimera.codec.CryptoCodec
+import com.intel.chimera._
+import com.intel.chimera.random.OpensslSecureRandom
+
+import sun.misc.Cleaner
+import sun.nio.ch.DirectBuffer
+
+import org.apache.spark.crypto.CommonConfigurationKeys._
+import org.apache.spark.deploy.SparkHadoopUtil
+import org.apache.spark.SparkConf
+
+/**
+ * A util class for CryptoInputStream and CryptoOutputStream
+ */
+private[spark] object CryptoStreamUtils {
+ val MIN_BUFFER_SIZE = 512
+ val IV_LENGTH = 16
+
+ /** Forcibly free the direct buffer. */
+ def freeDB(buffer: ByteBuffer): Unit = {
+ if (buffer.isInstanceOf[DirectBuffer]) {
+ val bufferCleaner: Cleaner =
(buffer.asInstanceOf[DirectBuffer]).cleaner()
+ bufferCleaner.clean()
+ }
+ }
+
+ /** Read crypto buffer size */
+ private[this] def getBufferSize(sparkConf: SparkConf): Int = {
+ sparkConf.getInt(SPARK_SHUFFLE_CRYPTO_BUFFER_SIZE_KEY,
CommonConfigurationKeys
+ .SPARK_SHUFFLE_CRYPTO_BUFFER_SIZE_DEFAULT)
+ }
+
+ /** AES/CTR/NoPadding is required */
+ def checkCodec(codec: CryptoCodec): Unit = {
+ if (codec.getCipherSuite != CipherSuite.AES_CTR_NOPADDING) {
+ throw new RuntimeException("AES/CTR/NoPadding is required")
+ }
+ }
+
+ def createCryptoOutputStream(os: OutputStream, sparkConf: SparkConf):
CryptoOutputStream = {
+ val properties = getChimeraProps(sparkConf)
+ val iv = createInitializationVector(properties)
+ os.write(iv)
+ val credentials = SparkHadoopUtil.get.getCurrentUserCredentials()
+ val key = credentials.getSecretKey(SPARK_SHUFFLE_TOKEN)
+ new CryptoOutputStream(properties, os, key, iv)
+ }
+
+ def createCryptoInputStream(is: InputStream, sparkConf: SparkConf):
CryptoInputStream = {
+ val properties = getChimeraProps(sparkConf)
+ val iv: Array[Byte] = createInitializationVector(properties)
+ is.read(iv, 0, iv.length)
+ val credentials = SparkHadoopUtil.get.getCurrentUserCredentials()
+ val key = credentials.getSecretKey(SPARK_SHUFFLE_TOKEN)
+ new CryptoInputStream(properties, is, key, iv)
+ }
+
+ def getChimeraProps(sparkConf: SparkConf): Properties = {
+ val props = new Properties()
+ val cipherSuite = sparkConf.get(SPARK_SHUFFLE_CRYPTO_CIPHER_SUITE_KEY,
+ SPARK_SHUFFLE_CRYPTO_CIPHER_SUITE_DEFAULT)
+ props.setProperty(ConfigurationKeys.CHIMERA_CRYPTO_CIPHER_SUITE_KEY,
cipherSuite)
+ props.setProperty(
+ CHIMERA_CRYPTO_CODEC_CLASSES_KEY_PREFIX +
CipherSuite.getConfigSuffix(cipherSuite),
+ sparkConf.get(CHIMERA_CRYPTO_CODEC_CLASSES_KEY_PREFIX + cipherSuite,
+ DEFAULT_SPARK_SHUFFLE_CRYPTO_CODEC_CLASSES_AES_CTR_NOPADDING_KEY))
+ props
+ }
+
+ /**
+ * This method creates and initializes an IV (Initialization Vector)
+ *
+ * @param properties
+ * @return byte[]
+ * @throws IOException
+ */
+ private[this] def createInitializationVector(properties: Properties):
Array[Byte] = {
+ val iv = new Array[Byte](IV_LENGTH)
+ val secureRandom = new OpensslSecureRandom(properties)
--- End diff --
Ignoring my comment above about moving this code to the library, doesn't
this need to respect the user's configuration about which RNG to use?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]