atomic-threat-coverage/scripts/loggingpolicy.py
2019-03-01 10:27:02 +00:00

81 lines
2.5 KiB
Python
Executable File

#!/usr/bin/env python3
from atcutils import ATCutils
from jinja2 import Environment, FileSystemLoader
import os
from pdb import set_trace as bp
# ########################################################################### #
# ########################### Logging Policy ################################ #
# ########################################################################### #
ATCconfig = ATCutils.load_config("config.yml")
class LoggingPolicy:
"""Class for the Logging Policy entity"""
def __init__(self, yaml_file):
"""Init method"""
# Init vars
self.yaml_file = yaml_file
# The name of the directory containing future markdown LogginPolicy
self.parent_title = "Logging_Policies"
# Init methods
self.parse_into_fields(self.yaml_file)
def parse_into_fields(self, yaml_file):
"""Description"""
# self.fields contains parsed fields obtained from yaml file
self.fields = ATCutils.read_yaml_file(self.yaml_file)
def render_template(self, template_type):
"""Description
template_type:
- "markdown"
- "confluence"
"""
if template_type not in ["markdown", "confluence"]:
raise Exception(
"Bad template_type. Available values:" +
" [\"markdown\", \"confluence\"]")
# Point to the templates directory
env = Environment(loader=FileSystemLoader('templates'))
# Get proper template
if template_type == "markdown":
template = env.get_template(
'markdown_loggingpolicy_template.md.j2')
elif template_type == "confluence":
template = env.get_template(
'confluence_loggingpolicy_template.html.j2')
self.fields.update(
{'confluence_viewpage_url': ATCconfig.get('confluence_viewpage_url')})
# get rid of newline to not mess with table in md
self.fields.update(
{'description': self.fields.get('description').strip()})
self.content = template.render(self.fields)
return True
def save_markdown_file(self, atc_dir='../Atomic_Threat_Coverage/'):
"""Write content (md template filled with data) to a file"""
base = os.path.basename(self.yaml_file)
title = os.path.splitext(base)[0]
file_path = atc_dir + self.parent_title + "/" + \
title + ".md"
return ATCutils.write_file(file_path, self.content)