Skip to content

Commit 7236e16

Browse files
authored
Merge pull request #234307 from scottaddie/scottaddie/service-bus-migration
Add more languages to Service Bus passwordless migration guide
2 parents ddc429a + ffe43dd commit 7236e16

File tree

2 files changed

+168
-51
lines changed

2 files changed

+168
-51
lines changed

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

Lines changed: 167 additions & 50 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,57 @@ 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("<CONNECTION-STRING>");
2933
```
3034

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

33-
```java
34-
ServiceBusJmsConnectionFactorySettings connFactorySettings = new ServiceBusJmsConnectionFactorySettings();
35-
connFactorySettings.setConnectionIdleTimeoutMS(20000);
37+
**JMS:**
3638

39+
```java
3740
ConnectionFactory factory = new ServiceBusJmsConnectionFactory(
38-
"<NAMESPACE-CONNECTION-STRING>",
41+
"<CONNECTION-STRING>",
3942
new ServiceBusJmsConnectionFactorySettings());
4043
```
44+
45+
**Receiver client:**
46+
47+
```java
48+
ServiceBusReceiverClient receiver = new ServiceBusClientBuilder()
49+
.connectionString("<CONNECTION-STRING>")
50+
.receiver()
51+
.topicName("<TOPIC-NAME>")
52+
.subscriptionName("<SUBSCRIPTION-NAME>")
53+
.buildClient();
54+
```
55+
56+
**Sender client:**
57+
58+
```java
59+
ServiceBusSenderClient client = new ServiceBusClientBuilder()
60+
.connectionString("<CONNECTION-STRING>")
61+
.sender()
62+
.queueName("<QUEUE-NAME>")
63+
.buildClient();
64+
```
65+
66+
## [Node.js](#tab/nodejs)
67+
68+
```nodejs
69+
const client = new ServiceBusClient("<CONNECTION-STRING>");
70+
```
71+
72+
## [Python](#tab/python)
73+
74+
```python
75+
client = ServiceBusClient(
76+
fully_qualified_namespace = "<CONNECTION-STRING>"
77+
)
78+
```
79+
4180
---
4281

4382
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,72 +99,150 @@ For local development, make sure you're authenticated with the same Azure AD acc
6099

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

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

65-
# [C#](#tab/csharp)
104+
## [.NET](#tab/dotnet)
66105

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

69108
```dotnetcli
70109
dotnet add package Azure.Identity
71110
```
72111

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

75114
```csharp
76115
using Azure.Identity;
77116
```
78117

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:
118+
1. Identify the code that creates a `ServiceBusClient` object to connect to Azure Service Bus. Update your code to match the following example:
80119

81120
```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(
121+
// TODO: Replace the <SERVICE-BUS-NAMESPACE-NAME> placeholder.
122+
var client = new ServiceBusClient(
89123
"<SERVICE-BUS-NAMESPACE-NAME>.servicebus.windows.net",
90-
new DefaultAzureCredential(),
91-
clientOptions);
124+
new DefaultAzureCredential());
92125
```
93126

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.
127+
## [Java](#tab/java)
95128

96-
# [Java - JMS](#tab/java-jms)
129+
1. To use `DefaultAzureCredential`:
130+
- In a JMS application, add at least version 1.0.0 of the `azure-servicebus-jms` package to your application:
97131

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.
132+
```xml
133+
<dependency>
134+
<groupId>com.microsoft.azure</groupId>
135+
<artifactId>azure-servicebus-jms</artifactId>
136+
<version>1.0.0</version>
137+
</dependency>
138+
```
99139

