Skip to content

Commit 930d0b2

Browse files
committed
Address PR comments
1 parent 40942a9 commit 930d0b2

2 files changed

Lines changed: 50 additions & 6 deletions

File tree

src/Microsoft.ComponentDetection.Detectors/helm/HelmComponentDetector.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ protected override async Task OnFileFoundAsync(ProcessRequest processRequest, ID
8989
return;
9090
}
9191

92-
this.ExtractImageReferencesFromValues(yaml, processRequest.SingleFileComponentRecorder, file.Location);
92+
this.ExtractImageReferencesFromValues(yaml, processRequest.SingleFileComponentRecorder);
9393
}
9494
catch (Exception e)
9595
{
@@ -112,13 +112,13 @@ private static bool IsValuesFile(string fileName) =>
112112
(fileName.EndsWith(".yaml", StringComparison.OrdinalIgnoreCase) ||
113113
fileName.EndsWith(".yml", StringComparison.OrdinalIgnoreCase));
114114

115-
private void ExtractImageReferencesFromValues(YamlStream yaml, ISingleFileComponentRecorder recorder, string fileLocation)
115+
private void ExtractImageReferencesFromValues(YamlStream yaml, ISingleFileComponentRecorder recorder)
116116
{
117117
foreach (var document in yaml.Documents)
118118
{
119119
if (document.RootNode is YamlMappingNode rootMapping)
120120
{
121-
this.WalkYamlForImages(rootMapping, recorder, fileLocation);
121+
this.WalkYamlForImages(rootMapping, recorder);
122122
}
123123
}
124124
}
@@ -128,7 +128,7 @@ private void ExtractImageReferencesFromValues(YamlStream yaml, ISingleFileCompon
128128
/// 1. Direct image string: `image: nginx:1.21`
129129
/// 2. Structured image object: `image: { repository: nginx, tag: "1.21" }`.
130130
/// </summary>
131-
private void WalkYamlForImages(YamlMappingNode mapping, ISingleFileComponentRecorder recorder, string fileLocation)
131+
private void WalkYamlForImages(YamlMappingNode mapping, ISingleFileComponentRecorder recorder)
132132
{
133133
foreach (var entry in mapping.Children)
134134
{
@@ -156,15 +156,15 @@ private void WalkYamlForImages(YamlMappingNode mapping, ISingleFileComponentReco
156156
}
157157
else if (entry.Value is YamlMappingNode childMapping)
158158
{
159-
this.WalkYamlForImages(childMapping, recorder, fileLocation);
159+
this.WalkYamlForImages(childMapping, recorder);
160160
}
161161
else if (entry.Value is YamlSequenceNode sequenceNode)
162162
{
163163
foreach (var item in sequenceNode)
164164
{
165165
if (item is YamlMappingNode sequenceMapping)
166166
{
167-
this.WalkYamlForImages(sequenceMapping, recorder, fileLocation);
167+
this.WalkYamlForImages(sequenceMapping, recorder);
168168
}
169169
}
170170
}

test/Microsoft.ComponentDetection.Detectors.Tests/HelmComponentDetectorTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,4 +430,48 @@ public async Task TestHelm_ChartYmlExtensionAsync()
430430
scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);
431431
componentRecorder.GetDetectedComponents().Should().BeEmpty();
432432
}
433+
434+
[TestMethod]
435+
public async Task TestHelm_ChartYmlWithValuesFileProcessedAsync()
436+
{
437+
var valuesYaml = @"
438+
image: nginx:1.21
439+
";
440+
441+
var (scanResult, componentRecorder) = await this.DetectorTestUtility
442+
.WithFile("Chart.yml", MinimalChartYaml)
443+
.WithFile("values.yaml", valuesYaml)
444+
.ExecuteDetectorAsync();
445+
446+
scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);
447+
var components = componentRecorder.GetDetectedComponents();
448+
components.Should().ContainSingle();
449+
450+
var dockerRef = components.First().Component as DockerReferenceComponent;
451+
dockerRef.Should().NotBeNull();
452+
dockerRef.Repository.Should().Be("library/nginx");
453+
dockerRef.Tag.Should().Be("1.21");
454+
}
455+
456+
[TestMethod]
457+
public async Task TestHelm_MultipleValuesFilesAsync()
458+
{
459+
var valuesYaml = @"
460+
image: nginx:1.21
461+
";
462+
463+
var valuesOverrideYaml = @"
464+
image: redis:7-alpine
465+
";
466+
467+
var (scanResult, componentRecorder) = await this.DetectorTestUtility
468+
.WithFile("Chart.yaml", MinimalChartYaml)
469+
.WithFile("values.yaml", valuesYaml)
470+
.WithFile("values.production.yaml", valuesOverrideYaml)
471+
.ExecuteDetectorAsync();
472+
473+
scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);
474+
var components = componentRecorder.GetDetectedComponents();
475+
components.Should().HaveCount(2);
476+
}
433477
}

0 commit comments

Comments
 (0)