gpt4 book ai didi

Python Azure 函数 - 使用 Key Vault 进行 MSI 身份验证

转载 作者:行者123 更新时间:2023-12-02 08:30:04 24 4
gpt4 key购买 nike

我正在尝试设置一个 Python Azure 函数,该函数将使用托管标识从 key 保管库检索 secret 。我已授予功能应用程序的托管身份权限来访问 Key Vault 并检索 secret 。我已经根据此处看到的 Microsoft 文档配置了我的 Python 脚本:

https://learn.microsoft.com/en-us/python/api/overview/azure/key-vault?view=azure-python

我尝试使用 MSI 身份验证,而不是使用默认凭据,如下所述:

https://learn.microsoft.com/en-us/azure/developer/python/azure-sdk-authenticate#mgmt-auth-msi

如果我使用 MSI 身份验证链接中提供的示例,它就可以工作。但是当我使用 Key Vault 方法时,我收到以下错误:

MSIAuthentication' object has no attribute 'get_token'

我的代码如下所示

    if name:
# Create MSI Authentication
credentials = MSIAuthentication()

try:
secret_client = SecretClient(vault_url="https://myvault.vault.azure.net", credential=credentials)

secret = secret_client.get_secret("mySecret")

return func.HttpResponse(f"My super secret is: {secret.value}!")

except Exception as e:
err = "type error: " + str(e)
return func.HttpResponse(f"{err}")

有关如何使此方法发挥作用的任何建议。

TIA

(我知道我可以利用应用程序设置来设置到 Key Vault 的链接并在我的 Python 脚本中引用该变量。此方法的问题是 Key Vault key 是当 key 轮换时需要重新启动该功能应用程序)

最佳答案

您需要将MSIAuthentication更改为ManagedIdentityCredential 。然后它就会工作得很好。示例代码如下:

from azure.identity import ManagedIdentityCredential
from azure.keyvault.secrets import SecretClient

credentials = ManagedIdentityCredential()

secret_client = SecretClient(vault_url="https://myKeyvault.vault.azure.net", credential=credentials)
secret = secret_client.get_secret("mysecret")
print(secret.value)

关于Python Azure 函数 - 使用 Key Vault 进行 MSI 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61644298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com