This is a sample project to demonstrate how to use Hashicorp Vault as a configuration backend for ASP.NET Core applications. The project uses the VaultSharp.Extensions.Configuration library to load configuration from Hashicorp Vault.
- Hashicorp Vault server running locally or in a cloud environment.
- .NET Core 3.1 SDK or later.
- Pull the vault image from docker hub
docker pull vault- Run the vault server in dev mode. Obtain token from the console output.
docker run --cap-add=IPC_LOCK -e 'VAULT_DEV_ROOT_TOKEN_ID=myroot' -p 8200:8200 vault- Open the vault UI in the browser
http://localhost:8200
- Use the token obtained to log in to the vault UI.
- Create a new secret engine of type KV and version 2 and mount it at
MyApppath. - Create a new secret with key
appsettings.jsonand the following value as a JSON object with the configuration settings.
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ApplicationSetting": {
"DisplayTitle": "App using vault for config storage",
"IsLive": true
}
}- Create a new auth method of type userpass and enable it.
- Create a new user with a password and assign it to the
defaultpolicy. - Modify the default policy to allow access to the
MyApppath with the following policy.
path "MyApp/*" {
capabilities = ["create", "read", "update", "list"]
}All good! The new user should have access to the Configurations stored in the MyApp path.
- Clone the repository
- Update
appsettings.jsonwith the vault server URL and the user credentials. - Run the project in your favourite IDE or using the dotnet CLI.
- Visit the URL
https://localhost:5267/view-configto see the configuration settings loaded from Hashicorp Vault. - Change the configuration settings in the vault UI, wait for the configured reload seconds to elapse and refresh the URl to see the updated configuration settings.