Skip to main content

Rotation Service Principals Inherit Key Vault Purge

Critical severityAzure Key Vault About 20 minutes to fix

Overview

A secret-rotation identity holding purge rights can erase every version of a Key Vault secret outright, skipping the soft-delete retention period. Once triggered by a scripting error or a leaked credential, no vault backup, RBAC lockout, or incident response runbook restores what was purged.

At a glance

Symptom
A rotation script error, stale secret reference, or compromised service principal credential invokes a purge call against the deletedsecrets endpoint.
Likely cause
An application service principal used for automated secret rotation holds the purge permission on secrets alongside delete, with vault-level purge protection left disabled.
Impact
Every version of the targeted secret is destroyed instantly, bypassing the soft-delete retention period (7 to 90 days) with no backup or forensic history remaining.
Verification signal
Re-run the detection test and a controlled negative-path test.
Safe correction
Enable irreversible purge protection on the vault and restrict automation identities to access policies without purge, or a custom RBAC role without the secrets purge action (Key Vault…
Rollback or recovery
Restore the exported configuration if the new control blocks required production traffic, then narrow the policy before redeployment.

The Trap

The service principal that runs automated secret rotation is granted purge alongside delete in Key Vault legacy access policies.

The Default State

Many Terraform modules and sample az keyvault set-policy commands in circulation list secret_permissions as ["get","list","set","delete","purge"] as a single block, written for administrators rather than automation. Engineers building CI/CD pipelines paste the whole block onto the rotation service principal instead of trimming it. On top of that, purge protection is not enabled by default on a new vault and must be turned on explicitly, so soft-delete is there but does nothing to stop an identity that already holds the purge action.

The Blast Radius

Soft-delete is meant to give a recovery window after a secret is deleted; Microsoft documents the retention period as a value from 7 to 90 days, 90 by default, that can only be set when the vault is created and cannot be changed afterwards. Purge permission skips that window. The call below is destructive and shown only to illustrate the risk, so only ever run it against a disposable test vault. Running Remove-AzKeyVaultSecret -VaultName <vault-name> -Name <secret-name> -InRemovedState, which Microsoft documents as permanently deleting a soft-deleted secret (it asks for confirmation first), or sending the equivalent REST DELETE to the deletedsecrets endpoint, destroys every version of a soft-deleted secret immediately and irreversibly whenever purge protection is off. A logic fault in the rotation script, a retry against a stale secret name or a stolen service principal credential is enough: an attacker or a bad deployment can then permanently purge soft-deleted secrets, connection strings included, with one authenticated call per secret. Keys and certificates have their own purge operations, Remove-AzKeyVaultKey -InRemovedState and Remove-AzKeyVaultCertificate -InRemovedState, controlled by the key and certificate purge permissions, so the same exposure applies wherever those are granted. Unless someone exported the value offline beforehand, there is nothing to restore from. Workloads that reference the deleted version fail for good, and the history of earlier secret versions, which investigators often rely on to reconstruct a breach, goes with it.

The Lead Mechanic Fix

Enable purge protection on the vault itself so deleted objects cannot be purged before their retention period ends. Microsoft documents that purge protection cannot be disabled once enabled. That is deliberate: from then on nobody can purge deleted objects, or the vault itself if it is deleted, until the retention period ends. Because the soft-delete retention period set when the vault was created cannot be changed afterwards, enabling purge protection locks the vault into that period permanently. Three things must be true before you run the command. The vault owner has approved the change. The same setting is in the vault’s infrastructure-as-code definition, so the next deployment does not drift from it. And everyone involved accepts that the change cannot be undone and that a deleted vault’s name stays reserved until the retention period expires. Confirm the retention period with az keyvault show --name <vault-name> --query properties.softDeleteRetentionInDays and test on a non-production vault first. Then run az keyvault update --name <vault-name> --enable-purge-protection true.

The second change is to remove purge from every application-facing access policy. For an existing entry, az keyvault set-policy replaces that principal’s secret permission list with exactly the list you pass, so always start by reading the principal’s current permissions with az keyvault show --name <vault-name> --query "properties.accessPolicies[?objectId=='<object-id>']". Build the new --secret-permissions list from that output: drop purge and keep recover, backup or restore if the rotation needs them. Only then run set-policy. For a principal whose output shows get, list, set, delete and purge, az keyvault set-policy --name <vault-name> --spn <app-id> --secret-permissions get list set delete leaves it without secret purge. Key and certificate lists are left as they were, so check those too, and confirm the result with az keyvault show --name <vault-name> --query properties.accessPolicies.

In the longer run, move the vault to Azure RBAC. Once it uses Azure RBAC its access policies are ignored, and role assignments can take several minutes to propagate, so create and check every role assignment first and rehearse on a non-production vault. Then assign automation identities a custom role without Microsoft.KeyVault/vaults/secrets/purge/action (the documented definition of the built-in Key Vault Secrets Officer role grants the data action Microsoft.KeyVault/vaults/secrets/* with no NotDataActions, so it includes Microsoft.KeyVault/vaults/secrets/purge/action; check the definition in your tenant with az role definition list --name "Key Vault Secrets Officer"). Keep Key Vault Administrator for break-glass access behind Privileged Identity Management, with time-bound activation and approval.

Turn on the vault’s AuditEvent diagnostic logs and alert on SecretPurge, KeyPurge and CertificatePurge operations. For the wider estate, assign the built-in Azure Policy "Key vaults should have deletion protection enabled" with the Audit effect first, scoped to a test subscription or management group, and review the compliance results. Switch to Deny only once the pipelines that create or update vaults set purge protection, because Deny blocks creating vaults without purge protection and also blocks updates to existing non-compliant vaults.

3

Apply the safer control

Before you change production

Confirm the affected scope, export the current configuration, and test the replacement control in a non-production environment first.

Fix commands and configuration

az keyvault show --name <vault-name> --query properties.softDeleteRetentionInDays
az keyvault update --name <vault-name> --enable-purge-protection true
purge
4

Verify, roll back or escalate

Verify

Re-run the detection test and a controlled negative-path test. Confirm the unsafe behaviour is blocked while approved traffic still succeeds.

Rollback

Restore the exported configuration if the new control blocks required production traffic, then narrow the policy before redeployment.

Escalate

Escalate when the blast radius is uncertain, the control cannot be tested safely, or remediation requires an outage or security exception.

The KBY Brief

Fixed? Get the next one before it hits you: The KBY Brief

Practical fixes for the systems you run, by email about twice a week. Unsubscribe at any time.

About this page

Credited to

Dates

Published

Last updated

Corrections

  • : Corrected during a review of published articles. Details
  • : Corrected during a review of published articles. Details
  • : Corrected during a review of published articles. Details
  • : Corrected during a review of published articles. Details
  • : Corrected during a review of published articles. Details

Was this useful?

Reports go to the site owner and are used to correct pages. Corrections are listed at /corrections.

After remediation

Further reading stays below the corrective workflow and is selected by platform, category and shared technical keywords.

More from KBY that goes with this config trap.

Rotation Service Principals Inherit Key Vault Purge