从Azure密钥保管库读取机密的Python脚本

huangapple go评论170阅读模式
英文:

Python script to read secrets from Azure Key Vault

问题

  1. 我正在尝试编写一个Python脚本Azure Key Vault读取机密在使用***SecretClient***类时我遇到了身份验证的问题
  2. 我的代码如下
  3. ```python
  4. from azure.identity import DefaultAzureCredential
  5. from azure.keyvault.secrets import SecretClient
  6. CREDENTIAL = DefaultAzureCredential()
  7. client = SecretClient(
  8. vault_url="https://my_vault_name.vault.azure.net/",
  9. credential=CREDENTIAL
  10. )
  11. secret = client.get_secret('my_secret_name')

我得到的错误如下:

EnvironmentCredential: EnvironmentCredential身份验证不可用。环境变量未完全配置。请访问https://aka.ms/azsdk/python/identity/environmentcredential/troubleshoot进行故障排除。

ManagedIdentityCredential: ManagedIdentityCredential身份验证不可用,IMDS终结点没有响应。

SharedTokenCacheCredential: 当前凭据未配置为获取租户74******-****-****-****-**********62的令牌。要为此租户启用获取令牌,请在创建凭据时将其添加到additionally_allowed_tenants中,或者将“*”添加到additionally_allowed_tenants以允许获取任何租户的令牌。

我在Azure门户中创建了托管标识,并将其“分配”给了我的Key Vault,并赋予了所有可能的权限。

我也尝试了以下方法:

  1. CREDENTIAL = azure.identity.ManagedIdentityCredential(managed_identity_client_id='my_managed_identity_client_id')

  1. CREDENTIAL = ManagedIdentityCredential()

但我得到了上述相同的ManagedIdentityCredential错误。

请注意,我试图在我的本地机器上运行该代码。而且,我已经尝试过在脚本中使用DefaultAzureCredential()类上传文件到我的Blob或列出所有资源,它可以正常工作,所以似乎问题出现在SecretClient类上。出于安全原因,我不希望使用环境变量,因为该脚本将在生产环境中运行。

我也可以使用Azure CLI列出我的机密。

我会很感激任何关于如何解决这个问题的想法和建议。

  1. <details>
  2. <summary>英文:</summary>
  3. I am trying to write a python script to read secrets from Azure Key Vault. I am facing an issue with authentication when using ***SecretClient*** class.
  4. My code is the below:

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

CREDENTIAL = DefaultAzureCredential()
client = SecretClient(
vault_url="https://my_vault_name.vault.azure.net/",
credential=CREDENTIAL
)

secret = client.get_secret('my_secret_name')

  1. The error I am getting is as follows:
  2. `EnvironmentCredential: EnvironmentCredential authentication unavailable. Environment variables are not fully configured. Visit https://aka.ms/azsdk/python/identity/environmentcredential/troubleshoot to troubleshoot.this issue. `
  3. `ManagedIdentityCredential: ManagedIdentityCredential authentication unavailable, no response from the IMDS endpoint. `
  4. `SharedTokenCacheCredential: The current credential is not configured to acquire tokens for tenant 74******-****-****-****-**********62. To enable acquiring tokens for this tenant add it to the additionally_allowed_tenants when creating the credential, or add &quot;*&quot; to additionally_allowed_tenants to allow acquiring tokens for any tenant.`
  5. I created Managed Identity in Azure Portal and &#39;assigned it&#39; to my Key Vault with all possible permissions.
  6. I&#39;ve tried the below as well:

CREDENTIAL = azure.identity.ManagedIdentityCredential(managed_identity_client_id='my_managed_identity_client_id')

  1. and

CREDENTIAL = ManagedIdentityCredential()

  1. but I&#39;m getting the same *ManagedIdentityCredential* error as above.
  2. Please note that I am trying to run the code on my local machine. What&#39;s more, I&#39;ve tried using *DefaultAzureCredential()* class for scripts to upload a file to my blob or list all my resources and it works ok so it&#39;s seems like there is an issue with the *SecretClient* class specifically. I do not want to use environmental variables for security reasons as the script will be ran in prod environment.
  3. I am also able to list my secrets using Azure CLI.
  4. I would appreciate any ideas and tips on how to tackle this issue.
  5. </details>
  6. # 答案1
  7. **得分**: 1
  8. 我决定更专注于以下错误消息:
  9. &gt; *SharedTokenCacheCredential:当前凭据未配置为获取租户74******-****-****-****-**********62的令牌。要为此租户启用获取令牌,请在创建凭据时将其添加到additionally_allowed_tenants,或者将“*”添加到additionally_allowed_tenants以允许获取任何租户的令牌。*
  10. 我添加了一个参数如下:
  11. CREDENTIAL = DefaultAzureCredential(additionally_allowed_tenants=['*'])
  12. 它运行得很好。我在重新运行代码之前还使用了Azure CLI进行了日志记录,这可能也产生了影响。
  13. 感谢大家的评论/答案。
  14. <details>
  15. <summary>英文:</summary>
  16. I decided to focus more on the following error msg:
  17. &gt; *SharedTokenCacheCredential: The current credential is not configured to acquire tokens for tenant 74******-****-****-****-**********62. To
  18. &gt; enable acquiring tokens for this tenant add it to the
  19. &gt; additionally_allowed_tenants when creating the credential, or add &quot;*&quot;
  20. &gt; to additionally_allowed_tenants to allow acquiring tokens for any
  21. &gt; tenant.*
  22. I have added a parameter as below:
  23. CREDENTIAL = DefaultAzureCredential(additionally_allowed_tenants=[&#39;*&#39;])
  24. and it worked fine. I have also logged using Azure CLI before re-running the code so it might have had an impact as well.
  25. Thank you all for the comments/answers.
  26. </details>

huangapple
  • 本文由 发表于 2023年3月9日 17:06:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682429.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定