如何保护 Spark 中的密码和用户名(例如用于 JDBC 连接/访问 RDBMS 数据库)? [英] How to protect password and username in Spark (such as for JDBC connections/accessing RDBMS databases)?

查看:30
本文介绍了如何保护 Spark 中的密码和用户名(例如用于 JDBC 连接/访问 RDBMS 数据库)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个用例,需要将数据从 HDFS 导出到 RDBMS.我看到了这个 example .在这里,他们将用户名和密码存储在代码中.有什么办法可以在导出数据时隐藏密码,就像我们在 Sqoop 中可以选择密码别名一样.

We have a use case where we need to export data from HDFS to a RDBMS. I saw this example . Here they have store the username and password in the code. Is there any way to hide the password while export the data like we have the option of password-alias in Sqoop.

推荐答案

设置密码

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

At the command line as a plaintext spark config:

spark-submit --conf spark.jdbc.password=test_pass ... 

使用环境变量:

export jdbc_password=test_pass_export
spark-submit --conf spark.jdbc.password=$jdbc_password ...

使用 spark 配置属性文件:

Using spark config properties file:

echo "spark.jdbc.b64password=test_pass_prop" > credentials.properties
spark-submit --properties-file credentials.properties

使用 base64 编码来混淆":

With base64 encoding to "obfuscate":

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

在代码中使用密码

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)

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

spark command line interface help docs for --conf and --properties-file:

  --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.

属性文件名是任意的.

这篇关于如何保护 Spark 中的密码和用户名(例如用于 JDBC 连接/访问 RDBMS 数据库)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