From 7cbace4f5fe054c86ec08bcd1117adb19ced64c3 Mon Sep 17 00:00:00 2001 From: Gabe Stocco Date: Thu, 9 May 2019 06:40:31 -0700 Subject: [PATCH] Gfs/add trim to latest (#196) * Add a new command to trim all runs except latest * Add exploded output option --- Cli/Program.cs | 70 ++++++++++++++++++++++++++++++++++++++++--------- Cli/run_list | 13 +++++++++ Gui/asa.log.txt | 0 3 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 Cli/run_list create mode 100644 Gui/asa.log.txt diff --git a/Cli/Program.cs b/Cli/Program.cs index 37b5c737c..4b2b8c5a6 100644 --- a/Cli/Program.cs +++ b/Cli/Program.cs @@ -61,6 +61,9 @@ public class ExportCollectCommandOptions [Option(HelpText = "Directory to output to", Default = ".")] public string OutputPath { get; set; } + [Option(HelpText = "Exploded output")] + public bool ExplodedOutput { get; set; } + [Option(Default = false, HelpText = "Increase logging verbosity")] public bool Verbose { get; set; } @@ -183,6 +186,10 @@ public class ConfigCommandOptions [Option("delete-run", Required = false, HelpText = "Delete a specific run from the database")] public string DeleteRunId { get; set; } + + [Option("trim-to-latest", HelpText = "Delete all runs except the latest.")] + public bool TrimToLatest { get; set; } + } public static class AttackSurfaceAnalyzerCLI @@ -259,7 +266,6 @@ private static int RunConfigCommand(ConfigCommandOptions opts) if (opts.ListRuns) { - if (_isFirstRun) { Log.Warning(Strings.Get("FirstRunListRunsError"), opts.DatabaseFilename); @@ -375,6 +381,24 @@ private static int RunConfigCommand(ConfigCommandOptions opts) { DatabaseManager.DeleteRun(opts.DeleteRunId); } + if (opts.TrimToLatest) + { + string GET_RUNS = "select run_id from runs order by timestamp desc;"; + + List Runs = new List(); + + var cmd = new SqliteCommand(GET_RUNS, DatabaseManager.Connection, DatabaseManager.Transaction); + using (var reader = cmd.ExecuteReader()) + { + //Skip first row, that is the one we want to keep + reader.Read(); + + while (reader.Read()) + { + DatabaseManager.DeleteRun((string)reader["run_id"]); + } + } + } } return 0; @@ -420,7 +444,7 @@ private static int RunExportCollectCommand(ExportCollectCommandOptions opts) options.DatabaseFilename = opts.DatabaseFilename; options.FirstRunId = opts.FirstRunId; options.SecondRunId = opts.SecondRunId; - + var results = CompareRuns(options); JsonSerializer serializer = new JsonSerializer { @@ -428,19 +452,41 @@ private static int RunExportCollectCommand(ExportCollectCommandOptions opts) NullValueHandling = NullValueHandling.Ignore }; serializer.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter()); - Log.Debug("{0} RunExportCollectCommand", Strings.Get("End")); - string path = Path.Combine(opts.OutputPath, Helpers.MakeValidFileName(opts.FirstRunId + "_vs_" + opts.SecondRunId + "_summary.json.txt")); - var output = new Dictionary(); - output["results"] = results; - output["metadata"] = Helpers.GenerateMetadata(); - using (StreamWriter sw = new StreamWriter(path)) //lgtm[cs/path-injection] + + + if (opts.ExplodedOutput) { - using (JsonWriter writer = new JsonTextWriter(sw)) + results.Add("metadata", Helpers.GenerateMetadata()); + string path = Path.Combine(opts.OutputPath, Helpers.MakeValidFileName(opts.FirstRunId + "_vs_" + opts.SecondRunId)); + Directory.CreateDirectory(path); + foreach(var key in results.Keys) { - serializer.Serialize(writer, output); + string filePath = Path.Combine(path, Helpers.MakeValidFileName(key)); + 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"), path); + } + else + { + string path = Path.Combine(opts.OutputPath, Helpers.MakeValidFileName(opts.FirstRunId + "_vs_" + opts.SecondRunId + "_summary.json.txt")); + var output = new Dictionary(); + output["results"] = results; + output["metadata"] = Helpers.GenerateMetadata(); + 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"), path); } - Log.Information(Strings.Get("OutputWrittenTo"), path); return 0; } @@ -1347,7 +1393,7 @@ public static List GetMonitorRuns() public static List GetRuns(string type) { - string Select_Runs = "select distinct run_id from runs where type=@type;"; + string Select_Runs = "select distinct run_id from runs where type=@type order by timestamp asc;"; List Runs = new List(); diff --git a/Cli/run_list b/Cli/run_list new file mode 100644 index 000000000..89d5e7906 --- /dev/null +++ b/Cli/run_list @@ -0,0 +1,13 @@ +[20:35:53 INF] AttackSurfaceAnalyzerCli v.1.0.0 +[20:35:53 DBG] Starting database setup +[20:35:53 DBG] Done with database setup +[20:35:53 DBG] Starting database setup +[20:35:53 DBG] Done with database setup +[20:35:53 INF] Dumping data from database located at asa.sqlite. +[20:35:53 INF] Begin Enumerating Collect Run Ids +[20:35:53 INF] RunId:2019-05-07 20:33:54 Timestamp:2019-05-07 20:33:55 AsaVersion:1.0.0 +[20:35:53 INF] CERTIFICATE : 179 +[20:35:53 INF] RunId:2019-05-07 20:35:13 Timestamp:2019-05-07 20:35:13 AsaVersion:1.0.0 +[20:35:53 INF] CERTIFICATE : 179 +[20:35:53 INF] There were no monitor runs in the database. +[20:35:53 INF] Attack Surface Analyzer Completed. diff --git a/Gui/asa.log.txt b/Gui/asa.log.txt new file mode 100644 index 000000000..e69de29bb