Reference secrets are the easier way to bind KeyVault variables to the Environment variable context.
On this approach, only configuration is required.
So if you have your Function configuration file, for instance `settings.json`, `appsettings.json` (depending on how you name it) you don't need to change a thing.
Let’s have a look at this settings.json configuration file:
The section SecretSettings contains the connection string secrets. Those secrets will get superseded by KeyVault secrets if you do this in your Azure Configuration settings:
Note that the Source shows Key vault Reference, and the mapping that makes it possible:
Notice that I have only provided SecretSettings:SqlConnectionString as per highlighting, but what about SecretSettings:BlobConnectionString? What’s going to happen when I run this function locally? And on Azure?
Before we answer these questions, let’s have a look at the function created for this proof of concept:
We have 3 important classes:
Startup.cs - Responsible for initializing the function.
SecretSettings.cs - The model for SecretSettings json section.
TestApi.cs - Test endpoint to validate the variables
And let’s look at the code implemented for these classes:
If we run the function locally, and we call the endpoint http://localhost:7071/api/test/secret, we get:
In this result no KeyVault secrets were loaded, because we are running it locally.
If we run the function on Azure, and we call the endpoint https://yourfunction.azurewebsites.net/api/test/secret, we get:
In this result we got the KeyVault secret that we mapped on the Function! Because we have not mapped the other secret, the Function automatically falls back to what is available on settings.json. This is because of the ConfigurationBuilder that we have mapped on Startup.cs.
2 - Variables with Configuration Provider
The Configuration Provider available in C# is called AzureKeyVaultConfigurationProvider.
You get this provider available by referencing the Nuget packageAzure.Extensions.AspNetCore.Configuration.Secrets. Together with the Nuget packageAzure.Identity you get to authenticate to KeyVault.
In this article I use DefaultAzureCredential with Azure.Identity. Explore the different ways to use Azure.Identityhere
The ConfigurationBuilder class allows you to build key/value-based configuration settings for use in an application, and with AzureKeyVaultConfigurationProvider hooked up, the key/value from Azure KeyVault gets referenced by the Builder, allowing the app to connect to KeyVault.
Let’s come back to the same questions. What’s going to happen when I run this function locally now? And on Azure?
Proof of Concept
Ready to run the Function with changes?
If we run the function locally, and we call the endpoint http://localhost:7071/api/test/secret, we get:
If we run the function on Azure, and we call the endpoint https://yourfunction.azurewebsites.net/api/test/secret, we get:
In both cases the secrets were loaded from KeyVault, and not from settings.json. Both secrets SecretSettings--SqlConnectionString and SecretSettings--BlobConnectionString came from KeyVault.
Join the conversation! Share your thoughts and connect with other readers.