diff --git a/src/MMLib.SwaggerForOcelot/Configuration/RouteOptions.cs b/src/MMLib.SwaggerForOcelot/Configuration/RouteOptions.cs index e7d09ad..63c2c04 100644 --- a/src/MMLib.SwaggerForOcelot/Configuration/RouteOptions.cs +++ b/src/MMLib.SwaggerForOcelot/Configuration/RouteOptions.cs @@ -40,6 +40,7 @@ public RouteOptions( string upstreamPathTemplate, string downstreamPathTemplate, string virtualDirectory, + bool dangerousAcceptAnyServerCertificateValidator, IEnumerable upstreamMethods) : this() { SwaggerKey = swaggerKey; @@ -47,6 +48,7 @@ public RouteOptions( DownstreamPathTemplate = downstreamPathTemplate; VirtualDirectory = virtualDirectory; UpstreamHttpMethod = upstreamMethods; + DangerousAcceptAnyServerCertificateValidator = dangerousAcceptAnyServerCertificateValidator; } /// @@ -90,6 +92,11 @@ public RouteOptions( /// public IEnumerable UpstreamHttpMethod { get; set; } + /// + /// Gets or sets the downstream ssl certificate check value. + /// + public bool DangerousAcceptAnyServerCertificateValidator { get; set; } + /// /// Gets or sets the key. /// diff --git a/src/MMLib.SwaggerForOcelot/DependencyInjection/ServiceCollectionExtensions.cs b/src/MMLib.SwaggerForOcelot/DependencyInjection/ServiceCollectionExtensions.cs index 58bc4d8..596aeeb 100644 --- a/src/MMLib.SwaggerForOcelot/DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/MMLib.SwaggerForOcelot/DependencyInjection/ServiceCollectionExtensions.cs @@ -11,6 +11,7 @@ using MMLib.SwaggerForOcelot.Aggregates; using Microsoft.Extensions.DependencyInjection.Extensions; using System.IO; +using System.Net.Http; namespace Microsoft.Extensions.DependencyInjection { @@ -19,6 +20,7 @@ namespace Microsoft.Extensions.DependencyInjection /// public static class ServiceCollectionExtensions { + public const string IgnoreSslCertificate = "HttpClientWithSSLUntrusted"; /// /// Adds configuration for for into . /// @@ -44,6 +46,17 @@ public static IServiceCollection AddSwaggerForOcelot( .AddMemoryCache() .AddSingleton(); + services.AddHttpClient(IgnoreSslCertificate, c => + { + }).ConfigurePrimaryHttpMessageHandler(() => + { + return new HttpClientHandler + { + ClientCertificateOptions = ClientCertificateOption.Manual, + ServerCertificateCustomValidationCallback = (httpRequestMessage, cert, certChain, policyErrors) => true + }; + }); + services.TryAddTransient(); var options = new OcelotSwaggerGenOptions(); diff --git a/src/MMLib.SwaggerForOcelot/Repositories/DownstreamSwaggerDocsRepository.cs b/src/MMLib.SwaggerForOcelot/Repositories/DownstreamSwaggerDocsRepository.cs index 0956aa6..381a0f9 100644 --- a/src/MMLib.SwaggerForOcelot/Repositories/DownstreamSwaggerDocsRepository.cs +++ b/src/MMLib.SwaggerForOcelot/Repositories/DownstreamSwaggerDocsRepository.cs @@ -1,4 +1,5 @@ using Kros.Extensions; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using MMLib.SwaggerForOcelot.Configuration; using MMLib.SwaggerForOcelot.ServiceDiscovery; @@ -42,7 +43,8 @@ public async Task GetSwaggerJsonAsync( string docsVersion = null) { string url = await GetUrlAsync(route, endPoint, docsVersion); - HttpClient httpClient = _httpClientFactory.CreateClient(); + var clientName = (route?.DangerousAcceptAnyServerCertificateValidator ?? false) ? ServiceCollectionExtensions.IgnoreSslCertificate : string.Empty; + HttpClient httpClient = _httpClientFactory.CreateClient(clientName); SetHttpVersion(httpClient, route); AddHeaders(httpClient); diff --git a/src/MMLib.SwaggerForOcelot/RouteOptionsExtensions.cs b/src/MMLib.SwaggerForOcelot/RouteOptionsExtensions.cs index 137bfb1..0603894 100644 --- a/src/MMLib.SwaggerForOcelot/RouteOptionsExtensions.cs +++ b/src/MMLib.SwaggerForOcelot/RouteOptionsExtensions.cs @@ -24,6 +24,7 @@ public static IEnumerable GroupByPaths(this IEnumerable r.UpstreamHttpMethod != null).SelectMany(r => r.UpstreamHttpMethod)) { DownstreamHttpVersion = route.DownstreamHttpVersion,