Github user vanzin commented on a diff in the pull request:

    https://github.com/apache/spark/pull/14065#discussion_r69989897
  
    --- Diff: 
yarn/src/main/scala/org/apache/spark/deploy/yarn/token/ConfigurableTokenManager.scala
 ---
    @@ -0,0 +1,214 @@
    +/*
    + * 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.token
    +
    +import scala.collection.mutable
    +import scala.util.control.NonFatal
    +
    +import org.apache.hadoop.conf.Configuration
    +import org.apache.hadoop.security.Credentials
    +import org.apache.hadoop.security.token.Token
    +
    +import org.apache.spark.{SparkConf, SparkException}
    +import org.apache.spark.internal.Logging
    +import org.apache.spark.util.Utils
    +
    +/**
    + * A [[ConfigurableTokenManager]] to manage all the token providers 
register in this class. Also
    + * it provides other modules the functionality to obtain tokens, get token 
renewal interval and
    + * calculate the time length till next renewal.
    + *
    + * By default ConfigurableTokenManager has 3 built-in token providers, 
HDFSTokenProvider,
    + * HiveTokenProvider and HBaseTokenProvider, and this 3 token providers 
can also be controlled
    + * by configuration spark.yarn.security.tokens.{service}.enabled, if it is 
set to false, this
    + * provider will not be loaded.
    + *
    + * For other token providers which need to be loaded in should:
    + * 1. Implement [[ServiceTokenProvider]] or [[ServiceTokenRenewable]] if 
token renewal is
    + * required for this service.
    + * 2. set spark.yarn.security.tokens.{service}.enabled to true
    + * 3. Specify the class name through 
spark.yarn.security.tokens.{service}.class
    + *
    + */
    +final class ConfigurableTokenManager private[yarn] (sparkConf: SparkConf) 
extends Logging {
    +  private val tokenProviderEnabledConfig = 
"spark\\.yarn\\.security\\.tokens\\.(.+)\\.enabled".r
    +  private val tokenProviderClsConfig = 
"spark.yarn.security.tokens.%s.class"
    +
    +  // Maintain all the registered token providers
    +  private val tokenProviders = mutable.HashMap[String, 
ServiceTokenProvider]()
    +
    +  private val defaultTokenProviders = Map(
    +    "hdfs" -> "org.apache.spark.deploy.yarn.token.HDFSTokenProvider",
    +    "hive" -> "org.apache.spark.deploy.yarn.token.HiveTokenProvider",
    +    "hbase" -> "org.apache.spark.deploy.yarn.token.HBaseTokenProvider"
    +  )
    +
    +  // AMDelegationTokenRenewer, this will only be create and started in the 
AM
    +  private var _delegationTokenRenewer: AMDelegationTokenRenewer = null
    +
    +  // ExecutorDelegationTokenUpdater, this will only be created and started 
in the driver and
    +  // executor side.
    +  private var _delegationTokenUpdater: ExecutorDelegationTokenUpdater = 
null
    +
    +  def initialize(): Unit = {
    +    // Copy SparkConf and add default enabled token provider 
configurations to SparkConf.
    +    val clonedConf = sparkConf.clone
    +    defaultTokenProviders.keys.foreach { key =>
    +      clonedConf.setIfMissing(s"spark.yarn.security.tokens.$key.enabled", 
"true")
    +    }
    +
    +    // Instantialize all the service token providers according to the 
configurations.
    +    clonedConf.getAll.filter { case (key, value) =>
    +      if (tokenProviderEnabledConfig.findPrefixOf(key).isDefined) {
    +        value.toBoolean
    +      } else {
    +        false
    +      }
    +    }.map { case (key, _) =>
    +      val tokenProviderEnabledConfig(service) = key
    +      val cls = sparkConf.getOption(tokenProviderClsConfig.format(service))
    +        .orElse(defaultTokenProviders.get(service))
    +      (service, cls)
    +    }.foreach { case (service, cls) =>
    +      if (cls.isDefined) {
    +        try {
    +          val tokenProvider =
    +            
Utils.classForName(cls.get).newInstance().asInstanceOf[ServiceTokenProvider]
    +          tokenProviders += (service -> tokenProvider)
    +        } catch {
    +          case NonFatal(e) =>
    +            logWarning(s"Fail to instantiate class ${cls.get}", e)
    +        }
    +      }
    +    }
    +  }
    +
    +  /**
    +   * Get service token provider by name.
    +   */
    +  def getServiceTokenProvider(service: String): 
Option[ServiceTokenProvider] = {
    +    tokenProviders.get(service)
    +  }
    +
    +  /**
    +   * Obtain tokens from all the token providers and add into credentials, 
also return as an array.
    +   */
    +  def obtainTokens(conf: Configuration, creds: Credentials): 
Array[Token[_]] = {
    +    val tokenBuf = mutable.ArrayBuffer[Token[_]]()
    +    tokenProviders.values.foreach { provider =>
    --- End diff --
    
    minor: you can use `flatMap` instead of a mutable buffer.


---
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]

Reply via email to