refactor: delegate ssl cert read to common lib
This commit is contained in:
parent
9b9143632a
commit
67231c86af
|
@ -0,0 +1 @@
|
||||||
|
wwwroot
|
|
@ -0,0 +1,41 @@
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Security.Cryptography.X509Certificates;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
|
||||||
|
namespace OliverBooth.Common.Extensions;
|
||||||
|
|
||||||
|
public static class WebHostBuilderExtensions
|
||||||
|
{
|
||||||
|
public static IWebHostBuilder AddCertificateFromEnvironment(this IWebHostBuilder builder)
|
||||||
|
{
|
||||||
|
return builder.UseKestrel(options =>
|
||||||
|
{
|
||||||
|
string certPath = Environment.GetEnvironmentVariable("SSL_CERT_PATH")!;
|
||||||
|
if (!File.Exists(certPath))
|
||||||
|
{
|
||||||
|
options.ListenAnyIP(5049);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
string? keyPath = Environment.GetEnvironmentVariable("SSL_KEY_PATH");
|
||||||
|
if (string.IsNullOrWhiteSpace(keyPath) || !File.Exists(keyPath)) keyPath = null;
|
||||||
|
|
||||||
|
options.ListenAnyIP(2845, options =>
|
||||||
|
{
|
||||||
|
X509Certificate2 cert = CreateCertFromPemFile(certPath, keyPath);
|
||||||
|
options.UseHttps(cert);
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
|
||||||
|
static X509Certificate2 CreateCertFromPemFile(string certPath, string? keyPath)
|
||||||
|
{
|
||||||
|
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||||
|
return X509Certificate2.CreateFromPemFile(certPath, keyPath);
|
||||||
|
|
||||||
|
//workaround for windows issue https://github.com/dotnet/runtime/issues/23749#issuecomment-388231655
|
||||||
|
using var cert = X509Certificate2.CreateFromPemFile(certPath, keyPath);
|
||||||
|
return new X509Certificate2(cert.Export(X509ContentType.Pkcs12));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk.Razor">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net7.0</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<SupportedPlatform Include="browser"/>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<FrameworkReference Include="Microsoft.AspNetCore.App"/>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="7.0.9"/>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="7.0.9"/>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
|
@ -1,4 +1,4 @@
|
||||||
|
|
||||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OliverBooth", "OliverBooth\OliverBooth.csproj", "{A58A6FA3-480C-400B-822A-3786741BF39C}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OliverBooth", "OliverBooth\OliverBooth.csproj", "{A58A6FA3-480C-400B-822A-3786741BF39C}"
|
||||||
EndProject
|
EndProject
|
||||||
|
@ -31,6 +31,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ts", "ts", "{BB9F76AC-292A-
|
||||||
src\ts\Input.ts = src\ts\Input.ts
|
src\ts\Input.ts = src\ts\Input.ts
|
||||||
EndProjectSection
|
EndProjectSection
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OliverBooth.Common", "OliverBooth.Common\OliverBooth.Common.csproj", "{38DEB2FA-3DF4-4D37-A12D-22CAEEA3A8AB}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
@ -41,6 +43,10 @@ Global
|
||||||
{A58A6FA3-480C-400B-822A-3786741BF39C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{A58A6FA3-480C-400B-822A-3786741BF39C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{A58A6FA3-480C-400B-822A-3786741BF39C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{A58A6FA3-480C-400B-822A-3786741BF39C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{A58A6FA3-480C-400B-822A-3786741BF39C}.Release|Any CPU.Build.0 = Release|Any CPU
|
{A58A6FA3-480C-400B-822A-3786741BF39C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{38DEB2FA-3DF4-4D37-A12D-22CAEEA3A8AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{38DEB2FA-3DF4-4D37-A12D-22CAEEA3A8AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{38DEB2FA-3DF4-4D37-A12D-22CAEEA3A8AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{38DEB2FA-3DF4-4D37-A12D-22CAEEA3A8AB}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(NestedProjects) = preSolution
|
GlobalSection(NestedProjects) = preSolution
|
||||||
{822F528E-3CA7-4B7D-9250-BD248ADA7BAE} = {8A323E64-E41E-4780-99FD-17BF58961FB5}
|
{822F528E-3CA7-4B7D-9250-BD248ADA7BAE} = {8A323E64-E41E-4780-99FD-17BF58961FB5}
|
||||||
|
|
|
@ -17,7 +17,6 @@
|
||||||
<PackageReference Include="Alexinea.Extensions.Configuration.Toml" Version="7.0.0"/>
|
<PackageReference Include="Alexinea.Extensions.Configuration.Toml" Version="7.0.0"/>
|
||||||
<PackageReference Include="Humanizer.Core" Version="2.14.1"/>
|
<PackageReference Include="Humanizer.Core" Version="2.14.1"/>
|
||||||
<PackageReference Include="Markdig" Version="0.32.0"/>
|
<PackageReference Include="Markdig" Version="0.32.0"/>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="7.0.9"/>
|
|
||||||
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.3"/>
|
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.3"/>
|
||||||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="7.0.0"/>
|
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="7.0.0"/>
|
||||||
<PackageReference Include="SmartFormat.NET" Version="3.2.2"/>
|
<PackageReference Include="SmartFormat.NET" Version="3.2.2"/>
|
||||||
|
@ -26,4 +25,8 @@
|
||||||
<PackageReference Include="ZString" Version="2.5.0"/>
|
<PackageReference Include="ZString" Version="2.5.0"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\OliverBooth.Common\OliverBooth.Common.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
using System.Security.Cryptography.X509Certificates;
|
|
||||||
using Markdig;
|
using Markdig;
|
||||||
using NLog;
|
using NLog;
|
||||||
using NLog.Extensions.Logging;
|
using NLog.Extensions.Logging;
|
||||||
|
using OliverBooth.Common.Extensions;
|
||||||
using OliverBooth.Data;
|
using OliverBooth.Data;
|
||||||
using OliverBooth.Markdown.Template;
|
using OliverBooth.Markdown.Template;
|
||||||
using OliverBooth.Markdown.Timestamp;
|
using OliverBooth.Markdown.Timestamp;
|
||||||
|
@ -18,6 +17,7 @@ builder.Logging.AddNLog();
|
||||||
builder.Services.AddHostedSingleton<LoggingService>();
|
builder.Services.AddHostedSingleton<LoggingService>();
|
||||||
builder.Services.AddSingleton<ConfigurationService>();
|
builder.Services.AddSingleton<ConfigurationService>();
|
||||||
builder.Services.AddSingleton<TemplateService>();
|
builder.Services.AddSingleton<TemplateService>();
|
||||||
|
builder.Services.AddHostedSingleton<BlogSessionService>();
|
||||||
builder.Services.AddSingleton<BlogUserService>();
|
builder.Services.AddSingleton<BlogUserService>();
|
||||||
|
|
||||||
builder.Services.AddSingleton(provider => new MarkdownPipelineBuilder()
|
builder.Services.AddSingleton(provider => new MarkdownPipelineBuilder()
|
||||||
|
@ -41,35 +41,7 @@ builder.Services.AddCors(options => options.AddPolicy("BlogApi", policy => (buil
|
||||||
.AllowAnyHeader()));
|
.AllowAnyHeader()));
|
||||||
builder.Services.AddRouting(options => options.LowercaseUrls = true);
|
builder.Services.AddRouting(options => options.LowercaseUrls = true);
|
||||||
|
|
||||||
builder.WebHost.UseKestrel(kestrel =>
|
builder.WebHost.AddCertificateFromEnvironment();
|
||||||
{
|
|
||||||
string certPath = Environment.GetEnvironmentVariable("SSL_CERT_PATH")!;
|
|
||||||
if (!File.Exists(certPath))
|
|
||||||
{
|
|
||||||
kestrel.ListenAnyIP(5049);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
string? keyPath = Environment.GetEnvironmentVariable("SSL_KEY_PATH");
|
|
||||||
if (string.IsNullOrWhiteSpace(keyPath) || !File.Exists(keyPath)) keyPath = null;
|
|
||||||
|
|
||||||
kestrel.ListenAnyIP(2845, options =>
|
|
||||||
{
|
|
||||||
X509Certificate2 cert = CreateCertFromPemFile(certPath, keyPath);
|
|
||||||
options.UseHttps(cert);
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
|
|
||||||
static X509Certificate2 CreateCertFromPemFile(string certPath, string? keyPath)
|
|
||||||
{
|
|
||||||
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
||||||
return X509Certificate2.CreateFromPemFile(certPath, keyPath);
|
|
||||||
|
|
||||||
//workaround for windows issue https://github.com/dotnet/runtime/issues/23749#issuecomment-388231655
|
|
||||||
using var cert = X509Certificate2.CreateFromPemFile(certPath, keyPath);
|
|
||||||
return new X509Certificate2(cert.Export(X509ContentType.Pkcs12));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
WebApplication app = builder.Build();
|
WebApplication app = builder.Build();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue