How to connect Azure DevOps Pipelines Variables to Azure Key Vault?
Variable groups in Azure DevOps provide a centralized and reusable way to manage these variables across multiple pipelines or stages within a pipeline.
Here are the key advantages of using variable groups:
Reuse variables across pipelines or stages, which reduces repetition and makes maintenance easier.
Update variable values in one place, which automatically applies the change to all pipelines or stages using that variable group. This makes maintenance simpler and less error-prone.
Keep variables consistent across pipelines, which avoids discrepancies that may happen when handling variables in each pipeline separately.
Advantages of storing credentials in Azure Key Vault:
Better Security: Azure Key Vault offers a secure and centralized way to store sensitive data. You can use Key Vault to keep sensitive information safe and hidden from the pipeline variables.
Access Management: Azure Key Vault lets you control access to stored variables, so you can set permissions for different users or applications.
While there are some limitations to consider, such as inflexible settable variables and stable Key Vault values, the benefits of migrating to Azure Key Vault generally outweigh these drawbacks.
Steps involved in migrating Azure DevOps Pipeline Variables to Azure Key Vault
Step 1: Create an Azure Key Vault in Azure Portal
Step 2: Create Secrets in Azure Key Vault
Step 3: Create a service connection in Azure DevOps
Step 4: Create Variable Groups in Azure DevOps
Provision access on the azure KV for service principal (App ID)
Step 5: Link the Azure Key Vault to variable group by ensuring the appropriate permissions on the service connection
Step 6: Link your Variable Group to the Pipeline
Step-by-Step elaborate Guide: Migrating Azure DevOps Pipeline Variables to Azure Key Vault
Step 1: Create an Azure Key Vault
Select Go to resource when the deployment of your new resource is completed.
You might face a problem while authorizing the Key Vault through a service connection. Here’s how you can resolve it:
Problem: During the authorization process, you may encounter an error indicating that the service connection lacks “list and get” permissions for the Key Vault.
Solution: Switch the permission mode to use access policies by accessing the Key Vault’s details page in the Azure Portal, clicking on “Access Configuration,” and switch to “Vault Access Policy” and apply. (RBAC will take care of it)
Select first option from the below page:
Step 2: Create Secrets in Azure Key Vault
With the proper permissions in place, create the corresponding secrets within the Azure Key Vault. For each variable in the pipeline, create a secret in the Key Vault with the same name and the respective value.
Step 3: Create service connection in Azure DevOps
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, and then select Next.
Select Service principal (manual), and then select Next.
Select Azure Cloud for Environment and Subscription for the Scope Level, then enter your Subscription Id and your Subscription Name.
Fill out the following fields with the information you obtained when creating the service principal, and then select Verify when you’re done:
Service Principal Id: Your service principal appId.
Service Principal key: Your service principal password.
Tenant ID: Your service principal tenant.
Once the verification has succeeded, provide a name and description (optional) for your service connection, and then check the Grant access permission to all pipelines checkbox.
Select Verify and save when you’re done.
2 ways to create service connection –
Option 1: APPid created randomly – display name is same – app id is different
Option 2: create service principal first- first create app id and use it in service connection – have unique ID name in ADO and Azure portal – to be used
Step 4: Create Variable Groups in Azure DevOps (To link to Azure Key Vault in following steps)
Open the variables tab inside Pipelines->Library and choose the new variable groups
Add variable group name and description
Select check box for ‘Allow access to pipelines’ and ‘Link secrets from AzKeyVault as variables’
Select Azure subscription
Link secrets from an Azure key vault
In the Variable groups page, enable Link secrets from an Azure key vault as variables. You’ll need an existing key vault containing your secrets.
To link your Azure Key Vault to the variable group, ensure that you have the appropriate permissions on the service connection. Service connections provide the necessary credentials to access resources like Azure Key Vault. Grant the necessary permissions by configuring the access policies in the Azure Key Vault settings.
Step 5: Link your Variable Group to the Pipeline
To utilize the migrated variables from Azure Key Vault, link the variable group to your pipeline:
Go to the variables tab on your pipeline
Once you link the variable group to your pipeline, it will look like this:
Variable groups in Azure DevOps provide a centralized and reusable way to manage these variables across multiple pipelines or stages within a pipeline.
Here are the key advantages of using variable groups:
Reuse variables across pipelines or stages, which reduces repetition and makes maintenance easier.
Update variable values in one place, which automatically applies the change to all pipelines or stages using that variable group. This makes maintenance simpler and less error-prone.
Keep variables consistent across pipelines, which avoids discrepancies that may happen when handling variables in each pipeline separately.
Advantages of storing credentials in Azure Key Vault:
Better Security: Azure Key Vault offers a secure and centralized way to store sensitive data. You can use Key Vault to keep sensitive information safe and hidden from the pipeline variables.
Access Management: Azure Key Vault lets you control access to stored variables, so you can set permissions for different users or applications.
While there are some limitations to consider, such as inflexible settable variables and stable Key Vault values, the benefits of migrating to Azure Key Vault generally outweigh these drawbacks.
Steps involved in migrating Azure DevOps Pipeline Variables to Azure Key Vault
Step 1: Create an Azure Key Vault in Azure Portal
Step 2: Create Secrets in Azure Key Vault
Step 3: Create a service connection in Azure DevOps
Step 4: Create Variable Groups in Azure DevOps
Provision access on the azure KV for service principal (App ID)
Step 5: Link the Azure Key Vault to variable group by ensuring the appropriate permissions on the service connection
Step 6: Link your Variable Group to the Pipeline
Step-by-Step elaborate Guide: Migrating Azure DevOps Pipeline Variables to Azure Key Vault
Step 1: Create an Azure Key Vault
Select Go to resource when the deployment of your new resource is completed.
https://dev.azure.com/MSComAnalytics/DigitalStoresAnalytics/_wiki/wikis/DigitalStoresAnalytics.wiki/8379/keyvault-secret-tagging-checklist
You might face a problem while authorizing the Key Vault through a service connection. Here’s how you can resolve it:
Problem: During the authorization process, you may encounter an error indicating that the service connection lacks “list and get” permissions for the Key Vault.
Solution: Switch the permission mode to use access policies by accessing the Key Vault’s details page in the Azure Portal, clicking on “Access Configuration,” and switch to “Vault Access Policy” and apply. (RBAC will take care of it)
Select first option from the below page:
Step 2: Create Secrets in Azure Key Vault
https://learn.microsoft.com/en-us/azure/devops/pipelines/process/set-secret-variables?view=azure-devops&source=recommendations&tabs=yaml%2Cbash
With the proper permissions in place, create the corresponding secrets within the Azure Key Vault. For each variable in the pipeline, create a secret in the Key Vault with the same name and the respective value.
Step 3: Create service connection in Azure DevOps
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, and then select Next.
Select Service principal (manual), and then select Next.
Select Azure Cloud for Environment and Subscription for the Scope Level, then enter your Subscription Id and your Subscription Name.
Fill out the following fields with the information you obtained when creating the service principal, and then select Verify when you’re done:
Service Principal Id: Your service principal appId.
Service Principal key: Your service principal password.
Tenant ID: Your service principal tenant.
Once the verification has succeeded, provide a name and description (optional) for your service connection, and then check the Grant access permission to all pipelines checkbox.
Select Verify and save when you’re done.
https://learn.microsoft.com/en-us/azure/devops/pipelines/library/service-endpoints?view=azure-devops&tabs=yaml
2 ways to create service connection –
Option 1: APPid created randomly – display name is same – app id is different
Option 2: create service principal first- first create app id and use it in service connection – have unique ID name in ADO and Azure portal – to be used
Step 4: Create Variable Groups in Azure DevOps (To link to Azure Key Vault in following steps)
Open the variables tab inside Pipelines->Library and choose the new variable groups
Add variable group name and description
Select check box for ‘Allow access to pipelines’ and ‘Link secrets from AzKeyVault as variables’
Select Azure subscription
Link secrets from an Azure key vault
In the Variable groups page, enable Link secrets from an Azure key vault as variables. You’ll need an existing key vault containing your secrets.
To link your Azure Key Vault to the variable group, ensure that you have the appropriate permissions on the service connection. Service connections provide the necessary credentials to access resources like Azure Key Vault. Grant the necessary permissions by configuring the access policies in the Azure Key Vault settings.
Step 5: Link your Variable Group to the Pipeline
To utilize the migrated variables from Azure Key Vault, link the variable group to your pipeline:
Go to the variables tab on your pipeline
Once you link the variable group to your pipeline, it will look like this: