Even when stored securely in environment variables or secret management systems, long-lived service principal credentials (such as Client Secrets or Certificates) remain valid indefinitely unless manually rotated, which introduces risk. And so even though the credentials are securely retrieved, they are long-lived, meaning if they are compromised, attackers will have persistent access until the credentials are rotated or revoked.
from azure.identity import ClientSecretCredential
from azure.keyvault.secrets import SecretClient
import os
# Long-lived service principal key retrieved securely
client_secret = os.getenv('AZURE_CLIENT_SECRET')
tenant_id = os.getenv('AZURE_TENANT_ID')
client_id = os.getenv('AZURE_CLIENT_ID')
credential = ClientSecretCredential(tenant_id, client_id,
client_secret)
# Access Azure Key Vault using long-lived credentials
secret_client =
SecretClient(vault_url="https://<your-key-vault>.vault.azure.net/",
credential=credential)
secret = secret_client.get_secret("your-secret-name")
print(f"Secret value: {secret.value}")
Managed Identities provide a more secure alternative to using the standard IAM credentials in Azure, by eliminating the need for long-lived credentials. Managed Identities are automatically managed by Azure and are short-lived, reducing the attack surface.
To convert an Azure service principal key to role-based authentication, you can either use Managed Identities or Azure AD App Role Assignments.
Managed Identities allow Azure resources like Virtual Machines or App Services to authenticate to other Azure services securely without needing credentials, while Azure AD App Role Assignments enable secure, role-based access for external or non-Azure services.
Once the application is updated to use these methods, the old client secret (key) can be deleted, improving security by eliminating long-lived credentials and replacing them with automatically managed, short-lived tokens.
from azure.identity import DefaultAzureCredential
from azure.keyvault.secrets import SecretClient
# Use Managed Identity to get ephemeral credentials
credential = DefaultAzureCredential()
# Access Azure Key Vault using Managed Identity
secret_client =
SecretClient(vault_url="https://<your-key-vault>.vault.azure.net/",
credential=credential)
secret = secret_client.get_secret("your-secret-name")
print(f"Secret value: {secret.value}")
When an Azure resource (e.g., a VM or Azure Function) with a managed identity needs to access another Azure service (such as a Key Vault, Storage Account, or Azure SQL Database), it authenticates using Azure AD. This process does not require any manual management of credentials, as Azure manages the lifecycle of the identity and authentication behind the scenes.
When an Azure resource (e.g., a VM or Azure Function) with a managed identity needs to access another Azure service (such as a Key Vault, Storage Account, or Azure SQL Database), it authenticates using Azure AD. This process does not require any manual management of credentials, as Azure manages the lifecycle of the identity and authentication behind the scenes.
The key steps involved in the process are:
Authorization
header as aBearer token.read, write, list,
etc.).