aboutsummaryrefslogtreecommitdiff
path: root/core/src/test/scala/org/apache/spark/FileServerSuite.scala
blob: 2c32b69715484c73dd6a41b6d5f6ced8c4e66e38 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
 * 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

import java.io._
import java.net.URI
import java.util.jar.{JarEntry, JarOutputStream}
import javax.net.ssl.SSLException

import com.google.common.io.{ByteStreams, Files}
import org.apache.commons.lang3.RandomUtils

import org.apache.spark.util.Utils

import SSLSampleConfigs._

class FileServerSuite extends SparkFunSuite with LocalSparkContext {

  @transient var tmpDir: File = _
  @transient var tmpFile: File = _
  @transient var tmpJarUrl: String = _

  def newConf: SparkConf = new SparkConf(loadDefaults = false).set("spark.authenticate", "false")

  override def beforeEach() {
    super.beforeEach()
    resetSparkContext()
  }

  override def beforeAll() {
    super.beforeAll()

    tmpDir = Utils.createTempDir()
    val testTempDir = new File(tmpDir, "test")
    testTempDir.mkdir()

    val textFile = new File(testTempDir, "FileServerSuite.txt")
    val pw = new PrintWriter(textFile)
    // scalastyle:off println
    pw.println("100")
    // scalastyle:on println
    pw.close()

    val jarFile = new File(testTempDir, "test.jar")
    val jarStream = new FileOutputStream(jarFile)
    val jar = new JarOutputStream(jarStream, new java.util.jar.Manifest())

    val jarEntry = new JarEntry(textFile.getName)
    jar.putNextEntry(jarEntry)

    val in = new FileInputStream(textFile)
    ByteStreams.copy(in, jar)

    in.close()
    jar.close()
    jarStream.close()

    tmpFile = textFile
    tmpJarUrl = jarFile.toURI.toURL.toString
  }

  override def afterAll() {
    try {
      Utils.deleteRecursively(tmpDir)
    } finally {
      super.afterAll()
    }
  }

  test("Distributing files locally") {
    sc = new SparkContext("local[4]", "test", newConf)
    sc.addFile(tmpFile.toString)
    val testData = Array((1, 1), (1, 1), (2, 1), (3, 5), (2, 2), (3, 0))
    val result = sc.parallelize(testData).reduceByKey {
      val path = SparkFiles.get("FileServerSuite.txt")
      val in = new BufferedReader(new FileReader(path))
      val fileVal = in.readLine().toInt
      in.close()
      _ * fileVal + _ * fileVal
    }.collect()
    assert(result.toSet === Set((1, 200), (2, 300), (3, 500)))
  }

  test("Distributing files locally security On") {
    val sparkConf = new SparkConf(false)
    sparkConf.set("spark.authenticate", "true")
    sparkConf.set("spark.authenticate.secret", "good")
    sc = new SparkContext("local[4]", "test", sparkConf)

    sc.addFile(tmpFile.toString)
    assert(sc.env.securityManager.isAuthenticationEnabled() === true)
    val testData = Array((1, 1), (1, 1), (2, 1), (3, 5), (2, 2), (3, 0))
    val result = sc.parallelize(testData).reduceByKey {
      val path = SparkFiles.get("FileServerSuite.txt")
      val in = new BufferedReader(new FileReader(path))
      val fileVal = in.readLine().toInt
      in.close()
      _ * fileVal + _ * fileVal
    }.collect()
    assert(result.toSet === Set((1, 200), (2, 300), (3, 500)))
  }

  test("Distributing files locally using URL as input") {
    // addFile("file:///....")
    sc = new SparkContext("local[4]", "test", newConf)
    sc.addFile(new File(tmpFile.toString).toURI.toString)
    val testData = Array((1, 1), (1, 1), (2, 1), (3, 5), (2, 2), (3, 0))
    val result = sc.parallelize(testData).reduceByKey {
      val path = SparkFiles.get("FileServerSuite.txt")
      val in = new BufferedReader(new FileReader(path))
      val fileVal = in.readLine().toInt
      in.close()
      _ * fileVal + _ * fileVal
    }.collect()
    assert(result.toSet === Set((1, 200), (2, 300), (3, 500)))
  }

