You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kyuubi.apache.org by fe...@apache.org on 2022/07/04 06:50:10 UTC
[incubator-kyuubi] branch master updated: [KYUUBI #2801] Add local dir allow list and check the application access path URI
This is an automated email from the ASF dual-hosted git repository.
feiwang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-kyuubi.git
The following commit(s) were added to refs/heads/master by this push:
new b585fb425 [KYUUBI #2801] Add local dir allow list and check the application access path URI
b585fb425 is described below
commit b585fb4257995d5dd8905c0a25e62a13a1264b77
Author: Fei Wang <fw...@ebay.com>
AuthorDate: Mon Jul 4 14:50:02 2022 +0800
[KYUUBI #2801] Add local dir allow list and check the application access path URI
### _Why are the changes needed?_
To close #2801
Customer might set some parameters to upload the local files, such as spark.files=/etc/security/keytabs/kyuubi.keytab. It is not security for kyuubi server.
In this pr, I introduce a local dir allow list and customer can only upload the files under these dirs.
### _How was this patch tested?_
- [x] Add some test cases that check the changes thoroughly including negative and positive cases if possible
- [ ] Add screenshots for manual tests if appropriate
- [x] [Run test](https://kyuubi.apache.org/docs/latest/develop_tools/testing.html#running-tests) locally before make a pull request
Closes #2991 from turboFei/local_white_list.
Closes #2801
6315067d [Fei Wang] comments
deb9fa8a [Fei Wang] remove unused
3330fa98 [Fei Wang] refactor
6eca2f8b [Fei Wang] move to KyuubiApplicationManager and add ut
1290d9e2 [Fei Wang] add more
f14ec7d4 [Fei Wang] add more path configs and add comment
82357183 [Fei Wang] refactor
f201f577 [Fei Wang] disable relative path
f806e69a [Fei Wang] refactor
4f2faa6a [Fei Wang] save
Authored-by: Fei Wang <fw...@ebay.com>
Signed-off-by: Fei Wang <fw...@ebay.com>
---
docs/deployment/settings.md | 1 +
.../org/apache/kyuubi/config/KyuubiConf.scala | 18 +++++-
.../kyuubi/engine/KyuubiApplicationManager.scala | 50 ++++++++++++++-
.../kyuubi/engine/spark/SparkProcessBuilder.scala | 21 ++++++
.../kyuubi/session/KyuubiBatchSessionImpl.scala | 10 +++
.../org/apache/kyuubi/session/KyuubiSession.scala | 2 +
.../apache/kyuubi/session/KyuubiSessionImpl.scala | 11 +++-
.../engine/KyuubiApplicationManagerSuite.scala | 75 ++++++++++++++++++++++
8 files changed, 185 insertions(+), 3 deletions(-)
diff --git a/docs/deployment/settings.md b/docs/deployment/settings.md
index 57a244ef2..563d500c6 100644
--- a/docs/deployment/settings.md
+++ b/docs/deployment/settings.md
@@ -444,6 +444,7 @@ kyuubi.session.engine.trino.connection.catalog|<undefined>|The default cat
kyuubi.session.engine.trino.connection.url|<undefined>|The server url that trino engine will connect to|string|1.5.0
kyuubi.session.engine.trino.main.resource|<undefined>|The package used to create Trino engine remote job. If it is undefined, Kyuubi will use the default|string|1.5.0
kyuubi.session.idle.timeout|PT6H|session idle timeout, it will be closed when it's not accessed for this duration|duration|1.2.0
+kyuubi.session.local.dir.allow.list||The local dir list that are allowed to access by the kyuubi session application. User might set some parameters such as `spark.files` and it will upload some local files when launching the kyuubi engine, if the local dir allow list is defined, kyuubi will check whether the path to upload is in the allow list. Note that, if it is empty, there is no limitation for that and please use absolute path list.|seq|1.6.0
kyuubi.session.name|<undefined>|A human readable name of session and we use empty string by default. This name will be recorded in event. Note that, we only apply this value from session conf.|string|1.4.0
kyuubi.session.timeout|PT6H|(deprecated)session timeout, it will be closed when it's not accessed for this duration|duration|1.0.0
diff --git a/kyuubi-common/src/main/scala/org/apache/kyuubi/config/KyuubiConf.scala b/kyuubi-common/src/main/scala/org/apache/kyuubi/config/KyuubiConf.scala
index 5f85b9879..d48c5750d 100644
--- a/kyuubi-common/src/main/scala/org/apache/kyuubi/config/KyuubiConf.scala
+++ b/kyuubi-common/src/main/scala/org/apache/kyuubi/config/KyuubiConf.scala
@@ -17,6 +17,7 @@
package org.apache.kyuubi.config
+import java.io.File
import java.time.Duration
import java.util.Locale
import java.util.concurrent.ConcurrentHashMap
@@ -153,7 +154,8 @@ case class KyuubiConf(loadSysDefault: Boolean = true) extends Logging {
SERVER_NAME,
SERVER_LIMIT_CONNECTIONS_PER_IPADDRESS,
SERVER_LIMIT_CONNECTIONS_PER_USER_IPADDRESS,
- SERVER_LIMIT_CONNECTIONS_PER_USER)
+ SERVER_LIMIT_CONNECTIONS_PER_USER,
+ SESSION_LOCAL_DIR_ALLOW_LIST)
def getUserDefaults(user: String): KyuubiConf = {
val cloned = KyuubiConf(false)
@@ -951,6 +953,20 @@ object KyuubiConf {
.booleanConf
.createWithDefault(true)
+ val SESSION_LOCAL_DIR_ALLOW_LIST: ConfigEntry[Seq[String]] =
+ buildConf("kyuubi.session.local.dir.allow.list")
+ .doc("The local dir list that are allowed to access by the kyuubi session application. User" +
+ " might set some parameters such as `spark.files` and it will upload some local files" +
+ " when launching the kyuubi engine, if the local dir allow list is defined, kyuubi will" +
+ " check whether the path to upload is in the allow list. Note that, if it is empty, there" +
+ " is no limitation for that and please use absolute path list.")
+ .version("1.6.0")
+ .stringConf
+ .checkValue(dir => dir.startsWith(File.separator), "the dir should be absolute path")
+ .transform(dir => dir.stripSuffix(File.separator) + File.separator)
+ .toSequence()
+ .createWithDefault(Nil)
+
val BATCH_APPLICATION_CHECK_INTERVAL: ConfigEntry[Long] =
buildConf("kyuubi.batch.application.check.interval")
.doc("The interval to check batch job application information.")
diff --git a/kyuubi-server/src/main/scala/org/apache/kyuubi/engine/KyuubiApplicationManager.scala b/kyuubi-server/src/main/scala/org/apache/kyuubi/engine/KyuubiApplicationManager.scala
index 7dacd381f..fc8d63ea4 100644
--- a/kyuubi-server/src/main/scala/org/apache/kyuubi/engine/KyuubiApplicationManager.scala
+++ b/kyuubi-server/src/main/scala/org/apache/kyuubi/engine/KyuubiApplicationManager.scala
@@ -17,11 +17,14 @@
package org.apache.kyuubi.engine
-import java.util.ServiceLoader
+import java.io.File
+import java.net.{URI, URISyntaxException}
+import java.util.{Locale, ServiceLoader}
import scala.collection.JavaConverters._
import scala.util.control.NonFatal
+import org.apache.kyuubi.KyuubiException
import org.apache.kyuubi.config.KyuubiConf
import org.apache.kyuubi.engine.KubernetesApplicationOperation.LABEL_KYUUBI_UNIQUE_KEY
import org.apache.kyuubi.engine.flink.FlinkProcessBuilder
@@ -106,6 +109,40 @@ object KyuubiApplicationManager {
conf.set(FlinkProcessBuilder.TAG_KEY, newTag)
}
+ private[kyuubi] def checkApplicationPathURI(uri: URI, conf: KyuubiConf): Unit = {
+ val localDirAllowList = conf.get(KyuubiConf.SESSION_LOCAL_DIR_ALLOW_LIST)
+ if (localDirAllowList.nonEmpty && (uri.getScheme == null || uri.getScheme == "file")) {
+ if (!uri.getPath.startsWith(File.separator)) {
+ throw new KyuubiException(
+ s"Relative path ${uri.getPath} is not allowed, please use absolute path.")
+ }
+
+ if (!localDirAllowList.exists(uri.getPath.startsWith(_))) {
+ throw new KyuubiException(
+ s"The file ${uri.getPath} to access is not in the local dir allow list" +
+ s" [${localDirAllowList.mkString(",")}].")
+ }
+ }
+ }
+
+ private def checkSparkPathURIs(
+ appConf: Map[String, String],
+ kyuubiConf: KyuubiConf): Unit = {
+ if (kyuubiConf.get(KyuubiConf.SESSION_LOCAL_DIR_ALLOW_LIST).nonEmpty) {
+ SparkProcessBuilder.PATH_CONFIGS.flatMap { key =>
+ appConf.get(key).map(_.split(",")).getOrElse(Array.empty)
+ }.filter(_.nonEmpty).foreach { path =>
+ val uri =
+ try {
+ new URI(path)
+ } catch {
+ case e: URISyntaxException => throw new IllegalArgumentException(e)
+ }
+ checkApplicationPathURI(uri, kyuubiConf)
+ }
+ }
+ }
+
/**
* Add a unique tag on the application
* @param applicationTag a unique tag to identify application
@@ -132,4 +169,15 @@ object KyuubiApplicationManager {
case _ =>
}
}
+
+ def checkApplicationAccessPaths(
+ applicationType: String,
+ appConf: Map[String, String],
+ kyuubiConf: KyuubiConf): Unit = {
+ applicationType.toUpperCase(Locale.ROOT) match {
+ case appType if appType.startsWith("SPARK") => checkSparkPathURIs(appConf, kyuubiConf)
+ case appType if appType.startsWith("FLINK") => // TODO: check flink app access local paths
+ case _ =>
+ }
+ }
}
diff --git a/kyuubi-server/src/main/scala/org/apache/kyuubi/engine/spark/SparkProcessBuilder.scala b/kyuubi-server/src/main/scala/org/apache/kyuubi/engine/spark/SparkProcessBuilder.scala
index 736f368fd..544d3effe 100644
--- a/kyuubi-server/src/main/scala/org/apache/kyuubi/engine/spark/SparkProcessBuilder.scala
+++ b/kyuubi-server/src/main/scala/org/apache/kyuubi/engine/spark/SparkProcessBuilder.scala
@@ -169,6 +169,27 @@ object SparkProcessBuilder {
final val TAG_KEY = "spark.yarn.tags"
final val MASTER_KEY = "spark.master"
+ /**
+ * The path configs from Spark project that might upload local files:
+ * - SparkSubmit
+ * - org.apache.spark.deploy.yarn.Client::prepareLocalResources
+ * - KerberosConfDriverFeatureStep::configurePod
+ * - KubernetesUtils.uploadAndTransformFileUris
+ */
+ final val PATH_CONFIGS = Seq(
+ SPARK_FILES,
+ "spark.jars",
+ "spark.archives",
+ "spark.yarn.jars",
+ "spark.yarn.dist.files",
+ "spark.yarn.dist.pyFiles",
+ "spark.yarn.dist.jars",
+ "spark.yarn.dist.archives",
+ "spark.kerberos.keytab",
+ "spark.yarn.keytab",
+ "spark.kubernetes.kerberos.krb5.path",
+ "spark.kubernetes.file.upload.path")
+
final private[spark] val CONF = "--conf"
final private[spark] val CLASS = "--class"
final private[spark] val PROXY_USER = "--proxy-user"
diff --git a/kyuubi-server/src/main/scala/org/apache/kyuubi/session/KyuubiBatchSessionImpl.scala b/kyuubi-server/src/main/scala/org/apache/kyuubi/session/KyuubiBatchSessionImpl.scala
index 9c1af80fb..d236fc97d 100644
--- a/kyuubi-server/src/main/scala/org/apache/kyuubi/session/KyuubiBatchSessionImpl.scala
+++ b/kyuubi-server/src/main/scala/org/apache/kyuubi/session/KyuubiBatchSessionImpl.scala
@@ -26,6 +26,7 @@ import org.apache.hive.service.rpc.thrift.TProtocolVersion
import org.apache.kyuubi.client.api.v1.dto.BatchRequest
import org.apache.kyuubi.config.KyuubiConf
+import org.apache.kyuubi.engine.KyuubiApplicationManager
import org.apache.kyuubi.events.{EventBus, KyuubiSessionEvent}
import org.apache.kyuubi.metrics.MetricsConstants.{CONN_OPEN, CONN_TOTAL}
import org.apache.kyuubi.metrics.MetricsSystem
@@ -94,6 +95,13 @@ class KyuubiBatchSessionImpl(
Option(sessionEvent)
}
+ override def checkSessionAccessPathURIs(): Unit = {
+ KyuubiApplicationManager.checkApplicationAccessPaths(
+ batchRequest.getBatchType,
+ normalizedConf,
+ sessionManager.getConf)
+ }
+
override def open(): Unit = {
MetricsSystem.tracing { ms =>
ms.incCount(CONN_TOTAL)
@@ -123,6 +131,8 @@ class KyuubiBatchSessionImpl(
sessionManager.insertMetadata(metaData)
}
+ checkSessionAccessPathURIs()
+
// we should call super.open before running batch job submission operation
super.open()
diff --git a/kyuubi-server/src/main/scala/org/apache/kyuubi/session/KyuubiSession.scala b/kyuubi-server/src/main/scala/org/apache/kyuubi/session/KyuubiSession.scala
index 64dc649e5..d7eaa14a6 100644
--- a/kyuubi-server/src/main/scala/org/apache/kyuubi/session/KyuubiSession.scala
+++ b/kyuubi-server/src/main/scala/org/apache/kyuubi/session/KyuubiSession.scala
@@ -34,6 +34,8 @@ abstract class KyuubiSession(
def getSessionEvent: Option[KyuubiSessionEvent]
+ def checkSessionAccessPathURIs(): Unit
+
private[kyuubi] def handleSessionException(f: => Unit): Unit = {
try {
f
diff --git a/kyuubi-server/src/main/scala/org/apache/kyuubi/session/KyuubiSessionImpl.scala b/kyuubi-server/src/main/scala/org/apache/kyuubi/session/KyuubiSessionImpl.scala
index 1a4e4146c..667d86ac8 100644
--- a/kyuubi-server/src/main/scala/org/apache/kyuubi/session/KyuubiSessionImpl.scala
+++ b/kyuubi-server/src/main/scala/org/apache/kyuubi/session/KyuubiSessionImpl.scala
@@ -27,7 +27,7 @@ import org.apache.kyuubi.client.KyuubiSyncThriftClient
import org.apache.kyuubi.config.KyuubiConf
import org.apache.kyuubi.config.KyuubiConf._
import org.apache.kyuubi.config.KyuubiReservedKeys.KYUUBI_ENGINE_CREDENTIALS_KEY
-import org.apache.kyuubi.engine.EngineRef
+import org.apache.kyuubi.engine.{EngineRef, KyuubiApplicationManager}
import org.apache.kyuubi.events.{EventBus, KyuubiSessionEvent}
import org.apache.kyuubi.ha.client.DiscoveryClientProvider._
import org.apache.kyuubi.metrics.MetricsConstants._
@@ -82,6 +82,13 @@ class KyuubiSessionImpl(
Option(sessionEvent)
}
+ override def checkSessionAccessPathURIs(): Unit = {
+ KyuubiApplicationManager.checkApplicationAccessPaths(
+ sessionConf.get(ENGINE_TYPE),
+ sessionConf.getAll,
+ sessionManager.getConf)
+ }
+
private var _client: KyuubiSyncThriftClient = _
def client: KyuubiSyncThriftClient = _client
@@ -93,6 +100,8 @@ class KyuubiSessionImpl(
ms.incCount(MetricRegistry.name(CONN_OPEN, user))
}
+ checkSessionAccessPathURIs()
+
// we should call super.open before running launch engine operation
super.open()
diff --git a/kyuubi-server/src/test/scala/org/apache/kyuubi/engine/KyuubiApplicationManagerSuite.scala b/kyuubi-server/src/test/scala/org/apache/kyuubi/engine/KyuubiApplicationManagerSuite.scala
new file mode 100644
index 000000000..c4bc06c0d
--- /dev/null
+++ b/kyuubi-server/src/test/scala/org/apache/kyuubi/engine/KyuubiApplicationManagerSuite.scala
@@ -0,0 +1,75 @@
+/*
+ * 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.kyuubi.engine
+
+import java.net.URI
+
+import org.apache.kyuubi.{KyuubiException, KyuubiFunSuite}
+import org.apache.kyuubi.config.KyuubiConf
+
+class KyuubiApplicationManagerSuite extends KyuubiFunSuite {
+ test("application access path uri") {
+ val localDirLimitConf = KyuubiConf()
+ .set(KyuubiConf.SESSION_LOCAL_DIR_ALLOW_LIST, Seq("/apache/kyuubi"))
+ val noLocalDirLimitConf = KyuubiConf()
+
+ var uri = new URI("/apache/kyuubi/a.jar")
+ KyuubiApplicationManager.checkApplicationPathURI(uri, localDirLimitConf)
+ KyuubiApplicationManager.checkApplicationPathURI(uri, noLocalDirLimitConf)
+
+ uri = new URI("/apache/kyuubijar")
+ var e = intercept[KyuubiException] {
+ KyuubiApplicationManager.checkApplicationPathURI(uri, localDirLimitConf)
+ }
+ assert(e.getMessage.contains("is not in the local dir allow list"))
+ KyuubiApplicationManager.checkApplicationPathURI(uri, noLocalDirLimitConf)
+
+ uri = new URI("hdfs:/apache/kyuubijar")
+ KyuubiApplicationManager.checkApplicationPathURI(uri, localDirLimitConf)
+ KyuubiApplicationManager.checkApplicationPathURI(uri, noLocalDirLimitConf)
+
+ uri = new URI("path/to/kyuubijar")
+ e = intercept[KyuubiException] {
+ KyuubiApplicationManager.checkApplicationPathURI(uri, localDirLimitConf)
+ }
+ assert(e.getMessage.contains("please use absolute path"))
+ KyuubiApplicationManager.checkApplicationPathURI(uri, noLocalDirLimitConf)
+
+ var appConf = Map("spark.files" -> "/apache/kyuubi/jars/a.jar")
+ KyuubiApplicationManager.checkApplicationAccessPaths(
+ "SPARK",
+ appConf,
+ localDirLimitConf)
+ KyuubiApplicationManager.checkApplicationAccessPaths(
+ "SPARK_SQL",
+ appConf,
+ localDirLimitConf)
+ KyuubiApplicationManager.checkApplicationAccessPaths(
+ "SPARK",
+ appConf,
+ noLocalDirLimitConf)
+
+ appConf = Map("spark.files" -> "/apache/jars/a.jar")
+ intercept[KyuubiException] {
+ KyuubiApplicationManager.checkApplicationAccessPaths(
+ "SPARK",
+ appConf,
+ localDirLimitConf)
+ }
+ }
+}