Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0dc75fe
implement JDBC Authentication Method
bowenliang123 Aug 15, 2022
0e7f0ad
refactor config and init process.remove unused import.
bowenliang123 Aug 15, 2022
996f796
add unit test in JdbcAuthenticationProviderImplSuite
bowenliang123 Aug 15, 2022
49c18c2
update
bowenliang123 Aug 15, 2022
df4be56
update code style
bowenliang123 Aug 15, 2022
7025330
fix derby startup error in test
bowenliang123 Aug 15, 2022
46cc1dd
add config docs in docs/deployment/settings.md
bowenliang123 Aug 15, 2022
15176b2
fix import orders
bowenliang123 Aug 15, 2022
cd2c7c2
update settings.md config doc
bowenliang123 Aug 15, 2022
1dc4187
update settings.md config doc
bowenliang123 Aug 15, 2022
575301c
update options usage
bowenliang123 Aug 15, 2022
30974d1
update format
bowenliang123 Aug 15, 2022
3672919
fix ddl statement and remove truncate statement in test
bowenliang123 Aug 16, 2022
cdec206
more test cases
bowenliang123 Aug 16, 2022
653bc12
add more checks for query sql
bowenliang123 Aug 16, 2022
aeb19ce
update doc
bowenliang123 Aug 16, 2022
b9ffac3
Merge branch 'master' into feature-jdbc-auth-provider
bowenliang123 Aug 16, 2022
9885f81
add JDBC condition for getValidPasswordAuthMethod
bowenliang123 Aug 16, 2022
4ebe12e
add JDBC value to AuthTypes enum
bowenliang123 Aug 16, 2022
1c956df
update KyuubiAuthenticationFactorySuite
bowenliang123 Aug 16, 2022
5a0ac49
output password length only in checkConfigs
bowenliang123 Aug 16, 2022
3a4d5fe
update checkConfigs() signature
bowenliang123 Aug 16, 2022
a4fe582
refactor connection creation on using HikariDataSource in HikariCP. a…
bowenliang123 Aug 16, 2022
543c66c
prefer scala style string usage
bowenliang123 Aug 16, 2022
6765aff
changed to use in-memory derby db for test
bowenliang123 Aug 16, 2022
77f5f86
remove unuseful comment
bowenliang123 Aug 16, 2022
a9404fa
use {} for intercept
bowenliang123 Aug 16, 2022
6fc42bf
code styling
bowenliang123 Aug 16, 2022
e9af096
use clone instead of repeatly generating configs
bowenliang123 Aug 16, 2022
d5f43e0
remove unuseful logs for unrecognized placeholder error
bowenliang123 Aug 16, 2022
17403b3
cleanup docs
bowenliang123 Aug 17, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
more test cases
  • Loading branch information
bowenliang123 committed Aug 16, 2022
commit cdec2066fed7de0994d63f3f0264ff17c0982e2d
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,24 @@ class JdbcAuthenticationProviderImpl(conf: KyuubiConf) extends PasswdAuthenticat
debug(configLog("Query SQL", querySql.orNull))

// Check if JDBC parameters valid
if (dbDriver.isEmpty || dbUrl.isEmpty || dbUserName.isEmpty || dbPassword.isEmpty) {
error("User auth Database has not been configured!")
throw new IllegalArgumentException("User auth Database has not been configured!")
if (dbDriver.isEmpty) {
throw new IllegalArgumentException("JDBC driver class is not configured.")
}

if (dbUrl.isEmpty) {
throw new IllegalArgumentException("JDBC url is not configured")
}

if (dbUserName.isEmpty || dbPassword.isEmpty) {
throw new IllegalArgumentException("JDBC username or password is not configured")
}

// Check Query SQL
if (querySql.isEmpty) {
error("Query SQL not configured!")
throw new IllegalArgumentException("Query SQL not configured!")
throw new IllegalArgumentException("Query SQL is not configured")
}
if (!querySql.get.trim.toLowerCase.startsWith("select")) { // only allow select query sql
error("Query SQL must start with \"select\"!")
throw new IllegalArgumentException("Query SQL must start with \"select\"!");
throw new IllegalArgumentException("Query SQL must start with \"SELECT\"");
}
}

