gpt4 book ai didi

Python Azure SDK : How to retrieve secrets from keyvault?

转载 作者:行者123 更新时间:2023-11-28 21:03:56 27 4
gpt4 key购买 nike

我需要从 keyvault 检索 secret 。这是我到目前为止的代码:

from azure.mgmt.keyvault import KeyVaultManagementClient
from azure.common.credentials import ServicePrincipalCredentials


subscription_id = 'x'
# See above for details on creating different types of AAD credentials
credentials = ServicePrincipalCredentials(
client_id = 'x',
secret = 'x',
tenant = 'x'
)

kv_client = KeyVaultManagementClient(credentials, subscription_id)

for vault in kv_client.vaults.list():
print(vault)

但我收到此错误:

msrestazure.azure_exceptions.CloudError: Azure Error: AuthorizationFailed Message: The client 'x' with object id 'x' does not have authorization to perform action 'Microsoft.Resources/subscriptions/resources/read' over scope '/subscriptions/x'.

现在我可以使用 C# 代码/POwershell 使用相同的凭据访问相同的 keyvault,因此授权肯定没有问题。不知道为什么它不能使用 SDK 工作。请帮忙。

最佳答案

如果您希望通过 ServicePrincipalCredentials 实例进行访问,则可以使用:

from azure.keyvault import KeyVaultClient, KeyVaultAuthentication
from azure.common.credentials import ServicePrincipalCredentials

credentials = None

def auth_callback(server, resource, scope):
credentials = ServicePrincipalCredentials(
client_id = '',
secret = '',
tenant = '',
resource = "https://vault.azure.net"
)
token = credentials.token
return token['token_type'], token['access_token']

client = KeyVaultClient(KeyVaultAuthentication(auth_callback))

secret_bundle = client.get_secret("https://vault_url", "secret_id", "")

print(secret_bundle.value)

这假设您不想传递版本。如果这样做,您可以替换最后一个参数。

关于Python Azure SDK : How to retrieve secrets from keyvault?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45967706/

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