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

Improve presenter stability #255

Merged
merged 2 commits into from
Apr 11, 2024
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
18 changes: 11 additions & 7 deletions src/presenters/presenters/base_presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def get_max_tlp(self, reports):
color_values = {"WHITE": 0, "CLEAR": 1, "GREEN": 2, "AMBER": 3, "AMBER+STRICT": 4, "RED": 5}
colors = []
for report in reports:
if report.type.startswith("Vulnerability Report"):
if hasattr(report.attrs, "tlp"):
colors.append(report.attrs.tlp)
if colors:
max_tlp = max(colors, key=lambda color: color_values.get(color, 1))
Expand Down Expand Up @@ -184,21 +184,21 @@ def __init__(self, presenter_input):
presenter_input -- input data
"""
# types of report items (e.g. vuln report, disinfo report)
report_types = dict()
report_types = {}
for report_type in presenter_input.report_types:
# index by ID
report_types[report_type.id] = report_type

# attributes that can be used in report items (for internal use)
attribute_map = dict()
attribute_map = {}
for report_type in presenter_input.report_types:
for attribute_group in report_type.attribute_groups:
for attribute_group_item in attribute_group.attribute_group_items:
attribute_map[attribute_group_item.id] = attribute_group_item

self.product = presenter_input.product
self.product.date = datetime.datetime.now()
self.report_items = list()
self.report_items = []

for report in presenter_input.reports:
self.report_items.append(BasePresenter.ReportItemObject(report, report_types, attribute_map))
Expand All @@ -209,12 +209,16 @@ def __init__(self, presenter_input):
for report in self.report_items:
if report.type.startswith("Vulnerability Report"):
vul_report_count += 1
if report.attrs.links:
if hasattr(report.attrs, "links"):
for link in report.attrs.links:
if link not in product_links:
product_links.append(link)
report.attrs.description = self.link_renumbering(report.attrs.description, report.attrs.links, product_links)
report.attrs.recommendations = self.link_renumbering(report.attrs.recommendations, report.attrs.links, product_links)
if hasattr(report.attrs, "description"):
report.attrs.description = self.link_renumbering(report.attrs.description, report.attrs.links, product_links)
if hasattr(report.attrs, "recommendations"):
report.attrs.recommendations = self.link_renumbering(
report.attrs.recommendations, report.attrs.links, product_links
)
# If there are vulnerability reports, set the max TLP and product links
if vul_report_count > 0:
self.product.max_tlp = self.get_max_tlp(self.report_items)
Expand Down
39 changes: 25 additions & 14 deletions src/presenters/presenters/html_presenter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
"""HTML Presenter.

Returns:
A dictionary containing the MIME type and the base64-encoded HTML data.
"""
import os
from base64 import b64encode
import jinja2
Expand All @@ -8,37 +12,44 @@


class HTMLPresenter(BasePresenter):
"""Presenter for generating HTML documents.

Arguments:
BasePresenter -- The base presenter class.

Returns:
A dictionary containing the MIME type and the base64-encoded HTML data.
"""

type = "HTML_PRESENTER"
name = "HTML Presenter"
description = "Presenter for generating html documents"

parameters = [
Parameter(0, "HTML_TEMPLATE_PATH", "HTML template with its path", "Path of html template file",
ParameterType.STRING)
]
parameters = [Parameter(0, "HTML_TEMPLATE_PATH", "HTML template with its path", "Path of html template file", ParameterType.STRING)]

parameters.extend(BasePresenter.parameters)

def generate(self, presenter_input):
"""Generate the HTML presentation.

Arguments:
presenter_input -- The input data for the presenter.

Returns:
A dictionary containing the MIME type and the base64-encoded HTML data.
"""
try:
head, tail = os.path.split(presenter_input.parameter_values_map['HTML_TEMPLATE_PATH'])
head, tail = os.path.split(presenter_input.parameter_values_map["HTML_TEMPLATE_PATH"])
input_data = BasePresenter.generate_input_data(presenter_input)
env = jinja2.Environment(loader=jinja2.FileSystemLoader(head))
env.filters["strfdate"] = BasePresenter._filter_datetime
output_text = env.get_template(tail).render(data=input_data).encode()
base64_bytes = b64encode(output_text)
data = base64_bytes.decode('UTF-8')
data = base64_bytes.decode("UTF-8")

presenter_output = {
'mime_type': 'text/html',
'data': data
}
presenter_output = {"mime_type": "text/html", "data": data}
return presenter_output
except Exception as error:
BasePresenter.print_exception(self, error)
presenter_output = {
'mime_type': 'text/plain',
'data': b64encode(("TEMPLATING ERROR\n"+str(error)).encode()).decode('UTF-8')
}
presenter_output = {"mime_type": "text/plain", "data": b64encode((f"TEMPLATING ERROR\n{error}").encode()).decode("UTF-8")}
return presenter_output
3 changes: 2 additions & 1 deletion src/presenters/presenters/message_presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Returns:
_description_
"""

import os
from base64 import b64encode
import jinja2
Expand Down Expand Up @@ -66,5 +67,5 @@ def generate_part(template_path):

