I want to link an Azure key vault and map vault secrets to the variable group, but I am getting the following error:
Specified Azure service connection needs to have "Get, List" secret management permissions on the selected key vault. Set these secret permissions in Azure portal or run the following commands in powershell window.
and a suggestion:
$ErrorActionPreference="Stop";Login-AzAccount -SubscriptionId
**************;$spn=(Get-AzADServicePrincipal
-SPN ****************);Set-AzKeyVaultAccessPolicy -VaultName ********** -ObjectId $spn.Id -PermissionsToSecrets get,list;
But even after giving all the access to the service principle to access the keyvault and applying the suggested solutions
I am still getting the same error.
2
Answers
From your screenshot, it appears you are using
Vault access policy
not Azure RBAC for permission model on Azure Key vault, and the permission has been already granted with secretget, list
.With same permission, the service connection works fine on Azure KeyVault task and Variable group on my side(doc here).
But the error could also happen when
firewall
restriction is setting on the Azure Key vault, even above permission granted.Hence please check if you have
firewall setting
orprivate endpoint
on azure key vault, make sure the agent ip is in the azure key vault whitelist, make sure agent can access it.Here is the script which i use to dynamically add the agent ip to the key vault whitelist.
If it’s prviate endpoint, you could set
self-hosted
agent on a machine which has same private endpoint, so it can access the key vault.Edit:
Add permission screenshots.
The service principal from service connection has
contributor
role on the key vault which is inherited:When you link key vault in variable group, the
get, List
permssion will be automatically assigned to service principal.Based on the screenshot, you have granted the enough permission to the service Principal.
The issue can be related to the firewall in Azure Key Vault.
When we access the Azure Key Vault from the Azure DevOps site -> Variable Group, it will use the Azure DevOps Public IP to access the Azure Key Vault.
We need to add the Inbound connections to the Azure Key Vault firewall. Then the Variable Group can connect to Azure Key Vault.
Here are the steps:
Step1: The IP range is related to the Organization Region of your organization.
You can check the Organization Region in Organization Settings -> Overview -> Region. Then you can find the related IP range in the doc: Inbound connections.
For example:
Step2: We can enable the option: Allow public access from specific virtual networks and IP addresses and need to add the IP range to Azure Key Vault firewall(Azure Key Vault -> Networking)
For example:
Note: You need to make sure that the Azure Key Vault can be accessed by Public IP. If you have disabled the Public IP in Azure Key Vault, the Variable Group will not be able to connect to Azure Key Vault.