如何保护Spark中的密码和用户名(例如用于JDBC连接/访问RDBMS数据库)?

Raj*_*hra 7 apache-spark apache-spark-sql

我们有一个用例,需要将数据从HDFS导出到RDBMS。我看到了这个例子。他们在这里将用户名和密码存储在代码中。像我们在Sqoop中选择了password-alias一样,在导出数据时是否有任何隐藏密码的方法。

Gar*_*n S 6

设置密码

在命令行作为明文火花配置:

spark-submit --conf spark.jdbc.password=test_pass ... 
Run Code Online (Sandbox Code Playgroud)

使用环境变量:

export jdbc_password=test_pass_export
spark-submit --conf spark.jdbc.password=$jdbc_password ...
Run Code Online (Sandbox Code Playgroud)

使用 spark 配置属性文件:

echo "spark.jdbc.b64password=test_pass_prop" > credentials.properties
spark-submit --properties-file credentials.properties
Run Code Online (Sandbox Code Playgroud)

使用 base64 编码“混淆”:

echo "spark.jdbc.b64password=$(echo -n test_pass_prop | base64)" > credentials_b64.properties
spark-submit --properties-file credentials_b64.properties
Run Code Online (Sandbox Code Playgroud)

在代码中使用密码

import java.util.Base64 // for base64
import java.nio.charset.StandardCharsets // for base64
val properties = new java.util.Properties()
properties.put("driver", "com.mysql.jdbc.Driver")
properties.put("url", "jdbc:mysql://mysql-host:3306")
properties.put("user", "test_user")
val password = new String(Base64.getDecoder().decode(spark.conf.get("spark.jdbc.b64password")), StandardCharsets.UTF_8)
properties.put("password", password)
val models = spark.read.jdbc(properties.get("url").toString, "ml_models", properties)
Run Code Online (Sandbox Code Playgroud)

编辑:--conf 和--properties-file 的 spark 命令行界面帮助文档:

  --conf PROP=VALUE           Arbitrary Spark configuration property.
  --properties-file FILE      Path to a file from which to load extra properties. If not
                              specified, this will look for conf/spark-defaults.conf.
Run Code Online (Sandbox Code Playgroud)

属性文件名是任意的。