Skip to content

Commit

Permalink
Add modify acls
Browse files Browse the repository at this point in the history
  • Loading branch information
tgravescs committed Jun 24, 2014
1 parent 420c1c3 commit 72eb0ac
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 29 deletions.
64 changes: 51 additions & 13 deletions core/src/main/scala/org/apache/spark/SecurityManager.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,15 @@ import org.apache.spark.deploy.SparkHadoopUtil
* secure the UI if it has data that other users should not be allowed to see. The javax
* servlet filter specified by the user can authenticate the user and then once the user
* is logged in, Spark can compare that user versus the view acls to make sure they are
* authorized to view the UI. The configs 'spark.ui.acls.enable' and 'spark.ui.view.acls'
* authorized to view the UI. The configs 'spark.acls.enable' and 'spark.ui.view.acls'
* control the behavior of the acls. Note that the person who started the application
* always has view access to the UI.
*
* Spark has a set of modify acls that controls which users have permission to modify a single
* application. This would include things like killing the application. By default the person who
* started the application has modify access. For modify access through the UI, you must have a
* filter that does authentication in place for the modify acls to work properly.
*
* Spark does not currently support encryption after authentication.
*
* At this point spark has multiple communication protocols that need to be secured and
Expand Down Expand Up @@ -137,18 +142,27 @@ private[spark] class SecurityManager(sparkConf: SparkConf) extends Logging {
private val sparkSecretLookupKey = "sparkCookie"

private val authOn = sparkConf.getBoolean("spark.authenticate", false)
private var uiAclsOn = sparkConf.getBoolean("spark.ui.acls.enable", false)
// keep spark.ui.acls.enable for backwards compatibility with 1.0
private var aclsOn = sparkConf.getOption("spark.acls.enable").getOrElse(
sparkConf.get("spark.ui.acls.enable", "false")).toBoolean

private var viewAcls: Set[String] = _

// list of users who have permission to modify the application. This should
// apply to both UI and CLI for things like killing the application.
private var modifyAcls: Set[String] = _

// always add the current user and SPARK_USER to the viewAcls
private val defaultAclUsers = Seq[String](System.getProperty("user.name", ""),
Option(System.getenv("SPARK_USER")).getOrElse(""))
setViewAcls(defaultAclUsers, sparkConf.get("spark.ui.view.acls", ""))
setModifyAcls(defaultAclUsers, sparkConf.get("spark.modify.acls", ""))

private val secretKey = generateSecretKey()
logInfo("SecurityManager: authentication " + (if (authOn) "enabled" else "disabled") +
"; ui acls " + (if (uiAclsOn) "enabled" else "disabled") +
"; users with view permissions: " + viewAcls.toString())
"; ui acls " + (if (aclsOn) "enabled" else "disabled") +
"; users with view permissions: " + viewAcls.toString() +
"; users with modify permissions: " + modifyAcls.toString())

// Set our own authenticator to properly negotiate user/password for HTTP connections.
// This is needed by the HTTP client fetching from the HttpServer. Put here so its
Expand All @@ -170,17 +184,26 @@ private[spark] class SecurityManager(sparkConf: SparkConf) extends Logging {
}

private[spark] def setViewAcls(defaultUsers: Seq[String], allowedUsers: String) {
viewAcls = (defaultUsers ++ allowedUsers.split(',')).map(_.trim()).filter(!_.isEmpty).toSet
viewAcls = (defaultUsers ++ allowedUsers.split(',')).map(_.trim()).filter(!_.isEmpty).toSet
logInfo("Changing view acls to: " + viewAcls.mkString(","))
}

private[spark] def setViewAcls(defaultUser: String, allowedUsers: String) {
setViewAcls(Seq[String](defaultUser), allowedUsers)
}

