在Azure Databricks上获取Azure Key Vault中所存储的机密(secret)的两种方式?
实例代码如下:
import os from azure.keyvault.secrets import SecretClient from azure.identity import DefaultAzureCredential KVUri = f"https://<your key vault name>.vault.azure.cn/" credential = DefaultAzureCredential() client = SecretClient(vault_url=KVUri, credential=credential) retrieved_secret = client.get_secret("<your secret name>") print(f"Your secret is '{retrieved_secret.value}'.")
在执行中,会先后遇见
%pip install azure.keyvault.secrets %pip install azure.identity dbutils.library.restartPython()
执行效果截图:
解决以上两个问题后,再次执行Python Code,可以成功获取到Key Vault中的机密信息。
当Azure Databricks和Azure Key Vault资源都创建成功后。
首先在Databricks的页面中添加 key Vault backed-secret scope,使用如下的URL打开Create Secret Scope
URL : https://<Azure Databricks Service URL>.databricks.azure.cn/#secrets/createScope
在保存中如果遇见权限问题,可以在Key Vault的 Access Policy中为 AzureDatabricks 添加权限(GET, SET等权限)
设置完成后,回到Databricks的Notebook页面,使用如下语句进行验证:
ENCODED_AUTH_KEY = dbutils.secrets.get(scope = "scope name in databricks", key = "the secret name in key value") print(f"this result is:'{ENCODED_AUTH_KEY}'")
执行效果截图:
Databricks Secrets scopes: https://learn.microsoft.com/zh-cn/azure/databricks/security/secrets/secret-scopes
Databricks Secrets: https://learn.microsoft.com/zh-cn/azure/databricks/security/secrets/secrets
Databricks Escrets redaction: https://learn.microsoft.com/zh-cn/azure/databricks/security/secrets/redaction
【END】
spring.cloud.azure.keyvault.secret.property-sources[0].profile.cloud-type=AZURE_CHINA