Lesson 3 of 3 Managing Secrets with Azure Key Vault

Rotating Secrets with Key Vault

This lesson explains how versions and references support rotation, event-driven rotation, the single- and dual-credential patterns, and automatic rotation of keys and certificates.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain how version-less references follow rotation.
  • Describe event-driven rotation with SecretNearExpiry.
  • Compare single and dual credential rotation.
  • Describe automatic rotation policies for keys.

Why and how secrets rotate

A leaked secret is useful to an attacker until it is changed. Rotation limits that window by replacing secrets regularly, for example every 60 or 90 days, and immediately after a suspected exposure. Rotation has two halves that must stay in step: the target service (the database, the third-party API) must accept a new credential, and Key Vault must hold it as a new version that applications pick up.

Version-less references make the second half automatic. Applications that read the current version, through the SDK with a cache refresh or through a version-less platform reference, receive the new value without a code or configuration change. App Service and Functions re-fetch version-less references periodically, within 24 hours, or immediately when the app restarts; Container Apps replicas read secret values at start-up, so a revision restart picks up a new value. References pinned to a version never rotate.

Sort into groups

Will it pick up the new version?

Exam skills 4.1.1

Decide whether each consumer picks up a rotated secret without a configuration change.

Log in to try this activity Interactive activities are free with an account.

Event-driven rotation

Key Vault publishes events to Event Grid for changes to its objects. For rotation, the most useful are Microsoft.KeyVault.SecretNearExpiry, published 30 days before a secret version's expiry date, SecretExpired, and SecretNewVersionCreated. An automated rotation subscribes a function to the near-expiry event.

  1. Expiry approaches

    Key Vault publishes SecretNearExpiry to Event Grid 30 days before the version expires.

  2. Function runs

    An Event Grid-triggered function receives the event with the vault and secret name.

  3. New credential

    The function creates or regenerates a credential in the target service.

  4. New version

    The function stores the credential as a new secret version with a new expiry date.

  5. Applications follow

    Version-less references and cache refreshes pick up the new version.

The rotation function's identity needs Key Vault Secrets Officer on the vault and permission to change credentials in the target service. Monitoring should alert if a near-expiry event is not followed by a new version, which indicates a failed rotation.

Put in order

Automated secret rotation

Exam skills 4.1.1 · 3.1.2

Put the steps of event-driven secret rotation in order.

Log in to try this activity Interactive activities are free with an account.

Single and dual credentials

Rotating a credential that applications are actively using risks an outage: between changing the password in the database and the applications reading the new version, connections fail. Two patterns address this.

Single credential
Target service hasOne credential per user
RotationChange the credential, then store the new value
Risk during rotationA short window of failed authentication
Choose when Services with one credential and tolerant clients
Dual credential
Target service hasTwo valid credentials at once (such as key1 and key2, or two users)
RotationRegenerate the credential not in use, store it, switch applications to it
Risk during rotationNo window; the old credential stays valid until the next rotation
Choose when Storage account keys, services with two keys, production databases

Azure Storage accounts, many Azure AI services and some external APIs provide two keys precisely for this. The rotation alternates: this cycle regenerates key2 while applications use key1, stores key2 as the new secret version, and the next cycle regenerates key1.

The following diagram shows the rotation sequence of a storage account key and how its two keys alternate.

The dual-credential rotation of a storage account key. When the secret nears its 60-day expiry, Key Vault publishes SecretNearExpiry; an Event Grid-triggered function regenerates the key that is not in use and stores it as a new secret version. The App Service app picks it up through its version-less reference, while the old key stays valid until the next cycle.
A tag on the secret records which key is current.
Scenario questions

Single or dual credential?

Exam skills 4.1.1

Choose the rotation approach for each target service.

Log in to try this activity Interactive activities are free with an account.

Automatic rotation for keys and certificates

Cryptographic keys in Key Vault support an automatic rotation policy, which creates a new key version on a schedule, such as 90 days after creation or a set time before expiry, and can send a near-expiry notification. Services that use customer-managed keys and reference the key without a version pick up the new version automatically.

{
  "lifetimeActions": [
    { "trigger": { "timeAfterCreate": "P90D" }, "action": { "type": "Rotate" } },
    { "trigger": { "timeBeforeExpiry": "P30D" }, "action": { "type": "Notify" } }
  ],
  "attributes": { "expiryTime": "P1Y" }
}

Certificates have their own lifecycle policy: certificates from an integrated certificate authority can renew automatically at a percentage of their lifetime, and services such as App Service can import renewed certificates from Key Vault. Secrets have no built-in automatic rotation, because Key Vault cannot change a password in an external system; that is why secret rotation uses events and functions.

True or false

Automatic rotation

Exam skills 4.1.1

Decide whether each statement about automatic rotation is true.

Log in to try this activity Interactive activities are free with an account.
Worked example

Rotating a storage key without downtime

A legacy component must authenticate to a storage account with an account key, which is stored in Key Vault and read through a version-less App Service Key Vault reference. Security requires rotation every 60 days with no failed requests during rotation.

The design uses the dual-credential pattern with storage's two keys. The secret has an expiry date of 60 days, so Key Vault publishes SecretNearExpiry; an Event Grid-triggered function regenerates the key that is not in use, stores it as a new secret version with a new expiry, and records which key is current in a tag. The App Service app picks up the new version through its version-less reference, and the old key remains valid until the next cycle, so no request fails. Where possible, the longer-term fix is to move the component to a managed identity and remove the key.

Regenerating the key currently in use would break requests until the app refreshed. A reference pinned to a version would never pick up the new key. A key rotation policy applies to Key Vault keys, not to secrets that hold external credentials.

Check yourself

How long before a secret version's expiry does Key Vault publish the SecretNearExpiry event?

Show answer

30 days before the expiry date. A rotation function subscribed through Event Grid can then create the new version.

Exam Tips

Remember for the exam

  • Rotation updates the target service and stores a new secret version; version-less references follow it.
  • SecretNearExpiry is published to Event Grid 30 days before a version expires.
  • Dual credentials (two keys or users) let rotation happen with no authentication failures.
  • Keys support automatic rotation policies; secrets need event-driven rotation functions.