pan3793 commented on code in PR #3235: URL: https://github.com/apache/incubator-kyuubi/pull/3235#discussion_r947055015
########## kyuubi-common/src/main/scala/org/apache/kyuubi/service/authentication/JdbcAuthenticationProviderImpl.scala: ########## @@ -0,0 +1,235 @@ +/* + * 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, PreparedStatement, Statement} +import java.util.Properties +import javax.security.sasl.AuthenticationException + +import com.zaxxer.hikari.{HikariConfig, HikariDataSource} +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 driverClass = conf.get(AUTHENTICATION_JDBC_DRIVER) + private val jdbcUrl = conf.get(AUTHENTICATION_JDBC_URL) + private val jdbcUsername = conf.get(AUTHENTICATION_JDBC_USERNAME) + private val jdbcUserPassword = conf.get(AUTHENTICATION_JDBC_PASSWORD) + private val authQuerySql = conf.get(AUTHENTICATION_JDBC_QUERY) + + private val SQL_PLACEHOLDER_REGEX = """\$\{.+?}""".r + private val USERNAME_SQL_PLACEHOLDER = "${username}" + private val PASSWORD_SQL_PLACEHOLDER = "${password}" + + checkJdbcConfigs() + + private[kyuubi] val hikariDataSource = getHikariDataSource() + + /** + * 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") + } + + var connection: Connection = null + var queryStatement: PreparedStatement = null + + try { + connection = hikariDataSource.getConnection + + queryStatement = getAndPrepareQueryStatement(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 checkJdbcConfigs(): Unit = { + def configLog(config: String, value: String): String = s"JDBCAuthConfig: $config = '$value'" + + debug(configLog("Driver Class", driverClass.orNull)) + debug(configLog("JDBC URL", jdbcUrl.orNull)) + debug(configLog("Database username", jdbcUsername.orNull)) + debug(configLog("Database password length", jdbcUserPassword.getOrElse("").length.toString)) + debug(configLog("Query SQL", authQuerySql.orNull)) + + // Check if JDBC parameters valid + if (driverClass.isEmpty) { + throw new IllegalArgumentException("JDBC driver class is not configured.") + } + + if (jdbcUrl.isEmpty) { + throw new IllegalArgumentException("JDBC url is not configured") + } + + if (jdbcUsername.isEmpty || jdbcUserPassword.isEmpty) { + throw new IllegalArgumentException("JDBC username or password is not configured") + } + + // Check Query SQL + if (authQuerySql.isEmpty) { + throw new IllegalArgumentException("Query SQL is not configured") + } + val querySqlInLowerCase = authQuerySql.get.trim.toLowerCase + if (!querySqlInLowerCase.startsWith("select")) { // allow select query sql only + throw new IllegalArgumentException("Query SQL must start with \"SELECT\""); + } + if (!querySqlInLowerCase.contains("where")) { + warn("Query SQL does not contains \"WHERE\" keyword"); + } + if (!querySqlInLowerCase.contains("${username}")) { + warn("Query SQL does not contains \"${username}\" placeholder"); + } + } + + /** + * Extract all placeholders from query and put them into a list. + * + * @param sql + * @return + */ + private def getPlaceholderList(sql: String): List[String] = { + SQL_PLACEHOLDER_REGEX.findAllMatchIn(sql) + .map(m => m.matched) + .toList + } + + /** + * Replace all placeholders as "?" + * + * @param sql + * @return + */ + private def getPreparedSql(sql: String): String = { + SQL_PLACEHOLDER_REGEX.replaceAllIn(sql, "?") + } + + /** + * prepare the final query statement + * by replacing placeholder in query sql with user and password + * + * @param connection + * @param user + * @param password + * @return + */ + private def getAndPrepareQueryStatement( + connection: Connection, + user: String, + password: String): PreparedStatement = { + // Replace placeholders by "?" and prepare the statement + val stmt = connection.prepareStatement(getPreparedSql(authQuerySql.get)) + + // Extract placeholder list and use its order to pass parameters + val placeholderList: List[String] = getPlaceholderList(authQuerySql.get) + for (i <- placeholderList.indices) { + val param = placeholderList(i) match { + case USERNAME_SQL_PLACEHOLDER => user + case PASSWORD_SQL_PLACEHOLDER => password + case otherPlaceholder => + error(s"Unrecognized Placeholder In Query SQL: $otherPlaceholder") Review Comment: duplicated error message, the exeception will be propagate and logged ########## kyuubi-common/src/main/scala/org/apache/kyuubi/service/authentication/JdbcAuthenticationProviderImpl.scala: ########## @@ -0,0 +1,235 @@ +/* + * 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, PreparedStatement, Statement} +import java.util.Properties +import javax.security.sasl.AuthenticationException + +import com.zaxxer.hikari.{HikariConfig, HikariDataSource} +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 driverClass = conf.get(AUTHENTICATION_JDBC_DRIVER) + private val jdbcUrl = conf.get(AUTHENTICATION_JDBC_URL) + private val jdbcUsername = conf.get(AUTHENTICATION_JDBC_USERNAME) + private val jdbcUserPassword = conf.get(AUTHENTICATION_JDBC_PASSWORD) + private val authQuerySql = conf.get(AUTHENTICATION_JDBC_QUERY) + + private val SQL_PLACEHOLDER_REGEX = """\$\{.+?}""".r + private val USERNAME_SQL_PLACEHOLDER = "${username}" + private val PASSWORD_SQL_PLACEHOLDER = "${password}" + + checkJdbcConfigs() + + private[kyuubi] val hikariDataSource = getHikariDataSource() + + /** + * 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") + } + + var connection: Connection = null + var queryStatement: PreparedStatement = null + + try { + connection = hikariDataSource.getConnection + + queryStatement = getAndPrepareQueryStatement(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 checkJdbcConfigs(): Unit = { + def configLog(config: String, value: String): String = s"JDBCAuthConfig: $config = '$value'" + + debug(configLog("Driver Class", driverClass.orNull)) + debug(configLog("JDBC URL", jdbcUrl.orNull)) + debug(configLog("Database username", jdbcUsername.orNull)) + debug(configLog("Database password length", jdbcUserPassword.getOrElse("").length.toString)) + debug(configLog("Query SQL", authQuerySql.orNull)) + + // Check if JDBC parameters valid + if (driverClass.isEmpty) { + throw new IllegalArgumentException("JDBC driver class is not configured.") + } + + if (jdbcUrl.isEmpty) { + throw new IllegalArgumentException("JDBC url is not configured") + } + + if (jdbcUsername.isEmpty || jdbcUserPassword.isEmpty) { + throw new IllegalArgumentException("JDBC username or password is not configured") + } + + // Check Query SQL + if (authQuerySql.isEmpty) { + throw new IllegalArgumentException("Query SQL is not configured") + } + val querySqlInLowerCase = authQuerySql.get.trim.toLowerCase + if (!querySqlInLowerCase.startsWith("select")) { // allow select query sql only + throw new IllegalArgumentException("Query SQL must start with \"SELECT\""); + } + if (!querySqlInLowerCase.contains("where")) { + warn("Query SQL does not contains \"WHERE\" keyword"); + } + if (!querySqlInLowerCase.contains("${username}")) { + warn("Query SQL does not contains \"${username}\" placeholder"); + } + } + + /** + * Extract all placeholders from query and put them into a list. + * + * @param sql + * @return + */ + private def getPlaceholderList(sql: String): List[String] = { + SQL_PLACEHOLDER_REGEX.findAllMatchIn(sql) + .map(m => m.matched) + .toList + } + + /** + * Replace all placeholders as "?" + * + * @param sql + * @return + */ + private def getPreparedSql(sql: String): String = { + SQL_PLACEHOLDER_REGEX.replaceAllIn(sql, "?") + } + + /** + * prepare the final query statement + * by replacing placeholder in query sql with user and password + * + * @param connection + * @param user + * @param password + * @return + */ + private def getAndPrepareQueryStatement( + connection: Connection, + user: String, + password: String): PreparedStatement = { + // Replace placeholders by "?" and prepare the statement + val stmt = connection.prepareStatement(getPreparedSql(authQuerySql.get)) + + // Extract placeholder list and use its order to pass parameters + val placeholderList: List[String] = getPlaceholderList(authQuerySql.get) + for (i <- placeholderList.indices) { + val param = placeholderList(i) match { + case USERNAME_SQL_PLACEHOLDER => user + case PASSWORD_SQL_PLACEHOLDER => password + case otherPlaceholder => + error(s"Unrecognized Placeholder In Query SQL: $otherPlaceholder") Review Comment: duplicated error message, the exeception will be propagated and logged -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
