Authenticate to Azure resources from Python apps hosted on-premises
Apps hosted outside of Azure (for example on-premises or at a third-party data center) should use an application service principal to authenticate to Azure when accessing Azure resources. Application service principal objects are created using the app registration process in Azure. When an application service principal is created, a client ID and client secret will be generated for your app. The client ID, client secret, and your tenant ID are then stored in environment variables so they can be used by the Azure SDK for Python to authenticate your app to Azure at runtime.
A different app registration should be created for each environment the app is hosted in. This allows environment specific resource permissions to be configured for each service principal and make sure an app deployed to one environment does not talk to Azure resources that are part of another environment.
1 - Register the application in Azure
An app can be registered with Azure using either the Azure portal or the Azure CLI.
Sign in to the Azure portal and follow these steps.
2 - Assign roles to the application service principal
Next, you need to determine what roles (permissions) your app needs on what resources and assign those roles to your app. Roles can be assigned a role at a resource, resource group, or subscription scope. This example will show how to assign roles for the service principal at the resource group scope since most applications group all their Azure resources into a single resource group.
3 - Configure environment variables for application
You must set the AZURE_CLIENT_ID, AZURE_TENANT_ID, and AZURE_CLIENT_SECRET environment variables for the process that runs your Python app to make the application service principal credentials available to your app at runtime. The DefaultAzureCredential object looks for the service principal information in these environment variables.
When using Gunicorn to run Python web apps in a UNIX server environment, environment variables for an app can be specified by using the EnvironmentFile directive in the gunicorn.server file as shown below.
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=www-user
Group=www-data
WorkingDirectory=/path/to/python-app
EnvironmentFile=/path/to/python-app/py-env/app-environment-variables
ExecStart=/path/to/python-app/py-env/gunicorn --config config.py wsgi:app
[Install]
WantedBy=multi-user.target
The file specified in the EnvironmentFile directive should contain a list of environment variables with their values as shown below.
AZURE_CLIENT_ID=<value>
AZURE_TENANT_ID=<value>
AZURE_CLIENT_SECRET=<value>
4 - Implement DefaultAzureCredential in application
To authenticate Azure SDK client objects to Azure, your application should use the DefaultAzureCredential class from the azure.identity package.
Start by adding the azure.identity package to your application.
pip install azure-identity
Next, for any Python code that creates an Azure SDK client object in your app, you will want to:
- Import the
DefaultAzureCredentialclass from theazure.identitymodule. - Create a
DefaultAzureCredentialobject. - Pass the
DefaultAzureCredentialobject to the Azure SDK client object constructor.
An example of this is shown in the following code segment.
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient
# Acquire a credential object
token_credential = DefaultAzureCredential()
blob_service_client = BlobServiceClient(
account_url="https://<my_account_name>.blob.core.windows.net",
credential=token_credential)
When the above code instantiates the DefaultAzureCredential object, DefaultAzureCredential reads the environment variables AZURE_SUBSCRIPTION_ID, AZURE_TENANT_ID, AZURE_CLIENT_ID, and AZURE_CLIENT_SECRET for the application service principal information to connect to Azure with.
الملاحظات
إرسال الملاحظات وعرضها المتعلقة بـ