From 2533b5298f344519d42841bf6aadbe6e9bc6d67c Mon Sep 17 00:00:00 2001 From: Bangara Raju Kottedi Date: Sun, 15 Feb 2026 14:22:22 +0530 Subject: [PATCH] Support encrypted cache conn strings & update dependencies Moved DecryptConnectionString to KBR.Shared.Extensions for reuse and improved configuration access. Added Encryption property to CacheOptions and appsettings for conditional decryption of cache connection strings. Updated ServiceCollectionExtensions to decrypt SQL Server cache connection strings when needed. Upgraded NuGet packages across projects to latest .NET 8/10.0.x versions. Included minor code cleanups and OpenApiSecurityScheme improvements. --- PortBlog.API/PortBlog.API.csproj | 6 +++--- PortBlog.API/Program.cs | 7 +++---- PortBlog.API/appsettings.Development.json | 1 + PortBlog.Tests/PortBlog.Tests.csproj | 12 +++++++++--- .../Extensions/ServiceCollectionExtensions.cs | 8 +++++++- Shared/KBR.Cache/KBR.Cache.csproj | 16 ++++++++-------- Shared/KBR.Cache/Options/CacheOptions.cs | 2 ++ .../Extensions/ConfigurationExtensions.cs | 8 +++++--- Shared/KBR.Shared/KBR.Shared.csproj | 3 ++- 9 files changed, 40 insertions(+), 23 deletions(-) rename {PortBlog.API => Shared/KBR.Shared}/Extensions/ConfigurationExtensions.cs (83%) diff --git a/PortBlog.API/PortBlog.API.csproj b/PortBlog.API/PortBlog.API.csproj index cf49513..1abaca1 100644 --- a/PortBlog.API/PortBlog.API.csproj +++ b/PortBlog.API/PortBlog.API.csproj @@ -10,10 +10,10 @@ - - + + - + diff --git a/PortBlog.API/Program.cs b/PortBlog.API/Program.cs index 1389589..b31ba14 100644 --- a/PortBlog.API/Program.cs +++ b/PortBlog.API/Program.cs @@ -12,7 +12,7 @@ using PortBlog.API.Middleware; using Serilog; using System.Reflection; using System.Text; - +using KBR.Shared.Extensions; var builder = WebApplication.CreateBuilder(args); @@ -131,14 +131,13 @@ builder.Services.AddSwaggerGen(c => Scheme = "ApiKeyScheme" }); - var key = new OpenApiSecurityScheme() + var key = new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "ApiKey" - }, - In = ParameterLocation.Header + } }; // JWT Bearer Security Definition diff --git a/PortBlog.API/appsettings.Development.json b/PortBlog.API/appsettings.Development.json index 8598e30..1f73a69 100644 --- a/PortBlog.API/appsettings.Development.json +++ b/PortBlog.API/appsettings.Development.json @@ -6,6 +6,7 @@ }, "Cache": { "ConnectionString": "SERVER=192.168.0.197; DATABASE=cv_blog; UID=PortBlogDevUser; PWD=p@$$w0rd1234;Allow User Variables=true;", + "Encryption": "false", "Provider": "SqlServer" }, "Jwt": { diff --git a/PortBlog.Tests/PortBlog.Tests.csproj b/PortBlog.Tests/PortBlog.Tests.csproj index db296aa..163250f 100644 --- a/PortBlog.Tests/PortBlog.Tests.csproj +++ b/PortBlog.Tests/PortBlog.Tests.csproj @@ -8,10 +8,16 @@ - - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/Shared/KBR.Cache/Extensions/ServiceCollectionExtensions.cs b/Shared/KBR.Cache/Extensions/ServiceCollectionExtensions.cs index 9233798..6661a32 100644 --- a/Shared/KBR.Cache/Extensions/ServiceCollectionExtensions.cs +++ b/Shared/KBR.Cache/Extensions/ServiceCollectionExtensions.cs @@ -1,5 +1,6 @@ using KBR.Cache.Constants; using KBR.Cache.Options; +using KBR.Shared.Extensions; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -20,9 +21,14 @@ namespace KBR.Cache.Extensions } else if (cacheOptions.Provider == CacheProviders.SqlServer) { + var connectionString = cacheOptions.ConnectionString; + if ((bool)cacheOptions.Encryption) + { + connectionString = configuration.DecryptConnectionString(connectionString); + } services.AddDistributedMySqlCache(options => { - options.ConnectionString = cacheOptions.ConnectionString; + options.ConnectionString = connectionString; options.TableName = "Cache"; }); } diff --git a/Shared/KBR.Cache/KBR.Cache.csproj b/Shared/KBR.Cache/KBR.Cache.csproj index 2c97411..9e1d787 100644 --- a/Shared/KBR.Cache/KBR.Cache.csproj +++ b/Shared/KBR.Cache/KBR.Cache.csproj @@ -7,14 +7,14 @@ - - - - - - - - + + + + + + + + diff --git a/Shared/KBR.Cache/Options/CacheOptions.cs b/Shared/KBR.Cache/Options/CacheOptions.cs index 4f0c502..b90a250 100644 --- a/Shared/KBR.Cache/Options/CacheOptions.cs +++ b/Shared/KBR.Cache/Options/CacheOptions.cs @@ -4,6 +4,8 @@ { public string Provider { get; set; } = null!; + public bool? Encryption { get; set; } = false; + public string? ConnectionString { get; set; } } } diff --git a/PortBlog.API/Extensions/ConfigurationExtensions.cs b/Shared/KBR.Shared/Extensions/ConfigurationExtensions.cs similarity index 83% rename from PortBlog.API/Extensions/ConfigurationExtensions.cs rename to Shared/KBR.Shared/Extensions/ConfigurationExtensions.cs index dd0763a..7a026c5 100644 --- a/PortBlog.API/Extensions/ConfigurationExtensions.cs +++ b/Shared/KBR.Shared/Extensions/ConfigurationExtensions.cs @@ -1,12 +1,14 @@ -using System.Security.Cryptography; +using Microsoft.Extensions.Configuration; +using System.Security.Cryptography; -namespace PortBlog.API.Extensions +namespace KBR.Shared.Extensions { public static class ConfigurationExtensions { public static string DecryptConnectionString(this IConfiguration configuration, string encryptedConnectionString) { - string keyBase64 = configuration.GetValue("ConnectionStrings:Key").ToString(); + // Fix: Use GetSection and Value instead of GetValue (since GetValue is not available on IConfiguration) + string keyBase64 = configuration.GetSection("ConnectionStrings:Key").Value; string vectorBase64 = encryptedConnectionString.Split(":")[1]; string cipherText = encryptedConnectionString.Split(":")[0]; diff --git a/Shared/KBR.Shared/KBR.Shared.csproj b/Shared/KBR.Shared/KBR.Shared.csproj index 5a714a1..69ee995 100644 --- a/Shared/KBR.Shared/KBR.Shared.csproj +++ b/Shared/KBR.Shared/KBR.Shared.csproj @@ -7,7 +7,8 @@ - + + -- 2.47.3