0

Unable to get the values of tenant_id, client_id,client_secret which are stored in Azure Keyvault, by using library from azure.identity import ClientSecretCredential. I have installed and imported the modules azure-keyvault-secrets azure-identity but for some reason its not working. i don't want to use import DefaultCredentials. Below is my python script

from azure.identity import ClientSecretCredential
from azure.mgmt.compute import ComputeManagementClient
from azure.keyvault.secrets import SecretClient

credential = ClientSecretCredential(
    tenant_id='xxxxx',
    client_id='xxxxx',   ##the secret id's values should pick from Keyvault #
    client_secret='xxxxx'
)
subscription_id = "xxxxx"
compute_client = ComputeManagementClient(credential, subscription_id)
`````
Assist me to solve this
User
  • 31
  • 4
  • 1
    ClientSecretCredential expects you to give it the AAD tenant ID, an app registration client ID, and a client secret value that is added to the app registration. It does not fetch values from Key Vault. – juunas Mar 01 '23 at 14:36
  • 1
    yeah, i think you misunderstand how it works :) you need to provide those values and then you can use those credentials to access key vault to retrieve something. alternatively you can look at managed identities – 4c74356b41 Mar 01 '23 at 14:36

1 Answers1

0

I tried in my environment and got the below results:

I agree with @junnas and 4c74356b41 comments you misunderstand how it works :) you need to provide those values and then you can use those credentials to access the key vault to retrieve something

You need to use the Clientsecretcredential for authorization. you can use those credentials to retrieve the values.

Here is a sample of how it works:

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

credential = ClientSecretCredential(
    tenant_id='',#tenantid
    client_id='',#appid(clientid)   
    client_secret=''#app secret
)
client=SecretClient(vault_url="https://< Your vault name >.vault.azure.net/",credential=credential)
secret=client.get_secret("secret1")
print("The keyvault secret value:",secret.value)

Output: enter image description here

Reference: Quickstart – Azure Key Vault Python client library – manage secrets | Microsoft Learn

Venkatesan
  • 3,748
  • 1
  • 3
  • 15