My requirement is to automate the creation of secrets in Keyvault without exposing the values of these secrets during the pipeline execution(AzureDevops Server).
tried, different options such as having input paramaters (it will display the values when its perform initialization) , runtime variables etc (list of variables, or dynamic creation as per input is not possible), but couldn’t fulfil our requirements.
Finally planned to use the below script to read values from library group and create the values in key vault. But when we are masking the secret values in Library group (by locking), the secrets are getting created with null value only. So is there any way to retrieve the values secret variables and create them in key vault only if the secret value is different(if existing)
-bash: |
az login --service-principal --username $(spid) --password $(spsecret) --tenant $(tenantid)
az account set --subscription ${{ variables.subscription }}
az config set extension.use_dynamic_install=yes_without_prompt
groupID=`az pipelines variable-group list -p myproject --group-name ${{ environment }}-${{ parameters.myapp}}-kv-secret --query '[].id' -o tsv`
echo "grouup id is $groupID"
variables=$(az pipelines variable-group variable list -p myproject --group-id $groupID --output json)
echo "$variables" | jq -r 'keys[] as $k | "($k)=(.[$k].value)"' | while read variable; do
name=$(echo "$variable" | cut -d= -f1)
value=$(echo "$variable" | cut -d= -f2)
if ! az keyvault secret show --vault-name "$(${{ variables.podkv }})" --name "$name" &>/dev/null; then
expiryDate=$(date -u -d '+2 years' '+%Y-%m-%dT%H:%MZ')
az keyvault secret set --vault-name "$(${{ variables.podkv }})" --name "$name" --value "$value" --expires $expiryDate
secretValue=$(az keyvault secret show --vault-name "$(${{ variables.podkv }})" --name "$name" --query value --output tsv)
if [[ "$secretValue" != "$value" ]]; then
echo "Failed to create secret $secretName in Key Vault $keyVaultName"
exit 1
fi
if [[ "$secretValue" == "$value" ]]; then
echo "Created secret $secretName in Key Vault $keyVaultName, hence cleaning the variable from the keyvault"
az pipelines variable-group variable delete --group-id $groupID --name "$name --yes"
fi
fi
done
2
Answers
When using Azure CLI you can use the
--output none
output format to keep sensitive information from being displayed in your console.Example:
See None output format for more details.
I have just test using
variable group
it works well even with locked maskmy-pipline.yaml
What’s in azure devops pipeline variable group
My result: