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 2 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
10 changes: 10 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,14 @@ 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>

/// <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 Down Expand Up @@ -44,6 +45,17 @@ public static IServiceCollection AddSwaggerForOcelot(
.AddMemoryCache()
.AddSingleton<ISwaggerEndPointProvider, SwaggerEndPointProvider>();

services.AddHttpClient("HttpClientWithSSLUntrusted", 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
Expand Up @@ -42,7 +42,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) ? "HttpClientWithSSLUntrusted" : string.Empty;
tomer-cohen marked this conversation as resolved.
Show resolved Hide resolved
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