Skip to content

Commit ffaf0f9

Browse files
committed
[SPARK-24062][THRIFT SERVER] Fix SASL encryption cannot enabled issue in thrift server
## What changes were proposed in this pull request? For the details of the exception please see [SPARK-24062](https://issues.apache.org/jira/browse/SPARK-24062). The issue is: Spark on Yarn stores SASL secret in current UGI's credentials, this credentials will be distributed to AM and executors, so that executors and drive share the same secret to communicate. But STS/Hive library code will refresh the current UGI by UGI's loginFromKeytab() after Spark application is started, this will create a new UGI in the current driver's context with empty tokens and secret keys, so secret key is lost in the current context's UGI, that's why Spark driver throws secret key not found exception. In Spark 2.2 code, Spark also stores this secret key in SecurityManager's class variable, so even UGI is refreshed, the secret is still existed in the object, so STS with SASL can still be worked in Spark 2.2. But in Spark 2.3, we always search key from current UGI, which makes it fail to work in Spark 2.3. To fix this issue, there're two possible solutions: 1. Fix in STS/Hive library, when a new UGI is refreshed, copy the secret key from original UGI to the new one. The difficulty is that some codes to refresh the UGI is existed in Hive library, which makes us hard to change the code. 2. Roll back the logics in SecurityManager to match Spark 2.2, so that this issue can be fixed. 2nd solution seems a simple one. So I will propose a PR with 2nd solution. ## How was this patch tested? Verified in local cluster. CC vanzin tgravescs please help to review. Thanks! Author: jerryshao <[email protected]> Closes #21138 from jerryshao/SPARK-24062.
1 parent cd10f9d commit ffaf0f9

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

core/src/main/scala/org/apache/spark/SecurityManager.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ private[spark] class SecurityManager(
8989
setViewAclsGroups(sparkConf.get("spark.ui.view.acls.groups", ""));
9090
setModifyAclsGroups(sparkConf.get("spark.modify.acls.groups", ""));
9191

92+
private var secretKey: String = _
9293
logInfo("SecurityManager: authentication " + (if (authOn) "enabled" else "disabled") +
9394
"; ui acls " + (if (aclsOn) "enabled" else "disabled") +
9495
"; users with view permissions: " + viewAcls.toString() +
@@ -321,6 +322,12 @@ private[spark] class SecurityManager(
321322
val creds = UserGroupInformation.getCurrentUser().getCredentials()
322323
Option(creds.getSecretKey(SECRET_LOOKUP_KEY))
323324
.map { bytes => new String(bytes, UTF_8) }
325+
// Secret key may not be found in current UGI's credentials.
326+
// This happens when UGI is refreshed in the driver side by UGI's loginFromKeytab but not
327+
// copy secret key from original UGI to the new one. This exists in ThriftServer's Hive
328+
// logic. So as a workaround, storing secret key in a local variable to make it visible
329+
// in different context.
330+
.orElse(Option(secretKey))
324331
.orElse(Option(sparkConf.getenv(ENV_AUTH_SECRET)))
325332
.orElse(sparkConf.getOption(SPARK_AUTH_SECRET_CONF))
326333
.getOrElse {
@@ -364,8 +371,8 @@ private[spark] class SecurityManager(
364371
rnd.nextBytes(secretBytes)
365372

366373
val creds = new Credentials()
367-
val secretStr = HashCodes.fromBytes(secretBytes).toString()
368-
creds.addSecretKey(SECRET_LOOKUP_KEY, secretStr.getBytes(UTF_8))
374+
secretKey = HashCodes.fromBytes(secretBytes).toString()
375+
creds.addSecretKey(SECRET_LOOKUP_KEY, secretKey.getBytes(UTF_8))
369376
UserGroupInformation.getCurrentUser().addCredentials(creds)
370377
}
371378

0 commit comments

Comments
 (0)