-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathDefaultGraphTranslationService.cs
More file actions
363 lines (302 loc) · 15.5 KB
/
DefaultGraphTranslationService.cs
File metadata and controls
363 lines (302 loc) · 15.5 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#nullable disable
namespace Microsoft.ComponentDetection.Orchestrator.Services.GraphTranslation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.ComponentDetection.Common;
using Microsoft.ComponentDetection.Common.DependencyGraph;
using Microsoft.ComponentDetection.Common.Telemetry.Records;
using Microsoft.ComponentDetection.Contracts;
using Microsoft.ComponentDetection.Contracts.BcdeModels;
using Microsoft.ComponentDetection.Contracts.TypedComponent;
using Microsoft.ComponentDetection.Orchestrator.Commands;
using Microsoft.Extensions.Logging;
internal class DefaultGraphTranslationService : IGraphTranslationService
{
private readonly ILogger<DefaultGraphTranslationService> logger;
public DefaultGraphTranslationService(ILogger<DefaultGraphTranslationService> logger) => this.logger = logger;
public ScanResult GenerateScanResultFromProcessingResult(
DetectorProcessingResult detectorProcessingResult,
ScanSettings settings,
bool updateLocations = true)
{
var recorderDetectorPairs = detectorProcessingResult.ComponentRecorders;
var unmergedComponents = this.GatherSetOfDetectedComponentsUnmerged(recorderDetectorPairs, settings.SourceDirectory, updateLocations);
var mergedComponents = this.FlattenAndMergeComponents(unmergedComponents);
this.LogComponentScopeTelemetry(mergedComponents);
return new DefaultGraphScanResult
{
ComponentsFound = mergedComponents.Select(x => this.ConvertToContract(x)).ToList(),
ContainerDetailsMap = detectorProcessingResult.ContainersDetailsMap,
DependencyGraphs = GraphTranslationUtility.AccumulateAndConvertToContract(recorderDetectorPairs
.Select(tuple => tuple.Recorder)
.Where(x => x != null)
.Select(x => x.GetDependencyGraphsByLocation())),
SourceDirectory = settings.SourceDirectory.ToString(),
};
}
private static ConcurrentHashSet<string> MergeTargetFrameworks(ConcurrentHashSet<string> left, ConcurrentHashSet<string> right)
{
if (left == null && right == null)
{
return [];
}
if (left == null)
{
return right;
}
if (right == null)
{
return left;
}
foreach (var targetFramework in right)
{
left.Add(targetFramework);
}
return left;
}
/// <summary>
/// Checks whether the graph contains the component by its full Id, or by its BaseId
/// when the component is a rich entry (Id != BaseId) whose bare counterpart was registered in the graph.
/// </summary>
private static bool GraphContainsComponent(IDependencyGraph graph, TypedComponent component)
{
return graph.Contains(component.Id) ||
(component.Id != component.BaseId && graph.Contains(component.BaseId));
}
private void LogComponentScopeTelemetry(List<DetectedComponent> components)
{
using var record = new DetectedComponentScopeRecord();
Parallel.ForEach(components, x =>
{
if (x.Component.Type.Equals(ComponentType.Maven)
&& x.DependencyScope.HasValue
&& (x.DependencyScope.Equals(DependencyScope.MavenProvided) || x.DependencyScope.Equals(DependencyScope.MavenSystem)))
{
record.IncrementProvidedScopeCount();
}
});
}
private IEnumerable<DetectedComponent> GatherSetOfDetectedComponentsUnmerged(IEnumerable<(IComponentDetector Detector, ComponentRecorder Recorder)> recorderDetectorPairs, DirectoryInfo rootDirectory, bool updateLocations)
{
return recorderDetectorPairs
.Where(recorderDetectorPair => recorderDetectorPair.Recorder != null)
.SelectMany(recorderDetectorPair =>
{
using var record = new DependencyGraphTranslationRecord()
{
DetectorId = recorderDetectorPair.Detector.Id,
};
var detector = recorderDetectorPair.Detector;
var componentRecorder = recorderDetectorPair.Recorder;
var detectedComponents = componentRecorder.GetDetectedComponents();
var dependencyGraphsByLocation = componentRecorder.GetDependencyGraphsByLocation();
foreach (var graph in dependencyGraphsByLocation.Values)
{
graph.FillTypedComponents(componentRecorder.GetComponent);
}
var totalTimeToAddRoots = TimeSpan.Zero;
var totalTimeToAddAncestors = TimeSpan.Zero;
// Note that it looks like we are building up detected components functionally, but they are not immutable -- the code is just written
// to look like a pipeline.
foreach (var component in detectedComponents)
{
// clone custom locations and make them relative to root.
var componentCustomLocations = component.FilePaths ?? [];
if (updateLocations)
{
if (component.FilePaths != null)
{
componentCustomLocations = [.. component.FilePaths];
component.FilePaths?.Clear();
}
}
// Information about each component is relative to all of the graphs it is present in, so we take all graphs containing a given component and apply the graph data.
foreach (var graphKvp in dependencyGraphsByLocation.Where(x => GraphContainsComponent(x.Value, component.Component)))
{
var location = graphKvp.Key;
var dependencyGraph = graphKvp.Value;
// Determine the Id stored in this graph — may be the rich Id or the bare BaseId.
var graphComponentId = dependencyGraph.Contains(component.Component.Id)
? component.Component.Id
: component.Component.BaseId;
// Calculate roots of the component
var rootStartTime = DateTime.UtcNow;
this.AddRootsToDetectedComponent(component, graphComponentId, dependencyGraph, componentRecorder);
var rootEndTime = DateTime.UtcNow;
totalTimeToAddRoots += rootEndTime - rootStartTime;
// Calculate Ancestors of the component
var ancestorStartTime = DateTime.UtcNow;
this.AddAncestorsToDetectedComponent(component, graphComponentId, dependencyGraph, componentRecorder);
var ancestorEndTime = DateTime.UtcNow;
totalTimeToAddAncestors += ancestorEndTime - ancestorStartTime;
component.DevelopmentDependency = this.MergeDevDependency(component.DevelopmentDependency, dependencyGraph.IsDevelopmentDependency(graphComponentId));
component.DependencyScope = DependencyScopeComparer.GetMergedDependencyScope(component.DependencyScope, dependencyGraph.GetDependencyScope(graphComponentId));
component.DetectedBy = detector;
// Experiments uses this service to build the dependency graph for analysis. In this case, we do not want to update the locations of the component.
// Updating the locations of the component will propogate to the final depenendcy graph and cause the graph to be incorrect.
if (updateLocations)
{
// Return in a format that allows us to add the additional files for the components
var locations = dependencyGraph.GetAdditionalRelatedFiles();
// graph authoritatively stores the location of the component
locations.Add(location);
foreach (var customLocation in componentCustomLocations)
{
locations.Add(customLocation);
}
var relativePaths = this.MakeFilePathsRelative(this.logger, rootDirectory, locations);
foreach (var additionalRelatedFile in relativePaths ?? Enumerable.Empty<string>())
{
component.AddComponentFilePath(additionalRelatedFile);
}
}
}
}
record.TimeToAddRoots = totalTimeToAddRoots;
record.TimeToAddAncestors = totalTimeToAddAncestors;
return detectedComponents;
}).ToList();
}
private List<DetectedComponent> FlattenAndMergeComponents(IEnumerable<DetectedComponent> components)
{
var flattenedAndMergedComponents = new List<DetectedComponent>();
foreach (var grouping in components.GroupBy(x => x.Component.Id + x.DetectedBy.Id))
{
flattenedAndMergedComponents.Add(this.MergeComponents(grouping));
}
return flattenedAndMergedComponents;
}
private bool? MergeDevDependency(bool? left, bool? right)
{
if (left == null)
{
return right;
}
if (right != null)
{
return left.Value && right.Value;
}
return left;
}
private DetectedComponent MergeComponents(IEnumerable<DetectedComponent> enumerable)
{
if (enumerable.Count() == 1)
{
return enumerable.First();
}
// Multiple detected components for the same logical component id -- this happens when different files see the same component. This code should go away when we get all
// mutable data out of detected component -- we can just take any component.
var firstComponent = enumerable.First();
HashSet<string> mergedLicenses = null;
HashSet<ActorInfo> mergedSuppliers = null;
foreach (var nextComponent in enumerable.Skip(1))
{
foreach (var filePath in nextComponent.FilePaths ?? Enumerable.Empty<string>())
{
firstComponent.AddComponentFilePath(filePath);
}
foreach (var root in nextComponent.DependencyRoots ?? Enumerable.Empty<TypedComponent>())
{
firstComponent.DependencyRoots.Add(root);
}
firstComponent.DevelopmentDependency = this.MergeDevDependency(firstComponent.DevelopmentDependency, nextComponent.DevelopmentDependency);
firstComponent.DependencyScope = DependencyScopeComparer.GetMergedDependencyScope(firstComponent.DependencyScope, nextComponent.DependencyScope);
if (nextComponent.ContainerDetailIds.Count > 0)
{
foreach (var containerDetailId in nextComponent.ContainerDetailIds)
{
firstComponent.ContainerDetailIds.Add(containerDetailId);
}
}
firstComponent.TargetFrameworks = MergeTargetFrameworks(firstComponent.TargetFrameworks, nextComponent.TargetFrameworks);
if (nextComponent.LicensesConcluded != null)
{
mergedLicenses ??= new HashSet<string>(firstComponent.LicensesConcluded ?? [], StringComparer.OrdinalIgnoreCase);
mergedLicenses.UnionWith(nextComponent.LicensesConcluded);
}
if (nextComponent.Suppliers != null)
{
mergedSuppliers ??= new HashSet<ActorInfo>(firstComponent.Suppliers ?? []);
mergedSuppliers.UnionWith(nextComponent.Suppliers);
}
}
if (mergedLicenses != null)
{
firstComponent.LicensesConcluded = mergedLicenses.Where(x => x != null).OrderBy(x => x, StringComparer.OrdinalIgnoreCase).ToList();
}
if (mergedSuppliers != null)
{
firstComponent.Suppliers = mergedSuppliers.Where(s => s != null).OrderBy(s => s.Name).ThenBy(s => s.Type).ToList();
}
return firstComponent;
}
private void AddRootsToDetectedComponent(DetectedComponent detectedComponent, string graphComponentId, IDependencyGraph dependencyGraph, IComponentRecorder componentRecorder)
{
detectedComponent.DependencyRoots ??= new HashSet<TypedComponent>(new ComponentComparer());
if (dependencyGraph == null)
{
return;
}
detectedComponent.DependencyRoots.UnionWith(dependencyGraph.GetRootsAsTypedComponents(graphComponentId, componentRecorder.GetComponent));
}
private void AddAncestorsToDetectedComponent(DetectedComponent detectedComponent, string graphComponentId, IDependencyGraph dependencyGraph, IComponentRecorder componentRecorder)
{
detectedComponent.AncestralDependencyRoots ??= new HashSet<TypedComponent>(new ComponentComparer());
if (dependencyGraph == null)
{
return;
}
detectedComponent.AncestralDependencyRoots.UnionWith(dependencyGraph.GetAncestorsAsTypedComponents(graphComponentId, componentRecorder.GetComponent));
}
private HashSet<string> MakeFilePathsRelative(ILogger logger, DirectoryInfo rootDirectory, HashSet<string> filePaths)
{
if (rootDirectory == null)
{
return null;
}
// Make relative Uri needs a trailing separator to ensure that we turn "directory we are scanning" into "/"
var rootDirectoryFullName = rootDirectory.FullName;
if (!rootDirectory.FullName.EndsWith(Path.DirectorySeparatorChar) && !rootDirectory.FullName.EndsWith(Path.AltDirectorySeparatorChar))
{
rootDirectoryFullName += Path.DirectorySeparatorChar;
}
var rootUri = new Uri(rootDirectoryFullName);
var relativePathSet = new HashSet<string>();
foreach (var path in filePaths)
{
if (!Uri.TryCreate(path, UriKind.Absolute, out var uriPath))
{
logger.LogDebug("The path: {Path} is not a valid absolute path and so could not be resolved relative to the root {RootUri}", path, rootUri);
continue;
}
var relativePath = rootUri.MakeRelativeUri(uriPath).ToString();
if (!relativePath.StartsWith('/'))
{
relativePath = "/" + relativePath;
}
relativePathSet.Add(relativePath);
}
return relativePathSet;
}
private ScannedComponent ConvertToContract(DetectedComponent component)
{
return new ScannedComponent
{
DetectorId = component.DetectedBy.Id,
IsDevelopmentDependency = component.DevelopmentDependency,
DependencyScope = component.DependencyScope,
LocationsFoundAt = component.FilePaths,
Component = component.Component,
TopLevelReferrers = component.DependencyRoots,
AncestralReferrers = component.AncestralDependencyRoots,
ContainerDetailIds = component.ContainerDetailIds,
ContainerLayerIds = component.ContainerLayerIds,
TargetFrameworks = component.TargetFrameworks?.ToHashSet(),
LicensesConcluded = component.LicensesConcluded,
Suppliers = component.Suppliers,
};
}
}