Skip to content

Commit b2b154b

Browse files
committed
Add more languages to Service Bus passwordless migration guide
1 parent 2d91a04 commit b2b154b

File tree

1 file changed

+133
-39
lines changed

1 file changed

+133
-39
lines changed

articles/service-bus-messaging/service-bus-migrate-azure-credentials.md

Lines changed: 133 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ description: Learn to migrate existing Service Bus applications away from connec
55
author: alexwolfmsft
66
ms.author: alexwolf
77
ms.reviewer: randolphwest
8-
ms.date: 12/07/2022
8+
ms.date: 04/12/2023
99
ms.service: service-bus
1010
ms.topic: how-to
11-
ms.custom: devx-track-csharp, passwordless-dotnet, devx-track-azurecli, devx-track-azurepowershell
12-
ms.devlang: csharp
11+
ms.custom: |
12+
- devx-track-csharp
13+
- devx-track-azurecli
14+
- devx-track-azurepowershell
15+
- passwordless-dotnet
16+
- passwordless-java
17+
- passwordless-js
18+
- passwordless-python
1319
---
1420

1521
# Migrate an application to use passwordless connections with Azure Service Bus
@@ -20,24 +26,44 @@ Application requests to Azure Service Bus must be authenticated using either acc
2026

2127
The following code example demonstrates how to connect to Azure Service Bus using a connection string that includes an access key. When you create a Service Bus, Azure generates these keys and connection strings automatically. Many developers gravitate towards this solution because it feels familiar to options they've worked with in the past. If your application currently uses connection strings, consider migrating to passwordless connections using the steps described in this document.
2228

23-
# [C#](#tab/csharp)
29+
## [.NET](#tab/dotnet)
2430

2531
```csharp
26-
var serviceBusClient = new ServiceBusClient(
27-
"<NAMESPACE-CONNECTION-STRING>",
28-
clientOptions);
32+
await using var client = new ServiceBusClient("<NAMESPACE-CONNECTION-STRING>");
2933
```
3034

31-
# [Java - JMS](#tab/java-jms)
35+
## [Java](#tab/java)
3236

3337
```java
34-
ServiceBusJmsConnectionFactorySettings connFactorySettings = new ServiceBusJmsConnectionFactorySettings();
35-
connFactorySettings.setConnectionIdleTimeoutMS(20000);
38+
ServiceBusSenderClient client = new ServiceBusClientBuilder()
39+
.connectionString("<NAMESPACE-CONNECTION-STRING>")
40+
.sender()
41+
.queueName("<QUEUE-NAME>")
42+
.buildClient();
43+
```
44+
45+
## [Java - JMS](#tab/java-jms)
3646

47+
```java
3748
ConnectionFactory factory = new ServiceBusJmsConnectionFactory(
3849
"<NAMESPACE-CONNECTION-STRING>",
3950
new ServiceBusJmsConnectionFactorySettings());
4051
```
52+
53+
## [Node.js](#tab/nodejs)
54+
55+
```nodejs
56+
const client = new ServiceBusClient("<NAMESPACE-CONNECTION-STRING>");
57+
```
58+
59+
## [Python](#tab/python)
60+
61+
```python
62+
client = ServiceBusClient(
63+
fully_qualified_namespace = "<NAMESPACE-CONNECTION-STRING>"
64+
)
65+
```
66+
4167
---
4268

4369
Connection strings should be used with caution. Developers must be diligent to never expose the keys in an unsecure location. Anyone who gains access to the key is able to authenticate. For example, if an account key is accidentally checked into source control, sent through an unsecure email, pasted into the wrong chat, or viewed by someone who shouldn't have permission, there's risk of a malicious user accessing the application. Instead, consider updating your application to use passwordless connections.
@@ -60,42 +86,60 @@ For local development, make sure you're authenticated with the same Azure AD acc
6086

