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

local html reports improved #592

Merged
merged 8 commits into from
Sep 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
31 changes: 16 additions & 15 deletions core/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import json
import csv
import texttable
import html
from core.alert import messages
from core.alert import info
from core.alert import info, write
from core.compatible import version_info
from core.time import now
from core.die import die_failure
from database.db import get_logs_by_scan_unique_id
from database.db import submit_report_to_db
from core.utility import merge_logs_to_list


def build_graph(graph_name, events):
Expand Down Expand Up @@ -61,10 +63,8 @@ def build_texttable(events):
'date',
'target',
'module_name',
'scan_unique_id',
'port',
'event',
'json_event'
'logs'

]
_table.add_rows(
Expand All @@ -73,17 +73,16 @@ def build_texttable(events):
]
)
for event in events:
log = merge_logs_to_list(json.loads(event["json_event"]), [])
_table.add_rows(
[
table_headers,
[
event['date'],
event['target'],
event['module_name'],
event['scan_unique_id'],
event['port'],
event['event'],
event['json_event']
"\n".join(log) if log else "Detected"

]
]
Expand Down Expand Up @@ -128,26 +127,28 @@ def create_report(options, scan_unique_id):
'date',
'target',
'module_name',
'scan_unique_id',
'port',
'event',
'logs',
'json_event'
)
index=1
for event in all_scan_logs:
log = merge_logs_to_list(json.loads(event["json_event"]), [])
html_table_content += log_data.table_items.format(
event["date"],
event["target"],
event["module_name"],
event["scan_unique_id"],
event["port"],
event["event"],
event["json_event"]
"<br>".join(log) if log else "Detected", #event["event"], #log
index,
html.escape(event["json_event"])
)
html_table_content += log_data.table_end + '<p class="footer">' + messages("nettacker_version_details").format(
index+=1
html_table_content += log_data.table_end + '<div id="json_length">' + str(index-1) + '</div>' + '<p class="footer">' + messages("nettacker_version_details").format(
version_info()[0],
version_info()[1],
now()
) + '</p>'
) + '</p>' + log_data.json_parse_js
with open(report_path_filename, 'w', encoding='utf-8') as save:
save.write(html_table_content + '\n')
save.close()
Expand Down Expand Up @@ -177,7 +178,7 @@ def create_report(options, scan_unique_id):
build_texttable(all_scan_logs)
)
save.close()

write(build_texttable(all_scan_logs))
submit_report_to_db(
{
"date": now(model=None),
Expand Down
7 changes: 7 additions & 0 deletions lib/html_log/log_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
)
).read()

json_parse_js = open(
os.path.join(
nettacker_paths()['web_static_files_path'],
'report/json_parse.js'
)
).read()

