Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an option to skip ssl validation #228

Merged
merged 3 commits into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/MMLib.SwaggerForOcelot/Configuration/RouteOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ public RouteOptions(
string upstreamPathTemplate,
string downstreamPathTemplate,
string virtualDirectory,
bool dangerousAcceptAnyServerCertificateValidator,
IEnumerable<string> upstreamMethods) : this()
{
SwaggerKey = swaggerKey;
UpstreamPathTemplate = upstreamPathTemplate;
DownstreamPathTemplate = downstreamPathTemplate;
VirtualDirectory = virtualDirectory;
UpstreamHttpMethod = upstreamMethods;
DangerousAcceptAnyServerCertificateValidator = dangerousAcceptAnyServerCertificateValidator;
}

/// <summary>
Expand Down Expand Up @@ -90,6 +92,11 @@ public RouteOptions(
/// </summary>
public IEnumerable<string> UpstreamHttpMethod { get; set; }

/// <summary>
/// Gets or sets the downstream ssl certificate check value.
/// </summary>
public bool DangerousAcceptAnyServerCertificateValidator { get; set; }

/// <summary>
/// Gets or sets the key.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using MMLib.SwaggerForOcelot.Aggregates;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System.IO;
using System.Net.Http;

namespace Microsoft.Extensions.DependencyInjection
{
Expand All @@ -19,6 +20,7 @@ namespace Microsoft.Extensions.DependencyInjection
/// </summary>
public static class ServiceCollectionExtensions
{
public const string IgnoreSslCertificate = "HttpClientWithSSLUntrusted";
/// <summary>
/// Adds configuration for for <see cref="SwaggerForOcelotMiddleware"/> into <see cref="IServiceCollection"/>.
/// </summary>
Expand All @@ -44,6 +46,17 @@ public static IServiceCollection AddSwaggerForOcelot(
.AddMemoryCache()
.AddSingleton<ISwaggerEndPointProvider, SwaggerEndPointProvider>();

services.AddHttpClient(IgnoreSslCertificate, c =>
{
}).ConfigurePrimaryHttpMessageHandler(() =>
{
return new HttpClientHandler
{
ClientCertificateOptions = ClientCertificateOption.Manual,
ServerCertificateCustomValidationCallback = (httpRequestMessage, cert, certChain, policyErrors) => true
};
});

services.TryAddTransient<IAggregateRouteDocumentationGenerator, AggregateRouteDocumentationGenerator>();

var options = new OcelotSwaggerGenOptions();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Kros.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using MMLib.SwaggerForOcelot.Configuration;
using MMLib.SwaggerForOcelot.ServiceDiscovery;
Expand Down Expand Up @@ -42,7 +43,8 @@ public async Task<string> 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);
Expand Down
1 change: 1 addition & 0 deletions src/MMLib.SwaggerForOcelot/RouteOptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static IEnumerable<RouteOptions> GroupByPaths(this IEnumerable<RouteOptio
route.UpstreamPathTemplate,
route.DownstreamPathTemplate,
p.Key.VirtualDirectory,
route.DangerousAcceptAnyServerCertificateValidator,
p.Where(r => r.UpstreamHttpMethod != null).SelectMany(r => r.UpstreamHttpMethod))
{
DownstreamHttpVersion = route.DownstreamHttpVersion,
Expand Down