Skip to content

Commit

Permalink
Gfs/add trim to latest (#196)
Browse files Browse the repository at this point in the history
* Add a new command to trim all runs except latest

* Add exploded output option
  • Loading branch information
gfs committed May 9, 2019
1 parent d8db3fd commit 7cbace4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 12 deletions.
70 changes: 58 additions & 12 deletions Cli/Program.cs
Expand Up @@ -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; }

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -259,7 +266,6 @@ private static int RunConfigCommand(ConfigCommandOptions opts)

if (opts.ListRuns)
{

if (_isFirstRun)
{
Log.Warning(Strings.Get("FirstRunListRunsError"), opts.DatabaseFilename);
Expand Down Expand Up @@ -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<string> Runs = new List<string>();

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;
Expand Down Expand Up @@ -420,27 +444,49 @@ 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
{
Formatting = Formatting.Indented,
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<string, Object>();
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<string, Object>();
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;

}
Expand Down Expand Up @@ -1347,7 +1393,7 @@ public static List<string> GetMonitorRuns()

public static List<string> 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<string> Runs = new List<string>();

Expand Down
13 changes: 13 additions & 0 deletions 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.
Empty file added Gui/asa.log.txt
Empty file.

0 comments on commit 7cbace4

Please sign in to comment.