如何解密python中的密码

How to decrypt the password in python

我的代码正在使用 pyspark 连接到 sql 服务器。对于该连接,我在 jceks 中获取加密密码。我如何解密该密码并用于从 sql 服务器加载表格。请提出建议。

import pyspark
import re
from pyspark_llap import HiveWarehouseSession
from pyspark.sql.functions import struct
from pyspark.sql.functions import *
from pyspark.sql.session import SparkSession

spark = SparkSession \
    .builder \
    .appName("Python Spark SQL data source example") \
    .getOrCreate()

hive = HiveWarehouseSession.session(spark).build()

df1 = spark.read.format("jdbc") \
    .option("url", "URL") \
    .option("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver") \
    .option("dbtable", "tableName") \
    .option("user", "user") \
    .option("password", "password_alias").load()

我知道现在回答这个问题有点晚了,这是将别名作为密码传递的方法之一

您需要使用 hadoopConfiguration 解密 password_alias,并将其传递给 spark。

#Declaring Jceks path and password alias path
    jceks_path="jceks://hdfs/sqlserver.password.jceks"
    alias="password_alias"

# Reading the path of jceks using hadoopConfiguration
    conf = spark.sparkContext._jsc.hadoopConfiguration()
    conf.set('{0}'.format("hadoop.security.credential.provider.path"), jceks_path)

# Get password and make it a string.
    credential_raw = conf.getPassword(alias)
    password = ''
        for i in range(credential_raw.__len__()):
            password = password + str(credential_raw.__getitem__(i))

# Pass the password string to spark.
    df1 = spark.read.format("jdbc") \
        .option("url", "URL") \
        .option("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver") \
        .option("dbtable", "tableName") \
        .option("user", "user") \
        .option("password", password).load()