forked from microsoft/VirtualClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenSslExecutor.cs
More file actions
300 lines (261 loc) · 13.6 KB
/
Copy pathOpenSslExecutor.cs
File metadata and controls
300 lines (261 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
namespace VirtualClient.Actions
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Abstractions;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using VirtualClient.Common;
using VirtualClient.Common.Extensions;
using VirtualClient.Common.Platform;
using VirtualClient.Common.Telemetry;
using VirtualClient.Contracts;
using VirtualClient.Contracts.Metadata;
using YamlDotNet.Serialization;
/// <summary>
/// Executes the OpenSSL workload.
/// <list type="bullet">
/// <item>
/// <a href='https://www.openssl.org/docs/manmaster/'>OpenSSL manual</a>
/// </item>
/// <item>
/// <a href='https://www.openssl.org/docs/manmaster/man1/openssl-speed.html'>OpenSSL speed command options</a>
/// </item>
/// </list>
/// </summary>
[SupportedPlatforms("linux-arm64,linux-x64,win-x64")]
public class OpenSslExecutor : VirtualClientComponent
{
private IFileSystem fileSystem;
private ISystemManagement systemManagement;
/// <summary>
/// Constructor
/// </summary>
/// <param name="dependencies">Provides required dependencies to the component.</param>
/// <param name="parameters">Parameters defined in the profile or supplied on the command line.</param>
public OpenSslExecutor(IServiceCollection dependencies, IDictionary<string, IConvertible> parameters)
: base(dependencies, parameters)
{
this.fileSystem = dependencies.GetService<IFileSystem>();
this.systemManagement = dependencies.GetService<ISystemManagement>();
}
/// <summary>
/// The command line argument defined in the profile.
/// </summary>
public string CommandArguments
{
get
{
return this.Parameters.GetValue<string>(nameof(OpenSslExecutor.CommandArguments));
}
}
/// <summary>
/// The path to the OpenSSL executable.
/// </summary>
public string ExecutablePath { get; set; }
/// <summary>
/// Defines the path to the OpenSSL package that contains the workload
/// executable/binaries.
/// </summary>
protected DependencyPath Package { get; set; }
/// <summary>
/// Executes the OpenSSL workload.
/// </summary>
protected override Task ExecuteAsync(EventContext telemetryContext, CancellationToken cancellationToken)
{
return this.ExecuteWorkloadAsync(telemetryContext, cancellationToken);
}
/// <summary>
/// Initializes the environment and dependencies for running the OpenSSL workload.
/// </summary>
protected override async Task InitializeAsync(EventContext telemetryContext, CancellationToken cancellationToken)
{
await this.InitializePackageLocationAsync(cancellationToken)
.ConfigureAwait(false);
await this.InitializeWorkloadToolsetsAsync(cancellationToken)
.ConfigureAwait(false);
}
/// <summary>
/// Gets openssl version by running openssl version command.
/// </summary>
private async Task GetOpenSslVersion(CancellationToken cancellationToken, string toolCommand)
{
// The OpenSSL version is not available in the workload output. We need to run a separate command to get the version.
// The command 'openssl version' will return the version of OpenSSL installed on the system.
string opensslVersion = "Unknown";
if (!cancellationToken.IsCancellationRequested)
{
this.Logger.LogTraceMessage($"Executing process 'openssl version' at directory '{this.ExecutablePath}'.");
using (IProcessProxy process = this.systemManagement.ProcessManager.CreateProcess(this.ExecutablePath, "version"))
{
this.SetEnvironmentVariables(process);
await process.StartAndWaitAsync(cancellationToken).ConfigureAwait(false);
process.ThrowIfWorkloadFailed();
opensslVersion = process.StandardOutput?.ToString().Trim() ?? "Unknown";
if (string.IsNullOrWhiteSpace(opensslVersion))
{
opensslVersion = "Unknown";
}
this.MetadataContract.Add("OpenSSLVersion", opensslVersion, MetadataContractCategory.Dependencies);
this.Logger.LogMessage($"{nameof(OpenSslExecutor)}.GetOpenSslVersion", LogLevel.Information, EventContext.Persisted().AddContext("opensslVersion", opensslVersion));
this.MetadataContract.AddForScenario(
"OpenSSL Speed",
toolCommand,
toolVersion: opensslVersion);
}
}
}
private void CaptureMetrics(IProcessProxy workloadProcess, string commandArguments, EventContext telemetryContext, CancellationToken cancellationToken)
{
if (workloadProcess.ExitCode == 0)
{
try
{
// Retrieve OpenSSL version
this.GetOpenSslVersion(cancellationToken, workloadProcess.FullCommand());
this.MetadataContract.Apply(telemetryContext);
OpenSslMetricsParser resultsParser = new OpenSslMetricsParser(workloadProcess.StandardOutput.ToString(), commandArguments);
IList<Metric> metrics = resultsParser.Parse();
this.Logger.LogMetrics(
"OpenSSL",
this.MetricScenario ?? this.Scenario,
workloadProcess.StartTime,
workloadProcess.ExitTime,
metrics,
null,
commandArguments,
this.Tags,
telemetryContext);
}
catch (SchemaException exc)
{
EventContext relatedContext = telemetryContext.Clone()
.AddError(exc);
this.Logger.LogMessage($"{nameof(OpenSslExecutor)}.WorkloadOutputParsingFailed", LogLevel.Warning, relatedContext);
}
}
}
private Task ExecuteWorkloadAsync(EventContext telemetryContext, CancellationToken cancellationToken)
{
string commandArguments = this.GetCommandArguments();
EventContext relatedContext = telemetryContext.Clone()
.AddContext("executable", this.ExecutablePath)
.AddContext("commandArguments", commandArguments);
return this.Logger.LogMessageAsync($"{nameof(OpenSslExecutor)}.ExecuteWorkload", relatedContext, async () =>
{
using (BackgroundOperations profiling = BackgroundOperations.BeginProfiling(this, cancellationToken))
{
using (IProcessProxy process = this.systemManagement.ProcessManager.CreateProcess(this.ExecutablePath, commandArguments))
{
this.SetEnvironmentVariables(process);
this.CleanupTasks.Add(() => process.SafeKill());
try
{
await process.StartAndWaitAsync(cancellationToken).ConfigureAwait();
if (!cancellationToken.IsCancellationRequested)
{
await this.LogProcessDetailsAsync(process, telemetryContext, "OpenSSL", logToFile: true);
process.ThrowIfWorkloadFailed();
this.CaptureMetrics(process, commandArguments, telemetryContext, cancellationToken);
}
}
finally
{
if (!process.HasExited)
{
process.Kill();
}
}
}
}
});
}
private string GetCommandArguments()
{
string commandArguments = this.CommandArguments;
if (this.Platform == PlatformID.Unix)
{
int indexOfCommand = commandArguments.IndexOf("speed", StringComparison.OrdinalIgnoreCase);
// We want to utilize ALL vCPUs/cores on the system in order to work the processor hard. However,
// the -multi flag is NOT supported on Windows builds of the OpenSSL toolset for some reason. To make things
// even more challenging, there is a bug in OpenSSL that will cause it to use the parameter name 'evp' for the
// name of the cipher (instead of the cipher name itself) in the output when the '-evp' parameter is used. As
// such, we are just ensuring the cipher name is at the end of the command arguments. The name of the cipher
// MUST be defined last in the profile.
//
// Example:
// profile: speed -elapsed -seconds 10 aes-256-cbc -> speed -multi 4 -elapsed -seconds 10 aes-256-cbc
commandArguments = commandArguments.Insert(indexOfCommand + 6, $"-multi {Environment.ProcessorCount} ");
}
return commandArguments;
}
private async Task InitializePackageLocationAsync(CancellationToken cancellationToken)
{
if (!cancellationToken.IsCancellationRequested)
{
DependencyPath workloadPackage = await this.systemManagement.PackageManager.GetPackageAsync(this.PackageName, CancellationToken.None)
.ConfigureAwait(false);
if (workloadPackage == null)
{
throw new DependencyException(
$"The expected package '{this.PackageName}' does not exist on the system or is not registered.",
ErrorReason.WorkloadDependencyMissing);
}
workloadPackage = this.PlatformSpecifics.ToPlatformSpecificPath(workloadPackage, this.Platform, this.CpuArchitecture);
this.Package = workloadPackage;
}
}
private async Task InitializeWorkloadToolsetsAsync(CancellationToken cancellationToken)
{
if (!cancellationToken.IsCancellationRequested)
{
if (this.Platform == PlatformID.Unix)
{
this.ExecutablePath = this.PlatformSpecifics.Combine(this.Package.Path, "bin", "openssl");
this.fileSystem.File.ThrowIfFileDoesNotExist(
this.ExecutablePath,
$"OpenSSL executable not found at path '{this.ExecutablePath}'");
await this.systemManagement.MakeFileExecutableAsync(this.ExecutablePath, this.Platform, cancellationToken)
.ConfigureAwait(false);
}
else if (this.Platform == PlatformID.Win32NT)
{
this.ExecutablePath = this.PlatformSpecifics.Combine(this.Package.Path, "bin", "openssl.exe");
this.fileSystem.File.ThrowIfFileDoesNotExist(
this.ExecutablePath,
$"OpenSSL executable not found at path '{this.ExecutablePath}'");
}
}
}
private void SetEnvironmentVariables(IProcessProxy process)
{
if (this.Platform == PlatformID.Win32NT)
{
// The OpenSSL toolset we use was compiled using the Visual Studio 2019 C++ compiler. This creates a runtime
// dependency on the VC runtime .dlls. These are packaged with the OpenSSL package itself in the 'vcruntime'
// folder. We append the location of these VC runtime .dlls to the PATH environment variable so they can be
// found.
string vcRuntimeDllPath = this.PlatformSpecifics.Combine(this.Package.Path, "vcruntime");
string currentPathValue = process.EnvironmentVariables["Path"]?.TrimEnd(';');
this.Logger.LogTraceMessage($"Setting Environment Variable:Path={currentPathValue};{vcRuntimeDllPath}", EventContext.Persisted());
process.EnvironmentVariables["Path"] = $"{currentPathValue};{vcRuntimeDllPath}";
}
else if (this.Platform == PlatformID.Unix)
{
// OpenSSL is typically installed on Linux systems by default. However, we want to use the package version that
// we compiled to ensure we are always running the exact same toolset across Windows and Linux systems. To ensure
// the package version of OpenSSL we use can find its lib/library files required to run, we have to set a special
// environment variable $LD_LIBRARY_PATH
string libPath = this.PlatformSpecifics.Combine(this.Package.Path, "lib64");
this.Logger.LogTraceMessage($"Setting Environment Variable:LD_LIBRARY_PATH={libPath}", EventContext.Persisted());
process.EnvironmentVariables["LD_LIBRARY_PATH"] = libPath;
}
}
}
}