private[spark] def setUIAcls(aclSetting: Boolean) {
uiAclsOn = aclSetting
logInfo("Changing acls enabled to: " + uiAclsOn)
private[spark] def getViewAcls: String = viewAcls.mkString(",")

private[spark] def setModifyAcls(defaultUsers: Seq[String], allowedUsers: String) {
modifyAcls = (defaultUsers ++ allowedUsers.split(',')).map(_.trim()).filter(!_.isEmpty).toSet
logInfo("Changing modify acls to: " + modifyAcls.mkString(","))
}

private[spark] def getModifyAcls: String = modifyAcls.mkString(",")

private[spark] def setAcls(aclSetting: Boolean) {
aclsOn = aclSetting
logInfo("Changing acls enabled to: " + aclsOn)
}

/**
Expand Down Expand Up @@ -224,22 +247,37 @@ private[spark] class SecurityManager(sparkConf: SparkConf) extends Logging {
* Check to see if Acls for the UI are enabled
* @return true if UI authentication is enabled, otherwise false
*/
def uiAclsEnabled(): Boolean = uiAclsOn
def aclsEnabled(): Boolean = aclsOn

/**
* Checks the given user against the view acl list to see if they have
* authorization to view the UI. If the UI acls must are disabled
* via spark.ui.acls.enable, all users have view access.
* authorization to view the UI. If the UI acls are disabled
* via spark.acls.enable, all users have view access.
*
* @param user to see if is authorized
* @return true is the user has permission, otherwise false
*/
def checkUIViewPermissions(user: String): Boolean = {
logDebug("user=" + user + " uiAclsEnabled=" + uiAclsEnabled() + " viewAcls=" +
logDebug("user=" + user + " aclsEnabled=" + aclsEnabled() + " viewAcls=" +
viewAcls.mkString(","))
if (uiAclsEnabled() && (user != null) && (!viewAcls.contains(user))) false else true
if (aclsEnabled() && (user != null) && (!viewAcls.contains(user))) false else true
}

/**
* Checks the given user against the modify acl list to see if they have
* authorization to modify the application. If the UI acls are disabled
* via spark.acls.enable, all users have modify access.
*
* @param user to see if is authorized
* @return true is the user has permission, otherwise false
*/
def checkModifyPermissions(user: String): Boolean = {
logDebug("user=" + user + " aclsEnabled=" + aclsEnabled() + " modifyAcls=" +
modifyAcls.mkString(","))
if (aclsEnabled() && (user != null) && (!modifyAcls.contains(user))) false else true
}


/**
* Check to see if authentication for the Spark communication protocols is enabled
* @return true if authentication is enabled, otherwise false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private[ui] class JobProgressTab(parent: SparkUI) extends WebUITab(parent, "stag
def isFairScheduler = listener.schedulingMode.exists(_ == SchedulingMode.FAIR)

def handleKillRequest(request: HttpServletRequest) = {
if (killEnabled) {
if ((killEnabled) && (parent.securityManager.checkModifyPermissions(request.getRemoteUser))) {
val killFlag = Option(request.getParameter("terminate")).getOrElse("false").toBoolean
val stageId = Option(request.getParameter("id")).getOrElse("-1").toInt
if (stageId >= 0 && killFlag && listener.activeStages.contains(stageId)) {
Expand Down
39 changes: 32 additions & 7 deletions core/src/test/scala/org/apache/spark/SecurityManagerSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SecurityManagerSuite extends FunSuite {
conf.set("spark.ui.view.acls", "user1,user2")
val securityManager = new SecurityManager(conf);
assert(securityManager.isAuthenticationEnabled() === true)
assert(securityManager.uiAclsEnabled() === true)
assert(securityManager.aclsEnabled() === true)
assert(securityManager.checkUIViewPermissions("user1") === true)
assert(securityManager.checkUIViewPermissions("user2") === true)
assert(securityManager.checkUIViewPermissions("user3") === false)
Expand All @@ -41,16 +41,16 @@ class SecurityManagerSuite extends FunSuite {
val conf = new SparkConf
conf.set("spark.ui.view.acls", "user1,user2")
val securityManager = new SecurityManager(conf);
securityManager.setUIAcls(true)
assert(securityManager.uiAclsEnabled() === true)
securityManager.setUIAcls(false)
assert(securityManager.uiAclsEnabled() === false)
securityManager.setAcls(true)
assert(securityManager.aclsEnabled() === true)
securityManager.setAcls(false)
assert(securityManager.aclsEnabled() === false)

// acls are off so doesn't matter what view acls set to
assert(securityManager.checkUIViewPermissions("user4") === true)

securityManager.setUIAcls(true)
assert(securityManager.uiAclsEnabled() === true)
securityManager.setAcls(true)
assert(securityManager.aclsEnabled() === true)
securityManager.setViewAcls(ArrayBuffer[String]("user5"), "user6,user7")
assert(securityManager.checkUIViewPermissions("user1") === false)
assert(securityManager.checkUIViewPermissions("user5") === true)
Expand All @@ -59,5 +59,30 @@ class SecurityManagerSuite extends FunSuite {
assert(securityManager.checkUIViewPermissions("user8") === false)
assert(securityManager.checkUIViewPermissions(null) === true)
}

test("set security modify acls") {
val conf = new SparkConf
conf.set("spark.modify.acls", "user1,user2")
val securityManager = new SecurityManager(conf);
securityManager.setAcls(true)
assert(securityManager.aclsEnabled() === true)
securityManager.setAcls(false)
assert(securityManager.aclsEnabled() === false)

// acls are off so doesn't matter what view acls set to
assert(securityManager.checkModifyPermissions("user4") === true)

securityManager.setAcls(true)
assert(securityManager.aclsEnabled() === true)
securityManager.setModifyAcls(ArrayBuffer[String]("user5"), "user6,user7")
assert(securityManager.checkModifyPermissions("user1") === false)
assert(securityManager.checkModifyPermissions("user5") === true)
assert(securityManager.checkModifyPermissions("user6") === true)
assert(securityManager.checkModifyPermissions("user7") === true)
assert(securityManager.checkModifyPermissions("user8") === false)
assert(securityManager.checkModifyPermissions(null) === true)
}


}

18 changes: 13 additions & 5 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -743,13 +743,13 @@ Apart from these, the following properties are also available, and may be useful
</td>
</tr>
<tr>
<td><code>spark.ui.acls.enable</code></td>
<td><code>spark.acls.enable</code></td>
<td>false</td>
<td>
Whether Spark web ui acls should are enabled. If enabled, this checks to see if the user has
access permissions to view the web ui. See <code>spark.ui.view.acls</code> for more details.
Also note this requires the user to be known, if the user comes across as null no checks
are done. Filters can be used to authenticate and set the user.
Whether Spark acls should are enabled. If enabled, this checks to see if the user has
access permissions to view or modify the job. Note this requires the user to be known,
so if the user comes across as null no checks are done. Filters can be used with the UI
to authenticate and set the user.
</td>
</tr>
<tr>
Expand All @@ -760,6 +760,14 @@ Apart from these, the following properties are also available, and may be useful
user that started the Spark job has view access.
</td>
</tr>
<tr>
<td><code>spark.modify.acls</code></td>
<td>Empty</td>
<td>
Comma separated list of users that have modify access to the Spark job. By default only the
user that started the Spark job has access to modify it (kill it for example).
</td>
</tr>
</table>

#### Spark Streaming
Expand Down
5 changes: 3 additions & 2 deletions docs/security.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ Spark currently supports authentication via a shared secret. Authentication can
* For Spark on [YARN](running-on-yarn.html) deployments, configuring `spark.authenticate` to `true` will automatically handle generating and distributing the shared secret. Each application will use a unique shared secret.
* For other types of Spark deployments, the Spark parameter `spark.authenticate.secret` should be configured on each of the nodes. This secret will be used by all the Master/Workers and applications.

The Spark UI can also be secured by using [javax servlet filters](http://docs.oracle.com/javaee/6/api/javax/servlet/Filter.html) via the `spark.ui.filters` setting. A user may want to secure the UI if it has data that other users should not be allowed to see. The javax servlet filter specified by the user can authenticate the user and then once the user is logged in, Spark can compare that user versus the view ACLs to make sure they are authorized to view the UI. The configs `spark.ui.acls.enable` and `spark.ui.view.acls` control the behavior of the ACLs. Note that the user who started the application always has view access to the UI.
On YARN, the Spark UI uses the standard YARN web application proxy mechanism and will authenticate via any installed Hadoop filters.
The Spark UI can also be secured by using [javax servlet filters](http://docs.oracle.com/javaee/6/api/javax/servlet/Filter.html) via the `spark.ui.filters` setting. A user may want to secure the UI if it has data that other users should not be allowed to see. The javax servlet filter specified by the user can authenticate the user and then once the user is logged in, Spark can compare that user versus the view ACLs to make sure they are authorized to view the UI. The configs `spark.acls.enable` and `spark.ui.view.acls` control the behavior of the ACLs. Note that the user who started the application always has view access to the UI. On YARN, the Spark UI uses the standard YARN web application proxy mechanism and will authenticate via any installed Hadoop filters.

Spark also supports modify ACLs to control who has access to modify a running Spark application. This includes things like killing the application or a task. This is controlled by the configs `spark.acls.enable` and `spark.modify.acls`.

If your applications are using event logging, the directory where the event logs go (`spark.eventLog.dir`) should be manually created and have the proper permissions set on it. If you want those log files secured, the permissions should be set to `drwxrwxrwxt` for that directory. The owner of the directory should be the super user who is running the history server and the group permissions should be restricted to super user group. This will allow all users to write to the directory but will prevent unprivileged users from removing or renaming a file unless they own the file or directory. The event log files will be created by Spark with permissions such that only the user and group have read and write access.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ import org.apache.hadoop.yarn.api.protocolrecords._
import org.apache.hadoop.yarn.api.records._
import org.apache.hadoop.yarn.conf.YarnConfiguration
import org.apache.hadoop.yarn.util.Records
import org.apache.spark.{SparkException, Logging, SparkConf, SparkContext}
import org.apache.spark._
import scala.util.Failure
import scala.Some
import scala.util.Success

/**
* The entry point (starting in Client#main() and Client#run()) for launching Spark on YARN. The
Expand Down Expand Up @@ -416,6 +419,12 @@ trait ClientBase extends Logging {
amContainer.setCommands(printableCommands)

setupSecurityToken(amContainer)

val securityManager = new SecurityManager(sparkConf)
val acls = Map[ApplicationAccessType, String] (
ApplicationAccessType.VIEW_APP -> securityManager.getViewAcls,
ApplicationAccessType.MODIFY_APP -> securityManager.getModifyAcls)
amContainer.setApplicationACLs(acls)
amContainer
}
}
Expand Down

0 comments on commit 72eb0ac

Please sign in to comment.