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

Add support for PEM certificates #787

Merged
merged 11 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Support PEM
  • Loading branch information
StefH committed Aug 11, 2022
commit 200aa53ab631946f7e3a8fd896895581e6fe2226
39 changes: 26 additions & 13 deletions src/WireMock.Net/HttpsCertificate/CertificateLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ namespace WireMock.HttpsCertificate;

internal static class CertificateLoader
{
private const string ExtensionPEM = ".PEM";

/// <summary>
/// Used by the WireMock.Net server
/// </summary>
public static X509Certificate2 LoadCertificate(
string storeName,
string storeLocation,
string thumbprintOrSubjectName,
string filePath,
string password,
string? storeName,
string? storeLocation,
string? thumbprintOrSubjectName,
string? filePath,
string? password,
string host)
{
if (!string.IsNullOrEmpty(storeName) && !string.IsNullOrEmpty(storeLocation))
Expand Down Expand Up @@ -47,19 +49,30 @@ public static X509Certificate2 LoadCertificate(
#if NETSTANDARD || NET46
certStore.Dispose();
#else
certStore.Close();
certStore.Close();
#endif
}
}

if (!string.IsNullOrEmpty(filePath) && !string.IsNullOrEmpty(password))
{
return new X509Certificate2(filePath, password);
}

if (!string.IsNullOrEmpty(filePath))
{
return new X509Certificate2(filePath);
if (filePath!.EndsWith(ExtensionPEM, StringComparison.OrdinalIgnoreCase))
{
if (!string.IsNullOrEmpty(password))
{
throw new NotSupportedException();
}
}

if (!string.IsNullOrEmpty(password))
{
return new X509Certificate2(filePath, password);
}

if (!string.IsNullOrEmpty(filePath))
{
return new X509Certificate2(filePath);
}
}

throw new InvalidOperationException("X509StoreName and X509StoreLocation OR X509CertificateFilePath are mandatory. Note that X509CertificatePassword is optional.");
Expand Down Expand Up @@ -97,7 +110,7 @@ public static X509Certificate2 LoadCertificate(string thumbprintOrSubjectName)
#if NETSTANDARD || NET46
certStore.Dispose();
#else
certStore.Close();
certStore.Close();
#endif
}
}
Expand Down
1 change: 0 additions & 1 deletion src/WireMock.Net/Owin/AspNetCoreSelfHost.NETStandard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using WireMock.HttpsCertificate;
using WireMock.Types;

