DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Data Store Options for Operational Analytics/Data Engineering
  • Which Tool Is Better for Code Completion — Azure Data Studio or dbForge SQL Complete?
  • Microsoft Azure Backup Service
  • Amazon RDS vs Azure SQL — Know the Key Differentiators and Choose the Best

Trending

  • How Has AI Impacted Code Obfuscation?
  • Testing Asynchronous Systems, Part 1: Mapping the System and Classifying Its Asynchrony
  • Seven AI Adoption Failures and How to Overcome Them
  • Same Prompt, Different Test: Using AI Agent Workspaces to Make Testing Repeatable

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Databases
  4. Securely Access Azure SQL Database from Azure Synapse

Securely Access Azure SQL Database from Azure Synapse

At the time of writing, there is no linked service or AAD pass-through support with the Azure SQL connector via Azure Synapse Analytics.

By 
Abhishek Gupta
DZone Core CORE ·
Aug. 10, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
12.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Introduction

The Apache Spark connector for Azure SQL Database (and SQL Server) enables these databases to be used as input data sources and output data sinks for Apache Spark jobs. You can use the connector in Azure Synapse Analytics for big data analytics on real-time transactional data and to persist results for ad-hoc queries or reporting.

At the time of writing, there is no linked service or AAD pass-through support with the Azure SQL connector via Azure Synapse Analytics. But you can use other options such as Azure Active Directory authentication or via direct SQL authentication (username and password-based). A secure way of doing this is to store the Azure SQL Database credentials in Azure Key Vault (as Secret) — this is what’s covered in this short blog post.

Assuming you have an Azure Synapse Workspace and Azure SQL Database already created, all you need to is:
  1. Create an Azure Key Vault and add a Secret to store the Azure SQL Database connectivity info.
  2. Create a Linked Service for your Azure Key Vault in Azure Synapse Workspace.
  3. Provide appropriate permissions to Azure Synapse workspace managed service identity to Azure Key Vault

Process Walkthrough

Create an Azure Key Vault and add a Secret.

Create an Azure Key Vault and add a Secret.

I have stored the entire JDBC connection string in this case but you can choose to just store the password as well.

To retrieve secrets from Azure Key Vault, the recommended way is to create a Linked Service to your Azure Key Vault. Also, make sure that the Synapse workspace managed service identity (MSI) has Secret Get privileges on your Azure Key Vault. This will let Synapse authenticate to Azure Key Vault using the Synapse workspace managed service identity.

You can also authenticate using your user Azure Active Directory credential.

Create a Linked Service in Azure Synapse Workspace:

Create a Linked Service in Azure Synapse Workspace:

Grant appropriate access for Azure Synapse workspace service managed identity to your Azure Key Vault:

Grant appropriate access for Azure Synapse workspace service managed identity to your Azure Key Vault:

Choose Get permission on Secret:

Choose Get permission on Secret:

Search for the Synapse Workspace Managed Service Identity — it’s the same name as that of the workspace

Search for the Synapse Workspace Managed Service Identity

Add the policy:

Add the policy:
Azure Key Vault Access Policy configuration (Image by author)

Click Save to confirm:

Azure Key Vault Access Policy configuration (Image by author) Click Save to confirm:

How To Use

I will be using pyspark in Synapse Spark pools as an example.

Synapse uses Azure Active Directory (AAD) passthrough by default for authentication between resources. If you need to connect to a resource using other credentials, use the TokenLibrary directly — this simplifies the process of retrieving SAS tokens, AAD tokens, connection strings, and secrets stored in a linked service or from an Azure Key Vault.

To retrieve a secret stored from Azure Key Vault, use the TokenLibrary.getSecret() function. Here is a python example but the same applies to C# or Scala.

For example, to access data from SalesLT.Customer table (part of AdventureWorks sample database), you can use the following:

SQL
 
url = TokenLibrary.getSecret("<Azure Key Vault name>", "<Secret name>", "<Linked Service name>")dbtable = "SalesLT.Customer"customers = spark.read \
    .format("com.microsoft.sqlserver.jdbc.spark") \
    .option("url", url) \
    .option("dbtable", dbtable) \
    .load()print(customers.count())
customers.show(5)

That’s all there is to it!

azure Database sql

Published at DZone with permission of Abhishek Gupta. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Data Store Options for Operational Analytics/Data Engineering
  • Which Tool Is Better for Code Completion — Azure Data Studio or dbForge SQL Complete?
  • Microsoft Azure Backup Service
  • Amazon RDS vs Azure SQL — Know the Key Differentiators and Choose the Best

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook