We have a secret in Azure Key Vault ("my-top-secret") that was generated as part of an automated deployment and that looks like this:
{"primaryKey": "someKey1", "secondaryKey": "someKey2", "primaryUrl": "someUrl.net"}
We need to build a connection string dynamically using one of the keys and "primaryUrl" from the secret.
Would it be a reasonable approach to do so in our deployment yaml using dependent environment variables?
I’m very new to yaml and Azure development, but based on the documentation, I believe these would be runtime variables, so this is the syntax I came up with:
env:
- name: my-top-secret
value: my-top-secret@azurekeyvault
- name: my-top-secret-values
value: $[convertToJson(my-top-secret)]
- name: connection-string
value: "$[my-top-secret-values.primaryUrl],abortConnect=false,ssl=true,password=$[my-top-secret-values.primaryKey]"
Thank you.
2
Answers
@pikkabird
You can link the Keyvault to Azure DevOps Variable group, and extract the JSON values in a script step, such that you set your desired connection string as an environment variable, so its available for a reuse by other steps after it.
In Azure Pipelines, to access secrets from Azure Key Vault, you can configure following the steps below:
In the Azure DevOps project where you want to run the pipeline, go to Project Settings > Service connections to create an ARM connection (Azure Resource Manager service connection) if you do have one.
manual
option to create the ARM connection with the existing service principal.automatic
option. It will automatically create a new service principal into the tenant for use.Open the service principal used by the ARM connection. Remember the
Display name
andApplication (client) ID
of it. They will be used in the subsequent steps.On Azure Portal, open the Key Vault where you store the secrets. Go to Settings >
Access configuration
to enable the option "Vault access policy
".On the the Key Vault, go to Access policies to assign the service principal with the Get and List permissions for Secret Permissions. When you assigning the permissions, you can find the service principal via searching for the
Display name
of it and ensure theID
of selected service principal is also consistent with that of the one created above.In the pipeline, you can use the Azure Key Vault task (
AzureKeyVault@2
) to download specified secrets from Azure Key Vault. This task will automatically use the same names of downloaded secret to set up pipeline secret variables with the secret values. Then the subsequent tasks within the same pipeline job can use these secret variables.