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

Refactor to include yamlpath directly in rule engine nupkg #513

Merged
merged 2 commits into from
Oct 24, 2022
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
9 changes: 9 additions & 0 deletions AppInspector.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,17 @@ private static int VerifyOutputArgsAndRunAnalyze(CLIAnalyzeCmdOptions options)
options.OutputFilePath ??= "output.html";
var extensionCheck = Path.GetExtension(options.OutputFilePath);
if (extensionCheck is not ".html" and not ".htm")
{
loggerFactory.CreateLogger("Program")
.LogInformation(MsgHelp.GetString(MsgHelp.ID.ANALYZE_HTML_EXTENSION));
}
}

if (CommonOutputChecks(options))
{
return RunAnalyzeCommand(options);
}

return (int)Utils.ExitCode.CriticalError;
}

Expand Down Expand Up @@ -258,12 +263,16 @@ private static int RunAnalyzeCommand(CLIAnalyzeCmdOptions cliOptions)
if (!cliOptions.NoShowProgressBar)
{
if (string.IsNullOrEmpty(cliOptions.LogFilePath))
{
logger.LogInformation(
"Progress bar is enabled so console output will be suppressed. No LogFilePath has been configured so you will not receive log messages");
}
else
{
logger.LogInformation(
"Progress bar is enabled so console output will be suppressed. For log messages check the log at {LogPath}",
cliOptions.LogFilePath);
}
}

var adjustedFactory = GetAdjustedFactory(cliOptions);
Expand Down
2 changes: 2 additions & 0 deletions AppInspector.CLI/ResultsWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public void Write(Result result, CLICommandOptions options)
//post checks
if (options.OutputFilePath is not null && File.Exists(options.OutputFilePath) &&
new FileInfo(options.OutputFilePath).Length > MAX_HTML_REPORT_FILE_SIZE)
{
_logger.LogInformation(MsgHelp.GetString(MsgHelp.ID.ANALYZE_REPORTSIZE_WARN));
}

Finalize(writer, "Analyze");
return;
Expand Down
8 changes: 7 additions & 1 deletion AppInspector.CLI/TagInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ public Regex Expression
get
{
if (_expression == null)
{
_expression = new Regex(SearchPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
}

return _expression;
}
}
Expand Down Expand Up @@ -133,7 +136,10 @@ public string Confidence
get => _confidence;
set
{
if (Enum.TryParse(value, true, out Confidence test)) _confidence = value;
if (Enum.TryParse(value, true, out Confidence test))
{
_confidence = value;
}
}
}

Expand Down
42 changes: 40 additions & 2 deletions AppInspector.CLI/Writers/AnalyzeHtmlWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,14 @@ private string MergeResourceFiles(string inputPath)
StringBuilder stringBuilder = new();

