From 6e30de1fd752c1a9ec73c02eb75ea1f80fc4cb23 Mon Sep 17 00:00:00 2001 From: Yuming Wang Date: Fri, 9 Aug 2019 07:24:43 +0800 Subject: [PATCH] (cherry picked from commit d19a56f9dbef4c995d80d4b46d03bfbfa4843c5c) --- .../spark/sql/execution/command/tables.scala | 2 +- .../org/apache/spark/sql/jdbc/JDBCSuite.scala | 29 ++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/sql/core/src/main/scala/org/apache/spark/sql/execution/command/tables.scala b/sql/core/src/main/scala/org/apache/spark/sql/execution/command/tables.scala index 5687993a3226..6dc842690219 100644 --- a/sql/core/src/main/scala/org/apache/spark/sql/execution/command/tables.scala +++ b/sql/core/src/main/scala/org/apache/spark/sql/execution/command/tables.scala @@ -1062,7 +1062,7 @@ case class ShowCreateTableCommand(table: TableIdentifier) extends RunnableComman private def showDataSourceTableOptions(metadata: CatalogTable, builder: StringBuilder): Unit = { builder ++= s"USING ${metadata.provider.get}\n" - val dataSourceOptions = metadata.storage.properties.map { + val dataSourceOptions = SQLConf.get.redactOptions(metadata.storage.properties).map { case (key, value) => s"${quoteIdentifier(key)} '${escapeSingleQuotedString(value)}'" } ++ metadata.storage.locationUri.flatMap { location => if (metadata.tableType == MANAGED) { diff --git a/sql/core/src/test/scala/org/apache/spark/sql/jdbc/JDBCSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/jdbc/JDBCSuite.scala index f4326aa6afd2..1e1d1eec604a 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/jdbc/JDBCSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/jdbc/JDBCSuite.scala @@ -26,10 +26,11 @@ import org.scalatest.{BeforeAndAfter, PrivateMethodTester} import org.apache.spark.SparkException import org.apache.spark.sql.{AnalysisException, DataFrame, QueryTest, Row} +import org.apache.spark.sql.catalyst.TableIdentifier import org.apache.spark.sql.catalyst.parser.CatalystSqlParser import org.apache.spark.sql.catalyst.util.CaseInsensitiveMap import org.apache.spark.sql.execution.DataSourceScanExec -import org.apache.spark.sql.execution.command.ExplainCommand +import org.apache.spark.sql.execution.command.{ExplainCommand, ShowCreateTableCommand} import org.apache.spark.sql.execution.datasources.LogicalRelation import org.apache.spark.sql.execution.datasources.jdbc.{JDBCOptions, JDBCPartition, JDBCRDD, JDBCRelation, JdbcUtils} import org.apache.spark.sql.execution.metric.InputOutputMetricsHelper @@ -1007,6 +1008,32 @@ class JDBCSuite extends QueryTest } } + test("Hide credentials in show create table") { + val password = "testPass" + val tableName = "tab1" + withTable(tableName) { + sql( + s""" + |CREATE TABLE $tableName + |USING org.apache.spark.sql.jdbc + |OPTIONS ( + | url '$urlWithUserAndPass', + | dbtable 'TEST.PEOPLE', + | user 'testUser', + | password '$password') + """.stripMargin) + + val show = ShowCreateTableCommand(TableIdentifier(tableName)) + spark.sessionState.executePlan(show).executedPlan.executeCollect().foreach { r => + assert(!r.toString.contains(password)) + } + + sql(s"SHOW CREATE TABLE $tableName").collect().foreach { r => + assert(!r.toString().contains(password)) + } + } + } + test("SPARK 12941: The data type mapping for StringType to Oracle") { val oracleDialect = JdbcDialects.get("jdbc:oracle://127.0.0.1/db") assert(oracleDialect.getJDBCType(StringType).