-
Notifications
You must be signed in to change notification settings - Fork 717
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use jinja templates to render the policies page.
- Loading branch information
Showing
3 changed files
with
98 additions
and
17 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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#!/usr/bin/python3 | ||
|
||
import argparse | ||
import os | ||
import pathlib | ||
import sys | ||
import yaml | ||
from collections import namedtuple | ||
|
||
import ssg.jinja | ||
from utils.template_renderer import FlexibleLoader | ||
from utils.gen_html_guides_index import create_index | ||
|
||
# Helper script used to generate an HTML page to display guides. | ||
|
||
Product = namedtuple("Product", ["id", "name"]) | ||
Policy = namedtuple("Policy", ["id", "name"]) | ||
|
||
|
||
def get_control_files(ssg_root): | ||
control_files = [] | ||
p = pathlib.Path(ssg_root) | ||
for control_file in p.glob("controls/*.yml"): | ||
# only process files, ignore controls directories | ||
if not os.path.isfile(control_file): | ||
continue | ||
policy_id = pathlib.Path(control_file).stem | ||
with open(control_file, "r") as f: | ||
policy_yaml = yaml.full_load(f) | ||
policy_name = policy_yaml["policy"] | ||
policy = Policy(id=policy_id, name=policy_name) | ||
control_files.append(policy) | ||
return control_files | ||
|
||
|
||
def get_data(ssg_root): | ||
products = [] | ||
p = pathlib.Path(ssg_root) | ||
for product_file in p.glob("products/**/product.yml"): | ||
product_dir = product_file.parent | ||
product_id = product_dir.name | ||
|
||
# skip if there are not built rendered-polices | ||
if not os.path.isdir(os.path.join(ssg_root, "build", product_id, "rendered-policies")): | ||
continue | ||
with open(product_file, "r") as f: | ||
product_yaml = yaml.full_load(f) | ||
product_name = product_yaml["full_name"] | ||
product = Product(id=product_id, name=product_name) | ||
products.append(product) | ||
data = {"products": products} | ||
return data | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
"ssg_root", | ||
help="Path to the root directory of scap-security-guide") | ||
parser.add_argument( | ||
"output", | ||
help="Path where the output HTML file should be generated") | ||
args = parser.parse_args() | ||
data = get_data(args.ssg_root) | ||
data.update({"control_files": get_control_files(args.ssg_root)}) | ||
create_index(data, "html_rendered_policies_index_template.html", args.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
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 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"/> | ||
<title>Rendered Policies</title> | ||
<style> | ||
</style> | ||
</head> | ||
<body> | ||
|
||
<h1>Rendered Policies</h1> | ||
|
||
{{% for product in products|sort(attribute="name") %}} | ||
<h4>{{{ product.name }}}</h4> | ||
<ul> | ||
{{% for control_file in control_files|sort(attribute="id") %}} | ||
<li> | ||
<a href="{{{ product.id }}}/{{{ control_file.id }}}.html">{{{ control_file.id }}}: {{{ control_file.name }}}</a> | ||
</li> | ||
{{% endfor %}} | ||
</ul> | ||
{{% endfor %}} | ||
|
||
</body> | ||
</html> |