if (Directory.Exists(inputPath))
{
try
{
var srcfileList = Directory.EnumerateFiles(inputPath, "*.*", SearchOption.AllDirectories);
if (!srcfileList.Any())
{
throw new OpException(MsgHelp.FormatString(MsgHelp.ID.CMD_INVALID_FILE_OR_DIR, inputPath));
}

foreach (var fileName in srcfileList)
{
Expand All @@ -153,6 +156,7 @@ private string MergeResourceFiles(string inputPath)
{
throw new OpException(MsgHelp.FormatString(MsgHelp.ID.CMD_INVALID_FILE_OR_DIR, inputPath));
}
}

return stringBuilder.ToString();
}
Expand All @@ -168,16 +172,24 @@ private void RenderResultsSafeforHTML()
{
//safeguard simple string meta-data
if (_appMetaData?.ApplicationName != null)
{
_appMetaData.ApplicationName = SafeString(_appMetaData?.ApplicationName);
}

if (_appMetaData?.Description != null)
{
_appMetaData.Description = SafeString(_appMetaData?.Description);
}

if (_appMetaData?.Authors != null)
{
_appMetaData.Authors = SafeString(_appMetaData?.Authors);
}

if (_appMetaData?.SourceVersion != null)
{
_appMetaData.SourceVersion = SafeString(_appMetaData?.SourceVersion);
}

//safeguard lists data
SafeList(_appMetaData?.AppTypes);
Expand All @@ -204,7 +216,10 @@ private void SafeList(List<string>? valuesList)

private string SafeString(string? value)
{
if (!string.IsNullOrEmpty(value)) return WebUtility.HtmlEncode(value);
if (!string.IsNullOrEmpty(value))
{
return WebUtility.HtmlEncode(value);
}

return "";
}
Expand All @@ -216,11 +231,15 @@ public void PopulateTagGroups()
{
//read default/user preferences on what tags to report presence on and groupings
if (File.Exists(Utils.GetPath(Utils.AppPath.tagGroupPref)))
{
TagGroupPreferences =
JsonConvert.DeserializeObject<List<TagCategory>>(
File.ReadAllText(Utils.GetPath(Utils.AppPath.tagGroupPref)));
}
else
{
TagGroupPreferences = new List<TagCategory>();
}

string[] unSupportedGroupsOrPatterns = { "metric", "dependency" };

Expand All @@ -236,18 +255,22 @@ public void PopulateTagGroups()

var test = tagGroup.Title.ToLower().Contains(unSupportedGroupsOrPatterns[0]);
if (unSupportedGroupsOrPatterns.Any(x => tagGroup.Title.ToLower().Contains(x)))
{
_logger.LogWarning(
"Unsupported tag group or pattern detected '{title}'. See online documentation at https://github.com/microsoft/ApplicationInspector/wiki/3.5-Tags",
tagGroup.Title);
}

foreach (var pattern in tagGroup.Patterns ?? new List<TagSearchPattern>())
{
pattern.Detected = _appMetaData?.UniqueTags is not null &&
_appMetaData.UniqueTags.Any(v => v == pattern.SearchPattern);
if (unSupportedGroupsOrPatterns.Any(x => pattern.SearchPattern.ToLower().Contains(x)))
{
_logger.LogWarning(
"Unsupported tag group or pattern detected '{pattern}'. See online documentation at https://github.com/microsoft/ApplicationInspector/wiki/3.5-Tags",
pattern.SearchPattern);
}

//create dynamic "category" groups of tags with pattern relationship established from TagReportGroups.json
//that can be used to populate reports with various attributes for each tag detected
Expand Down Expand Up @@ -358,7 +381,9 @@ private List<TagInfo> GetTagInfoListByTagGroup(TagGroup tagGroup, bool addNotFou
Severity oldSeverity;
Enum.TryParse(updateItem.Severity, out oldSeverity);
if (match.Severity > oldSeverity)
{
updateItem.Severity = match.Severity.ToString();
}

break;
}
Expand Down Expand Up @@ -453,7 +478,10 @@ private List<TagInfo> GetAllMatchingTagInfoList(TagGroup tagGroup, bool addNotFo

Severity oldSeverity;
Enum.TryParse(updateItem.Severity, out oldSeverity);
if (match.Severity > oldSeverity) updateItem.Severity = match.Severity.ToString();
if (match.Severity > oldSeverity)
{
updateItem.Severity = match.Severity.ToString();
}

break;
}
Expand All @@ -479,6 +507,7 @@ private List<TagInfo> GetTagInfoListByName()
foreach (var match in _appMetaData?.Matches ?? new List<MatchRecord>())
foreach (var testTag in match.Tags ?? Array.Empty<string>())
if (tag == testTag)
{
if (dupCheck.Add(testTag))
{
result.Add(new TagInfo
Expand All @@ -491,6 +520,7 @@ private List<TagInfo> GetTagInfoListByName()

break;
}
}
}
else
{
Expand Down Expand Up @@ -521,14 +551,18 @@ private List<TagInfo> GetTagInfoListByConfidence()
foreach (var match in _appMetaData?.Matches ?? new List<MatchRecord>())
foreach (var testTag in match.Tags ?? Array.Empty<string>())
if (searchPattern.IsMatch(testTag))
{
if (match.Confidence == confidence && dupCheck.Add(tag))
{
result.Add(new TagInfo
{
Tag = testTag,
Confidence = confidence.ToString(),
Severity = match.Severity.ToString(),
ShortTag = testTag[(testTag.LastIndexOf('.') + 1)..]
});
}
}
}

return result;
Expand All @@ -552,14 +586,18 @@ private List<TagInfo> GetTagInfoListBySeverity()
foreach (var match in _appMetaData?.Matches ?? new List<MatchRecord>())
foreach (var testTag in match.Tags ?? Array.Empty<string>())
if (searchPattern.IsMatch(testTag))
{
if (match.Severity == severity && dupCheck.Add(tag))
{
result.Add(new TagInfo
{
Tag = testTag,
Confidence = match.Confidence.ToString(),
Severity = severity.ToString(),
ShortTag = testTag[(testTag.LastIndexOf('.') + 1)..]
});
}
}
}

return result;
Expand Down
12 changes: 9 additions & 3 deletions AppInspector.CLI/Writers/AnalyzeJsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ public override void WriteResults(Result result, CLICommandOptions commandOption

JsonSerializer jsonSerializer = new();
jsonSerializer.Formatting = Formatting.Indented;
if (TextWriter != null) jsonSerializer.Serialize(TextWriter, analyzeResult);

if (autoClose) FlushAndClose();
if (TextWriter != null)
{
jsonSerializer.Serialize(TextWriter, analyzeResult);
}

if (autoClose)
{
FlushAndClose();
}
}

/// <summary>
Expand Down
23 changes: 20 additions & 3 deletions AppInspector.CLI/Writers/AnalyzeSarifWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ internal static class AnalyzeSarifWriterExtensions
{
internal static void AddRange(this TagsCollection tc, IEnumerable<string>? tagsToAdd)
{
if (tagsToAdd is null) return;
if (tagsToAdd is null)
{
return;
}

foreach (var tag in tagsToAdd) tc.Add(tag);
}
}
Expand All @@ -40,7 +44,11 @@ public AnalyzeSarifWriter(TextWriter textWriter, ILoggerFactory? loggerFactory =

public override void WriteResults(Result result, CLICommandOptions commandOptions, bool autoClose = true)
{
if (TextWriter is null) throw new NullReferenceException(nameof(TextWriter));
if (TextWriter is null)
{
throw new NullReferenceException(nameof(TextWriter));
}

string? basePath = null;
if (commandOptions is CLIAnalyzeCmdOptions cLIAnalyzeCmdOptions)
{
Expand All @@ -56,6 +64,7 @@ public override void WriteResults(Result result, CLICommandOptions commandOption
var run = new Run();

if (Uri.TryCreate(cLIAnalyzeCmdOptions.RepositoryUri, UriKind.RelativeOrAbsolute, out var uri))
{
run.VersionControlProvenance = new List<VersionControlDetails>
{
new()
Expand All @@ -64,6 +73,7 @@ public override void WriteResults(Result result, CLICommandOptions commandOption
RevisionId = cLIAnalyzeCmdOptions.CommitHash
}
};
}

var artifacts = new List<Artifact>();
run.Tool = new Tool
Expand Down Expand Up @@ -111,7 +121,11 @@ public override void WriteResults(Result result, CLICommandOptions commandOption
if (match.FileName is not null)
{
var fileName = match.FileName;
if (basePath is not null) fileName = Path.GetRelativePath(basePath, fileName);
if (basePath is not null)
{
fileName = Path.GetRelativePath(basePath, fileName);
}

if (Uri.TryCreate(fileName, UriKind.RelativeOrAbsolute, out var outUri))
{
var artifactIndex = artifacts.FindIndex(a => a.Location.Uri.Equals(outUri));
Expand All @@ -128,7 +142,10 @@ public override void WriteResults(Result result, CLICommandOptions commandOption
artifactIndex = artifact.Location.Index;
artifact.Tags.AddRange(match.Rule.Tags);
if (match.LanguageInfo is { } languageInfo)
{
artifact.SourceLanguage = languageInfo.Name;
}

artifacts.Add(artifact);
}
else
Expand Down
Loading