-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from multiflexi/email_presenter
Add message presenter, updated .pre-commit-config.yaml - EMAIL UPDATE 1/2
- Loading branch information
Showing
7 changed files
with
324 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,23 @@ | ||
|
||
repos: | ||
- repo: https://github.com/psf/black | ||
rev: 22.6.0 | ||
rev: 23.3.0 | ||
hooks: | ||
- id: black | ||
language_version: python3 | ||
args: [--line-length=142] | ||
|
||
- repo: https://github.com/PyCQA/flake8 | ||
rev: 6.0.0 | ||
hooks: | ||
- id: flake8 | ||
additional_dependencies: [flake8-docstrings] | ||
args: [--max-line-length=142] | ||
types: ['python'] | ||
|
||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v4.3.0 | ||
rev: v4.4.0 | ||
hooks: | ||
- id: trailing-whitespace | ||
- id: check-yaml | ||
- id: end-of-file-fixer | ||
- id: trailing-whitespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
"""Create a message presenter. | ||
Returns: | ||
_description_ | ||
""" | ||
import os | ||
from base64 import b64encode | ||
import jinja2 | ||
|
||
from .base_presenter import BasePresenter | ||
from shared.schema.parameter import Parameter, ParameterType | ||
|
||
|
||
class MESSAGEPresenter(BasePresenter): | ||
"""Class for MESSAGE presenter. | ||
Arguments: | ||
BasePresenter -- Superclass | ||
Returns: | ||
_description_ | ||
""" | ||
|
||
type = "MESSAGE_PRESENTER" | ||
name = "MESSAGE Presenter" | ||
description = "Presenter for generating message title and body" | ||
|
||
parameters = [ | ||
Parameter(0, "TITLE_TEMPLATE_PATH", "Title template", "Path of message title template file", ParameterType.STRING), | ||
Parameter(0, "BODY_TEMPLATE_PATH", "Body template", "Path to message body template file", ParameterType.STRING), | ||
] | ||
|
||
parameters.extend(BasePresenter.parameters) | ||
|
||
def generate(self, presenter_input): | ||
"""Generate message parts from Jinja templates. | ||
Arguments: | ||
presenter_input -- Input data for templating | ||
Returns: | ||
presenter_output -- dict with keys mime_type and data with message parts as subkeys | ||
""" | ||
message_title_template_path = presenter_input.parameter_values_map["TITLE_TEMPLATE_PATH"] | ||
message_body_template_path = presenter_input.parameter_values_map["BODY_TEMPLATE_PATH"] | ||
presenter_output = {"mime_type": "text/plain", "message_title": None, "message_body": None, "data": None} | ||
|
||
def generate_part(template_path): | ||
head, tail = os.path.split(template_path) | ||
input_data = BasePresenter.generate_input_data(presenter_input) | ||
env = jinja2.Environment(loader=jinja2.FileSystemLoader(head)) | ||
func_dict = { | ||
"vars": vars, | ||
} | ||
template = env.get_template(tail) | ||
template.globals.update(func_dict) | ||
output_text = template.render(data=input_data).encode() | ||
base64_bytes = b64encode(output_text) | ||
data = base64_bytes.decode("UTF-8") | ||
return data | ||
|
||
try: | ||
presenter_output["message_title"] = generate_part(message_title_template_path) | ||
presenter_output["message_body"] = generate_part(message_body_template_path) | ||
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")} | ||
return presenter_output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Hi, | ||
|
||
{{ data.product.description }} | ||
{% for report_item in data.report_items %}{% if report_item.type != "Vulnerability Report" %}This template cannot be used for item of type "{{ report_item.type }}". It can only handle "Vulnerability Report".{% else %} | ||
|
||
{{ report_item.name }} | ||
|
||
{{ report_item.attrs.description }} | ||
|
||
{% if report_item.attrs.recommendations %} | ||
### Recommendations | ||
|
||
{{ report_item.attrs.recommendations }} | ||
|
||
{% endif %} | ||
### CVE: {% for cve in report_item.attrs.cve %}{{ cve }}{{ ", " if not loop.last else "" }}{% endfor %} | ||
{% if report_item.attrs.links %} | ||
### Links | ||
{% for entry in report_item.attrs.links %} | ||
[{{ report_item.attrs.link_prefix }}{{ loop.index }}] - {{ entry }} | ||
{% endfor %}{% endif %}{% endif %}{% endfor %} | ||
-- | ||
{{ data.product.user.name }} | ||
Cyber Security Team | ||
Acme Corporation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[TLP: {{ data.product.max_tlp }}] {{ data.product.title }} |
Oops, something went wrong.