Skip to content

Commit 05736a1

Browse files
committed
self-sign certificate if not exists before benchmark
1 parent 2473d16 commit 05736a1

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/BenchmarksApps/TLS/HttpSys/NetShWrapper.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,33 @@ public static void SetTestCertBinding(string ipPort, bool enableClientCertNegoti
8181
Console.WriteLine("Configured binding for testCert for http.sys");
8282
}
8383

84+
public static bool TrySelfSignCertificate(string ipPort, out string certThumbprint)
85+
{
86+
certThumbprint = string.Empty;
87+
try
88+
{
89+
// Extract the IP address from ipPort
90+
string ipAddress = ipPort.Split(':')[0];
91+
92+
// Generate a self-signed certificate using PowerShell
93+
string command = $"New-SelfSignedCertificate -CertStoreLocation cert:\\LocalMachine\\My -DnsName {ipAddress}";
94+
string output = ExecutePowershellCommand(command);
95+
96+
// Extract the thumbprint from the output
97+
var lines = output.Split("\r\n", StringSplitOptions.RemoveEmptyEntries);
98+
var lastLine = lines[^1];
99+
certThumbprint = lastLine.Split(" ", StringSplitOptions.RemoveEmptyEntries)[0];
100+
101+
Console.WriteLine($"Self-signed certificate for {ipAddress}");
102+
return true;
103+
}
104+
catch (Exception ex)
105+
{
106+
Console.WriteLine("Failed to self-sign the certificate: " + ex.Message);
107+
return false;
108+
}
109+
}
110+
84111
public static void SetCertBinding(string ipPort, string certThumbprint, string appId = null, bool enableClientCertNegotiation = false)
85112
{
86113
var negotiateClientCert = enableClientCertNegotiation ? "enable" : "disable";
@@ -90,8 +117,12 @@ public static void SetCertBinding(string ipPort, string certThumbprint, string a
90117
}
91118
string command = $"http add sslcert ipport={ipPort} certstorename=MY certhash={certThumbprint} appid={{{appId}}} clientcertnegotiation={negotiateClientCert}";
92119
ExecuteNetShCommand(command);
120+
Console.WriteLine($"Performed cert bindign for {ipPort}");
93121
}
94122

123+
private static string ExecutePowershellCommand(string command, bool alwaysLogOutput = false)
124+
=> ExecuteCommand("powershell.exe", command, alwaysLogOutput);
125+
95126
private static string ExecuteNetShCommand(string command, bool alwaysLogOutput = false)
96127
=> ExecuteCommand("netsh", command, alwaysLogOutput);
97128

src/BenchmarksApps/TLS/HttpSys/Program.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@
2020
// existing netsh bindings to restore after the benchmark run
2121
if (!NetShWrapper.BindingExists(httpsIpPort, out var originalCertThumbprint, out var originalAppId))
2222
{
23-
Console.WriteLine("WARNING: no binding existed...");
24-
throw new ApplicationException($"SslCert binding should exist for '{httpsIpPort}' before. Infrastructure error.");
23+
Console.WriteLine($"No binding existed. Need to self-sign it and bind to '{httpsIpPort}'");
24+
if (!NetShWrapper.TrySelfSignCertificate(httpsIpPort, out originalCertThumbprint))
25+
{
26+
throw new ApplicationException($"Failed to setup ssl binding for '{httpsIpPort}'. Please unblock the VM.");
27+
}
28+
NetShWrapper.SetCertBinding(httpsIpPort, originalCertThumbprint);
2529
}
2630

2731
#pragma warning disable CA1416 // Can be launched only on Windows (HttpSys)

0 commit comments

Comments
 (0)