From 93ce4cbaa8e93af4deff16331a6d7064dfd5f062 Mon Sep 17 00:00:00 2001 From: Shawn Jackson Date: Fri, 21 Jun 2024 22:42:32 -0700 Subject: [PATCH] CU-8687yhbz5 adding option for self-signed certs. --- Core/Resgrid.Config/ApiConfig.cs | 5 +++++ Web/Resgrid.Web.Eventing/Startup.cs | 22 ++++++++++++++++++++-- Web/Resgrid.Web.ServicesCore/Startup.cs | 18 ++++++++++++++++++ Web/Resgrid.WebCore/Startup.cs | 23 +++++++++++++++++++++-- 4 files changed, 64 insertions(+), 4 deletions(-) diff --git a/Core/Resgrid.Config/ApiConfig.cs b/Core/Resgrid.Config/ApiConfig.cs index eeed8370..4adc192c 100644 --- a/Core/Resgrid.Config/ApiConfig.cs +++ b/Core/Resgrid.Config/ApiConfig.cs @@ -19,5 +19,10 @@ public static class ApiConfig /// Key used for authing with the backend internal apis /// public static string BackendInternalApikey = ""; + + /// + /// Used to bypass SSL checks, which is needed for self-signed certs + /// + public static bool BypassSslChecks = false; } } diff --git a/Web/Resgrid.Web.Eventing/Startup.cs b/Web/Resgrid.Web.Eventing/Startup.cs index a7505c15..1e7c36ee 100644 --- a/Web/Resgrid.Web.Eventing/Startup.cs +++ b/Web/Resgrid.Web.Eventing/Startup.cs @@ -49,6 +49,7 @@ using Microsoft.IdentityModel.Tokens; using Org.BouncyCastle.Asn1.Ess; using IPNetwork = Microsoft.AspNetCore.HttpOverrides.IPNetwork; +using System.Net.Http; namespace Resgrid.Web.Eventing { @@ -79,8 +80,6 @@ public void ConfigureServices(IServiceCollection services) bool configResult = ConfigProcessor.LoadAndProcessConfig(Configuration["AppOptions:ConfigPath"]); bool envConfigResult = ConfigProcessor.LoadAndProcessEnvVariables(Configuration.AsEnumerable()); - Framework.Logging.Initialize(ExternalErrorConfig.ExternalErrorServiceUrlForEventing); - var settings = System.Configuration.ConfigurationManager.ConnectionStrings; var element = typeof(ConfigurationElement).GetField("_readOnly", BindingFlags.Instance | BindingFlags.NonPublic); var collection = typeof(ConfigurationElementCollection).GetField("_readOnly", BindingFlags.Instance | BindingFlags.NonPublic); @@ -98,6 +97,25 @@ public void ConfigureServices(IServiceCollection services) collection.SetValue(settings, true); element.SetValue(settings, true); + Framework.Logging.Initialize(ExternalErrorConfig.ExternalErrorServiceUrlForEventing); + + if (Config.ApiConfig.BypassSslChecks) + { + services.AddHttpClient("Name") + .ConfigurePrimaryHttpMessageHandler(() => + { + var handler = new HttpClientHandler + { + AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate, + ServerCertificateCustomValidationCallback = (sender, certificate, chain, errors) => + { + return true; + } + }; + return handler; + }); + } + services.AddCors(); services.AddControllers().AddNewtonsoftJson(options => diff --git a/Web/Resgrid.Web.ServicesCore/Startup.cs b/Web/Resgrid.Web.ServicesCore/Startup.cs index 3a5b6476..f2ba07aa 100644 --- a/Web/Resgrid.Web.ServicesCore/Startup.cs +++ b/Web/Resgrid.Web.ServicesCore/Startup.cs @@ -62,6 +62,7 @@ using Resgrid.Web.ServicesCore.Middleware; using IPNetwork = Microsoft.AspNetCore.HttpOverrides.IPNetwork; using OpenTelemetry.Metrics; +using System.Net.Http; namespace Resgrid.Web.ServicesCore { @@ -121,6 +122,23 @@ public void ConfigureServices(IServiceCollection services) collection.SetValue(settings, true); element.SetValue(settings, true); + if (Config.ApiConfig.BypassSslChecks) + { + services.AddHttpClient("Name") + .ConfigurePrimaryHttpMessageHandler(() => + { + var handler = new HttpClientHandler + { + AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate, + ServerCertificateCustomValidationCallback = (sender, certificate, chain, errors) => + { + return true; + } + }; + return handler; + }); + } + services.AddScoped, IdentityUserStore>(); services.AddScoped, IdentityRoleStore>(); services.AddScoped, ClaimsPrincipalFactory>(); diff --git a/Web/Resgrid.WebCore/Startup.cs b/Web/Resgrid.WebCore/Startup.cs index 03fec74f..935afc5f 100644 --- a/Web/Resgrid.WebCore/Startup.cs +++ b/Web/Resgrid.WebCore/Startup.cs @@ -3,6 +3,7 @@ using System.Configuration; using System.Globalization; using System.Net; +using System.Net.Http; using System.Reflection; using System.Threading.Tasks; using Audit.Core; @@ -56,8 +57,8 @@ namespace Resgrid.Web { public class Startup { - //public IConfiguration Configuration { get; } - public IConfigurationRoot Configuration { get; private set; } + //public IConfiguration Configuration { get; } + public IConfigurationRoot Configuration { get; private set; } public ILifetimeScope AutofacContainer { get; private set; } public AutofacServiceLocator Locator { get; private set; } public IServiceCollection Services { get; private set; } @@ -102,6 +103,24 @@ public void ConfigureServices(IServiceCollection services) Logging.Initialize(ExternalErrorConfig.ExternalErrorServiceUrlForWebsite); + + if (Config.ApiConfig.BypassSslChecks) + { + services.AddHttpClient("Name") + .ConfigurePrimaryHttpMessageHandler(() => + { + var handler = new HttpClientHandler + { + AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate, + ServerCertificateCustomValidationCallback = (sender, certificate, chain, errors) => + { + return true; + } + }; + return handler; + }); + } + services.AddScoped, IdentityUserStore>(); services.AddScoped, IdentityRoleStore>(); services.AddScoped, ClaimsPrincipalFactory>();