简体   繁体   English

如何使用 python 从 Azure app vault 获取数据库密码? 我在 google Dataproc 集群上运行这个 python 文件

[英]How to get DB password from Azure app vault using python ? I am running this python file on google Dataproc cluster

My Sql server DB password is saved on Azure app vault which has DATAREF ID as a identifier.我的 Sql 服务器数据库密码保存在 Azure 应用程序库中,该库以 DATAREF ID 作为标识符。 I need that password to create spark dataframe from table which is present in SQL server.我需要该密码才能从 SQL 服务器中存在的表创建 spark dataframe。 I am running this.py file on google Dataproc cluster.我在 google Dataproc 集群上运行 this.py 文件。 How can I get that password using python?如何使用 python 获取该密码?

Since you are accessing an Azure service from a non-Azure service, you will need a service principal.由于您正在从非 Azure 服务访问 Azure 服务,因此您将需要一个服务主体。 You can use certificate or secret.您可以使用证书或秘密。 See THIS link for the different methods.有关不同方法,请参阅链接。 You will need to give the service principal proper access and this will depend if you are using RBAC or access policy for your key vault.您需要为服务主体授予适当的访问权限,这取决于您使用的是 RBAC 还是密钥保管库的访问策略。 So the steps you need to follow are:因此,您需要遵循的步骤是:

  1. Create a key vault and create a secret.创建密钥保管库并创建机密。
  2. Create a Service principal or application registration.创建服务主体或应用程序注册。 Store the clientid, clientsecret and tenantid.存储 clientid、clientsecret 和 tenantid。
  3. Give the service principal proper access to the key vault(if you are using access policies) or to the specific secret(if you are using RBAC model)授予服务主体对密钥保管库(如果您使用访问策略)或特定机密(如果您使用 RBAC 模型)的适当访问权限

The python link for the code is HERE .代码的 python 链接为HERE The code that will work for you is below:适合您的代码如下:

from azure.identity import ClientSecretCredential
from azure.keyvault.secrets import SecretClient
tenantid = <your_tenant_id>
clientsecret = <your_client_secret>
clientid = <your_client_id>

my_credentials = ClientSecretCredential(tenant_id=tenantid, client_id=clientid, client_secret=clientsecret)

secret_client = SecretClient(vault_url="https://<your_keyvault_name>.vault.azure.net/", credential=my_credentials)
secret = secret_client.get_secret("<your_secret_name>")

print(secret.name)
print(secret.value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Python 创建具有生存时间的 Dataproc 集群 SDK - How to create a Dataproc cluster with time to live using Python SDK 如何使用 dataproc 客户端通过 google dataproc 集群作业为 spark 传递自定义作业 ID - how to pass custom job id via google dataproc cluster job for spark using dataproc client 如何使用 Azure SQL 从 Azure Key Vault 获取机密? - How to get the secrets from Azure Key Vault Using Azure SQL? 从客户端应用程序写入的 Google Dataproc 将集群的内部 IP 用于 Datanodes - Google Dataproc writing from a client app uses cluster's internal IP for Datanodes 无法使用 App Service 上的 Azure MSI 访问 Key Vault - Unable to get access to Key Vault using Azure MSI on App Service 从 dataproc 中的谷歌存储中读取文件 - Read a file from google storage in dataproc 您可以从 Dataproc 触发 Python 脚本吗? - Can you trigger Python Scripts from Dataproc? 如何从 Spark Dataproc 检查文件是否存在于 Google Storage 中? - How to check if a file exists in Google Storage from Spark Dataproc? 如何使用 Python 库轮询 Google 长时间运行的操作? - How do I poll Google Long-Running Operations using Python Library? 暂停 Dataproc 集群 - Google 计算引擎 - Pausing Dataproc cluster - Google Compute engine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM