Last active
January 18, 2024 17:43
-
-
Save amalgjose/e86cf572bb1fc5c3f8027dacc6a21b11 to your computer and use it in GitHub Desktop.
Python program to migrate secrets from one Azure Keyvault to another
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
# pip install azure-keyvault-secrets | |
# pip install azure-identity | |
from azure.keyvault.secrets import SecretClient | |
from azure.identity import ClientSecretCredential | |
source_vault_url = "https://<sourcekeyvault>.vault.azure.net" | |
destination_vault_url = "https://<destkeyvault>.vault.azure.net/" | |
# Get the below details from Service Principle | |
AZURE_TENANT_ID = '<string>' | |
AZURE_CLIENT_ID = '<string>' | |
AZURE_CLIENT_SECRET = '<string>' | |
credentials = ClientSecretCredential( | |
client_id=AZURE_CLIENT_ID, | |
client_secret=AZURE_CLIENT_SECRET, | |
tenant_id=AZURE_TENANT_ID) | |
source_client = SecretClient(vault_url=source_vault_url, credential=credentials) | |
destination_client = SecretClient(vault_url=destination_vault_url, credential=credentials) | |
# Update this list with the list of secret keys to migrate | |
key_list = ['keyA', 'keyB', 'keyC'] | |
# Read secrets from the source keyvault | |
credentials = {} | |
for key in key_list : | |
credentials[key] = source_client.get_secret(key).value | |
# Write the secrets in the target keyvault | |
for secret_key, secret_value in credentials.items(): | |
print(f"Creating a secret called '{secret_key}' with the value '{secret_value}' ...") | |
destination_client.set_secret(secret_key, secret_value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment