Skip to content

Commit

Permalink
Use jinja templates to render the policies page.
Browse files Browse the repository at this point in the history
  • Loading branch information
ggbecker committed May 23, 2023
1 parent 8ef6b6a commit 45241b1
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 17 deletions.
66 changes: 66 additions & 0 deletions utils/gen_rendered_policies_index.py
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)
24 changes: 7 additions & 17 deletions utils/generate_html_pages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,35 +82,25 @@ echo "</html>" >> index.html
popd



# Generate Rendered Policies page
POLICY_DIR="$PAGES_DIR/rendered-policies"
mkdir -p "$POLICY_DIR"
touch "$POLICY_DIR/index.html"
echo "<!DOCTYPE html>" > "$POLICY_DIR/index.html"
echo '<html lang="en">' >> "$POLICY_DIR/index.html"
echo "<head>" >> "$POLICY_DIR/index.html"
echo '<meta charset="utf-8" />' >> "$POLICY_DIR/index.html"
echo "<title>Rendered Policies</title>" >> "$POLICY_DIR/index.html"
echo "</head>" >> "$POLICY_DIR/index.html"
echo "<body>" >> "$POLICY_DIR/index.html"
echo "<h1>Rendered Policies</h1>" >> "$POLICY_DIR/index.html"
# get supported products
products=$(echo -e "import ssg.constants\nprint(ssg.constants.product_directories)" | python3 | sed -s "s/'//g; s/,//g; s/\[//g; s/\]//g")
for product in $products
do
if [ -d build/$product ]; then
echo "<h4>Product: ${product}</h4>" >> "$POLICY_DIR/index.html"
echo "<ul>" >> "$POLICY_DIR/index.html"
mkdir -p "$POLICY_DIR/$product"
if [ -d "build/$product/rendered-policies/" ]; then
cp -rf "build/${product}/rendered-policies/"* "$POLICY_DIR/$product/"
echo "<li><a href=\"$product/\">Rendered Policies</a></li>" >> "$POLICY_DIR/index.html"
fi
echo "</ul>" >> "$POLICY_DIR/index.html"
fi
done
echo "</body>" >> "$POLICY_DIR/index.html"
echo "</html>" >> "$POLICY_DIR/index.html"
utils/gen_rendered_policies_index.py . "$PAGES_DIR/rendered-policies/index.html"
retVal=$?
if [ $retVal -ne 0 ]; then
echo "Something wrong happened while generating the HTML Rendered Policy Index page"
exit 1
fi

pushd $PAGES_DIR
touch index.html
Expand Down
25 changes: 25 additions & 0 deletions utils/html_rendered_policies_index_template.html
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>

0 comments on commit 45241b1

Please sign in to comment.