Skip to content

Repository files navigation

KeyVault Macros For Local Development

Build Status

NuGet KeyVaultLocalMacros Release

NuGet KeyVaultLocalMacros.WebJobs Release

Azure KeyVault is a great way to secure your app, but configuring how to read from it takes a tad bit of learning or finding the right packages. Recently Azure exposed a simplified way of accessing secrets: using macros in the application settings, but there is just one catch: there is no local development experience! This package bridges that gap by automatically expanding any KeyVault macros found in your configuration at develop-time.

Uses ASPNETCORE_ENVIRONMENT variable to determine if development or not. This is the same behavior as ASP.NET Core.

Example for WebJobs 3 & Azure Functions 2

As always when extending Azure Functions, we need to add a Startup, taking care to include [assembly: WebJobsStartup(typeof(Startup))]. In our Configure, we just call builder.TransformKeyVaultMacros<Startup>(); and all configuration values that are KeyVault macros will be resolved.

usingExampleFunction;usingMicrosoft.Azure.WebJobs;usingMicrosoft.Azure.WebJobs.Hosting;usingPsibr.Extensions.AspNetCore.KeyVaultLocalMacros.WebJobs;[assembly:WebJobsStartup(typeof(Startup))]namespaceExampleFunction{publicclassStartup:IWebJobsStartup{publicvoidConfigure(IWebJobsBuilderbuilder){builder.TransformKeyVaultMacros<Startup>();}}}

This simple example shows how you could replace any binding with a KeyVault secret, in this case the timer schedule %CronExpression% is our binding (not really secret, but useful for the demo).

usingSystem;usingMicrosoft.Azure.WebJobs;usingMicrosoft.Extensions.Logging;namespaceTimerFromKeyVaultFromMacroFunction{publicstaticclassTimerFromKeyVaultFromMacroFunction{[FunctionName(nameof(TimerFromKeyVaultFromMacroFunction))]publicstaticvoidRun([TimerTrigger("%CronExpression%")]TimerInfomyTimer,ILoggerlog){log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");}}}

Take an example local.settings.json file:

{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"CronExpression": "@Microsoft.KeyVault(SecretUri=https://your-keyvault-name.vault.azure.net/secrets/Values--CronExpression/fbe973a92f674ce09e9c03d044499243)"
}
}

About

Transform Azure KeyVault macros into their respective values during local development

Resources

Stars

6 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages