Skip to content

Commit 546753b

Browse files
committed
dont use scripts
1 parent deedf40 commit 546753b

File tree

6 files changed

+99
-50
lines changed

6 files changed

+99
-50
lines changed

scenarios/tls.benchmarks.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ scenarios:
5555
mTLS: true
5656
tlsRenegotiation: true
5757
certValidationConsoleEnabled: false # only for debug purposes
58-
options:
59-
beforeScript: "powershell -File .\\setup-httpsys.ps1"
60-
afterScript: "powershell -File .\\shutdown-httpsys.ps1"
6158
load:
6259
job: httpclient
6360
variables:

src/BenchmarksApps/TLS/HttpSys/HttpSys.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<None Update="scripts\setup-httpsys.ps1">
11-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
12-
</None>
1310
<None Update="testCert.pfx">
1411
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1512
</None>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System.Diagnostics;
2+
using System.Security.Cryptography.X509Certificates;
3+
4+
namespace HttpSys
5+
{
6+
public static class NetShWrapper
7+
{
8+
public static void DisableHttpSysMutualTls(string ipPort)
9+
{
10+
Console.WriteLine("Disabling mTLS for http.sys");
11+
12+
string command = $"http delete sslcert ipport={ipPort}";
13+
ExecuteNetShCommand(command);
14+
15+
Console.WriteLine("Disabled http.sys settings for mTLS");
16+
}
17+
18+
public static bool HttpSysCertBindingExists(string ipPort)
19+
{
20+
return true;
21+
}
22+
23+
public static void EnableHttpSysMutualTls(string ipPort)
24+
{
25+
Console.WriteLine("Setting up mTLS for http.sys");
26+
27+
var certificate = LoadCertificate();
28+
Console.WriteLine("Loaded `testCert.pfx` from local file system");
29+
using (var store = new X509Store(StoreName.My, StoreLocation.LocalMachine))
30+
{
31+
store.Open(OpenFlags.ReadWrite);
32+
store.Add(certificate);
33+
Console.WriteLine("Added `testCert.pfx` to localMachine cert store");
34+
store.Close();
35+
}
36+
37+
string certThumbprint = certificate.Thumbprint;
38+
string appId = Guid.NewGuid().ToString();
39+
40+
string command = $"http add sslcert ipport={ipPort} certstorename=MY certhash={certThumbprint} appid={{{appId}}} clientcertnegotiation=enable";
41+
ExecuteNetShCommand(command);
42+
43+
Console.WriteLine("Configured http.sys settings for mTLS");
44+
}
45+
46+
private static void ExecuteNetShCommand(string command)
47+
{
48+
ProcessStartInfo processInfo = new ProcessStartInfo("netsh", command)
49+
{
50+
RedirectStandardOutput = true,
51+
RedirectStandardError = true,
52+
UseShellExecute = false,
53+
CreateNoWindow = true
54+
};
55+
56+
Console.WriteLine($"Executing command: `netsh {command}`");
57+
using Process process = Process.Start(processInfo)!;
58+
string output = process.StandardOutput.ReadToEnd();
59+
process.WaitForExit();
60+
61+
if (process.ExitCode != 0)
62+
{
63+
throw new InvalidOperationException($"netsh command execution failure: {output}");
64+
}
65+
}
66+
67+
#pragma warning disable SYSLIB0057 // Type or member is obsolete
68+
private static X509Certificate2 LoadCertificate() => File.Exists("testCert.pfx")
69+
? new X509Certificate2("testCert.pfx", "testPassword", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable)
70+
: new X509Certificate2("../testCert.pfx", "testPassword", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
71+
#pragma warning restore SYSLIB0057 // Type or member is obsolete
72+
}
73+
}

src/BenchmarksApps/TLS/HttpSys/Program.cs

Lines changed: 26 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
using System.Diagnostics;
2-
using System.Security.Cryptography.X509Certificates;
1+
using HttpSys;
32
using Microsoft.AspNetCore.Server.HttpSys;
43

54
var builder = WebApplication.CreateBuilder(args);
65
builder.Logging.ClearProviders();
76

7+
Console.WriteLine($"args: {string.Join(" ", args)}");
8+
Console.WriteLine();
9+
810
var writeCertValidationEventsToConsole = bool.TryParse(builder.Configuration["certValidationConsoleEnabled"], out var certValidationConsoleEnabled) && certValidationConsoleEnabled;
911
var statsEnabled = bool.TryParse(builder.Configuration["statsEnabled"], out var connectionStatsEnabledConfig) && connectionStatsEnabledConfig;
1012
var mTlsEnabled = bool.TryParse(builder.Configuration["mTLS"], out var mTlsEnabledConfig) && mTlsEnabledConfig;
1113
var tlsRenegotiationEnabled = bool.TryParse(builder.Configuration["tlsRenegotiation"], out var tlsRenegotiationEnabledConfig) && tlsRenegotiationEnabledConfig;
1214
var listeningEndpoints = builder.Configuration["urls"] ?? "https://localhost:5000/";
15+
var httpsIpPort = listeningEndpoints.Split(";").First(x => x.Contains("https")).Replace("https://", "");
1316

1417
#pragma warning disable CA1416 // Can be launched only on Windows (HttpSys)
1518
builder.WebHost.UseHttpSys(options =>
@@ -19,7 +22,7 @@
1922
});
2023
#pragma warning restore CA1416 // Can be launched only on Windows (HttpSys)
2124

