Search code examples
azureazure-keyvaultazure-synapse

How can we fetch secrets from private keyvault in Synapse Notebooks


if we select "Allow public access from specific virtual networks and IP addresses" in Networking blade of keyvault. For Databricks notebooks we can fetch secrets by using secret scope, How do we do like that in synapse notebooks. Please help me with the appropriate solution.


Solution

  • There are two ways ,You can retrieve secret keys values in azure synapse.

    This is my Keyvault secret value:

    enter image description here

    Note: Make sure to check whether access policies permission given or not.

    Approach 1:

    Go to azure Synapse studio and follow this sample code.

    import sys
    from pyspark.sql import SparkSession
    sc = SparkSession.builder.getOrCreate()
    token_library = sc._jvm.com.microsoft.azure.synapse.tokenlibrary.TokenLibrary
    connection_string = token_library.getSecret("dem123","testSecret","AzureKeyVault1")
    print(connection_string)
    

    enter image description here

    Approach 2:

    As per the Ms_doc try this code:

    mssparkutils.credentials.getSecret('key_vault_name','secret_name')
    

    Or

    mssparkutils.credentials.getSecret('key_vault_name','secret_name','linked_service')
    

    For more information refer this SO thread