Restore Key Vault key and secret for encrypted VMs using Azure Backup
Article
This article talks about using Azure VM Backup to perform restore of encrypted Azure VMs, if your key and secret don't exist in the key vault. These steps can also be used if you want to maintain a separate copy of the key (Key Encryption Key) and secret (BitLocker Encryption Key) for the restored VM.
Configure Azure Key Vault – Ensure that key vault to which keys and secrets need to be restored is already present. Refer to the article Get Started with Azure Key Vault for details about key vault management.
Restore disk - Ensure that you've triggered the restore job for restoring disks for encrypted VM using PowerShell steps. This is because this job generates a JSON file in your storage account containing keys and secrets for the encrypted VM to be restored.
Get key and secret from Azure Backup
Note
Once disk has been restored for the encrypted VM, ensure that:
Once the JSON file is generated in the destination path mentioned above, generate key blob file from the JSON and feed it to restore key cmdlet to put the key (KEK) back in the key vault.
Use the JSON file generated above to get secret name and value and feed it to set secret cmdlet to put the secret (BEK) back in the key vault. Use these cmdlets if your VM is encrypted using BEK and KEK.
Use these cmdlets if your Windows VM is encrypted using BEK and KEK.
Use the JSON file generated above to get secret name and value and feed it to set secret cmdlet to put the secret (BEK) back in the key vault. Use these cmdlets if your VM is encrypted using BEK only.
The value for $secretname can be obtained by referring to the output of $encryptionObject.OsDiskKeyAndSecretDetails.SecretUrl and using text after secrets/ For example, the output secret URL is https://keyvaultname.vault.azure.net/secrets/B3284AAA-DAAA-4AAA-B393-60CAA848AAAA/xx000000xx0849999f3xx30000003163 and secret name is B3284AAA-DAAA-4AAA-B393-60CAA848AAAA
The value of the tag DiskEncryptionKeyFileName is the same as the secret name.
Create virtual machine from restored disk
If you've backed up encrypted VM using Azure VM Backup, the PowerShell cmdlets mentioned above help you restore key and secret back to the key vault. After restoring them, refer to the article Manage backup and restore of Azure VMs using PowerShell to create encrypted VMs from restored disk, key, and secret.
Legacy approach
The approach mentioned above would work for all the recovery points. However, the older approach of getting key and secret information from recovery point, would be valid for recovery points older than July 11, 2017 for VMs encrypted using BEK and KEK. Once restore disk job is complete for encrypted VM using PowerShell steps, ensure that $rp is populated with a valid value.
Restore key (legacy approach)
Use the following cmdlets to get key (KEK) information from recovery point and feed it to restore key cmdlet to put it back in the key vault.
Value for $secretname can be obtained by referring to the output of $rp1.KeyAndSecretDetails.SecretUrl and using text after secrets/ For example, the output secret URL is https://keyvaultname.vault.azure.net/secrets/B3284AAA-DAAA-4AAA-B393-60CAA848AAAA/xx000000xx0849999f3xx30000003163 and secret name is B3284AAA-DAAA-4AAA-B393-60CAA848AAAA
Value of the tag DiskEncryptionKeyFileName is same as secret name.
Value for DiskEncryptionKeyEncryptionKeyURL can be obtained from key vault after restoring the keys back and using Get-AzKeyVaultKey cmdlet