Skip to content

Commit 07ec75c

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. (cherry picked from commit ffaf0f9) Signed-off-by: jerryshao <[email protected]>
1 parent 096defd commit 07ec75c

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
@@ -227,6 +227,7 @@ private[spark] class SecurityManager(
227227
setViewAclsGroups(sparkConf.get("spark.ui.view.acls.groups", ""));
228228
setModifyAclsGroups(sparkConf.get("spark.modify.acls.groups", ""));
229229

230+
private var secretKey: String = _
230231
logInfo("SecurityManager: authentication " + (if (authOn) "enabled" else "disabled") +
231232
"; ui acls " + (if (aclsOn) "enabled" else "disabled") +
232233
"; users with view permissions: " + viewAcls.toString() +
@@ -504,6 +505,12 @@ private[spark] class SecurityManager(
504505
val creds = UserGroupInformation.getCurrentUser().getCredentials()
505506
Option(creds.getSecretKey(SECRET_LOOKUP_KEY))
506507
.map { bytes => new String(bytes, UTF_8) }
508+
// Secret key may not be found in current UGI's credentials.
509+
// This happens when UGI is refreshed in the driver side by UGI's loginFromKeytab but not
510+
// copy secret key from original UGI to the new one. This exists in ThriftServer's Hive
511+
// logic. So as a workaround, storing secret key in a local variable to make it visible
512+
// in different context.
513+
.orElse(Option(secretKey))
507514
.orElse(Option(sparkConf.getenv(ENV_AUTH_SECRET)))
508515
.orElse(sparkConf.getOption(SPARK_AUTH_SECRET_CONF))
509516
.getOrElse {
@@ -541,8 +548,8 @@ private[spark] class SecurityManager(
541548
rnd.nextBytes(secretBytes)
542549

543550
val creds = new Credentials()
544-
val secretStr = HashCodes.fromBytes(secretBytes).toString()
545-
creds.addSecretKey(SECRET_LOOKUP_KEY, secretStr.getBytes(UTF_8))
551+
secretKey = HashCodes.fromBytes(secretBytes).toString()
552+
creds.addSecretKey(SECRET_LOOKUP_KEY, secretKey.getBytes(UTF_8))
546553
UserGroupInformation.getCurrentUser().addCredentials(creds)
547554
}
548555

0 commit comments

Comments
 (0)