Compare commits

..

1 Commits

12 changed files with 37 additions and 64 deletions
+1 -1
View File
@@ -59,7 +59,7 @@ namespace PortBlog.API.Controllers
catch (Exception ex) catch (Exception ex)
{ {
logger.LogCritical(ex, "Exception while sending OTP for {ToEmail}.", messageSendDto.ToEmail); logger.LogCritical(ex, "Exception while sending OTP for {ToEmail}.", messageSendDto.ToEmail);
return StatusCode(500, ex.Message); return StatusCode(500, "A problem happened while handling your request.");
} }
} }
+2 -2
View File
@@ -262,8 +262,8 @@ namespace PortBlog.API.Controllers
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogCritical(ex, "Exception while sending message from {Name}.", message.Name); _logger.LogCritical($"Exception while sending message from {message.Name}.", ex);
return StatusCode(500, ex.Message); return StatusCode(500, "A problem happened while handling your request.");
} }
} }
} }
@@ -1,14 +1,12 @@
using Microsoft.Extensions.Configuration; using System.Security.Cryptography;
using System.Security.Cryptography;
namespace KBR.Shared.Extensions namespace PortBlog.API.Extensions
{ {
public static class ConfigurationExtensions public static class ConfigurationExtensions
{ {
public static string DecryptConnectionString(this IConfiguration configuration, string encryptedConnectionString) public static string DecryptConnectionString(this IConfiguration configuration, string encryptedConnectionString)
{ {
// Fix: Use GetSection and Value instead of GetValue (since GetValue is not available on IConfiguration) string keyBase64 = configuration.GetValue<string>("ConnectionStrings:Key").ToString();
string keyBase64 = configuration.GetSection("ConnectionStrings:Key").Value;
string vectorBase64 = encryptedConnectionString.Split(":")[1]; string vectorBase64 = encryptedConnectionString.Split(":")[1];
string cipherText = encryptedConnectionString.Split(":")[0]; string cipherText = encryptedConnectionString.Split(":")[0];
+3 -3
View File
@@ -10,10 +10,10 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Asp.Versioning.Mvc" Version="8.1.1" /> <PackageReference Include="Asp.Versioning.Mvc" Version="8.1.1" />
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.1" /> <PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
<PackageReference Include="AutoMapper" Version="16.0.0" /> <PackageReference Include="AutoMapper" Version="15.0.1" />
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" /> <PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.3" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="9.0.9" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="9.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.9" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.9"> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.9">
+7 -18
View File
@@ -12,7 +12,7 @@ using PortBlog.API.Middleware;
using Serilog; using Serilog;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using KBR.Shared.Extensions;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@@ -33,25 +33,13 @@ Log.Logger = loggerConfiguration
.WriteTo.File("logs/portblog.txt", rollingInterval: RollingInterval.Day) .WriteTo.File("logs/portblog.txt", rollingInterval: RollingInterval.Day)
.CreateLogger(); .CreateLogger();
var urlsSection = builder.Configuration.GetSection("Urls"); var urls = builder.Configuration.GetSection("Urls");
var urlsValue = urlsSection.Value;
if (!string.IsNullOrWhiteSpace(urlsValue)) if (!string.IsNullOrEmpty(urls.Value))
{ {
var allowedUrlsToUse = urlsValue var allowedUrlsToUse = urls.Value.Split(',');
.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.Where(u => Uri.IsWellFormedUriString(u, UriKind.Absolute))
.ToArray();
if (allowedUrlsToUse.Length > 0)
{
builder.WebHost.UseUrls(allowedUrlsToUse); builder.WebHost.UseUrls(allowedUrlsToUse);
}
else
{
// optionally log or throw depending on desired behavior
Log.Warning("Configured 'Urls' section was present but no valid URLs were found.");
}
} }
var allowedCorsOrigins = builder.Configuration.GetSection("AllowedCorsOrigins"); var allowedCorsOrigins = builder.Configuration.GetSection("AllowedCorsOrigins");
@@ -143,13 +131,14 @@ builder.Services.AddSwaggerGen(c =>
Scheme = "ApiKeyScheme" Scheme = "ApiKeyScheme"
}); });
var key = new OpenApiSecurityScheme var key = new OpenApiSecurityScheme()
{ {
Reference = new OpenApiReference Reference = new OpenApiReference
{ {
Type = ReferenceType.SecurityScheme, Type = ReferenceType.SecurityScheme,
Id = "ApiKey" Id = "ApiKey"
} },
In = ParameterLocation.Header
}; };
// JWT Bearer Security Definition // JWT Bearer Security Definition
+7 -5
View File
@@ -67,14 +67,16 @@ namespace PortBlog.API.Services
} }
catch (Exception ex) catch (Exception ex)
{ {
// Log full exception and persist failed message. logger.LogCritical(
logger.LogCritical(ex, "Exception while sending mail from {FromEmail} ({Name}).", messageSendDto.FromEmail, messageSendDto.Name); "Exception while sending mail from {FromEmail} ({Name}): {Exception}",
messageSendDto.FromEmail,
messageSendDto.Name,
ex.Message
);
messageSendDto.SentStatus = (int)MailConstants.MailStatus.Failed; messageSendDto.SentStatus = (int)MailConstants.MailStatus.Failed;
mailRepository.AddMessage(messageEntity); mailRepository.AddMessage(messageEntity);
await mailRepository.SaveChangesAsync(); await mailRepository.SaveChangesAsync();
throw new Exception();
// Throw a descriptive exception so callers can return an error message to the client.
throw new InvalidOperationException($"Failed to send email to '{messageSendDto.ToEmail}'. See inner exception for details.", ex);
} }
} }
} }
@@ -6,7 +6,6 @@
}, },
"Cache": { "Cache": {
"ConnectionString": "SERVER=192.168.0.197; DATABASE=cv_blog; UID=PortBlogDevUser; PWD=p@$$w0rd1234;Allow User Variables=true;", "ConnectionString": "SERVER=192.168.0.197; DATABASE=cv_blog; UID=PortBlogDevUser; PWD=p@$$w0rd1234;Allow User Variables=true;",
"Encryption": "false",
"Provider": "SqlServer" "Provider": "SqlServer"
}, },
"Jwt": { "Jwt": {
+3 -9
View File
@@ -8,16 +8,10 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="coverlet.collector" Version="8.0.0"> <PackageReference Include="coverlet.collector" Version="6.0.4" />
<PrivateAssets>all</PrivateAssets> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageReference Include="xunit" Version="2.9.3" /> <PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5"> <PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@@ -1,6 +1,5 @@
using KBR.Cache.Constants; using KBR.Cache.Constants;
using KBR.Cache.Options; using KBR.Cache.Options;
using KBR.Shared.Extensions;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions;
@@ -21,14 +20,9 @@ namespace KBR.Cache.Extensions
} }
else if (cacheOptions.Provider == CacheProviders.SqlServer) else if (cacheOptions.Provider == CacheProviders.SqlServer)
{ {
var connectionString = cacheOptions.ConnectionString;
if ((bool)cacheOptions.Encryption)
{
connectionString = configuration.DecryptConnectionString(connectionString);
}
services.AddDistributedMySqlCache(options => services.AddDistributedMySqlCache(options =>
{ {
options.ConnectionString = connectionString; options.ConnectionString = cacheOptions.ConnectionString;
options.TableName = "Cache"; options.TableName = "Cache";
}); });
} }
+8 -8
View File
@@ -7,14 +7,14 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="10.0.3" /> <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.9" />
<PackageReference Include="Microsoft.Extensions.Caching.SqlServer" Version="10.0.3" /> <PackageReference Include="Microsoft.Extensions.Caching.SqlServer" Version="9.0.9" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="10.0.3" /> <PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="9.0.9" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="10.0.3" /> <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.9" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="10.0.3" /> <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.9" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.3" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.9" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="10.0.3" /> <PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.9" />
<PackageReference Include="Microsoft.Extensions.Options" Version="10.0.3" /> <PackageReference Include="Microsoft.Extensions.Options" Version="9.0.9" />
<PackageReference Include="Pomelo.Extensions.Caching.MySql" Version="2.2.1" /> <PackageReference Include="Pomelo.Extensions.Caching.MySql" Version="2.2.1" />
</ItemGroup> </ItemGroup>
-2
View File
@@ -4,8 +4,6 @@
{ {
public string Provider { get; set; } = null!; public string Provider { get; set; } = null!;
public bool? Encryption { get; set; } = false;
public string? ConnectionString { get; set; } public string? ConnectionString { get; set; }
} }
} }
+1 -2
View File
@@ -7,8 +7,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="10.0.3" /> <PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="9.0.9" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="10.0.3" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>