Hi @manigandan
Thank you for posting you query!
As I understand that you are facing an issue while accessing Azure Key Vault secrets from a Synapse Spark job using PySpark, the error you're encountering indicates a problem with the authentication process. Issue occurs because DefaultAzureCredential()
is unable to authenticate, likely due to Managed Identity not being properly used
or missing permissions on Key Vault.
Here are the steps to troubleshoot and resolve this issue:
Verify Managed Identity Configuration - Ensure that Azure Synapse's Managed Identity has the necessary permissions to access the Key Vault. You mentioned that the managed identity has been given roles such as Key Vault Admin
, Key Vault Secret User
, and Key Vault Secret Officer
, which should generally suffice. Confirm that the Managed Identity is enabled for your Synapse workspace.
Use Managed Identity for Authentication - Since you are using Synapse, leveraging Managed Identity is usually the simplest and most secure way to authenticate. Ensure that your Synapse Spark environment is configured to allow this. You might need to explicitly specify the use of ManagedIdentityCredential
if DefaultAzureCredential
does not automatically detect it.
from azure.identity import ManagedIdentityCredential
credential = ManagedIdentityCredential()
Here’s a modified version of your code that explicitly uses ManagedIdentityCredential:
import os
from azure.keyvault.secrets import SecretClient
from azure.identity import ManagedIdentityCredential
# Get the Key Vault name from environment variables
keyVaultName = os.environ["KEY_VAULT_NAME"]
KVUri = f"https://{keyVaultName}.vault.azure.net"
# Use Managed Identity for authentication
credential = ManagedIdentityCredential()
client = SecretClient(vault_url=KVUri, credential=credential)
secretName = "my_secret_name"
print(f"Retrieving your secret from {keyVaultName}.")
try:
retrieved_secret = client.get_secret(secretName)
print(f"Your secret is '{retrieved_secret.value}'.")
except Exception as e:
print(f"An error occurred: {e}")
print(" done.")
Environment Variables Issue - The error mentions missing AZURE_CLIENT_ID
, AZURE_TENANT_ID
, and AZURE_CLIENT_SECRET
, but these are not needed for Managed Identity. Ensure KEY_VAULT_NAME
is correctly set in your Synapse Spark environment.
For more details, please refer to the following similar threads that may provide useful insights.
- Issue in accessing Azure Keyvault - DefaultAzureCredential failed to retrieve a token
- DefaultAzureCredential failed to retrieve a token from the included credentials
I hope this information helps. Please do let us know if you have any further queries.
If this answers your query, do click Accept Answer
and Yes
for was this answer helpful. And, if you have any further query do let us know.