Skip to content

Commit

Permalink
Abstract DP service to it's own file
Browse files Browse the repository at this point in the history
  • Loading branch information
DrizzlyOwl committed Dec 3, 2024
1 parent b8068aa commit 52d4a44
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
24 changes: 1 addition & 23 deletions Dfe.PrepareConversions/Dfe.PrepareConversions/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Azure.Identity;
using Dfe.Academisation.CorrelationIdMiddleware;
using Dfe.PrepareTransfers.Web.Services;
using Dfe.PrepareTransfers.Web.Services.Interfaces;
Expand All @@ -20,7 +19,6 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.CookiePolicy;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpOverrides;
Expand All @@ -32,7 +30,6 @@
using Microsoft.Identity.Web;
using Microsoft.Identity.Web.UI;
using System;
using System.IO;
using System.Security.Claims;
using System.Threading.Tasks;

Expand Down Expand Up @@ -98,26 +95,7 @@ public void ConfigureServices(IServiceCollection services)
options.MaxAge = TimeSpan.FromDays(365);
});

// Only proceed if this is not a local development environment (path is only valid when running in a Container)
var dpTargetPath = "@/srv/app/storage";

if (Directory.Exists(dpTargetPath)) {
// If a Key Vault Key URI is defined, expect to encrypt the keys.xml
string kvProtectionKeyUri = Configuration.GetValue<string>("DataProtection:KeyVaultKey");

// Setup basic Data Protection and persist keys.xml to local file system
var dp = services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(dpTargetPath));

if (!string.IsNullOrEmpty(kvProtectionKeyUri))
{
// Encrypt the keys using Key Vault
var credentials = new DefaultAzureCredential();
dp.ProtectKeysWithAzureKeyVault(
new Uri(kvProtectionKeyUri),
credentials
);
}
}
services.AddDataProtectionService(Configuration);

services.AddScoped(sp => sp.GetService<IHttpContextAccessor>()?.HttpContext?.Session);
services.AddSession(options =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Azure.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.DataProtection;
using System;
using System.IO;

namespace Dfe.PrepareConversions.Utils
{
internal static class DataProtectionService
{
public static void AddDataProtectionService(this IServiceCollection services, IConfiguration configuration)
{
var dp = services.AddDataProtection();
var dpTargetPath = "@/srv/app/storage";

if (Directory.Exists(dpTargetPath)) {
// If a Key Vault Key URI is defined, expect to encrypt the keys.xml
string kvProtectionKeyUri = configuration.GetValue<string>("DataProtection:KeyVaultKey");

if (!string.IsNullOrWhiteSpace(kvProtectionKeyUri))
{
throw new InvalidOperationException("DataProtection:Path is undefined or empty");
}

dp.PersistKeysToFileSystem(new DirectoryInfo(dpTargetPath));
dp.ProtectKeysWithAzureKeyVault(new Uri(kvProtectionKeyUri), new DefaultAzureCredential());
}
}
}
}

0 comments on commit 52d4a44

Please sign in to comment.