Skip to content

Commit 9d241d5

Browse files
author
Jill Grant
authored
Merge pull request #237756 from xfz11/xf/passwordless
Add a passwordless tutorial in service connector
2 parents d2c7e95 + 54034ff commit 9d241d5

File tree

8 files changed

+655
-0
lines changed

8 files changed

+655
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
---
2+
author: xiaofanzhou
3+
ms.service: service-connector
4+
ms.topic: include
5+
ms.date: 07/17/2023
6+
ms.author: xiaofanzhou
7+
---
8+
9+
10+
### [Java](#tab/java)
11+
12+
1. Add the following dependencies in your *pom.xml* file:
13+
14+
```xml
15+
<dependency>
16+
<groupId>mysql</groupId>
17+
<artifactId>mysql-connector-java</artifactId>
18+
<version>8.0.30</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>com.azure</groupId>
22+
<artifactId>azure-identity-extensions</artifactId>
23+
<version>1.1.5</version>
24+
</dependency>
25+
```
26+
27+
28+
1. Get the connection string from the environment variable, and add the plugin name to connect to the database:
29+
30+
```java
31+
String url = System.getenv("AZURE_MYSQL_CONNECTIONSTRING");
32+
String pluginName = "com.azure.identity.extensions.jdbc.mysql.AzureMysqlAuthenticationPlugin";
33+
Connection connection = DriverManager.getConnection(url + "&defaultAuthenticationPlugin=" +
34+
pluginName + "&authenticationPlugins=" + pluginName);
35+
```
36+
37+
For more information, see [Use Java and JDBC with Azure Database for MySQL - Flexible Server](../../mysql/flexible-server/connect-java.md?tabs=passwordless).
38+
39+
### [Spring](#tab/spring)
40+
41+
For a Spring application, if you create a connection with option `--client-type springboot`, Service Connector will set the properties `spring.datasource.azure.passwordless-enabled`, `spring.datasource.url`, and `spring.datasource.username` to Azure Spring Apps.
42+
43+
Update your application following the tutorial [Connect an Azure Database for MySQL instance to your application in Azure Spring Apps](../../spring-apps/how-to-bind-mysql.md#prepare-your-java-project). Remember to remove the `spring.datasource.password` configuration property if it was set before and add the correct dependencies to your Spring application.
44+
45+
For more tutorials, see [Use Spring Data JDBC with Azure Database for MySQL](/azure/developer/java/spring-framework/configure-spring-data-jdbc-with-azure-mysql?tabs=passwordless%2Cservice-connector&pivots=mysql-passwordless-flexible-server#store-data-from-azure-database-for-mysql)
46+
47+
### [.NET](#tab/dotnet)
48+
49+
For .NET, there's not a plugin or library to support passwordless connections. You can get an access token for the managed identity or service principal and use it as the password to connect to the database. For example, you can use [Azure.Identity](https://www.nuget.org/packages/Azure.Identity/) to get an access token for the managed identity or service principal:
50+
51+
```csharp
52+
using Azure.Core;
53+
using Azure.Identity;
54+
using MySqlConnector;
55+
56+
// user-assigned managed identity
57+
var credential = new DefaultAzureCredential(
58+
new DefaultAzureCredentialOptions
59+
{
60+
ManagedIdentityClientId = userAssignedClientId
61+
});
62+
63+
// system-assigned managed identity
64+
//var credential = new DefaultAzureCredential();
65+
66+
// service principal
67+
//var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
68+
69+
var tokenRequestContext = new TokenRequestContext(
70+
new[] { "https://ossrdbms-aad.database.windows.net/.default" });
71+
AccessToken accessToken = await credential.GetTokenAsync(tokenRequestContext);
72+
// Open a connection to the MySQL server using the access token.
73+
string connectionString =
74+
$"{Environment.GetEnvironmentVariable("AZURE_MYSQL_CONNECTIONSTRING")};Password={accessToken.Token}";
75+
76+
using var connection = new MySqlConnection(connectionString);
77+
Console.WriteLine("Opening connection using access token...");
78+
await connection.OpenAsync();
79+
80+
// do something
81+
```
82+
83+
84+
### [Others](#tab/others)
85+
86+
For other languages, you can use the connection string and username that Service Connector set to the environment variables to connect the database. For environment variable details, see [Integrate Azure Database for MySQL with Service Connector](../how-to-integrate-mysql.md).
87+
88+
For more code samples, see [Connect to Azure databases from App Service without secrets using a managed identity](/azure/app-service/tutorial-connect-msi-azure-database?tabs=mysql#3-modify-your-code).
89+
90+
---
91+
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
---
2+
author: xiaofanzhou
3+
ms.service: service-connector
4+
ms.topic: include
5+
ms.date: 07/17/2023
6+
ms.author: xiaofanzhou
7+
---
8+
9+
10+
11+
### [Java](#tab/java)
12+
13+
1. Add the following dependencies in your *pom.xml* file:
14+
15+
```xml
16+
<dependency>
17+
<groupId>org.postgresql</groupId>
18+
<artifactId>postgresql</artifactId>
19+
<version>42.3.6</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>com.azure</groupId>
23+
<artifactId>azure-identity-extensions</artifactId>
24+
<version>1.1.5</version>
25+
</dependency>
26+
```
27+
28+
29+
1. Get the connection string from environment variables and add the plugin name to connect to the database:
30+
31+
```java
32+
import java.sql.*;
33+
34+
String url = System.getenv("AZURE_POSTGRESQL_CONNECTIONSTRING");
35+
String pluginName = "com.Azure.identity.extensions.jdbc.postgresql.AzurePostgresqlAuthenticationPlugin";
36+
Connection connection = DriverManager.getConnection(url + "&authenticationPluginClassName=" + pluginName);
37+
```
38+
39+
For more information, see the following resources:
40+
41+
* [Tutorial: Connect to PostgreSQL Database from a Java Quarkus Container App without secrets using a managed identity](../../container-apps/tutorial-java-quarkus-connect-managed-identity-postgresql-database.md)
42+
* [Tutorial: Connect to a PostgreSQL Database from Java Tomcat App Service without secrets using a managed identity](../../app-service/tutorial-java-tomcat-connect-managed-identity-postgresql-database.md)
43+
* [Quickstart: Use Java and JDBC with Azure Database for PostgreSQL Flexible Server](../../postgresql/flexible-server/connect-java.md?tabs=passwordless#connect-to-the-database)
44+
45+
### [Spring](#tab/spring)
46+
47+
For a Spring application, if you create a connection with option `--client-type springboot`, Service Connector will set the properties `spring.datasource.azure.passwordless-enabled`, `spring.datasource.url`, and `spring.datasource.username` to Azure Spring Apps.
48+
49+
Update your application following the tutorial [Bind an Azure Database for PostgreSQL to your application in Azure Spring Apps](../../spring-apps/how-to-bind-postgres.md#prepare-your-java-project). Remember to remove the `spring.datasource.password` configuration property if it was set before and add the correct dependencies,
50+
51+
For more tutorials, see [Use Spring Data JDBC with Azure Database for PostgreSQL](/azure/developer/java/spring-framework/configure-spring-data-jdbc-with-azure-postgresql?tabs=passwordless%2Cservice-connector&pivots=postgresql-passwordless-flexible-server#store-data-from-azure-database-for-postgresql)
52+
53+
### [.NET](#tab/dotnet)
54+
55+
For .NET, there's not a plugin or library for passwordless connections. You can get an access token for the managed identity or service principal and use it as the password to connect to the database. For example, you can use [Azure.Identity](https://www.nuget.org/packages/Azure.Identity/) to get an access token for the managed identity or service principal:
56+
57+
```csharp
58+
using Npgsql;
59+
using Azure.Identity;
60+
using Azure.Core;
61+
62+
// user-assigned managed identity
63+
var sqlServerTokenProvider = new DefaultAzureCredential(
64+
new DefaultAzureCredentialOptions
65+
{
66+
ManagedIdentityClientId = userAssignedClientId
67+
});
68+
69+
// system-assigned managed identity
70+
//var sqlServerTokenProvider = new DefaultAzureCredential();
71+
72+
// service principal: tenantId, clientId, clientSecret can be retrieved from environment variables
73+
//var sqlServerTokenProvider = new ClientSecretCredential(tenantId, clientId, clientSecret);
74+
75+
AccessToken accessToken = await sqlServerTokenProvider.GetTokenAsync(
76+
new TokenRequestContext(scopes: new string[]
77+
{
78+
"https://ossrdbms-aad.database.windows.net/.default"
79+
}));
80+
string connectionString =
81+
$"{Environment.GetEnvironmentVariable("AZURE_POSTGRESQL_CONNECTIONSTRING")};Password={accessToken.Token}";
82+
83+
using (var connection = new NpgsqlConnection(connectionString))
84+
{
85+
Console.WriteLine("Opening connection using access token...");
86+
connection.Open();
87+
using var command = new NpgsqlCommand("SELECT version()", connection);
88+
using NpgsqlDataReader reader = await command.ExecuteReaderAsync();
89+
90+
while (reader.Read())
91+
{
92+
Console.WriteLine("\nConnected!\n\nPostgreSQL version: {0}", reader.GetString(0));
93+
}
94+
}
95+
```
96+
97+
### [Others](#tab/others)
98+
99+
For other languages, you can use the connection string and username that Service Connector set to the environment variables to connect the database. For environment variable details, see [Integrate Azure Database for PostgreSQL with Service Connector](../how-to-integrate-postgres.md).
100+
101+
For more code samples, see [Connect to Azure databases from App Service without secrets using a managed identity](/azure/app-service/tutorial-connect-msi-azure-database?tabs=postgresql#3-modify-your-code).
102+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
author: xiaofanzhou
3+
ms.service: service-connector
4+
ms.topic: include
5+
ms.date: 07/17/2023
6+
ms.author: xiaofanzhou
7+
---
8+
9+
10+
### [Java](#tab/java)
11+
12+
For managed identity authentication, see [Connect using Azure Active Directory authentication](/sql/connect/jdbc/connecting-using-azure-active-directory-authentication).
13+
14+
```java
15+
import java.sql.Connection;
16+
import java.sql.ResultSet;
17+
import java.sql.Statement;
18+
19+
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
20+
21+
public class Main {
22+
public static void main(String[] args) {
23+
// AZURE_SQL_CONNECTIONSTRING should be one of the following:
24+
// For system-assigned managed identity: "jdbc:sqlserver://{SQLName}.database.windows.net:1433;databaseName={SQLDbName};authentication=ActiveDirectoryMSI;"
25+
// For user-assigned managed identity: "jdbc:sqlserver://{SQLName}.database.windows.net:1433;databaseName={SQLDbName};msiClientId={UserAssignedMiClientId};authentication=ActiveDirectoryMSI;"
26+
// For service principal: "jdbc:sqlserver://{SQLName}.database.windows.net:1433;databaseName={SQLDbName};user={ServicePrincipalClientId};password={spSecret};authentication=ActiveDirectoryServicePrincipal;"
27+
String connectionString = System.getenv("AZURE_SQL_CONNECTIONSTRING");
28+
SQLServerDataSource ds = new SQLServerDataSource();
29+
ds.setURL(connectionString);
30+
try (Connection connection = ds.getConnection()) {
31+
System.out.println("Connected successfully.");
32+
} catch (SQLException e) {
33+
e.printStackTrace();
34+
}
35+
}
36+
}
37+
```
38+
39+
### [Spring](#tab/spring)
40+
41+
For a Spring application, if you create a connection with option `--client-type springboot`, Service Connector will set the properties `spring.datasource.url` with value format `jdbc:sqlserver://<sql-server>.database.windows.net:1433;databaseName=<sql-db>;authentication=ActiveDirectoryMSI;` to Azure Spring Apps.
42+
43+
Update your application following the tutorial [Migrate a Java application to use passwordless connections with Azure SQL Database](/azure/developer/java/spring-framework/migrate-sql-database-to-passwordless-connection?tabs=spring%2Capp-service%2Cassign-role-service-connector#2-migrate-the-app-code-to-use-passwordless-connections). Remember to remove the `spring.datasource.password` configuration property if it was set before and add the correct dependencies.
44+
45+
46+
### [.NET](#tab/dotnet)
47+
48+
For managed identity authentication, see [Using Active Directory Managed Identity authentication](/sql/connect/ado-net/sql/azure-active-directory-authentication#using-active-directory-managed-identity-authentication).
49+
50+
```csharp
51+
using Microsoft.Data.SqlClient;
52+
53+
// The connection string should've been set in environment variable AZURE_SQL_CONNECTIONSTRING by Service Connector.
54+
string connectionString =
55+
Environment.GetEnvironmentVariable("AZURE_SQL_CONNECTIONSTRING")!;
56+
57+
using var connection = new SqlConnection(connectionString);
58+
connection.Open();
59+
```
60+
61+
### [Others](#tab/others)
62+
63+
For other languages, you can use the connection string and username that Service Connector set to the environment variables to connect to the database. For environment variable details, see [Integrate Azure SQL Database with Service Connector](../how-to-integrate-sql-database.md).
64+
65+
For more code samples, see [Connect to Azure databases from App Service without secrets using a managed identity](/azure/app-service/tutorial-connect-msi-azure-database?tabs=sqldatabase#3-modify-your-code).
66+
67+
---
68+
69+
For more information, see [Homepage for client programming to Microsoft SQL Server](/sql/connect/homepage-sql-connection-programming).
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
author: xiaofanzhou
3+
ms.service: service-connector
4+
ms.topic: include
5+
ms.date: 05/21/2023
6+
ms.author: xiaofanzhou
7+
---
8+
9+
Install the Service Connector passwordless extension for the Azure CLI:
10+
11+
```azurecli
12+
az extension add --name serviceconnector-passwordless --upgrade
13+
```

articles/service-connector/index.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ landingContent:
7070
url: tutorial-portal-key-vault.md
7171
- text: Web app with App Configuration
7272
url: tutorial-connect-web-app-app-configuration.md
73+
- text: Passwordless connection to database
74+
url: tutorial-passwordless.md
7375

7476
- linkListType: how-to-guide
7577
links:

articles/service-connector/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ items:
8181
href: tutorial-portal-key-vault.md
8282
- name: Connect a container app to Blob Storage
8383
href: ../container-apps/service-connector.md?bc=%2fazure%2fservice-connector%2fbreadcrumb%2ftoc.json&toc=%2fazure%2fservice-connector%2fTOC.json
84+
- name: Create passwordless connection to database
85+
href: tutorial-passwordless.md
8486
- name: Get connection configurations
8587
href: how-to-get-configurations.md
8688
- name: Troubleshoot

0 commit comments

Comments
 (0)