Lesson 1 of 4 Sensitive Information in Automation

Managing Secrets, Keys and Certificates with Azure Key Vault

This lesson explains how vaults are secured, and how pipelines read from them.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe Key Vault objects and protections.
  • Choose a data plane role.
  • Read secrets in Azure Pipelines.
  • Read secrets in GitHub Actions.

Key Vault fundamentals

Azure Key Vault stores three kinds of object. Secrets are values such as passwords and connection strings. Keys are cryptographic keys, which are used inside the vault, so the key material does not leave it. Certificates are X.509 certificates, and a vault can manage their lifecycle and renewal.

Centralizing these objects has clear advantages. Each access is logged, a value can be rotated in one place without changing any pipeline, and each object has versions. Protection against mistakes comes from soft delete, which is always on and holds deleted items for a retention period, and purge protection, which stops anyone from permanently deleting an item during that period.

Sort into groups

Secret, key or certificate?

Exam skills 4.2.1

Decide which kind of Key Vault object each item should be stored as.

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

Controlling access

Azure RBAC is the recommended permission model for vaults, and is the default for new vaults from API version 2026-02-01. Roles separate duties at the data plane. The older model is called access policies.

RoleAllows
Key Vault Secrets UserReading secret values
Key Vault Secrets OfficerManaging secrets
Key Vault Crypto UserUsing keys for cryptographic operations
Key Vault Certificates OfficerManaging certificates
Key Vault ReaderReading metadata, not values
Key Vault AdministratorAll data plane operations

A pipeline normally needs only Key Vault Secrets User, ideally at the level of the vault used by one environment. A separate vault for each environment and application is a sound design, because a vault is the natural boundary for access. If public access is disabled on a vault, a hosted agent cannot reach it, so a self-hosted agent or Managed DevOps Pool in the virtual network is required.

Match the pairs

Key Vault RBAC roles

Exam skills 4.2.1

Match each Key Vault role with what it allows.

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

Reading secrets in Azure Pipelines

There are two main methods.

AzureKeyVault@2 task

The AzureKeyVault@2 task downloads named secrets into pipeline variables during a run. The SecretsFilter input limits which ones, and it is better to list only those required than to use *. With RunAsPreJob: true, secrets are fetched before the job begins, so that they are available to all steps.

steps:
  - task: AzureKeyVault@2
    inputs:
      azureSubscription: sc-azure-prod
      KeyVaultName: kv-contoso-prod
      SecretsFilter: 'DbPassword,ApiKey'
      RunAsPreJob: false

  - script: ./deploy.sh
    env:
      DB_PASSWORD: $(DbPassword)

Secret variables are not passed to scripts as environment variables automatically, so they must be mapped explicitly through env, as shown. Values are masked in logs.

Key Vault-linked variable group

A variable group in the Library can be linked to a vault. An administrator authorizes a service connection, then selects which secrets to map as variables. Only names are stored in Azure DevOps, and the latest values are fetched at run time, so that a rotated secret takes effect with no pipeline change. This suits several pipelines sharing the same values.

variables:
  - group: prod-secrets        # linked to kv-contoso-prod
steps:
  - script: ./deploy.sh
    env:
      DB_PASSWORD: $(DbPassword)

The following diagram shows eight pipelines reading a password from Key Vault through one linked variable group.

One Key Vault-linked variable group for eight pipelines. The password lives in Azure Key Vault and is read at run time through the group, by an identity holding Key Vault Secrets User, so it is rotated once, in the vault, with audit logs. A plain variable group centralizes the value but has neither.
The scenario is illustrative.
Scenario questions

AzureKeyVault@2 or a linked variable group?

Exam skills 4.2.1

Choose how each pipeline should read Key Vault secrets.

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

Reading secrets in GitHub Actions

GitHub Actions has no dedicated task, and the usual pattern is to sign in with azure/login, then read values with the Azure CLI. A value should be registered with add-mask, so that it does not appear in logs, and written to $GITHUB_ENV or passed directly to a step.

- uses: azure/login@v2
  with:
    client-id: ${{ vars.AZURE_CLIENT_ID }}
    tenant-id: ${{ vars.AZURE_TENANT_ID }}
    subscription-id: ${{ vars.AZURE_SUBSCRIPTION_ID }}

- name: Read a secret
  id: vault
  run: |
    value=$(az keyvault secret show --vault-name kv-contoso-prod --name ApiKey --query value -o tsv)
    echo "::add-mask::$value"
    echo "api-key=$value" >> "$GITHUB_OUTPUT"

- run: ./deploy.sh
  env:
    API_KEY: ${{ steps.vault.outputs.api-key }}

Keys, certificates and rotation

Keys can sign artifacts, and a build can call the vault to sign without the private key ever reaching the agent. Certificates can be retrieved as secrets, and App Service can import them directly from a vault. Certificates issued through integrated providers can renew automatically.

Rotation limits the damage from a leak. Key Vault publishes events through Azure Event Grid when a secret is near expiry, so an Azure Function can create a new value and update the vault. Applications that read the latest version pick it up without redeployment. Audit reads by sending vault logs to Log Analytics with a diagnostic setting.

True or false

Keys, certificates and rotation

Exam skills 4.2.1

Check how Key Vault supports signing, certificates and rotation.

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

Rotation without pipeline changes

Eight pipelines use a database password stored as a secret variable in each pipeline. Rotating it takes a day, and it is often forgotten. What should you recommend?

Store the password in Azure Key Vault, and use a Key Vault-linked variable group across the eight pipelines, with the identity holding Key Vault Secrets User. Values are read at run time, so rotation happens once, in the vault. A plain variable group centralizes the value, but it does not provide audit logs or managed rotation. A secret in each repository's YAML would expose the value.

Check yourself

A vault has public access disabled. Why might a hosted agent fail to read it?

Show answer

The hosted agent is outside the private network, so a self-hosted agent or Managed DevOps Pool in the network is needed.

Exam Tips

Remember for the exam

  • Key Vault stores secrets, keys and certificates.
  • Key Vault Secrets User lets a pipeline read secret values.
  • Use AzureKeyVault@2 or a Key Vault-linked variable group.
  • Linked variable groups fetch current values at run time.
  • Map secrets explicitly to scripts through env.