Skip to content

Commit c6615a1

Browse files
committed
prepare reset
1 parent 65c9f73 commit c6615a1

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/BenchmarksApps/TLS/HttpSys/NetSh/NetshConfigurator.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ public static class NetshConfigurator
55
private static readonly NetShWrapper _netshWrapper = new();
66
private static string _certThumbprint;
77

8+
private static string _resetCertThumbprint;
9+
810
public static SslCertBinding PreConfigureNetsh(
911
string httpsIpPort,
1012
int certPublicKeyLength = 2048,
@@ -43,22 +45,29 @@ public static SslCertBinding PreConfigureNetsh(
4345
public static void LogCurrentSslCertBinding(string httpsIpPort)
4446
=> _netshWrapper.LogSslCertBinding(httpsIpPort);
4547

46-
public static void ResetNetshConfiguration(
47-
string httpsIpPort,
48-
int certPublicKeyLength = 4096)
48+
public static void PrepareResetNetsh(string httpsIpPort, int certPublicKeyLength = 4096)
49+
{
50+
if (!_netshWrapper.TrySelfSignCertificate(httpsIpPort, certPublicKeyLength, out _resetCertThumbprint))
51+
{
52+
throw new ApplicationException($"Failed to self-sign a cert for '{httpsIpPort}'.");
53+
}
54+
}
55+
56+
public static void ResetNetshConfiguration(string httpsIpPort)
4957
{
5058
// delete cert binding and cert itself. We want it to be as clean and deterministic as possible (even if more actions are performed)
5159
_netshWrapper.DeleteBindingIfExists(httpsIpPort);
5260
SslCertificatesConfigurator.RemoveCertificate(_certThumbprint);
5361

54-
if (!_netshWrapper.TrySelfSignCertificate(httpsIpPort, certPublicKeyLength, out _certThumbprint))
62+
if (string.IsNullOrEmpty(_resetCertThumbprint))
5563
{
56-
throw new ApplicationException($"Failed to self-sign a cert for '{httpsIpPort}'.");
64+
throw new ApplicationException($"Reset certificate is not prepared for '{httpsIpPort}'.");
5765
}
5866

67+
// reset certificate was prepared in advance - just bind it at this moment
5968
_netshWrapper.AddCertBinding(
6069
httpsIpPort,
61-
_certThumbprint,
70+
_resetCertThumbprint,
6271
disablesessionid: NetShFlag.NotSet,
6372
enablesessionticket: NetShFlag.NotSet,
6473
clientCertNegotiation: NetShFlag.NotSet);

src/BenchmarksApps/TLS/HttpSys/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
disablesessionid: NetShFlag.Enable,
3030
enableSessionTicket: NetShFlag.Disabled);
3131

32+
// because app shutdown is on a timeout, we need to prepare the reset (pre-generate certificate)
33+
NetshConfigurator.PrepareResetNetsh(httpsIpPort, certPublicKeyLength: 4096);
34+
3235
#pragma warning disable CA1416 // Can be launched only on Windows (HttpSys)
3336
builder.WebHost.UseHttpSys(options =>
3437
{
@@ -137,5 +140,5 @@
137140
Console.WriteLine("Application stopped.");
138141

139142
Console.WriteLine("Starting netsh rollback configuration...");
140-
NetshConfigurator.ResetNetshConfiguration(httpsIpPort, certPublicKeyLength: 4096); // a default value
143+
NetshConfigurator.ResetNetshConfiguration(httpsIpPort);
141144
Console.WriteLine($"Reset netsh (ipport={httpsIpPort}) completed.");

0 commit comments

Comments
 (0)