peter-toth commented on code in PR #57285:
URL: https://github.com/apache/spark/pull/57285#discussion_r3631275260
##########
core/src/main/scala/org/apache/spark/deploy/security/HadoopDelegationTokenManager.scala:
##########
@@ -76,17 +77,33 @@ private[spark] class HadoopDelegationTokenManager(
require((principal == null) == (keytab == null),
"Both principal and keytab must be defined, or neither.")
+ if (sparkConf.get(CREDENTIALS_DIRECT_PROVIDERS_ENABLED)) {
+ require(sparkConf.get(NETWORK_CRYPTO_ENABLED),
Review Comment:
`spark.network.crypto.enabled` is only one of Spark's RPC-encryption
mechanisms. `spark.ssl.rpc.enabled` (TLS, SPARK-44080) and
`spark.authenticate.enableSaslEncryption` (SASL) also encrypt the channel, so
any of the three equally keeps bearer tokens off a plaintext wire. As written,
a deployment using either of the other two is rejected here and forced to also
turn on `network.crypto`. Suggest widening the check to all three:
```scala
if (sparkConf.get(CREDENTIALS_DIRECT_PROVIDERS_ENABLED)) {
require(sparkConf.get(NETWORK_CRYPTO_ENABLED) ||
sparkConf.get(SASL_ENCRYPTION_ENABLED) ||
sparkConf.getBoolean("spark.ssl.rpc.enabled", false),
"One of spark.network.crypto.enabled,
spark.authenticate.enableSaslEncryption, or " +
"spark.ssl.rpc.enabled must be true when " +
"spark.security.credentials.directProviders.enabled is true. " +
"Credential tokens must not be transmitted over unencrypted RPC
channels.")
}
```
`SecurityManager` already encodes exactly this (`isEncryptionEnabled() ||
isSslRpcEnabled()` covers all three), so if a `SecurityManager` is conveniently
reachable you could reuse that instead of the raw conf reads -- though it's
probably not worth plumbing one into this constructor just for the check.
##########
core/src/main/scala/org/apache/spark/internal/config/package.scala:
##########
@@ -1679,6 +1679,18 @@ package object config {
.timeConf(TimeUnit.SECONDS)
.createWithDefaultString("1h")
+ private[spark] val CREDENTIALS_DIRECT_PROVIDERS_ENABLED =
+ ConfigBuilder("spark.security.credentials.directProviders.enabled")
+ .doc(
+ "When true, enables delegation token collection and renewal without
Kerberos. " +
+ "Providers are called directly (without doLogin/doAs) and participate
in the " +
+ "same renewal and distribution lifecycle as Kerberos delegation token
providers. " +
+ "Providers that require Kerberos self-gate via
delegationTokensRequired.")
+ .version("5.0.0")
Review Comment:
Project policy: a new, backportable feature takes the version of the branch
it first ships in. A PR against `master` that gets backported ships first in
the next feature branch -- currently `branch-4.x` = `4.3.0` (master is
`5.0.0`). So unless this is deliberately master-only, this should be
`.version("4.3.0")`. Could you confirm the intended target release?
##########
core/src/main/scala/org/apache/spark/deploy/security/HadoopDelegationTokenManager.scala:
##########
@@ -226,24 +249,41 @@ private[spark] class HadoopDelegationTokenManager(
*
* @return Credentials containing the new tokens.
*/
- private def obtainTokensAndScheduleRenewal(ugi: UserGroupInformation):
Credentials = {
- ugi.doAs(new PrivilegedExceptionAction[Credentials]() {
- override def run(): Credentials = {
- val (creds, nextRenewal) = obtainDelegationTokens()
-
- // Calculate the time when new credentials should be created, based on
the configured
- // ratio.
- val now = System.currentTimeMillis
- val ratio = sparkConf.get(CREDENTIALS_RENEWAL_INTERVAL_RATIO)
- val delay = (ratio * (nextRenewal - now)).toLong
- logInfo(log"Calculated delay on renewal is ${MDC(LogKeys.DELAY,
delay)}," +
- log" based on next renewal ${MDC(LogKeys.NEXT_RENEWAL_TIME,
nextRenewal)}" +
- log" and the ratio ${MDC(LogKeys.CREDENTIALS_RENEWAL_INTERVAL_RATIO,
ratio)}," +
- log" and current time ${MDC(LogKeys.CURRENT_TIME, now)}")
- scheduleRenewal(delay)
- creds
+ private def obtainTokensAndScheduleRenewal(): Credentials = {
+ val hasKerberosConfig = sparkConf.get(KERBEROS_RENEWAL_CREDENTIALS) match {
Review Comment:
This `hasKerberosConfig` block duplicates `renewalEnabled` but diverges on
the `ccache` case: `renewalEnabled` returns true for `ccache` only when
`UserGroupInformation.getCurrentUser().hasKerberosCredentials()`, whereas here
`ccache => true` unconditionally.
Concrete effect: with `spark.kerberos.renewal.credentials=ccache`, no
Kerberos credentials present, and `directProviders.enabled=true`,
`renewalEnabled` is true (via the switch) so `start()` runs -- but this method
then sees `hasKerberosConfig=true` and takes the `doLogin()/doAs` branch
instead of the intended direct branch, contradicting the design ("providers are
called directly without doLogin/doAs"). The default `keytab` case isn't
affected, so the primary path works; it's the explicit-`ccache` case that's off.
Suggest one shared helper so both agree:
```scala
private def hasKerberosCredentials(): Boolean =
sparkConf.get(KERBEROS_RENEWAL_CREDENTIALS) match {
case "keytab" => principal != null
case "ccache" =>
UserGroupInformation.getCurrentUser().hasKerberosCredentials()
case _ => false
}
```
and call it from both `renewalEnabled` and here. (Legit `ccache`-with-creds
still resolves to the `doLogin` branch, so no regression there.)
##########
core/src/main/scala/org/apache/spark/deploy/security/HadoopDelegationTokenManager.scala:
##########
@@ -166,7 +184,14 @@ private[spark] class HadoopDelegationTokenManager(
val creds = new Credentials()
val nextRenewal = delegationTokenProviders.values.flatMap { provider =>
if (provider.delegationTokensRequired(sparkConf, hadoopConf)) {
- provider.obtainDelegationTokens(hadoopConf, sparkConf, creds)
+ try {
Review Comment:
Good for isolating one bad provider among many. Note though that this
`try/catch` is unconditional, so it also changes the existing Kerberos path:
previously a provider throwing propagated up and `updateTokensTask` retried the
whole fetch after `CREDENTIALS_RENEWAL_RETRY_WAIT`; now the failure is
swallowed, the surviving providers' tokens are sent, and the failed provider's
token isn't retried until the next full renewal (computed from the successful
providers, potentially far out). That's a behavior change for existing Kerberos
deployments, not just the new switch. Consider scoping the catch to the
direct-provider path, or calling the change out explicitly -- it's a reasonable
trade, just worth being deliberate about.
##########
core/src/test/scala/org/apache/spark/deploy/security/NonKerberosCredentialsSuite.scala:
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.security
+
+import org.apache.hadoop.conf.Configuration
+import org.apache.hadoop.io.Text
+import org.apache.hadoop.security.Credentials
+
+import org.apache.spark.{SparkConf, SparkFunSuite}
+import org.apache.spark.internal.config._
+import org.apache.spark.internal.config.Network.NETWORK_CRYPTO_ENABLED
+import org.apache.spark.security.HadoopDelegationTokenProvider
+
+private class TestNonKerberosTokenProvider extends
HadoopDelegationTokenProvider {
+ override def serviceName: String = "test-direct"
+
+ override def delegationTokensRequired(
+ sparkConf: SparkConf, hadoopConf: Configuration): Boolean = true
+
+ override def obtainDelegationTokens(
+ hadoopConf: Configuration,
+ sparkConf: SparkConf,
+ creds: Credentials): Option[Long] = {
+ creds.addSecretKey(new Text("test.direct.credential"),
"test-token".getBytes)
+ Some(System.currentTimeMillis() + 3600000L)
+ }
+}
+
+private class TestDisabledProvider extends HadoopDelegationTokenProvider {
+ override def serviceName: String = "test-disabled"
+
+ override def delegationTokensRequired(
+ sparkConf: SparkConf, hadoopConf: Configuration): Boolean = false
+
+ override def obtainDelegationTokens(
+ hadoopConf: Configuration,
+ sparkConf: SparkConf,
+ creds: Credentials): Option[Long] = {
+ // scalastyle:off throwerror
+ throw new AssertionError("Should not be called when
delegationTokensRequired is false")
+ // scalastyle:on throwerror
+ }
+}
+
+private class TestFailingProvider extends HadoopDelegationTokenProvider {
+ override def serviceName: String = "test-failing"
+
+ override def delegationTokensRequired(
+ sparkConf: SparkConf, hadoopConf: Configuration): Boolean = true
+
+ override def obtainDelegationTokens(
+ hadoopConf: Configuration,
+ sparkConf: SparkConf,
+ creds: Credentials): Option[Long] = {
+ throw new RuntimeException("Simulated provider failure")
+ }
+}
+
+class NonKerberosCredentialsSuite extends SparkFunSuite {
Review Comment:
The suite covers the public `obtainDelegationTokens(creds)` and the
constructor checks, but not the renewal/scheduling path -- `start()` ->
`updateTokensTask()` -> `obtainTokensAndScheduleRenewal()`. That's exactly
where the `ccache` branching from my other comment lives, and it's the code
most specific to this feature. You can exercise it with a stub `RpcEndpointRef`
as `schedulerRef` that captures the `UpdateDelegationTokens` message, then
assert `start()` sends the direct provider's token. Worth adding at least the
default-`keytab` non-Kerberos case.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]