Last active
May 12, 2021 19:11
-
-
Save thomast1906/e73fc8793d3993ff85c7255ed6edc427 to your computer and use it in GitHub Desktop.
Copy Azure KeyVault secrets to another KeyVault using Azure CLI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
SOURCE_KEYVAULT="<SOURCE KEYVAULT THAT YOU WANT TO COPY SECRETS FROM>" | |
DESTINATION_KEYVAULT="<DESTINATION KEYVAULT THAT YOU WILL COPY SECRETS TO>" | |
SECRETS+=($(az keyvault secret list --vault-name $SOURCE_KEYVAULT --query "[].id" -o tsv)) | |
for SECRET in "${SECRETS[@]}"; do | |
SECRETNAME=$(echo "$SECRET" | sed 's|.*/||') | |
SECRET_CHECK=$(az keyvault secret list --vault-name $DESTINATION_KEYVAULT --query "[?name=='$SECRETNAME']" -o tsv) | |
if [ -n "$SECRET_CHECK" ] | |
then | |
echo "A secret with name $SECRETNAME already exists in $DESTINATION_KEYVAULT" | |
else | |
echo "Copying $SECRETNAME to KeyVault: $DESTINATION_KEYVAULT" | |
SECRET=$(az keyvault secret show --vault-name $SOURCE_KEYVAULT -n $SECRETNAME --query "value" -o tsv) | |
az keyvault secret set --vault-name $DESTINATION_KEYVAULT -n $SECRETNAME --value "$SECRET" >/dev/null | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Blog post available here https://thomasthornton.cloud/2021/05/08/copy-azure-keyvault-secrets-to-another-keyvault-using-azure-cli/