6187
[!INCLUDE [default-azure-credential-sign-in](../../includes/passwordless/default-azure-credential-sign-in.md)]
6288

63-
Next you'll need to update your code to use passwordless connections.
89+
Next, update your code to use passwordless connections.
6490

65-
# [C#](#tab/csharp)
91+
## [.NET](#tab/dotnet)
6692

67-
1. To use `DefaultAzureCredential` in a .NET application, add the **Azure.Identity** NuGet package to your application.
93+
1. To use `DefaultAzureCredential` in a .NET application, install the `Azure.Identity` package:
6894

6995
```dotnetcli
7096
dotnet add package Azure.Identity
7197
```
7298

73-
1. At the top of your `Program.cs` file, add the following `using` statement:
99+
1. At the top of your file, add the following code:
74100

75101
```csharp
76102
using Azure.Identity;
77103
```
78104

79-
1. Identify the locations in your code that currently create a `ServiceBusClient` to connect to Azure Service Bus. This task is often handled in `Program.cs`, potentially as part of your service registration with the .NET dependency injection container. Update your code to match the following example:
105+
1. Identify the locations in your code that create a `ServiceBusClient` to connect to Azure Service Bus. Update your code to match the following example:
80106

81107
```csharp
82-
var clientOptions = new ServiceBusClientOptions
83-
{
84-
TransportType = ServiceBusTransportType.AmqpWebSockets
85-
};
86-
87-
//TODO: Replace the "<SERVICE-BUS-NAMESPACE-NAME>" placeholder.
88-
client = new ServiceBusClient(
108+
//TODO: Replace the <SERVICE-BUS-NAMESPACE-NAME> placeholder.
109+
var client = new ServiceBusClient(
89110
"<SERVICE-BUS-NAMESPACE-NAME>.servicebus.windows.net",
90-
new DefaultAzureCredential(),
91-
clientOptions);
111+
new DefaultAzureCredential());
92112
```
93113

94-
1. Make sure to update the Service Bus namespace in the URI of your `ServiceBusClient`. You can find the namespace on the overview page of the Azure portal.
114+
## [Java](#tab/java)
115+
116+
1. To use `DefaultAzureCredential` in a Java application, install the `azure-identity` package via one of the following approaches:
117+
1. [Include the BOM file](/java/api/overview/azure/identity-readme?view=azure-java-stable&preserve-view=true#include-the-bom-file).
118+
1. [Include a direct dependency](/java/api/overview/azure/identity-readme?view=azure-java-stable&preserve-view=true#include-direct-dependency).
119+
120+
1. At the top of your file, add the following code:
121+
122+
```java
123+
import com.azure.identity.DefaultAzureCredentialBuilder;
124+
```
125+
126+
1. Identify the locations in your code that create a `ServiceBusSenderClient` or `ServiceBusSenderAsyncClient` object to connect to Azure Service Bus. Update your code to match the following example:
127+
128+
```java
129+
DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
130+
.build();
131+
132+
// TODO: Update the <NAMESPACE-CONNECTION-STRING> placeholder.
133+
ServiceBusSenderClient client = new ServiceBusClientBuilder()
134+
.credential("<NAMESPACE-CONNECTION-STRING>", credential)
135+
.sender()
136+
.queueName("<QUEUE-NAME>")
137+
.buildClient();
138+
```
95139

96-
# [Java - JMS](#tab/java-jms)
140+
## [Java - JMS](#tab/java-jms)
97141

98-
1. To use `DefaultAzureCredential` in a JMS application, add at least version **1.0.0** of the **azure-servicebus-jms** package to your application.
142+
1. To use `DefaultAzureCredential` in a JMS application, add at least version 1.0.0 of the `azure-servicebus-jms` package to your application:
99143

100144
```xml
101145
<dependency>
@@ -105,27 +149,77 @@ Next you'll need to update your code to use passwordless connections.
105149
</dependency>
106150
```
107151

108-
2. At the top of your file, add the following `import` statements:
152+
1. At the top of your file, add the following code:
109153

110154
```java
111155
import com.azure.core.credential.TokenCredential;
112156
import com.azure.identity.DefaultAzureCredentialBuilder;
113157
```
114158

115-
3. Identify the locations in your code that currently create a `ServiceBusJmsConnectionFactory` to connect to Azure Service Bus. Update your code to match the following example:
159+
1. Identify the locations in your code that currently create a `ServiceBusJmsConnectionFactory` to connect to Azure Service Bus. Update your code to match the following example:
116160

117-
```java
118-
TokenCredential tokenCredential = new DefaultAzureCredentialBuilder()
119-
.build();
161+
```java
162+
DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
163+
.build();
120164

121-
ServiceBusJmsConnectionFactorySettings connFactorySettings = new ServiceBusJmsConnectionFactorySettings();
122-
connFactorySettings.setConnectionIdleTimeoutMS(20000);
123-
124-
//TODO: Replace the "<SERVICE-BUS-NAMESPACE-NAME>" placeholder.
125-
ConnectionFactory factory = new ServiceBusJmsConnectionFactory(tokenCredential, "<SERVICE-BUS-NAMESPACE-NAME>.servicebus.windows.net", connFactorySettings);
165+
//TODO: Replace the "<NAMESPACE-CONNECTION-STRING>" placeholder.
166+
ConnectionFactory factory = new ServiceBusJmsConnectionFactory(
167+
credential,
168+
"<NAMESPACE-CONNECTION-STRING>",
169+
new ServiceBusJmsConnectionFactorySettings());
126170
```
127171

128-
4. Make sure to update the Service Bus namespace in the URI of your `ServiceBusJmsConnectionFactory`. You can find the namespace on the overview page of the Azure portal.
172+
## [Node.js](#tab/nodejs)
173+
174+
1. To use `DefaultAzureCredential` in a Node.js application, install the `@azure/identity` package:
175+
176+
```bash
177+
npm install --save @azure/identity
178+
```
179+
180+
1. At the top of your file, add the following code:
181+
182+
```nodejs
183+
const { DefaultAzureCredential } = require("@azure/identity");
184+
```
185+
186+
1. Identify the locations in your code that create a `ServiceBusClient` object to connect to Azure Service Bus. Update your code to match the following example:
187+
188+
```nodejs
189+
const credential = new DefaultAzureCredential();
190+
191+
// TODO: Update the <NAMESPACE-CONNECTION-STRING> placeholder.
192+
const client = new ServiceBusClient(
193+
"<NAMESPACE-CONNECTION-STRING>",
194+
credential
195+
);
196+
```
197+
198+
## [Python](#tab/python)
199+
200+
1. To use `DefaultAzureCredential` in a Python application, install the `azure-identity` package:
201+
202+
```bash
203+
pip install azure-identity
204+
```
205+
206+
1. At the top of your file, add the following code:
207+
208+
```python
209+
from azure.identity import DefaultAzureCredential
210+
```
211+
212+
1. Identify the locations in your code that create a `ServiceBusClient` to connect to Azure Service Bus. Update your code to match the following example:
213+
214+
```python
215+
credential = DefaultAzureCredential()
216+
217+
# TODO: Update the <NAMESPACE-CONNECTION-STRING> placeholder.
218+
client = ServiceBusClient(
219+
fully_qualified_namespace = "<NAMESPACE-CONNECTION-STRING>",
220+
credential = credential
221+
)
222+
```
129223
130224
---
131225
@@ -230,7 +324,7 @@ You can assign a managed identity to an Azure Kubernetes Service (AKS) instance
230324
```azurecli
231325
az aks update \
232326
--resource-group <resource-group-name> \
233-
--name <virtual-machine-name>
327+
--name <virtual-machine-name> \
234328
--enable-managed-identity
235329
```
236330

0 commit comments

Comments
 (0)