-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstract DP service to it's own file
- Loading branch information
1 parent
b8068aa
commit 52d4a44
Showing
2 changed files
with
32 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
Dfe.PrepareConversions/Dfe.PrepareConversions/Utils/DataProtectionService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} | ||
} |