namespace WireMock.Owin
{
Expand Down
78 changes: 39 additions & 39 deletions src/WireMock.Net/Owin/AspNetCoreSelfHost.NETStandard13.cs
Original file line number Diff line number Diff line change
@@ -1,63 +1,63 @@
#if USE_ASPNETCORE && NETSTANDARD1_3
#if USE_ASPNETCORE && NETSTANDARD1_3
using System.Collections.Generic;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using WireMock.HttpsCertificate;

namespace WireMock.Owin
namespace WireMock.Owin;

internal partial class AspNetCoreSelfHost
{
internal partial class AspNetCoreSelfHost
private static void SetKestrelOptionsLimits(KestrelServerOptions options)
{
private static void SetKestrelOptionsLimits(KestrelServerOptions options)
{
options.Limits.MaxRequestBufferSize = null;
options.Limits.MaxRequestHeaderCount = 100;
options.Limits.MaxResponseBufferSize = null;
}
options.Limits.MaxRequestBufferSize = null;
options.Limits.MaxRequestHeaderCount = 100;
options.Limits.MaxResponseBufferSize = null;
}

private static void SetHttpsAndUrls(KestrelServerOptions options, IWireMockMiddlewareOptions wireMockMiddlewareOptions, IEnumerable<HostUrlDetails> urlDetails)
private static void SetHttpsAndUrls(KestrelServerOptions options, IWireMockMiddlewareOptions wireMockMiddlewareOptions, IEnumerable<HostUrlDetails> urlDetails)
{
foreach (var urlDetail in urlDetails)
{
foreach (var urlDetail in urlDetails)
if (urlDetail.IsHttps)
{
if (urlDetail.IsHttps)
if (wireMockMiddlewareOptions.CustomCertificateDefined)
{
options.UseHttps(CertificateLoader.LoadCertificate(
wireMockMiddlewareOptions.X509StoreName,
wireMockMiddlewareOptions.X509StoreLocation,
wireMockMiddlewareOptions.X509ThumbprintOrSubjectName,
wireMockMiddlewareOptions.X509CertificateFilePath,
wireMockMiddlewareOptions.X509CertificatePassword,
urlDetail.Host)
);
}
else
{
if (wireMockMiddlewareOptions.CustomCertificateDefined)
{
options.UseHttps(CertificateLoader.LoadCertificate(
wireMockMiddlewareOptions.X509StoreName,
wireMockMiddlewareOptions.X509StoreLocation,
wireMockMiddlewareOptions.X509ThumbprintOrSubjectName,
wireMockMiddlewareOptions.X509CertificateFilePath,
wireMockMiddlewareOptions.X509CertificatePassword,
urlDetail.Host)
);
}
else
{
options.UseHttps(PublicCertificateHelper.GetX509Certificate2());
}
options.UseHttps(PublicCertificateHelper.GetX509Certificate2());
}
}
}
}
}

internal static class IWebHostBuilderExtensions
{
internal static IWebHostBuilder ConfigureAppConfigurationUsingEnvironmentVariables(this IWebHostBuilder builder) => builder;

internal static class IWebHostBuilderExtensions
internal static IWebHostBuilder ConfigureKestrelServerOptions(this IWebHostBuilder builder)
{
internal static IWebHostBuilder ConfigureAppConfigurationUsingEnvironmentVariables(this IWebHostBuilder builder) => builder;
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.Build();

internal static IWebHostBuilder ConfigureKestrelServerOptions(this IWebHostBuilder builder)
return builder.ConfigureServices(services =>
{
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.Build();

return builder.ConfigureServices(services =>
{
services.Configure<KestrelServerOptions>(configuration.GetSection("Kestrel"));
});
}
services.Configure<KestrelServerOptions>(configuration.GetSection("Kestrel"));
});
}
}

#endif
59 changes: 29 additions & 30 deletions src/WireMock.Net/Owin/IWireMockMiddlewareOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,62 +12,61 @@
using Microsoft.Extensions.DependencyInjection;
#endif

namespace WireMock.Owin
namespace WireMock.Owin;

internal interface IWireMockMiddlewareOptions
{
internal interface IWireMockMiddlewareOptions
{
IWireMockLogger Logger { get; set; }
IWireMockLogger Logger { get; set; }

TimeSpan? RequestProcessingDelay { get; set; }
TimeSpan? RequestProcessingDelay { get; set; }

IStringMatcher? AuthenticationMatcher { get; set; }
IStringMatcher? AuthenticationMatcher { get; set; }

bool? AllowPartialMapping { get; set; }
bool? AllowPartialMapping { get; set; }

ConcurrentDictionary<Guid, IMapping> Mappings { get; }
ConcurrentDictionary<Guid, IMapping> Mappings { get; }

ConcurrentDictionary<string, ScenarioState> Scenarios { get; }
ConcurrentDictionary<string, ScenarioState> Scenarios { get; }

ConcurrentObservableCollection<LogEntry> LogEntries { get; }
ConcurrentObservableCollection<LogEntry> LogEntries { get; }

int? RequestLogExpirationDuration { get; set; }
int? RequestLogExpirationDuration { get; set; }

int? MaxRequestLogCount { get; set; }
int? MaxRequestLogCount { get; set; }

Action<IAppBuilder>? PreWireMockMiddlewareInit { get; set; }
Action<IAppBuilder>? PreWireMockMiddlewareInit { get; set; }

Action<IAppBuilder>? PostWireMockMiddlewareInit { get; set; }
Action<IAppBuilder>? PostWireMockMiddlewareInit { get; set; }

#if USE_ASPNETCORE
Action<IServiceCollection>? AdditionalServiceRegistration { get; set; }
Action<IServiceCollection>? AdditionalServiceRegistration { get; set; }

CorsPolicyOptions? CorsPolicyOptions { get; set; }
CorsPolicyOptions? CorsPolicyOptions { get; set; }
#endif

IFileSystemHandler? FileSystemHandler { get; set; }
IFileSystemHandler? FileSystemHandler { get; set; }

bool? AllowBodyForAllHttpMethods { get; set; }
bool? AllowBodyForAllHttpMethods { get; set; }

bool? AllowOnlyDefinedHttpStatusCodeInResponse { get; set; }
bool? AllowOnlyDefinedHttpStatusCodeInResponse { get; set; }

bool? DisableJsonBodyParsing { get; set; }
bool? DisableJsonBodyParsing { get; set; }

bool? DisableRequestBodyDecompressing { get; set; }
bool? DisableRequestBodyDecompressing { get; set; }

bool? HandleRequestsSynchronously { get; set; }
bool? HandleRequestsSynchronously { get; set; }

string? X509StoreName { get; set; }
string? X509StoreName { get; set; }

string? X509StoreLocation { get; set; }
string? X509StoreLocation { get; set; }

string? X509ThumbprintOrSubjectName { get; set; }
string? X509ThumbprintOrSubjectName { get; set; }

string? X509CertificateFilePath { get; set; }
string? X509CertificateFilePath { get; set; }

string? X509CertificatePassword { get; set; }
string? X509CertificatePassword { get; set; }

bool CustomCertificateDefined { get; }
bool CustomCertificateDefined { get; }

bool? SaveUnmatchedRequests { get; set; }
}
bool? SaveUnmatchedRequests { get; set; }
}
89 changes: 44 additions & 45 deletions src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,77 +12,76 @@
using Microsoft.Extensions.DependencyInjection;
#endif

