Azure Key Vault is a cloud service that provides a secure store for secrets, keys, and certificates. It is designed to help you safeguard cryptographic keys and secrets used by cloud applications and services. This guide demonstrates how to use Azure Key Vault to securely store an Azure AI services key and access it from an application using a service principal. This method enhances security by avoiding the need to store keys directly in application code or configuration files.
When managing secrets, keys, and certificates, ensuring their security is paramount. Azure Key Vault offers numerous advantages for this purpose, making it an essential tool for modern cloud applications.
Azure Key Vault is versatile and supports a wide range of use cases. Here are some scenarios where it can be particularly beneficial:
To begin leveraging the benefits of Azure Key Vault, you'll first need to create a Key Vault and add your secrets. This section will guide you through the process.
Navigate to the Azure Portal.
Create a new resource:
Access Configuration:
Once the deployment is finished, you'll have your KEY VAULT resource available, next we can start adding keys and secrets.
With your Key Vault created, the next step is to add the Azure AI services key as a secret.
The Name will be the value needed to identify the provided resource key.
Now that your key is securely stored in Azure Key Vault, you'll need a way for your application to access it. This is where creating a service principal comes into play.
All the commands must be used inside a Powershell after being log inside Azure
Open your terminal and run the following Azure CLI command, replacing placeholders with appropriate values:
az ad sp create-for-rbac -n "api://<spName>" --role owner --scopes subscriptions/<subscriptionId>/resourceGroups/<resourceGroup>
<spName>
with a unique name for your application identity.<subscriptionId>
and <resourceGroup>
with your subscription ID and resource group name.
Save the Output:
appId
, password
, and tenant
. Save these values securely.
To ensure the service principal has the necessary permissions, you need to retrieve its object ID.
az ad sp show --id <appId>
<appId>
with your service principal's app ID.id
value from the JSON response.
With the object ID in hand, the final step is to assign the appropriate permissions.
az keyvault set-policy -n <keyVaultName> --object-id <objectId> --secret-permissions get list
Replace <keyVaultName> with the name of your Key Vault.
Replace <objectId> with the service principal's object ID.
With the Key Vault and service principal set up, you're now ready to integrate this configuration into your application. This ensures your application can securely access the Azure AI services key.
When using Python app and assuming you want to connect Azure Language Service
You can install the following package:
pip install azure-ai-textanalytics==5.3.0
pip install azure-identity==1.5.0
pip install azure-keyvault-secrets==4.2.0
Finally we need to set up envs variables:
AI_SERVICE_ENDPOINT=<your-ai-service-endpoint>
KEY_VAULT_NAME=<your-key-vault-name>
TENANT_ID=<your-tenant-id>
CLIENT_ID=<your-app-id>
CLIENT_SECRET=<your-client-secret>
It's time to write the application code that will use these settings to access the Key Vault and retrieve the secret.
By securely storing sensitive keys in Azure Key Vault and accessing them using a service principal, you can significantly enhance the security of your applications. This approach ensures that keys are not exposed in application code or configuration files, thereby reducing the risk of unauthorized access.