Azure Key Vault: secrets, certificates, soft delete

Azure Key Vault keeps secrets, keys and certificates in one protected place, so passwords and connection strings never end up in scripts, config files or VM images. Apps read them at run time using a managed identity, and every read gets logged.

Two things catch out almost everyone. With Azure RBAC, creating a vault doesn’t give you permission to read what’s inside it, and deleting a secret doesn’t really delete it. Both are by design, and both generate tickets.

In this lab you create a vault with RBAC, store a password and a certificate, and read the password from a VM that has no stored credentials. After that you’ll hit a permission error and a soft-delete conflict, and fix both.

TimeYou needCostBy the end you can
About 45 minutesA free account and the CLIAbout 3 US cents: a small VM with a public IP for under an hour; Key Vault Standard costs USD 0.03 per 10,000 operations (Azure Retail Prices API, October 2026)Use Key Vault with RBAC and managed identities, scope access to single secrets, and recover deleted secrets

What it is, in one line

Key Vault is a locked safe for passwords, keys and certificates that apps open with their own identity.

In Simple Words

The bank locker

You keep your jewellery in a bank locker rather than under the mattress. To open it you show your ID, the bank notes down every visit, and you can let your brother open just one box instead of the whole locker. If you empty a box by mistake, the bank keeps what was in it in a back room for a few weeks so you can ask for it back, and until then nobody else can take that box number. Key Vault is the bank, the VM’s managed identity is its ID card, a role on one secret is “only this box”, and soft delete is the back room.

Engineer's View

How Key Vault works

  • Objects: secrets (any string, with versions), keys (RSA or EC, used for encryption and signing) and certificates (with policies that handle renewal).
  • Two planes: the management plane (creating the vault, setting up networking) uses Azure RBAC on the resource. The data plane (reading or writing secrets) uses either Azure RBAC data roles or the older access policies, and RBAC is the recommended model.
  • Data roles: Key Vault Administrator, Secrets Officer (manages secrets), Secrets User (reads secret values), Certificates Officer, Crypto Officer and a few more. You can assign them at vault scope or on a single secret.
  • Managed identity: a VM, function or app gets a token from the local metadata endpoint (169.254.169.254) and shows it to Key Vault, so nothing secret has to be stored on the VM.
  • Soft delete: always on. Deleted vaults and objects are kept for 7 to 90 days and can be recovered, and their names can’t be reused until they’ve been recovered or purged.
  • Purge protection: optional, and once it’s on you can’t turn it off. Nobody, not even an owner, can purge anything before the retention period ends, and production vaults should have it.

How it works, step by step

Here’s what happens when the VM reads db-password:

How a VM reads an Azure Key Vault secret with a managed identity
  1. The script on vm-kv asks the local metadata endpoint for a token for https://vault.azure.net.
  2. Microsoft Entra ID issues a token for the VM’s system-assigned identity.
  3. The script then calls the vault’s REST API with that token.
  4. Key Vault checks Azure RBAC to see whether this identity has Secrets User on this secret or on the vault, and if it doesn’t, it returns 403 ForbiddenByRbac.
  5. If it does, Key Vault returns the current version of the secret and logs the read.

No password, key file or connection string is stored on the VM. When the VM is deleted its identity goes with it, so there’s nothing to rotate.

Key Vault management plane, data plane and soft delete

And because you can scope roles to one secret, a compromised app can only read what it was given, not every secret in the vault.

Store secrets in Key Vault and read them with a managed identity

Before you run any command, double-check it

Read every command twice before you press Enter. Check the target: the system or account, the IDs, the names and the sizes. These commands are for learning and were tested in our lab, not on your system. At work, run the read-only list commands first, follow your change process, make sure a backup exists, and ask a second person to check anything that changes or deletes data. You run them at your own risk.

1Create a vault with RBAC and give yourself access

Vault names are global, so add a random suffix. Then give yourself Key Vault Administrator on the vault:

$sfx = Get-Random -Minimum 10000 -Maximum 99999
$kv = "kv-saat-$sfx"
az group create --name rg-saat-l55 --location centralindia --tags course=saatvedha-azure lesson=55 -o none
az keyvault create --resource-group rg-saat-l55 --name $kv --location centralindia --enable-rbac-authorization true --retention-days 7 --tags course=saatvedha-azure lesson=55 -o none
$kvId = az keyvault show --name $kv --query id -o tsv
$me = az ad signed-in-user show --query id -o tsv
az role assignment create --assignee-object-id $me --assignee-principal-type User --role "Key Vault Administrator" --scope $kvId -o none