namespace WireMock.Owin
namespace WireMock.Owin;

internal class WireMockMiddlewareOptions : IWireMockMiddlewareOptions
{
internal class WireMockMiddlewareOptions : IWireMockMiddlewareOptions
{
public IWireMockLogger Logger { get; set; }
public IWireMockLogger Logger { get; set; }

public TimeSpan? RequestProcessingDelay { get; set; }
public TimeSpan? RequestProcessingDelay { get; set; }

public IStringMatcher AuthenticationMatcher { get; set; }
public IStringMatcher AuthenticationMatcher { get; set; }

public bool? AllowPartialMapping { get; set; }
public bool? AllowPartialMapping { get; set; }

public ConcurrentDictionary<Guid, IMapping> Mappings { get; } = new ConcurrentDictionary<Guid, IMapping>();
public ConcurrentDictionary<Guid, IMapping> Mappings { get; } = new ConcurrentDictionary<Guid, IMapping>();

public ConcurrentDictionary<string, ScenarioState> Scenarios { get; } = new ConcurrentDictionary<string, ScenarioState>();
public ConcurrentDictionary<string, ScenarioState> Scenarios { get; } = new();

public ConcurrentObservableCollection<LogEntry> LogEntries { get; } = new ConcurrentObservableCollection<LogEntry>();
public ConcurrentObservableCollection<LogEntry> LogEntries { get; } = new();

public int? RequestLogExpirationDuration { get; set; }
public int? RequestLogExpirationDuration { get; set; }

public int? MaxRequestLogCount { get; set; }
public int? MaxRequestLogCount { get; set; }

public Action<IAppBuilder> PreWireMockMiddlewareInit { get; set; }
public Action<IAppBuilder>? PreWireMockMiddlewareInit { get; set; }

public Action<IAppBuilder> PostWireMockMiddlewareInit { get; set; }
public Action<IAppBuilder>? PostWireMockMiddlewareInit { get; set; }

#if USE_ASPNETCORE
public Action<IServiceCollection> AdditionalServiceRegistration { get; set; }
public Action<IServiceCollection>? AdditionalServiceRegistration { get; set; }

public CorsPolicyOptions? CorsPolicyOptions { get; set; }
public CorsPolicyOptions? CorsPolicyOptions { get; set; }
#endif

/// <inheritdoc cref="IWireMockMiddlewareOptions.FileSystemHandler"/>
public IFileSystemHandler FileSystemHandler { get; set; }
/// <inheritdoc cref="IWireMockMiddlewareOptions.FileSystemHandler"/>
public IFileSystemHandler FileSystemHandler { get; set; }

/// <inheritdoc cref="IWireMockMiddlewareOptions.AllowBodyForAllHttpMethods"/>
public bool? AllowBodyForAllHttpMethods { get; set; }
/// <inheritdoc cref="IWireMockMiddlewareOptions.AllowBodyForAllHttpMethods"/>
public bool? AllowBodyForAllHttpMethods { get; set; }

/// <inheritdoc cref="IWireMockMiddlewareOptions.AllowOnlyDefinedHttpStatusCodeInResponse"/>
public bool? AllowOnlyDefinedHttpStatusCodeInResponse { get; set; }
/// <inheritdoc cref="IWireMockMiddlewareOptions.AllowOnlyDefinedHttpStatusCodeInResponse"/>
public bool? AllowOnlyDefinedHttpStatusCodeInResponse { get; set; }

/// <inheritdoc cref="IWireMockMiddlewareOptions.DisableJsonBodyParsing"/>
public bool? DisableJsonBodyParsing { get; set; }
/// <inheritdoc cref="IWireMockMiddlewareOptions.DisableJsonBodyParsing"/>
public bool? DisableJsonBodyParsing { get; set; }

/// <inheritdoc cref="IWireMockMiddlewareOptions.DisableRequestBodyDecompressing"/>
public bool? DisableRequestBodyDecompressing { get; set; }
/// <inheritdoc cref="IWireMockMiddlewareOptions.DisableRequestBodyDecompressing"/>
public bool? DisableRequestBodyDecompressing { get; set; }

/// <inheritdoc cref="IWireMockMiddlewareOptions.HandleRequestsSynchronously"/>
public bool? HandleRequestsSynchronously { get; set; }
/// <inheritdoc cref="IWireMockMiddlewareOptions.HandleRequestsSynchronously"/>
public bool? HandleRequestsSynchronously { get; set; }

/// <inheritdoc cref="IWireMockMiddlewareOptions.X509StoreName"/>
public string X509StoreName { get; set; }
/// <inheritdoc cref="IWireMockMiddlewareOptions.X509StoreName"/>
public string? X509StoreName { get; set; }

/// <inheritdoc cref="IWireMockMiddlewareOptions.X509StoreLocation"/>
public string X509StoreLocation { get; set; }
/// <inheritdoc cref="IWireMockMiddlewareOptions.X509StoreLocation"/>
public string? X509StoreLocation { get; set; }

/// <inheritdoc cref="IWireMockMiddlewareOptions.X509ThumbprintOrSubjectName"/>
public string X509ThumbprintOrSubjectName { get; set; }
/// <inheritdoc cref="IWireMockMiddlewareOptions.X509ThumbprintOrSubjectName"/>
public string? X509ThumbprintOrSubjectName { get; set; }

/// <inheritdoc cref="IWireMockMiddlewareOptions.X509CertificateFilePath"/>
public string X509CertificateFilePath { get; set; }
/// <inheritdoc cref="IWireMockMiddlewareOptions.X509CertificateFilePath"/>
public string? X509CertificateFilePath { get; set; }

/// <inheritdoc cref="IWireMockMiddlewareOptions.X509CertificatePassword"/>
public string X509CertificatePassword { get; set; }
/// <inheritdoc cref="IWireMockMiddlewareOptions.X509CertificatePassword"/>
public string? X509CertificatePassword { get; set; }

/// <inheritdoc cref="IWireMockMiddlewareOptions.CustomCertificateDefined"/>
public bool CustomCertificateDefined =>
!string.IsNullOrEmpty(X509StoreName) && !string.IsNullOrEmpty(X509StoreLocation) ||
!string.IsNullOrEmpty(X509CertificateFilePath);
/// <inheritdoc cref="IWireMockMiddlewareOptions.CustomCertificateDefined"/>
public bool CustomCertificateDefined =>
!string.IsNullOrEmpty(X509StoreName) && !string.IsNullOrEmpty(X509StoreLocation) ||
!string.IsNullOrEmpty(X509CertificateFilePath);

/// <inheritdoc cref="IWireMockMiddlewareOptions.SaveUnmatchedRequests"/>
public bool? SaveUnmatchedRequests { get; set; }
}
/// <inheritdoc cref="IWireMockMiddlewareOptions.SaveUnmatchedRequests"/>
public bool? SaveUnmatchedRequests { get; set; }
}