Skip to content

Commit 6cdaa20

Browse files
authored
Revert "Add image scope scanning option to the Linux detector (#1600)" (#1613)
This reverts commit c6846ef.
1 parent a5fe5ed commit 6cdaa20

File tree

7 files changed

+9
-192
lines changed

7 files changed

+9
-192
lines changed

docs/detectors/linux.md

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,6 @@ Linux detection depends on the following:
1111
Linux package detection is performed by running [Syft](https://github.com/anchore/syft) and parsing the output.
1212
The output contains the package name, version, and the layer of the container in which it was found.
1313

14-
### Scanner Scope
15-
16-
By default, this detector invokes Syft with the `all-layers` scanning scope (i.e. the Syft argument `--scope all-layers`).
17-
18-
Syft has another scope, `squashed`, which can be used to scan only files accessible from the final layer of an image.
19-
20-
The detector argument `Linux.ImageScanScope` can be used to configure this option as `squashed` or `all-layers` when invoking Component Detection.
21-
22-
For example:
23-
24-
```sh
25-
--DetectorArgs Linux.ImageScanScope=squashed
26-
```
27-
2814
## Known limitations
2915

3016
- Windows container scanning is not supported

src/Microsoft.ComponentDetection.Detectors/linux/ILinuxScanner.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ public interface ILinuxScanner
1919
/// <param name="containerLayers">The collection of Docker layers that make up the container image.</param>
2020
/// <param name="baseImageLayerCount">The number of layers that belong to the base image, used to distinguish base image layers from application layers.</param>
2121
/// <param name="enabledComponentTypes">The set of component types to include in the scan results. Only components matching these types will be returned.</param>
22-
/// <param name="scope">The scope for scanning the image. See <see cref="LinuxScannerScope"/> for values.</param>
2322
/// <param name="cancellationToken">A token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
2423
/// <returns>A task that represents the asynchronous operation. The task result contains a collection of <see cref="LayerMappedLinuxComponents"/> representing the components found in the image and their associated layers.</returns>
2524
public Task<IEnumerable<LayerMappedLinuxComponents>> ScanLinuxAsync(
2625
string imageHash,
2726
IEnumerable<DockerLayer> containerLayers,
2827
int baseImageLayerCount,
2928
ISet<ComponentType> enabledComponentTypes,
30-
LinuxScannerScope scope,
3129
CancellationToken cancellationToken = default
3230
);
3331
}

src/Microsoft.ComponentDetection.Detectors/linux/LinuxContainerDetector.cs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ ILogger<LinuxContainerDetector> logger
2828
{
2929
private const string TimeoutConfigKey = "Linux.ScanningTimeoutSec";
3030
private const int DefaultTimeoutMinutes = 10;
31-
private const string ScanScopeConfigKey = "Linux.ImageScanScope";
32-
private const LinuxScannerScope DefaultScanScope = LinuxScannerScope.AllLayers;
3331

3432
private readonly ILinuxScanner linuxScanner = linuxScanner;
3533
private readonly IDockerService dockerService = dockerService;
@@ -79,8 +77,6 @@ public async Task<IndividualDetectorScanResult> ExecuteDetectorAsync(
7977
return EmptySuccessfulScan();
8078
}
8179

82-
var scannerScope = GetScanScope(request.DetectorArgs);
83-
8480
using var timeoutCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
8581
timeoutCts.CancelAfter(GetTimeout(request.DetectorArgs));
8682

@@ -100,7 +96,6 @@ public async Task<IndividualDetectorScanResult> ExecuteDetectorAsync(
10096
results = await this.ProcessImagesAsync(
10197
imagesToProcess,
10298
request.ComponentRecorder,
103-
scannerScope,
10499
timeoutCts.Token
105100
);
106101
}
@@ -142,26 +137,6 @@ private static TimeSpan GetTimeout(IDictionary<string, string> detectorArgs)
142137
: defaultTimeout;
143138
}
144139

145-
/// <summary>
146-
/// Extracts and returns the scan scope from detector arguments.
147-
/// </summary>
148-
/// <param name="detectorArgs">The arguments provided by the user.</param>
149-
/// <returns>The <see cref="LinuxScannerScope"/> to use for scanning. Defaults to <see cref="DefaultScanScope"/> if not specified.</returns>
150-
private static LinuxScannerScope GetScanScope(IDictionary<string, string> detectorArgs)
151-
{
152-
if (detectorArgs == null || !detectorArgs.TryGetValue(ScanScopeConfigKey, out var scopeValue))
153-
{
154-
return DefaultScanScope;
155-
}
156-
157-
return scopeValue?.ToUpperInvariant() switch
158-
{
159-
"ALL-LAYERS" => LinuxScannerScope.AllLayers,
160-
"SQUASHED" => LinuxScannerScope.Squashed,
161-
_ => DefaultScanScope,
162-
};
163-
}
164-
165140
private static IndividualDetectorScanResult EmptySuccessfulScan() =>
166141
new() { ResultCode = ProcessingResultCode.Success };
167142

@@ -204,7 +179,6 @@ private static void RecordImageDetectionFailure(Exception exception, string imag
204179
private async Task<IEnumerable<ImageScanningResult>> ProcessImagesAsync(
205180
IEnumerable<string> imagesToProcess,
206181
IComponentRecorder componentRecorder,
207-
LinuxScannerScope scannerScope,
208182
CancellationToken cancellationToken = default
209183
)
210184
{
@@ -275,7 +249,6 @@ await this.dockerService.InspectImageAsync(image, cancellationToken)
275249
internalContainerDetails.Layers,
276250
baseImageLayerCount,
277251
enabledComponentTypes,
278-
scannerScope,
279252
cancellationToken
280253
);
281254

src/Microsoft.ComponentDetection.Detectors/linux/LinuxScanner.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,12 @@ public class LinuxScanner : ILinuxScanner
2727
private static readonly IList<string> CmdParameters =
2828
[
2929
"--quiet",
30+
"--scope",
31+
"all-layers",
3032
"--output",
3133
"json",
3234
];
3335

34-
private static readonly IList<string> ScopeAllLayersParameter = ["--scope", "all-layers"];
35-
36-
private static readonly IList<string> ScopeSquashedParameter = ["--scope", "squashed"];
37-
3836
private static readonly SemaphoreSlim ContainerSemaphore = new SemaphoreSlim(2);
3937

4038
private static readonly int SemaphoreTimeout = Convert.ToInt32(
@@ -98,7 +96,6 @@ public async Task<IEnumerable<LayerMappedLinuxComponents>> ScanLinuxAsync(
9896
IEnumerable<DockerLayer> containerLayers,
9997
int baseImageLayerCount,
10098
ISet<ComponentType> enabledComponentTypes,
101-
LinuxScannerScope scope,
10299
CancellationToken cancellationToken = default
103100
)
104101
{
@@ -112,16 +109,6 @@ public async Task<IEnumerable<LayerMappedLinuxComponents>> ScanLinuxAsync(
112109
var stdout = string.Empty;
113110
var stderr = string.Empty;
114111

115-
var scopeParameters = scope switch
116-
{
117-
LinuxScannerScope.AllLayers => ScopeAllLayersParameter,
118-
LinuxScannerScope.Squashed => ScopeSquashedParameter,
119-
_ => throw new ArgumentOutOfRangeException(
120-
nameof(scope),
121-
$"Unsupported scope value: {scope}"
122-
),
123-
};
124-
125112
using var syftTelemetryRecord = new LinuxScannerSyftTelemetryRecord();
126113

127114
try
@@ -133,7 +120,6 @@ public async Task<IEnumerable<LayerMappedLinuxComponents>> ScanLinuxAsync(
133120
{
134121
var command = new List<string> { imageHash }
135122
.Concat(CmdParameters)
136-
.Concat(scopeParameters)
137123
.ToList();
138124
(stdout, stderr) = await this.dockerService.CreateAndRunContainerAsync(
139125
ScannerImage,

src/Microsoft.ComponentDetection.Detectors/linux/LinuxScannerScope.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.

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

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public LinuxContainerDetectorTests()
7373
It.IsAny<IEnumerable<DockerLayer>>(),
7474
It.IsAny<int>(),
7575
It.IsAny<ISet<ComponentType>>(),
76-
It.IsAny<LinuxScannerScope>(),
7776
It.IsAny<CancellationToken>()
7877
)
7978
)
@@ -278,7 +277,6 @@ public async Task TestLinuxContainerDetector_SameImagePassedMultipleTimesAsync()
278277
It.IsAny<IEnumerable<DockerLayer>>(),
279278
It.IsAny<int>(),
280279
It.IsAny<ISet<ComponentType>>(),
281-
It.IsAny<LinuxScannerScope>(),
282280
It.IsAny<CancellationToken>()
283281
),
284282
Times.Once
@@ -309,48 +307,6 @@ public async Task TestLinuxContainerDetector_TimeoutParameterSpecifiedAsync()
309307
await action.Should().NotThrowAsync<OperationCanceledException>();
310308
}
311309

312-
[TestMethod]
313-
[DataRow("all-layers", LinuxScannerScope.AllLayers)]
314-
[DataRow("squashed", LinuxScannerScope.Squashed)]
315-
[DataRow("ALL-LAYERS", LinuxScannerScope.AllLayers)]
316-
[DataRow("SQUASHED", LinuxScannerScope.Squashed)]
317-
[DataRow(null, LinuxScannerScope.AllLayers)] // Test default behavior
318-
[DataRow("", LinuxScannerScope.AllLayers)] // Test empty string default
319-
[DataRow("invalid-value", LinuxScannerScope.AllLayers)] // Test invalid input defaults to AllLayers
320-
public async Task TestLinuxContainerDetector_ImageScanScopeParameterSpecifiedAsync(string scopeValue, LinuxScannerScope expectedScope)
321-
{
322-
var detectorArgs = new Dictionary<string, string> { { "Linux.ImageScanScope", scopeValue } };
323-
var scanRequest = new ScanRequest(
324-
new DirectoryInfo(Path.GetTempPath()),
325-
(_, __) => false,
326-
this.mockLogger.Object,
327-
detectorArgs,
328-
[NodeLatestImage],
329-
new ComponentRecorder()
330-
);
331-
332-
var linuxContainerDetector = new LinuxContainerDetector(
333-
this.mockSyftLinuxScanner.Object,
334-
this.mockDockerService.Object,
335-
this.mockLinuxContainerDetectorLogger.Object
336-
);
337-
338-
await linuxContainerDetector.ExecuteDetectorAsync(scanRequest);
339-
340-
this.mockSyftLinuxScanner.Verify(
341-
scanner =>
342-
scanner.ScanLinuxAsync(
343-
It.IsAny<string>(),
344-
It.IsAny<IEnumerable<DockerLayer>>(),
345-
It.IsAny<int>(),
346-
It.IsAny<ISet<ComponentType>>(),
347-
expectedScope,
348-
It.IsAny<CancellationToken>()
349-
),
350-
Times.Once
351-
);
352-
}
353-
354310
[TestMethod]
355311
public async Task TestLinuxContainerDetector_HandlesScratchBaseAsync()
356312
{

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

Lines changed: 7 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#nullable disable
22
namespace Microsoft.ComponentDetection.Detectors.Tests;
33

4-
using System;
54
using System.Collections.Generic;
65
using System.Linq;
76
using System.Threading;
@@ -289,8 +288,7 @@ await this.linuxScanner.ScanLinuxAsync(
289288
},
290289
],
291290
0,
292-
enabledTypes,
293-
LinuxScannerScope.AllLayers
291+
enabledTypes
294292
)
295293
)
296294
.First()
@@ -337,8 +335,7 @@ await this.linuxScanner.ScanLinuxAsync(
337335
},
338336
],
339337
0,
340-
enabledTypes,
341-
LinuxScannerScope.AllLayers
338+
enabledTypes
342339
)
343340
)
344341
.First()
@@ -387,8 +384,7 @@ await this.linuxScanner.ScanLinuxAsync(
387384
},
388385
],
389386
0,
390-
enabledTypes,
391-
LinuxScannerScope.AllLayers
387+
enabledTypes
392388
)
393389
)
394390
.First()
@@ -437,8 +433,7 @@ await this.linuxScanner.ScanLinuxAsync(
437433
},
438434
],
439435
0,
440-
enabledTypes,
441-
LinuxScannerScope.AllLayers
436+
enabledTypes
442437
)
443438
)
444439
.First()
@@ -527,8 +522,7 @@ public async Task TestLinuxScanner_SupportsMultipleComponentTypes_Async()
527522
new DockerLayer { LayerIndex = 1, DiffId = "sha256:layer2" },
528523
],
529524
0,
530-
enabledTypes,
531-
LinuxScannerScope.AllLayers
525+
enabledTypes
532526
);
533527

