Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
WIP
  • Loading branch information
Ian Hummel committed Feb 24, 2017
commit 62a6e20179dd63703d18de9784c8b3770077e968
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.spark.deploy.yarn.security
package org.apache.spark.deploy.security

import java.security.PrivilegedExceptionAction
import java.util.concurrent.{Executors, TimeUnit}

import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.{FileSystem, Path}
import org.apache.hadoop.security.UserGroupInformation

import org.apache.spark.SparkConf
import org.apache.spark.deploy.SparkHadoopUtil
import org.apache.spark.deploy.yarn.YarnSparkHadoopUtil
import org.apache.spark.deploy.yarn.config._
import org.apache.spark.internal.Logging
import org.apache.spark.internal.config._
import org.apache.spark.util.ThreadUtils
Expand All @@ -51,7 +48,7 @@ import org.apache.spark.util.ThreadUtils
* appeared, it will read the credentials and update the currently running UGI with it. This
* process happens again once 80% of the validity of this has expired.
*/
private[yarn] class AMCredentialRenewer(
private[spark] class AMCredentialRenewer(
sparkConf: SparkConf,
hadoopConf: Configuration,
credentialManager: ConfigurableCredentialManager) extends Logging {
Expand All @@ -62,7 +59,7 @@ private[yarn] class AMCredentialRenewer(
Executors.newSingleThreadScheduledExecutor(
ThreadUtils.namedThreadFactory("Credential Refresh Thread"))

private val hadoopUtil = YarnSparkHadoopUtil.get
private val hadoopUtil = SparkHadoopUtil.get

private val credentialsFile = sparkConf.get(CREDENTIALS_FILE_PATH)
private val daysToKeepFiles = sparkConf.get(CREDENTIALS_FILE_MAX_RETENTION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@
* limitations under the License.
*/

package org.apache.spark.deploy.yarn.security
package org.apache.spark.deploy.security

import java.util.ServiceLoader

import scala.collection.JavaConverters._

import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.security.Credentials

import org.apache.spark.SparkConf
import org.apache.spark.internal.Logging
import org.apache.spark.util.Utils

import scala.collection.JavaConverters._

/**
* A ConfigurableCredentialManager to manage all the registered credential providers and offer
* APIs for other modules to obtain credentials as well as renewal time. By default
Expand All @@ -41,7 +40,7 @@ import org.apache.spark.util.Utils
* For example, Hive's credential provider [[HiveCredentialProvider]] can be enabled/disabled by
* the configuration spark.yarn.security.credentials.hive.enabled.
*/
private[yarn] final class ConfigurableCredentialManager(
private[spark] final class ConfigurableCredentialManager(
sparkConf: SparkConf, hadoopConf: Configuration) extends Logging {
private val deprecatedProviderEnabledConfig = "spark.yarn.security.tokens.%s.enabled"
private val providerEnabledConfig = "spark.yarn.security.credentials.%s.enabled"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,29 @@
* limitations under the License.
*/

package org.apache.spark.deploy.yarn.security
package org.apache.spark.deploy.security

import java.util.concurrent.{Executors, TimeUnit}

import scala.util.control.NonFatal

import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.{FileSystem, Path}
import org.apache.hadoop.security.{Credentials, UserGroupInformation}

import org.apache.spark.SparkConf
import org.apache.spark.deploy.SparkHadoopUtil
import org.apache.spark.deploy.yarn.config._
import org.apache.spark.internal.Logging
import org.apache.spark.util.{ThreadUtils, Utils}

import scala.util.control.NonFatal

private[spark] class CredentialUpdater(
sparkConf: SparkConf,
hadoopConf: Configuration,
credentialManager: ConfigurableCredentialManager) extends Logging {

@volatile private var lastCredentialsFileSuffix = 0

private val credentialsFile = sparkConf.get(CREDENTIALS_FILE_PATH)
// TODO move to ConfigBuilder
private val credentialsFile = sparkConf.get("spark.yarn.credentials.file")
private val freshHadoopConf =
SparkHadoopUtil.get.getConfBypassingFSCache(
hadoopConf, new Path(credentialsFile).toUri.getScheme)
Expand All @@ -55,7 +54,8 @@ private[spark] class CredentialUpdater(

/** Start the credential updater task */
def start(): Unit = {
val startTime = sparkConf.get(CREDENTIALS_RENEWAL_TIME)
// TODO move to ConfigBuilder
val startTime = sparkConf.getTimeAsMs("spark.yarn.credentials.renewalTime")
val remainingTime = startTime - System.currentTimeMillis()
if (remainingTime <= 0) {
credentialUpdater.schedule(credentialUpdaterRunnable, 1, TimeUnit.MINUTES)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@
* limitations under the License.
*/

package org.apache.spark.deploy.yarn.security

import scala.reflect.runtime.universe
import scala.util.control.NonFatal
package org.apache.spark.deploy.security

import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.security.Credentials
import org.apache.hadoop.security.token.{Token, TokenIdentifier}

import org.apache.spark.SparkConf
import org.apache.spark.internal.Logging

import scala.reflect.runtime.universe
import scala.util.control.NonFatal

private[security] class HBaseCredentialProvider extends ServiceCredentialProvider with Logging {

override def serviceName: String = "hbase"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,19 @@
* limitations under the License.
*/

package org.apache.spark.deploy.yarn.security

import scala.collection.JavaConverters._
import scala.util.Try
package org.apache.spark.deploy.security

import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.{FileSystem, Path}
import org.apache.hadoop.mapred.Master
import org.apache.hadoop.security.Credentials
import org.apache.hadoop.security.token.delegation.AbstractDelegationTokenIdentifier

import org.apache.spark.{SparkConf, SparkException}
import org.apache.spark.deploy.yarn.config._
import org.apache.spark.internal.Logging
import org.apache.spark.internal.config._
import org.apache.spark.{SparkConf, SparkException}

import scala.collection.JavaConverters._
import scala.util.Try

private[security] class HadoopFSCredentialProvider
extends ServiceCredentialProvider with Logging {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,23 @@
* limitations under the License.
*/

package org.apache.spark.deploy.yarn.security
package org.apache.spark.deploy.security

import java.lang.reflect.UndeclaredThrowableException
import java.security.PrivilegedExceptionAction

import scala.reflect.runtime.universe
import scala.util.control.NonFatal

import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.hdfs.security.token.delegation.DelegationTokenIdentifier
import org.apache.hadoop.io.Text
import org.apache.hadoop.security.{Credentials, UserGroupInformation}
import org.apache.hadoop.security.token.Token

import org.apache.hadoop.security.{Credentials, UserGroupInformation}
import org.apache.spark.SparkConf
import org.apache.spark.internal.Logging
import org.apache.spark.util.Utils

import scala.reflect.runtime.universe
import scala.util.control.NonFatal

private[security] class HiveCredentialProvider extends ServiceCredentialProvider with Logging {

override def serviceName: String = "hive"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
/*
* 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.deploy.yarn.security
package org.apache.spark.deploy.security

import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.security.{Credentials, UserGroupInformation}

import org.apache.spark.SparkConf

/**
Expand Down
45 changes: 45 additions & 0 deletions core/src/main/scala/org/apache/spark/internal/config/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,49 @@ package object config {
.booleanConf
.createWithDefault(false)


////// TODO

private[spark] val CREDENTIALS_RENEWAL_TIME = ConfigBuilder("spark.yarn.credentials.renewalTime")
.internal()
.timeConf(TimeUnit.MILLISECONDS)
.createWithDefault(Long.MaxValue)

private[spark] val CREDENTIALS_UPDATE_TIME = ConfigBuilder("spark.yarn.credentials.updateTime")
.internal()
.timeConf(TimeUnit.MILLISECONDS)
.createWithDefault(Long.MaxValue)

private[spark] val CREDENTIALS_FILE_PATH = ConfigBuilder("spark.yarn.credentials.file")
.internal()
.stringConf
.createWithDefault(null)

private[spark] val CREDENTIAL_FILE_MAX_COUNT =
ConfigBuilder("spark.yarn.credentials.file.retention.count")
.intConf
.createWithDefault(5)

private[spark] val CREDENTIALS_FILE_MAX_RETENTION =
ConfigBuilder("spark.yarn.credentials.file.retention.days")
.intConf
.createWithDefault(5)

private[spark] val STAGING_DIR = ConfigBuilder("spark.yarn.stagingDir")
.doc("Staging directory used while submitting applications.")
.stringConf
.createOptional

private[spark] val NAMENODES_TO_ACCESS = ConfigBuilder("spark.yarn.access.namenodes")
.doc("Extra NameNode URLs for which to request delegation tokens. The NameNode that hosts " +
"fs.defaultFS does not need to be listed here.")
.stringConf
.toSequence
.createWithDefault(Nil)

private[spark] val FILESYSTEMS_TO_ACCESS = ConfigBuilder("spark.yarn.access.hadoopFileSystems")
.doc("Extra Hadoop filesystem URLs for which to request delegation tokens. The filesystem " +
"that hosts fs.defaultFS does not need to be listed here.")
.fallbackConf(NAMENODES_TO_ACCESS)

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,23 @@

package org.apache.spark.scheduler.cluster

import java.io.File
import java.util.UUID
import java.util.concurrent.Semaphore

import scala.concurrent.Future

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.deploy.{ApplicationDescription, Command}
import org.apache.hadoop.security.{Credentials, UserGroupInformation}
import org.apache.spark.deploy.client.{StandaloneAppClient, StandaloneAppClientListener}
import org.apache.spark.deploy.security.ConfigurableCredentialManager
import org.apache.spark.deploy.{ApplicationDescription, Command, SparkHadoopUtil}
import org.apache.spark.internal.Logging
import org.apache.spark.internal.config.{KEYTAB, PRINCIPAL}
import org.apache.spark.launcher.{LauncherBackend, SparkAppHandle}
import org.apache.spark.rpc.RpcEndpointAddress
import org.apache.spark.scheduler._
import org.apache.spark.util.Utils
import org.apache.spark.{SparkConf, SparkContext}

import scala.concurrent.Future

/**
* A [[SchedulerBackend]] implementation for Spark's standalone cluster manager.
Expand All @@ -55,10 +60,43 @@ private[spark] class StandaloneSchedulerBackend(
private val maxCores = conf.getOption("spark.cores.max").map(_.toInt)
private val totalExpectedCores = maxCores.getOrElse(0)


private var loginFromKeytab = false
private var principal: String = null
private var keytab: String = null
private var credentials: Credentials = null

def setupCredentials(): Unit = {
loginFromKeytab = sc.conf.contains(PRINCIPAL.key)
if (loginFromKeytab) {
principal = sc.conf.get(PRINCIPAL).get
keytab = sc.conf.get(KEYTAB).orNull

require(keytab != null, "Keytab must be specified when principal is specified.")
logInfo("Attempting to login to the Kerberos" +
s" using principal: $principal and keytab: $keytab")
val f = new File(keytab)
// Generate a file name that can be used for the keytab file, that does not conflict
// with any user file.
val keytabFileName = f.getName + "-" + UUID.randomUUID().toString
sc.conf.set(KEYTAB.key, keytabFileName)
sc.conf.set(PRINCIPAL.key, principal)
}
// Defensive copy of the credentials
credentials = new Credentials(UserGroupInformation.getCurrentUser.getCredentials)
}






override def start() {
super.start()
launcherBackend.connect()

setupCredentials()

// The endpoint for executors to talk to us
val driverUrl = RpcEndpointAddress(
sc.conf.get("spark.driver.host"),
Expand Down Expand Up @@ -110,6 +148,17 @@ private[spark] class StandaloneSchedulerBackend(
launcherBackend.setState(SparkAppHandle.State.SUBMITTED)
waitForRegistration()
launcherBackend.setState(SparkAppHandle.State.RUNNING)




if (conf.contains("spark.yarn.credentials.file")) {
val newConf = SparkHadoopUtil.get.newConfiguration(conf)
val cu = new ConfigurableCredentialManager(conf, newConf).credentialUpdater()
cu.start()
}


}

override def stop(): Unit = synchronized {
Expand Down
2 changes: 1 addition & 1 deletion dev/.rat-excludes
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ spark-deps-.*
org.apache.spark.scheduler.ExternalClusterManager
.*\.sql
.Rbuildignore
org.apache.spark.deploy.yarn.security.ServiceCredentialProvider
org.apache.spark.deploy.security.ServiceCredentialProvider
spark-warehouse
structured-streaming/*
kafka-source-initial-offset-version-2.1.0.bin
3 changes: 2 additions & 1 deletion docs/running-on-yarn.md
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,9 @@ launch time. This is done by listing them in the `spark.yarn.access.hadoopFileSy
spark.yarn.access.hadoopFileSystems hdfs://ireland.example.org:8020/,webhdfs://frankfurt.example.org:50070/
```

TODO
Spark supports integrating with other security-aware services through Java Services mechanism (see
`java.util.ServiceLoader`). To do that, implementations of `org.apache.spark.deploy.yarn.security.ServiceCredentialProvider`
`java.util.ServiceLoader`). To do that, implementations of `org.apache.spark.deploy.security.ServiceCredentialProvider`
should be available to Spark by listing their names in the corresponding file in the jar's
`META-INF/services` directory. These plug-ins can be disabled by setting
`spark.yarn.security.credentials.{service}.enabled` to `false`, where `{service}` is the name of
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
org.apache.spark.deploy.security.HadoopFSCredentialProvider
org.apache.spark.deploy.security.HBaseCredentialProvider
org.apache.spark.deploy.security.HiveCredentialProvider

This file was deleted.

Loading