#!/usr/bin/env python
from tqdm import tqdm
import jinja2
import yaml
import json
from os.path import splitext, join
from os import listdir
from datetime import date
OUTPUT_ROOT = "output/"
CCSA_DIR ="CCSA/"
HTML_OUTPUT_DIR = "html/"
RSS_FEED = "rss.xml"
JSON_CATALOG = "CCSA.json"
BASE_URL = "https://0x29a.codeberg.pages/CCSA-DEMO/"
ccsa_html_template = """
Co-op Cloud Security Advisory (DEMO) {{ ccsa_id }}
Co-op Cloud Security Advisory (DEMO) {{ ccsa_id }}
Recipe:: {{ recipe }}
Affected Versions: {% for version in versions %}
{{ version }}{% endfor %}
Affected Images: {% for image in images %}
{{ image }}{% endfor %}
Upgrade paths: {% for upgrade in upgrade_paths %}
{{ upgrade }}{% endfor %}
Description:
{{ description }}
"""
ccsa_html_index_template = """
Co-op Cloud Security Advisories
Co-op Cloud Security Advisories
{% for ccsa in ccsas %}
{{ ccsa.ccsa_id }}: {{ ccsa.recipe }} {{ ccsa.short }}
{% endfor %}
"""
ccsa_rss_template = """
Co-op Cloud Security Advisories (DEMO)
{{ base_url }}
Running feed of Security Advisories related to Co-op Cloud
{% for ccsa in ccsas %}
-
{{ ccsa.ccsa_id }}
{{ ccsa.url }}
{{ ccsa.recipe }} {{ ccsa.short }} {% for upgrade in ccsa.upgrade_paths %}
{{ upgrade }} {% endfor %}
{{ ccsa.date }}
{% endfor %}
"""
class ForceDateTimeEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, date):
return str(obj)
return super().default(obj)
environment = jinja2.Environment()
html_template = environment.from_string(ccsa_html_template)
rss_template = environment.from_string(ccsa_rss_template)
index_template = environment.from_string(ccsa_html_index_template)
ccsas = []
print("parsing CCSAs and writing html advisories")
for f in tqdm(listdir(CCSA_DIR)):
ccsa_yml_path = join(CCSA_DIR, f)
with open(ccsa_yml_path) as fd:
ccsa = yaml.safe_load(fd.read())
ccsa["ccsa_id"] = splitext(f)[0]
html_file_name = f'{ccsa["ccsa_id"]}.html'
ccsa["url"] = f"{BASE_URL}/{html_file_name}"
ccsa["upgrade_paths"] = [f"{v} -> {u}" for v,u in zip(ccsa["versions"], ccsa["upgrade_to"])]
with open(join(OUTPUT_ROOT, html_file_name), "w") as fd:
fd.write(html_template.render(base_url=BASE_URL, **ccsa))
ccsas.append(ccsa)
print("creating RSS feed")
with open(join(OUTPUT_ROOT, RSS_FEED), "w") as fd:
fd.write(rss_template.render(base_url=BASE_URL, ccsas=ccsas))
print("creating json catalog")
with open(join(OUTPUT_ROOT, JSON_CATALOG), "w") as fd:
fd.write(json.dumps(ccsas, cls=ForceDateTimeEncoder))
print("creating index")
with open(join(OUTPUT_ROOT, "index.html"), "w") as fd:
fd.write(index_template.render(ccsas=ccsas))
print("complete")