How to solve azure keyvault secrets (Unauthorized) AKV10032: Invalid issuer. error in Python

Viewed 8840

I am using the azure-keyvault-secrets package to manage my resources secrets in Python 3.8, developping in PyCharm.

But when I am running the following:

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

VAULT_URL = os.environ["VAULT_URL"]

credential = DefaultAzureCredential()
client = SecretClient(
    vault_url=VAULT_URL,
    credential=credential
)
client.set_secret('my-secret-name', 'my-secret-value')

I get the following error:

HttpResponseError: azure keyvault secrets (Unauthorized) AKV10032: Invalid issuer. error

I have set the environment variables correct, according to the Microsoft Docs. I also restarted the runtime environment in PyCharm multiple times.

What to do?

1 Answers

I also faced the same issue. Following solution worked for me:

  1. Login into Azure portal and check how many subscriptions you have. Check under which subscription/ Resource Group the Key Vault is under.

  2. Login into Azure CLI and execute the following command:

az account list --output table
  1. Make the subscription which has the KeyVault as the default one:
az account set --subscription "subscription name"
  1. Re execute the code :
import os
from azure.keyvault.secrets import SecretClient
from azure.identity import DefaultAzureCredential

VAULT_URL = os.environ["VAULT_URL"]

credential = DefaultAzureCredential()
client = SecretClient(
    vault_url=VAULT_URL,
    credential=credential
)
client.set_secret('my-secret-name', 'my-secret-value')

It should work.

Related