Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019
Azure Key Vaults enable developers to securely store and manage sensitive information such as passwords, API keys, and certificates. This article walks you through how to query and use secrets from an Azure Key Vault in your pipeline.
Prerequisites
Product | Requirements |
---|---|
Azure DevOps | - An Azure DevOps project. - Permissions: - To grant access to all pipelines in the project: You must be a member of the Project Administrators group. - To create service connections: You must have the Administrator or Creator role for service connections. |
GitHub | - A GitHub account and a GitHub repository. - A GitHub service connection to authorize Azure Pipelines. |
Azure | - An Azure subscription. |
Create a Key Vault
Sign in to the Azure portal, and then select Create a resource.
Under Key Vault, select Create to create a new Azure Key Vault.
Select your Subscription from the dropdown menu, and then select an existing Resource group or create a new one. Enter a Key vault name, select a Region, choose a Pricing tier, and select Next if you want to configure additional properties. Otherwise, select Review + create to keep the default settings.
Once the deployment is complete, select Go to resource.
Set up authentication
Create a user-assigned managed identity
Sign in to the Azure portal, then search for the Managed Identities service in the search bar.
Select Create, and fill out the required fields as follows:
- Subscription: Select your subscription from the dropdown menu.
- Resource group: Select an existing resource group or create a new one.
- Region: Select a region from the dropdown menu.
- Name: Enter a name for your user-assigned managed identity.
Select Review + create when you're done.
Once the deployment is complete, select Go to resource, then copy the Subscription and Client ID, you'll need them in the next steps.
Navigate to Settings > Properties, and copy your managed identity's Tenant ID to use later.
Set up key vault access policies
Navigate to Azure portal, and use the search bar to find the key vault you created earlier.
Select Access policies, then select Create to add a new policy.
Under Secret permissions, select the Get and List checkboxes.
Select Next, then paste the Client ID of the managed identity you created earlier into the search bar.
Select your managed identity, select Next, then Next once more.
Review your new policy, and then select Create when you're done.
Create a service connection
Sign in to your Azure DevOps organization, and then navigate to your project.
Select Project settings > Service connections, and then select New service connection.
Select Azure Resource Manager, then select Next.
Under Identity Type, select Managed identity from the dropdown menu.
For Step 1: Managed identity details, fill out the fields as follows:
Subscription for managed identity: Select the subscription that contains your managed identity.
Resource group for managed identity: Select the resource group where your managed identity is hosted.
Managed Identity: Select your managed identity from the dropdown menu.
For Step 2: Azure Scope, fill out the fields as follows:
Scope level for service connection: Select Subscription.
Subscription for service connection: Select the subscription your managed identity will access.
Resource group for Service connection: (Optional) Specify this if you want to restrict access to a specific resource group.
For Step 3: Service connection details:
Service connection name: Provide a name for your service connection.
Service Management Reference: (Optional) Include context information from an ITSM database.
Description: (Optional) Add a description.
Under Security, check the Grant access permission to all pipelines box to allow all pipelines to use this service connection. If you leave this unchecked, you’ll need to manually grant access for each pipeline.
Select Save to validate and create the service connection.
Query and use secrets in your pipeline
Using the Azure Key Vault task, you can now query and fetch secrets from Azure Key Vault and use them in subsequent tasks in your pipeline. Note that secrets must be explicitly mapped to environment variables, as shown in the following example:
pool:
vmImage: 'ubuntu-latest'
steps:
- task: AzureKeyVault@1
inputs:
azureSubscription: 'SERVICE_CONNECTION_NAME'
KeyVaultName: 'KEY_VAULT_NAME'
SecretsFilter: '*'
- bash: |
echo "Secret Found! $MY_MAPPED_ENV_VAR"
env:
MY_MAPPED_ENV_VAR: $(SECRET_NAME)
The output from the last bash step should look like this:
Secret Found! ***
Note
To query multiple secrets from your Azure Key Vault, use the SecretsFilter
input and provide a comma-separated list of secret names, like: 'secret1, secret2'.