22-
var app = builder.Build();
25+
var app = builder.Build();
2326

2427
app.MapGet("/hello-world", () =>
2528
{
@@ -43,13 +46,30 @@
4346

4447
if (mTlsEnabled)
4548
{
49+
var hostAppLifetime = app.Services.GetService<IHostApplicationLifetime>();
50+
hostAppLifetime!.ApplicationStopping.Register(OnShutdown);
51+
52+
void OnShutdown()
53+
{
54+
try
55+
{
56+
NetShWrapper.DisableHttpSysMutualTls(ipPort: httpsIpPort);
57+
}
58+
catch
59+
{
60+
Console.WriteLine("Failed to disable HTTP.SYS mTLS settings");
61+
throw;
62+
}
63+
}
64+
4665
try
4766
{
48-
ConfigureHttpSysForMutualTls();
67+
NetShWrapper.EnableHttpSysMutualTls(ipPort: httpsIpPort);
4968
}
50-
catch (Exception ex)
69+
catch
5170
{
52-
throw new Exception($"Http.Sys configuration for mTLS failed. Current dir: {Directory.GetCurrentDirectory()}", innerException: ex);
71+
Console.WriteLine($"Http.Sys configuration for mTLS failed");
72+
throw;
5373
}
5474
}
5575

@@ -105,39 +125,3 @@
105125
Console.WriteLine("Application started.");
106126
await app.WaitForShutdownAsync();
107127

108-
void ConfigureHttpSysForMutualTls()
109-
{
110-
Console.WriteLine("Setting up mTLS for http.sys");
111-
112-
var certificate = new X509Certificate2("../testCert.pfx", "testPassword", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
113-
using (var store = new X509Store(StoreName.My, StoreLocation.LocalMachine))
114-
{
115-
store.Open(OpenFlags.ReadWrite);
116-
store.Add(certificate);
117-
store.Close();
118-
}
119-
120-
string certThumbprint = certificate.Thumbprint;
121-
string appId = Guid.NewGuid().ToString();
122-
123-
string command = $"http add sslcert ipport=0.0.0.0:5000 certhash={certThumbprint} appid={{{appId}}} clientcertnegotiation=enable";
124-
ProcessStartInfo processInfo = new ProcessStartInfo("netsh", command)
125-
{
126-
RedirectStandardOutput = true,
127-
RedirectStandardError = true,
128-
UseShellExecute = false,
129-
CreateNoWindow = true
130-
};
131-
132-
using Process process = Process.Start(processInfo)!;
133-
string output = process.StandardOutput.ReadToEnd();
134-
string error = process.StandardError.ReadToEnd();
135-
process.WaitForExit();
136-
137-
if (process.ExitCode != 0)
138-
{
139-
throw new InvalidOperationException($"Failed to configure http.sys: {error}");
140-
}
141-
142-
Console.WriteLine("Configured http.sys settings for mTLS");
143-
}

src/BenchmarksApps/TLS/HttpSys/scripts/setup-httpsys.ps1

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/BenchmarksApps/TLS/HttpSys/scripts/shutdown-httpsys.ps1

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)