-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
61 lines (58 loc) · 2.98 KB
/
Program.cs
File metadata and controls
61 lines (58 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.IO;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
namespace PortLogistics
{
public class Program
{
public static void Main(string[] args)
{
// Defensive check: when running in containerized environments, a host mount may
// expose a path like /https/aspnetapp.pfx. If that path is not a regular file
// (for example it's a directory) or is not readable from the container process,
// Kestrel will throw UnauthorizedAccessException while attempting to load it.
// Detect and remove the env var to fall back to HTTP-only behavior in development.
try
{
var certPathEnv = Environment.GetEnvironmentVariable("ASPNETCORE_Kestrel__Certificates__Default__Path");
if (!string.IsNullOrEmpty(certPathEnv))
{
// Only keep the variable when it points to a readable regular file
if (Directory.Exists(certPathEnv) || !File.Exists(certPathEnv))
{
// Unset the environment variable so Kestrel won't attempt to load it
Environment.SetEnvironmentVariable("ASPNETCORE_Kestrel__Certificates__Default__Path", null);
// Also unset password to be safe
Environment.SetEnvironmentVariable("ASPNETCORE_Kestrel__Certificates__Default__Password", null);
Console.WriteLine($"[Startup] Removed invalid Kestrel certificate path: {certPathEnv}");
}
else
{
// Try opening the file once to detect permission errors early
try
{
using var stream = File.Open(certPathEnv, FileMode.Open, FileAccess.Read);
}
catch (Exception ex)
{
Environment.SetEnvironmentVariable("ASPNETCORE_Kestrel__Certificates__Default__Path", null);
Environment.SetEnvironmentVariable("ASPNETCORE_Kestrel__Certificates__Default__Password", null);
Console.WriteLine($"[Startup] Could not read Kestrel certificate at {certPathEnv}: {ex.Message}. Environment variables cleared to avoid crash.");
}
}
}
}
catch (Exception ex)
{
// Don't prevent application from starting - only log diagnostic message
Console.WriteLine($"[Startup] Error while validating Kestrel certificate env vars: {ex}");
}
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
//.UseUrls("http://0.0.0.0:5001");
}
}