Expand Down Expand Up @@ -159,9 +164,9 @@ class JdbcAuthenticationProviderImpl(conf: KyuubiConf) extends PasswdAuthenticat
* @return
*/
private def getAndPrepareStatement(
connection: Connection,
user: String,
password: String): PreparedStatement = {
connection: Connection,
user: String,
password: String): PreparedStatement = {
// Replace placeholders by "?" and prepare the statement
val stmt = connection.prepareStatement(getPreparedSql(querySql.get))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,20 @@ import org.apache.kyuubi.config.KyuubiConf
import org.apache.kyuubi.config.KyuubiConf._

class JdbcAuthenticationProviderImplSuite extends KyuubiFunSuite {
protected val dbUser: String = "bowenliang123"
protected val dbPasswd: String = "bowenliang123"
protected var jdbcUrl: String = _

protected val authUser: String = "liangtiancheng"
protected val authPasswd: String = "liangtiancheng"

protected val dbUser: String = "liangbowen"
protected val dbPasswd: String = "liangbowen"

protected var jdbcUrl: String = _

private val conf = new KyuubiConf()
protected var conf = new KyuubiConf()
var conn: Connection = _
var authDb: Path = _

override def beforeAll(): Unit = {
super.beforeAll()

val datasourceProperties = new Properties()
datasourceProperties.put("user", dbUser)
datasourceProperties.put("password", dbPasswd)
Expand All @@ -54,7 +55,7 @@ class JdbcAuthenticationProviderImplSuite extends KyuubiFunSuite {
+ ";password=" + dbPasswd,
datasourceProperties)

conn.prepareStatement("create schema " + dbUser).execute();
conn.prepareStatement("CREATE SCHEMA " + dbUser).execute();

conn.prepareStatement("CREATE TABLE user_auth (" +
"username VARCHAR(64) NOT NULL PRIMARY KEY, " +
Expand All @@ -66,17 +67,12 @@ class JdbcAuthenticationProviderImplSuite extends KyuubiFunSuite {
insertStmt.setString(2, authPasswd)
insertStmt.execute();

conf.set(AUTHENTICATION_JDBC_DRIVER, "org.apache.derby.jdbc.AutoloadedDriver")
conf.set(AUTHENTICATION_JDBC_URL, jdbcUrl)
conf.set(AUTHENTICATION_JDBC_USERNAME, dbUser)
conf.set(AUTHENTICATION_JDBC_PASSWORD, dbPasswd)
conf.set(
AUTHENTICATION_JDBC_QUERY,
"select 1 from user_auth " +
"where username=${username} and passwd=${password}")
conf = genJdbcAuthConfigs
}

override def afterAll(): Unit = {
super.afterAll()

// shutdown derby database
try {
DriverManager.getConnection(s"jdbc:derby:;databaseName=$authDb;shutdown=true")
Expand All @@ -85,23 +81,67 @@ class JdbcAuthenticationProviderImplSuite extends KyuubiFunSuite {
}
}

def genJdbcAuthConfigs: KyuubiConf = {
conf = new KyuubiConf()
conf.set(AUTHENTICATION_JDBC_DRIVER, "org.apache.derby.jdbc.AutoloadedDriver")
conf.set(AUTHENTICATION_JDBC_URL, jdbcUrl)
conf.set(AUTHENTICATION_JDBC_USERNAME, dbUser)
conf.set(AUTHENTICATION_JDBC_PASSWORD, dbPasswd)
conf.set(
AUTHENTICATION_JDBC_QUERY,
"SELECT 1 FROM user_auth " +
" WHERE username=${username} and passwd=${password}")
conf
}

test("authenticate tests") {
var providerImpl = new JdbcAuthenticationProviderImpl(conf)

providerImpl.authenticate(authUser, authPasswd)

val e1 = intercept[AuthenticationException](providerImpl.authenticate("", ""))
assert(e1.getMessage.contains("user is null"))

val e2 = intercept[AuthenticationException](providerImpl.authenticate("kyuubi", ""))
assert(e2.getMessage.contains("password is null"))

providerImpl.authenticate(authUser, authPasswd)

val e4 = intercept[AuthenticationException](
providerImpl.authenticate(authPasswd, "pass"))
assert(e4.isInstanceOf[AuthenticationException])

conf = genJdbcAuthConfigs
conf.unset(AUTHENTICATION_JDBC_URL)
providerImpl = new JdbcAuthenticationProviderImpl(conf)
val e5 = intercept[IllegalArgumentException](providerImpl.authenticate(authUser, authPasswd))
assert(e5.getMessage.contains("User auth Database has not been configured!"))
assert(e5.getMessage.contains("JDBC url is not configured"))

conf = genJdbcAuthConfigs
conf.unset(AUTHENTICATION_JDBC_USERNAME)
providerImpl = new JdbcAuthenticationProviderImpl(conf)
val e6 = intercept[IllegalArgumentException](providerImpl.authenticate(authUser, authPasswd))
assert(e6.getMessage.contains("JDBC username or password is not configured"))

conf = genJdbcAuthConfigs
conf.unset(AUTHENTICATION_JDBC_PASSWORD)
providerImpl = new JdbcAuthenticationProviderImpl(conf)
val e7 = intercept[IllegalArgumentException](providerImpl.authenticate(authUser, authPasswd))
assert(e7.getMessage.contains("JDBC username or password is not configured"))

conf = genJdbcAuthConfigs
conf.unset(AUTHENTICATION_JDBC_QUERY)
providerImpl = new JdbcAuthenticationProviderImpl(conf)
val e8 = intercept[IllegalArgumentException](providerImpl.authenticate(authUser, authPasswd))
assert(e8.getMessage.contains("Query SQL is not configured"))

conf.set(AUTHENTICATION_JDBC_QUERY, "INSERT INTO user_auth (username, password) " +
" VALUES ('demouser','demopassword'); ")
providerImpl = new JdbcAuthenticationProviderImpl(conf)
val e9 = intercept[IllegalArgumentException](providerImpl.authenticate(authUser, authPasswd))
assert(e9.getMessage.contains("Query SQL must start with \"SELECT\""))

conf.unset(AUTHENTICATION_JDBC_URL)
providerImpl = new JdbcAuthenticationProviderImpl(conf)
val e10 = intercept[IllegalArgumentException](providerImpl.authenticate(authUser, authPasswd))
assert(e10.getMessage.contains("JDBC url is not configured"))
}
}