-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-31337][SQL]Support MS SQL Kerberos login in JDBC connector #28635
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3c30c4a
88306f0
16687d6
587a502
0a71849
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| /* | ||
| * 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.sql.execution.datasources.jdbc.connection | ||
|
|
||
| import java.security.PrivilegedExceptionAction | ||
| import java.sql.{Connection, Driver} | ||
| import java.util.Properties | ||
|
|
||
| import org.apache.hadoop.security.UserGroupInformation | ||
|
|
||
| import org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions | ||
|
|
||
| private[sql] class MSSQLConnectionProvider( | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The implementation is based on this. |
||
| driver: Driver, | ||
| options: JDBCOptions, | ||
| parserMethod: String = "parseAndMergeProperties" | ||
| ) extends SecureConnectionProvider(driver, options) { | ||
| override val appEntry: String = { | ||
| val configName = "jaasConfigurationName" | ||
| val appEntryDefault = "SQLJDBCDriver" | ||
|
|
||
| val parseURL = try { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are basically 2 approaches to parse the URL to get
Both way has been tested in |
||
| // The default parser method signature is the following: | ||
| // private Properties parseAndMergeProperties(String Url, Properties suppliedProperties) | ||
| val m = driver.getClass.getDeclaredMethod(parserMethod, classOf[String], classOf[Properties]) | ||
| m.setAccessible(true) | ||
| Some(m) | ||
| } catch { | ||
| case _: NoSuchMethodException => None | ||
| } | ||
|
|
||
| parseURL match { | ||
| case Some(m) => | ||
| logDebug("Property parser method found, using it") | ||
| m.invoke(driver, options.url, null).asInstanceOf[Properties] | ||
gaborgsomogyi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .getProperty(configName, appEntryDefault) | ||
|
|
||
| case None => | ||
| logDebug("Property parser method not found, using custom parsing mechanism") | ||
| options.url.split(';').map(_.split('=')) | ||
| .find(kv => kv.length == 2 && kv(0) == configName) | ||
| .getOrElse(Array(configName, appEntryDefault))(1) | ||
| } | ||
| } | ||
|
|
||
| override def getConnection(): Connection = { | ||
| setAuthenticationConfigIfNeeded() | ||
| UserGroupInformation.loginUserFromKeytabAndReturnUGI(options.principal, options.keytab).doAs( | ||
| new PrivilegedExceptionAction[Connection]() { | ||
| override def run(): Connection = { | ||
| MSSQLConnectionProvider.super.getConnection() | ||
| } | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| override def getAdditionalProperties(): Properties = { | ||
| val result = new Properties() | ||
| // These props needed to reach internal kerberos authentication in the JDBC driver | ||
| result.put("integratedSecurity", "true") | ||
| result.put("authenticationScheme", "JavaKerberos") | ||
|
Comment on lines
+75
to
+76
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These props needed to reach kerberos authentication case:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd be nice to add the background information on comment, either the code side or the class doc.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added an inline comment. |
||
| result | ||
| } | ||
|
|
||
| override def setAuthenticationConfigIfNeeded(): Unit = SecurityConfigurationLock.synchronized { | ||
| val (parent, configEntry) = getConfigWithAppEntry() | ||
| /** | ||
| * Couple of things to mention here (v8.2.2 client): | ||
| * 1. MS SQL supports JAAS application name configuration | ||
| * 2. MS SQL sets a default JAAS config if "java.security.auth.login.config" is not set | ||
| */ | ||
| val entryUsesKeytab = configEntry != null && | ||
| configEntry.exists(_.getOptions().get("useKeyTab") == "true") | ||
| if (configEntry == null || configEntry.isEmpty || !entryUsesKeytab) { | ||
| setAuthenticationConfig(parent) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private[sql] object MSSQLConnectionProvider { | ||
| val driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* | ||
| * 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.sql.execution.datasources.jdbc.connection | ||
|
|
||
| class MSSQLConnectionProviderSuite extends ConnectionProviderSuiteBase { | ||
| test("setAuthenticationConfigIfNeeded default parser must set authentication if not set") { | ||
| val driver = registerDriver(MSSQLConnectionProvider.driverClass) | ||
| val defaultProvider = new MSSQLConnectionProvider( | ||
| driver, options("jdbc:sqlserver://localhost/mssql")) | ||
| val customProvider = new MSSQLConnectionProvider( | ||
| driver, options("jdbc:sqlserver://localhost/mssql;jaasConfigurationName=custommssql")) | ||
|
|
||
| testProviders(defaultProvider, customProvider) | ||
| } | ||
|
|
||
| test("setAuthenticationConfigIfNeeded custom parser must set authentication if not set") { | ||
| val parserMethod = "IntentionallyNotExistingMethod" | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This simulates a driver where the private parser method doesn't exist. Such case manual parsing takes place and the code goes forward without issue. |
||
| val driver = registerDriver(MSSQLConnectionProvider.driverClass) | ||
| val defaultProvider = new MSSQLConnectionProvider( | ||
| driver, options("jdbc:sqlserver://localhost/mssql"), parserMethod) | ||
| val customProvider = new MSSQLConnectionProvider( | ||
| driver, | ||
| options("jdbc:sqlserver://localhost/mssql;jaasConfigurationName=custommssql"), | ||
| parserMethod) | ||
|
|
||
| testProviders(defaultProvider, customProvider) | ||
| } | ||
|
|
||
| private def testProviders( | ||
| defaultProvider: SecureConnectionProvider, | ||
| customProvider: SecureConnectionProvider) = { | ||
| assert(defaultProvider.appEntry !== customProvider.appEntry) | ||
| testSecureConnectionProvider(defaultProvider) | ||
| testSecureConnectionProvider(customProvider) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not absolutely necessary, if we think we can extract it into a new PR. Thought it would be overkill.