Hello Prachi D
You are correct, storing SSH private keys directly on virtual machines (VMs) can pose security risks. Azure recommends storing these keys in Azure Key Vault and granting the VM's managed identity access to retrieve them.
This approach ensures that sensitive keys are centrally managed and securely accessed.
Answering your questions:
- Automated Retrieval: You can automate the retrieval of the SSH private key from Azure Key Vault during the VM's startup. By using the VM's system-assigned managed identity, the VM can authenticate to Azure Key Vault and fetch the required key without manual intervention.
- Access for Multiple Users: By storing the SSH private key in Azure Key Vault and configuring appropriate access policies, multiple users can securely retrieve the key as needed as VM can authenticate to Azure Key Vault itself
Steps:
- Create an Azure Key Vault in your Azure subscription.
- Upload your SSH private key file to Azure Key Vault as a secret named "SSH-PrivateKey".
- Enable the system-assigned managed identity for your VM. This identity allows the VM to authenticate to Azure resources without storing credentials on the VM itself.
- In Azure Key Vault, create an access policy that grants the VM's managed identity the permission to get secrets. This allows the VM to retrieve the SSH private key when needed.
- Automate Key Retrieval on the VM:
- Install Azure CLI or Use Azure SDKs: Ensure that the Azure CLI or appropriate Azure SDKs are available on the VM to interact with Azure Key Vault.
- Retrieve the Key Using a Script: Create a script that uses the VM's managed identity to authenticate to Azure Key Vault and retrieve the SSH private key. This script can be executed at startup or on-demand, depending on your requirements.
#!/bin/bash SSH_KEY=$(az keyvault secret show --name "SSH-PrivateKey" --vault-name <YourKeyVaultName> --query value -o tsv) # Use the SSH key as needed, for example, save it to a file with appropriate permissions echo "$SSH_KEY" > /home/youruser/.ssh/id_rsa chmod 600 /home/youruser/.ssh/id_rsa
https://learn.microsoft.com/en-us/azure/key-vault/secrets/quick-create-cli