Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019
With Azure Key Vault, you can securely store and manage your sensitive information such as passwords, API keys, certificates, etc. using Azure Key Vault, you can easily create and manage encryption keys to encrypt your data. Azure Key Vault can also be used to manage certificates for all your resources. In this article, you'll learn how to:
Create an Azure Key Vault.
Configure your Key Vault permissions.
Create a new service connection.
Query for secrets from your Azure Pipeline.
Prerequisites
An Azure DevOps organization. Create one for free if you don't already have one.
Your own project. Create a project if you don't already have one.
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.
First, set your default region and Azure subscription:
Set default subscription:
Azure CLI
az account set --subscription<your_subscription_name_or_subscription_ID>
Set default region:
Azure CLI
az config set defaults.location=<your_region>
Create a new resource group to host your Azure Key Vault. A resource group is a container that holds related resources for an Azure solution:
Azure CLI
az group create --name<your-resource-group>
Create a new Azure Key Vault:
Azure CLI
az keyvault create \
--name<your-key-vault-name> \
--resource-group<your-resource-group>
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 values to use in upcoming steps.
Navigate to Settings > Properties, and copy your managed identity's Tenant ID value for later use.
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 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 policies, 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 to create a new service connection.
Select Azure Resource Manager, then select Next.
For 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 containing your managed identity.
Resource group for managed identity: Select the resource group hosting your managed identity.
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 to limit managed identity access to one resource group.
For Step 3: Service connection details:
Service connection name: Provide a name for your service connection.
Service Management Reference: (Optional) Context information from an ITSM database.
Description: (Optional) Add a description.
In Security, select the Grant access permission to all pipelines checkbox to allow all pipelines to use this service connection. If you don't select this option, you must manually grant access to each pipeline that uses this service connection.
Select Save to validate and create the service connection.
Create a service principal
In this step, we will create a new service principal in Azure, enabling us to query our Azure Key Vault from Azure Pipelines.
Navigate to Azure portal, then select the >_ icon from the menu bar to open the Cloud Shell.
Select PowerShell or leave it as Bash based on your preference.
Run the following command to create a new service principal:
Azure
az ad sp create-for-rbac --name YOUR_SERVICE_PRINCIPAL_NAME
Your output should match the example below. Be sure to copy the output of your command, as you will need it to create the service connection in the upcoming step.
Sign in to your Azure DevOps organization, and then navigate to your project.
Select Project settings, and then select Service connections.
Select New service connection, select Azure Resource Manager, and then select Next.
Select Service principal (manual), and then select Next.
For Identity Type, select App registration or managed identity (manual) from the dropdown menu.
For Credential*, select Workload identity federation.
Provide a name for your service connection, and then select Next.
Copy the Issuer and the Subject identifier as we will need it in the next step.
Select Azure Cloud for Environment, and Subscription for the Subscription scope.
Enter your Azure Subscription ID and Subscription name.
For Authentication, paste your service principal's Application (client) ID and Directory (tenant) ID
Under Security, select the Grant access permission to all pipelines checkbox to allow all pipelines to use this service connection. If you don't select this option, you must manually grant access to each pipeline that uses this service connection.
Leave this open, you'll return to verify and save once you've (1) created the federated credential in Azure and (2) granted your service principal Read access at the subscription level.
Create a federated credential in Azure
Navigate to Azure portal, then enter your service principal's ClientID in the search bar, and then select your Application.
Under Manage, select Certificates & secrets > Federated credentials.
Select Add credential, and then for Federated credential scenario, select Other issuer.
For Issuer, paste the Issuer you copied from your service connection earlier.
For Subject identifier, paste the Subject identifier you copied from your service connection earlier.
Provide a Name for your federated credential, and then select Add when you're done.
Add role assignment to your subscription
Before you can verify the connection, you need to grant the service principal Read access at the subscription level:
Under Azure service, select Subscriptions, and then find and select your subscription.
Select Access control (IAM), and then select Add > Add role assignment.
Select Reader under the Role tab, and then select Next.
Select User, group, or service principal, and then select Select members.
In the search bar, paste your service principal's Object ID, select it, then click on the Select button.
Select Review + assign, review your settings, and then select Review + assign once more to confirm your choices and add the role assignment.
Once the role assignment is added. go back to your service connection (in Azure DevOps) to finally select Verify and Save to save your service connection.
Configure Key Vault access policies
Navigate to Azure portal, find the key vault you created earlier, and then select Access policies.
Select Create, and then under Secret permissions add the Get and List permissions, and then select Next.
Under Principal, paste your service principal's Object ID, select it and then select Next.
Select Next once more, review your settings, and then select Save when you're done.
Query and use secrets in your pipeline
Using the Azure Key Vault task we can fetch the value of our secret and use it in subsequent tasks in our pipeline. One thing to keep in mind is that secrets must be explicitly mapped to env variable as shown in the example below.
The output from the last bash command should look like this:
Secret Found! ***
Piezīme
If you want to query for multiple secrets from your Azure Key Vault, use the SecretsFilter argument to pass a comma-separated list of secret names: 'secret1, secret2'.
Pievienojieties meetup sērijai, lai kopā ar citiem izstrādātājiem un ekspertiem izveidotu mērogojamus AI risinājumus, kuru pamatā ir reālas lietošanas gadījumi.
Demonstrate the skills needed to implement security controls, maintain an organization’s security posture, and identify and remediate security vulnerabilities.