Skip to content

Commit c31ee31

Browse files
committed
Add more Python code
1 parent f96d15d commit c31ee31

File tree

1 file changed

+73
-18
lines changed

1 file changed

+73
-18
lines changed

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

Lines changed: 73 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ You can also enable access to Azure resources for local development by assigning
9494

9595
#### [.NET](#tab/csharp)
9696

97-
1. In your project, add a reference to the `Azure.Identity` NuGet package. This library contains the necessary entities to implement `DefaultAzureCredential`. You can also add any other Azure libraries that are relevant to your app. For this example, the `Azure.Storage.Blobs` and `Azure.Messaging.ServiceBus` packages are added to connect to Blob Storage and Service Bus, respectively.
97+
1. In your project, install the `Azure.Identity` NuGet package. This library provides `DefaultAzureCredential`. You can also add any other Azure libraries that are relevant to your app. For this example, the `Azure.Storage.Blobs` and `Azure.Messaging.ServiceBus` packages are added to connect to Blob Storage and Service Bus, respectively.
9898

9999
```dotnetcli
100100
dotnet add package Azure.Identity
@@ -230,7 +230,7 @@ You can also enable access to Azure resources for local development by assigning
230230
231231
#### [Node.js](#tab/javascript)
232232
233-
1. In your project, use [npm](https://docs.npmjs.com/) to add a reference to the `@azure/identity` package. This library provides `DefaultAzureCredential`. For this example, the `@azure/storage-blob` and `@azure/service-bus` packages are installed to interact with Blob Storage and Service Bus.
233+
1. In your project, install the `@azure/identity` package. This library provides `DefaultAzureCredential`. For this example, the `@azure/storage-blob` and `@azure/service-bus` packages are installed to interact with Blob Storage and Service Bus.
234234
235235
```bash
236236
npm install --save @azure/identity @azure/storage-blob @azure/service-bus
@@ -281,19 +281,19 @@ You can also enable access to Azure resources for local development by assigning
281281
# Create an instance of DefaultAzureCredential that will use a system-assigned managed identity
282282
credential = DefaultAzureCredential()
283283
284-
blobServiceClient = BlobServiceClient(
284+
blob_service_client = BlobServiceClient(
285285
account_url="https://<my-storage-account-name>.blob.core.windows.net/",
286286
credential=credential
287287
)
288288
289289
fully_qualified_namespace = os.environ['SERVICEBUS_FULLY_QUALIFIED_NAMESPACE']
290290
queue_name = os.environ['SERVICE_BUS_QUEUE_NAME']
291291
292-
with ServiceBusClient(fully_qualified_namespace, credential) as client:
293-
with client.get_queue_sender(queue_name) as sender:
292+
with ServiceBusClient(fully_qualified_namespace, credential) as service_bus_client:
293+
with service_bus_client.get_queue_sender(queue_name) as sender:
294294
# Sending a single message
295295
single_message = ServiceBusMessage("Single message")
296-
sender.send_messages(single_message)
296+
sender.send_messages(single_message)
297297
```
298298
299299
---
@@ -310,13 +310,22 @@ Although the apps in the previous example all shared the same service access req
310310
311311
:::image type="content" source="media/multiple-managed-identities-small.png" lightbox="media/multiple-managed-identities.png" alt-text="Diagram showing multiple user-assigned managed identities.":::
312312
313-
To configure this setup in your code, make sure your application registers separate service clients to connect to each storage account or database. Make sure to pull in the correct managed identity client IDs for each service when configuring `DefaultAzureCredential`. The following code example configures the following service connections:
313+
To configure this setup in your code, ensure your application registers separate service clients to connect to each storage account or database. Make sure to pull in the correct managed identity client IDs for each service when configuring `DefaultAzureCredential`. The following code sample configures these Azure service connections:
314314
315315
* Two connections to separate storage accounts using a shared user-assigned managed identity
316316
* A connection to Azure Cosmos DB and Azure SQL services using a second shared user-assigned managed identity
317317
318318
### [.NET](#tab/csharp)
319319
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.
321+
322+
```dotnetcli
323+
dotnet add package Azure.Identity
324+
dotnet add package Azure.Storage.Blobs
325+
dotnet add package Microsoft.Azure.Cosmos
326+
dotnet add package Microsoft.Data.SqlClient
327+
```
328+
320329
```csharp
321330
using Azure.Core;
322331
using Azure.Identity;
@@ -329,7 +338,7 @@ string clientIdStorage =
329338

330339
// Create a DefaultAzureCredential instance that configures the underlying
331340
// ManagedIdentityCredential to use a user-assigned managed identity.
332-
DefaultAzureCredential credential = new(
341+
DefaultAzureCredential credentialStorage = new(
333342
new DefaultAzureCredentialOptions
334343
{
335344
ManagedIdentityClientId = clientIdStorage,
@@ -338,25 +347,26 @@ DefaultAzureCredential credential = new(
338347
// First Blob Storage client that uses a user-assigned managed identity
339348
BlobServiceClient blobServiceClient1 = new(
340349
new Uri("https://<receipt-storage-account>.blob.core.windows.net"),
341-
credential);
350+
credentialStorage);
342351

343352
// Second Blob Storage client that uses a user-assigned managed identity
344353
BlobServiceClient blobServiceClient2 = new(
345354
new Uri("https://<contract-storage-account>.blob.core.windows.net"),
346-
credential);
355+
credentialStorage);
347356

348357
// Get the second user-assigned managed identity client ID to connect to shared databases
349358
string clientIdDatabases =
350359
Environment.GetEnvironmentVariable("Managed_Identity_Client_ID_Databases")!;
360+
DefaultAzureCredential credentialDatabases = new(
361+
new DefaultAzureCredentialOptions
362+
{
363+
ManagedIdentityClientId = clientIdDatabases,
364+
});
351365

352366
// Create an Azure Cosmos DB client
353367
CosmosClient cosmosClient = new(
354368
Environment.GetEnvironmentVariable("COSMOS_ENDPOINT", EnvironmentVariableTarget.Process),
355-
new DefaultAzureCredential(
356-
new DefaultAzureCredentialOptions
357-
{
358-
ManagedIdentityClientId = clientIdDatabases,
359-
}));
369+
credentialDatabases);
360370

361371
// Open a connection to Azure SQL using a user-assigned managed identity
362372
string connectionString =
@@ -370,7 +380,7 @@ using (SqlConnection connection = new(connectionString)
370380
string scope = authParams.Resource.EndsWith(defaultScopeSuffix)
371381
? authParams.Resource
372382
: $"{authParams.Resource}{defaultScopeSuffix}";
373-
AccessToken token = await credential.GetTokenAsync(
383+
AccessToken token = await credentialDatabases.GetTokenAsync(
374384
new TokenRequestContext([scope]),
375385
cancellationToken);
376386

@@ -566,7 +576,7 @@ public class ExampleService {
566576

567577
### [Node.js](#tab/javascript)
568578

569-
1. Inside of your project, use [npm](https://docs.npmjs.com/) to add a reference to the `@azure/identity` package. This library contains all of the necessary entities to implement `DefaultAzureCredential`. Install any other [Azure SDK libraries](https://www.npmjs.com/search?q=%40azure) which are relevant to your app.
579+
1. In your project, use [npm](https://docs.npmjs.com/) to add a reference to 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.
570580

571581
```bash
572582
npm install --save @azure/identity @azure/storage-blob @azure/cosmos mssql
@@ -641,7 +651,52 @@ public class ExampleService {
641651
642652
### [Python](#tab/python)
643653
644-
TODO
654+
1. In your project, install the `azure-identity` package. This library provides `DefaultAzureCredential`.
655+
656+
```bash
657+
pip install azure-identity azure-storage-blob azure-cosmos pyodbc
658+
```
659+
660+
1. Create service client objects for the Azure services your app will connect to. The following example connects to Blob Storage, Cosmos DB, and Azure SQL using the corresponding service clients.
661+
662+
```python
663+
from azure.cosmos import CosmosClient
664+
from azure.identity import DefaultAzureCredential
665+
from azure.storage.blob import BlobServiceClient
666+
import os, pyodbc, struct
667+
668+
# Create an instance of DefaultAzureCredential that will use a user-assigned managed identity
669+
client_id_storage = os.environ['Managed_Identity_Client_ID_Storage']
670+
credential_storage = DefaultAzureCredential(managed_identity_client_id=client_id_storage)
671+
672+
# First Blob Storage client that uses a user-assigned managed identity
673+
blob_service_client_1 = BlobServiceClient(
674+
account_url="https://<receipt-storage-account>.blob.core.windows.net/",
675+
credential=credential_storage
676+
)
677+
678+
# Second Blob Storage client that uses a user-assigned managed identity
679+
blob_service_client_2 = BlobServiceClient(
680+
account_url="https://<contract-storage-account>.blob.core.windows.net/",
681+
credential=credential_storage
682+
)
683+
684+
# Get the second user-assigned managed identity client ID to connect to shared databases
685+
client_id_databases = os.environ['Managed_Identity_Client_ID_Databases']
686+
credential_databases = DefaultAzureCredential(managed_identity_client_id=client_id_databases)
687+
688+
# Create an Azure Cosmos DB client
689+
cosmos_client = CosmosClient(
690+
os.environ['COSMOS_ENDPOINT'],
691+
credential=credential_databases
692+
)
693+
694+
# Azure SQL code here
695+
token_bytes = credential.get_token("https://database.windows.net/.default").token.encode("UTF-16-LE")
696+
token_struct = struct.pack(f'<I{len(token_bytes)}s', len(token_bytes), token_bytes)
697+
SQL_COPT_SS_ACCESS_TOKEN = 1256 # This connection option is defined by microsoft in msodbcsql.h
698+
conn = pyodbc.connect(connection_string, attrs_before={SQL_COPT_SS_ACCESS_TOKEN: token_struct})
699+
```
645700
646701
---
647702

0 commit comments

Comments
 (0)