Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT: Added Azure Key Vault for storing application secrets #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Extensions/KeyVaultConfigurationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Azure.Extensions.AspNetCore.Configuration.Secrets;
using Azure.Identity;

namespace MyAccountPage
{
public static class KeyVaultConfigurationExtensions
{

public static IConfigurationBuilder ConfigureAzureKeyVault(this WebApplicationBuilder builder)
{
var kvName = builder.Configuration["KeyVault:Name"];
if (string.IsNullOrWhiteSpace(kvName))
return builder.Configuration;

var kvPrefix = builder.Configuration["KeyVault:SecretsPrefix"];
var kvReloadIntervalInMinutes = builder.Configuration.GetValue<int>("KeyVault:ReloadIntervalInMinutes", default);

TimeSpan? kvReloadInterval = kvReloadIntervalInMinutes == default ? null : new TimeSpan(hours: 0, minutes: kvReloadIntervalInMinutes, seconds: 0);

var kvOptions = new AzureKeyVaultConfigurationOptions() { ReloadInterval = kvReloadInterval, Manager = new KeyVault.PrefixKeyVaultSecretManager(kvPrefix) };

return builder.Configuration.AddAzureKeyVault(
vaultUri: new Uri($"https://{kvName}.vault.azure.net/"),
credential: new ChainedTokenCredential(new DefaultAzureCredential()
), kvOptions);
}
}
}
30 changes: 30 additions & 0 deletions KeyVault/PrefixKeyVaultSecretManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Azure.Extensions.AspNetCore.Configuration.Secrets;
using Azure.Security.KeyVault.Secrets;
using Microsoft.IdentityModel.Tokens;

namespace KeyVault
{
public class PrefixKeyVaultSecretManager : KeyVaultSecretManager
{
private readonly string _prefix;


public PrefixKeyVaultSecretManager(string? prefix)
{
prefix ??= string.Empty;
_prefix = prefix.IsNullOrEmpty() || prefix.EndsWith("--") ? prefix : $"{prefix}--";
}

public override bool Load(SecretProperties secret)
{
return secret.Name.StartsWith(_prefix, StringComparison.OrdinalIgnoreCase);
}

public override string GetKey(KeyVaultSecret secret)
{
return secret.Name
.Substring(_prefix.Length)
.Replace("--", ConfigurationPath.KeyDelimiter);
}
}
}
6 changes: 5 additions & 1 deletion MyAccountPage.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
Expand All @@ -7,11 +7,15 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Session" Version="2.2.0" />
<PackageReference Include="Microsoft.Identity.Web" Version="2.5.0" />
<PackageReference Include="Microsoft.Identity.Web.MicrosoftGraph" Version="2.5.0" />
<PackageReference Include="Microsoft.Identity.Web.UI" Version="2.5.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />

<PackageReference Include="Azure.Identity" Version="1.8.2" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
</ItemGroup>

</Project>
5 changes: 2 additions & 3 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Identity.Web;
using Microsoft.Identity.Web.UI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Authentication.Cookies;
using MyAccountPage;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Options;

var builder = WebApplication.CreateBuilder(args);

builder.ConfigureAzureKeyVault();

var initialScopes = builder.Configuration["MicrosoftGraph:Scopes"]?.Split(' ');

// Add services to the container.
Expand Down
14 changes: 14 additions & 0 deletions ReadmeFiles/Deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,20 @@ And the last bit is your configuration for your Verified Employee credential and
"Authority": "YOUR VC SERVICE AUTHORITY/DID"
},
```

If you want to add Azure Key Vault for storing secrets, add the following configuration section.
```
"KeyVault": {
"Name": "YOUR KEYVAULT NAME",
"SecretsPrefix": "",
"ReloadIntervalInMinutes": 15
},
```
The Name is the name of your key vault (not the full url).
The secrets prefix is optional and can be used if you store multiple applications in the same key vault.
The reload interval is the interval in minutes the application will reload the secrets from the key vault. This is useful if you want to change the configuration without restarting the application.


If you want to configure the app through web app configuration navigates to your created web app and select configuration on the left-hand side.

![Web Application Configuration](Images/WebApplicationConfiguration.png)
Expand Down
6 changes: 6 additions & 0 deletions appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
"Scopes": "user.read"
},

"KeyVault": {
//"Name": "YOUR KEYVAULT NAME",
//"SecretsPrefix": "",
//"ReloadIntervalInMinutes": 15
},

"Logging": {
"LogLevel": {
"Default": "Information",
Expand Down