Skip to content

Commit 7a48eed

Browse files
committed
Updates to Python Entra include file
1 parent 474905d commit 7a48eed

File tree

1 file changed

+56
-16
lines changed

1 file changed

+56
-16
lines changed

includes/iot-hub-howto-connect-service-iothub-entra-python.md

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,83 @@ ms.manager: lizross
1111
ms.date: 11/06/2024
1212
---
1313

14-
For an overview of Python SDK authentication, see [Authenticate Python apps to Azure services by using the Azure SDK for Python](/azure/developer/python/sdk/authentication/overview)
15-
16-
### Entra token credential
14+
A backend app that uses Microsoft Entra must successfully authenticate and obtain a security token credential before connecting to IoT Hub. This token is passed to a IoT Hub connection method. For general information about setting up and using Microsoft Entra for IoT Hub, see [Control access to IoT Hub by using Microsoft Entra ID](/azure/iot-hub/authenticate-authorize-azure-ad).
1715

18-
You must generate and supply a token credential to `from_token_credential`.
16+
For an overview of Python SDK authentication, see [Authenticate Python apps to Azure services by using the Azure SDK for Python](/azure/developer/python/sdk/authentication/overview)
1917

20-
[DefaultAzureCredential](/azure/developer/python/sdk/authentication/overview#use-defaultazurecredential-in-an-application) is the easiest way to generate a token. You can also use credential chains to generate a token. For more information, see [Credential chains in the Azure Identity client library for Python](/azure/developer/python/sdk/authentication/credential-chains).
18+
##### Configure Microsoft Entra app
2119

22-
To create required Microsoft Entra app parameters for `DefaultAzureCredential`, create a Microsoft Entra app registration that contains your selected authentication mechanism:
20+
You must set up a Microsoft Entra app that is configured for your preferred authentication credential. The app contains parameters such as client secret that are used by the backend application to authenticate. The available app authentication configurations are:
2321

2422
* Client secret
2523
* Certificate
2624
* Federated identity credential
2725

28-
For more information, see [Quickstart: Register an application with the Microsoft identity platform](/entra/identity-platform/quickstart-register-app).
26+
Microsoft Entra apps may require specific role permissions depending on operations being performed. For example, [IoT Hub Twin Contributor](/azure/role-based-access-control/built-in-roles/internet-of-things#iot-hub-twin-contributor) is required to enable read and write access to a IoT Hub device and module twins. For more information, see [Manage access to IoT Hub by using Azure RBAC role assignment](/azure/iot-hub/authenticate-authorize-azure-ad?#manage-access-to-iot-hub-by-using-azure-rbac-role-assignment).
27+
28+
For more information about setting up a Microsoft Entra app, see [Quickstart: Register an application with the Microsoft identity platform](/entra/identity-platform/quickstart-register-app).
29+
30+
##### Authenticate using DefaultAzureCredential
31+
32+
The easiest way to use Microsoft Entra to authenticate a backend application is to use [DefaultAzureCredential](/azure/developer/python/sdk/authentication/overview#use-defaultazurecredential-in-an-application), but it's recommended to use a different method in a production environment including a specific `TokenCredential` or pared-down `ChainedTokenCredential`. For simplicity, this section describes authentication using `DefaultAzureCredential` and Client secret. For more information about the pros and cons of using `DefaultAzureCredential`, see [Credential chains in the Azure Identity client library for Python](/azure/developer/python/sdk/authentication/credential-chains).
33+
34+
[DefaultAzureCredential](/python/api/azure-identity/azure.identity.defaultazurecredential) supports different authentication mechanisms and determines the appropriate credential type based on the environment it's executing in. It attempts to use multiple credential types in an order until it finds a working credential.
35+
36+
Microsoft Entra requires this import pakage and corresponding `import` statement:
37+
38+
```shell
39+
pip install azure-identity
40+
```
41+
42+
```python
43+
from azure.identity import DefaultAzureCredential
44+
```
2945

30-
Microsoft Entra apps may require permissions depending on operations performed. For example, [IoT Hub Twin Contributor](/azure/role-based-access-control/built-in-roles/internet-of-things#iot-hub-twin-contributor) is required to enable read and write access to a IoT Hub device and module twins. For more information, see [Azure built-in roles](/azure/role-based-access-control/built-in-roles#internet-of-things).
46+
In this example, Microsoft Entra app registration client secret, client ID, and tenant ID have been added to environment variables. These environment variables are used by `DefaultAzureCredential` to authenticate the application. The result of a successful Microsoft Entra authentication is a security token credential that is passed to an IoT Hub connection method.
3147

32-
#### Connect to IoT Hub
48+
```python
49+
from azure.identity import DefaultAzureCredential
50+
credential = DefaultAzureCredential()
51+
```
52+
53+
The resulting [AccessToken](/python/api/azure-core/azure.core.credentials.accesstoken) can then be passed to `from_token_credential` to connect to IoT Hub method for any SDK client that accepts Microsoft Entra credentials:
3354

34-
Use [from_token_credential](/python/api/azure-iot-hub/azure.iot.hub.iothubregistrymanager?#azure-iot-hub-iothubregistrymanager-from-token-credential) to create a service connection to IoT Hub using an Entra token credential.
55+
* [IoTHubRegistryManager](/python/api/azure-iot-hub/azure.iot.hub.iothubregistrymanager?#azure-iot-hub-iothubregistrymanager-from-token-credential) to create a service connection to IoT Hub using an Entra token credential.
56+
* [IoTHubJobManager](/python/api/azure-iot-hub/azure.iot.hub.iothubjobmanager?view=azure-python&#azure-iot-hub-iothubjobmanager-from-token-credential)
57+
* [DigitalTwinClient](/python/api/azure-iot-hub/azure.iot.hub.digitaltwinclient?#azure-iot-hub-digitaltwinclient-from-token-credential)
58+
* [IoTHubHttpRuntimeManager](/python/api/azure-iot-hub/azure.iot.hub.iothubhttpruntimemanager?#azure-iot-hub-iothubhttpruntimemanager-from-token-credential)
59+
* [IoTHubConfigurationManager](/python/api/azure-iot-hub/azure.iot.hub.iothubconfigurationmanager?#azure-iot-hub-iothubconfigurationmanager-from-token-credential)
3560

3661
`from_token_credential` requires two parameters:
3762

38-
* The Azure service URL
63+
* The Azure service URL - The Azure service URL should be in the format `{Your Entra domain URL}.azure-devices.net` without a `https://` prefix. For example, `MyAzureDomain.azure-devices.net`.
3964
* The Azure credential token
4065

41-
In this example, the Azure credential is obtained using `DefaultAzureCredential`. The Azure domain URL and credential are then supplied to `BlobServiceClient`.
66+
In this example, the Azure credential is obtained using `DefaultAzureCredential`. The Azure service URL and credential are then supplied to `IoTHubRegistryManager.from_token_credential` to create the connection to IoT Hub.
4267

4368
```python
69+
import sys
70+
import os
71+
4472
from azure.identity import DefaultAzureCredential
45-
from azure.storage.blob import BlobServiceClient
73+
from azure.iot.hub import IoTHubRegistryManager
74+
75+
# Define the client secret values
76+
clientSecretValue = 'xxxxxxxxxxxxxxx'
77+
clientID = 'xxxxxxxxxxxxxx'
78+
tenantID = 'xxxxxxxxxxxxx'
79+
80+
# Set environment variables
81+
os.environ['AZURE_CLIENT_SECRET'] = clientSecretValue
82+
os.environ['AZURE_CLIENT_ID'] = clientID
83+
os.environ['AZURE_TENANT_ID'] = tenantID
4684

4785
# Acquire a credential object
4886
credential = DefaultAzureCredential()
4987

50-
blob_service_client = BlobServiceClient(
51-
account_url="https://<my_account_name>.blob.core.windows.net",
52-
credential=credential)
88+
# Use Entra to auth IoT Hub service
89+
print("Connecting to IoTHubRegistryManager...")
90+
iothub_registry_manager = IoTHubRegistryManager.from_token_credential(
91+
url="{Your Entra domain URL}.azure-devices.net",
92+
token_credential=credential)
5393
```

0 commit comments

Comments
 (0)