dashboard init support

This commit is contained in:
yugoslavskiy 2019-03-23 00:21:07 +01:00
parent fcbee66d31
commit 458fa8e64a
3 changed files with 120 additions and 21 deletions

View File

@ -328,16 +328,15 @@ class BaseKibanaVisualizationObject(BaseKibana):
return str(self.__call__())
class BasePanelsJson(BaseKibana):
"""Base panelsJSON"""
class BaseGridData(BaseKibana):
"""Base gridData"""
def __init__(self):
self.embeddableConfig = dict()
self.gridData = dict()
self.id = str()
self.panelIndex = str()
self.type = str()
self.version = str()
def __init__(self, x=0, y=0, w=0, h=0, vid=1):
self.w = w
self.h = h
self.x = x
self.y = y
self.i = str(vid)
def validate(self):
# TODO: Write validate method
@ -351,15 +350,23 @@ class BasePanelsJson(BaseKibana):
return str(self.__call__())
class BaseGridData(BaseKibana):
"""Base gridData"""
class BasePanelsJson(BaseKibana):
"""Base panelsJSON"""
def __init__(self):
self.w = 0
self.h = 0
self.x = 0
self.y = 0
self.i = str()
def __init__(self, vis_uuid=None, vis_id=None, version="6.6.2", type="visualization"):
self.embeddableConfig = dict()
self.gridData = BaseGridData()
self.id = str()
self.panelIndex = str()
self.type = type
self.version = version
if vis_uuid:
self.id = vis_uuid
if vis_id:
self.panelIndex = str(vis_id)
self.gridData.i = str(vis_id)
def validate(self):
# TODO: Write validate method

View File

@ -1,20 +1,26 @@
#!/usr/bin/env python3
import base
import json
import uuid
from ast import literal_eval
class BaseKibanaDashboardObject(base.BaseKibana):
class KibanaDashboardObject(base.BaseKibana):
"""Base Kibana DashboardObject"""
def __init__(self, title=None):
self.title = str()
self.description = str()
self.panelsJSON = str() # To je wazne
self.optionsJSON = str() # to tyz
self.panelsJSON = list() # double escaping
self.optionsJSON = str() # double escaping
self.timeRestore = bool()
self.kibanaSavedObjectMeta = dict()
self.version = 1
self.hits = 0
self._id = 1
if title:
self.title = title
@ -29,3 +35,26 @@ class BaseKibanaDashboardObject(base.BaseKibana):
def __repr__(self):
return str(self.__call__())
def json_export(self, return_dict=False):
_tmp = {}
_tmp["_source"] = literal_eval(str(self.__dict__))
_tmp["_id"] = str(uuid.uuid4())
_tmp["_type"] = "dashboard"
_tmp.pop("_id", None)
_tmp["_source"]["panelsJSON"] = json.dumps(_tmp["_source"]["panelsJSON"])
_tmp["_source"]["optionsJSON"] = json.dumps(_tmp["_source"]["optionsJSON"])
if return_dict:
return _tmp
else:
return json.dumps(_tmp)
def add_visualization(self, vis, x=0, y=0, w=0, h=0):
_vis = base.BasePanelsJson(vid=vis.uuid)
_vis.gridData.x = x
_vis.gridData.y = y
_vis.gridData.w = w
_vis.gridData.h = h
self.panelsJSON.append(_vis)
self._id += 1

View File

@ -5,6 +5,11 @@ import visualisation
import metrics
import argparse
import json
import dashboard
import base
from os import listdir
from os.path import isfile, join
"""
type: 1
@ -67,10 +72,12 @@ class YamlHandler:
if _type == "visualization":
self.visualization(yaml_document)
elif _type == "dashboard":
self.dashboard(yaml_document)
else:
raise Exception("Not supported yet. Sorry!")
def visualization(self, yaml_document):
def visualization_f(self, yaml_document):
self._name = yaml_document.get('name')
_title = yaml_document.get('title')
_saved_search_id = yaml_document.get('saved_search_id')
@ -136,6 +143,39 @@ class YamlHandler:
_vis.add_metric(_metric)
self._results.append(_vis.json_export(return_dict=True))
def dashboard(self, yaml_document):
if not yaml_document.get('visualizations'):
raise Exception("No visualizations, no sense. Provide it!")
_title = yaml_document.get('title')
_visualizations = yaml_document.get('visualizations')
if not _title:
raise Exception("Provide title")
if not isinstance(_visualizations, list):
raise Exception("visualizations var needs to be a list")
_dashboard = dashboard.KibanaDashboardObject()
_dashboard.title = _title
_dashboard.kibanaSavedObjectMeta = {
"searchSourceJSON": "{\"query\":{\"query\":\"\"," +
"\"language\":\"lucene\"},\"filter\":[]}"
}
_dashboard.panelsJSON = base.BasePanelsJson()
_dashboard.panelsJSON.gridData = base.BaseGridData()
_dashboard.optionsJSON = base.BaseOptionsJson()
visualization_objects_list = self.load_yamls("/tmp/atomic-threat-coverage/scripts/KibanaStuff/visualizations/")
for visualization in visualization_objects_list:
if visualization not in _dashboard.visualization:
continue
self.visualization(visualization)
_dashboard.add_visualization(visualization)
self._results.append(_dashboard.json_export(return_dict=True))
def handle_metric(self, id, metric_name, args=None):
if metric_name not in self._general_metrics:
raise Exception("Metric not supported")
@ -335,6 +375,29 @@ class YamlHandler:
return True
return False
def load_yamls(self, path):
"""Load multiple yamls into list"""
yamls = [
join(path, f) for f in listdir(path)
if isfile(join(path, f))
if f.endswith('.yaml')
or f.endswith('.yml')
]
result = []
for yaml in yamls:
try:
print(yaml)
_ = yaml.load_all(yaml)
_["uuid"] = uuid.uuid4()
result.append(_)
except ScannerError:
raise ScannerError('yaml is bad! %s' % yaml)
return result
def main():
parser = argparse.ArgumentParser(description='Visualizations')