except Exception as error:
BasePresenter.print_exception(self, error)
presenter_output = {"mime_type": "text/plain", "data": b64encode(("TEMPLATING ERROR\n" + str(error)).encode()).decode("UTF-8")}
presenter_output = {"mime_type": "text/plain", "data": b64encode((f"TEMPLATING ERROR\n{error}").encode()).decode("UTF-8")}
return presenter_output
42 changes: 28 additions & 14 deletions src/presenters/presenters/misp_presenter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"""Module for MISP presenter.

Returns:
dict: The presenter output containing the MIME type and data.
"""
import os
from base64 import b64encode
import jinja2
Expand All @@ -7,21 +12,36 @@


class MISPPresenter(BasePresenter):
"""Presenter class for generating MISP platform.

This presenter is responsible for generating the MISP platform using a template file.

Arguments:
BasePresenter (class): The base presenter class.

Returns:
dict: The presenter output containing the MIME type and data.
"""

type = "MISP_PRESENTER"
name = "MISP Presenter"
description = "Presenter for generating MISP platform"

parameters = [
Parameter(0, "MISP_TEMPLATE_PATH", "MISP template with its path", "Path of MISP template file",
ParameterType.STRING)
]
parameters = [Parameter(0, "MISP_TEMPLATE_PATH", "MISP template with its path", "Path of MISP template file", ParameterType.STRING)]

parameters.extend(BasePresenter.parameters)

def generate(self, presenter_input):
"""Generate the output data based on the presenter input.

Arguments:
presenter_input (PresenterInput): The input data for the presenter.

Returns:
dict: The presenter output containing the mime type and data.
"""
try:
head, tail = os.path.split(presenter_input.parameter_values_map['MISP_TEMPLATE_PATH'])
head, tail = os.path.split(presenter_input.parameter_values_map["MISP_TEMPLATE_PATH"])

input_data = BasePresenter.generate_input_data(presenter_input)

Expand All @@ -31,18 +51,12 @@ def generate(self, presenter_input):

base64_bytes = b64encode(output_text)

data = base64_bytes.decode('UTF-8')
data = base64_bytes.decode("UTF-8")

presenter_output = {
'mime_type': 'application/json',
'data': data
}
presenter_output = {"mime_type": "application/json", "data": data}

return presenter_output
except Exception as error:
BasePresenter.print_exception(self, error)
presenter_output = {
'mime_type': 'text/plain',
'data': b64encode(("TEMPLATING ERROR\n"+str(error)).encode()).decode('UTF-8')
}
presenter_output = {"mime_type": "text/plain", "data": b64encode((f"TEMPLATING ERROR\n{error}").encode()).decode("UTF-8")}
return presenter_output
42 changes: 28 additions & 14 deletions src/presenters/presenters/text_presenter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"""Module for the TEXT presenter.

Returns:
dict: The presenter output containing the mime type and data of the generated text document.
"""
import os
from base64 import b64encode
import jinja2
Expand All @@ -7,21 +12,36 @@


class TEXTPresenter(BasePresenter):
"""Presenter for generating text documents.

This presenter is responsible for generating text documents based on a text template.

Arguments:
BasePresenter (class): The base presenter class.

Returns:
dict: The presenter output containing the mime type and data of the generated text document.
"""

type = "TEXT_PRESENTER"
name = "TEXT Presenter"
description = "Presenter for generating text documents"

parameters = [
Parameter(0, "TEXT_TEMPLATE_PATH", "TEXT template with its path", "Path of text template file",
ParameterType.STRING)
]
parameters = [Parameter(0, "TEXT_TEMPLATE_PATH", "TEXT template with its path", "Path of text template file", ParameterType.STRING)]

parameters.extend(BasePresenter.parameters)

def generate(self, presenter_input):
"""Generate the output text based on the presenter input.

Arguments:
presenter_input (PresenterInput): The input data for the presenter.

Returns:
dict: The presenter output containing the mime type and the generated text data.
"""
try:
head, tail = os.path.split(presenter_input.parameter_values_map['TEXT_TEMPLATE_PATH'])
head, tail = os.path.split(presenter_input.parameter_values_map["TEXT_TEMPLATE_PATH"])

input_data = BasePresenter.generate_input_data(presenter_input)

Expand All @@ -39,18 +59,12 @@ def generate(self, presenter_input):

base64_bytes = b64encode(output_text)

data = base64_bytes.decode('UTF-8')
data = base64_bytes.decode("UTF-8")

presenter_output = {
'mime_type': 'text/plain',
'data': data
}
presenter_output = {"mime_type": "text/plain", "data": data}

return presenter_output
except Exception as error:
BasePresenter.print_exception(self, error)
presenter_output = {
'mime_type': 'text/plain',
'data': b64encode(("TEMPLATING ERROR\n"+str(error)).encode()).decode('UTF-8')
}
presenter_output = {"mime_type": "text/plain", "data": b64encode((f"TEMPLATING ERROR\n{error}").encode()).decode("UTF-8")}
return presenter_output
Loading