如何在 SQL 服务器的 Apache Spark 连接器中使用 Azure Key Vault

How to use Azure Key Vault in Apache Spark Connector for SQL Server

以下来自 Azure 团队 Using Apache Spark connector for SQL Server is using hard-coded user name and password. But I am storing password in Azure key Vault 的安全要求示例。

问题:在下面的示例代码中,我们如何使用存储在 Azure Key Vault 中的秘密(密码)来代替硬编码密码?例如,假设我创建了 Azure Key Vault 秘密调用 mySQLSecret,值为 myPassword。我将如何在下面的代码中使用 mySQLSecret

    server_name = "jdbc:sqlserver://{SERVER_ADDR}"
    database_name = "database_name"
    url = server_name + ";" + "databaseName=" + database_name + ";"
    
    table_name = "table_name"
    username = "username"
    password = "password123!#" # Please specify password here
    
    try:
      df.write \
        .format("com.microsoft.sqlserver.jdbc.spark") \
        .mode("overwrite") \
        .option("url", url) \
        .option("dbtable", table_name) \
        .option("user", username) \
        .option("password", password) \
        .save()
    except ValueError as error :
        print("Connector write failed", error)

最简单的方法是 link azure keyvault 作为 Databricks 秘密范围 (doc),并使用 dbutils.secrets.get 函数从中获取带有密码的秘密。此函数接受两个参数 - 您在 linking 时用于注册的秘密范围的名称,以及 keyvault

中的秘密名称