azure api management,

How to Reference Key Vault Secrets in Azure API Management

Sven Malvik Sven Malvik Connect Feb 05, 2021 · 3 mins read
How to Reference Key Vault Secrets in Azure API Management

In an enterprise, an Azure API Management instance is often shared by many teams and many developers. The developers may all have access to all secrets stored in named values for using in policies for JWT token validation or because for sending passwords in authentication headers. It’s therefore best practice to store secrets in Azure Key Vault and not in named values. Azure API Management can then use its Managed Service Identity to access the secrets from this Azure Key Vault by referencing secrets. This post will show how to set a secret as the value in a response header.

Enable Managed Identity in APIM

In Azure, an AD identity can be assigned to a managed resource such as a Azure Function, App Service and also an instance of Azure API Management. A Resource with an identity has the capabilities to work with other resources that leverage Azure AD for authentication. We can easily enable a managed system identity (MSI) in APIM.

Enable managed system identity in Azure API ManagementEnable managed system identity in Azure API Management

Key Vault

To demonstrate how to access a secret from APIM, let’s first create a secret mysecret with a value secretaccesscode.

Create secret in Azure Key VaultCreate secret in Azure Key Vault

Just to show you the value of the secret, here it’s visible.

Show secret value in Azure Key VaultShow secret value in Azure Key Vault

We need now to tell our Key Vault that our apim instance has permission to Get mysecret. We do this by adding a new access policy as shown below.

Add access policy in Azure Key VaultAdd access policy in Azure Key Vault

Set the secret permission to Get and select the identity of your Azure API Management instance.

Configure access policy in Azure Key VaultConfigure access policy in Azure Key Vault

Now we can see that we set up a new access policy. Remember to save :).

Save access policy in Azure Key VaultSave access policy in Azure Key Vault

Referencing a Key Vault Key in Azure API Management

Add a new named value in your APIM instance and select the type Key Vault. A new pane opens where you can select the key vault and secret you want to reference. In my case it’s mysecret.

Reference secret in apim named valuesReference secret in apim named values

If everything went well you will see a green Success icon.

Referenced secret in apim named valuesReferenced secret in apim named values

Let’s now select a random API operation and open the policy so we can add a response header.

Edit API policyEdit API policy

I add a basic header with the named value that I called secret-from-kv.

Add customer header with secret value to API policyAdd customer header with secret value to API policy

Finally, we test this endpoint and can see the value in the response.

Retrieve customer header with secret value from APIRetrieve customer header with secret value from API

Developers that have access to this instance may be able to debug a policy, hence retrieve the secret.

Conclusion

Referencing secrets from Azure Key Vaults in Named Values was introduced December 2020. That means that we don’t need to follow tutorials from many blog posts that were written before.

Use named values in Azure API Management policies - Key vault secrets

Join Newsletter
Get the latest updates right in your inbox. I never spam!
Sven Malvik
Written by Sven Malvik

Latest Stories

How to Secure Azure Functions App with Azure API Management

How to use an Azure Managed Identity to authenticate against an Azure Functions app that is exposed through Azure API Management. Our Fun...

Feb 02, 2021

Understanding Policies in Azure API Management

Policies are the heart of Azure API Management. They let us change the behavior of our APIs in a very flexible manner. Before I dive in t...

Apr 18, 2020

Event-Driven Infrastructure with App Configuration

Azure App Configuration is great for externalizing application configurations. But what if an application is our infrastructure? How coul...

Sep 12, 2020

Logging in Azure API Management

This post is a complete step-by-step guide on how to send logs from Azure API Management to Azure Event Hub with PowerShell. We start by ...

Apr 11, 2020

How To Debug Policies in Azure API Management. A Step-by-Step Guide.

In this post I want to briefly go through the Azure API Management extension for VSCode and how we can debug policies. It’s one of the qu...

Jan 16, 2021

Introduction to Azure API Management

Azure API Management (APIM) is a way to create consistent and modern API gateways for existing backend services. It provides an interface...

Jan 25, 2021

How Azure Key Vault is Different to Azure App Configuration

We store certificates and sensitive data as secrets in Azure Key Vault. I know that many store their application configuration there as w...

Feb 05, 2021

Azure App Configuration Introduction

We build this great application that we configure exactly the way it fits into our environments, and then we realize that changing a conf...

May 09, 2020

Using App Configuration in Azure DevOps

Application deployments dependent often on environment specific data like the name of a resource group, location or flags for certain use...

Aug 01, 2020

How To Manage Azure Virtual Machines

I will go through the first steps for managing Virtual Machines. We will create a Windows VM, start the Internet Information Service IIS,...

Dec 26, 2020

Azure API Management with REST

Today, we will provision Azure API Management by using Postman as an API client, sending plain web requests to Azure. We already provisio...

Mar 14, 2020

Latest Stories

How to Secure Azure Functions App with Azure API Management

How to Secure Azure Functions App with Azure API Management

How to use an Azure Managed Identity to authenticate against an Azure Functions app that is exposed through Azure API Management. Our Fun...

Feb 02, 2021

Understanding Policies in Azure API Management

Understanding Policies in Azure API Management

Policies are the heart of Azure API Management. They let us change the behavior of our APIs in a very flexible manner. Before I dive in t...

Apr 18, 2020

Event-Driven Infrastructure with App Configuration

Event-Driven Infrastructure with App Configuration

Azure App Configuration is great for externalizing application configurations. But what if an application is our infrastructure? How coul...

Sep 12, 2020

Logging in Azure API Management

Logging in Azure API Management

This post is a complete step-by-step guide on how to send logs from Azure API Management to Azure Event Hub with PowerShell. We start by ...

Apr 11, 2020

How To Debug Policies in Azure API Management. A Step-by-Step Guide.

How To Debug Policies in Azure API Management. A Step-by-Step Guide.

In this post I want to briefly go through the Azure API Management extension for VSCode and how we can debug policies. It’s one of the qu...

Jan 16, 2021

Introduction to Azure API Management

Introduction to Azure API Management

Azure API Management (APIM) is a way to create consistent and modern API gateways for existing backend services. It provides an interface...

Jan 25, 2021

How Azure Key Vault is Different to Azure App Configuration

How Azure Key Vault is Different to Azure App Configuration

We store certificates and sensitive data as secrets in Azure Key Vault. I know that many store their application configuration there as w...

Feb 05, 2021

Azure App Configuration Introduction

Azure App Configuration Introduction

We build this great application that we configure exactly the way it fits into our environments, and then we realize that changing a conf...

May 09, 2020

Using App Configuration in Azure DevOps

Using App Configuration in Azure DevOps

Application deployments dependent often on environment specific data like the name of a resource group, location or flags for certain use...

Aug 01, 2020

How To Manage Azure Virtual Machines

How To Manage Azure Virtual Machines

I will go through the first steps for managing Virtual Machines. We will create a Windows VM, start the Internet Information Service IIS,...

Dec 26, 2020

Azure API Management with REST

Azure API Management with REST

Today, we will provision Azure API Management by using Postman as an API client, sending plain web requests to Azure. We already provisio...

Mar 14, 2020