  test ("Dynamically adding JARS locally") {
    sc = new SparkContext("local[4]", "test", newConf)
    sc.addJar(tmpJarUrl)
    val testData = Array((1, 1))
    sc.parallelize(testData).foreach { x =>
      if (Thread.currentThread.getContextClassLoader.getResource("FileServerSuite.txt") == null) {
        throw new SparkException("jar not added")
      }
    }
  }

  test("Distributing files on a standalone cluster") {
    sc = new SparkContext("local-cluster[1,1,1024]", "test", newConf)
    sc.addFile(tmpFile.toString)
    val testData = Array((1, 1), (1, 1), (2, 1), (3, 5), (2, 2), (3, 0))
    val result = sc.parallelize(testData).reduceByKey {
      val path = SparkFiles.get("FileServerSuite.txt")
      val in = new BufferedReader(new FileReader(path))
      val fileVal = in.readLine().toInt
      in.close()
      _ * fileVal + _ * fileVal
    }.collect()
    assert(result.toSet === Set((1, 200), (2, 300), (3, 500)))
  }

  test ("Dynamically adding JARS on a standalone cluster") {
    sc = new SparkContext("local-cluster[1,1,1024]", "test", newConf)
    sc.addJar(tmpJarUrl)
    val testData = Array((1, 1))
    sc.parallelize(testData).foreach { x =>
      if (Thread.currentThread.getContextClassLoader.getResource("FileServerSuite.txt") == null) {
        throw new SparkException("jar not added")
      }
    }
  }

  test ("Dynamically adding JARS on a standalone cluster using local: URL") {
    sc = new SparkContext("local-cluster[1,1,1024]", "test", newConf)
    sc.addJar(tmpJarUrl.replace("file", "local"))
    val testData = Array((1, 1))
    sc.parallelize(testData).foreach { x =>
      if (Thread.currentThread.getContextClassLoader.getResource("FileServerSuite.txt") == null) {
        throw new SparkException("jar not added")
      }
    }
  }

  test ("HttpFileServer should work with SSL") {
    val sparkConf = sparkSSLConfig()
    val sm = new SecurityManager(sparkConf)
    val server = new HttpFileServer(sparkConf, sm, 0)
    try {
      server.initialize()

      fileTransferTest(server, sm)
    } finally {
      server.stop()
    }
  }

  test ("HttpFileServer should work with SSL and good credentials") {
    val sparkConf = sparkSSLConfig()
    sparkConf.set("spark.authenticate", "true")
    sparkConf.set("spark.authenticate.secret", "good")

    val sm = new SecurityManager(sparkConf)
    val server = new HttpFileServer(sparkConf, sm, 0)
    try {
      server.initialize()

      fileTransferTest(server, sm)
    } finally {
      server.stop()
    }
  }

  test ("HttpFileServer should not work with valid SSL and bad credentials") {
    val sparkConf = sparkSSLConfig()
    sparkConf.set("spark.authenticate", "true")
    sparkConf.set("spark.authenticate.secret", "bad")

    val sm = new SecurityManager(sparkConf)
    val server = new HttpFileServer(sparkConf, sm, 0)
    try {
      server.initialize()

      intercept[IOException] {
        fileTransferTest(server)
      }
    } finally {
      server.stop()
    }
  }

  test ("HttpFileServer should not work with SSL when the server is untrusted") {
    val sparkConf = sparkSSLConfigUntrusted()
    val sm = new SecurityManager(sparkConf)
    val server = new HttpFileServer(sparkConf, sm, 0)
    try {
      server.initialize()

      intercept[SSLException] {
        fileTransferTest(server)
      }
    } finally {
      server.stop()
    }
  }

  def fileTransferTest(server: HttpFileServer, sm: SecurityManager = null): Unit = {
    val randomContent = RandomUtils.nextBytes(100)
    val file = File.createTempFile("FileServerSuite", "sslTests", tmpDir)
    Files.write(randomContent, file)
    server.addFile(file)

    val uri = new URI(server.serverUri + "/files/" + file.getName)

    val connection = if (sm != null && sm.isAuthenticationEnabled()) {
      Utils.constructURIForAuthentication(uri, sm).toURL.openConnection()
    } else {
      uri.toURL.openConnection()
    }

    if (sm != null) {
      Utils.setupSecureURLConnection(connection, sm)
    }

    val buf = ByteStreams.toByteArray(connection.getInputStream)
    assert(buf === randomContent)
  }

}