Azure Key Vault — Creation and Management using Terraform and Azure DevOps — 2 of 2

In my previous article, I discussed IaC and some of the best practices around it. I used the Azure Key vault as an example to demonstrate my point. We saw various features of Terraform to create a key vault and provide RBAC (role-based access control) access to azure SPN (service principal name).
In this article, we will discuss managing key vault secrets and certificates. I prefer using Azure DevOps instead of Terraform to manage secrets and certificates. Secrets and certificates are application-specific entities, and the application deployment pipeline should handle them instead.
There are some exceptions to this approach though, and I will cover those in a different article.
Create Service Connection
The first step would be to configure the azure connection so that the Azure DevOps pipeline can perform commands on Azure resources.
- Navigate to your Azure DevOps project and click on the settings icon on the bottom left corner of the page. Click on “Pipelines -> Service connections.”
- Click on the “New Service Connection” button in the top right-hand corner and choose “Azure Resource Manager.”
- In the previous article, we provided four different roles to some SPNs to perform actions on the key vault. We need to use one of those SPNs to configure the service connection. If we use another SPN who does not have access to perform actions on the key vault, the pipeline execution will fail.
- Let’s name this connection: ‘azure-service-connection’.
Create variable group
A variable group is a container to store key-value pairs. It helps us avoid hard coding values in our pipeline.
- Click on Pipelines -> Library on the left-hand menu of the DevOps project.
- Click on ‘+ Variable Group’ to create a new group for our pipeline.
- Give a name to the variable group (azure_key_vault_variable_group)
I will be creating two variables within this variable group:
- pfxpassword — Password to import a PFX into the Azure Key Vault
- test-secret — Another secret with some dummy value.
Make sure to click on the lock icon beside the value. It will replace the value with dots so no one can read it.
Import certificates into the Azure Key Vault
Upload the certificate as a secure file.
To work on this article, I have created a test pfx using New-SelfSignedCertificate cmdlet. Once created, we will upload the PFX file as a secret file in our DevOps project.
- Click on Pipelines -> Library on the left-hand menu of the Azure DevOps project.
- Click on the secure file tab and upload the pfx file.
Add password in variables
Since it’s a PFX, I will need the password (pfxpassword) to import the certificate into the Key vault. Earlier, I have stored this value in a variable group (azure_key_vault_variable_group).
We will then add this variable group to our pipeline as shown below:
variables:
- group: “azure_key_vault_variable_group”task: DownloadSecureFile@1
Next, we will use the DownloadSecureFile task in our YAML file to download the pfx file. The task will download the pfx file on the DevOps agents.
- task: DownloadSecureFile@1
name: pfx_certificate
inputs:
secureFile: ‘pfx-certifcate.pfx’We will use the name (pfx_certificate) in future tasks to refer to this file.
task: AzureCLI@2
The last step will be to use AzureCli to import the pfx file into the Azure Key Vault.
- azureSubscription — The field will hold the name of the service connection created earlier in this article.
- To reference the pfx file, we will refer to the name of the download task i.e pfx_certificate.secureFilePath
- Read the password for importing the PFX file from the variable group azure_key_vault_variable_group by using the syntax $(pfxpassword)
- task: AzureCLI@2
inputs:
azureSubscription: ‘azure-service-connection’
scriptType: ‘bash’
scriptLocation: ‘inlineScript’
inlineScript: |
az keyvault certificate import — file $(pfx_certificate.secureFilePath) — name pfx_certificate — vault-name “azure-keyvault” — password $(pfxpassword)Add Secret into the Azure Key Vault
I will use another AzureCli task to save a secret within the key vault.
- task: AzureCLI@2
inputs:
azureSubscription: ‘azure-service-conne’
scriptType: ‘bash’
scriptLocation: ‘inlineScript’
inlineScript: |
az keyvault secret set — vault-name “azure-keyvault” — name “test-secret” — value $(test-secret) — expires “2023–12–01T07:28:38Z”



