Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2228 Create --kind and --level arguments for more granular control #2241

Merged
merged 39 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
d8fbaee
Creating BaseLogger to share methods
eddynaka Jan 12, 2021
dd2a148
removing logmessage from interface
eddynaka Jan 12, 2021
5bccb3d
updating more loggers
eddynaka Jan 12, 2021
ceb56ae
Merge branch 'main' into users/ednakamu/base-logger
jameswinkler Jan 14, 2021
c52e91a
Fix parameters
jameswinkler Jan 14, 2021
1b16fb8
Replace verbose in ConsoleLogger. It should know about quiet instead
jameswinkler Jan 15, 2021
ec37c1e
Merge branch 'main' into users/v-jwinkler/#2228_base-logger
jameswinkler Jan 15, 2021
c3d8e91
Remove verbose
jameswinkler Jan 15, 2021
2e07ac5
Merge main -> users/ednakamu/base-logger
jameswinkler Jan 15, 2021
d8fc5e4
Remove unused method
jameswinkler Jan 15, 2021
35247a6
Merge branch 'users/ednakamu/base-logger' into users/v-jwinkler/#2228…
jameswinkler Jan 15, 2021
e54cf2c
Remove unnecessary nullcheck
jameswinkler Jan 16, 2021
de5f2b9
fixing functional tests
eddynaka Jan 16, 2021
81fa57e
back to quiet
eddynaka Jan 16, 2021
662c140
fixing tests
eddynaka Jan 16, 2021
af84726
Use ShouldLog in more locations
jameswinkler Jan 18, 2021
0f12856
Add to release history for breaking change
jameswinkler Jan 18, 2021
3d7fab4
Merge pull request #2246 from microsoft/users/v-jwinkler/#2228_base-l…
jameswinkler Jan 18, 2021
3092114
Fix formatting
jameswinkler Jan 18, 2021
f068b44
Make more loggers children of BaseLogger. Cleanup some functionality…
jameswinkler Jan 18, 2021
cd8a99f
fixing spacing
eddynaka Jan 18, 2021
130b1fa
Rename some parameters, classes, and files. Remove "quiet" from Logg…
jameswinkler Jan 18, 2021
508ae6b
More complete renaming
jameswinkler Jan 18, 2021
0b081e3
Merge pull request #2247 from microsoft/users/v-jwinkler/#2228_base-l…
jameswinkler Jan 18, 2021
eef88e2
Rewording, minor cleanup
jameswinkler Jan 18, 2021
4cbed39
fixing sample build
eddynaka Jan 19, 2021
a1c523c
Correct validate logic. Remove redundant "none"s
jameswinkler Jan 19, 2021
6bc4b62
Rename parameters, reword helper text
jameswinkler Jan 19, 2021
b2ad7af
Add unit tests for DriverExtensionMethod and BaseLogger
jameswinkler Jan 19, 2021
7440769
Fix casting error
jameswinkler Jan 19, 2021
2b53283
fixing samples compilation issue
eddynaka Jan 20, 2021
ba0c58b
Small changes to address comments
jameswinkler Jan 20, 2021
908e5ae
Merge pull request #2249 from microsoft/users/v-jwinkler/#2228_base-l…
jameswinkler Jan 20, 2021
a49b35d
Formatting fixes
jameswinkler Jan 20, 2021
66e51f2
Address renaming and refactoring comments
jameswinkler Jan 21, 2021
1720e89
More renaming
jameswinkler Jan 21, 2021
0f0bee9
Merge branch 'main' into users/ednakamu/base-logger
eddynaka Jan 21, 2021
1a1c3fe
Change validation in BaseLogger and correct unit tests and code (#2262)
jameswinkler Jan 25, 2021
ea67391
Merge branch 'main' into users/ednakamu/base-logger
eddynaka Jan 25, 2021
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
3 changes: 3 additions & 0 deletions src/ReleaseHistory.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# SARIF Package Release History (SDK, Driver, Converters, and Multitool)

* BREAKING: Entirely remove "verbose" whose fuctionality has been replaced by --level and --kind. [#2241](https://github.com/microsoft/sarif-sdk/pull/2241)
* BREAKING: Rename "LoggingOptions" to "LogFilePersistenceOptions". [#2241](https://github.com/microsoft/sarif-sdk/pull/2241)
* FEATURE: --quiet will now suppress all console messages except for errors. [#2241](https://github.com/microsoft/sarif-sdk/pull/2241)
* BUGFIX: Fix NullReference in SARIF1012 rule validation [#2254](https://github.com/microsoft/sarif-sdk/pull/2254)

## **v2.3.17** [Sdk](https://www.nuget.org/packages/Sarif.Sdk/2.3.17) | [Driver](https://www.nuget.org/packages/Sarif.Driver/2.3.17) | [Converters](https://www.nuget.org/packages/Sarif.Converters/2.3.17) | [Multitool](https://www.nuget.org/packages/Sarif.Multitool/2.3.17) | [Multitool Library](https://www.nuget.org/packages/Sarif.Multitool.Library/2.3.17)
Expand Down
2 changes: 1 addition & 1 deletion src/Samples/Sarif.Sdk.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ internal static int CreateSarifLogFile(CreateOptions options)
{
using (var sarifLogger = new SarifLogger(
textWriter,
loggingOptions: LoggingOptions.PrettyPrint, // Use PrettyPrint to generate readable (multi-line, indented) JSON
logFilePersistenceOptions: LogFilePersistenceOptions.PrettyPrint, // Use PrettyPrint to generate readable (multi-line, indented) JSON
dataToInsert:
OptionallyEmittedData.TextFiles | // Embed source file content directly in the log file -- great for portability of the log!
OptionallyEmittedData.Hashes |
Expand Down
2 changes: 1 addition & 1 deletion src/Samples/SarifTrim/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private static void Main(string[] args)
consolidator.RemoveWebResponses = (removeParts.Contains("WebResponses"));

// Consolidate the SarifLog per settings
using (SarifLogger logger = new SarifLogger(outputFilePath, LoggingOptions.OverwriteExistingOutputFile, tool: run.Tool, run: run))
using (SarifLogger logger = new SarifLogger(outputFilePath, LogFilePersistenceOptions.OverwriteExistingOutputFile, tool: run.Tool, run: run))
{
foreach (Result result in run.Results)
{
Expand Down
10 changes: 2 additions & 8 deletions src/Sarif.Converters/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,5 @@

using System.Diagnostics.CodeAnalysis;

[module: SuppressMessage("Microsoft.Security.Xml", "CA3053:UseXmlSecureResolver", Scope = "member", Target = "Microsoft.CodeAnalysis.Sarif.Converters.AndroidStudioConverter.#Convert(System.IO.Stream,Microsoft.CodeAnalysis.Sarif.IResultLogWriter,Microsoft.CodeAnalysis.Sarif.Writers.LoggingOptions)")]
[module: SuppressMessage("Microsoft.Security.Xml", "CA3053:UseXmlSecureResolver", Scope = "member", Target = "Microsoft.CodeAnalysis.Sarif.Converters.ClangAnalyzerConverter.#Convert(System.IO.Stream,Microsoft.CodeAnalysis.Sarif.IResultLogWriter,Microsoft.CodeAnalysis.Sarif.Writers.LoggingOptions)")]
[module: SuppressMessage("Microsoft.Security.Xml", "CA3053:UseXmlSecureResolver", Scope = "member", Target = "Microsoft.CodeAnalysis.Sarif.Converters.CppCheckConverter.#Convert(System.IO.Stream,Microsoft.CodeAnalysis.Sarif.IResultLogWriter,Microsoft.CodeAnalysis.Sarif.Writers.LoggingOptions)")]
[module: SuppressMessage("Microsoft.Security.Xml", "CA3053:UseXmlSecureResolver", Scope = "member", Target = "Microsoft.CodeAnalysis.Sarif.Converters.FortifyConverter.#Convert(System.IO.Stream,Microsoft.CodeAnalysis.Sarif.IResultLogWriter,Microsoft.CodeAnalysis.Sarif.Writers.LoggingOptions)")]
[module: SuppressMessage("Microsoft.Security.Xml", "CA3053:UseXmlSecureResolver", Scope = "member", Target = "Microsoft.CodeAnalysis.Sarif.Converters.FortifyFprConverter.#ParseAuditStream(System.IO.Stream)")]
[module: SuppressMessage("Microsoft.Security.Xml", "CA3053:UseXmlSecureResolver", Scope = "member", Target = "Microsoft.CodeAnalysis.Sarif.Converters.FxCopLogReader.#Read(Microsoft.CodeAnalysis.Sarif.Converters.FxCopLogReader+Context,System.IO.Stream)")]
[module: SuppressMessage("Microsoft.Security.Xml", "CA3053:UseXmlSecureResolver", Scope = "member", Target = "Microsoft.CodeAnalysis.Sarif.Converters.PREfastConverter.#Convert(System.IO.Stream,Microsoft.CodeAnalysis.Sarif.IResultLogWriter,Microsoft.CodeAnalysis.Sarif.Writers.LoggingOptions)")]
[module: SuppressMessage("Microsoft.Security.Xml", "CA3053:UseXmlSecureResolver", Scope = "member", Target = "Microsoft.CodeAnalysis.Sarif.Converters.SparseReader.#CreateFromStream(Microsoft.CodeAnalysis.Sarif.Converters.SparseReaderDispatchTable,System.IO.Stream,System.Xml.Schema.XmlSchemaSet)")]
[module: SuppressMessage("Microsoft.Security.Xml", "CA3053:UseXmlSecureResolver", Scope = "member", Target = "~M:Microsoft.CodeAnalysis.Sarif.Converters.FxCopLogReader.Read(Microsoft.CodeAnalysis.Sarif.Converters.FxCopLogReader.Context,System.IO.Stream)")]
[module: SuppressMessage("Microsoft.Security.Xml", "CA3053:UseXmlSecureResolver", Scope = "member", Target = "~M:Microsoft.CodeAnalysis.Sarif.Converters.SparseReader.CreateFromStream(Microsoft.CodeAnalysis.Sarif.Converters.SparseReaderDispatchTable,System.IO.Stream,System.Xml.Schema.XmlSchemaSet)~Microsoft.CodeAnalysis.Sarif.Converters.SparseReader")]
4 changes: 2 additions & 2 deletions src/Sarif.Converters/ToolFormatConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void ConvertToStandardFormat(
string toolFormat,
string inputFileName,
string outputFileName,
LoggingOptions loggingOptions = LoggingOptions.None,
LogFilePersistenceOptions logFilePersistenceOptions = LogFilePersistenceOptions.None,
OptionallyEmittedData dataToInsert = OptionallyEmittedData.None,
string pluginAssemblyPath = null)
{
Expand All @@ -52,7 +52,7 @@ public void ConvertToStandardFormat(
using (var outputTextWriter = new StreamWriter(outputTextStream))
using (var outputJson = new JsonTextWriter(outputTextWriter))
{
if (loggingOptions.HasFlag(LoggingOptions.PrettyPrint))
if (logFilePersistenceOptions.HasFlag(LogFilePersistenceOptions.PrettyPrint))
{
outputJson.Formatting = Formatting.Indented;
}
Expand Down
25 changes: 17 additions & 8 deletions src/Sarif.Driver/DriverExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ namespace Microsoft.CodeAnalysis.Sarif.Driver
{
public static class DriverExtensionMethods
{
public static LoggingOptions ConvertToLoggingOptions(this AnalyzeOptionsBase analyzeOptions)
public static LogFilePersistenceOptions ConvertToLogFilePersistenceOptions(this AnalyzeOptionsBase analyzeOptions)
{
LoggingOptions loggingOptions = LoggingOptions.PrettyPrint;
LogFilePersistenceOptions logFilePersistenceOptions = LogFilePersistenceOptions.PrettyPrint;

if (analyzeOptions.Force) { loggingOptions |= LoggingOptions.OverwriteExistingOutputFile; }
if (analyzeOptions.Minify) { loggingOptions ^= LoggingOptions.PrettyPrint; }
if (analyzeOptions.Verbose) { loggingOptions |= LoggingOptions.Verbose; }
if (analyzeOptions.Optimize) { loggingOptions |= LoggingOptions.Optimize; }
if (analyzeOptions.PrettyPrint) { loggingOptions |= LoggingOptions.PrettyPrint; }
if (analyzeOptions.Force) { logFilePersistenceOptions |= LogFilePersistenceOptions.OverwriteExistingOutputFile; }
if (analyzeOptions.Minify) { logFilePersistenceOptions ^= LogFilePersistenceOptions.PrettyPrint; }
if (analyzeOptions.Optimize) { logFilePersistenceOptions |= LogFilePersistenceOptions.Optimize; }
if (analyzeOptions.PrettyPrint) { logFilePersistenceOptions |= LogFilePersistenceOptions.PrettyPrint; }

return loggingOptions;
return logFilePersistenceOptions;
}

/// <summary>
Expand Down Expand Up @@ -139,12 +138,22 @@ public static bool ValidateOutputOptions(this MultipleFilesOptionsBase options)
{
bool valid = true;

// TODO: Is it correct to modify options in a "validate" method?
if (options.Inline)
{
options.Force = true;
}

return valid;
}

public static bool ValidateOutputOptions(this AnalyzeOptionsBase options)
{
bool valid = true;

valid &= !(options.Quiet && string.IsNullOrWhiteSpace(options.OutputFilePath));

return valid;
}
}
}
8 changes: 0 additions & 8 deletions src/Sarif.Driver/Sdk/AggregatingLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,6 @@ public void AnalyzingTarget(IAnalysisContext context)
}
}

public void LogMessage(bool verbose, string message)
{
foreach (IAnalysisLogger logger in Loggers)
{
logger.LogMessage(verbose, message);
}
}

public void Log(ReportingDescriptor rule, Result result)
{
foreach (IAnalysisLogger logger in Loggers)
Expand Down
21 changes: 14 additions & 7 deletions src/Sarif.Driver/Sdk/AnalyzeCommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,12 @@ protected virtual void ValidateOptions(TContext context, TOptions analyzeOptions
succeeded &= ValidateFiles(context, analyzeOptions.PluginFilePaths, shouldExist: true);
succeeded &= ValidateInvocationPropertiesToLog(context, analyzeOptions.InvocationPropertiesToLog);
succeeded &= ValidateOutputFileCanBeCreated(context, analyzeOptions.OutputFilePath, analyzeOptions.Force);
succeeded &= analyzeOptions.ValidateOutputOptions();

if (!succeeded)
{
// TODO: This seems like uninformative error output. All these errors get squished into one generic message
// whenever something goes wrong. #2260 https://github.com/microsoft/sarif-sdk/issues/2260
ThrowExitApplicationException(context, ExitReason.InvalidCommandLineOption);
}
}
Expand Down Expand Up @@ -267,13 +270,13 @@ internal AggregatingLogger InitializeLogger(AnalyzeOptionsBase analyzeOptions)

if (!analyzeOptions.Quiet)
{
_consoleLogger = new ConsoleLogger(analyzeOptions.Verbose, _tool.Driver.Name) { CaptureOutput = _captureConsoleOutput };
_consoleLogger = new ConsoleLogger(analyzeOptions.Quiet, _tool.Driver.Name, analyzeOptions.Level, analyzeOptions.Kind) { CaptureOutput = _captureConsoleOutput };
logger.Loggers.Add(_consoleLogger);
}

if ((analyzeOptions.DataToInsert.ToFlags() & OptionallyEmittedData.Hashes) != 0)
{
_cacheByFileHashLogger = new CacheByFileHashLogger(analyzeOptions.Verbose);
_cacheByFileHashLogger = new CacheByFileHashLogger(analyzeOptions.Level, analyzeOptions.Kind);
logger.Loggers.Add(_cacheByFileHashLogger);
}

Expand Down Expand Up @@ -408,7 +411,7 @@ private void InitializeOutputFile(TOptions analyzeOptions, TContext context, ISe
(
() =>
{
LoggingOptions loggingOptions = analyzeOptions.ConvertToLoggingOptions();
LogFilePersistenceOptions logFilePersistenceOptions = analyzeOptions.ConvertToLogFilePersistenceOptions();

OptionallyEmittedData dataToInsert = analyzeOptions.DataToInsert.ToFlags();
OptionallyEmittedData dataToRemove = analyzeOptions.DataToRemove.ToFlags();
Expand All @@ -419,27 +422,31 @@ private void InitializeOutputFile(TOptions analyzeOptions, TContext context, ISe
{
sarifLogger = new SarifLogger(
analyzeOptions.OutputFilePath,
loggingOptions,
logFilePersistenceOptions,
dataToInsert,
dataToRemove,
tool: _tool,
run: null,
analysisTargets: targets,
invocationTokensToRedact: GenerateSensitiveTokensList(),
invocationPropertiesToLog: analyzeOptions.InvocationPropertiesToLog);
invocationPropertiesToLog: analyzeOptions.InvocationPropertiesToLog,
levels: analyzeOptions.Level,
kinds: analyzeOptions.Kind);
}
else
{
sarifLogger = new SarifOneZeroZeroLogger(
analyzeOptions.OutputFilePath,
loggingOptions,
logFilePersistenceOptions,
dataToInsert,
dataToRemove,
tool: _tool,
run: null,
analysisTargets: targets,
invocationTokensToRedact: GenerateSensitiveTokensList(),
invocationPropertiesToLog: analyzeOptions.InvocationPropertiesToLog);
invocationPropertiesToLog: analyzeOptions.InvocationPropertiesToLog,
levels: analyzeOptions.Level,
kinds: analyzeOptions.Kind);
}
_pathToHashDataMap = sarifLogger.AnalysisTargetToHashDataMap;
sarifLogger.AnalysisStarted();
Expand Down
21 changes: 15 additions & 6 deletions src/Sarif.Driver/Sdk/AnalyzeOptionsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ public abstract class AnalyzeOptionsBase : CommonOptionsBase
HelpText = "File path to which analysis output will be written.")]
public string OutputFilePath { get; set; }

[Option(
"verbose",
HelpText = "Emit verbose output. The resulting comprehensive report is designed to provide appropriate evidence for compliance scenarios.")]
public bool Verbose { get; set; }

[Option(
'r',
"recurse",
Expand All @@ -41,7 +36,7 @@ public abstract class AnalyzeOptionsBase : CommonOptionsBase
[Option(
'q',
"quiet",
HelpText = "Do not log results to the console.")]
HelpText = "Suppress all console output (except for catastrophic tool runtime or configuration errors).")]
public bool Quiet { get; set; }

[Option(
Expand Down Expand Up @@ -95,5 +90,19 @@ public abstract class AnalyzeOptionsBase : CommonOptionsBase
"should be emitted to the console and log file (if appropriate). " +
"Valid values: ScanTime.")]
public virtual IEnumerable<string> Traces { get; set; }

[Option(
"level",
Separator = ';',
Default = new FailureLevel[] { FailureLevel.Error, FailureLevel.Warning },
HelpText = "Filter output of scan results to one or more failure levels. Valid values: Error, Warning and Note.")]
public IEnumerable<FailureLevel> Level { get; set; }

[Option(
"kind",
Separator = ';',
Default = new ResultKind[] { ResultKind.Fail },
HelpText = "Filter output one or more result kinds. Valid values: Fail (for literal scan results), Pass, Review, Open, NotApplicable and Informational.")]
public IEnumerable<ResultKind> Kind { get; set; }
}
}
18 changes: 11 additions & 7 deletions src/Sarif.Driver/Sdk/MultithreadedAnalyzeCommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ private async Task<bool> FindFilesAsync(TOptions options, TContext rootContext)
_fileContexts.Add(
CreateContext(
options,
new CachingLogger(),
new CachingLogger(options.Level, options.Kind),
rootContext.RuntimeErrors,
rootContext.Policy,
filePath: file)
Expand Down Expand Up @@ -551,7 +551,7 @@ internal AggregatingLogger InitializeLogger(AnalyzeOptionsBase analyzeOptions)

if (!analyzeOptions.Quiet)
{
_consoleLogger = new ConsoleLogger(analyzeOptions.Verbose, _tool.Driver.Name) { CaptureOutput = _captureConsoleOutput };
_consoleLogger = new ConsoleLogger(analyzeOptions.Quiet, _tool.Driver.Name, analyzeOptions.Level, analyzeOptions.Kind) { CaptureOutput = _captureConsoleOutput };
logger.Loggers.Add(_consoleLogger);
}

Expand Down Expand Up @@ -642,7 +642,7 @@ private void InitializeOutputFile(TOptions analyzeOptions, TContext context)
(
() =>
{
LoggingOptions loggingOptions = analyzeOptions.ConvertToLoggingOptions();
LogFilePersistenceOptions logFilePersistenceOptions = analyzeOptions.ConvertToLogFilePersistenceOptions();

OptionallyEmittedData dataToInsert = analyzeOptions.DataToInsert.ToFlags();
OptionallyEmittedData dataToRemove = analyzeOptions.DataToRemove.ToFlags();
Expand All @@ -655,27 +655,31 @@ private void InitializeOutputFile(TOptions analyzeOptions, TContext context)
{
sarifLogger = new SarifLogger(
analyzeOptions.OutputFilePath,
loggingOptions,
logFilePersistenceOptions,
dataToInsert,
dataToRemove,
tool: _tool,
run: _run,
analysisTargets: null,
invocationTokensToRedact: GenerateSensitiveTokensList(),
invocationPropertiesToLog: analyzeOptions.InvocationPropertiesToLog);
invocationPropertiesToLog: analyzeOptions.InvocationPropertiesToLog,
levels: analyzeOptions.Level,
kinds: analyzeOptions.Kind);
}
else
{
sarifLogger = new SarifOneZeroZeroLogger(
analyzeOptions.OutputFilePath,
loggingOptions,
logFilePersistenceOptions,
dataToInsert,
dataToRemove,
tool: _tool,
run: _run,
analysisTargets: null,
invocationTokensToRedact: GenerateSensitiveTokensList(),
invocationPropertiesToLog: analyzeOptions.InvocationPropertiesToLog);
invocationPropertiesToLog: analyzeOptions.InvocationPropertiesToLog,
levels: analyzeOptions.Level,
kinds: analyzeOptions.Kind);
}
_pathToHashDataMap = sarifLogger.AnalysisTargetToHashDataMap;
sarifLogger.AnalysisStarted();
Expand Down
Loading