Skip to content

Commit c3ce85c

Browse files
committed
Edit pass
1 parent f84dccf commit c3ce85c

File tree

1 file changed

+76
-63
lines changed

1 file changed

+76
-63
lines changed

articles/storage/common/multiple-identity-scenarios.md

Lines changed: 76 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ You can also enable access to Azure resources for local development by assigning
109109
using Azure.Messaging.ServiceBus;
110110
using Azure.Storage.Blobs;
111111
112-
// Create an instance of DefaultAzureCredential that will use a system-assigned managed identity
112+
// Create DefaultAzureCredential instance that uses system-assigned managed identity
113+
// in the underlying ManagedIdentityCredential.
113114
DefaultAzureCredential credential = new();
114115
115116
BlobServiceClient blobServiceClient = new(
@@ -157,6 +158,8 @@ You can also enable access to Azure resources for local development by assigning
157158
```java
158159
class Demo {
159160
public static void main(String[] args) {
161+
// Create DefaultAzureCredential instance that uses system-assigned managed identity
162+
// in the underlying ManagedIdentityCredential.
160163
DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
161164
.build();
162165
@@ -247,15 +250,17 @@ You can also enable access to Azure resources for local development by assigning
247250
const storageAccount = process.env.AZURE_STORAGE_ACCOUNT_NAME;
248251
const serviceBusNamespace = process.env.AZURE_SERVICE_BUS_NAMESPACE;
249252
253+
// Create DefaultAzureCredential instance that uses system-assigned managed identity
254+
// in the underlying ManagedIdentityCredential.
250255
const credential = new DefaultAzureCredential();
251256
252-
// Create client for Blob Storage using a system-assigned managed identity
257+
// Create client for Blob Storage
253258
const blobServiceClient = new BlobServiceClient(
254259
`https://${storageAccount}.blob.core.windows.net`,
255260
credential
256261
);
257262
258-
// Create client for Service Bus using a system-assigned managed identity
263+
// Create client for Service Bus
259264
const serviceBusClient = new ServiceBusClient(
260265
`https://${serviceBusNamespace}.servicebus.windows.net`,
261266
credential
@@ -278,7 +283,8 @@ You can also enable access to Azure resources for local development by assigning
278283
from azure.storage.blob import BlobServiceClient
279284
import os
280285
281-
# Create an instance of DefaultAzureCredential that will use a system-assigned managed identity
286+
# Create DefaultAzureCredential instance that uses system-assigned managed identity
287+
# in the underlying ManagedIdentityCredential.
282288
credential = DefaultAzureCredential()
283289
284290
blob_service_client = BlobServiceClient(
@@ -300,7 +306,7 @@ You can also enable access to Azure resources for local development by assigning
300306
301307
When this code runs locally, `DefaultAzureCredential` searches a credential chain for the first available credentials. If the `Managed_Identity_Client_ID` is null locally, it will automatically use the credentials from your local Azure CLI or Visual Studio sign-in. You can read more about this process in the [Azure Identity library overview](/dotnet/api/overview/azure/Identity-readme#defaultazurecredential).
302308
303-
When the application is deployed to Azure, `DefaultAzureCredential` will automatically retrieve the `Managed_Identity_Client_ID` variable from the app service environment. That value becomes available when a managed identity is associated with your app.
309+
When the application is deployed to Azure, `DefaultAzureCredential` automatically retrieves the `Managed_Identity_Client_ID` variable from the App Service environment. That value becomes available when a managed identity is associated with your app.
304310
305311
This overall process ensures that your app can run securely locally and in Azure without the need for any code changes.
306312
@@ -313,11 +319,11 @@ Although the apps in the previous example all shared the same service access req
313319
To configure this setup in your code, ensure your application registers separate service clients to connect to each storage account or database. Reference the correct managed identity client IDs for each service when configuring `DefaultAzureCredential`. The following code sample configures these Azure service connections:
314320
315321
* Two connections to separate storage accounts using a shared user-assigned managed identity
316-
* A connection to Azure Cosmos DB and Azure SQL services using a second shared user-assigned managed identity
322+
* A connection to Azure Cosmos DB and Azure SQL services using a second user-assigned managed identity. This managed identity is shared when the Azure SQL client driver allows for it; see the code comments for more details.
317323
318324
### [.NET](#tab/csharp)
319325
320-
1. In your project, install the `Azure.Identity` package. This library provides `DefaultAzureCredential`. Install any other [Azure SDK libraries](https://www.npmjs.com/search?q=%40azure) which are relevant to your app.
326+
1. In your project, install the required packages. The Azure Identity library provides `DefaultAzureCredential`.
321327
322328
```dotnetcli
323329
dotnet add package Azure.Identity
@@ -346,19 +352,21 @@ To configure this setup in your code, ensure your application registers separate
346352
ManagedIdentityClientId = clientIdStorage,
347353
});
348354
349-
// First Blob Storage client that uses a user-assigned managed identity
355+
// First Blob Storage client
350356
BlobServiceClient blobServiceClient1 = new(
351357
new Uri("https://<receipt-storage-account>.blob.core.windows.net"),
352358
credentialStorage);
353359
354-
// Second Blob Storage client that uses a user-assigned managed identity
360+
// Second Blob Storage client
355361
BlobServiceClient blobServiceClient2 = new(
356362
new Uri("https://<contract-storage-account>.blob.core.windows.net"),
357363
credentialStorage);
358364
359-
// Get the second user-assigned managed identity client ID to connect to shared databases
360365
string clientIdDatabases =
361366
Environment.GetEnvironmentVariable("Managed_Identity_Client_ID_Databases")!;
367+
368+
// Create a DefaultAzureCredential instance that configures the underlying
369+
// ManagedIdentityCredential to use a user-assigned managed identity.
362370
DefaultAzureCredential credentialDatabases = new(
363371
new DefaultAzureCredentialOptions
364372
{
@@ -370,7 +378,7 @@ To configure this setup in your code, ensure your application registers separate
370378
Environment.GetEnvironmentVariable("COSMOS_ENDPOINT", EnvironmentVariableTarget.Process),
371379
credentialDatabases);
372380
373-
// Open a connection to Azure SQL using a user-assigned managed identity
381+
// Open a connection to Azure SQL
374382
string connectionString =
375383
$"Server=<azure-sql-hostname>.database.windows.net;User Id={clientIdDatabases};Authentication=Active Directory Default;Database=<database-name>";
376384
@@ -436,36 +444,43 @@ To configure this setup in your code, ensure your application registers separate
436444
```java
437445
class Demo {
438446
public static void main(String[] args) {
439-
// Get the first user-assigned managed identity client ID to connect to shared storage
440447
String clientIdStorage = System.getenv("Managed_Identity_Client_ID_Storage");
441448
442-
// Get the DefaultAzureCredential from clientIdStorage
443-
DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
449+
// Create a DefaultAzureCredential instance that configures the underlying
450+
// ManagedIdentityCredential to use a user-assigned managed identity.
451+
DefaultAzureCredential credentialStorage = new DefaultAzureCredentialBuilder()
444452
.managedIdentityClientId(clientIdStorage)
445453
.build();
446454
447-
// First blob storage client that uses a user-assigned managed identity
455+
// First Blob Storage client
448456
BlobServiceClient blobServiceClient1 = new BlobServiceClientBuilder()
449457
.endpoint("https://<receipt-storage-account>.blob.core.windows.net")
450-
.credential(credential)
458+
.credential(credentialStorage)
451459
.buildClient();
452460
453-
// Second blob storage client that uses a user-assigned managed identity
461+
// Second Blob Storage client
454462
BlobServiceClient blobServiceClient2 = new BlobServiceClientBuilder()
455463
.endpoint("https://<contract-storage-account>.blob.core.windows.net")
456-
.credential(credential)
464+
.credential(credentialStorage)
457465
.buildClient();
458466
459-
// Get the second user-assigned managed identity ID to connect to shared databases
460-
String clientIdDatabase = System.getenv("Managed_Identity_Client_ID_Databases");
461-
467+
String clientIdDatabases = System.getenv("Managed_Identity_Client_ID_Databases");
468+
469+
// Create a DefaultAzureCredential instance that configures the underlying
470+
// ManagedIdentityCredential to use a user-assigned managed identity.
471+
DefaultAzureCredential credentialDatabases = new DefaultAzureCredentialBuilder()
472+
.managedIdentityClientId(clientIdDatabases)
473+
.build()
474+
462475
// Create an Azure Cosmos DB client
463476
CosmosClient cosmosClient = new CosmosClientBuilder()
464477
.endpoint("https://<cosmos-db-account>.documents.azure.com:443/")
465-
.credential(new DefaultAzureCredentialBuilder().managedIdentityClientId(clientIdDatabase).build())
478+
.credential(credentialDatabases)
466479
.buildClient();
467480
468-
// Open a connection to Azure SQL using a managed identity
481+
// Open a connection to Azure SQL using a managed identity.
482+
// The DefaultAzureCredential instance stored in the credentialDatabases variable can't be
483+
// used here, so sharing isn't possible between Cosmos DB and Azure SQL.
469484
String connectionUrl = "jdbc:sqlserver://<azure-sql-hostname>.database.windows.net:1433;"
470485
+ "database=<database-name>;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database"
471486
+ ".windows.net;loginTimeout=30;Authentication=ActiveDirectoryMSI;";
@@ -539,7 +554,7 @@ To configure this setup in your code, ensure your application registers separate
539554
1. Add the following to your code:
540555
541556
> [!NOTE]
542-
> Spring Cloud Azure doesn't support configure multiple clients of the same service, the following codes create multiple beans for this situation.
557+
> Spring Cloud Azure doesn't support configure multiple clients of the same service, the following code samples create multiple beans for this situation.
543558
544559
```java
545560
@Configuration
@@ -578,7 +593,7 @@ To configure this setup in your code, ensure your application registers separate
578593
579594
### [Node.js](#tab/javascript)
580595
581-
1. In your project, install the `@azure/identity` package. This library provides `DefaultAzureCredential`. Install any other [Azure SDK libraries](https://www.npmjs.com/search?q=%40azure) which are relevant to your app.
596+
1. In your project, install the required packages. The Azure Identity library provides `DefaultAzureCredential`.
582597
583598
```bash
584599
npm install --save @azure/identity @azure/storage-blob @azure/cosmos mssql
@@ -592,56 +607,50 @@ To configure this setup in your code, ensure your application registers separate
592607
import { CosmosClient } from "@azure/cosmos";
593608
import { sql } from "mssql";
594609
595-
// Get the first user-assigned managed identity client ID to connect to shared storage
596-
const clientIdStorage = process.env.MANAGED_IDENTITY_CLIENT_ID_STORAGE;
597-
598-
const credential = new DefaultAzureCredential({
599-
managedIdentityClientId: clientIdStorage
610+
// Create a DefaultAzureCredential instance that configures the underlying
611+
// ManagedIdentityCredential to use a user-assigned managed identity.
612+
const credentialStorage = new DefaultAzureCredential({
613+
managedIdentityClientId: process.env.MANAGED_IDENTITY_CLIENT_ID_STORAGE
600614
});
601615
602-
// Storage account names
603-
const storageAccountName1 = process.env.AZURE_STORAGE_ACCOUNT_NAME_1;
604-
const storageAccountName2 = process.env.AZURE_STORAGE_ACCOUNT_NAME_2;
605-
606-
// First Blob Storage client that uses a managed identity
616+
// First Blob Storage client
607617
const blobServiceClient1 = new BlobServiceClient(
608-
`https://${storageAccountName1}.blob.core.windows.net`,
609-
credential
618+
`https://${process.env.AZURE_STORAGE_ACCOUNT_NAME_1}.blob.core.windows.net`,
619+
credentialStorage
610620
);
611621
612-
// Second Blob Storage client that uses a managed identity
622+
// Second Blob Storage client
613623
const blobServiceClient2 = new BlobServiceClient(
614-
`https://${storageAccountName2}.blob.core.windows.net`,
615-
credential
624+
`https://${process.env.AZURE_STORAGE_ACCOUNT_NAME_2}.blob.core.windows.net`,
625+
credentialStorage
616626
);
617627
618-
// Get the second user-assigned managed identity client ID to connect to shared databases
619-
const clientIdDatabases = process.env.MANAGED_IDENTITY_CLIENT_ID_DATABASES;
628+
// Create a DefaultAzureCredential instance that configures the underlying
629+
// ManagedIdentityCredential to use a user-assigned managed identity.
630+
const credentialDatabases = new DefaultAzureCredential({
631+
managedIdentityClientId: process.env.MANAGED_IDENTITY_CLIENT_ID_DATABASES
632+
});
620633
621-
// Cosmos DB Account endpoint
622-
const cosmosDbAccountEndpoint = process.env.COSMOS_ENDPOINT;
623-
624634
// Create an Azure Cosmos DB client
625635
const cosmosClient = new CosmosClient({
626-
endpoint: cosmosDbAccountEndpoint,
627-
credential: new DefaultAzureCredential({
628-
managedIdentityClientId: clientIdDatabases
629-
})
636+
endpoint: process.env.COSMOS_ENDPOINT,
637+
credential: credentialDatabases
630638
});
631639
632640
// Open a connection to Azure SQL using a managed identity with mssql package.
633641
// mssql reads the environment variables to get the managed identity.
642+
// The DefaultAzureCredential instance stored in the credentialDatabases variable can't be
643+
// used here, so sharing isn't possible between Cosmos DB and Azure SQL.
634644
const server = process.env.AZURE_SQL_SERVER;
635645
const database = process.env.AZURE_SQL_DATABASE;
636646
const port = parseInt(process.env.AZURE_SQL_PORT);
637-
const type = process.env.AZURE_SQL_AUTHENTICATIONTYPE;
638647
639648
const config = {
640649
server,
641650
port,
642651
database,
643652
authentication: {
644-
type // <---- passwordless connection
653+
type: 'azure-active-directory-default'
645654
},
646655
options: {
647656
encrypt: true
@@ -653,7 +662,7 @@ To configure this setup in your code, ensure your application registers separate
653662
654663
### [Python](#tab/python)
655664
656-
1. In your project, install the `azure-identity` package. This library provides `DefaultAzureCredential`.
665+
1. In your project, install the required packages. The Azure Identity library provides `DefaultAzureCredential`.
657666
658667
```bash
659668
pip install azure-identity azure-storage-blob azure-cosmos pyodbc
@@ -667,34 +676,38 @@ To configure this setup in your code, ensure your application registers separate
667676
from azure.storage.blob import BlobServiceClient
668677
import os, pyodbc, struct
669678
670-
# Create an instance of DefaultAzureCredential that will use a user-assigned managed identity
671-
client_id_storage = os.environ['Managed_Identity_Client_ID_Storage']
672-
credential_storage = DefaultAzureCredential(managed_identity_client_id=client_id_storage)
679+
# Create a DefaultAzureCredential instance that configures the underlying
680+
# ManagedIdentityCredential to use a user-assigned managed identity.
681+
credential_storage = DefaultAzureCredential(
682+
managed_identity_client_id=os.environ['Managed_Identity_Client_ID_Storage']
683+
)
673684
674-
# First Blob Storage client that uses a user-assigned managed identity
685+
# First Blob Storage client
675686
blob_service_client_1 = BlobServiceClient(
676687
account_url="https://<receipt-storage-account>.blob.core.windows.net/",
677688
credential=credential_storage
678689
)
679690
680-
# Second Blob Storage client that uses a user-assigned managed identity
691+
# Second Blob Storage client
681692
blob_service_client_2 = BlobServiceClient(
682693
account_url="https://<contract-storage-account>.blob.core.windows.net/",
683694
credential=credential_storage
684695
)
685696
686-
# Get the second user-assigned managed identity client ID to connect to shared databases
687-
client_id_databases = os.environ['Managed_Identity_Client_ID_Databases']
688-
credential_databases = DefaultAzureCredential(managed_identity_client_id=client_id_databases)
697+
# Create a DefaultAzureCredential instance that configures the underlying
698+
# ManagedIdentityCredential to use a user-assigned managed identity.
699+
credential_databases = DefaultAzureCredential(
700+
managed_identity_client_id=os.environ['Managed_Identity_Client_ID_Databases']
701+
)
689702
690703
# Create an Azure Cosmos DB client
691704
cosmos_client = CosmosClient(
692705
os.environ['COSMOS_ENDPOINT'],
693706
credential=credential_databases
694707
)
695708
696-
# Azure SQL code here
697-
token_bytes = credential.get_token("https://database.windows.net/.default").token.encode("UTF-16-LE")
709+
# Connect to Azure SQL
710+
token_bytes = credential_databases.get_token("https://database.windows.net/.default").token.encode("UTF-16-LE")
698711
token_struct = struct.pack(f'<I{len(token_bytes)}s', len(token_bytes), token_bytes)
699712
SQL_COPT_SS_ACCESS_TOKEN = 1256 # This connection option is defined by microsoft in msodbcsql.h
700713
conn = pyodbc.connect(connection_string, attrs_before={SQL_COPT_SS_ACCESS_TOKEN: token_struct})
@@ -706,7 +719,7 @@ You can also associate a user-assigned managed identity and a system-assigned ma
706719
707720
:::image type="content" lightbox="media/user-and-system-assigned-identities-small.png" source="media/user-and-system-assigned-identities.png" alt-text="Diagram showing user-assigned and system-assigned managed identities.":::
708721
709-
These types of scenarios are explored in more depth in the [identities best practice recommendations](../../active-directory/managed-identities-azure-resources/managed-identity-best-practice-recommendations.md).
722+
These types of scenarios are explored in more depth in the [managed identity best practice recommendations](/entra/identity/managed-identities-azure-resources/managed-identity-best-practice-recommendations).
710723
711724
## Next steps
712725

0 commit comments

Comments
 (0)