Skip to content

Commit

Permalink
Moved Data Protection configuration into it's own typed Options (#1242)
Browse files Browse the repository at this point in the history
  • Loading branch information
DrizzlyOwl authored Dec 10, 2024
1 parent 6890ae1 commit a8d65db
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Dfe.PrepareConversions.Configuration;

public class DataProtectionOptions
{
public const string ConfigurationSection = "DataProtection";
public string KeyVaultKey { get; init; }
}
5 changes: 3 additions & 2 deletions Dfe.PrepareConversions/Dfe.PrepareConversions/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ public void ConfigureServices(IServiceCollection services)
options.MaxAge = TimeSpan.FromDays(365);
});

services.AddDataProtectionService(Configuration);

services.AddScoped(sp => sp.GetService<IHttpContextAccessor>()?.HttpContext?.Session);
services.AddSession(options =>
{
Expand Down Expand Up @@ -150,6 +148,9 @@ public void ConfigureServices(IServiceCollection services)
services.Configure<ServiceLinkOptions>(GetConfigurationSectionFor<ServiceLinkOptions>());
services.Configure<AzureAdOptions>(GetConfigurationSectionFor<AzureAdOptions>());
services.Configure<ApplicationInsightsOptions>(GetConfigurationSectionFor<ApplicationInsightsOptions>());
services.Configure<DataProtectionOptions>(GetConfigurationSectionFor<DataProtectionOptions>());

services.AddDataProtectionService(GetTypedConfigurationFor<DataProtectionOptions>());

services.AddScoped<ErrorService>();
services.AddScoped<IGetEstablishment, EstablishmentService>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Azure.Identity;
using Microsoft.Extensions.Configuration;
using Config = Dfe.PrepareConversions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.DataProtection;
using System;
Expand All @@ -9,16 +9,16 @@ namespace Dfe.PrepareConversions.Utils
{
internal static class DataProtectionService
{
public static void AddDataProtectionService(this IServiceCollection services, IConfiguration configuration)
public static void AddDataProtectionService(this IServiceCollection services, Config.DataProtectionOptions options)
{
var dp = services.AddDataProtection();
var dpTargetPath = "@/srv/app/storage";
var dpTargetPath = @"/srv/app/storage";

if (Directory.Exists(dpTargetPath)) {
dp.PersistKeysToFileSystem(new DirectoryInfo(dpTargetPath));

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

if (!string.IsNullOrWhiteSpace(kvProtectionKeyUri))
{
Expand Down

0 comments on commit a8d65db

Please sign in to comment.