Creating the vault made you its owner on the management plane, but with RBAC that doesn’t cover reading or writing secrets; the data role does that. Role assignments can take a minute or two to kick in.

2Store a password and a certificate

Generate a random password and store it, then create a self-signed certificate from the default policy:

$pw = -join ((65..90) + (97..122) + (48..57) | Get-Random -Count 20 | ForEach-Object { [char]$_ })
az keyvault secret set --vault-name $kv --name db-password --value $pw --query name -o tsv
az keyvault certificate get-default-policy | Out-File -Encoding ascii C:\azure-lab\certpolicy.json
az keyvault certificate create --vault-name $kv --name web-cert --policy '@C:\azure-lab\certpolicy.json' -o none
az keyvault certificate show --vault-name $kv --name web-cert --query "{subject:policy.x509CertificateProperties.subject, expires:attributes.expires}" -o table

You should see something like this:

db-password
Subject                Expires
---------------------  -------------------------
CN=CLIGetDefaultPolicy 2027-10-21T09:14:02+00:00

A real certificate would come from a certificate authority linked to the vault, with a policy that renews it automatically before it runs out.

3A VM with a managed identity

Create a small VM with a system-assigned identity, and give that identity read access to one secret and nothing else:

az vm create --resource-group rg-saat-l55 --name vm-kv --image Canonical:ubuntu-24_04-lts:server:latest --size Standard_B1s --public-ip-sku Standard --nsg-rule NONE --assign-identity --admin-username azureuser --generate-ssh-keys --tags course=saatvedha-azure lesson=55 -o none
$vmId = az vm show --resource-group rg-saat-l55 --name vm-kv --query identity.principalId -o tsv
az role assignment create --assignee-object-id $vmId --assignee-principal-type ServicePrincipal --role "Key Vault Secrets User" --scope "$kvId/secrets/db-password" -o none

The VM only has a public IP so that it can reach the vault’s public endpoint, and no inbound port is open. In production you’d use a private endpoint, like in Lesson 26.

4Read the secret from the VM

Save this script as C:\azure-lab\read-secret.sh. KVNAME and SECRETNAME are filled in before each run:

