This scenario demonstrates how you can use Azure Managed Identity to obtain a secret from Azure Key Vault from a Linux based Azure VM without the need for handling credentials.
The deployment script creates a secret in the Key Vault called supersecret
with a random value created a deployment time.
This scenario requires
- Active Azure Subscription
- PowerShell
- Ubuntu (WSL)
- Azure CLI v2.8.0
Active login to Azure CLI is assumed
- Open PowerShell prompt
- Execute
.\deploy.ps1
to deploy Azure resources
- Navigate to Azure Portal
- Go to
rg-keylessauth-dev
blade - Go to
vm-keylessauth-dev
blade - Go to
Identity
- On
System assigned
tab switchStatus
toOn
and clickSave
- Go to
vlt-keylessauth-dev
blade - Go to
Access Policies
- Click
Add Access Policy
- Select
Get
permission inSecret permissions
dropdown - Click
None selected
inSelect principal
section - Search for
vm-keylessauth-dev
, select in list and clickSelect
- Click
Add
- Execute
ssh azureuser@{publicIpAddress}
in shell to connect to running VM, replace{publicIpAdress}
with value provided by deployment script in the console
- Execute
sudo apt-get update
to updateapt
index - Execute
sudo apt-get install jq
to installjq
library for parsing JSON
- Execute
token=`curl 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://vault.azure.net' -H Metadata:true -s | jq -r '.access_token'
in SSH console to obtain access token for Azure Key Vault and store in variable - Execute
value=`curl https://vlt-keylessauth-dev.vault.azure.net/secrets/supersecret?api-version=2016-10-01 -H "Authorization: Bearer ${token}" -s | jq -r ".value"`
to obtain secret value and store in variable - Execute
echo $value
to show secret value