From 02f931399d39effbd26a64c7e99b3cf075bbf3c5 Mon Sep 17 00:00:00 2001 From: Justin Perez Date: Mon, 6 Mar 2023 08:54:23 -0800 Subject: [PATCH 1/2] feat: add `--PrintManifest` option --- .../ArgumentSets/BcdeArguments.cs | 3 +++ .../ArgumentSets/IDetectionArguments.cs | 2 ++ .../Orchestrator.cs | 2 +- .../Services/BcdeScanCommandService.cs | 12 ++++++++++-- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.ComponentDetection.Orchestrator/ArgumentSets/BcdeArguments.cs b/src/Microsoft.ComponentDetection.Orchestrator/ArgumentSets/BcdeArguments.cs index 255e447af..68a83b4ea 100644 --- a/src/Microsoft.ComponentDetection.Orchestrator/ArgumentSets/BcdeArguments.cs +++ b/src/Microsoft.ComponentDetection.Orchestrator/ArgumentSets/BcdeArguments.cs @@ -38,6 +38,9 @@ public class BcdeArguments : BaseArguments, IDetectionArguments [Option("ManifestFile", Required = false, HelpText = "The file to write scan results to.")] public FileInfo ManifestFile { get; set; } + [Option("PrintManifest", Required = false, HelpText = "Prints the manifest to standard output.")] + public bool PrintManifest { get; set; } + public string ManifestFileSerialized => this.ManifestFile?.ToString(); [Option("DockerImagesToScan", Required = false, Separator = ',', HelpText = "Comma separated list of docker image names or hashes to execute container scanning on, ex: ubuntu:16.04, 56bab49eef2ef07505f6a1b0d5bd3a601dfc3c76ad4460f24c91d6fa298369ab")] diff --git a/src/Microsoft.ComponentDetection.Orchestrator/ArgumentSets/IDetectionArguments.cs b/src/Microsoft.ComponentDetection.Orchestrator/ArgumentSets/IDetectionArguments.cs index 72a823fa3..351d78ed7 100644 --- a/src/Microsoft.ComponentDetection.Orchestrator/ArgumentSets/IDetectionArguments.cs +++ b/src/Microsoft.ComponentDetection.Orchestrator/ArgumentSets/IDetectionArguments.cs @@ -20,5 +20,7 @@ public interface IDetectionArguments : IScanArguments FileInfo ManifestFile { get; set; } + public bool PrintManifest { get; set; } + IEnumerable DockerImagesToScan { get; set; } } diff --git a/src/Microsoft.ComponentDetection.Orchestrator/Orchestrator.cs b/src/Microsoft.ComponentDetection.Orchestrator/Orchestrator.cs index d62f0db17..fb00f3e0d 100644 --- a/src/Microsoft.ComponentDetection.Orchestrator/Orchestrator.cs +++ b/src/Microsoft.ComponentDetection.Orchestrator/Orchestrator.cs @@ -68,7 +68,7 @@ public async Task LoadAsync(string[] args, CancellationToken cancell var reloadableLogger = (ReloadableLogger)Log.Logger; reloadableLogger.Reload(configuration => configuration - .WriteTo.Console() + .WriteTo.Console(standardErrorFromLevel: LogEventLevel.Debug) .WriteTo.Async(x => x.File(logFile)) .WriteTo.Providers(this.serviceProvider.GetRequiredService()) .MinimumLevel.Is(baseArguments.Verbosity switch diff --git a/src/Microsoft.ComponentDetection.Orchestrator/Services/BcdeScanCommandService.cs b/src/Microsoft.ComponentDetection.Orchestrator/Services/BcdeScanCommandService.cs index 7bfc48944..6b10e3fe5 100644 --- a/src/Microsoft.ComponentDetection.Orchestrator/Services/BcdeScanCommandService.cs +++ b/src/Microsoft.ComponentDetection.Orchestrator/Services/BcdeScanCommandService.cs @@ -1,5 +1,6 @@ namespace Microsoft.ComponentDetection.Orchestrator.Services; +using System; using System.IO; using System.Threading.Tasks; using Microsoft.ComponentDetection.Common; @@ -53,13 +54,20 @@ private void WriteComponentManifest(IDetectionArguments detectionArguments, Scan this.logger.LogInformation("Scan Manifest file: {ManifestFile}", this.fileWritingService.ResolveFilePath(ManifestRelativePath)); } + var manifestJson = JsonConvert.SerializeObject(scanResult, Formatting.Indented); + if (userRequestedManifestPath == null) { - this.fileWritingService.AppendToFile(ManifestRelativePath, JsonConvert.SerializeObject(scanResult, Formatting.Indented)); + this.fileWritingService.AppendToFile(ManifestRelativePath, manifestJson); } else { - this.fileWritingService.WriteFile(userRequestedManifestPath, JsonConvert.SerializeObject(scanResult, Formatting.Indented)); + this.fileWritingService.WriteFile(userRequestedManifestPath, manifestJson); + } + + if (detectionArguments.PrintManifest) + { + Console.WriteLine(manifestJson); } } } From 805c1bc42c3c028b4d5132b29f89f0e4111de8e9 Mon Sep 17 00:00:00 2001 From: Justin Perez Date: Mon, 6 Mar 2023 13:50:44 -0800 Subject: [PATCH 2/2] fix: only write to stderr if `--PrintManifest` --- .../ArgumentSets/BcdeArguments.cs | 2 +- .../Orchestrator.cs | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.ComponentDetection.Orchestrator/ArgumentSets/BcdeArguments.cs b/src/Microsoft.ComponentDetection.Orchestrator/ArgumentSets/BcdeArguments.cs index 68a83b4ea..e5c249753 100644 --- a/src/Microsoft.ComponentDetection.Orchestrator/ArgumentSets/BcdeArguments.cs +++ b/src/Microsoft.ComponentDetection.Orchestrator/ArgumentSets/BcdeArguments.cs @@ -38,7 +38,7 @@ public class BcdeArguments : BaseArguments, IDetectionArguments [Option("ManifestFile", Required = false, HelpText = "The file to write scan results to.")] public FileInfo ManifestFile { get; set; } - [Option("PrintManifest", Required = false, HelpText = "Prints the manifest to standard output.")] + [Option("PrintManifest", Required = false, HelpText = "Prints the manifest to standard output. Logging will be redirected to standard error.")] public bool PrintManifest { get; set; } public string ManifestFileSerialized => this.ManifestFile?.ToString(); diff --git a/src/Microsoft.ComponentDetection.Orchestrator/Orchestrator.cs b/src/Microsoft.ComponentDetection.Orchestrator/Orchestrator.cs index fb00f3e0d..d5a6043e5 100644 --- a/src/Microsoft.ComponentDetection.Orchestrator/Orchestrator.cs +++ b/src/Microsoft.ComponentDetection.Orchestrator/Orchestrator.cs @@ -68,7 +68,11 @@ public async Task LoadAsync(string[] args, CancellationToken cancell var reloadableLogger = (ReloadableLogger)Log.Logger; reloadableLogger.Reload(configuration => configuration - .WriteTo.Console(standardErrorFromLevel: LogEventLevel.Debug) + .WriteTo.Console(standardErrorFromLevel: args.Contains( + $"--{nameof(IDetectionArguments.PrintManifest)}", + StringComparer.InvariantCultureIgnoreCase) + ? LogEventLevel.Debug + : null) .WriteTo.Async(x => x.File(logFile)) .WriteTo.Providers(this.serviceProvider.GetRequiredService()) .MinimumLevel.Is(baseArguments.Verbosity switch