-1

I see lot of documentations which retires the default keyvault. But lets say I have a key vault names, sample_kv, how do i retrieve that in my code?

I couldn't find any sources helpful, please let me know any

#to get deafult keyvault,i used this, but i want to retrieve keyvault by its name

keyvault = ws.get_default_keyvault()

error

DefaultAzureCredential failed to retrieve a token from the included credentials.Attempted credentials:EnvironmentCredential: EnvironmentCredential authentication unavailable. Environment variables are not fully configured.
userrrr
  • 9
  • 4

1 Answers1

0

You can retrieve a key vault by its name using the Azure Key Vault Secrets client library:

First install azure-keyvault-secrets and azure-identity.

Then you can retrieves a secret stored in the Key Vault using this code:

from azure.identity import DefaultAzureCredential 
from azure.keyvault.secrets import SecretClient 
credential = DefaultAzureCredential() 
keyvault_name = "sample_kv" 
secret_name = "my_secret" 
secret_client = SecretClient(vault_url=f"https://{keyvault_name}.vault.azure.net/", credential=credential) 
secret_value = secret_client.get_secret(secret_name).value

Make sure you have the configuring your environment for the DefaultAzureCredential for authentication.

For more information you can refer to Azure Key Vault Secrets client library documentation.

RishabhM
  • 525
  • 1
  • 5
  • 1
    Hi rishab, i'm getting this error if i use DefaultAzureCredential DefaultAzureCredential failed to retrieve a token from the included credentials. Attempted credentials: EnvironmentCredential: EnvironmentCredential authentication unavailable. Environment variables are not fully configured. i'm new to this, can you help me out ? – userrrr Jun 13 '23 at 14:15
  • Please check this Troubleshooting guide [link](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/identity/azure-identity/TROUBLESHOOTING.md#troubleshoot-environmentcredential-authentication-issues) for Default Azure credential errors. – RishabhM Jun 14 '23 at 04:17
  • found the answer - for futher guys looking for it in this [stack overflow link](https://stackoverflow.com/questions/72258087/unexpected-keyword-argument-tenant-id-while-accessing-azure-key-vault-in-pytho) azure identity version must be >=1.8 – userrrr Jun 14 '23 at 09:53