From 3dc6bffe63f086d2d08bd54eeb58eb1b14d6e38a Mon Sep 17 00:00:00 2001 From: Mia Winter Date: Sun, 21 Jan 2024 21:03:06 +0100 Subject: [PATCH] Added redis --- Wave/Program.cs | 27 +++++++++++++++++++++++++++ Wave/Wave.csproj | 1 + docker-compose.yml | 12 ++++++++++++ 3 files changed, 40 insertions(+) diff --git a/Wave/Program.cs b/Wave/Program.cs index 4b7e5e3..3ab163a 100644 --- a/Wave/Program.cs +++ b/Wave/Program.cs @@ -1,8 +1,13 @@ using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Components.Server; +using Microsoft.AspNetCore.DataProtection; +using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption; +using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Diagnostics.Metrics; using Microsoft.Extensions.Options; +using StackExchange.Redis; using Tomlyn.Extensions.Configuration; using Wave.Components; using Wave.Components.Account; @@ -21,6 +26,27 @@ builder.Services.AddRazorComponents().AddInteractiveServerComponents(); builder.Services.AddControllers(); +#region Data Protection & Redis + +if (builder.Configuration.GetConnectionString("Redis") is { } redisUri) { + var redis = ConnectionMultiplexer.Connect(redisUri); + builder.Services.AddDataProtection() + .PersistKeysToStackExchangeRedis(redis) + .UseCryptographicAlgorithms(new AuthenticatedEncryptorConfiguration() { + EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC, + ValidationAlgorithm = ValidationAlgorithm.HMACSHA256 + }); +} else { + builder.Services.AddDataProtection() + .UseCryptographicAlgorithms(new AuthenticatedEncryptorConfiguration() { + EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC, + ValidationAlgorithm = ValidationAlgorithm.HMACSHA256 + }); + Console.WriteLine("No Redis connection string found."); +} + +#endregion + #region Authentication & Authorization builder.Services.AddCascadingAuthenticationState(); @@ -80,6 +106,7 @@ builder.Services.AddScoped, SmtpEmailSender>(); } else { builder.Services.AddSingleton, IdentityNoOpEmailSender>(); + Console.WriteLine("No Email provider configured."); } #endregion diff --git a/Wave/Wave.csproj b/Wave/Wave.csproj index d7f1ce2..6705cb9 100644 --- a/Wave/Wave.csproj +++ b/Wave/Wave.csproj @@ -16,6 +16,7 @@ + diff --git a/docker-compose.yml b/docker-compose.yml index a3c4b7c..6fe9f1f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,11 +14,14 @@ services: - database:db environment: - "WAVE_ConnectionStrings__DefaultConnection=Host=db; Username=wave; Password=development" + - "WAVE_ConnectionStrings__Redis=redis,password=development" volumes: - wave-files:/app/files - wave-config:/configuration networks: - wave + depends_on: + - database database: image: postgres:16.1-alpine restart: unless-stopped @@ -28,6 +31,14 @@ services: - "POSTGRES_PASSWORD=development" volumes: - wave-db:/var/lib/postgresql/data + networks: + - wave + redis: + image: redis:7-alpine + restart: unless-stopped + command: redis-server --requirepass development --save 60 1 --loglevel warning + volumes: + - wave-redis:/data networks: - wave @@ -35,5 +46,6 @@ volumes: wave-files: wave-config: wave-db: + wave-redis: networks: wave: \ No newline at end of file