feat: Implementieren der Verschlüsselungs- und Entschlüsselungsdienste mit AES und Integration in die API

- Hinzufügen der `Encryptor`-Klasse für AES-Verschlüsselung und -Entschlüsselung.
- Implementierung des `EncryptionController` zur Bereitstellung von Endpunkten für Verschlüsselung, Entschlüsselung und Generierung von Verschlüsselungsparametern.
- Erweiterung der DI-Konfiguration mit `AddEncryptor`-Erweiterungsmethode und Integration in `Program.cs`.
- Bedingte Registrierung des `EncryptionController` basierend auf der Konfiguration `UseEncryptor`, um sicherzustellen, dass der Controller nur bei Bedarf verfügbar ist.
- Implementierung von Lazy Loading für die Verbindungszeichenfolge in `UserManagerDbContext` zur sicheren Handhabung von verschlüsselten Verbindungszeichenfolgen.
This commit is contained in:
Developer 02
2024-08-29 11:35:47 +02:00
parent c8bcb5a6ac
commit 6e973a494e
8 changed files with 179 additions and 12 deletions

View File

@@ -6,12 +6,19 @@ using Microsoft.AspNetCore.Authentication.Cookies;
using NLog.Web;
using NLog;
using DigitalData.Core.API;
using DigitalData.UserManager.API;
using DigitalData.UserManager.API.Controllers;
using DigitalData.UserManager.Application.Services;
var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
logger.Debug("init main");
try {
var builder = WebApplication.CreateBuilder(args);
var config = builder.Configuration;
builder.Services.AddEncryptor(builder.Configuration.GetSection("EncryptionParameters"));
if (builder.Configuration.GetValue<bool>("RunAsWindowsService"))
builder.Host.UseWindowsService();
@@ -27,7 +34,12 @@ try {
builder.Services.AddSwaggerGen();
}
builder.Services.AddControllers();
builder.Services.AddControllers(opt =>
{
opt.Conventions.Add(new RemoveIfControllerConvention()
.AndIf(c => c.ControllerName == nameof(EncryptionController).Replace("Controller", ""))
.AndIf(c => !config.GetValue<bool>("UseEncryptor")));
});
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
@@ -39,9 +51,10 @@ try {
options.LogoutPath = "/api/auth/logout";
});
builder.Services.AddDbContext<UserManagerDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DD_ECM_Connection"))
.EnableDetailedErrors());
// Once the app is built, the password will be decrypted with Encryptor. lazy loading also acts as a call back method.
Lazy<string>? cnn_str = null;
builder.Services.AddDbContext<UserManagerDbContext>(options => options.UseSqlServer(cnn_str!.Value).EnableDetailedErrors());
var allowedOrigins = builder.Configuration.GetSection("AllowedOrigins").Get<string[]>() ?? throw new InvalidOperationException("In appsettings there is no allowed origin.");
@@ -64,9 +77,17 @@ try {
builder.Services.AddDirectorySearchService();
builder.Services.AddCookieBasedLocalizer();
var app = builder.Build();
cnn_str = new(() =>
{
var encryptor = app.Services.GetRequiredService<Encryptor>();
var eCnnStr = config.GetConnectionString("DD_ECM_Connection") ?? throw new InvalidOperationException("Connection string 'DD_ECM_Connection' is missing from the configuration.");
var cnnStr = encryptor.Decrypt(eCnnStr);
return cnnStr;
});
app.UseCors("DefaultCorsPolicy");
if (builder.Configuration.GetValue<bool>("UseSwagger"))