-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathDockerComposeComponentDetector.cs
More file actions
133 lines (114 loc) · 4.8 KB
/
Copy pathDockerComposeComponentDetector.cs
File metadata and controls
133 lines (114 loc) · 4.8 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
namespace Microsoft.ComponentDetection.Detectors.DockerCompose;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.ComponentDetection.Common;
using Microsoft.ComponentDetection.Contracts;
using Microsoft.ComponentDetection.Contracts.Internal;
using Microsoft.ComponentDetection.Contracts.TypedComponent;
using Microsoft.Extensions.Logging;
using YamlDotNet.RepresentationModel;
public class DockerComposeComponentDetector : FileComponentDetector, IExperimentalDetector
{
public DockerComposeComponentDetector(
IComponentStreamEnumerableFactory componentStreamEnumerableFactory,
IObservableDirectoryWalkerFactory walkerFactory,
ILogger<DockerComposeComponentDetector> logger)
{
this.ComponentStreamEnumerableFactory = componentStreamEnumerableFactory;
this.Scanner = walkerFactory;
this.Logger = logger;
}
public override string Id => "DockerCompose";
public override IList<string> SearchPatterns { get; } =
[
"docker-compose.yml", "docker-compose.yaml",
"docker-compose.*.yml", "docker-compose.*.yaml",
"compose.yml", "compose.yaml",
"compose.*.yml", "compose.*.yaml",
];
public override IEnumerable<ComponentType> SupportedComponentTypes => [ComponentType.DockerReference];
public override int Version => 1;
public override IEnumerable<string> Categories => [nameof(DetectorClass.DockerCompose)];
/// <summary>
/// Gets or sets a value indicating whether compose files are processed concurrently.
/// Each file is parsed independently into its own <see cref="ISingleFileComponentRecorder"/>
/// and <see cref="DockerReferenceUtility"/> is stateless, so parsing is thread-safe and
/// scales across cores for repositories containing many compose files.
/// </summary>
protected override bool EnableParallelism { get; set; } = true;
protected override Task OnFileFoundAsync(ProcessRequest processRequest, IDictionary<string, string> detectorArgs, CancellationToken cancellationToken = default)
{
var singleFileComponentRecorder = processRequest.SingleFileComponentRecorder;
var file = processRequest.ComponentStream;
try
{
this.Logger.LogInformation("Discovered Docker Compose file: {Location}", file.Location);
// Parse directly from the stream; the content is already buffered in memory by
// LazyComponentStream, so reading it into an intermediate string only adds an
// extra full-file allocation and GC pressure under parallel processing.
var yaml = new YamlStream();
using (var reader = new StreamReader(file.Stream))
{
yaml.Load(reader);
}
if (yaml.Documents.Count == 0)
{
return Task.CompletedTask;
}
foreach (var document in yaml.Documents)
{
if (document.RootNode is YamlMappingNode rootMapping)
{
this.ExtractImageReferences(rootMapping, singleFileComponentRecorder);
}
}
}
catch (Exception e)
{
this.Logger.LogError(e, "Failed to parse Docker Compose file: {Location}", file.Location);
}
return Task.CompletedTask;
}
private static YamlMappingNode? GetMappingChild(YamlMappingNode parent, string key)
{
foreach (var entry in parent.Children)
{
if (entry.Key is YamlScalarNode scalarKey && string.Equals(scalarKey.Value, key, StringComparison.OrdinalIgnoreCase))
{
return entry.Value as YamlMappingNode;
}
}
return null;
}
private void ExtractImageReferences(YamlMappingNode rootMapping, ISingleFileComponentRecorder recorder)
{
var services = GetMappingChild(rootMapping, "services");
if (services == null)
{
return;
}
foreach (var serviceEntry in services.Children)
{
if (serviceEntry.Value is not YamlMappingNode serviceMapping)
{
continue;
}
// Extract direct image: references
foreach (var entry in serviceMapping.Children)
{
var key = (entry.Key as YamlScalarNode)?.Value;
if (string.Equals(key, "image", StringComparison.OrdinalIgnoreCase))
{
var imageRef = (entry.Value as YamlScalarNode)?.Value;
if (!string.IsNullOrWhiteSpace(imageRef))
{
DockerReferenceUtility.TryRegisterImageReference(imageRef, recorder, this.Logger);
}
}
}
}
}
}