Reading Azure Key Vault secrets in Azure Synapse notebook using Python SDK

heta desai 247 Reputation points
2022-10-18T21:20:28.087+00:00

I have created python package which read azure key vault secrets. The wheel package is created and imported into synapse spark pool. when I tried to add package in synapse notebook from wheel package it throws an error :

ModuleNotFoundError: No module named 'azure.keyvault'

here is the python module that reads secrets from Azure key vault and wheel package is imported into apache spark pool

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

KEYVAULT_NAME = "lll-kv-scus"

KEYVAULT_URI = f"https://{KEYVAULT_NAME}.vault.azure.net"

credential = DefaultAzureCredential(exclude_shared_token_cache_credential=True)

client = SecretClient(vault_url=KEYVAULT_URI, credential=credential)

Server = client.get_secret("erverURL").value
User = client.get_secret("sql-user").value
Password = client.get_secret("sql-password").value
Database = client.get_secret("sql-databasename").value

A method is defined which returns key vault secrets. In synapse notebook tried to import above package to get secrets but it is unable to import SecretClient package.

Azure Key Vault
Azure Key Vault
An Azure service that is used to manage and protect cryptographic keys and other secrets used by cloud apps and services.
1,194 questions
Azure Synapse Analytics
Azure Synapse Analytics
An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.
4,696 questions
{count} votes

1 answer

Sort by: Most helpful
  1. PRADEEPCHEEKATLA-MSFT 85,746 Reputation points Microsoft Employee
    2022-10-19T07:36:59.36+00:00

    Hello @Anonymous ,

    Thanks for the question and using MS Q&A platform.

    As per the error message: ModuleNotFoundError: No module named 'azure.keyvault' - which clearly says Azure Key Vault Secrets modules has not installed on the Apache Spark pool.

    Before installing the Azure Key Vault Module:

    251875-image.png

    Here is the requirements.txt file contains:

    azure-keyvault==4.2.0  
    azure-keyvault-secrets==4.6.0  
    

    To resolve this issue you need to install Azure Key Vault module on the Apache Spark pool as shown below:

    251839-synapse-kvmodule.gif

    After installing the Azure Key Vault Module:

    251931-image.png

    Hope this will help. Please let us know if any further queries.

    ------------------------------

    • Please don't forget to click on 130616-image.png or upvote 130671-image.png button whenever the information provided helps you. Original posters help the community find answers faster by identifying the correct answer. Here is how
    • Want a reminder to come back and check responses? Here is how to subscribe to a notification
    • If you are interested in joining the VM program and help shape the future of Q&A: Here is jhow you can be part of Q&A Volunteer Moderators
    0 comments No comments