@Lotus88
Instead of relying on Linked Service v1.0, the production-grade approach is to securely connect to Azure SQL by generating an access token using ManagedIdentityCredential or AzureCliCredential, and then using that token in your JDBC connection. This method aligns with long-term security best practices, avoids hardcoding credentials, and ensures compatibility even if Linked Service v1.0 is deprecated in the future.
Using Azure Managed Identity with token-based JDBC authentication is a secure and future-proof approach that aligns with Microsoft’s best practices. It eliminates the need for hardcoded secrets and avoids reliance on deprecated linked service versions, ensuring better security and maintainability for enterprise-grade data solutions.
Prerequisites:
- Enable System-Assigned Managed Identity for your Synapse workspace.
- Grant that identity access to your Azure SQL DB:
CREATE USER [<workspace-managed-identity-name>] FROM EXTERNAL PROVIDER;
ALTER ROLE db_datareader ADD MEMBER [<workspace-managed-identity-name>];
- Ensure your SQL firewall allows the Managed VNet
from azure.identity import ManagedIdentityCredential
# Step 1: Get access token for Azure SQL
credential = ManagedIdentityCredential()
token = credential.get_token("https://database.windows.net/").token
# Step 2: Build JDBC connection
jdbc_url = "jdbc:sqlserver://<your-server>.database.windows.net:1433;" \
"database=<your-database>;encrypt=true;" \
"trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;"
# Step 3: Use token in Spark read
df = spark.read \
.format("jdbc") \
.option("url", jdbc_url) \
.option("accessToken", token) \
.option("dbtable", "<your-table-name>") \
.option("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver") \
.load()
df.show()
Use ManagedIdentityCredential() to fetch a secure token, and connect to Azure SQL via JDBC using that token. This is secure, supported, and avoids reliance on Linked Service v1.0 or TokenLibrary.