534528
var allComponents = layers.SelectMany(l => l.Components).ToList();
@@ -628,8 +622,7 @@ public async Task TestLinuxScanner_FiltersComponentsByEnabledTypes_OnlyLinux_Asy
628622
new DockerLayer { LayerIndex = 1, DiffId = "sha256:layer2" },
629623
],
630624
0,
631-
enabledTypes,
632-
LinuxScannerScope.AllLayers
625+
enabledTypes
633626
);
634627

635628
var allComponents = layers.SelectMany(l => l.Components).ToList();
@@ -714,8 +707,7 @@ public async Task TestLinuxScanner_FiltersComponentsByEnabledTypes_OnlyNpmAndPip
714707
new DockerLayer { LayerIndex = 1, DiffId = "sha256:layer2" },
715708
],
716709
0,
717-
enabledTypes,
718-
LinuxScannerScope.AllLayers
710+
enabledTypes
719711
);
720712

721713
var allComponents = layers.SelectMany(l => l.Components).ToList();
@@ -730,61 +722,4 @@ public async Task TestLinuxScanner_FiltersComponentsByEnabledTypes_OnlyNpmAndPip
730722
var pipComponent = allComponents.OfType<PipComponent>().Single();
731723
pipComponent.Name.Should().Be("requests");
732724
}
733-
734-
[TestMethod]
735-
[DataRow(LinuxScannerScope.AllLayers, "all-layers")]
736-
[DataRow(LinuxScannerScope.Squashed, "squashed")]
737-
public async Task TestLinuxScanner_ScopeParameter_IncludesCorrectFlagAsync(
738-
LinuxScannerScope scope,
739-
string expectedFlag
740-
)
741-
{
742-
this.mockDockerService.Setup(service =>
743-
service.CreateAndRunContainerAsync(
744-
It.IsAny<string>(),
745-
It.IsAny<List<string>>(),
746-
It.IsAny<CancellationToken>()
747-
)
748-
)
749-
.ReturnsAsync((SyftOutputNoAuthorOrLicense, string.Empty));
750-
751-
var enabledTypes = new HashSet<ComponentType> { ComponentType.Linux };
752-
await this.linuxScanner.ScanLinuxAsync(
753-
"fake_hash",
754-
[new DockerLayer { LayerIndex = 0, DiffId = "sha256:layer1" }],
755-
0,
756-
enabledTypes,
757-
scope
758-
);
759-
760-
this.mockDockerService.Verify(
761-
service =>
762-
service.CreateAndRunContainerAsync(
763-
It.IsAny<string>(),
764-
It.Is<List<string>>(cmd =>
765-
cmd.Contains("--scope") && cmd.Contains(expectedFlag)
766-
),
767-
It.IsAny<CancellationToken>()
768-
),
769-
Times.Once
770-
);
771-
}
772-
773-
[TestMethod]
774-
public async Task TestLinuxScanner_InvalidScopeParameter_ThrowsArgumentOutOfRangeExceptionAsync()
775-
{
776-
var enabledTypes = new HashSet<ComponentType> { ComponentType.Linux };
777-
var invalidScope = (LinuxScannerScope)999; // Invalid enum value
778-
779-
Func<Task> action = async () =>
780-
await this.linuxScanner.ScanLinuxAsync(
781-
"fake_hash",
782-
[new DockerLayer { LayerIndex = 0, DiffId = "sha256:layer1" }],
783-
0,
784-
enabledTypes,
785-
invalidScope
786-
);
787-
788-
await action.Should().ThrowAsync<ArgumentOutOfRangeException>();
789-
}
790725
}

0 commit comments

Comments
 (0)