-
Notifications
You must be signed in to change notification settings - Fork 28.1k
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
[SPARK-31021][SQL] Support MariaDB Kerberos login in JDBC connector #28019
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ff04926
[SPARK-31021][SQL] Support MariaDB Kerberos login in JDBC connector
gaborgsomogyi 89f5ac9
Review fixes
gaborgsomogyi 2bb6426
Simplification since creating new database is not essential for kerbe…
gaborgsomogyi 2740a50
Review fix
gaborgsomogyi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
external/docker-integration-tests/src/test/resources/mariadb_docker_entrypoint.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/usr/bin/env bash | ||
|
||
# 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. | ||
# | ||
|
||
dpkg-divert --add /bin/systemctl && ln -sT /bin/true /bin/systemctl | ||
apt update | ||
apt install -y mariadb-plugin-gssapi-server | ||
echo "gssapi_keytab_path=/docker-entrypoint-initdb.d/mariadb.keytab" >> /etc/mysql/mariadb.conf.d/auth_gssapi.cnf | ||
echo "gssapi_principal_name=mariadb/__IP_ADDRESS_REPLACE_ME__@EXAMPLE.COM" >> /etc/mysql/mariadb.conf.d/auth_gssapi.cnf | ||
docker-entrypoint.sh mysqld |
20 changes: 20 additions & 0 deletions
20
external/docker-integration-tests/src/test/resources/mariadb_krb_setup.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env bash | ||
|
||
# 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. | ||
# | ||
|
||
mysql -u root -p'rootpass' -e 'CREATE USER "mariadb/__IP_ADDRESS_REPLACE_ME__@EXAMPLE.COM" IDENTIFIED WITH gssapi;' | ||
mysql -u root -p'rootpass' -D mysql -e 'GRANT ALL PRIVILEGES ON *.* TO "mariadb/__IP_ADDRESS_REPLACE_ME__@EXAMPLE.COM";' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,17 +18,22 @@ | |
package org.apache.spark.sql.jdbc | ||
|
||
import java.io.{File, FileInputStream, FileOutputStream} | ||
import java.sql.Connection | ||
import java.util.Properties | ||
import javax.security.auth.login.Configuration | ||
|
||
import scala.io.Source | ||
|
||
import org.apache.hadoop.minikdc.MiniKdc | ||
|
||
import org.apache.spark.sql.Row | ||
import org.apache.spark.sql.types.StringType | ||
import org.apache.spark.util.{SecurityUtils, Utils} | ||
|
||
abstract class DockerKrbJDBCIntegrationSuite extends DockerJDBCIntegrationSuite { | ||
private var kdc: MiniKdc = _ | ||
protected var workDir: File = _ | ||
protected var entryPointDir: File = _ | ||
protected var initDbDir: File = _ | ||
protected val userName: String | ||
protected var principal: String = _ | ||
protected val keytabFileName: String | ||
|
@@ -46,8 +51,9 @@ abstract class DockerKrbJDBCIntegrationSuite extends DockerJDBCIntegrationSuite | |
|
||
principal = s"$userName@${kdc.getRealm}" | ||
|
||
workDir = Utils.createTempDir() | ||
val keytabFile = new File(workDir, keytabFileName) | ||
entryPointDir = Utils.createTempDir() | ||
initDbDir = Utils.createTempDir() | ||
val keytabFile = new File(initDbDir, keytabFileName) | ||
keytabFullPath = keytabFile.getAbsolutePath | ||
kdc.createPrincipal(keytabFile, userName) | ||
logInfo(s"Created keytab file: $keytabFullPath") | ||
|
@@ -62,6 +68,7 @@ abstract class DockerKrbJDBCIntegrationSuite extends DockerJDBCIntegrationSuite | |
try { | ||
if (kdc != null) { | ||
kdc.stop() | ||
kdc = null | ||
} | ||
Configuration.setConfiguration(null) | ||
SecurityUtils.setGlobalKrbDebug(false) | ||
|
@@ -71,7 +78,7 @@ abstract class DockerKrbJDBCIntegrationSuite extends DockerJDBCIntegrationSuite | |
} | ||
|
||
protected def copyExecutableResource( | ||
fileName: String, dir: File, processLine: String => String) = { | ||
fileName: String, dir: File, processLine: String => String = identity) = { | ||
val newEntry = new File(dir.getAbsolutePath, fileName) | ||
newEntry.createNewFile() | ||
Utils.tryWithResource( | ||
|
@@ -91,4 +98,64 @@ abstract class DockerKrbJDBCIntegrationSuite extends DockerJDBCIntegrationSuite | |
logInfo(s"Created executable resource file: ${newEntry.getAbsolutePath}") | ||
newEntry | ||
} | ||
|
||
override def dataPreparation(conn: Connection): Unit = { | ||
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. FYI to further reviewers: this, and below tests are moved from |
||
conn.prepareStatement("CREATE TABLE bar (c0 text)").executeUpdate() | ||
conn.prepareStatement("INSERT INTO bar VALUES ('hello')").executeUpdate() | ||
} | ||
|
||
test("Basic read test in query option") { | ||
// This makes sure Spark must do authentication | ||
Configuration.setConfiguration(null) | ||
|
||
val expectedResult = Set("hello").map(Row(_)) | ||
|
||
val query = "SELECT c0 FROM bar" | ||
// query option to pass on the query string. | ||
val df = spark.read.format("jdbc") | ||
.option("url", jdbcUrl) | ||
.option("keytab", keytabFullPath) | ||
.option("principal", principal) | ||
.option("query", query) | ||
.load() | ||
assert(df.collect().toSet === expectedResult) | ||
} | ||
|
||
test("Basic read test in create table path") { | ||
// This makes sure Spark must do authentication | ||
Configuration.setConfiguration(null) | ||
|
||
val expectedResult = Set("hello").map(Row(_)) | ||
|
||
val query = "SELECT c0 FROM bar" | ||
// query option in the create table path. | ||
sql( | ||
s""" | ||
|CREATE OR REPLACE TEMPORARY VIEW queryOption | ||
|USING org.apache.spark.sql.jdbc | ||
|OPTIONS (url '$jdbcUrl', query '$query', keytab '$keytabFullPath', principal '$principal') | ||
""".stripMargin.replaceAll("\n", " ")) | ||
assert(sql("select c0 from queryOption").collect().toSet === expectedResult) | ||
} | ||
|
||
test("Basic write test") { | ||
// This makes sure Spark must do authentication | ||
Configuration.setConfiguration(null) | ||
|
||
val props = new Properties | ||
props.setProperty("keytab", keytabFullPath) | ||
props.setProperty("principal", principal) | ||
|
||
val tableName = "write_test" | ||
sqlContext.createDataFrame(Seq(("foo", "bar"))) | ||
.write.jdbc(jdbcUrl, tableName, props) | ||
val df = sqlContext.read.jdbc(jdbcUrl, tableName, props) | ||
|
||
val schema = df.schema | ||
assert(schema.map(_.dataType).toSeq === Seq(StringType, StringType)) | ||
val rows = df.collect() | ||
assert(rows.length === 1) | ||
assert(rows(0).getString(0) === "foo") | ||
assert(rows(0).getString(1) === "bar") | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...tegration-tests/src/test/scala/org/apache/spark/sql/jdbc/MariaDBKrbIntegrationSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* 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.jdbc | ||
|
||
import javax.security.auth.login.Configuration | ||
|
||
import com.spotify.docker.client.messages.{ContainerConfig, HostConfig} | ||
|
||
import org.apache.spark.sql.execution.datasources.jdbc.connection.SecureConnectionProvider | ||
import org.apache.spark.tags.DockerTest | ||
|
||
@DockerTest | ||
class MariaDBKrbIntegrationSuite extends DockerKrbJDBCIntegrationSuite { | ||
override protected val userName = s"mariadb/$dockerIp" | ||
override protected val keytabFileName = "mariadb.keytab" | ||
|
||
override val db = new DatabaseOnDocker { | ||
override val imageName = "mariadb:10.4" | ||
override val env = Map( | ||
"MYSQL_ROOT_PASSWORD" -> "rootpass" | ||
) | ||
override val usesIpc = false | ||
override val jdbcPort = 3306 | ||
|
||
override def getJdbcUrl(ip: String, port: Int): String = | ||
s"jdbc:mysql://$ip:$port/mysql?user=$principal" | ||
|
||
override def getEntryPoint: Option[String] = | ||
Some("/docker-entrypoint/mariadb_docker_entrypoint.sh") | ||
|
||
override def beforeContainerStart( | ||
hostConfigBuilder: HostConfig.Builder, | ||
containerConfigBuilder: ContainerConfig.Builder): Unit = { | ||
def replaceIp(s: String): String = s.replace("__IP_ADDRESS_REPLACE_ME__", dockerIp) | ||
copyExecutableResource("mariadb_docker_entrypoint.sh", entryPointDir, replaceIp) | ||
copyExecutableResource("mariadb_krb_setup.sh", initDbDir, replaceIp) | ||
|
||
hostConfigBuilder.appendBinds( | ||
HostConfig.Bind.from(entryPointDir.getAbsolutePath) | ||
.to("/docker-entrypoint").readOnly(true).build(), | ||
HostConfig.Bind.from(initDbDir.getAbsolutePath) | ||
.to("/docker-entrypoint-initdb.d").readOnly(true).build() | ||
) | ||
} | ||
} | ||
|
||
override protected def setAuthentication(keytabFile: String, principal: String): Unit = { | ||
val config = new SecureConnectionProvider.JDBCConfiguration( | ||
Configuration.getConfiguration, "Krb5ConnectorContext", keytabFile, principal) | ||
Configuration.setConfiguration(config) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Changing from
mysql
tomariadb
is needed because of this: https://stackoverflow.com/questions/52718788/how-to-read-data-from-mariadb-using-spark-java