100-
```xml
101-
<dependency>
102-
<groupId>com.microsoft.azure</groupId>
103-
<artifactId>azure-servicebus-jms</artifactId>
104-
<version>1.0.0</version>
105-
</dependency>
106-
```
107-
108-
2. At the top of your file, add the following `import` statements:
140+
- In a Java application, install the `azure-identity` package via one of the following approaches:
141+
- [Include the BOM file](/java/api/overview/azure/identity-readme?view=azure-java-stable&preserve-view=true#include-the-bom-file).
142+
- [Include a direct dependency](/java/api/overview/azure/identity-readme?view=azure-java-stable&preserve-view=true#include-direct-dependency).
143+
144+
1. At the top of your file, add the following code:
109145

110146
```java
111-
import com.azure.core.credential.TokenCredential;
112147
import com.azure.identity.DefaultAzureCredentialBuilder;
113148
```
114149

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:
150+
1. Update the code that connects to Azure Service Bus:
151+
- In a JMS application, identify the code that creates a `ServiceBusJmsConnectionFactory` object to connect to Azure Service Bus. Update your code to match the following example:
116152

117-
```java
118-
TokenCredential tokenCredential = new DefaultAzureCredentialBuilder()
153+
```java
154+
DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
155+
.build();
156+
157+
// TODO: Replace the <SERVICE-BUS-NAMESPACE-NAME> placeholder.
158+
ConnectionFactory factory = new ServiceBusJmsConnectionFactory(
159+
credential,
160+
"<SERVICE-BUS-NAMESPACE-NAME>.servicebus.windows.net",
161+
new ServiceBusJmsConnectionFactorySettings());
162+
```
163+
164+
- In a Java application, identify the code that creates a Service Bus sender or receiver client object to connect to Azure Service Bus. Update your code to match one of the following examples:
165+
166+
**Receiver client:**
167+
168+
```java
169+
DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
119170
.build();
171+
172+
// TODO: Update the <SERVICE-BUS-NAMESPACE-NAME> placeholder.
173+
ServiceBusReceiverClient receiver = new ServiceBusClientBuilder()
174+
.credential("<SERVICE-BUS-NAMESPACE-NAME>.servicebus.windows.net", credential)
175+
.receiver()
176+
.topicName("<TOPIC-NAME>")
177+
.subscriptionName("<SUBSCRIPTION-NAME>")
178+
.buildClient();
179+
```
180+
181+
**Sender client:**
182+
183+
```java
184+
DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
185+
.build();
186+
187+
// TODO: Update the <SERVICE-BUS-NAMESPACE-NAME> placeholder.
188+
ServiceBusSenderClient client = new ServiceBusClientBuilder()
189+
.credential("<SERVICE-BUS-NAMESPACE-NAME>.servicebus.windows.net", credential)
190+
.sender()
191+
.queueName("<QUEUE-NAME>")
192+
.buildClient();
193+
```
120194

121-
ServiceBusJmsConnectionFactorySettings connFactorySettings = new ServiceBusJmsConnectionFactorySettings();
122-
connFactorySettings.setConnectionIdleTimeoutMS(20000);
195+
## [Node.js](#tab/nodejs)
123196

124-
//TODO: Replace the "<SERVICE-BUS-NAMESPACE-NAME>" placeholder.
125-
ConnectionFactory factory = new ServiceBusJmsConnectionFactory(tokenCredential, "<SERVICE-BUS-NAMESPACE-NAME>.servicebus.windows.net", connFactorySettings);
126-
```
197+
1. To use `DefaultAzureCredential` in a Node.js application, install the `@azure/identity` package:
198+
199+
```bash
200+
npm install --save @azure/identity
201+
```
202+
203+
1. At the top of your file, add the following code:
204+
205+
```nodejs
206+
const { DefaultAzureCredential } = require("@azure/identity");
207+
```
208+
209+
1. Identify the code that creates a `ServiceBusClient` object to connect to Azure Service Bus. Update your code to match the following example:
210+
211+
```nodejs
212+
const credential = new DefaultAzureCredential();
213+
214+
// TODO: Update the <SERVICE-BUS-NAMESPACE-NAME> placeholder.
215+
const client = new ServiceBusClient(
216+
"<SERVICE-BUS-NAMESPACE-NAME>.servicebus.windows.net",
217+
credential
218+
);
219+
```
127220

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.
221+
## [Python](#tab/python)
222+
223+
1. To use `DefaultAzureCredential` in a Python application, install the `azure-identity` package:
224+
225+
```bash
226+
pip install azure-identity
227+
```
228+
229+
1. At the top of your file, add the following code:
230+
231+
```python
232+
from azure.identity import DefaultAzureCredential
233+
```
234+
235+
1. Identify the code that creates a `ServiceBusClient` object to connect to Azure Service Bus. Update your code to match the following example:
236+
237+
```python
238+
credential = DefaultAzureCredential()
239+
240+
# TODO: Update the <SERVICE-BUS-NAMESPACE-NAME> placeholder.
241+
client = ServiceBusClient(
242+
fully_qualified_namespace = "<SERVICE-BUS-NAMESPACE-NAME>.servicebus.windows.net",
243+
credential = credential
244+
)
245+
```
129246

130247
---
131248

@@ -230,7 +347,7 @@ You can assign a managed identity to an Azure Kubernetes Service (AKS) instance
230347
```azurecli
231348
az aks update \
232349
--resource-group <resource-group-name> \
233-
--name <virtual-machine-name>
350+
--name <virtual-machine-name> \
234351
--enable-managed-identity
235352
```
236353

@@ -268,7 +385,7 @@ If you connected your services using the Service Connector you don't need to com
268385

269386
### [Azure CLI](#tab/assign-role-azure-cli)
270387

271-
To assign a role at the resource level using the Azure CLI, you first must retrieve the resource ID using the `az servicebus show` command. You can filter the output properties using the --query parameter.
388+
To assign a role at the resource level using the Azure CLI, you first must retrieve the resource ID using the `az servicebus show` command. You can filter the output properties using the `--query` parameter.
272389

273390
```azurecli
274391
az servicebus show \

includes/passwordless/migration-guide/migrate-to-passwordless-overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The order and locations in which `DefaultAzureCredential` searches for credentia
1212
1313
The following code example demonstrates how to connect to Service Bus using passwordless connections. The next section describes how to migrate to this setup for a specific service in more detail.
1414

15-
A .NET Core application can pass an instance of `DefaultAzureCredential` into the constructor of a service client class. `DefaultAzureCredential` will automatically discover the credentials that are available in that environment.
15+
A .NET application can pass an instance of `DefaultAzureCredential` into the constructor of a service client class. `DefaultAzureCredential` will automatically discover the credentials that are available in that environment.
1616

1717
```csharp
1818
var serviceBusClient = new ServiceBusClient(

0 commit comments

Comments
 (0)