This page shows supported authentication methods and clients, and shows sample code you can use to connect Azure Service Bus to other cloud services using Service Connector. You might still be able to connect to Service Bus in other programming languages without using Service Connector. This page also shows default environment variable names and values (or Spring Boot configuration) you get when you create service connections.
Supported compute services
Service Connector can be used to connect the following compute services to Azure Service Bus:
Azure App Service
Azure Container Apps
Azure Functions
Azure Kubernetes Service (AKS)
Azure Spring Apps
Supported authentication types and client types
The table below shows which combinations of authentication methods and clients are supported for connecting your compute service to Azure Service Bus using Service Connector. A “Yes” indicates that the combination is supported, while a “No” indicates that it is not supported.
Client type
System-assigned managed identity
User-assigned managed identity
Secret/connection string
Service principal
.NET
Yes
Yes
Yes
Yes
Go
Yes
Yes
Yes
Yes
Java
Yes
Yes
Yes
Yes
Java - Spring Boot
Yes
Yes
Yes
Yes
Node.js
Yes
Yes
Yes
Yes
Python
Yes
Yes
Yes
Yes
None
Yes
Yes
Yes
Yes
This table indicates that all combinations of client types and authentication methods in the table are supported. All client types can use any of the authentication methods to connect to Azure Service Bus using Service Connector.
Default environment variable names or application properties
Use the connection details below to connect compute services to Service Bus. For each example below, replace the placeholder texts <Service-Bus-namespace>, <access-key-name>, <access-key-value><client-ID>, <client-secret>, and <tenant-id> with your own Service Bus namespace, shared access key name, shared access key value, client ID, client secret and tenant ID. For more information about naming conventions, check the Service Connector internals article.
System-assigned managed identity
SpringBoot client type
Default environment variable name
Description
Sample value
spring.cloud.azure.servicebus.namespace
Service Bus namespace
<Service-Bus-namespace>.servicebus.windows.net
Other client types
Default environment variable name
Description
Sample value
AZURE_SERVICEBUS_FULLYQUALIFIEDNAMESPACE
Service Bus namespace
<Service-Bus-namespace>.servicebus.windows.net
Sample code
Refer to the steps and code below to connect to Service Bus using a system-assigned managed identity.
Authenticate using Azure.Identity and get the Service Bus namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
using Azure.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.AzureSERVICEBUS;
string namespace = Environment.GetEnvironmentVariable("AZURE_SERVICEBUS_FULLYQUALIFIEDNAMESPACE");
// Uncomment the following lines corresponding to the authentication type you want to use.
// system-assigned managed identity
// var credential = new DefaultAzureCredential();
// user-assigned managed identity
// var credential = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_SERVICEBUS_CLIENTID");
// });
// service principal
// var tenantId = Environment.GetEnvironmentVariable("AZURE_SERVICEBUS_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_SERVICEBUS_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_SERVICEBUS_CLIENTSECRET");
// var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var client = new ServiceBusClient(namespace, credential);
Add the following dependencies in your pom.xml file:
Authenticate using azure-identity and get the Service Bus namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
import com.azure.messaging.servicebus.*;
import com.azure.identity.*;
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-managed identity
// DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder().build();
// for user-assigned managed identity
// DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_SERVICEBUS_CLIENTID"))
// .build();
// for service principal
// ClientSecretCredential defaultCredential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("<AZURE_SERVICEBUS_CLIENTID>"))
// .clientSecret(System.getenv("<AZURE_SERVICEBUS_CLIENTSECRET>"))
// .tenantId(System.getenv("<AZURE_SERVICEBUS_TENANTID>"))
// .build();
String namespace = System.getenv("AZURE_SERVICEBUS_FULLYQUALIFIEDNAMESPACE");
// For example, create a Service Bus Sender client for a queue using a managed identity or a service principal.
ServiceBusSenderClient senderClient = new ServiceBusClientBuilder()
.fullyQualifiedNamespace(namespace)
.credential(credential)
.sender()
.queueName("<queueName>")
.buildClient();
Add the following dependencies to your pom.xml file:
Set up a Spring application. The Service Bus connection configuration properties are set to Spring Apps by Service Connector. For more information, check Use Azure Service Bus in Spring applications.
Authenticate using azure-identity and get the Service Bus namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
import os
from azure.servicebus.aio import ServiceBusClient
from azure.servicebus import ServiceBusMessage
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
# Uncomment the following lines corresponding to the authentication type you want to use.
# system-assigned managed identity
# cred = ManagedIdentityCredential()
# user-assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_SERVICEBUS_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_SERVICEBUS_TENANTID')
# client_id = os.getenv('AZURE_SERVICEBUS_CLIENTID')
# client_secret = os.getenv('AZURE_SERVICEBUS_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
namespace = os.getenv('AZURE_SERVICEBUS_FULLYQUALIFIEDNAMESPACE')
client = ServiceBusClient(fully_qualified_namespace=namespace, credential=cred)
Install dependencies.
go get github.com/Azure/azure-sdk-for-go/sdk/azidentity
go get github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus
Authenticate using azidentity and get the Service Bus namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
import (
"context"
"errors"
"fmt"
"os"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus"
)
namespace, ok := os.LookupEnv("AZURE_SERVICEBUS_FULLYQUALIFIEDNAMESPACE")
if !ok {
panic("AZURE_SERVICEBUS_FULLYQUALIFIEDNAMESPACE environment variable not found")
}
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity.
// cred, err := azidentity.NewDefaultAzureCredential(nil)
// For user-assigned identity.
// clientid := os.Getenv("AZURE_POSTGRESQL_CLIENTID")
// azidentity.ManagedIdentityCredentialOptions.ID := clientid
// options := &azidentity.ManagedIdentityCredentialOptions{ID: clientid}
// cred, err := azidentity.NewManagedIdentityCredential(options)
// For service principal.
// clientid := os.Getenv("AZURE_POSTGRESQL_CLIENTID")
// tenantid := os.Getenv("AZURE_POSTGRESQL_TENANTID")
// clientsecret := os.Getenv("AZURE_POSTGRESQL_CLIENTSECRET")
// cred, err := azidentity.NewClientSecretCredential(tenantid, clientid, clientsecret, &azidentity.ClientSecretCredentialOptions{})
if err != nil {
panic(err)
}
client, err := azservicebus.NewClient(namespace, cred, nil)
if err != nil {
panic(err)
}
Install dependencies.
npm install @azure/service-bus @azure/identity
Authenticate using @azure/identity and get the Service Bus namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity";
const { ServiceBusClient } = require("@azure/service-bus");
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// const credential = new DefaultAzureCredential();
// for user-assigned managed identity
// const clientId = process.env.AZURE_SERVICEBUS_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// for service principal
// const tenantId = process.env.AZURE_SERVICEBUS_TENANTID;
// const clientId = process.env.AZURE_SERVICEBUS_CLIENTID;
// const clientSecret = process.env.AZURE_SERVICEBUS_CLIENTSECRET;
// const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
const fullyQualifiedNamespace = process.env.AZURE_SERVICEBUS_FULLYQUALIFIEDNAMESPACE;
const client = new ServiceBusClient(fullyQualifiedNamespace, credential);
For other languages, you can use the connection information that Service Connector sets to the environment variables to connect compute services to the Service Bus. For environment variable details, see Integrate Service Bus with Service Connector.
User-assigned managed identity
SpringBoot client type
Default environment variable name
Description
Sample value
spring.cloud.azure.servicebus.namespace
Service Bus namespace
<Service-Bus-namespace>.servicebus.windows.net
spring.cloud.azure.client-id
Your client ID
<client-ID>
Other client types
Default environment variable name
Description
Sample value
AZURE_SERVICEBUS_FULLYQUALIFIEDNAMESPACE
Service Bus namespace
<Service-Bus-namespace>.servicebus.windows.net
AZURE_SERVICEBUS_CLIENTID
Your client ID
<client-ID>
Sample code
Refer to the steps and code below to connect to Service Bus using a user-assigned managed identity.
Authenticate using Azure.Identity and get the Service Bus namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
using Azure.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.AzureSERVICEBUS;
string namespace = Environment.GetEnvironmentVariable("AZURE_SERVICEBUS_FULLYQUALIFIEDNAMESPACE");
// Uncomment the following lines corresponding to the authentication type you want to use.
// system-assigned managed identity
// var credential = new DefaultAzureCredential();
// user-assigned managed identity
// var credential = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_SERVICEBUS_CLIENTID");
// });
// service principal
// var tenantId = Environment.GetEnvironmentVariable("AZURE_SERVICEBUS_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_SERVICEBUS_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_SERVICEBUS_CLIENTSECRET");
// var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var client = new ServiceBusClient(namespace, credential);
Add the following dependencies in your pom.xml file:
Authenticate using azure-identity and get the Service Bus namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
import com.azure.messaging.servicebus.*;
import com.azure.identity.*;
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-managed identity
// DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder().build();
// for user-assigned managed identity
// DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_SERVICEBUS_CLIENTID"))
// .build();
// for service principal
// ClientSecretCredential defaultCredential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("<AZURE_SERVICEBUS_CLIENTID>"))
// .clientSecret(System.getenv("<AZURE_SERVICEBUS_CLIENTSECRET>"))
// .tenantId(System.getenv("<AZURE_SERVICEBUS_TENANTID>"))
// .build();
String namespace = System.getenv("AZURE_SERVICEBUS_FULLYQUALIFIEDNAMESPACE");
// For example, create a Service Bus Sender client for a queue using a managed identity or a service principal.
ServiceBusSenderClient senderClient = new ServiceBusClientBuilder()
.fullyQualifiedNamespace(namespace)
.credential(credential)
.sender()
.queueName("<queueName>")
.buildClient();
Add the following dependencies to your pom.xml file:
Set up a Spring application. The Service Bus connection configuration properties are set to Spring Apps by Service Connector. For more information, check Use Azure Service Bus in Spring applications.
Authenticate using azure-identity and get the Service Bus namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
import os
from azure.servicebus.aio import ServiceBusClient
from azure.servicebus import ServiceBusMessage
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
# Uncomment the following lines corresponding to the authentication type you want to use.
# system-assigned managed identity
# cred = ManagedIdentityCredential()
# user-assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_SERVICEBUS_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_SERVICEBUS_TENANTID')
# client_id = os.getenv('AZURE_SERVICEBUS_CLIENTID')
# client_secret = os.getenv('AZURE_SERVICEBUS_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
namespace = os.getenv('AZURE_SERVICEBUS_FULLYQUALIFIEDNAMESPACE')
client = ServiceBusClient(fully_qualified_namespace=namespace, credential=cred)
Install dependencies.
go get github.com/Azure/azure-sdk-for-go/sdk/azidentity
go get github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus
Authenticate using azidentity and get the Service Bus namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
import (
"context"
"errors"
"fmt"
"os"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus"
)
namespace, ok := os.LookupEnv("AZURE_SERVICEBUS_FULLYQUALIFIEDNAMESPACE")
if !ok {
panic("AZURE_SERVICEBUS_FULLYQUALIFIEDNAMESPACE environment variable not found")
}
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity.
// cred, err := azidentity.NewDefaultAzureCredential(nil)
// For user-assigned identity.
// clientid := os.Getenv("AZURE_POSTGRESQL_CLIENTID")
// azidentity.ManagedIdentityCredentialOptions.ID := clientid
// options := &azidentity.ManagedIdentityCredentialOptions{ID: clientid}
// cred, err := azidentity.NewManagedIdentityCredential(options)
// For service principal.
// clientid := os.Getenv("AZURE_POSTGRESQL_CLIENTID")
// tenantid := os.Getenv("AZURE_POSTGRESQL_TENANTID")
// clientsecret := os.Getenv("AZURE_POSTGRESQL_CLIENTSECRET")
// cred, err := azidentity.NewClientSecretCredential(tenantid, clientid, clientsecret, &azidentity.ClientSecretCredentialOptions{})
if err != nil {
panic(err)
}
client, err := azservicebus.NewClient(namespace, cred, nil)
if err != nil {
panic(err)
}
Install dependencies.
npm install @azure/service-bus @azure/identity
Authenticate using @azure/identity and get the Service Bus namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity";
const { ServiceBusClient } = require("@azure/service-bus");
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// const credential = new DefaultAzureCredential();
// for user-assigned managed identity
// const clientId = process.env.AZURE_SERVICEBUS_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// for service principal
// const tenantId = process.env.AZURE_SERVICEBUS_TENANTID;
// const clientId = process.env.AZURE_SERVICEBUS_CLIENTID;
// const clientSecret = process.env.AZURE_SERVICEBUS_CLIENTSECRET;
// const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
const fullyQualifiedNamespace = process.env.AZURE_SERVICEBUS_FULLYQUALIFIEDNAMESPACE;
const client = new ServiceBusClient(fullyQualifiedNamespace, credential);
For other languages, you can use the connection information that Service Connector sets to the environment variables to connect compute services to the Service Bus. For environment variable details, see Integrate Service Bus with Service Connector.
Connection string
Warning
Microsoft recommends that you use the most secure authentication flow available. The authentication flow described in this procedure requires a very high degree of trust in the application, and carries risks that are not present in other flows. You should only use this flow when other more secure flows, such as managed identities, aren't viable.
Get the Service Bus connection string from the environment variables added by Service Connector.
using Azure.Messaging.ServiceBus;
var connectionString = Environment.GetEnvironmentVariable("AZURE_SERVICEBUS_CONNECTIONSTRING");
var client = client = new ServiceBusClient(connectionString);
Add the following dependency in your pom.xml file:
Get the Service Bus connection string from the environment variables added by Service Connector.
import com.azure.messaging.servicebus.*;
String connectionString = System.getenv("AZURE_SERVICEBUS_CONNECTIONSTRING");
// For example, create a Service Bus Sender client for a queue using the connection string.
ServiceBusSenderClient senderClient = new ServiceBusClientBuilder()
.connectionString(connectionString)
.sender()
.queueName("<queueName>")
.buildClient();
Add the following dependencies to your pom.xml file:
Set up a Spring application. The Service Bus connection string spring.cloud.azure.servicebus.connection-string is set to Spring Apps by Service Connector. For more information, check Use Azure Service Bus in Spring applications.
Install dependencies.
pip install azure-servicebus
Get the Service Bus connection string from the environment variables added by Service Connector.
import os
from azure.servicebus.aio import ServiceBusClient
from azure.servicebus import ServiceBusMessage
connection_str = os.getenv('AZURE_SERVICEBUS_CONNECTIONSTRING')
client = ServiceBusClient.from_connection_string(connection_str)
Install dependencies.
go get github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus
Get the Service Bus connection string from the environment variables added by Service Connector.
import (
"context"
"errors"
"fmt"
"os"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus"
)
connectionString, ok := os.LookupEnv("AZURE_SERVICEBUS_CONNECTIONSTRING")
if !ok {
panic("AZURE_SERVICEBUS_CONNECTIONSTRING environment variable not found")
}
client, err := azservicebus.NewClientFromConnectionString(connectionString, nil)
if err != nil {
panic(err)
}
Install dependencies.
npm install @azure/service-bus
Get the Service Bus connection string from the environment variables added by Service Connector.
For other languages, you can use the connection information that Service Connector sets to the environment variables to connect compute services to the Service Bus. For environment variable details, see Integrate Service Bus with Service Connector.
Service principal
SpringBoot client type
Default environment variable name
Description
Sample value
spring.cloud.azure.servicebus.namespace
Service Bus namespace
<Service-Bus-namespace>.servicebus.windows.net
spring.cloud.azure.client-id
Your client ID
<client-ID>
spring.cloud.azure.tenant-id
Your client secret
<client-secret>
spring.cloud.azure.client-secret
Your tenant ID
<tenant-id>
Other client types
Default environment variable name
Description
Sample value
AZURE_SERVICEBUS_FULLYQUALIFIEDNAMESPACE
Service Bus namespace
<Service-Bus-namespace>.servicebus.windows.net
AZURE_SERVICEBUS_CLIENTID
Your client ID
<client-ID>
AZURE_SERVICEBUS_CLIENTSECRET
Your client secret
<client-secret>
AZURE_SERVICEBUS_TENANTID
Your tenant ID
<tenant-id>
Sample code
Refer to the steps and code below to connect to Service Bus using a service principal.
Authenticate using Azure.Identity and get the Service Bus namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
using Azure.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.AzureSERVICEBUS;
string namespace = Environment.GetEnvironmentVariable("AZURE_SERVICEBUS_FULLYQUALIFIEDNAMESPACE");
// Uncomment the following lines corresponding to the authentication type you want to use.
// system-assigned managed identity
// var credential = new DefaultAzureCredential();
// user-assigned managed identity
// var credential = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_SERVICEBUS_CLIENTID");
// });
// service principal
// var tenantId = Environment.GetEnvironmentVariable("AZURE_SERVICEBUS_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_SERVICEBUS_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_SERVICEBUS_CLIENTSECRET");
// var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var client = new ServiceBusClient(namespace, credential);
Add the following dependencies in your pom.xml file:
Authenticate using azure-identity and get the Service Bus namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
import com.azure.messaging.servicebus.*;
import com.azure.identity.*;
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-managed identity
// DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder().build();
// for user-assigned managed identity
// DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_SERVICEBUS_CLIENTID"))
// .build();
// for service principal
// ClientSecretCredential defaultCredential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("<AZURE_SERVICEBUS_CLIENTID>"))
// .clientSecret(System.getenv("<AZURE_SERVICEBUS_CLIENTSECRET>"))
// .tenantId(System.getenv("<AZURE_SERVICEBUS_TENANTID>"))
// .build();
String namespace = System.getenv("AZURE_SERVICEBUS_FULLYQUALIFIEDNAMESPACE");
// For example, create a Service Bus Sender client for a queue using a managed identity or a service principal.
ServiceBusSenderClient senderClient = new ServiceBusClientBuilder()
.fullyQualifiedNamespace(namespace)
.credential(credential)
.sender()
.queueName("<queueName>")
.buildClient();
Add the following dependencies to your pom.xml file:
Set up a Spring application. The Service Bus connection configuration properties are set to Spring Apps by Service Connector. For more information, check Use Azure Service Bus in Spring applications.
Authenticate using azure-identity and get the Service Bus namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
import os
from azure.servicebus.aio import ServiceBusClient
from azure.servicebus import ServiceBusMessage
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
# Uncomment the following lines corresponding to the authentication type you want to use.
# system-assigned managed identity
# cred = ManagedIdentityCredential()
# user-assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_SERVICEBUS_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_SERVICEBUS_TENANTID')
# client_id = os.getenv('AZURE_SERVICEBUS_CLIENTID')
# client_secret = os.getenv('AZURE_SERVICEBUS_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
namespace = os.getenv('AZURE_SERVICEBUS_FULLYQUALIFIEDNAMESPACE')
client = ServiceBusClient(fully_qualified_namespace=namespace, credential=cred)
Install dependencies.
go get github.com/Azure/azure-sdk-for-go/sdk/azidentity
go get github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus
Authenticate using azidentity and get the Service Bus namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
import (
"context"
"errors"
"fmt"
"os"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus"
)
namespace, ok := os.LookupEnv("AZURE_SERVICEBUS_FULLYQUALIFIEDNAMESPACE")
if !ok {
panic("AZURE_SERVICEBUS_FULLYQUALIFIEDNAMESPACE environment variable not found")
}
// Uncomment the following lines corresponding to the authentication type you want to use.
// For system-assigned identity.
// cred, err := azidentity.NewDefaultAzureCredential(nil)
// For user-assigned identity.
// clientid := os.Getenv("AZURE_POSTGRESQL_CLIENTID")
// azidentity.ManagedIdentityCredentialOptions.ID := clientid
// options := &azidentity.ManagedIdentityCredentialOptions{ID: clientid}
// cred, err := azidentity.NewManagedIdentityCredential(options)
// For service principal.
// clientid := os.Getenv("AZURE_POSTGRESQL_CLIENTID")
// tenantid := os.Getenv("AZURE_POSTGRESQL_TENANTID")
// clientsecret := os.Getenv("AZURE_POSTGRESQL_CLIENTSECRET")
// cred, err := azidentity.NewClientSecretCredential(tenantid, clientid, clientsecret, &azidentity.ClientSecretCredentialOptions{})
if err != nil {
panic(err)
}
client, err := azservicebus.NewClient(namespace, cred, nil)
if err != nil {
panic(err)
}
Install dependencies.
npm install @azure/service-bus @azure/identity
Authenticate using @azure/identity and get the Service Bus namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity";
const { ServiceBusClient } = require("@azure/service-bus");
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// const credential = new DefaultAzureCredential();
// for user-assigned managed identity
// const clientId = process.env.AZURE_SERVICEBUS_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// for service principal
// const tenantId = process.env.AZURE_SERVICEBUS_TENANTID;
// const clientId = process.env.AZURE_SERVICEBUS_CLIENTID;
// const clientSecret = process.env.AZURE_SERVICEBUS_CLIENTSECRET;
// const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
const fullyQualifiedNamespace = process.env.AZURE_SERVICEBUS_FULLYQUALIFIEDNAMESPACE;
const client = new ServiceBusClient(fullyQualifiedNamespace, credential);
For other languages, you can use the connection information that Service Connector sets to the environment variables to connect compute services to the Service Bus. For environment variable details, see Integrate Service Bus with Service Connector.
Next step
Follow the tutorial listed below to learn more about Service Connector.