-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathGo117ComponentDetector.cs
More file actions
241 lines (202 loc) · 9.03 KB
/
Copy pathGo117ComponentDetector.cs
File metadata and controls
241 lines (202 loc) · 9.03 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
namespace Microsoft.ComponentDetection.Detectors.Go;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reactive.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.ComponentDetection.Common;
using Microsoft.ComponentDetection.Common.Telemetry.Records;
using Microsoft.ComponentDetection.Contracts;
using Microsoft.ComponentDetection.Contracts.Internal;
using Microsoft.ComponentDetection.Contracts.TypedComponent;
using Microsoft.Extensions.Logging;
public class Go117ComponentDetector : FileComponentDetector, IExperimentalDetector
{
private readonly HashSet<string> projectRoots = [];
private readonly ICommandLineInvocationService commandLineInvocationService;
private readonly IGoParserFactory goParserFactory;
private readonly IEnvironmentVariableService envVarService;
public Go117ComponentDetector(
IComponentStreamEnumerableFactory componentStreamEnumerableFactory,
IObservableDirectoryWalkerFactory walkerFactory,
ICommandLineInvocationService commandLineInvocationService,
IEnvironmentVariableService envVarService,
ILogger<GoComponentDetector> logger,
IFileUtilityService fileUtilityService,
IGoParserFactory goParserFactory)
{
this.ComponentStreamEnumerableFactory = componentStreamEnumerableFactory;
this.Scanner = walkerFactory;
this.commandLineInvocationService = commandLineInvocationService;
this.Logger = logger;
this.goParserFactory = goParserFactory;
this.envVarService = envVarService;
}
public override string Id => "Go117";
public override IEnumerable<string> Categories => [Enum.GetName(typeof(DetectorClass), DetectorClass.GoMod)];
public override IList<string> SearchPatterns { get; } = ["go.mod", "go.sum"];
public override IEnumerable<ComponentType> SupportedComponentTypes { get; } = [ComponentType.Go];
public override int Version => 3;
protected override Task<IObservable<ProcessRequest>> OnPrepareDetectionAsync(
IObservable<ProcessRequest> processRequests,
IDictionary<string, string> detectorArgs,
CancellationToken cancellationToken = default)
{
var goModProcessRequests = processRequests.Where(processRequest =>
{
if (Path.GetFileName(processRequest.ComponentStream.Location) != "go.sum")
{
return true;
}
var goModFile = this.FindAdjacentGoModComponentStreams(processRequest).FirstOrDefault();
try
{
if (goModFile == null)
{
this.Logger.LogDebug(
"go.sum file found without an adjacent go.mod file. Location: {Location}",
processRequest.ComponentStream.Location);
return true;
}
return GoDetectorUtils.ShouldIncludeGoSumFromDetection(goSumFilePath: processRequest.ComponentStream.Location, goModFile, this.Logger);
}
finally
{
goModFile?.Stream.Dispose();
}
});
return Task.FromResult(goModProcessRequests);
}
protected override async Task OnFileFoundAsync(ProcessRequest processRequest, IDictionary<string, string> detectorArgs, CancellationToken cancellationToken = default)
{
var singleFileComponentRecorder = processRequest.SingleFileComponentRecorder;
var file = processRequest.ComponentStream;
var projectRootDirectory = Directory.GetParent(file.Location);
if (this.projectRoots.Any(path => projectRootDirectory.FullName.StartsWith(path)))
{
return;
}
using var record = new GoGraphTelemetryRecord();
var wasGoCliDisabled = this.IsGoCliManuallyDisabled();
record.WasGoCliDisabled = wasGoCliDisabled;
record.WasGoFallbackStrategyUsed = false;
var fileExtension = Path.GetExtension(file.Location).ToUpperInvariant();
switch (fileExtension)
{
case ".MOD":
{
this.Logger.LogDebug("Found Go.mod: {Location}", file.Location);
await this.goParserFactory.CreateParser(GoParserType.GoMod, this.Logger).ParseAsync(singleFileComponentRecorder, file, record);
if (await this.ShouldRunGoGraphAsync())
{
await GoDependencyGraphUtility.GenerateAndPopulateDependencyGraphAsync(
this.commandLineInvocationService,
this.Logger,
singleFileComponentRecorder,
projectRootDirectory.FullName,
record,
cancellationToken);
}
break;
}
case ".SUM":
{
this.Logger.LogDebug("Found Go.sum: {Location}", file.Location);
// check if we can use Go CLI instead
var wasGoCliScanSuccessful = false;
if (!wasGoCliDisabled)
{
wasGoCliScanSuccessful = await this.goParserFactory.CreateParser(GoParserType.GoCLI, this.Logger).ParseAsync(singleFileComponentRecorder, file, record);
}
this.Logger.LogDebug("Status of Go CLI scan when considering {GoSumLocation}: {Status}", file.Location, wasGoCliScanSuccessful);
// If Go CLI scan was not successful/disabled, scan go.sum because this go.sum was recorded due to go.mod
// containing go < 1.17. So go.mod is incomplete. We need to parse go.sum to make list of dependencies complete
if (!wasGoCliScanSuccessful)
{
record.WasGoFallbackStrategyUsed = true;
this.Logger.LogDebug("Go CLI scan when considering {GoSumLocation} was not successful. Falling back to scanning go.sum", file.Location);
await this.goParserFactory.CreateParser(GoParserType.GoSum, this.Logger).ParseAsync(singleFileComponentRecorder, file, record);
}
else
{
this.projectRoots.Add(projectRootDirectory.FullName);
}
break;
}
default:
{
throw new InvalidOperationException("Unexpected file type detected in go detector");
}
}
}
private bool IsGoCliManuallyDisabled()
{
return this.envVarService.IsEnvironmentVariableValueTrue("DisableGoCliScan");
}
private async Task<bool> ShouldRunGoGraphAsync()
{
if (this.IsGoCliManuallyDisabled())
{
return false;
}
var goVersion = await this.GetGoVersionAsync();
if (goVersion == null)
{
return false;
}
return goVersion >= new Version(1, 11);
}
private async Task<Version> GetGoVersionAsync()
{
try
{
var isGoAvailable = await this.commandLineInvocationService.CanCommandBeLocatedAsync("go", null, null, new List<string> { "version" }.ToArray());
if (!isGoAvailable)
{
this.Logger.LogInformation("Go CLI was not found in the system");
return null;
}
var processExecution = await this.commandLineInvocationService.ExecuteCommandAsync("go", null, null, cancellationToken: default, new List<string> { "version" }.ToArray());
if (processExecution.ExitCode != 0)
{
return null;
}
// Define the regular expression pattern to match the version number
var versionPattern = @"go version go(\d+\.\d+\.\d+)";
var match = Regex.Match(processExecution.StdOut, versionPattern);
if (match.Success)
{
// Extract the version number from the match
var versionStr = match.Groups[1].Value;
return new Version(versionStr);
}
}
catch (Exception e)
{
this.Logger.LogWarning("Failed to get go version: {Exception}", e);
}
return null;
}
private IEnumerable<ComponentStream> FindAdjacentGoModComponentStreams(ProcessRequest processRequest) =>
this.ComponentStreamEnumerableFactory.GetComponentStreams(
new FileInfo(processRequest.ComponentStream.Location).Directory,
["go.mod"],
(_, _) => false,
false)
.Select(x =>
{
// The stream will be disposed at the end of this method, so we need to copy it to a new stream.
var memoryStream = new MemoryStream();
x.Stream.CopyTo(memoryStream);
memoryStream.Position = 0;
return new ComponentStream
{
Stream = memoryStream,
Location = x.Location,
Pattern = x.Pattern,
};
});
}