table_title = open(
os.path.join(
nettacker_paths()['web_static_files_path'],
Expand Down
12 changes: 12 additions & 0 deletions web/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@
<script src="/js/bootstrap-tagsinput.min.js"></script>
<script src="/js/bootstrap-tagsinput-angular.min.js"></script>
<script src="/js/intro.min.js"></script>
<script src="/js/renderjson.js"></script>
<script>
length = document.getElementById("json_length").innerText;
document.getElementById("json_length").innerText = "";
renderjson.set_icons('▶','▼')
renderjson.set_collapse_msg(function (asd) {return "...";});
for (let i=1; i<=length; i++) {
value = document.getElementById("json_event_"+i).innerText;
document.getElementById("json_event_"+i).innerText = "";
document.getElementById("json_event_"+i).appendChild(renderjson(JSON.parse(value)));
}
</script>
</head>
<div class="container">
<!-- Topper w/ logo -->
Expand Down
216 changes: 216 additions & 0 deletions web/static/js/renderjson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
// Copyright © 2013-2017 David Caldwell <[email protected]>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

// Usage
// -----
// The module exports one entry point, the `renderjson()` function. It takes in
// the JSON you want to render as a single argument and returns an HTML
// element.
//
// Options
// -------
// renderjson.set_icons("+", "-")
// This Allows you to override the disclosure icons.
//
// renderjson.set_show_to_level(level)
// Pass the number of levels to expand when rendering. The default is 0, which
// starts with everything collapsed. As a special case, if level is the string
// "all" then it will start with everything expanded.
//
// renderjson.set_max_string_length(length)
// Strings will be truncated and made expandable if they are longer than
// `length`. As a special case, if `length` is the string "none" then
// there will be no truncation. The default is "none".
//
// renderjson.set_sort_objects(sort_bool)
// Sort objects by key (default: false)
//
// renderjson.set_replacer(replacer_function)
// Equivalent of JSON.stringify() `replacer` argument when it's a function
//
// renderjson.set_collapse_msg(collapse_function)
// Accepts a function (len:number):string => {} where len is the length of the
// object collapsed. Function should return the message displayed when a
// object is collapsed. The default message is "X items"
//
// renderjson.set_property_list(property_list)
// Equivalent of JSON.stringify() `replacer` argument when it's an array
//
// Theming
// -------
// The HTML output uses a number of classes so that you can theme it the way
// you'd like:
// .disclosure ("⊕", "⊖")
// .syntax (",", ":", "{", "}", "[", "]")
// .string (includes quotes)
// .number
// .boolean
// .key (object key)
// .keyword ("null", "undefined")
// .object.syntax ("{", "}")
// .array.syntax ("[", "]")

var module, window, define, renderjson=(function() {
var themetext = function(/* [class, text]+ */) {
var spans = [];
while (arguments.length)
spans.push(append(span(Array.prototype.shift.call(arguments)),
text(Array.prototype.shift.call(arguments))));
return spans;
};
var append = function(/* el, ... */) {
var el = Array.prototype.shift.call(arguments);
for (var a=0; a<arguments.length; a++)
if (arguments[a].constructor == Array)
append.apply(this, [el].concat(arguments[a]));
else
el.appendChild(arguments[a]);
return el;
};
var prepend = function(el, child) {
el.insertBefore(child, el.firstChild);
return el;
}
var isempty = function(obj, pl) { var keys = pl || Object.keys(obj);
for (var i in keys) if (Object.hasOwnProperty.call(obj, keys[i])) return false;
return true; }
var text = function(txt) { return document.createTextNode(txt) };
var div = function() { return document.createElement("div") };
var span = function(classname) { var s = document.createElement("span");
if (classname) s.className = classname;
return s; };
var A = function A(txt, classname, callback) { var a = document.createElement("a");
if (classname) a.className = classname;
a.appendChild(text(txt));
a.href = '#';
a.onclick = function(e) { callback(); if (e) e.stopPropagation(); return false; };
return a; };

function _renderjson(json, indent, dont_indent, show_level, options) {
var my_indent = dont_indent ? "" : indent;

var disclosure = function(open, placeholder, close, type, builder) {
var content;
var empty = span(type);
var show = function() { if (!content) append(empty.parentNode,
content = prepend(builder(),
A(options.hide, "disclosure",
function() { content.style.display="none";
empty.style.display="inline"; } )));
content.style.display="inline";
empty.style.display="none"; };
append(empty,
A(options.show, "disclosure", show),
themetext(type+ " syntax", open),
A(placeholder, null, show),
themetext(type+ " syntax", close));

var el = append(span(), text(my_indent.slice(0,-1)), empty);
if (show_level > 0 && type != "string")
show();
return el;
};

if (json === null) return themetext(null, my_indent, "keyword", "null");
if (json === void 0) return themetext(null, my_indent, "keyword", "undefined");

if (typeof(json) == "string" && json.length > options.max_string_length)
return disclosure('"', json.substr(0,options.max_string_length)+" ...", '"', "string", function () {
return append(span("string"), themetext(null, my_indent, "string", JSON.stringify(json)));
});

if (typeof(json) != "object" || [Number, String, Boolean, Date].indexOf(json.constructor) >= 0) // Strings, numbers and bools
return themetext(null, my_indent, typeof(json), JSON.stringify(json));

if (json.constructor == Array) {
if (json.length == 0) return themetext(null, my_indent, "array syntax", "[]");

return disclosure("[", options.collapse_msg(json.length), "]", "array", function () {
var as = append(span("array"), themetext("array syntax", "[", null, "\n"));
for (var i=0; i<json.length; i++)
append(as,
_renderjson(options.replacer.call(json, i, json[i]), indent+" ", false, show_level-1, options),
i != json.length-1 ? themetext("syntax", ",") : [],
text("\n"));
append(as, themetext(null, indent, "array syntax", "]"));
return as;
});
}

// object
if (isempty(json, options.property_list))
return themetext(null, my_indent, "object syntax", "{}");

return disclosure("{", options.collapse_msg(Object.keys(json).length), "}", "object", function () {
var os = append(span("object"), themetext("object syntax", "{", null, "\n"));
for (var k in json) var last = k;
var keys = options.property_list || Object.keys(json);
if (options.sort_objects)
keys = keys.sort();
for (var i in keys) {
var k = keys[i];
if (!(k in json)) continue;
append(os, themetext(null, indent+" ", "key", '"'+k+'"', "object syntax", ': '),
_renderjson(options.replacer.call(json, k, json[k]), indent+" ", true, show_level-1, options),
k != last ? themetext("syntax", ",") : [],
text("\n"));
}
append(os, themetext(null, indent, "object syntax", "}"));
return os;
});
}

var renderjson = function renderjson(json)
{
var options = new Object(renderjson.options);
options.replacer = typeof(options.replacer) == "function" ? options.replacer : function(k,v) { return v; };
var pre = append(document.createElement("pre"), _renderjson(json, "", false, options.show_to_level, options));
pre.className = "renderjson";
return pre;
}
renderjson.set_icons = function(show, hide) { renderjson.options.show = show;
renderjson.options.hide = hide;
return renderjson; };
renderjson.set_show_to_level = function(level) { renderjson.options.show_to_level = typeof level == "string" &&
level.toLowerCase() === "all" ? Number.MAX_VALUE
: level;
return renderjson; };
renderjson.set_max_string_length = function(length) { renderjson.options.max_string_length = typeof length == "string" &&
length.toLowerCase() === "none" ? Number.MAX_VALUE
: length;
return renderjson; };
renderjson.set_sort_objects = function(sort_bool) { renderjson.options.sort_objects = sort_bool;
return renderjson; };
renderjson.set_replacer = function(replacer) { renderjson.options.replacer = replacer;
return renderjson; };
renderjson.set_collapse_msg = function(collapse_msg) { renderjson.options.collapse_msg = collapse_msg;
return renderjson; };
renderjson.set_property_list = function(prop_list) { renderjson.options.property_list = prop_list;
return renderjson; };
// Backwards compatiblity. Use set_show_to_level() for new code.
renderjson.set_show_by_default = function(show) { renderjson.options.show_to_level = show ? Number.MAX_VALUE : 0;
return renderjson; };
renderjson.options = {};
renderjson.set_icons('⊕', '⊖');
renderjson.set_show_by_default(false);
renderjson.set_sort_objects(false);
renderjson.set_max_string_length("none");
renderjson.set_replacer(void 0);
renderjson.set_property_list(void 0);
renderjson.set_collapse_msg(function(len) { return len + " item" + (len==1 ? "" : "s") })
return renderjson;
})();

if (define) define({renderjson:renderjson})
else (module||{}).exports = (window||{}).renderjson = renderjson;
Loading