diff --git a/src/Microsoft.ComponentDetection.Detectors/linux/LinuxContainerDetector.cs b/src/Microsoft.ComponentDetection.Detectors/linux/LinuxContainerDetector.cs index 0ea1393f2..4ae74067e 100644 --- a/src/Microsoft.ComponentDetection.Detectors/linux/LinuxContainerDetector.cs +++ b/src/Microsoft.ComponentDetection.Detectors/linux/LinuxContainerDetector.cs @@ -214,10 +214,14 @@ private async Task GetBaseImageLayerCount(ContainerDetails scannedImageDeta record.BaseImageLayerMessage = $"Base image annotations not found on image {image}, Results will not be mapped to base image layers"; Logger.LogInfo(record.BaseImageLayerMessage); return 0; + } else if (scannedImageDetails.BaseImageRef == "scratch") { + record.BaseImageLayerMessage = $"{image} has no base image"; + Logger.LogInfo(record.BaseImageLayerMessage); + return 0; } var baseImageDigest = scannedImageDetails.BaseImageDigest; - var refWithDigest = scannedImageDetails.BaseImageRef + (baseImageDigest != string.Empty ? $"@{baseImageDigest}" : string.Empty); + var refWithDigest = scannedImageDetails.BaseImageRef + (!string.IsNullOrEmpty(baseImageDigest) ? $"@{baseImageDigest}" : string.Empty); record.BaseImageDigest = baseImageDigest; record.BaseImageRef = scannedImageDetails.BaseImageRef; diff --git a/test/Microsoft.ComponentDetection.Detectors.Tests/LinuxContainerDetectorTests.cs b/test/Microsoft.ComponentDetection.Detectors.Tests/LinuxContainerDetectorTests.cs index 677ee82a6..112d1514b 100644 --- a/test/Microsoft.ComponentDetection.Detectors.Tests/LinuxContainerDetectorTests.cs +++ b/test/Microsoft.ComponentDetection.Detectors.Tests/LinuxContainerDetectorTests.cs @@ -205,5 +205,18 @@ public async Task TestLinuxContainerDetector_TimeoutParameterSpecified() Func action = async () => await linuxContainerDetector.ExecuteDetectorAsync(scanRequest); await action.Should().NotThrowAsync(); } + + [TestMethod] + public async Task TestLinuxContainerDetector_HandlesScratchBase() { + // Setup docker service to throw an exception on scratch + // then specify that the base image is scratch, to test this + // is coped with. + mockDockerService.Setup(service => service.TryPullImageAsync("scratch", It.IsAny())) + .Throws(new IOException()); + mockDockerService.Setup(service => service.InspectImageAsync(It.IsAny(), It.IsAny())) + // Specify BaseImageRef = scratch to verify that cope + .ReturnsAsync(new ContainerDetails { Id = 1, ImageId = NodeLatestDigest, Layers = Enumerable.Empty() , BaseImageRef = "scratch"}); + await TestLinuxContainerDetector(); + } } }