HeartSaVioR commented on a change in pull request #28215: [SPARK-31272][SQL] 
Support DB2 Kerberos login in JDBC connector
URL: https://github.com/apache/spark/pull/28215#discussion_r408682674
 
 

 ##########
 File path: 
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/connection/DB2ConnectionProvider.scala
 ##########
 @@ -0,0 +1,66 @@
+/*
+ * 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 javax.security.auth.login.Configuration
+
+import org.apache.hadoop.security.UserGroupInformation
+
+import org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions
+
+private[sql] class DB2ConnectionProvider(driver: Driver, options: JDBCOptions)
+    extends SecureConnectionProvider(driver, options) {
+  override val appEntry: String = "JaasClient"
+
+  override def getConnection(): Connection = {
+    setAuthenticationConfigIfNeeded()
+    UserGroupInformation.loginUserFromKeytabAndReturnUGI(options.principal, 
options.keytab).doAs(
+      new PrivilegedExceptionAction[Connection]() {
+        override def run(): Connection = {
+          DB2ConnectionProvider.super.getConnection()
+        }
+      }
+    )
+  }
+
+  override def getAdditionalProperties(): Properties = {
+    val result = new Properties()
+    // 11 is the integer value for kerberos
+    result.put("securityMechanism", new String("11"))
+    result.put("KerberosServerPrincipal", options.principal)
+    result
+  }
+
+  override def setAuthenticationConfigIfNeeded(): Unit = {
 
 Review comment:
   There're two parts which are duplicated across implementations and have each 
functionality:
   
   1.
   ```
       val parent = Configuration.getConfiguration
       val configEntry = parent.getAppConfigurationEntry(appEntry)
   ```
   
   (Maybe it could be only one line to extract safely here as `parent` is used 
in below, so might not be much helpful as 2. I wouldn't mind to call 
Configuration.getConfiguration separately in 1 and 2, but that's only me.)
   
   2.
   ```
         val config = new SecureConnectionProvider.JDBCConfiguration(
           parent, appEntry, options.keytab, options.principal)
         logDebug("Adding database specific security configuration")
         Configuration.setConfiguration(config)
   ```
   
   Looks like they could be extracted as two methods in base class (or utility 
object) and being called from each implementation.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to