Lesson 4 of 5 Azure Key Vault

Managing Keys, Secrets and Certificates

This lesson explains versions, key rotation policies, the pattern for rotating secrets, and certificate renewal.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe versions and object attributes.
  • Explain what key rotation does to encrypted data.
  • Describe an event-driven secret rotation.
  • Explain how certificates are renewed.

Versions and attributes

Every key, secret and certificate has versions. Creating a new value with the same name adds a version rather than replacing the old one, and a client can request a specific version or the latest. Attributes control when an object is usable: enabled, activation date and expiration date. Key Vault does not itself block a secret from being read after its expiry, although the API can report it and Azure Policy can require that an expiry is set. Key operations, by contrast, respect the dates.

Good lifecycle management means that nothing lives for ever and that replacing a value needs no human action. Rotating a credential often is much safer when it is automated, because a person who must do it by hand tends to postpone it.

Key rotation

A rotation policy on a key creates new versions automatically. The policy sets an expiry time and a rotation time, either after creation or before expiry, and a notification time. The minimum rotation time is 7 days from creation and 7 days from expiration, and each scheduled rotation has a small cost. The following JSON shows a policy that rotates every 18 months, notifies 30 days before expiry, and expires each version after two years.

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

Understanding what rotation does is essential. Services use envelope encryption: data is encrypted with a data encryption key, and that key is wrapped by the key encryption key in Key Vault. Rotation creates a new key version, and services then re-wrap their data encryption keys with it. The data itself is not re-encrypted. Old versions must remain enabled until re-wrapping completes, and many services pick up the new version automatically if they are configured with a versionless key URI.

Key Vault Crypto Officer can set a rotation policy. Azure Policy has a built-in definition that requires keys to have a rotation policy, and a compliance scan can take up to 24 hours.

True or false

What rotation does

Exam skills 1.2.5

Check what happens when a key rotates.

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

Secret rotation

Key Vault cannot rotate a secret by itself, because it cannot change the password on a database that it knows nothing about. The usual pattern uses an event.

  1. Event

    Key Vault publishes a SecretNearExpiry event to Azure Event Grid.

  2. Function

    An Azure Function receives the event and creates a new credential on the target service.

  3. Store

    The function writes the new value as a new version of the secret.

  4. Consume

    Applications retrieve the latest version the next time they read it.

For services that have two keys, such as Azure Storage, the function can alternate them, so that the old key keeps working while clients move to the new one. Better still, avoid the secret altogether by using a managed identity. The most secure secret is one that does not exist.

The following diagram shows the rotation pattern as a flow.

The event-driven secret rotation pattern. Key Vault publishes a SecretNearExpiry event to Azure Event Grid. An Azure Function receives it, creates a new credential on the target service and writes the new value as a new version of the secret. Applications read the latest version next time. For a service with two keys, the function alternates them so the old key keeps working while clients move.
Key Vault raises the event; a function changes the credential.
Put in order

Rotating a database password

Exam skills 1.2.5

Put the event-driven secret rotation pattern in order.

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

Certificates

A certificate object consists of three parts: a key, a secret that holds the certificate with its private key, and a public certificate. This explains why Key Vault Certificate User can read the private portion through the secret. A certificate policy sets the subject, key type, validity and renewal behaviour, such as auto-renewing at a percentage of lifetime or emailing contacts.

Key Vault can issue certificates from an integrated certificate authority, DigiCert or GlobalSign, renew them automatically, and also store certificates that were created elsewhere by importing them. Services such as Azure Application Gateway and Azure App Service can reference a vault certificate and pick up renewals automatically, which removes a common cause of outages.

Select all that apply

Key Vault certificates

Exam skills 1.2.5

Identify what is true of certificates in Key Vault.

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

Keeping track of expiry

Azure Policy offers built-in definitions that audit or deny objects without an expiry date, or with a period that is too long. Key Vault can also send certificate contacts email before expiry, and Azure Monitor can alert on vault events. Together these provide the governance layer: rotation makes change easy, and policy shows where it is missing.

The Azure CLI has commands for most of this work. The following example sets a rotation policy from a file and then rotates a key at once to test it.

az keyvault key rotation-policy update --vault-name VAULT --name KEY --value policy.json
az keyvault key rotate --vault-name VAULT --name KEY
Worked example

Rotating a key without an outage

A storage account uses a customer-managed key, and the account references a specific key version. Compliance requires rotation every year. After rotation, the old version must stay available until the storage account uses the new one. How should you configure this?

Add a rotation policy with a timeAfterCreate of one year, and change the storage account's key reference to a versionless key URI, so that the service moves to the new version automatically. Keep the old version enabled until the account has re-wrapped its keys, which happens automatically.

Disabling the old version immediately could make the account unable to unwrap its encryption key, which would cut off access to data. Re-encrypting all the data is unnecessary, because envelope encryption requires only that the data encryption key is re-wrapped.

Check yourself

Why can Key Vault rotate a key automatically, but not a database password?

Show answer

A key lives in the vault, whereas a password must also change on the database, which needs an external process such as a function.

Exam Tips

Remember for the exam

  • Rotation re-wraps data encryption keys; it does not re-encrypt data.
  • Key Vault rotates keys by policy, but secrets need Event Grid and a function.
  • Use a versionless key URI so that a service follows rotation.
  • Key Vault Certificate User can read the private portion through the secret.