You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@kyuubi.apache.org by GitBox <gi...@apache.org> on 2022/08/16 07:40:13 UTC

[GitHub] [incubator-kyuubi] bowenliang123 commented on a diff in pull request #3235: [KYUUBI #3222] JDBC Authentication Provider for server

bowenliang123 commented on code in PR #3235:
URL: https://github.com/apache/incubator-kyuubi/pull/3235#discussion_r946436179


##########
kyuubi-common/src/main/scala/org/apache/kyuubi/service/authentication/JdbcAuthenticationProviderImpl.scala:
##########
@@ -0,0 +1,232 @@
+/*
+ * 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.kyuubi.service.authentication
+
+import java.sql.{Connection, DriverManager, PreparedStatement, Statement}
+import javax.security.sasl.AuthenticationException
+
+import org.apache.commons.lang3.StringUtils
+
+import org.apache.kyuubi.Logging
+import org.apache.kyuubi.config.KyuubiConf
+import org.apache.kyuubi.config.KyuubiConf._
+
+class JdbcAuthenticationProviderImpl(conf: KyuubiConf) extends PasswdAuthenticationProvider
+  with Logging {
+
+  private val dbDriver = conf.get(AUTHENTICATION_JDBC_DRIVER)
+  private val dbUrl = conf.get(AUTHENTICATION_JDBC_URL)
+  private val dbUserName = conf.get(AUTHENTICATION_JDBC_USERNAME)
+  private val dbPassword = conf.get(AUTHENTICATION_JDBC_PASSWORD)
+  private val querySql = conf.get(AUTHENTICATION_JDBC_QUERY)
+
+  private val SQL_PLACEHOLDER_REGEX = """\$\{.+?}""".r
+  private val USERNAME_SQL_PLACEHOLDER = "${username}"
+  private val PASSWORD_SQL_PLACEHOLDER = "${password}"
+
+  /**
+   * The authenticate method is called by the Kyuubi Server authentication layer
+   * to authenticate users for their requests.
+   * If a user is to be granted, return nothing/throw nothing.
+   * When a user is to be disallowed, throw an appropriate [[AuthenticationException]].
+   *
+   * @param user     The username received over the connection request
+   * @param password The password received over the connection request
+   * @throws AuthenticationException When a user is found to be invalid by the implementation
+   */
+  @throws[AuthenticationException]
+  override def authenticate(user: String, password: String): Unit = {
+    if (StringUtils.isBlank(user)) {
+      throw new AuthenticationException(s"Error validating, user is null" +
+        s" or contains blank space")
+    }
+
+    if (StringUtils.isBlank(password)) {
+      throw new AuthenticationException(s"Error validating, password is null" +
+        s" or contains blank space")
+    }
+
+    checkConfigs
+
+    loadJdbcDriverClass
+
+    var connection: Connection = null
+    var queryStatement: PreparedStatement = null
+
+    try {
+      connection = DriverManager.getConnection(dbUrl.get, dbUserName.orNull, dbPassword.orNull)
+
+      queryStatement = getAndPrepareStatement(connection, user, password)
+
+      val resultSet = queryStatement.executeQuery()
+
+      if (resultSet == null || !resultSet.next()) {
+        // Auth failed
+        throw new AuthenticationException(s"Password does not match or no such user. user:" +
+          s" $user , password length: ${password.length}")
+      }
+
+      // Auth passed
+
+    } catch {
+      case e: AuthenticationException =>
+        throw e
+      case e: Exception =>
+        error("Cannot get user info", e);
+        throw e
+    } finally {
+      closeDbConnection(connection, queryStatement)
+    }
+  }
+
+  private def checkConfigs: Unit = {
+    def configLog(config: String, value: String): String = s"JDBCAuthConfig: $config = '$value'"
+
+    debug(configLog("Driver Class", dbDriver.orNull))
+    debug(configLog("JDBC URL", dbUrl.orNull))
+    debug(configLog("Database Username", dbUserName.orNull))
+    debug(configLog("Database Password", dbPassword.orNull))

Review Comment:
   It supposed to be view in debug logging and only accessed by limited user. Let me change to log password length only.



-- 
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: notifications-unsubscribe@kyuubi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@kyuubi.apache.org
For additional commands, e-mail: notifications-help@kyuubi.apache.org