Skip to main content

Rotation Service Principals Inherit Key Vault Purge

Critical severityAzure Key Vault About 20 minutes to fixGenerated 29 Jul 2026

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

Purge permission granted alongside delete in Key Vault legacy access policies, applied to the same service principal that performs automated secret rotation.

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 this block wholesale onto the rotation service principal rather than stripping it down. Compounding this, purge protection is not enabled by default on a new vault and must be turned on explicitly, so soft-delete exists but carries no immutability guarantee against 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 bypasses that window entirely. The following call is destructive and shown only to illustrate the risk, so run it only ever against a disposable test vault: Remove-AzKeyVaultSecret -VaultName <vault-name> -Name <secret-name> -InRemovedState, which Microsoft documents as permanently deleting a soft-deleted secret (it prompts for confirmation before purging), or the equivalent REST DELETE against the deletedsecrets endpoint, destroys every version of a soft-deleted secret immediately and irreversibly whenever purge protection is off. If the rotation script has a logic fault, retries against a stale secret name, or the service principal’s credential is exfiltrated, an attacker or a bad deployment can 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. There is no backup path unless the value was exported offline beforehand. Dependent workloads referencing the deleted version fail permanently, and the audit trail of prior secret versions, often relied upon for forensic reconstruction after a breach, is gone 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, which is the point: from then on, deleted objects, and the vault itself if deleted, cannot be purged by anyone 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. Before running the command, meet these preconditions: the vault owner has approved the change; the same setting is made in the vault’s infrastructure-as-code definition, so the next deployment does not drift from it; and the change is acknowledged as one that cannot be undone, and as one that keeps a deleted vault’s name 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. Separately, 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 the first step is mandatory: read 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. Longer term, migrate the vault to Azure RBAC. Once the vault uses Azure RBAC its access policies are ignored, and role assignments can take several minutes to propagate, so create and verify 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"), reserving Key Vault Administrator for break-glass access gated behind Privileged Identity Management with time-bound activation and approval. Enable the vault’s AuditEvent diagnostic logs and alert on SecretPurge, KeyPurge and CertificatePurge operations. Across the 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. Move to Deny, which blocks creating vaults without purge protection and also blocks updates to existing non-compliant vaults, only once the pipelines that create or update vaults set purge protection.

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

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.

Rotation Service Principals Inherit Key Vault Purge