Skip to content

Commit

Permalink
fix launch and prevent certificate crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Freezor committed Jun 10, 2024
1 parent 6db4390 commit 4356284
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/AasxServerBlazor/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"AasxServerBlazor": {
"commandName": "Project",
"commandLineArgs": "--no-security --aasx-in-memory 1000 --data-path \"C:\\Development\" --edit --external-blazor http://localhost:5001",
"commandLineArgs": "--no-security --aasx-in-memory 1000 --data-path \"/Users/oliverfries/projects\" --edit --external-blazor http://localhost:5001",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
Expand Down
61 changes: 42 additions & 19 deletions src/AasxServerStandardBib/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -861,28 +861,11 @@ private static async Task<int> Run(CommandLineArguments a)
Console.WriteLine("Security 1 Startup - Server");
Console.WriteLine("Security 1.1 Load X509 Root Certificates into X509 Store Root");

X509Store root = new X509Store("Root", StoreLocation.CurrentUser);
root.Open(OpenFlags.ReadWrite);

System.IO.DirectoryInfo ParentDirectory = new System.IO.DirectoryInfo(".");

if (Directory.Exists("./root"))
{
foreach (System.IO.FileInfo f in ParentDirectory.GetFiles("./root/*.cer"))
{
X509Certificate2 cert = new X509Certificate2("./root/" + f.Name);

root.Add(cert);
Console.WriteLine("Security 1.1 Add " + f.Name);
}
}
InitializeCertificateValidation();

if (!Directory.Exists("./temp"))
Directory.CreateDirectory("./temp");

string fn = null;


bool createFilesOnly = false;
if (System.IO.File.Exists(AasxHttpContextHelper.DataPath + "/FILES.ONLY"))
createFilesOnly = true;
Expand Down Expand Up @@ -983,6 +966,7 @@ private static async Task<int> Run(CommandLineArguments a)
int taskIndex = 0;

int fi = 0;
string fn = null;
while (fi < fileNames.Length)
{
// try
Expand Down Expand Up @@ -1270,7 +1254,7 @@ private static async Task<int> Run(CommandLineArguments a)
// MICHA MICHA
AasxTimeSeries.TimeSeries.timeSeriesInit();

AasxTask.taskInit();
//AasxTask.taskInit();

RunScript(true);
//// Initialize NewDataAvailable?.Invoke(null, EventArgs.Empty);
Expand Down Expand Up @@ -1336,6 +1320,45 @@ private static async Task<int> Run(CommandLineArguments a)
return 0;
}

private static void InitializeCertificateValidation()
{
try
{
using var root = new X509Store("Root", StoreLocation.CurrentUser);
root.Open(OpenFlags.ReadWrite);

const string rootDirectory = "./root";
if (!Directory.Exists(rootDirectory))
{
Console.WriteLine("The directory './root' does not exist.");
}
else
{
var parentDirectory = new DirectoryInfo(rootDirectory);

foreach (var file in parentDirectory.GetFiles("*.cer"))
{
try
{
var certPath = Path.Combine(rootDirectory, file.Name);
var cert = new X509Certificate2(certPath);

root.Add(cert);
Console.WriteLine($"Security 1.1 Add {file.Name}");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to add certificate {file.Name}: {ex.Message}");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Failed to open the certificate store: {ex.Message}");
}
}

public static void Main(string[] args)
{
Console.WriteLine("args:");
Expand Down

0 comments on commit 4356284

Please sign in to comment.