Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ namespace Microsoft.ComponentDetection.Detectors.Dockerfile;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reactive.Linq;
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 Sprache;
Comment thread
jpinz marked this conversation as resolved.
using Valleysoft.DockerfileModel;

public class DockerfileComponentDetector : FileComponentDetector, IExperimentalDetector
Expand Down Expand Up @@ -41,6 +44,15 @@ public DockerfileComponentDetector(

public override int Version => 1;

protected override IList<string> SkippedFolders => ["node_modules"];
Comment thread
grvillic marked this conversation as resolved.

protected override Task<IObservable<ProcessRequest>> OnPrepareDetectionAsync(
Comment thread
grvillic marked this conversation as resolved.
IObservable<ProcessRequest> processRequests,
IDictionary<string, string> detectorArgs,
CancellationToken cancellationToken = default) =>
Task.FromResult(processRequests.Where(processRequest =>
!this.IsInSkippedFolder(processRequest.ComponentStream.Location)));
Comment thread
jpinz marked this conversation as resolved.

protected override async Task OnFileFoundAsync(ProcessRequest processRequest, IDictionary<string, string> detectorArgs, CancellationToken cancellationToken = default)
{
var singleFileComponentRecorder = processRequest.SingleFileComponentRecorder;
Expand All @@ -59,12 +71,22 @@ protected override async Task OnFileFoundAsync(ProcessRequest processRequest, ID
var stageNameMap = new Dictionary<string, string>();
var dockerFileComponent = this.ParseDockerFileAsync(contents, file.Location, singleFileComponentRecorder, stageNameMap);
}
catch (ParseException e)
{
this.Logger.LogWarning(e, "Ignoring file that doesn't appear to be a Dockerfile: {Location}", filePath);
Comment thread
jpinz marked this conversation as resolved.
}
catch (Exception e)
{
this.Logger.LogError(e, "The file doesn't appear to be a Dockerfile: {Location}", filePath);
this.Logger.LogError(e, "Failed to process Dockerfile: {Location}", filePath);
}
}

private bool IsInSkippedFolder(string filePath)
{
var pathSegments = filePath.Split(['/', '\\'], StringSplitOptions.RemoveEmptyEntries);
return this.SkippedFolders.Any(skippedFolder => pathSegments.Contains(skippedFolder, StringComparer.OrdinalIgnoreCase));
}

private Task ParseDockerFileAsync(string fileContents, string fileLocation, ISingleFileComponentRecorder singleFileComponentRecorder, Dictionary<string, string> stageNameMap)
{
var dockerfileModel = Dockerfile.Parse(fileContents);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#nullable enable
namespace Microsoft.ComponentDetection.Detectors.Tests;

using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using AwesomeAssertions;
using Microsoft.ComponentDetection.Contracts;
using Microsoft.ComponentDetection.Contracts.TypedComponent;
using Microsoft.ComponentDetection.Detectors.Dockerfile;
using Microsoft.ComponentDetection.TestsUtilities;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

Expand Down Expand Up @@ -210,6 +213,51 @@ public async Task TestDockerfile_PrefixedFilenameAsync()
componentRecorder.GetDetectedComponents().Should().ContainSingle();
}

[TestMethod]
[DataRow("dockerfile.mjs")]
[DataRow("dockerfile.d.mts")]
public async Task TestDockerfile_ShikiLanguageDefinitionIsIgnoredAsync(string fileName)
{
var loggerMock = new Mock<ILogger<DockerfileComponentDetector>>();

var (scanResult, componentRecorder) = await this.DetectorTestUtility
.WithFile(fileName, "export default Object.freeze({ name: 'dockerfile' });")
.AddServiceMock(loggerMock)
.ExecuteDetectorAsync();

scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);
componentRecorder.GetDetectedComponents().Should().BeEmpty();
Comment thread
jpinz marked this conversation as resolved.
loggerMock.Verify(
logger => logger.Log(
LogLevel.Warning,
It.IsAny<EventId>(),
It.IsAny<It.IsAnyType>(),
It.IsAny<Exception>(),
It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
Times.Once);
loggerMock.Verify(
logger => logger.Log(
LogLevel.Error,
It.IsAny<EventId>(),
It.IsAny<It.IsAnyType>(),
It.IsAny<Exception>(),
It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
Times.Never);
}

[TestMethod]
public async Task TestDockerfile_UnderNodeModulesIsIgnoredAsync()
{
var fileLocation = Path.Combine(Path.GetTempPath(), "node_modules", "shiki", "dist", "langs", "dockerfile.mjs");

var (scanResult, componentRecorder) = await this.DetectorTestUtility
.WithFile("dockerfile.mjs", "FROM redis:7-alpine", fileLocation: fileLocation)
.ExecuteDetectorAsync();

scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);
componentRecorder.GetDetectedComponents().Should().BeEmpty();
}

[TestMethod]
public async Task TestDockerfile_NoFromInstructionsAsync()
{
Expand Down
Loading