#!/bin/bash
# Read one Key Vault secret with the VM's managed identity (no passwords on the VM)
TOKEN=$(curl -s -H Metadata:true 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://vault.azure.net' | python3 -c 'import sys,json; print(json.load(sys.stdin)["access_token"])')
curl -s -H "Authorization: Bearer $TOKEN" 'https://KVNAME.vault.azure.net/secrets/SECRETNAME?api-version=7.4' | python3 -c 'import sys,json; d=json.load(sys.stdin); print(d.get("value") or d["error"]["code"] + " / " + d["error"]["innererror"]["code"])'

Run it for db-password, but give the role assignment a couple of minutes first:

(Get-Content C:\azure-lab\read-secret.sh -Raw) -replace 'KVNAME', $kv -replace 'SECRETNAME', 'db-password' | Set-Content C:\azure-lab\read-now.sh
az vm run-command invoke --resource-group rg-saat-l55 --name vm-kv --command-id RunShellScript --scripts '@C:\azure-lab\read-now.sh' --query "value[0].message" -o tsv

You should see something like this (your password is different):

Enable succeeded:
[stdout]
Qm7Rt2KpX9aLw4ZsN8vB

[stderr]

The value matches $pw, and the VM proved who it was with a token instead of a password.

Azure Key Vault secret role assignment for a VM managed identity
vm-kv is Key Vault Secrets User on db-password only, not the whole vault.
A VM reading a Key Vault secret with its managed identity
The VM reads the secret with a token, not a password.

✅ Checkpoint: The vault uses RBAC, holds db-password and web-cert, and vm-kv reads db-password with its managed identity.

Break It, Then Fix It

1Break it: a new secret the app can’t read

The app team adds an API key and changes the app to read it:

az keyvault secret set --vault-name $kv --name api-key --value demo-key-123 -o none
(Get-Content C:\azure-lab\read-secret.sh -Raw) -replace 'KVNAME', $kv -replace 'SECRETNAME', 'api-key' | Set-Content C:\azure-lab\read-now.sh
az vm run-command invoke --resource-group rg-saat-l55 --name vm-kv --command-id RunShellScript --scripts '@C:\azure-lab\read-now.sh' --query "value[0].message" -o tsv

You should see something like this:

[stdout]
Forbidden / ForbiddenByRbac

The vault is fine and so is the identity. The role was granted on db-password only, which is what least privilege means in practice. The full error from Key Vault also names the action it refused: Microsoft.KeyVault/vaults/secrets/getSecret/action.

To fix it, give the VM the same role on the new secret, wait a minute or two and read it again:

az role assignment create --assignee-object-id $vmId --assignee-principal-type ServicePrincipal --role "Key Vault Secrets User" --scope "$kvId/secrets/api-key" -o none
(Get-Content C:\azure-lab\read-secret.sh -Raw) -replace 'KVNAME', $kv -replace 'SECRETNAME', 'api-key' | Set-Content C:\azure-lab\read-now.sh
az vm run-command invoke --resource-group rg-saat-l55 --name vm-kv --command-id RunShellScript --scripts '@C:\azure-lab\read-now.sh' --query "value[0].message" -o tsv

The script prints demo-key-123. If an app is going to read lots of secrets, it’s simpler to give it its own vault with the role at vault scope than to add one role per secret.

2Break it: a secret that can’t be recreated

Someone deletes db-password while tidying up, then tries to put it back:

az keyvault secret delete --vault-name $kv --name db-password -o none
az keyvault secret set --vault-name $kv --name db-password --value $pw

You’ll get an error like this:

(Conflict) Secret db-password is currently in a deleted but recoverable state, and its name cannot be reused; in this state, the secret can only be recovered or purged.
Code: Conflict
Inner error: {
    "code": "ObjectIsDeletedButRecoverable"
}

This is soft delete looking after you. The secret, its versions and its value still exist in a deleted state, and recreating it would throw away that history, so Key Vault refuses.

To fix it, recover the secret and check that the VM can read it again:

az keyvault secret list-deleted --vault-name $kv --query "[].{name:name, purge:scheduledPurgeDate}" -o table
az keyvault secret recover --vault-name $kv --name db-password -o none
(Get-Content C:\azure-lab\read-secret.sh -Raw) -replace 'KVNAME', $kv -replace 'SECRETNAME', 'db-password' | Set-Content C:\azure-lab\read-now.sh
az vm run-command invoke --resource-group rg-saat-l55 --name vm-kv --command-id RunShellScript --scripts '@C:\azure-lab\read-now.sh' --query "value[0].message" -o tsv

The VM prints the original password. Recovery kept the secret’s ID, so the role assignment on it still works.

Conflict error when recreating a soft-deleted Key Vault secret
Soft delete keeps the name reserved until you recover or purge.

3Clean up

Delete the resource group, then purge the soft-deleted vault so its name is freed up:

az group delete --name rg-saat-l55 --yes
az keyvault list-deleted --query "[?name=='$kv'].name" -o tsv
az keyvault purge --name $kv

After the purge, az keyvault list-deleted doesn’t show the vault any more. Purging is allowed here because there’s no purge protection; with it, you’d have to wait out the retention days.

✅ Checkpoint: You scoped secret access to one secret, fixed a ForbiddenByRbac error, recovered a deleted secret, and removed the lab.

This happened at work

The vault that couldn’t be recreated by the pipeline

A logistics company rebuilt its test environment every night with a pipeline that deleted the resource group and deployed everything again from Bicep. Then a security review turned on purge protection for all vaults, and from that night on the build failed every time with a conflict on the vault name.

Deleting the resource group soft-deleted the vault, and with purge protection nobody could purge it for 90 days, so when the template tried to create a new vault with the same name, Azure refused. Meanwhile 90 days’ worth of soft-deleted test vaults kept piling up.

What they changed: test vaults now sit in a resource group the nightly job doesn’t delete, and the pipeline reuses the vault and only replaces the secrets. Production keeps purge protection but test doesn’t, and the policy that requires purge protection exempts the test subscription with a documented reason.

Common mistakes and troubleshooting

MistakeWhat you seeWhat to do
Assuming vault owner can read secrets403 ForbiddenByRbac for yourselfAssign a Key Vault data role, such as Secrets Officer.
Role on one secret, app needs another403 on the new secret onlyAdd the role on that secret or use a vault per app.
Recreating a deleted secretConflict, ObjectIsDeletedButRecoverableRecover it, or purge it if it truly must go.
Purge protection on test vaultsPipelines can’t reuse names for 90 daysReuse vaults or use unique names in test.
Secrets in VM images or scriptsLeaked passwords during auditsRead secrets at run time with a managed identity.
Forgetting role propagation403 right after assigningWait a few minutes and retry.

Interview questions

Click a question to open it. Each one is worded the way an interviewer would ask it, and underneath you’ll find why they ask it and one way you could answer in your own words.

Fresher level

1. What is Azure Key Vault?

Why they ask this: The basic concept.

How you might answer: It’s a managed service for storing secrets, keys and certificates and controlling who can get at them, with logging and soft delete.

2. What is soft delete?

Why they ask this: A must-know feature.

How you might answer: Deleted vaults and objects are kept for 7 to 90 days and you can recover them. For Key Vault it’s always on.

3. Secret vs key vs certificate?

Why they ask this: Basic vocabulary.

How you might answer: A secret is any string, like a password. A key is a cryptographic key that’s used inside the vault for encrypting or signing, and a certificate combines a key, a public certificate and a renewal policy.

2–5 years experience

4. An app gets 403 from Key Vault. How do you troubleshoot?

Why they ask this: A real troubleshooting path.

How you might answer: First I’d check which identity the app is using and which permission model the vault has (RBAC or access policies). Then the role assignments at vault and secret scope, any recent changes and propagation delay, and finally networking: firewall, private endpoint and DNS.

5. RBAC or access policies?

Why they ask this: They want the modern answer.

How you might answer: RBAC. It’s one model across Azure, roles can be scoped to single objects, and you can manage it with PIM and Policy. Access policies are older and only work vault-wide, and Microsoft recommends RBAC.

6. How do you rotate a database password stored in Key Vault?

Why they ask this: Operations practice.

How you might answer: I’d create a new secret version, update the database, and let the apps pick up the latest version (or a specific version through config), then disable the old version. You can automate it with Event Grid notifications or a rotation function.

Senior level

7. How would you design Key Vault for 30 applications?

Why they ask this: They want architecture.

How you might answer: One vault per app per environment, RBAC with managed identities and private endpoints. I’d turn on purge protection in production, send diagnostic logs to Log Analytics, and use Azure Policy to enforce all of that.

8. What happens to TLS certificates in Application Gateway when they renew in Key Vault?

Why they ask this: Integration depth.

How you might answer: Application Gateway points at the versionless secret ID and polls Key Vault, so it picks up the renewed certificate on its own, as long as its identity can still read the vault.

9. How do you protect against someone deleting a production vault?

Why they ask this: Security thinking.

How you might answer: Purge protection and a CanNotDelete resource lock, RBAC that limits who can delete, alerts on delete operations, and soft delete recovery written into the runbook.

Quick quiz

Q1. You created the vault but can’t set a secret. Why?

  1. Vault is full
  2. No data-plane role with RBAC
  3. Soft delete
  4. Wrong region
Show answer

B — Creating the vault doesn’t grant data access.

Q2. Least-privilege role for an app that only reads secrets?

  1. Key Vault Administrator
  2. Key Vault Secrets User
  3. Contributor
  4. Owner
Show answer

B — Secrets User reads secret values.

Q3. ObjectIsDeletedButRecoverable means?

  1. The vault is gone
  2. The secret is soft-deleted; recover or purge it
  3. The role is missing
  4. The VM is stopped
Show answer

B — Soft delete keeps the name reserved.

Q4. How does vm-kv authenticate to Key Vault?

  1. A stored password
  2. A token from its managed identity
  3. An SSH key
  4. A SAS token
Show answer

B — The metadata endpoint gives it a token.

Q5. What does purge protection prevent?

  1. Reading secrets
  2. Purging before retention ends
  3. Creating vaults
  4. Soft delete
Show answer

B — Nobody can purge early.

Quick recap

You can now keep secrets out of code and VMs, give apps only the access they need, and undo accidental deletes.

  • With RBAC, vault owners still need a data role.
  • Managed identities read secrets with tokens, not passwords.
  • Roles can be scoped to one secret; plan for new secrets.
  • Soft delete reserves names; recover or purge.
  • Use purge protection in production, not in throwaway test vaults.

Next lesson

Lesson 56: Azure NetApp Files → You learn how Azure NetApp Files capacity pools, service levels and volumes work, with a simulated lab for NFS and SMB.

Further reading: Azure Key Vault

Want to go deeper into Azure Key Vault? See Microsoft’s Provide access to Key Vault with Azure RBAC and Key Vault soft-delete overview. You can also see the full plan for this course on the Azure Cloud Administration course page.

Scroll to Top

This website uses cookies

We use a few cookies to keep this site working. If you allow it, we also use Google Analytics to count visits so we can improve our lessons. You can accept all, deny the optional ones, or choose what to allow.