Skip to content

Commit

Permalink
Merge pull request #130 from LeonAquitaine/master
Browse files Browse the repository at this point in the history
Proper certificate file handling
  • Loading branch information
lbotinelly authored Nov 8, 2023
2 parents 3f04a95 + e717b31 commit 32fe8b8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
20 changes: 8 additions & 12 deletions Samples/Sample05-Docker/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Sample05_Docker.Model;
using Zen.Base.Extension;
using MongoDB.Bson;
using System.Collections.Generic;
using Zen.MessageQueue.Shared;
using Zen.Web.Host;

Expand All @@ -9,23 +9,19 @@ public class Program
{
public static void Main(string[] args)
{
Zen.MessageQueue.Queue.RegisterType(new TypeA(), true);
Zen.MessageQueue.Queue.RegisterType(new TypeB());
Zen.MessageQueue.Queue.RegisterType(new TypeC(), true);

Zen.MessageQueue.Queue.Receive += model =>
Zen.MessageQueue.Queue.RegisterType("string", true);
Zen.MessageQueue.Queue.RegisterType(new List<string>(), true);

Zen.MessageQueue.Queue.Receive += (model) =>
{
Zen.Base.Log.Add(model.ToJson());
};

Zen.MessageQueue.Queue.Send("string");
Zen.MessageQueue.Queue.Send(new TypeA());
Zen.MessageQueue.Queue.Send(new TypeB());
Zen.MessageQueue.Queue.Send(new TypeC());
Zen.MessageQueue.Queue.Send("string", EDistributionStyle.Broadcast);
Zen.MessageQueue.Queue.Send(new List<string> { "a", "b" }, EDistributionStyle.Broadcast);

Builder.Start<Startup>(args);


}
}
}
3 changes: 2 additions & 1 deletion Zen.Web/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public interface IOptions
public bool UseIisIntegration { get; set; }
public BehaviorDescriptor Behavior { get; set; }
public string CertificateSubject { get; set; }
public string CertificateFile { get; set; }
public string CertificatePassword { get; set; }
public string QualifiedServerName { get; set; }
public string RoutePrefix { get; set; }
Expand All @@ -41,7 +42,6 @@ public interface IOptions
public bool EnableHsts { get; set; }
public bool EnableHttpsRedirection { get; set; }
public PathString DefaultPage { get; set; }

}

[IoCIgnore]
Expand All @@ -56,6 +56,7 @@ public class Options : AutoOptions { }
public bool UseIisIntegration { get; set; } = false;
public BehaviorDescriptor Behavior { get; set; } = new BehaviorDescriptor();
public string CertificateSubject { get; set; }
public string CertificateFile { get; set; }
public string CertificatePassword { get; set; }
public string QualifiedServerName { get; set; }
public string RoutePrefix { get; set; }
Expand Down
36 changes: 27 additions & 9 deletions Zen.Web/Host/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static void Start<T>(string[] args) where T : class
else
{



IWebHostBuilder hostBuilder = WebHost.CreateDefaultBuilder() // Pretty standard pipeline,
.UseContentRoot(Directory.GetCurrentDirectory())
Expand Down Expand Up @@ -145,27 +145,45 @@ private static X509Certificate2 GetCertificate()
return targetCertificate;
}

var certPath = $"{Base.Host.DataDirectory}{Path.DirectorySeparatorChar}certificate{Path.DirectorySeparatorChar}";
string certFile = Current.Options?.CertificateFile;

if (!Directory.Exists(certPath))
if (certFile == null)
{
Log.Warn($"No physical certificate storage [{certPath}]");

var certPath = $"{Base.Host.DataDirectory}{Path.DirectorySeparatorChar}certificate{Path.DirectorySeparatorChar}";

if (!Directory.Exists(certPath))
{
Log.Warn($"No physical certificate storage [{certPath}]");
}
else
{
certFile = Directory.GetFiles(certPath).FirstOrDefault();
if (certFile == null)
Log.Warn($"No certificate in physical storage [{certPath}]");
}

}
else


if (certFile != null)
{
var certFile = Directory.GetFiles(certPath).FirstOrDefault();

if (certFile == null)
Log.Warn($"No certificate in physical storage [{certPath}]");
if (!File.Exists(certFile))
{
Log.KeyValuePair("Physical certificate", "Not Found", Base.Module.Log.Message.EContentType.Warning);
Log.KeyValuePair("Certificate path", certFile, Base.Module.Log.Message.EContentType.Warning);
}
else
{
Log.KeyValuePair("Physical certificate", certFile, Base.Module.Log.Message.EContentType.Info);

if (Current.Options?.CertificatePassword != null)
targetCertificate = new X509Certificate2(File.ReadAllBytes(certFile), Current.Options?.CertificatePassword);
targetCertificate = new X509Certificate2(File.ReadAllBytes(certFile), Current.Options?.CertificatePassword);
}
}


return targetCertificate;
}
}
Expand Down

0 comments on commit 32fe8b8

Please sign in to comment.