in Cli/AttackSurfaceAnalyzerClient.cs [521:592]
private static ASA_ERROR ExportCompareResults(ConcurrentDictionary<(RESULT_TYPE, CHANGE_TYPE), List<CompareResult>> resultsIn, ExportOptions opts, string baseFileName, string analysesHash, IEnumerable<AsaRule> rules)
{
var results = resultsIn.Select(x => new KeyValuePair<string, object>($"{x.Key.Item1}_{x.Key.Item2}", x.Value)).ToDictionary(x => x.Key, x => x.Value);
JsonSerializer serializer = JsonSerializer.Create(new JsonSerializerSettings()
{
Formatting = Formatting.Indented,
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore,
Converters = new List<JsonConverter>() { new StringEnumConverter() },
ContractResolver = new AsaExportContractResolver()
});
var outputPath = opts.OutputPath;
if (outputPath is null)
{
outputPath = Directory.GetCurrentDirectory();
}
var metadata = AsaHelpers.GenerateMetadata();
metadata.Add("analyses-hash", analysesHash);
if (opts.ExplodedOutput)
{
results.Add("metadata", metadata);
string path = Path.Combine(outputPath, AsaHelpers.MakeValidFileName(baseFileName));
Directory.CreateDirectory(path);
foreach (var key in results.Keys)
{
string filePath = Path.Combine(path, AsaHelpers.MakeValidFileName(key));
if (opts.OutputSarif)
{
WriteSarifLog(new Dictionary<string, object>() { { key, results[key] } }, rules, filePath);
}
else
{
using (StreamWriter sw = new StreamWriter(filePath)) //lgtm[cs/path-injection]
{
using (JsonWriter writer = new JsonTextWriter(sw))
{
serializer.Serialize(writer, results[key]);
}
}
}
}
Log.Information(Strings.Get("OutputWrittenTo"), (new DirectoryInfo(path)).FullName);
}
else
{
string path = Path.Combine(outputPath, AsaHelpers.MakeValidFileName(baseFileName + "_summary.json.txt"));
var output = new Dictionary<string, object>();
output["results"] = results;
output["metadata"] = metadata;
if (opts.OutputSarif)
{
string pathSarif = Path.Combine(outputPath, AsaHelpers.MakeValidFileName(baseFileName + "_summary.Sarif"));
WriteSarifLog(output, rules, pathSarif);
Log.Information(Strings.Get("OutputWrittenTo"), (new FileInfo(pathSarif)).FullName);
}
else
{
using (StreamWriter sw = new StreamWriter(path)) //lgtm[cs/path-injection]
{
using (JsonWriter writer = new JsonTextWriter(sw))
{
serializer.Serialize(writer, output);
}
}
Log.Information(Strings.Get("OutputWrittenTo"), (new FileInfo(path)).FullName);
}
}
return ASA_ERROR.NONE;
}