fix: data protection
All checks were successful
BCards Deployment Pipeline / Run Tests (push) Successful in 2s
BCards Deployment Pipeline / PR Validation (push) Has been skipped
BCards Deployment Pipeline / Build and Push Image (push) Successful in 14m42s
BCards Deployment Pipeline / Deploy to Production (ARM - OCI) (push) Successful in 1m28s
BCards Deployment Pipeline / Deploy to Test (x86 - Local) (push) Has been skipped
BCards Deployment Pipeline / Cleanup Old Resources (push) Has been skipped
BCards Deployment Pipeline / Deployment Summary (push) Successful in 0s

This commit is contained in:
Ricardo Carneiro 2025-09-21 13:45:49 -03:00
parent b70fd7c23a
commit 6c1c6cb543
2 changed files with 40 additions and 13 deletions

View File

@ -29,6 +29,7 @@
<PackageReference Include="Serilog.Sinks.Async" Version="1.5.0" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="8.0.0" />
<PackageReference Include="AspNetCore.HealthChecks.MongoDb" Version="7.0.0" />
<PackageReference Include="AspNetCore.DataProtection.MongoDB" Version="8.0.0" />
</ItemGroup>
<ItemGroup>

View File

@ -2,6 +2,7 @@ using BCards.Web.Configuration;
using BCards.Web.Services;
using BCards.Web.Repositories;
using BCards.Web.HealthChecks;
using AspNetCore.DataProtection.MongoDb;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.Google;
using Microsoft.AspNetCore.Localization;
@ -19,6 +20,7 @@ using Microsoft.Extensions.Diagnostics.HealthChecks;
using Serilog.Sinks.OpenSearch;
using BCards.Web.TestSupport;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.DataProtection;
var builder = WebApplication.CreateBuilder(args);
@ -233,6 +235,30 @@ builder.Services.AddScoped(serviceProvider =>
return client.GetDatabase(settings.DatabaseName);
});
var dataProtectionSection = builder.Configuration.GetSection("DataProtection:Mongo");
var dataProtectionConnectionString = dataProtectionSection.GetValue<string>("ConnectionString")
?? builder.Configuration.GetSection("MongoDb").GetValue<string>("ConnectionString");
var dataProtectionDatabase = dataProtectionSection.GetValue<string>("DatabaseName")
?? builder.Configuration.GetSection("MongoDb").GetValue<string>("DatabaseName")
?? "BCardsDB";
var dataProtectionCollection = dataProtectionSection.GetValue<string>("CollectionName") ?? "DataProtectionKeys";
if (!string.IsNullOrWhiteSpace(dataProtectionConnectionString))
{
Log.Information("Configuring DataProtection to persist keys in MongoDB database {Database} / collection {Collection}",
dataProtectionDatabase, dataProtectionCollection);
builder.Services.AddDataProtection()
.SetApplicationName("BCards")
.PersistKeysToMongoDb(
() => new MongoClient(dataProtectionConnectionString).GetDatabase(dataProtectionDatabase),
dataProtectionCollection);
}
else
{
Log.Warning("DataProtection MongoDB configuration missing; encryption keys will be ephemeral per container.");
}
// Stripe Configuration with validation
builder.Services.Configure<StripeSettings>(
builder.Configuration.GetSection("Stripe"));